]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/mcookie.c
Imported from util-linux-2.9i tarball.
[thirdparty/util-linux.git] / 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.
6 *
7 * $Id: mcookie.c,v 1.5 1997/07/06 00:13:06 aebr Exp $
8 *
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
11 * use with xauth(1).
12 *
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.
16 */
17
18 #ifdef __linux__
19 #define HAVE_GETTIMEOFDAY 1
20 #endif
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <fcntl.h>
25 #include "md5.h"
26 #if HAVE_GETTIMEOFDAY
27 #include <sys/time.h>
28 #include <unistd.h>
29 #endif
30
31 #define MAXBUFFERSIZE 512
32
33 struct rngs {
34 const char *path;
35 int length;
36 } rngs[] = {
37 { "/dev/random", 16 },
38 { "/dev/urandom", 128 },
39 { "/proc/stat", MAXBUFFERSIZE },
40 { "/proc/loadavg", MAXBUFFERSIZE },
41 { "/dev/audio", MAXBUFFERSIZE },
42 };
43 #define RNGS (sizeof(rngs)/sizeof(struct rngs))
44
45 int Verbose = 0;
46
47 int main( int argc, char **argv )
48 {
49 int i;
50 struct MD5Context ctx;
51 unsigned char digest[16];
52 unsigned char buf[MAXBUFFERSIZE];
53 int fd;
54 int c;
55 pid_t pid;
56 char *file = NULL;
57 int r;
58 #if HAVE_GETTIMEOFDAY
59 struct timeval tv;
60 struct timezone tz;
61 #else
62 long int t;
63 #endif
64
65 while ((c = getopt( argc, argv, "vf:" )) != EOF)
66 switch (c) {
67 case 'v': ++Verbose; break;
68 case 'f': file = optarg; break;
69 }
70
71 MD5Init( &ctx );
72
73 #if HAVE_GETTIMEOFDAY
74 gettimeofday( &tv, &tz );
75 MD5Update( &ctx, (unsigned char *)&tv, sizeof( tv ) );
76 #else
77 time( &t );
78 MD5Update( &ctx, (unsigned char *)&t, sizeof( t ) );
79 #endif
80 pid = getppid();
81 MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid ));
82 pid = getpid();
83 MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid ));
84
85 if (file) {
86 int count = 0;
87
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 );
91 }
92
93 while ((r = read( fd, buf, sizeof( buf ) )) > 0) {
94 MD5Update( &ctx, buf, r );
95 count += r;
96 }
97 if (Verbose)
98 fprintf( stderr, "Got %d bytes from %s\n", count, file );
99
100 if (file[0] != '-' || file[1]) close( fd );
101 }
102
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 ) );
106 if (r > 0)
107 MD5Update( &ctx, buf, r );
108 else
109 r = 0;
110 close( fd );
111 if (Verbose)
112 fprintf( stderr, "Got %d bytes from %s\n", r, rngs[i].path );
113 if (r >= rngs[i].length) break;
114 } else if (Verbose)
115 fprintf( stderr, "Could not open %s\n", rngs[i].path );
116 }
117
118 MD5Final( digest, &ctx );
119 for (i = 0; i < 16; i++) printf( "%02x", digest[i] );
120 putchar ( '\n' );
121
122 return 0;
123 }