Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 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. |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 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 | |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 27 | #include <openssl/rand.h> |
| 28 | #include <openssl/sha.h> |
Damien Miller | 767c7fc | 2001-02-27 09:20:57 +1100 | [diff] [blame] | 29 | #include <openssl/crypto.h> |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 30 | |
Damien Miller | ecbb26d | 2000-07-15 14:59:14 +1000 | [diff] [blame] | 31 | /* SunOS 4.4.4 needs this */ |
| 32 | #ifdef HAVE_FLOATINGPOINT_H |
| 33 | # include <floatingpoint.h> |
| 34 | #endif /* HAVE_FLOATINGPOINT_H */ |
| 35 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 36 | #include "ssh.h" |
Damien Miller | a1072a8 | 2001-02-18 15:28:11 +1100 | [diff] [blame] | 37 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 38 | #include "xmalloc.h" |
| 39 | #include "atomicio.h" |
Ben Lindstrom | cb57733 | 2001-01-22 21:06:19 +0000 | [diff] [blame] | 40 | #include "pathnames.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 41 | #include "log.h" |
| 42 | |
Damien Miller | 248131a | 2001-02-27 09:47:16 +1100 | [diff] [blame] | 43 | RCSID("$Id: entropy.c,v 1.33 2001/02/26 22:47:16 djm Exp $"); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 44 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 45 | #ifndef offsetof |
| 46 | # define offsetof(type, member) ((size_t) &((type *)0)->member) |
| 47 | #endif |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 48 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 49 | /* Number of times to pass through command list gathering entropy */ |
| 50 | #define NUM_ENTROPY_RUNS 1 |
| 51 | |
| 52 | /* Scale entropy estimates back by this amount on subsequent runs */ |
| 53 | #define SCALE_PER_RUN 10.0 |
| 54 | |
| 55 | /* Minimum number of commands to be considered valid */ |
| 56 | #define MIN_ENTROPY_SOURCES 16 |
| 57 | |
| 58 | #define WHITESPACE " \t\n" |
| 59 | |
Damien Miller | 7b22d65 | 2000-06-18 14:07:04 +1000 | [diff] [blame] | 60 | #ifndef RUSAGE_SELF |
| 61 | # define RUSAGE_SELF 0 |
| 62 | #endif |
| 63 | #ifndef RUSAGE_CHILDREN |
| 64 | # define RUSAGE_CHILDREN 0 |
| 65 | #endif |
| 66 | |
Damien Miller | fbd884a | 2001-02-27 08:39:07 +1100 | [diff] [blame] | 67 | #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS) |
| 68 | # define SAVED_IDS_WORK_WITH_SETEUID |
| 69 | #endif |
| 70 | |
Damien Miller | 767c7fc | 2001-02-27 09:20:57 +1100 | [diff] [blame] | 71 | void check_openssl_version(void) |
| 72 | { |
| 73 | if (SSLeay() != OPENSSL_VERSION_NUMBER) |
| 74 | fatal("OpenSSL version mismatch. Built against %x, you " |
| 75 | "have %x", OPENSSL_VERSION_NUMBER, SSLeay()); |
| 76 | } |
| 77 | |
| 78 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 79 | #if defined(EGD_SOCKET) || defined(RANDOM_POOL) |
| 80 | |
| 81 | #ifdef EGD_SOCKET |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 82 | /* Collect entropy from EGD */ |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 83 | int get_random_bytes(unsigned char *buf, int len) |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 84 | { |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 85 | int fd; |
| 86 | char msg[2]; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 87 | struct sockaddr_un addr; |
Damien Miller | b3ffc5f | 2001-02-18 12:44:29 +1100 | [diff] [blame] | 88 | int addr_len, rval, errors; |
Damien Miller | a1072a8 | 2001-02-18 15:28:11 +1100 | [diff] [blame] | 89 | mysig_t old_sigpipe; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 90 | |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 91 | /* Sanity checks */ |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 92 | if (sizeof(EGD_SOCKET) > sizeof(addr.sun_path)) |
| 93 | fatal("Random pool path is too long"); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 94 | if (len > 255) |
| 95 | fatal("Too many bytes to read from EGD"); |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 96 | |
| 97 | memset(&addr, '\0', sizeof(addr)); |
| 98 | addr.sun_family = AF_UNIX; |
| 99 | strlcpy(addr.sun_path, EGD_SOCKET, sizeof(addr.sun_path)); |
| 100 | addr_len = offsetof(struct sockaddr_un, sun_path) + sizeof(EGD_SOCKET); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 101 | |
Damien Miller | a1072a8 | 2001-02-18 15:28:11 +1100 | [diff] [blame] | 102 | old_sigpipe = mysignal(SIGPIPE, SIG_IGN); |
Damien Miller | b3ffc5f | 2001-02-18 12:44:29 +1100 | [diff] [blame] | 103 | |
| 104 | errors = rval = 0; |
| 105 | reopen: |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 106 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 107 | if (fd == -1) { |
| 108 | error("Couldn't create AF_UNIX socket: %s", strerror(errno)); |
Damien Miller | b3ffc5f | 2001-02-18 12:44:29 +1100 | [diff] [blame] | 109 | goto done; |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 113 | error("Couldn't connect to EGD socket \"%s\": %s", |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 114 | addr.sun_path, strerror(errno)); |
Damien Miller | b3ffc5f | 2001-02-18 12:44:29 +1100 | [diff] [blame] | 115 | goto done; |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 116 | } |
| 117 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 118 | /* Send blocking read request to EGD */ |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 119 | msg[0] = 0x02; |
| 120 | msg[1] = len; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 121 | |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 122 | if (atomicio(write, fd, msg, sizeof(msg)) != sizeof(msg)) { |
Damien Miller | b3ffc5f | 2001-02-18 12:44:29 +1100 | [diff] [blame] | 123 | if (errno == EPIPE && errors < 10) { |
| 124 | close(fd); |
| 125 | errors++; |
| 126 | goto reopen; |
| 127 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 128 | error("Couldn't write to EGD socket \"%s\": %s", |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 129 | EGD_SOCKET, strerror(errno)); |
Damien Miller | b3ffc5f | 2001-02-18 12:44:29 +1100 | [diff] [blame] | 130 | goto done; |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 131 | } |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 132 | |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 133 | if (atomicio(read, fd, buf, len) != len) { |
Damien Miller | b3ffc5f | 2001-02-18 12:44:29 +1100 | [diff] [blame] | 134 | if (errno == EPIPE && errors < 10) { |
| 135 | close(fd); |
| 136 | errors++; |
| 137 | goto reopen; |
| 138 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 139 | error("Couldn't read from EGD socket \"%s\": %s", |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 140 | EGD_SOCKET, strerror(errno)); |
Damien Miller | b3ffc5f | 2001-02-18 12:44:29 +1100 | [diff] [blame] | 141 | goto done; |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 142 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 143 | |
Damien Miller | b3ffc5f | 2001-02-18 12:44:29 +1100 | [diff] [blame] | 144 | rval = 1; |
| 145 | done: |
Kevin Steves | 4679f5b | 2001-02-18 11:34:32 +0000 | [diff] [blame] | 146 | mysignal(SIGPIPE, old_sigpipe); |
Damien Miller | b3ffc5f | 2001-02-18 12:44:29 +1100 | [diff] [blame] | 147 | if (fd != -1) |
| 148 | close(fd); |
| 149 | return(rval); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 150 | } |
| 151 | #else /* !EGD_SOCKET */ |
| 152 | #ifdef RANDOM_POOL |
| 153 | /* Collect entropy from /dev/urandom or pipe */ |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 154 | int get_random_bytes(unsigned char *buf, int len) |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 155 | { |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 156 | int random_pool; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 157 | |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 158 | random_pool = open(RANDOM_POOL, O_RDONLY); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 159 | if (random_pool == -1) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 160 | error("Couldn't open random pool \"%s\": %s", |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 161 | RANDOM_POOL, strerror(errno)); |
| 162 | return(0); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 163 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 164 | |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 165 | if (atomicio(read, random_pool, buf, len) != len) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 166 | error("Couldn't read from random pool \"%s\": %s", |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 167 | RANDOM_POOL, strerror(errno)); |
| 168 | close(random_pool); |
| 169 | return(0); |
| 170 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 171 | |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 172 | close(random_pool); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 173 | |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 174 | return(1); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 175 | } |
| 176 | #endif /* RANDOM_POOL */ |
| 177 | #endif /* EGD_SOCKET */ |
| 178 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 179 | /* |
| 180 | * Seed OpenSSL's random number pool from Kernel random number generator |
| 181 | * or EGD |
| 182 | */ |
| 183 | void |
| 184 | seed_rng(void) |
| 185 | { |
| 186 | char buf[32]; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 187 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 188 | debug("Seeding random number generator"); |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 189 | |
Damien Miller | 0800647 | 2000-06-26 13:55:31 +1000 | [diff] [blame] | 190 | if (!get_random_bytes(buf, sizeof(buf))) { |
| 191 | if (!RAND_status()) |
| 192 | fatal("Entropy collection failed and entropy exhausted"); |
| 193 | } else { |
| 194 | RAND_add(buf, sizeof(buf), sizeof(buf)); |
| 195 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 196 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 197 | memset(buf, '\0', sizeof(buf)); |
| 198 | } |
| 199 | |
Damien Miller | 767c7fc | 2001-02-27 09:20:57 +1100 | [diff] [blame] | 200 | void init_rng(void) |
| 201 | { |
| 202 | check_openssl_version(); |
| 203 | } |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 204 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 205 | #else /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */ |
| 206 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 207 | /* |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 208 | * FIXME: proper entropy estimations. All current values are guesses |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 209 | * FIXME: (ATL) do estimates at compile time? |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 210 | * FIXME: More entropy sources |
| 211 | */ |
| 212 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 213 | /* slow command timeouts (all in milliseconds) */ |
| 214 | /* static int entropy_timeout_default = ENTROPY_TIMEOUT_MSEC; */ |
| 215 | static int entropy_timeout_current = ENTROPY_TIMEOUT_MSEC; |
| 216 | |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 217 | static int prng_seed_saved = 0; |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 218 | static int prng_initialised = 0; |
| 219 | uid_t original_uid; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 220 | |
| 221 | typedef struct |
| 222 | { |
| 223 | /* Proportion of data that is entropy */ |
| 224 | double rate; |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 225 | /* Counter goes positive if this command times out */ |
| 226 | unsigned int badness; |
| 227 | /* Increases by factor of two each timeout */ |
| 228 | unsigned int sticky_badness; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 229 | /* Path to executable */ |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 230 | char *path; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 231 | /* argv to pass to executable */ |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 232 | char *args[5]; |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 233 | /* full command string (debug) */ |
| 234 | char *cmdstring; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 235 | } entropy_source_t; |
| 236 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 237 | double stir_from_system(void); |
| 238 | double stir_from_programs(void); |
| 239 | double stir_gettimeofday(double entropy_estimate); |
| 240 | double stir_clock(double entropy_estimate); |
| 241 | double stir_rusage(int who, double entropy_estimate); |
| 242 | double hash_output_from_command(entropy_source_t *src, char *hash); |
| 243 | |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 244 | /* this is initialised from a file, by prng_read_commands() */ |
| 245 | entropy_source_t *entropy_sources = NULL; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 246 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 247 | double |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 248 | stir_from_system(void) |
| 249 | { |
| 250 | double total_entropy_estimate; |
| 251 | long int i; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 252 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 253 | total_entropy_estimate = 0; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 254 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 255 | i = getpid(); |
Damien Miller | 7b22d65 | 2000-06-18 14:07:04 +1000 | [diff] [blame] | 256 | RAND_add(&i, sizeof(i), 0.5); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 257 | total_entropy_estimate += 0.1; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 258 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 259 | i = getppid(); |
Damien Miller | 7b22d65 | 2000-06-18 14:07:04 +1000 | [diff] [blame] | 260 | RAND_add(&i, sizeof(i), 0.5); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 261 | total_entropy_estimate += 0.1; |
| 262 | |
| 263 | i = getuid(); |
| 264 | RAND_add(&i, sizeof(i), 0.0); |
| 265 | i = getgid(); |
| 266 | RAND_add(&i, sizeof(i), 0.0); |
| 267 | |
| 268 | total_entropy_estimate += stir_gettimeofday(1.0); |
Damien Miller | 7b22d65 | 2000-06-18 14:07:04 +1000 | [diff] [blame] | 269 | total_entropy_estimate += stir_clock(0.5); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 270 | total_entropy_estimate += stir_rusage(RUSAGE_SELF, 2.0); |
| 271 | |
| 272 | return(total_entropy_estimate); |
| 273 | } |
| 274 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 275 | double |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 276 | stir_from_programs(void) |
| 277 | { |
| 278 | int i; |
| 279 | int c; |
| 280 | double entropy_estimate; |
| 281 | double total_entropy_estimate; |
| 282 | char hash[SHA_DIGEST_LENGTH]; |
| 283 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 284 | total_entropy_estimate = 0; |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 285 | for(i = 0; i < NUM_ENTROPY_RUNS; i++) { |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 286 | c = 0; |
| 287 | while (entropy_sources[c].path != NULL) { |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 288 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 289 | if (!entropy_sources[c].badness) { |
| 290 | /* Hash output from command */ |
| 291 | entropy_estimate = hash_output_from_command(&entropy_sources[c], hash); |
| 292 | |
| 293 | /* Scale back entropy estimate according to command's rate */ |
| 294 | entropy_estimate *= entropy_sources[c].rate; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 295 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 296 | /* Upper bound of entropy estimate is SHA_DIGEST_LENGTH */ |
| 297 | if (entropy_estimate > SHA_DIGEST_LENGTH) |
| 298 | entropy_estimate = SHA_DIGEST_LENGTH; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 299 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 300 | /* Scale back estimates for subsequent passes through list */ |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 301 | entropy_estimate /= SCALE_PER_RUN * (i + 1.0); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 302 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 303 | /* Stir it in */ |
| 304 | RAND_add(hash, sizeof(hash), entropy_estimate); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 305 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 306 | debug3("Got %0.2f bytes of entropy from '%s'", entropy_estimate, |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 307 | entropy_sources[c].cmdstring); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 308 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 309 | total_entropy_estimate += entropy_estimate; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 310 | |
| 311 | /* Execution times should be a little unpredictable */ |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 312 | total_entropy_estimate += stir_gettimeofday(0.05); |
| 313 | total_entropy_estimate += stir_clock(0.05); |
| 314 | total_entropy_estimate += stir_rusage(RUSAGE_SELF, 0.1); |
| 315 | total_entropy_estimate += stir_rusage(RUSAGE_CHILDREN, 0.1); |
| 316 | } else { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 317 | debug2("Command '%s' disabled (badness %d)", |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 318 | entropy_sources[c].cmdstring, entropy_sources[c].badness); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 319 | |
| 320 | if (entropy_sources[c].badness > 0) |
| 321 | entropy_sources[c].badness--; |
| 322 | } |
| 323 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 324 | c++; |
| 325 | } |
| 326 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 327 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 328 | return(total_entropy_estimate); |
| 329 | } |
| 330 | |
| 331 | double |
| 332 | stir_gettimeofday(double entropy_estimate) |
| 333 | { |
| 334 | struct timeval tv; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 335 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 336 | if (gettimeofday(&tv, NULL) == -1) |
| 337 | fatal("Couldn't gettimeofday: %s", strerror(errno)); |
| 338 | |
| 339 | RAND_add(&tv, sizeof(tv), entropy_estimate); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 340 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 341 | return(entropy_estimate); |
| 342 | } |
| 343 | |
| 344 | double |
| 345 | stir_clock(double entropy_estimate) |
| 346 | { |
| 347 | #ifdef HAVE_CLOCK |
| 348 | clock_t c; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 349 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 350 | c = clock(); |
| 351 | RAND_add(&c, sizeof(c), entropy_estimate); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 352 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 353 | return(entropy_estimate); |
| 354 | #else /* _HAVE_CLOCK */ |
| 355 | return(0); |
| 356 | #endif /* _HAVE_CLOCK */ |
| 357 | } |
| 358 | |
| 359 | double |
| 360 | stir_rusage(int who, double entropy_estimate) |
| 361 | { |
| 362 | #ifdef HAVE_GETRUSAGE |
| 363 | struct rusage ru; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 364 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 365 | if (getrusage(who, &ru) == -1) |
Damien Miller | 7b22d65 | 2000-06-18 14:07:04 +1000 | [diff] [blame] | 366 | return(0); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 367 | |
Damien Miller | 7b22d65 | 2000-06-18 14:07:04 +1000 | [diff] [blame] | 368 | RAND_add(&ru, sizeof(ru), entropy_estimate); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 369 | |
| 370 | return(entropy_estimate); |
| 371 | #else /* _HAVE_GETRUSAGE */ |
| 372 | return(0); |
| 373 | #endif /* _HAVE_GETRUSAGE */ |
| 374 | } |
| 375 | |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 376 | |
| 377 | static |
| 378 | int |
| 379 | _get_timeval_msec_difference(struct timeval *t1, struct timeval *t2) { |
| 380 | int secdiff, usecdiff; |
| 381 | |
| 382 | secdiff = t2->tv_sec - t1->tv_sec; |
| 383 | usecdiff = (secdiff*1000000) + (t2->tv_usec - t1->tv_usec); |
| 384 | return (int)(usecdiff / 1000); |
| 385 | } |
| 386 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 387 | double |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 388 | hash_output_from_command(entropy_source_t *src, char *hash) |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 389 | { |
| 390 | static int devnull = -1; |
| 391 | int p[2]; |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 392 | fd_set rdset; |
| 393 | int cmd_eof = 0, error_abort = 0; |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 394 | struct timeval tv_start, tv_current; |
| 395 | int msec_elapsed = 0; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 396 | pid_t pid; |
| 397 | int status; |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 398 | char buf[16384]; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 399 | int bytes_read; |
| 400 | int total_bytes_read; |
| 401 | SHA_CTX sha; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 402 | |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 403 | debug3("Reading output from \'%s\'", src->cmdstring); |
| 404 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 405 | if (devnull == -1) { |
| 406 | devnull = open("/dev/null", O_RDWR); |
| 407 | if (devnull == -1) |
| 408 | fatal("Couldn't open /dev/null: %s", strerror(errno)); |
| 409 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 410 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 411 | if (pipe(p) == -1) |
| 412 | fatal("Couldn't open pipe: %s", strerror(errno)); |
| 413 | |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 414 | (void)gettimeofday(&tv_start, NULL); /* record start time */ |
| 415 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 416 | switch (pid = fork()) { |
| 417 | case -1: /* Error */ |
| 418 | close(p[0]); |
| 419 | close(p[1]); |
| 420 | fatal("Couldn't fork: %s", strerror(errno)); |
| 421 | /* NOTREACHED */ |
| 422 | case 0: /* Child */ |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 423 | dup2(devnull, STDIN_FILENO); |
| 424 | dup2(p[1], STDOUT_FILENO); |
| 425 | dup2(p[1], STDERR_FILENO); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 426 | close(p[0]); |
| 427 | close(p[1]); |
| 428 | close(devnull); |
| 429 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 430 | setuid(original_uid); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 431 | execv(src->path, (char**)(src->args)); |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 432 | debug("(child) Couldn't exec '%s': %s", src->cmdstring, |
| 433 | strerror(errno)); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 434 | _exit(-1); |
| 435 | default: /* Parent */ |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | RAND_add(&pid, sizeof(&pid), 0.0); |
| 440 | |
| 441 | close(p[1]); |
| 442 | |
| 443 | /* Hash output from child */ |
| 444 | SHA1_Init(&sha); |
| 445 | total_bytes_read = 0; |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 446 | |
| 447 | while (!error_abort && !cmd_eof) { |
| 448 | int ret; |
| 449 | struct timeval tv; |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 450 | int msec_remaining; |
| 451 | |
| 452 | (void) gettimeofday(&tv_current, 0); |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 453 | msec_elapsed = _get_timeval_msec_difference(&tv_start, &tv_current); |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 454 | if (msec_elapsed >= entropy_timeout_current) { |
| 455 | error_abort=1; |
| 456 | continue; |
| 457 | } |
| 458 | msec_remaining = entropy_timeout_current - msec_elapsed; |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 459 | |
| 460 | FD_ZERO(&rdset); |
| 461 | FD_SET(p[0], &rdset); |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 462 | tv.tv_sec = msec_remaining / 1000; |
| 463 | tv.tv_usec = (msec_remaining % 1000) * 1000; |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 464 | |
| 465 | ret = select(p[0]+1, &rdset, NULL, NULL, &tv); |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 466 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 467 | RAND_add(&tv, sizeof(tv), 0.0); |
| 468 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 469 | switch (ret) { |
| 470 | case 0: |
| 471 | /* timer expired */ |
| 472 | error_abort = 1; |
| 473 | break; |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 474 | case 1: |
| 475 | /* command input */ |
| 476 | bytes_read = read(p[0], buf, sizeof(buf)); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 477 | RAND_add(&bytes_read, sizeof(&bytes_read), 0.0); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 478 | if (bytes_read == -1) { |
| 479 | error_abort = 1; |
| 480 | break; |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 481 | } else if (bytes_read) { |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 482 | SHA1_Update(&sha, buf, bytes_read); |
| 483 | total_bytes_read += bytes_read; |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 484 | } else { |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 485 | cmd_eof = 1; |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 486 | } |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 487 | break; |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 488 | case -1: |
| 489 | default: |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 490 | /* error */ |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 491 | debug("Command '%s': select() failed: %s", src->cmdstring, |
| 492 | strerror(errno)); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 493 | error_abort = 1; |
| 494 | break; |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 495 | } |
| 496 | } |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 497 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 498 | SHA1_Final(hash, &sha); |
| 499 | |
| 500 | close(p[0]); |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 501 | |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 502 | debug3("Time elapsed: %d msec", msec_elapsed); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 503 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 504 | if (waitpid(pid, &status, 0) == -1) { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 505 | error("Couldn't wait for child '%s' completion: %s", src->cmdstring, |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 506 | strerror(errno)); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 507 | return(0.0); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | RAND_add(&status, sizeof(&status), 0.0); |
| 511 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 512 | if (error_abort) { |
| 513 | /* closing p[0] on timeout causes the entropy command to |
| 514 | * SIGPIPE. Take whatever output we got, and mark this command |
| 515 | * as slow */ |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 516 | debug2("Command '%s' timed out", src->cmdstring); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 517 | src->sticky_badness *= 2; |
| 518 | src->badness = src->sticky_badness; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 519 | return(total_bytes_read); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | if (WIFEXITED(status)) { |
| 523 | if (WEXITSTATUS(status)==0) { |
| 524 | return(total_bytes_read); |
| 525 | } else { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 526 | debug2("Command '%s' exit status was %d", src->cmdstring, |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 527 | WEXITSTATUS(status)); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 528 | src->badness = src->sticky_badness = 128; |
| 529 | return (0.0); |
| 530 | } |
| 531 | } else if (WIFSIGNALED(status)) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 532 | debug2("Command '%s' returned on uncaught signal %d !", src->cmdstring, |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 533 | status); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 534 | src->badness = src->sticky_badness = 128; |
| 535 | return(0.0); |
| 536 | } else |
| 537 | return(0.0); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 538 | } |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 539 | |
| 540 | /* |
| 541 | * prng seedfile functions |
| 542 | */ |
| 543 | int |
| 544 | prng_check_seedfile(char *filename) { |
| 545 | |
| 546 | struct stat st; |
| 547 | |
| 548 | /* FIXME raceable: eg replace seed between this stat and subsequent open */ |
| 549 | /* Not such a problem because we don't trust the seed file anyway */ |
| 550 | if (lstat(filename, &st) == -1) { |
Damien Miller | 52dc96b | 2000-10-16 20:13:43 +1100 | [diff] [blame] | 551 | /* Give up on hard errors */ |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 552 | if (errno != ENOENT) |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 553 | debug("WARNING: Couldn't stat random seed file \"%s\": %s", |
Damien Miller | 52dc96b | 2000-10-16 20:13:43 +1100 | [diff] [blame] | 554 | filename, strerror(errno)); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 555 | |
| 556 | return(0); |
| 557 | } |
| 558 | |
| 559 | /* regular file? */ |
| 560 | if (!S_ISREG(st.st_mode)) |
| 561 | fatal("PRNG seedfile %.100s is not a regular file", filename); |
| 562 | |
| 563 | /* mode 0600, owned by root or the current user? */ |
Damien Miller | 52dc96b | 2000-10-16 20:13:43 +1100 | [diff] [blame] | 564 | if (((st.st_mode & 0177) != 0) || !(st.st_uid == original_uid)) { |
| 565 | debug("WARNING: PRNG seedfile %.100s must be mode 0600, owned by uid %d", |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 566 | filename, getuid()); |
Damien Miller | 52dc96b | 2000-10-16 20:13:43 +1100 | [diff] [blame] | 567 | return(0); |
| 568 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 569 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 570 | return(1); |
| 571 | } |
| 572 | |
| 573 | void |
| 574 | prng_write_seedfile(void) { |
| 575 | int fd; |
| 576 | char seed[1024]; |
| 577 | char filename[1024]; |
| 578 | struct passwd *pw; |
| 579 | |
| 580 | /* Don't bother if we have already saved a seed */ |
| 581 | if (prng_seed_saved) |
| 582 | return; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 583 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 584 | setuid(original_uid); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 585 | |
Damien Miller | fc0b11b | 2000-05-02 00:03:55 +1000 | [diff] [blame] | 586 | prng_seed_saved = 1; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 587 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 588 | pw = getpwuid(original_uid); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 589 | if (pw == NULL) |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 590 | fatal("Couldn't get password entry for current user (%i): %s", |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 591 | original_uid, strerror(errno)); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 592 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 593 | /* Try to ensure that the parent directory is there */ |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 594 | snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, |
Ben Lindstrom | cb57733 | 2001-01-22 21:06:19 +0000 | [diff] [blame] | 595 | _PATH_SSH_USER_DIR); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 596 | mkdir(filename, 0700); |
| 597 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 598 | snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 599 | SSH_PRNG_SEED_FILE); |
| 600 | |
| 601 | debug("writing PRNG seed to file %.100s", filename); |
| 602 | |
| 603 | RAND_bytes(seed, sizeof(seed)); |
| 604 | |
| 605 | /* Don't care if the seed doesn't exist */ |
| 606 | prng_check_seedfile(filename); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 607 | |
Damien Miller | 52dc96b | 2000-10-16 20:13:43 +1100 | [diff] [blame] | 608 | if ((fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0600)) == -1) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 609 | debug("WARNING: couldn't access PRNG seedfile %.100s (%.100s)", |
Damien Miller | 52dc96b | 2000-10-16 20:13:43 +1100 | [diff] [blame] | 610 | filename, strerror(errno)); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 611 | } else { |
Damien Miller | 52dc96b | 2000-10-16 20:13:43 +1100 | [diff] [blame] | 612 | if (atomicio(write, fd, &seed, sizeof(seed)) != sizeof(seed)) |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 613 | fatal("problem writing PRNG seedfile %.100s (%.100s)", filename, |
Damien Miller | 52dc96b | 2000-10-16 20:13:43 +1100 | [diff] [blame] | 614 | strerror(errno)); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 615 | |
Damien Miller | 52dc96b | 2000-10-16 20:13:43 +1100 | [diff] [blame] | 616 | close(fd); |
| 617 | } |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | void |
| 621 | prng_read_seedfile(void) { |
| 622 | int fd; |
| 623 | char seed[1024]; |
| 624 | char filename[1024]; |
| 625 | struct passwd *pw; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 626 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 627 | pw = getpwuid(original_uid); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 628 | if (pw == NULL) |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 629 | fatal("Couldn't get password entry for current user (%i): %s", |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 630 | original_uid, strerror(errno)); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 631 | |
| 632 | snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 633 | SSH_PRNG_SEED_FILE); |
| 634 | |
| 635 | debug("loading PRNG seed from file %.100s", filename); |
| 636 | |
| 637 | if (!prng_check_seedfile(filename)) { |
Damien Miller | 21de450 | 2001-01-17 09:37:15 +1100 | [diff] [blame] | 638 | verbose("Random seed file not found or not valid, ignoring."); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 639 | return; |
| 640 | } |
| 641 | |
| 642 | /* open the file and read in the seed */ |
| 643 | fd = open(filename, O_RDONLY); |
| 644 | if (fd == -1) |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 645 | fatal("could not open PRNG seedfile %.100s (%.100s)", filename, |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 646 | strerror(errno)); |
| 647 | |
| 648 | if (atomicio(read, fd, &seed, sizeof(seed)) != sizeof(seed)) { |
| 649 | verbose("invalid or short read from PRNG seedfile %.100s - ignoring", |
| 650 | filename); |
| 651 | memset(seed, '\0', sizeof(seed)); |
| 652 | } |
| 653 | close(fd); |
| 654 | |
| 655 | /* stir in the seed, with estimated entropy zero */ |
| 656 | RAND_add(&seed, sizeof(seed), 0.0); |
| 657 | } |
| 658 | |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 659 | |
| 660 | /* |
| 661 | * entropy command initialisation functions |
| 662 | */ |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 663 | int |
| 664 | prng_read_commands(char *cmdfilename) |
| 665 | { |
| 666 | FILE *f; |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 667 | char *cp; |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 668 | char line[1024]; |
| 669 | char cmd[1024]; |
| 670 | char path[256]; |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 671 | int linenum; |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 672 | int num_cmds = 64; |
| 673 | int cur_cmd = 0; |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 674 | double est; |
| 675 | entropy_source_t *entcmd; |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 676 | |
| 677 | f = fopen(cmdfilename, "r"); |
| 678 | if (!f) { |
| 679 | fatal("couldn't read entropy commands file %.100s: %.100s", |
| 680 | cmdfilename, strerror(errno)); |
| 681 | } |
| 682 | |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 683 | entcmd = (entropy_source_t *)xmalloc(num_cmds * sizeof(entropy_source_t)); |
| 684 | memset(entcmd, '\0', num_cmds * sizeof(entropy_source_t)); |
| 685 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 686 | /* Read in file */ |
| 687 | linenum = 0; |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 688 | while (fgets(line, sizeof(line), f)) { |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 689 | int arg; |
| 690 | char *argv; |
| 691 | |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 692 | linenum++; |
| 693 | |
| 694 | /* skip leading whitespace, test for blank line or comment */ |
| 695 | cp = line + strspn(line, WHITESPACE); |
| 696 | if ((*cp == 0) || (*cp == '#')) |
| 697 | continue; /* done with this line */ |
| 698 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 699 | /* First non-whitespace char should be double quote delimiting */ |
| 700 | /* commandline */ |
| 701 | if (*cp != '"') { |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 702 | error("bad entropy command, %.100s line %d", cmdfilename, |
| 703 | linenum); |
| 704 | continue; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 705 | } |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 706 | |
| 707 | /* first token, command args (incl. argv[0]) in double quotes */ |
| 708 | cp = strtok(cp, "\""); |
| 709 | if (cp == NULL) { |
| 710 | error("missing or bad command string, %.100s line %d -- ignored", |
| 711 | cmdfilename, linenum); |
| 712 | continue; |
| 713 | } |
| 714 | strlcpy(cmd, cp, sizeof(cmd)); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 715 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 716 | /* second token, full command path */ |
| 717 | if ((cp = strtok(NULL, WHITESPACE)) == NULL) { |
| 718 | error("missing command path, %.100s line %d -- ignored", |
| 719 | cmdfilename, linenum); |
| 720 | continue; |
| 721 | } |
| 722 | |
| 723 | /* did configure mark this as dead? */ |
| 724 | if (strncmp("undef", cp, 5) == 0) |
| 725 | continue; |
| 726 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 727 | strlcpy(path, cp, sizeof(path)); |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 728 | |
| 729 | /* third token, entropy rate estimate for this command */ |
| 730 | if ((cp = strtok(NULL, WHITESPACE)) == NULL) { |
| 731 | error("missing entropy estimate, %.100s line %d -- ignored", |
| 732 | cmdfilename, linenum); |
| 733 | continue; |
| 734 | } |
| 735 | est = strtod(cp, &argv); |
| 736 | |
| 737 | /* end of line */ |
| 738 | if ((cp = strtok(NULL, WHITESPACE)) != NULL) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 739 | error("garbage at end of line %d in %.100s -- ignored", linenum, |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 740 | cmdfilename); |
| 741 | continue; |
| 742 | } |
| 743 | |
| 744 | /* save the command for debug messages */ |
| 745 | entcmd[cur_cmd].cmdstring = xstrdup(cmd); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 746 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 747 | /* split the command args */ |
| 748 | cp = strtok(cmd, WHITESPACE); |
| 749 | arg = 0; |
| 750 | argv = NULL; |
| 751 | do { |
| 752 | char *s = (char*)xmalloc(strlen(cp) + 1); |
| 753 | strncpy(s, cp, strlen(cp) + 1); |
| 754 | entcmd[cur_cmd].args[arg] = s; |
| 755 | arg++; |
| 756 | } while ((arg < 5) && (cp = strtok(NULL, WHITESPACE))); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 757 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 758 | if (strtok(NULL, WHITESPACE)) |
| 759 | error("ignored extra command elements (max 5), %.100s line %d", |
| 760 | cmdfilename, linenum); |
| 761 | |
| 762 | /* Copy the command path and rate estimate */ |
| 763 | entcmd[cur_cmd].path = xstrdup(path); |
| 764 | entcmd[cur_cmd].rate = est; |
| 765 | |
| 766 | /* Initialise other values */ |
| 767 | entcmd[cur_cmd].sticky_badness = 1; |
| 768 | |
| 769 | cur_cmd++; |
| 770 | |
| 771 | /* If we've filled the array, reallocate it twice the size */ |
| 772 | /* Do this now because even if this we're on the last command, |
| 773 | we need another slot to mark the last entry */ |
| 774 | if (cur_cmd == num_cmds) { |
| 775 | num_cmds *= 2; |
| 776 | entcmd = xrealloc(entcmd, num_cmds * sizeof(entropy_source_t)); |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
| 780 | /* zero the last entry */ |
| 781 | memset(&entcmd[cur_cmd], '\0', sizeof(entropy_source_t)); |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 782 | |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 783 | /* trim to size */ |
| 784 | entropy_sources = xrealloc(entcmd, (cur_cmd+1) * sizeof(entropy_source_t)); |
| 785 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 786 | debug("Loaded %d entropy commands from %.100s", cur_cmd, cmdfilename); |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 787 | |
| 788 | return (cur_cmd >= MIN_ENTROPY_SOURCES); |
| 789 | } |
| 790 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 791 | /* |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 792 | * Write a keyfile at exit |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 793 | */ |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 794 | void |
| 795 | prng_seed_cleanup(void *junk) |
| 796 | { |
| 797 | prng_write_seedfile(); |
| 798 | } |
| 799 | |
| 800 | /* |
| 801 | * Conditionally Seed OpenSSL's random number pool from |
| 802 | * syscalls and program output |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 803 | */ |
| 804 | void |
| 805 | seed_rng(void) |
| 806 | { |
Damien Miller | a1072a8 | 2001-02-18 15:28:11 +1100 | [diff] [blame] | 807 | mysig_t old_sigchld_handler; |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 808 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 809 | if (!prng_initialised) |
| 810 | fatal("RNG not initialised"); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 811 | |
Damien Miller | f3c6cf1 | 2000-05-17 22:08:29 +1000 | [diff] [blame] | 812 | /* Make sure some other sigchld handler doesn't reap our entropy */ |
| 813 | /* commands */ |
Damien Miller | a1072a8 | 2001-02-18 15:28:11 +1100 | [diff] [blame] | 814 | old_sigchld_handler = mysignal(SIGCHLD, SIG_DFL); |
Damien Miller | f3c6cf1 | 2000-05-17 22:08:29 +1000 | [diff] [blame] | 815 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 816 | debug("Seeded RNG with %i bytes from programs", (int)stir_from_programs()); |
| 817 | debug("Seeded RNG with %i bytes from system calls", (int)stir_from_system()); |
| 818 | |
| 819 | if (!RAND_status()) |
| 820 | fatal("Not enough entropy in RNG"); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 821 | |
Damien Miller | a1072a8 | 2001-02-18 15:28:11 +1100 | [diff] [blame] | 822 | mysignal(SIGCHLD, old_sigchld_handler); |
Damien Miller | f3c6cf1 | 2000-05-17 22:08:29 +1000 | [diff] [blame] | 823 | |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 824 | if (!RAND_status()) |
| 825 | fatal("Couldn't initialise builtin random number generator -- exiting."); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 826 | } |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 827 | |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 828 | void init_rng(void) |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 829 | { |
Damien Miller | d592b63 | 2000-11-25 10:09:32 +1100 | [diff] [blame] | 830 | int original_euid; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 831 | |
Damien Miller | 767c7fc | 2001-02-27 09:20:57 +1100 | [diff] [blame] | 832 | check_openssl_version(); |
| 833 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 834 | original_uid = getuid(); |
Damien Miller | d592b63 | 2000-11-25 10:09:32 +1100 | [diff] [blame] | 835 | original_euid = geteuid(); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 836 | |
| 837 | /* Read in collection commands */ |
| 838 | if (!prng_read_commands(SSH_PRNG_COMMAND_FILE)) |
| 839 | fatal("PRNG initialisation failed -- exiting."); |
| 840 | |
| 841 | /* Set ourselves up to save a seed upon exit */ |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 842 | prng_seed_saved = 0; |
Damien Miller | d592b63 | 2000-11-25 10:09:32 +1100 | [diff] [blame] | 843 | |
| 844 | /* Give up privs while reading seed file */ |
Damien Miller | bb7c976 | 2001-02-26 20:49:58 +1100 | [diff] [blame] | 845 | #ifdef SAVED_IDS_WORK_WITH_SETEUID |
Damien Miller | d592b63 | 2000-11-25 10:09:32 +1100 | [diff] [blame] | 846 | if ((original_uid != original_euid) && (seteuid(original_uid) == -1)) |
| 847 | fatal("Couldn't give up privileges"); |
Damien Miller | bb7c976 | 2001-02-26 20:49:58 +1100 | [diff] [blame] | 848 | #else /* SAVED_IDS_WORK_WITH_SETEUID */ |
| 849 | /* |
| 850 | * Propagate the privileged uid to all of our uids. |
| 851 | * Set the effective uid to the given (unprivileged) uid. |
| 852 | */ |
Damien Miller | 248131a | 2001-02-27 09:47:16 +1100 | [diff] [blame] | 853 | if (original_uid != original_euid && (setuid(original_euid) == -1 || |
| 854 | seteuid(original_uid) == -1)) |
Damien Miller | bb7c976 | 2001-02-26 20:49:58 +1100 | [diff] [blame] | 855 | fatal("Couldn't give up privileges"); |
| 856 | #endif /* SAVED_IDS_WORK_WITH_SETEUID */ |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 857 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 858 | prng_read_seedfile(); |
Damien Miller | d592b63 | 2000-11-25 10:09:32 +1100 | [diff] [blame] | 859 | |
Damien Miller | bb7c976 | 2001-02-26 20:49:58 +1100 | [diff] [blame] | 860 | #ifdef SAVED_IDS_WORK_WITH_SETEUID |
Damien Miller | d592b63 | 2000-11-25 10:09:32 +1100 | [diff] [blame] | 861 | if ((original_uid != original_euid) && (seteuid(original_euid) == -1)) |
| 862 | fatal("Couldn't restore privileges"); |
Damien Miller | bb7c976 | 2001-02-26 20:49:58 +1100 | [diff] [blame] | 863 | #else /* SAVED_IDS_WORK_WITH_SETEUID */ |
| 864 | /* |
| 865 | * We are unable to restore the real uid to its unprivileged value. |
| 866 | * Propagate the real uid (usually more privileged) to effective uid |
| 867 | * as well. |
| 868 | */ |
Damien Miller | 248131a | 2001-02-27 09:47:16 +1100 | [diff] [blame] | 869 | if (original_uid != original_euid && (seteuid(original_euid) == -1 || |
| 870 | setuid(original_uid) == -1)) |
Damien Miller | bb7c976 | 2001-02-26 20:49:58 +1100 | [diff] [blame] | 871 | fatal("Couldn't restore privileges"); |
| 872 | #endif /* SAVED_IDS_WORK_WITH_SETEUID */ |
Damien Miller | d592b63 | 2000-11-25 10:09:32 +1100 | [diff] [blame] | 873 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 874 | fatal_add_cleanup(prng_seed_cleanup, NULL); |
| 875 | atexit(prng_write_seedfile); |
| 876 | |
| 877 | prng_initialised = 1; |
| 878 | } |
| 879 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 880 | #endif /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */ |