]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/mcookie.c
1 /* mcookie.c -- Generates random numbers for xauth
2 * Created: Fri Feb 3 10:42:48 1995 by faith@cs.unc.edu
3 * Revised: Fri Mar 19 07:48:01 1999 by faith@acm.org
4 * Public Domain 1995, 1999 Rickard E. Faith (faith@acm.org)
5 * This program comes with ABSOLUTELY NO WARRANTY.
7 * $Id: mcookie.c,v 1.5 1997/07/06 00:13:06 aebr Exp $
9 * This program gathers some random bits of data and used the MD5
10 * message-digest algorithm to generate a 128-bit hexadecimal number for
13 * NOTE: Unless /dev/random is available, this program does not actually
14 * gather 128 bits of random information, so the magic cookie generated
15 * will be considerably easier to guess than one might expect.
17 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
18 * - added Native Language Support
19 * 1999-03-21 aeb: Added some fragments of code from Colin Plumb.
24 #define HAVE_GETTIMEOFDAY 1
31 #ifdef HAVE_GETTIMEOFDAY
37 #define BUFFERSIZE 4096
41 int minlength
, maxlength
;
43 { "/dev/random", 16, 16 }, /* 16 bytes = 128 bits suffice */
44 { "/proc/interrupts", 0, 0 },
45 { "/proc/slabinfo", 0, 0 },
46 { "/proc/stat", 0, 0 },
47 { "/dev/urandom", 32, 64 },
49 #define RNGS (sizeof(rngs)/sizeof(struct rngs))
53 /* The basic function to hash a file */
55 hash_file(struct MD5Context
*ctx
, int fd
)
59 unsigned char buf
[BUFFERSIZE
];
61 while ((r
= read(fd
, buf
, sizeof(buf
))) > 0) {
62 MD5Update(ctx
, buf
, r
);
65 /* Separate files with a null byte */
67 MD5Update(ctx
, buf
, 1);
71 int main( int argc
, char **argv
)
74 struct MD5Context ctx
;
75 unsigned char digest
[16];
76 unsigned char buf
[BUFFERSIZE
];
82 #ifdef HAVE_GETTIMEOFDAY
89 setlocale(LC_ALL
, "");
90 bindtextdomain(PACKAGE
, LOCALEDIR
);
93 while ((c
= getopt( argc
, argv
, "vf:" )) != -1)
95 case 'v': ++Verbose
; break;
96 case 'f': file
= optarg
; break;
101 #ifdef HAVE_GETTIMEOFDAY
102 gettimeofday( &tv
, &tz
);
103 MD5Update( &ctx
, (unsigned char *)&tv
, sizeof( tv
) );
106 MD5Update( &ctx
, (unsigned char *)&t
, sizeof( t
) );
109 MD5Update( &ctx
, (unsigned char *)&pid
, sizeof( pid
));
111 MD5Update( &ctx
, (unsigned char *)&pid
, sizeof( pid
));
116 if (file
[0] == '-' && !file
[1])
119 fd
= open( file
, O_RDONLY
);
122 fprintf( stderr
, _("Could not open %s\n"), file
);
124 count
= hash_file( &ctx
, fd
);
126 fprintf( stderr
, _("Got %d bytes from %s\n"), count
, file
);
128 if (file
[0] != '-' || file
[1]) close( fd
);
132 for (i
= 0; i
< RNGS
; i
++) {
133 if ((fd
= open( rngs
[i
].path
, O_RDONLY
|O_NONBLOCK
)) >= 0) {
134 int count
= sizeof(buf
);
136 if (rngs
[i
].maxlength
&& count
> rngs
[i
].maxlength
)
137 count
= rngs
[i
].maxlength
;
138 r
= read( fd
, buf
, count
);
140 MD5Update( &ctx
, buf
, r
);
145 fprintf( stderr
, _("Got %d bytes from %s\n"), r
, rngs
[i
].path
);
146 if (rngs
[i
].minlength
&& r
>= rngs
[i
].minlength
)
149 fprintf( stderr
, _("Could not open %s\n"), rngs
[i
].path
);
152 MD5Final( digest
, &ctx
);
153 for (i
= 0; i
< 16; i
++) printf( "%02x", digest
[i
] );
157 * The following is important for cases like disk full, so shell scripts
158 * can bomb out properly rather than think they succeeded.
160 if (fflush(stdout
) < 0 || fclose(stdout
) < 0)