]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/mcookie.c
md5: use symbolical digest length
[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 * This program gathers some random bits of data and used the MD5
8 * message-digest algorithm to generate a 128-bit hexadecimal number for
9 * use with xauth(1).
10 *
11 * NOTE: Unless /dev/random is available, this program does not actually
12 * gather 128 bits of random information, so the magic cookie generated
13 * will be considerably easier to guess than one might expect.
14 *
15 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
16 * - added Native Language Support
17 * 1999-03-21 aeb: Added some fragments of code from Colin Plumb.
18 *
19 */
20
21 #include "c.h"
22 #include "md5.h"
23 #include "nls.h"
24 #include <fcntl.h>
25 #include <getopt.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/time.h>
30 #include <unistd.h>
31
32 #define BUFFERSIZE 4096
33
34 struct rngs {
35 const char *path;
36 int minlength, maxlength;
37 } rngs[] = {
38 {"/dev/random", 16, 16}, /* 16 bytes = 128 bits suffice */
39 {"/proc/interrupts", 0, 0},
40 {"/proc/slabinfo", 0, 0},
41 {"/proc/stat", 0, 0},
42 {"/dev/urandom", 32, 64},
43 };
44
45 #define RNGS (sizeof(rngs)/sizeof(struct rngs))
46
47 /* The basic function to hash a file */
48 static off_t hash_file(struct MD5Context *ctx, int fd)
49 {
50 off_t count = 0;
51 ssize_t r;
52 unsigned char buf[BUFFERSIZE];
53
54 while ((r = read(fd, buf, sizeof(buf))) > 0) {
55 MD5Update(ctx, buf, r);
56 count += r;
57 }
58 /* Separate files with a null byte */
59 buf[0] = '\0';
60 MD5Update(ctx, buf, 1);
61 return count;
62 }
63
64 static void __attribute__ ((__noreturn__)) usage(FILE * out)
65 {
66 fprintf(out, _("Usage: %s [options]\n"),
67 program_invocation_short_name);
68
69 fprintf(out, _("\nOptions:\n"
70 " -f, --file=FILE use file as a cookie seed\n"
71 " -v, --verbose explain what is being done\n"
72 " -V, --version output version information and exit\n"
73 " -h, --help display this help and exit\n"));
74
75 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
76 }
77
78 int main(int argc, char **argv)
79 {
80 size_t i;
81 struct MD5Context ctx;
82 unsigned char digest[MD5LENGTH];
83 unsigned char buf[BUFFERSIZE];
84 int fd;
85 int c;
86 pid_t pid;
87 char *file = NULL;
88 int verbose = 0;
89 int r;
90 struct timeval tv;
91 struct timezone tz;
92
93 static const struct option longopts[] = {
94 {"file", required_argument, NULL, 'f'},
95 {"verbose", no_argument, NULL, 'v'},
96 {"version", no_argument, NULL, 'V'},
97 {"help", no_argument, NULL, 'h'},
98 {NULL, 0, NULL, 0}
99 };
100
101 setlocale(LC_ALL, "");
102 bindtextdomain(PACKAGE, LOCALEDIR);
103 textdomain(PACKAGE);
104
105 while ((c =
106 getopt_long(argc, argv, "f:vVh", longopts, NULL)) != -1)
107 switch (c) {
108 case 'v':
109 verbose = 1;
110 break;
111 case 'f':
112 file = optarg;
113 break;
114 case 'V':
115 printf(_("%s from %s\n"),
116 program_invocation_short_name,
117 PACKAGE_STRING);
118 return EXIT_SUCCESS;
119 case 'h':
120 usage(stdout);
121 default:
122 usage(stderr);
123 }
124
125 MD5Init(&ctx);
126 gettimeofday(&tv, &tz);
127 MD5Update(&ctx, (unsigned char *) &tv, sizeof(tv));
128
129 pid = getppid();
130 MD5Update(&ctx, (unsigned char *) &pid, sizeof(pid));
131 pid = getpid();
132 MD5Update(&ctx, (unsigned char *) &pid, sizeof(pid));
133
134 if (file) {
135 int count = 0;
136
137 if (file[0] == '-' && !file[1])
138 fd = STDIN_FILENO;
139 else
140 fd = open(file, O_RDONLY);
141
142 if (fd < 0) {
143 warn(_("Could not open %s"), file);
144 } else {
145 count = hash_file(&ctx, fd);
146 if (verbose)
147 fprintf(stderr,
148 _("Got %d bytes from %s\n"), count,
149 file);
150
151 if (fd != STDIN_FILENO)
152 if (close(fd))
153 err(EXIT_FAILURE,
154 _("closing %s failed"), file);
155 }
156 }
157
158 for (i = 0; i < RNGS; i++) {
159 if ((fd = open(rngs[i].path, O_RDONLY | O_NONBLOCK)) >= 0) {
160 int count = sizeof(buf);
161
162 if (rngs[i].maxlength && count > rngs[i].maxlength)
163 count = rngs[i].maxlength;
164 r = read(fd, buf, count);
165 if (r > 0)
166 MD5Update(&ctx, buf, r);
167 else
168 r = 0;
169 close(fd);
170 if (verbose)
171 fprintf(stderr,
172 _("Got %d bytes from %s\n"), r,
173 rngs[i].path);
174 if (rngs[i].minlength && r >= rngs[i].minlength)
175 break;
176 } else if (verbose)
177 warn(_("Could not open %s"), rngs[i].path);
178 }
179
180 MD5Final(digest, &ctx);
181 for (i = 0; i < MD5LENGTH; i++)
182 printf("%02x", digest[i]);
183 putchar('\n');
184
185 /*
186 * The following is important for cases like disk full,
187 * so shell scripts can bomb out properly rather than
188 * think they succeeded.
189 */
190 if (fflush(stdout) < 0 || fclose(stdout) < 0)
191 return EXIT_FAILURE;
192
193 return EXIT_SUCCESS;
194 }