]> andersk Git - openssh.git/blobdiff - entropy.c
- (djm) Fix pam sprintf fix
[openssh.git] / entropy.c
index 0b0b53611a9d5a0cf9ecaafa89748a6d0f2a6f2d..84b1dd1d951d2fd908839db00e0e96c3855153d3 100644 (file)
--- a/entropy.c
+++ b/entropy.c
 
 RCSID("$Id$");
 
-#ifdef EGD_SOCKET
 #ifndef offsetof
 # define offsetof(type, member) ((size_t) &((type *)0)->member)
 #endif
+
+/* Print lots of detail */
+/* #define DEBUG_ENTROPY */
+
+/* Number of times to pass through command list gathering entropy */
+#define NUM_ENTROPY_RUNS       1
+
+/* Scale entropy estimates back by this amount on subsequent runs */
+#define SCALE_PER_RUN          10.0
+
+/* Minimum number of commands to be considered valid */
+#define MIN_ENTROPY_SOURCES 16
+
+#define WHITESPACE " \t\n"
+
+#ifndef RUSAGE_SELF
+# define RUSAGE_SELF 0
+#endif
+#ifndef RUSAGE_CHILDREN
+# define RUSAGE_CHILDREN 0
+#endif
+
+#if defined(EGD_SOCKET) || defined(RANDOM_POOL)
+
+#ifdef EGD_SOCKET
 /* Collect entropy from EGD */
-void get_random_bytes(unsigned char *buf, int len)
+int get_random_bytes(unsigned char *buf, int len)
 {
-       static int egd_socket = -1;
-       int c;
-       char egd_message[2] = { 0x02, 0x00 };
+       int fd;
+       char msg[2];
        struct sockaddr_un addr;
        int addr_len;
 
-       memset(&addr, '\0', sizeof(addr));
-       addr.sun_family = AF_UNIX;
-       
-       /* FIXME: compile time check? */
+       /* Sanity checks */
        if (sizeof(EGD_SOCKET) > sizeof(addr.sun_path))
                fatal("Random pool path is too long");
-       
-       strcpy(addr.sun_path, EGD_SOCKET);
-       
-       addr_len = offsetof(struct sockaddr_un, sun_path) + sizeof(EGD_SOCKET);
-       
-       if (egd_socket == -1) {
-               egd_socket = socket(AF_UNIX, SOCK_STREAM, 0);
-               if (egd_socket == -1)
-                       fatal("Couldn't create AF_UNIX socket: %s", strerror(errno));
-               if (connect(egd_socket, (struct sockaddr*)&addr, addr_len) == -1)
-                       fatal("Couldn't connect to EGD socket \"%s\": %s", addr.sun_path, strerror(errno));
-       }       
-
        if (len > 255)
                fatal("Too many bytes to read from EGD");
+
+       memset(&addr, '\0', sizeof(addr));
+       addr.sun_family = AF_UNIX;
+       strlcpy(addr.sun_path, EGD_SOCKET, sizeof(addr.sun_path));
+       addr_len = offsetof(struct sockaddr_un, sun_path) + sizeof(EGD_SOCKET);
        
+       fd = socket(AF_UNIX, SOCK_STREAM, 0);
+       if (fd == -1) {
+               error("Couldn't create AF_UNIX socket: %s", strerror(errno));
+               return(0);
+       }
+
+       if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
+               error("Couldn't connect to EGD socket \"%s\": %s", 
+                       addr.sun_path, strerror(errno));
+               close(fd);
+               return(0);
+       }
+
        /* Send blocking read request to EGD */
-       egd_message[1] = len;
+       msg[0] = 0x02;
+       msg[1] = len;
 
-       c = atomicio(write, egd_socket, egd_message, sizeof(egd_message));
-       if (c == -1)
-               fatal("Couldn't write to EGD socket \"%s\": %s", EGD_SOCKET, strerror(errno));
+       if (atomicio(write, fd, msg, sizeof(msg)) != sizeof(msg)) {
+               error("Couldn't write to EGD socket \"%s\": %s", 
+                       EGD_SOCKET, strerror(errno));
+               close(fd);
+               return(0);
+       }
 
