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