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