-       c = atomicio(read, egd_socket, buf, len);
-       if (c <= 0)
-               fatal("Couldn't read from EGD socket \"%s\": %s", EGD_SOCKET, strerror(errno));
+       if (atomicio(read, fd, buf, len) != len) {
+               error("Couldn't read from EGD socket \"%s\": %s", 
+                       EGD_SOCKET, strerror(errno));
+               close(fd);
+               return(0);
+       }
        
-       close(EGD_SOCKET);
+       close(fd);
+       
+       return(1);
 }
 #else /* !EGD_SOCKET */
 #ifdef RANDOM_POOL
 /* Collect entropy from /dev/urandom or pipe */
-void get_random_bytes(unsigned char *buf, int len)
+int get_random_bytes(unsigned char *buf, int len)
 {
-       static int random_pool = -1;
-       int c;
+       int random_pool;
 
+       random_pool = open(RANDOM_POOL, O_RDONLY);
        if (random_pool == -1) {
-               random_pool = open(RANDOM_POOL, O_RDONLY);
-               if (random_pool == -1)
-                       fatal("Couldn't open random pool \"%s\": %s", RANDOM_POOL, strerror(errno));
+               error("Couldn't open random pool \"%s\": %s", 
+                       RANDOM_POOL, strerror(errno));
+               return(0);
+       }
+       
+       if (atomicio(read, random_pool, buf, len) != len) {
+               error("Couldn't read from random pool \"%s\": %s", 
+                       RANDOM_POOL, strerror(errno));
+               close(random_pool);
+               return(0);
        }
        
-       c = atomicio(read, random_pool, buf, len);
-       if (c <= 0)
-               fatal("Couldn't read from random pool \"%s\": %s", RANDOM_POOL, strerror(errno));
+       close(random_pool);
+       
+       return(1);
 }
 #endif /* RANDOM_POOL */
 #endif /* EGD_SOCKET */
 
-#if !defined(EGD_SOCKET) && !defined(RANDOM_POOL)
+/*
+ * Seed OpenSSL's random number pool from Kernel random number generator
+ * or EGD
+ */
+void
+seed_rng(void)
+{
+       char buf[32];
+       
+       debug("Seeding random number generator");
+
+       if (!get_random_bytes(buf, sizeof(buf))) {
+               if (!RAND_status())
+                       fatal("Entropy collection failed and entropy exhausted");
+       } else {
+               RAND_add(buf, sizeof(buf), sizeof(buf));
+       }
+       
+       memset(buf, '\0', sizeof(buf));
+}
+
+/* No-op */
+void init_rng(void) {}
+
+#else /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */
+
 /* 
  * FIXME: proper entropy estimations. All current values are guesses
  * FIXME: (ATL) do estimates at compile time?
  * FIXME: More entropy sources
- * FIXME: (ATL) bring in entropy sources from file
- * FIXME: (ATL) add heuristic to increase the timeout if needed
  */
 
 /* slow command timeouts (all in milliseconds) */
 /* static int entropy_timeout_default = ENTROPY_TIMEOUT_MSEC; */
 static int entropy_timeout_current = ENTROPY_TIMEOUT_MSEC;
 
-static int prng_seed_loaded = 0;
-static int prng_seed_saved = 0;                
+static int prng_seed_saved = 0;
+static int prng_initialised = 0;
+uid_t original_uid;
 
 typedef struct
 {
@@ -131,9 +196,11 @@ typedef struct
        /* Increases by factor of two each timeout */
        unsigned int sticky_badness;
        /* Path to executable */
-       const char *path;
+       char *path;
        /* argv to pass to executable */
-       const char *args[5];
+       char *args[5];
+       /* full command string (debug) */
+       char *cmdstring;
 } entropy_source_t;
 
 double stir_from_system(void);
