tinc/src/genauth.c

107 lines
2.4 KiB
C
Raw Normal View History

2000-03-26 00:33:07 +00:00
/*
genauth.c -- generate a random passphrase
2000-05-14 12:22:42 +00:00
Copyright (C) 1998,1999,2000 Ivo Timmermans <zarq@iname.com>
2000-03-26 00:33:07 +00:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
2000-05-31 18:21:27 +00:00
$Id: genauth.c,v 1.7 2000/05/31 18:21:27 zarq Exp $
2000-03-26 00:33:07 +00:00
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <xalloc.h>
#include "encr.h"
2000-05-29 21:01:26 +00:00
#include "system.h"
2000-03-26 00:33:07 +00:00
unsigned char initvec[] = { 0x22, 0x7b, 0xad, 0x55, 0x41, 0xf4, 0x3e, 0xf3 };
int main(int argc, char **argv)
{
FILE *fp;
int bits, c, i, bytes;
unsigned char *p;
2000-05-31 18:21:27 +00:00
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
if(argc > 2 || (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))))
2000-03-26 00:33:07 +00:00
{
2000-05-29 21:01:26 +00:00
fprintf(stderr, _("Usage: %s bits\n"), argv[0]);
2000-03-26 00:33:07 +00:00
return 1;
}
if(!argv[1])
argv[1] = "1024";
2000-03-26 00:33:07 +00:00
if(!(bits = atol(argv[1])))
{
2000-05-29 21:01:26 +00:00
fprintf(stderr, _("Illegal number: %s\n"), argv[1]);
2000-03-26 00:33:07 +00:00
return 1;
}
bits = ((bits - 1) | 63) + 1;
2000-05-29 21:01:26 +00:00
fprintf(stderr, _("Generating %d bits number"), bits);
2000-03-26 00:33:07 +00:00
bytes = bits >> 3;
if((fp = fopen("/dev/urandom", "r")) == NULL)
{
2000-05-29 21:01:26 +00:00
perror(_("Opening /dev/urandom"));
2000-03-26 00:33:07 +00:00
return 1;
}
p = xmalloc(bytes);
setbuf(stdout, NULL);
for(i = 0; i < bytes; i++)
2000-03-26 00:33:07 +00:00
{
c = fgetc(fp);
if(feof(fp))
{
puts("");
2000-05-29 21:01:26 +00:00
fprintf(stderr, _("File was empty!\n"));
2000-03-26 00:33:07 +00:00
}
p[i] = c;
}
fclose(fp);
if(isatty(1))
{
2000-05-29 21:01:26 +00:00
fprintf(stderr, _(": done.\nThe following line should be ENTIRELY copied into a passphrase file:\n"));
printf("%d ", bits);
for(i = 0; i < bytes; i++)
printf("%02x", p[i]);
puts("");
}
else
{
printf("%d ", bits);
for(i = 0; i < bytes; i++)
printf("%02x", p[i]);
puts("");
2000-05-29 21:01:26 +00:00
fprintf(stderr, _(": done.\n"));
}
2000-03-26 00:33:07 +00:00
return 0;
}