diff options
author | Karel Zak | 2006-12-07 00:25:33 +0100 |
---|---|---|
committer | Karel Zak | 2006-12-07 00:25:33 +0100 |
commit | 726f69e29ca9d4842f3acb20fffd2466fda62c09 (patch) | |
tree | abbc1b6e9bfb0dfe32e81a83648e261ccb2d5a5f /misc-utils/mcookie.c | |
parent | Imported from util-linux-2.2 tarball. (diff) | |
download | kernel-qcow2-util-linux-726f69e29ca9d4842f3acb20fffd2466fda62c09.tar.gz kernel-qcow2-util-linux-726f69e29ca9d4842f3acb20fffd2466fda62c09.tar.xz kernel-qcow2-util-linux-726f69e29ca9d4842f3acb20fffd2466fda62c09.zip |
Imported from util-linux-2.5 tarball.
Diffstat (limited to 'misc-utils/mcookie.c')
-rw-r--r-- | misc-utils/mcookie.c | 110 |
1 files changed, 93 insertions, 17 deletions
diff --git a/misc-utils/mcookie.c b/misc-utils/mcookie.c index d0730edde..a4c896075 100644 --- a/misc-utils/mcookie.c +++ b/misc-utils/mcookie.c @@ -1,43 +1,119 @@ /* mcookie.c -- Generates random numbers for xauth * Created: Fri Feb 3 10:42:48 1995 by faith@cs.unc.edu - * Revised: Sun Feb 12 20:29:58 1995 by faith@cs.unc.edu + * Revised: Mon Sep 25 23:44:43 1995 by r.faith@ieee.org * Public Domain 1995 Rickard E. Faith (faith@cs.unc.edu) * This program comes with ABSOLUTELY NO WARRANTY. * - * mcookie.c,v 1.1.1.1 1995/02/22 19:09:16 faith Exp + * $Id: mcookie.c,v 1.2 1995/10/07 01:32:00 faith Exp $ + * + * This program gathers some random bits of data and used the MD5 + * message-digest algorithm to generate a 128-bit hexadecimal number for + * use with xauth(1). + * + * NOTE: Unless /dev/random is available, this program does not actually + * gather 128 bits of random information, so the magic cookie generated + * will be considerably easier to guess than one might expect. */ -#define SECURE 1 +#ifdef __linux__ +#define HAVE_GETTIMEOFDAY 1 +#endif #include <stdio.h> #include <stdlib.h> -#if SECURE +#include <fcntl.h> +#include "md5.h" +#if HAVE_GETTIMEOFDAY #include <sys/time.h> #include <unistd.h> #endif -int main( void ) +#define MAXBUFFERSIZE 512 + +struct rngs { + const char *path; + int length; +} rngs[] = { + { "/dev/random", 16 }, + { "/dev/urandom", 128 }, + { "/proc/stat", MAXBUFFERSIZE }, + { "/proc/loadavg", MAXBUFFERSIZE }, + { "/dev/audio", MAXBUFFERSIZE }, +}; +#define RNGS (sizeof(rngs)/sizeof(struct rngs)) + +int Verbose = 0; + +int main( int argc, char **argv ) { - int i; -#if SECURE - struct timeval tv; - struct timezone tz; + int i; + struct MD5Context ctx; + unsigned char digest[16]; + unsigned char buf[MAXBUFFERSIZE]; + int fd; + int c; + pid_t pid; + char *file = NULL; + int r; +#if HAVE_GETTIMEOFDAY + struct timeval tv; + struct timezone tz; +#else + long int t; +#endif + while ((c = getopt( argc, argv, "vf:" )) != EOF) + switch (c) { + case 'v': ++Verbose; break; + case 'f': file = optarg; break; + } + + MD5Init( &ctx ); + +#if HAVE_GETTIMEOFDAY gettimeofday( &tv, &tz ); - srand( tv.tv_sec + tv.tv_usec ); + MD5Update( &ctx, (unsigned char *)&tv, sizeof( tv ) ); #else - long int t; - time( &t ); - srand( t ); + MD5Update( &ctx, (unsigned char *)&t, sizeof( t ) ); #endif + pid = getppid(); + MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid )); + pid = getpid(); + MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid )); - for (i = 0; i < 32; i++) { - int r = (rand() & 0x0f0) >> 4; + if (file) { + int count = 0; + + if (file[0] == '-' && !file[1]) fd = fileno(stdin); + else if ((fd = open( file, O_RDONLY )) <0) { + fprintf( stderr, "Could not open %s\n" ); + } - if (r < 10) putchar( '0' + r ); - else putchar( 'a' + r - 10 ); + while ((r = read( fd, buf, sizeof( buf ) )) > 0) { + MD5Update( &ctx, buf, r ); + count += r; + } + if (Verbose) + fprintf( stderr, "Got %d bytes from %s\n", count, file ); + + if (file[0] != '-' || file[1]) close( fd ); } + + for (i = 0; i < RNGS; i++) { + if ((fd = open( rngs[i].path, O_RDONLY )) >= 0) { + r = read( fd, buf, sizeof( buf ) ); + MD5Update( &ctx, buf, r ); + close( fd ); + if (Verbose) + fprintf( stderr, "Got %d bytes from %s\n", r, rngs[i].path ); + if (r >= rngs[i].length) break; + } else if (Verbose) + fprintf( stderr, "Could not open %s\n", rngs[i].path ); + } + + MD5Final( digest, &ctx ); + for (i = 0; i < 16; i++) printf( "%02x", digest[i] ); putchar ( '\n' ); return 0; |