@@ -143,67 +210,8 @@ double stir_clock(double entropy_estimate);
 double stir_rusage(int who, double entropy_estimate);
 double hash_output_from_command(entropy_source_t *src, char *hash);
 
-entropy_source_t entropy_sources[] = {
-#ifdef PROG_LS
-       { 0.002, 0, 1, PROG_LS,       { "ls", "-alni", "/var/log", NULL } },
-       { 0.002, 0, 1, PROG_LS,       { "ls", "-alni", "/var/adm", NULL } },
-       { 0.002, 0, 1, PROG_LS,       { "ls", "-alni", "/var/mail", NULL } },
-       { 0.002, 0, 1, PROG_LS,       { "ls", "-alni", "/var/spool/mail", NULL } },
-       { 0.002, 0, 1, PROG_LS,       { "ls", "-alni", "/proc", NULL } },
-       { 0.002, 0, 1, PROG_LS,       { "ls", "-alni", "/tmp", NULL } },
-#endif
-#ifdef PROG_NETSTAT
-       { 0.005, 0, 1, PROG_NETSTAT,  { "netstat","-an", NULL, NULL } },
-       { 0.010, 0, 1, PROG_NETSTAT,  { "netstat","-in", NULL, NULL } },
-       { 0.002, 0, 1, PROG_NETSTAT,  { "netstat","-rn", NULL, NULL } },
-       { 0.002, 0, 1, PROG_NETSTAT,  { "netstat","-s", NULL, NULL } },
-#endif
-#ifdef PROG_ARP
-       { 0.002, 0, 1, PROG_ARP,      { "arp","-a","-n", NULL } },
-#endif
-#ifdef PROG_IFCONFIG
-       { 0.002, 0, 1, PROG_IFCONFIG, { "ifconfig", "-a", NULL, NULL } },
-#endif
-#ifdef PROG_PS
-       { 0.003, 0, 1, PROG_PS,       { "ps", "laxww", NULL, NULL } },
-       { 0.003, 0, 1, PROG_PS,       { "ps", "-al", NULL, NULL } },
-       { 0.003, 0, 1, PROG_PS,       { "ps", "-efl", NULL, NULL } },
-#endif
-#ifdef PROG_W
-       { 0.005, 0, 1, PROG_W,        { "w", NULL, NULL, NULL } },
-#endif
-#ifdef PROG_WHO
-       { 0.001, 0, 1, PROG_WHO,      { "who","-i", NULL, NULL } },
-#endif
-#ifdef PROG_LAST
-       { 0.001, 0, 1, PROG_LAST,     { "last", NULL, NULL, NULL } },
-#endif
-#ifdef PROG_LASTLOG
-       { 0.001, 0, 1, PROG_LASTLOG,  { "lastlog", NULL, NULL, NULL } },
-#endif
-#ifdef PROG_DF
-       { 0.010, 0, 1, PROG_DF,       { "df", NULL, NULL, NULL } },
-       { 0.010, 0, 1, PROG_DF,       { "df", "-i", NULL, NULL } },
-#endif
-#ifdef PROG_VMSTAT
-       { 0.010, 0, 1, PROG_VMSTAT,   { "vmstat", NULL, NULL, NULL } },
-#endif
-#ifdef PROG_UPTIME
-       { 0.001, 0, 1, PROG_UPTIME,   { "uptime", NULL, NULL, NULL } },
-#endif
-#ifdef PROG_IPCS
-       { 0.001, 0, 1, PROG_IPCS,     { "-a", NULL, NULL, NULL } },
-#endif
-#ifdef PROG_TAIL
-       { 0.001, 0, 1, PROG_TAIL,     { "tail", "-200", "/var/log/messages", NULL, NULL } },
-       { 0.001, 0, 1, PROG_TAIL,     { "tail", "-200", "/var/log/syslog", NULL, NULL } },
-       { 0.001, 0, 1, PROG_TAIL,     { "tail", "-200", "/var/adm/messages", NULL, NULL } },
-       { 0.001, 0, 1, PROG_TAIL,     { "tail", "-200", "/var/adm/syslog", NULL, NULL } },
-       { 0.001, 0, 1, PROG_TAIL,     { "tail", "-200", "/var/log/maillog", NULL, NULL } },
-       { 0.001, 0, 1, PROG_TAIL,     { "tail", "-200", "/var/adm/maillog", NULL, NULL } },
-#endif
-       { 0.000, 0, 0, NULL,          { NULL, NULL, NULL, NULL, NULL } },
-};
+/* this is initialised from a file, by prng_read_commands() */
+entropy_source_t *entropy_sources = NULL;
 
 double 
 stir_from_system(void)
