]> andersk Git - openssh.git/blob - ssh-rand-helper.c
- (stevesk) [Makefile.in ssh-rand-helper.c]
[openssh.git] / ssh-rand-helper.c
1 /*
2  * Copyright (c) 2001 Damien Miller.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "includes.h"
26
27 #include <openssl/rand.h>
28 #include <openssl/sha.h>
29 #include <openssl/crypto.h>
30
31 /* SunOS 4.4.4 needs this */
32 #ifdef HAVE_FLOATINGPOINT_H
33 # include <floatingpoint.h>
34 #endif /* HAVE_FLOATINGPOINT_H */
35
36 #include "misc.h"
37 #include "xmalloc.h"
38 #include "atomicio.h"
39 #include "pathnames.h"
40 #include "log.h"
41
42 #ifdef HAVE___PROGNAME
43 extern char *__progname;
44 #else
45 char *__progname;
46 #endif
47
48 RCSID("$Id$");
49
50 #define RANDOM_SEED_SIZE 48
51
52 #ifndef SSH_PRNG_SEED_FILE
53 # define SSH_PRNG_SEED_FILE      _PATH_SSH_USER_DIR"/prng_seed"
54 #endif /* SSH_PRNG_SEED_FILE */
55 #ifndef SSH_PRNG_COMMAND_FILE
56 # define SSH_PRNG_COMMAND_FILE   ETCDIR "/ssh_prng_cmds"
57 #endif /* SSH_PRNG_COMMAND_FILE */
58
59
60 #ifndef offsetof
61 # define offsetof(type, member) ((size_t) &((type *)0)->member)
62 #endif
63
64 /* Number of times to pass through command list gathering entropy */
65 #define NUM_ENTROPY_RUNS        1
66
67 /* Scale entropy estimates back by this amount on subsequent runs */
68 #define SCALE_PER_RUN           10.0
69
70 /* Minimum number of commands to be considered valid */
71 #define MIN_ENTROPY_SOURCES 16
72
73 #define WHITESPACE " \t\n"
74
75 #ifndef RUSAGE_SELF
76 # define RUSAGE_SELF 0
77 #endif
78 #ifndef RUSAGE_CHILDREN
79 # define RUSAGE_CHILDREN 0
80 #endif
81
82 #if defined(PRNGD_SOCKET) || defined(PRNGD_PORT)
83 # define USE_PRNGD
84 #endif
85
86 #ifdef USE_PRNGD
87 /* Collect entropy from PRNGD/EGD */
88 int
89 get_random_bytes(unsigned char *buf, int len)
90 {
91         int fd;
92         char msg[2];
93 #ifdef PRNGD_PORT
94         struct sockaddr_in addr;
95 #else
96         struct sockaddr_un addr;
97 #endif
98         int addr_len, rval, errors;
99         mysig_t old_sigpipe;
100
101         if (len > 255)
102                 fatal("Too many bytes to read from PRNGD");
103
104         memset(&addr, '\0', sizeof(addr));
105
106 #ifdef PRNGD_PORT
107         addr.sin_family = AF_INET;
108         addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
109         addr.sin_port = htons(PRNGD_PORT);
110         addr_len = sizeof(struct sockaddr_in);
111 #else /* use IP socket PRNGD_SOCKET instead */
112         /* Sanity checks */
113         if (sizeof(PRNGD_SOCKET) > sizeof(addr.sun_path))
114                 fatal("Random pool path is too long");
115
116         addr.sun_family = AF_UNIX;
117         strlcpy(addr.sun_path, PRNGD_SOCKET, sizeof(addr.sun_path));
118         addr_len = offsetof(struct sockaddr_un, sun_path) +
119             sizeof(PRNGD_SOCKET);
120 #endif
121
122         old_sigpipe = mysignal(SIGPIPE, SIG_IGN);
123
124         errors = rval = 0;
125 reopen:
126 #ifdef PRNGD_PORT
127         fd = socket(addr.sin_family, SOCK_STREAM, 0);
128         if (fd == -1) {
129                 error("Couldn't create AF_INET socket: %s", strerror(errno));
130                 goto done;
131         }
132 #else
133         fd = socket(addr.sun_family, SOCK_STREAM, 0);
134         if (fd == -1) {
135                 error("Couldn't create AF_UNIX socket: %s", strerror(errno));
136                 goto done;
137         }
138 #endif
139
140         if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
141 #ifdef PRNGD_PORT
142                 error("Couldn't connect to PRNGD port %d: %s",
143                     PRNGD_PORT, strerror(errno));
144 #else
145                 error("Couldn't connect to PRNGD socket \"%s\": %s",
146                     addr.sun_path, strerror(errno));
147 #endif
148                 goto done;
149         }
150
151         /* Send blocking read request to PRNGD */
152         msg[0] = 0x02;
153         msg[1] = len;
154
155         if (atomicio(write, fd, msg, sizeof(msg)) != sizeof(msg)) {
156                 if (errno == EPIPE && errors < 10) {
157                         close(fd);
158                         errors++;
159                         goto reopen;
160                 }
161                 error("Couldn't write to PRNGD socket: %s",
162                     strerror(errno));
163                 goto done;
164         }
165
166         if (atomicio(read, fd, buf, len) != len) {
167                 if (errno == EPIPE && errors < 10) {
168                         close(fd);
169                         errors++;
170                         goto reopen;
171                 }
172                 error("Couldn't read from PRNGD socket: %s",
173                     strerror(errno));
174                 goto done;
175         }
176
177         rval = 1;
178 done:
179         mysignal(SIGPIPE, old_sigpipe);
180         if (fd != -1)
181                 close(fd);
182         return(rval);
183 }
184
185 static void
186 seed_openssl_rng(void)
187 {
188         unsigned char buf[RANDOM_SEED_SIZE];
189
190         if (!get_random_bytes(buf, sizeof(buf)))
191                 fatal("Entropy collection failed");
192
193         RAND_add(buf, sizeof(buf), sizeof(buf));
194         memset(buf, '\0', sizeof(buf));
195 }
196
197 #else /* USE_PRNGD */
198
199 /*
200  * FIXME: proper entropy estimations. All current values are guesses
201  * FIXME: (ATL) do estimates at compile time?
202  * FIXME: More entropy sources
203  */
204
205 /* slow command timeouts (all in milliseconds) */
206 /* static int entropy_timeout_default = ENTROPY_TIMEOUT_MSEC; */
207 static int entropy_timeout_current = ENTROPY_TIMEOUT_MSEC;
208
209 typedef struct
210 {
211         /* Proportion of data that is entropy */
212         double rate;
213         /* Counter goes positive if this command times out */
214         unsigned int badness;
215         /* Increases by factor of two each timeout */
216         unsigned int sticky_badness;
217         /* Path to executable */
218         char *path;
219         /* argv to pass to executable */
220         char *args[5];
221         /* full command string (debug) */
222         char *cmdstring;
223 } entropy_source_t;
224
225 double stir_from_system(void);
226 double stir_from_programs(void);
227 double stir_gettimeofday(double entropy_estimate);
228 double stir_clock(double entropy_estimate);
229 double stir_rusage(int who, double entropy_estimate);
230 double hash_output_from_command(entropy_source_t *src, char *hash);
231
232 /* this is initialised from a file, by prng_read_commands() */
233 entropy_source_t *entropy_sources = NULL;
234
235 double
236 stir_from_system(void)
237 {
238         double total_entropy_estimate;
239         long int i;
240
241         total_entropy_estimate = 0;
242
243         i = getpid();
244         RAND_add(&i, sizeof(i), 0.5);
245         total_entropy_estimate += 0.1;
246
247         i = getppid();
248         RAND_add(&i, sizeof(i), 0.5);
249         total_entropy_estimate += 0.1;
250
251         i = getuid();
252         RAND_add(&i, sizeof(i), 0.0);
253         i = getgid();
254         RAND_add(&i, sizeof(i), 0.0);
255
256         total_entropy_estimate += stir_gettimeofday(1.0);
257         total_entropy_estimate += stir_clock(0.5);
258         total_entropy_estimate += stir_rusage(RUSAGE_SELF, 2.0);
259
260         return(total_entropy_estimate);
261 }
262
263 double
264 stir_from_programs(void)
265 {
266         int i;
267         int c;
268         double entropy_estimate;
269         double total_entropy_estimate;
270         char hash[SHA_DIGEST_LENGTH];
271
272         total_entropy_estimate = 0;
273         for(i = 0; i < NUM_ENTROPY_RUNS; i++) {
274                 c = 0;
275                 while (entropy_sources[c].path != NULL) {
276
277                         if (!entropy_sources[c].badness) {
278                                 /* Hash output from command */
279                                 entropy_estimate = hash_output_from_command(&entropy_sources[c], hash);
280
281                                 /* Scale back entropy estimate according to command's rate */
282                                 entropy_estimate *= entropy_sources[c].rate;
283
284                                 /* Upper bound of entropy estimate is SHA_DIGEST_LENGTH */
285                                 if (entropy_estimate > SHA_DIGEST_LENGTH)
286                                         entropy_estimate = SHA_DIGEST_LENGTH;
287
288                                 /* Scale back estimates for subsequent passes through list */
289                                 entropy_estimate /= SCALE_PER_RUN * (i + 1.0);
290
291                                 /* Stir it in */
292                                 RAND_add(hash, sizeof(hash), entropy_estimate);
293
294                                 debug3("Got %0.2f bytes of entropy from '%s'", entropy_estimate,
295                                         entropy_sources[c].cmdstring);
296
297                                 total_entropy_estimate += entropy_estimate;
298
299                         /* Execution times should be a little unpredictable */
300                                 total_entropy_estimate += stir_gettimeofday(0.05);
301                                 total_entropy_estimate += stir_clock(0.05);
302                                 total_entropy_estimate += stir_rusage(RUSAGE_SELF, 0.1);
303                                 total_entropy_estimate += stir_rusage(RUSAGE_CHILDREN, 0.1);
304                         } else {
305                                 debug2("Command '%s' disabled (badness %d)",
306                                         entropy_sources[c].cmdstring, entropy_sources[c].badness);
307
308                                 if (entropy_sources[c].badness > 0)
309                                         entropy_sources[c].badness--;
310                         }
311
312                         c++;
313                 }
314         }
315
316         return(total_entropy_estimate);
317 }
318
319 double
320 stir_gettimeofday(double entropy_estimate)
321 {
322         struct timeval tv;
323
324         if (gettimeofday(&tv, NULL) == -1)
325                 fatal("Couldn't gettimeofday: %s", strerror(errno));
326
327         RAND_add(&tv, sizeof(tv), entropy_estimate);
328
329         return(entropy_estimate);
330 }
331
332 double
333 stir_clock(double entropy_estimate)
334 {
335 #ifdef HAVE_CLOCK
336         clock_t c;
337
338         c = clock();
339         RAND_add(&c, sizeof(c), entropy_estimate);
340
341         return(entropy_estimate);
342 #else /* _HAVE_CLOCK */
343         return(0);
344 #endif /* _HAVE_CLOCK */
345 }
346
347 double
348 stir_rusage(int who, double entropy_estimate)
349 {
350 #ifdef HAVE_GETRUSAGE
351         struct rusage ru;
352
353         if (getrusage(who, &ru) == -1)
354                 return(0);
355
356         RAND_add(&ru, sizeof(ru), entropy_estimate);
357
358         return(entropy_estimate);
359 #else /* _HAVE_GETRUSAGE */
360         return(0);
361 #endif /* _HAVE_GETRUSAGE */
362 }
363
364
365 static int
366 _get_timeval_msec_difference(struct timeval *t1, struct timeval *t2) {
367         int secdiff, usecdiff;
368
369         secdiff = t2->tv_sec - t1->tv_sec;
370         usecdiff = (secdiff*1000000) + (t2->tv_usec - t1->tv_usec);
371         return (int)(usecdiff / 1000);
372 }
373
374 double
375 hash_output_from_command(entropy_source_t *src, char *hash)
376 {
377         static int devnull = -1;
378         int p[2];
379         fd_set rdset;
380         int cmd_eof = 0, error_abort = 0;
381         struct timeval tv_start, tv_current;
382         int msec_elapsed = 0;
383         pid_t pid;
384         int status;
385         char buf[16384];
386         int bytes_read;
387         int total_bytes_read;
388         SHA_CTX sha;
389
390         debug3("Reading output from \'%s\'", src->cmdstring);
391
392         if (devnull == -1) {
393                 devnull = open("/dev/null", O_RDWR);
394                 if (devnull == -1)
395                         fatal("Couldn't open /dev/null: %s", strerror(errno));
396         }
397
398         if (pipe(p) == -1)
399                 fatal("Couldn't open pipe: %s", strerror(errno));
400
401         (void)gettimeofday(&tv_start, NULL); /* record start time */
402
403         switch (pid = fork()) {
404                 case -1: /* Error */
405                         close(p[0]);
406                         close(p[1]);
407                         fatal("Couldn't fork: %s", strerror(errno));
408                         /* NOTREACHED */
409                 case 0: /* Child */
410                         dup2(devnull, STDIN_FILENO);
411                         dup2(p[1], STDOUT_FILENO);
412                         dup2(p[1], STDERR_FILENO);
413                         close(p[0]);
414                         close(p[1]);
415                         close(devnull);
416
417                         execv(src->path, (char**)(src->args));
418                         debug("(child) Couldn't exec '%s': %s", src->cmdstring,
419                               strerror(errno));
420                         _exit(-1);
421                 default: /* Parent */
422                         break;
423         }
424
425         RAND_add(&pid, sizeof(&pid), 0.0);
426
427         close(p[1]);
428
429         /* Hash output from child */
430         SHA1_Init(&sha);
431         total_bytes_read = 0;
432
433         while (!error_abort && !cmd_eof) {
434                 int ret;
435                 struct timeval tv;
436                 int msec_remaining;
437
438                 (void) gettimeofday(&tv_current, 0);
439                 msec_elapsed = _get_timeval_msec_difference(&tv_start, &tv_current);
440                 if (msec_elapsed >= entropy_timeout_current) {
441                         error_abort=1;
442                         continue;
443                 }
444                 msec_remaining = entropy_timeout_current - msec_elapsed;
445
446                 FD_ZERO(&rdset);
447                 FD_SET(p[0], &rdset);
448                 tv.tv_sec =  msec_remaining / 1000;
449                 tv.tv_usec = (msec_remaining % 1000) * 1000;
450
451                 ret = select(p[0]+1, &rdset, NULL, NULL, &tv);
452
453                 RAND_add(&tv, sizeof(tv), 0.0);
454
455                 switch (ret) {
456                 case 0:
457                         /* timer expired */
458                         error_abort = 1;
459                         break;
460                 case 1:
461                         /* command input */
462                         do {
463                                 bytes_read = read(p[0], buf, sizeof(buf));
464                         } while (bytes_read == -1 && errno == EINTR);
465                         RAND_add(&bytes_read, sizeof(&bytes_read), 0.0);
466                         if (bytes_read == -1) {
467                                 error_abort = 1;
468                                 break;
469                         } else if (bytes_read) {
470                                 SHA1_Update(&sha, buf, bytes_read);
471                                 total_bytes_read += bytes_read;
472                         } else {
473                                 cmd_eof = 1;
474                         }
475                         break;
476                 case -1:
477                 default:
478                         /* error */
479                         debug("Command '%s': select() failed: %s", src->cmdstring,
480                               strerror(errno));
481                         error_abort = 1;
482                         break;
483                 }
484         }
485
486         SHA1_Final(hash, &sha);
487
488         close(p[0]);
489
490         debug3("Time elapsed: %d msec", msec_elapsed);
491
492         if (waitpid(pid, &status, 0) == -1) {
493                error("Couldn't wait for child '%s' completion: %s", src->cmdstring,
494                      strerror(errno));
495                 return(0.0);
496         }
497
498         RAND_add(&status, sizeof(&status), 0.0);
499
500         if (error_abort) {
501                 /* closing p[0] on timeout causes the entropy command to
502                  * SIGPIPE. Take whatever output we got, and mark this command
503                  * as slow */
504                 debug2("Command '%s' timed out", src->cmdstring);
505                 src->sticky_badness *= 2;
506                 src->badness = src->sticky_badness;
507                 return(total_bytes_read);
508         }
509
510         if (WIFEXITED(status)) {
511                 if (WEXITSTATUS(status)==0) {
512                         return(total_bytes_read);
513                 } else {
514                         debug2("Command '%s' exit status was %d", src->cmdstring,
515                                 WEXITSTATUS(status));
516                         src->badness = src->sticky_badness = 128;
517                         return (0.0);
518                 }
519         } else if (WIFSIGNALED(status)) {
520                 debug2("Command '%s' returned on uncaught signal %d !", src->cmdstring,
521                         status);
522                 src->badness = src->sticky_badness = 128;
523                 return(0.0);
524         } else
525                 return(0.0);
526 }
527
528 /*
529  * prng seedfile functions
530  */
531 int
532 prng_check_seedfile(char *filename) {
533
534         struct stat st;
535
536         /* FIXME raceable: eg replace seed between this stat and subsequent open */
537         /* Not such a problem because we don't trust the seed file anyway */
538         if (lstat(filename, &st) == -1) {
539                 /* Give up on hard errors */
540                 if (errno != ENOENT)
541                         debug("WARNING: Couldn't stat random seed file \"%s\": %s",
542                            filename, strerror(errno));
543
544                 return(0);
545         }
546
547         /* regular file? */
548         if (!S_ISREG(st.st_mode))
549                 fatal("PRNG seedfile %.100s is not a regular file", filename);
550
551         /* mode 0600, owned by root or the current user? */
552         if (((st.st_mode & 0177) != 0) || !(st.st_uid == getuid())) {
553                 debug("WARNING: PRNG seedfile %.100s must be mode 0600, owned by uid %d",
554                          filename, getuid());
555                 return(0);
556         }
557
558         return(1);
559 }
560
561 void
562 prng_write_seedfile(void) {
563         int fd;
564         char seed[1024];
565         char filename[1024];
566         struct passwd *pw;
567
568         pw = getpwuid(getuid());
569         if (pw == NULL)
570                 fatal("Couldn't get password entry for current user (%i): %s",
571                         getuid(), strerror(errno));
572
573         /* Try to ensure that the parent directory is there */
574         snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
575                 _PATH_SSH_USER_DIR);
576         mkdir(filename, 0700);
577
578         snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
579                 SSH_PRNG_SEED_FILE);
580
581         debug("writing PRNG seed to file %.100s", filename);
582
583         RAND_bytes(seed, sizeof(seed));
584
585         /* Don't care if the seed doesn't exist */
586         prng_check_seedfile(filename);
587
588         if ((fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0600)) == -1) {
589                 debug("WARNING: couldn't access PRNG seedfile %.100s (%.100s)",
590                    filename, strerror(errno));
591         } else {
592                 if (atomicio(write, fd, &seed, sizeof(seed)) != sizeof(seed))
593                         fatal("problem writing PRNG seedfile %.100s (%.100s)", filename,
594                                  strerror(errno));
595                 close(fd);
596         }
597 }
598
599 void
600 prng_read_seedfile(void) {
601         int fd;
602         char seed[1024];
603         char filename[1024];
604         struct passwd *pw;
605
606         pw = getpwuid(getuid());
607         if (pw == NULL)
608                 fatal("Couldn't get password entry for current user (%i): %s",
609                         getuid(), strerror(errno));
610
611         snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
612                 SSH_PRNG_SEED_FILE);
613
614         debug("loading PRNG seed from file %.100s", filename);
615
616         if (!prng_check_seedfile(filename)) {
617                 verbose("Random seed file not found or not valid, ignoring.");
618                 return;
619         }
620
621         /* open the file and read in the seed */
622         fd = open(filename, O_RDONLY);
623         if (fd == -1)
624                 fatal("could not open PRNG seedfile %.100s (%.100s)", filename,
625                         strerror(errno));
626
627         if (atomicio(read, fd, &seed, sizeof(seed)) != sizeof(seed)) {
628                 verbose("invalid or short read from PRNG seedfile %.100s - ignoring",
629                         filename);
630                 memset(seed, '\0', sizeof(seed));
631         }
632         close(fd);
633
634         /* stir in the seed, with estimated entropy zero */
635         RAND_add(&seed, sizeof(seed), 0.0);
636 }
637
638
639 /*
640  * entropy command initialisation functions
641  */
642 int
643 prng_read_commands(char *cmdfilename)
644 {
645         FILE *f;
646         char *cp;
647         char line[1024];
648         char cmd[1024];
649         char path[256];
650         int linenum;
651         int num_cmds = 64;
652         int cur_cmd = 0;
653         double est;
654         entropy_source_t *entcmd;
655
656         f = fopen(cmdfilename, "r");
657         if (!f) {
658                 fatal("couldn't read entropy commands file %.100s: %.100s",
659                     cmdfilename, strerror(errno));
660         }
661
662         entcmd = (entropy_source_t *)xmalloc(num_cmds * sizeof(entropy_source_t));
663         memset(entcmd, '\0', num_cmds * sizeof(entropy_source_t));
664
665         /* Read in file */
666         linenum = 0;
667         while (fgets(line, sizeof(line), f)) {
668                 int arg;
669                 char *argv;
670
671                 linenum++;
672
673                 /* skip leading whitespace, test for blank line or comment */
674                 cp = line + strspn(line, WHITESPACE);
675                 if ((*cp == 0) || (*cp == '#'))
676                         continue; /* done with this line */
677
678                 /* First non-whitespace char should be double quote delimiting */
679                 /* commandline */
680                 if (*cp != '"') {
681                         error("bad entropy command, %.100s line %d", cmdfilename,
682                              linenum);
683                         continue;
684                 }
685
686                 /* first token, command args (incl. argv[0]) in double quotes */
687                 cp = strtok(cp, "\"");
688                 if (cp == NULL) {
689                         error("missing or bad command string, %.100s line %d -- ignored",
690                               cmdfilename, linenum);
691                         continue;
692                 }
693                 strlcpy(cmd, cp, sizeof(cmd));
694
695                 /* second token, full command path */
696                 if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
697                         error("missing command path, %.100s line %d -- ignored",
698                               cmdfilename, linenum);
699                         continue;
700                 }
701
702                 /* did configure mark this as dead? */
703                 if (strncmp("undef", cp, 5) == 0)
704                         continue;
705
706                 strlcpy(path, cp, sizeof(path));
707
708                 /* third token, entropy rate estimate for this command */
709                 if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
710                         error("missing entropy estimate, %.100s line %d -- ignored",
711                               cmdfilename, linenum);
712                         continue;
713                 }
714                 est = strtod(cp, &argv);
715
716                 /* end of line */
717                 if ((cp = strtok(NULL, WHITESPACE)) != NULL) {
718                         error("garbage at end of line %d in %.100s -- ignored", linenum,
719                                 cmdfilename);
720                         continue;
721                 }
722
723                 /* save the command for debug messages */
724                 entcmd[cur_cmd].cmdstring = xstrdup(cmd);
725
726                 /* split the command args */
727                 cp = strtok(cmd, WHITESPACE);
728                 arg = 0;
729                 argv = NULL;
730                 do {
731                         char *s = (char*)xmalloc(strlen(cp) + 1);
732                         strncpy(s, cp, strlen(cp) + 1);
733                         entcmd[cur_cmd].args[arg] = s;
734                         arg++;
735                 } while ((arg < 5) && (cp = strtok(NULL, WHITESPACE)));
736
737                 if (strtok(NULL, WHITESPACE))
738                         error("ignored extra command elements (max 5), %.100s line %d",
739                               cmdfilename, linenum);
740
741                 /* Copy the command path and rate estimate */
742                 entcmd[cur_cmd].path = xstrdup(path);
743                 entcmd[cur_cmd].rate = est;
744
745                 /* Initialise other values */
746                 entcmd[cur_cmd].sticky_badness = 1;
747
748                 cur_cmd++;
749
750                 /* If we've filled the array, reallocate it twice the size */
751                 /* Do this now because even if this we're on the last command,
752                    we need another slot to mark the last entry */
753                 if (cur_cmd == num_cmds) {
754                         num_cmds *= 2;
755                         entcmd = xrealloc(entcmd, num_cmds * sizeof(entropy_source_t));
756                 }
757         }
758
759         /* zero the last entry */
760         memset(&entcmd[cur_cmd], '\0', sizeof(entropy_source_t));
761
762         /* trim to size */
763         entropy_sources = xrealloc(entcmd, (cur_cmd+1) * sizeof(entropy_source_t));
764
765         debug("Loaded %d entropy commands from %.100s", cur_cmd, cmdfilename);
766
767         return (cur_cmd >= MIN_ENTROPY_SOURCES);
768 }
769
770 static void
771 seed_openssl_rng(void)
772 {
773         /* Read in collection commands */
774         if (!prng_read_commands(SSH_PRNG_COMMAND_FILE))
775                 fatal("PRNG initialisation failed -- exiting.");
776
777         prng_read_seedfile();
778
779         debug("Seeded RNG with %i bytes from programs", 
780             (int)stir_from_programs());
781         debug("Seeded RNG with %i bytes from system calls", 
782             (int)stir_from_system());
783
784         prng_write_seedfile();
785 }
786
787 #endif /* USE_PRNGD */
788
789 int 
790 main(int argc, char **argv)
791 {
792         unsigned char buf[48];
793         int ret;
794
795         __progname = get_progname(argv[0]);
796         /* XXX: need some debugging mode */
797         log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
798
799         seed_openssl_rng();
800
801         if (!RAND_status())
802                 fatal("Not enough entropy in RNG");
803
804         RAND_bytes(buf, sizeof(buf));
805
806         ret = atomicio(write, STDOUT_FILENO, buf, sizeof(buf));
807                 
808         memset(buf, '\0', sizeof(buf));
809         
810         return ret == sizeof(buf) ? 0 : 1;
811 }
812
This page took 0.166878 seconds and 5 git commands to generate.