]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/mcookie.c
Imported from util-linux-2.13-pre1 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: 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.
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 * 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.
20 *
21 */
22
23 #ifdef __linux__
24 #define HAVE_GETTIMEOFDAY 1
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <fcntl.h>
30 #include "md5.h"
31 #ifdef HAVE_GETTIMEOFDAY
32 #include <sys/time.h>
33 #include <unistd.h>
34 #endif
35 #include "nls.h"
36
37 #define BUFFERSIZE 4096
38
39 struct rngs {
40 const char *path;
41 int minlength, maxlength;
42 } rngs[] = {
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 },
48 };
49 #define RNGS (sizeof(rngs)/sizeof(struct rngs))
50
51 int Verbose = 0;
52
53 /* The basic function to hash a file */
54 static off_t
55 hash_file(struct MD5Context *ctx, int fd)
56 {
57 off_t count = 0;
58 ssize_t r;
59 unsigned char buf[BUFFERSIZE];
60
61 while ((r = read(fd, buf, sizeof(buf))) > 0) {
62 MD5Update(ctx, buf, r);
63 count += r;
64 }
65 /* Separate files with a null byte */
66 buf[0] = 0;
67 MD5Update(ctx, buf, 1);
68 return count;
69 }
70
71 int main( int argc, char **argv )
72 {
73 int i;
74 struct MD5Context ctx;
75 unsigned char digest[16];
76 unsigned char buf[BUFFERSIZE];
77 int fd;
78 int c;
79 pid_t pid;
80 char *file = NULL;
81 int r;
82 #ifdef HAVE_GETTIMEOFDAY
83 struct timeval tv;
84 struct timezone tz;
85 #else
86 long int t;
87 #endif
88
89 setlocale(LC_ALL, "");
90 bindtextdomain(PACKAGE, LOCALEDIR);
91 textdomain(PACKAGE);
92
93 while ((c = getopt( argc, argv, "vf:" )) != -1)
94 switch (c) {
95 case 'v': ++Verbose; break;
96 case 'f': file = optarg; break;
97 }
98
99 MD5Init( &ctx );
100
101 #ifdef HAVE_GETTIMEOFDAY
102 gettimeofday( &tv, &tz );
103 MD5Update( &ctx, (unsigned char *)&tv, sizeof( tv ) );
104 #else
105 time( &t );
106 MD5Update( &ctx, (unsigned char *)&t, sizeof( t ) );
107 #endif
108 pid = getppid();
109 MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid ));
110 pid = getpid();
111 MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid ));
112
113 if (file) {
114 int count = 0;
115
116 if (file[0] == '-' && !file[1])
117 fd = fileno(stdin);
118 else
119 fd = open( file, O_RDONLY );
120
121 if (fd < 0) {
122 fprintf( stderr, _("Could not open %s\n"), file );
123 } else {
124 count = hash_file( &ctx, fd );
125 if (Verbose)
126 fprintf( stderr, _("Got %d bytes from %s\n"), count, file );
127
128 if (file[0] != '-' || file[1]) close( fd );
129 }
130 }
131
132 for (i = 0; i < RNGS; i++) {
133 if ((fd = open( rngs[i].path, O_RDONLY|O_NONBLOCK )) >= 0) {
134 int count = sizeof(buf);
135
136 if (rngs[i].maxlength && count > rngs[i].maxlength)
137 count = rngs[i].maxlength;
138 r = read( fd, buf, count );
139 if (r > 0)
140 MD5Update( &ctx, buf, r );
141 else
142 r = 0;
143 close( fd );
144 if (Verbose)
145 fprintf( stderr, _("Got %d bytes from %s\n"), r, rngs[i].path );
146 if (rngs[i].minlength && r >= rngs[i].minlength)
147 break;
148 } else if (Verbose)
149 fprintf( stderr, _("Could not open %s\n"), rngs[i].path );
150 }
151
152 MD5Final( digest, &ctx );
153 for (i = 0; i < 16; i++) printf( "%02x", digest[i] );
154 putchar ( '\n' );
155
156 /*
157 * The following is important for cases like disk full, so shell scripts
158 * can bomb out properly rather than think they succeeded.
159 */
160 if (fflush(stdout) < 0 || fclose(stdout) < 0)
161 return 1;
162
163 return 0;
164 }