@@ -214,11 +222,11 @@ stir_from_system(void)
        total_entropy_estimate = 0;
        
        i = getpid();
-       RAND_add(&i, sizeof(i), 0.1);
+       RAND_add(&i, sizeof(i), 0.5);
        total_entropy_estimate += 0.1;
        
        i = getppid();
-       RAND_add(&i, sizeof(i), 0.1);
+       RAND_add(&i, sizeof(i), 0.5);
        total_entropy_estimate += 0.1;
 
        i = getuid();
@@ -227,7 +235,7 @@ stir_from_system(void)
        RAND_add(&i, sizeof(i), 0.0);
 
        total_entropy_estimate += stir_gettimeofday(1.0);
-       total_entropy_estimate += stir_clock(0.2);
+       total_entropy_estimate += stir_clock(0.5);
        total_entropy_estimate += stir_rusage(RUSAGE_SELF, 2.0);
 
        return(total_entropy_estimate);
@@ -242,11 +250,8 @@ stir_from_programs(void)
        double total_entropy_estimate;
        char hash[SHA_DIGEST_LENGTH];
 
-       /*
-        * Run through list of programs twice to catch differences
-        */
        total_entropy_estimate = 0;
-       for(i = 0; i < 2; i++) {
+       for(i = 0; i < NUM_ENTROPY_RUNS; i++) {
                c = 0;
                while (entropy_sources[c].path != NULL) {
 
@@ -261,16 +266,15 @@ stir_from_programs(void)
                                if (entropy_estimate > SHA_DIGEST_LENGTH)
                                        entropy_estimate = SHA_DIGEST_LENGTH;
 
-                       /* * Scale back estimates for subsequent passes through list */
-                               entropy_estimate /= 10.0 * (i + 1.0);
+                               /* Scale back estimates for subsequent passes through list */
+                               entropy_estimate /= SCALE_PER_RUN * (i + 1.0);
                        
                                /* Stir it in */
                                RAND_add(hash, sizeof(hash), entropy_estimate);
 
-/* FIXME: turn this off later */
-#if 1
-                               debug("Got %0.2f bytes of entropy from %s", entropy_estimate, 
-                                       entropy_sources[c].path);
+#ifdef DEBUG_ENTROPY
+                               debug("Got %0.2f bytes of entropy from '%s'", entropy_estimate, 
+                                       entropy_sources[c].cmdstring);
 #endif
 
                                total_entropy_estimate += entropy_estimate;
@@ -281,11 +285,9 @@ stir_from_programs(void)
                                total_entropy_estimate += stir_rusage(RUSAGE_SELF, 0.1);
                                total_entropy_estimate += stir_rusage(RUSAGE_CHILDREN, 0.1);
                        } else {
-/* FIXME: turn this off later */
-#if 1
-                               debug("Command '%s %s %s' disabled (badness %d)",
-                                       entropy_sources[c].path, entropy_sources[c].args[1],
-                                       entropy_sources[c].args[2], entropy_sources[c].badness);
+#ifdef DEBUG_ENTROPY
+                               debug("Command '%s' disabled (badness %d)",
+                                       entropy_sources[c].cmdstring, entropy_sources[c].badness);
 #endif
 
                                if (entropy_sources[c].badness > 0)
@@ -334,9 +336,9 @@ stir_rusage(int who, double entropy_estimate)
        struct rusage ru;
        
        if (getrusage(who, &ru) == -1)
-               fatal("Couldn't getrusage: %s", strerror(errno));
+               return(0);
 
-       RAND_add(&ru, sizeof(ru), 0.1);
+       RAND_add(&ru, sizeof(ru), entropy_estimate);
 
        return(entropy_estimate);
 #else /* _HAVE_GETRUSAGE */
@@ -344,6 +346,17 @@ stir_rusage(int who, double entropy_estimate)
 #endif /* _HAVE_GETRUSAGE */
 }
 
+
+static
+int
+_get_timeval_msec_difference(struct timeval *t1, struct timeval *t2) {
+       int secdiff, usecdiff;
+
+       secdiff = t2->tv_sec - t1->tv_sec;
+       usecdiff = (secdiff*1000000) + (t2->tv_usec - t1->tv_usec);
+       return (int)(usecdiff / 1000);
+}
+
 double
 hash_output_from_command(entropy_source_t *src, char *hash)
 {
@@ -351,9 +364,11 @@ hash_output_from_command(entropy_source_t *src, char *hash)
        int p[2];
        fd_set rdset;
        int cmd_eof = 0, error_abort = 0;
+       struct timeval tv_start, tv_current;
+       int msec_elapsed = 0;
        pid_t pid;
        int status;
-       char buf[2048];
+       char buf[16384];
        int bytes_read;
        int total_bytes_read;
        SHA_CTX sha;
@@ -367,6 +382,8 @@ hash_output_from_command(entropy_source_t *src, char *hash)
        if (pipe(p) == -1)
                fatal("Couldn't open pipe: %s", strerror(errno));
 
+       (void)gettimeofday(&tv_start, NULL); /* record start time */
+
        switch (pid = fork()) {
                case -1: /* Error */
                        close(p[0]);
@@ -381,10 +398,10 @@ hash_output_from_command(entropy_source_t *src, char *hash)
                        close(p[1]);
                        close(devnull);
 
+                       setuid(original_uid);
                        execv(src->path, (char**)(src->args));
-                       debug("(child) Couldn't exec '%s %s %s': %s", src->path,
-                               src->args[1], src->args[2], strerror(errno));
-                       src->badness = src->sticky_badness = 128;
+                       debug("(child) Couldn't exec '%s': %s", src->cmdstring,
+                             strerror(errno));
                        _exit(-1);
                default: /* Parent */
                        break;
@@ -401,52 +418,65 @@ hash_output_from_command(entropy_source_t *src, char *hash)
        while (!error_abort && !cmd_eof) {
                int ret;
                struct timeval tv;
+               int msec_remaining;
+
+               (void) gettimeofday(&tv_current, 0);
+               msec_elapsed = _get_timeval_msec_difference(&tv_start, &tv_current);
+               if (msec_elapsed >= entropy_timeout_current) {
+                       error_abort=1;
+                       continue;
+               }
+               msec_remaining = entropy_timeout_current - msec_elapsed;
 
                FD_ZERO(&rdset);
                FD_SET(p[0], &rdset);
-               tv.tv_sec = entropy_timeout_current / 1000;
-               tv.tv_usec = (entropy_timeout_current % 1000) * 1000;
+               tv.tv_sec =  msec_remaining / 1000;
+               tv.tv_usec = (msec_remaining % 1000) * 1000;
 
                ret = select(p[0]+1, &rdset, NULL, NULL, &tv);
+
+               RAND_add(&tv, sizeof(tv), 0.0);
+
                switch (ret) {
                case 0:
                        /* timer expired */
                        error_abort = 1;
                        break;
-                       
                case 1:
                        /* command input */
                        bytes_read = read(p[0], buf, sizeof(buf));
+                       RAND_add(&bytes_read, sizeof(&bytes_read), 0.0);
                        if (bytes_read == -1) {
                                error_abort = 1;
                                break;
+                       } else if (bytes_read) {
+                               SHA1_Update(&sha, buf, bytes_read);
+                               total_bytes_read += bytes_read;
+                       } else {
+                               cmd_eof = 1;
                        }
-                       SHA1_Update(&sha, buf, bytes_read);
-                       total_bytes_read += bytes_read;
-                       RAND_add(&bytes_read, sizeof(&bytes_read), 0.0);
-                       cmd_eof = bytes_read ? 0 : 1;
-
                        break;
-
                case -1:
                default:
-                       error("Command '%s %s': select() failed: %s", src->path, src->args[1],
-                               strerror(errno));
+                       /* error */
+                       debug("Command '%s': select() failed: %s", src->cmdstring,
+                             strerror(errno));
                        error_abort = 1;
                        break;
-               } /* switch ret */
-
-               RAND_add(&tv, sizeof(&tv), 0.0);
-       } /* while !error_abort && !cmd_eof */
+               }
+       }
 
        SHA1_Final(hash, &sha);
 
        close(p[0]);
+
+#ifdef DEBUG_ENTROPY
+       debug("Time elapsed: %d msec", msec_elapsed);
+#endif
        
        if (waitpid(pid, &status, 0) == -1) {
-               error("Couldn't wait for child '%s %s' completion: %s", src->path,
-                       src->args[1], strerror(errno));
-               /* return(-1); */ /* FIXME: (ATL) this doesn't feel right */
+              debug("Couldn't wait for child '%s' completion: %s", src->cmdstring,
+                    strerror(errno));
                return(0.0);
        }
 
@@ -456,7 +486,7 @@ hash_output_from_command(entropy_source_t *src, char *hash)
                /* closing p[0] on timeout causes the entropy command to
                 * SIGPIPE. Take whatever output we got, and mark this command
                 * as slow */
-               debug("Command %s %s timed out", src->path, src->args[1]);
+               debug("Command '%s' timed out", src->cmdstring);
                src->sticky_badness *= 2;
                src->badness = src->sticky_badness;
                return(total_bytes_read);
@@ -466,12 +496,14 @@ hash_output_from_command(entropy_source_t *src, char *hash)
                if (WEXITSTATUS(status)==0) {
                        return(total_bytes_read);
                } else {
-                       debug("Exit status was %d", WEXITSTATUS(status));
+                       debug("Command '%s' exit status was %d", src->cmdstring, 
+                               WEXITSTATUS(status));
                        src->badness = src->sticky_badness = 128;
                        return (0.0);
                }
        } else if (WIFSIGNALED(status)) {
-               debug("Returned on uncaught signal %d !", status);
+               debug("Command '%s' returned on uncaught signal %d !", src->cmdstring, 
+                       status);
                src->badness = src->sticky_badness = 128;
                return(0.0);
        } else
@@ -502,7 +534,7 @@ prng_check_seedfile(char *filename) {
                fatal("PRNG seedfile %.100s is not a regular file", filename);
 
        /* mode 0600, owned by root or the current user? */
-       if (((st.st_mode & 0177) != 0) || !(st.st_uid == geteuid()))
+       if (((st.st_mode & 0177) != 0) || !(st.st_uid == original_uid))
                fatal("PRNG seedfile %.100s must be mode 0600, owned by uid %d",
                         filename, getuid());
 
@@ -520,10 +552,14 @@ prng_write_seedfile(void) {
        if (prng_seed_saved)
                return;
        
-       pw = getpwuid(getuid());
+       setuid(original_uid);
+       
+       prng_seed_saved = 1;
+       
+       pw = getpwuid(original_uid);
        if (pw == NULL)
                fatal("Couldn't get password entry for current user (%i): %s", 
-                       getuid(), strerror(errno));
+                       original_uid, strerror(errno));
                                
        /* Try to ensure that the parent directory is there */
        snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, 
@@ -558,10 +594,10 @@ prng_read_seedfile(void) {
        char filename[1024];
        struct passwd *pw;
        
-       pw = getpwuid(getuid());
+       pw = getpwuid(original_uid);
        if (pw == NULL)
                fatal("Couldn't get password entry for current user (%i): %s", 
-                       getuid(), strerror(errno));
+                       original_uid, strerror(errno));
                        
        snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, 
                SSH_PRNG_SEED_FILE);
@@ -595,26 +631,137 @@ prng_read_seedfile(void) {
        RAND_add(&seed, sizeof(seed), 0.0);
 }
 
-#endif /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */
-
-#if defined(EGD_SOCKET) || defined(RANDOM_POOL)
 
 /*
- * Seed OpenSSL's random number pool from Kernel random number generator
- * or EGD
+ * entropy command initialisation functions
  */
-void
-seed_rng(void)
+int
+prng_read_commands(char *cmdfilename)
 {
-       char buf[32];
-       
-       debug("Seeding random number generator");
-       get_random_bytes(buf, sizeof(buf));
-       RAND_add(buf, sizeof(buf), sizeof(buf));
-       memset(buf, '\0', sizeof(buf));
-}
+       FILE *f;
+       char *cp;
+       char line[1024];
+       char cmd[1024];
+       char path[256];
+       int linenum;
+       int num_cmds = 64;
+       int cur_cmd = 0;
+       double est;
+       entropy_source_t *entcmd;
+
+       f = fopen(cmdfilename, "r");
+       if (!f) {
+               fatal("couldn't read entropy commands file %.100s: %.100s",
+                   cmdfilename, strerror(errno));
+       }
 
-#else /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */
+       entcmd = (entropy_source_t *)xmalloc(num_cmds * sizeof(entropy_source_t));
+       memset(entcmd, '\0', num_cmds * sizeof(entropy_source_t));
+
+       /* Read in file */
+       linenum = 0;
+       while (fgets(line, sizeof(line), f)) {
+               int arg;
+               char *argv;
+
+               linenum++;
+
+               /* skip leading whitespace, test for blank line or comment */
+               cp = line + strspn(line, WHITESPACE);
+               if ((*cp == 0) || (*cp == '#'))
+                       continue; /* done with this line */
+
+               /* First non-whitespace char should be double quote delimiting */
+               /* commandline */
+               if (*cp != '"') {
+                       error("bad entropy command, %.100s line %d", cmdfilename,
+                            linenum);
+                       continue;
+               }               
+
+               /* first token, command args (incl. argv[0]) in double quotes */
+               cp = strtok(cp, "\"");
+               if (cp == NULL) {
+                       error("missing or bad command string, %.100s line %d -- ignored",
+                             cmdfilename, linenum);
+                       continue;
+               }
+               strlcpy(cmd, cp, sizeof(cmd));
+               
+               /* second token, full command path */
+               if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
+                       error("missing command path, %.100s line %d -- ignored",
+                             cmdfilename, linenum);
+                       continue;
+               }
+
+               /* did configure mark this as dead? */
+               if (strncmp("undef", cp, 5) == 0)
+                       continue;
+
+               strlcpy(path, cp, sizeof(path));                        
+
+               /* third token, entropy rate estimate for this command */
+               if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
+                       error("missing entropy estimate, %.100s line %d -- ignored",
+                             cmdfilename, linenum);
+                       continue;
+               }
+               est = strtod(cp, &argv);
+
+               /* end of line */
+               if ((cp = strtok(NULL, WHITESPACE)) != NULL) {
+                       error("garbage at end of line %d in %.100s -- ignored", linenum, 
+                               cmdfilename);
+                       continue;
+               }
+
+               /* save the command for debug messages */
+               entcmd[cur_cmd].cmdstring = xstrdup(cmd);
+                       
+               /* split the command args */
+               cp = strtok(cmd, WHITESPACE);
+               arg = 0;
+               argv = NULL;
+               do {
+                       char *s = (char*)xmalloc(strlen(cp) + 1);
+                       strncpy(s, cp, strlen(cp) + 1);
+                       entcmd[cur_cmd].args[arg] = s;
+                       arg++;
+               } while ((arg < 5) && (cp = strtok(NULL, WHITESPACE)));
+               
+               if (strtok(NULL, WHITESPACE))
+                       error("ignored extra command elements (max 5), %.100s line %d",
+                             cmdfilename, linenum);
+
+               /* Copy the command path and rate estimate */
+               entcmd[cur_cmd].path = xstrdup(path);
+               entcmd[cur_cmd].rate = est;
+
+               /* Initialise other values */
+               entcmd[cur_cmd].sticky_badness = 1;
+
+               cur_cmd++;
+
+               /* If we've filled the array, reallocate it twice the size */
+               /* Do this now because even if this we're on the last command,
+                  we need another slot to mark the last entry */
+               if (cur_cmd == num_cmds) {
+                       num_cmds *= 2;
+                       entcmd = xrealloc(entcmd, num_cmds * sizeof(entropy_source_t));
+               }
+       }
+
+       /* zero the last entry */
+       memset(&entcmd[cur_cmd], '\0', sizeof(entropy_source_t));
+
+       /* trim to size */
+       entropy_sources = xrealloc(entcmd, (cur_cmd+1) * sizeof(entropy_source_t));
+
+       debug("Loaded %d entropy commands from %.100s", cur_cmd, cmdfilename);
+
+       return (cur_cmd >= MIN_ENTROPY_SOURCES);
+}
 
 /*
  * Write a keyfile at exit
@@ -632,19 +779,42 @@ prng_seed_cleanup(void *junk)
 void
 seed_rng(void)
 {
-       debug("Seeding random number generator.");
-       debug("OpenSSL random status is now %i\n", RAND_status());
-       debug("%i bytes from system calls", (int)stir_from_system());
-       debug("%i bytes from programs", (int)stir_from_programs());
-       debug("OpenSSL random status is now %i\n", RAND_status());
-
-       if (!prng_seed_loaded)
-       {
-               prng_seed_loaded = 1;
-               prng_seed_saved = 0;            
-               prng_read_seedfile();
-               fatal_add_cleanup(prng_seed_cleanup, NULL);
-               atexit(prng_write_seedfile);
-       }
+       void *old_sigchld_handler;
+
+       if (!prng_initialised)
+               fatal("RNG not initialised");
+       
+       /* Make sure some other sigchld handler doesn't reap our entropy */
+       /* commands */
+       old_sigchld_handler = signal(SIGCHLD, SIG_DFL);
+
+       debug("Seeded RNG with %i bytes from programs", (int)stir_from_programs());
+       debug("Seeded RNG with %i bytes from system calls", (int)stir_from_system());
+
+       if (!RAND_status())
+               fatal("Not enough entropy in RNG");
+
+       signal(SIGCHLD, old_sigchld_handler);
+
+       if (!RAND_status())
+               fatal("Couldn't initialise builtin random number generator -- exiting.");
 }
+
+void init_rng(void) 
+{
+       original_uid = getuid();
+
+       /* Read in collection commands */
+       if (!prng_read_commands(SSH_PRNG_COMMAND_FILE))
+               fatal("PRNG initialisation failed -- exiting.");
+
+       /* Set ourselves up to save a seed upon exit */
+       prng_seed_saved = 0;            
+       prng_read_seedfile();
+       fatal_add_cleanup(prng_seed_cleanup, NULL);
+       atexit(prng_write_seedfile);
+
+       prng_initialised = 1;
+}
+
 #endif /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */
This page took 0.242349 seconds and 4 git commands to generate.