]>
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: Mon Sep 25 23:44:43 1995 by r.faith@ieee.org
4 * Public Domain 1995 Rickard E. Faith (faith@cs.unc.edu)
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.
19 #define HAVE_GETTIMEOFDAY 1
31 #define MAXBUFFERSIZE 512
37 { "/dev/random", 16 },
38 { "/dev/urandom", 128 },
39 { "/proc/stat", MAXBUFFERSIZE
},
40 { "/proc/loadavg", MAXBUFFERSIZE
},
41 { "/dev/audio", MAXBUFFERSIZE
},
43 #define RNGS (sizeof(rngs)/sizeof(struct rngs))
47 int main( int argc
, char **argv
)
50 struct MD5Context ctx
;
51 unsigned char digest
[16];
52 unsigned char buf
[MAXBUFFERSIZE
];
65 while ((c
= getopt( argc
, argv
, "vf:" )) != EOF
)
67 case 'v': ++Verbose
; break;
68 case 'f': file
= optarg
; break;
74 gettimeofday( &tv
, &tz
);
75 MD5Update( &ctx
, (unsigned char *)&tv
, sizeof( tv
) );
78 MD5Update( &ctx
, (unsigned char *)&t
, sizeof( t
) );
81 MD5Update( &ctx
, (unsigned char *)&pid
, sizeof( pid
));
83 MD5Update( &ctx
, (unsigned char *)&pid
, sizeof( pid
));
88 if (file
[0] == '-' && !file
[1]) fd
= fileno(stdin
);
89 else if ((fd
= open( file
, O_RDONLY
)) <0) {
90 fprintf( stderr
, "Could not open %s\n", file
);
93 while ((r
= read( fd
, buf
, sizeof( buf
) )) > 0) {
94 MD5Update( &ctx
, buf
, r
);
98 fprintf( stderr
, "Got %d bytes from %s\n", count
, file
);
100 if (file
[0] != '-' || file
[1]) close( fd
);
103 for (i
= 0; i
< RNGS
; i
++) {
104 if ((fd
= open( rngs
[i
].path
, O_RDONLY
|O_NONBLOCK
)) >= 0) {
105 r
= read( fd
, buf
, sizeof( buf
) );
107 MD5Update( &ctx
, buf
, r
);
112 fprintf( stderr
, "Got %d bytes from %s\n", r
, rngs
[i
].path
);
113 if (r
>= rngs
[i
].length
) break;
115 fprintf( stderr
, "Could not open %s\n", rngs
[i
].path
);
118 MD5Final( digest
, &ctx
);
119 for (i
= 0; i
< 16; i
++) printf( "%02x", digest
[i
] );