blob: 9743e598befb00daf0926f1ae4370fe67802e9a9 [file] [log] [blame]
Damien Miller040f3832000-04-03 14:50:43 +10001/*
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 Miller040f3832000-04-03 14:50:43 +100012 *
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 Miller5f056372000-04-16 12:31:48 +100027#include <openssl/rand.h>
28#include <openssl/sha.h>
Damien Miller040f3832000-04-03 14:50:43 +100029
Damien Millerecbb26d2000-07-15 14:59:14 +100030/* SunOS 4.4.4 needs this */
31#ifdef HAVE_FLOATINGPOINT_H
32# include <floatingpoint.h>
33#endif /* HAVE_FLOATINGPOINT_H */
34
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "ssh.h"
Damien Millera1072a82001-02-18 15:28:11 +110036#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000037#include "xmalloc.h"
38#include "atomicio.h"
Ben Lindstromcb577332001-01-22 21:06:19 +000039#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include "log.h"
41
Damien Millerfbd884a2001-02-27 08:39:07 +110042RCSID("$Id: entropy.c,v 1.31 2001/02/26 21:39:07 djm Exp $");
Damien Miller040f3832000-04-03 14:50:43 +100043
Damien Miller040f3832000-04-03 14:50:43 +100044#ifndef offsetof
45# define offsetof(type, member) ((size_t) &((type *)0)->member)
46#endif
Damien Miller14c12cb2000-06-07 22:20:23 +100047
Damien Miller14c12cb2000-06-07 22:20:23 +100048/* Number of times to pass through command list gathering entropy */
49#define NUM_ENTROPY_RUNS 1
50
51/* Scale entropy estimates back by this amount on subsequent runs */
52#define SCALE_PER_RUN 10.0
53
54/* Minimum number of commands to be considered valid */
55#define MIN_ENTROPY_SOURCES 16
56
57#define WHITESPACE " \t\n"
58
Damien Miller7b22d652000-06-18 14:07:04 +100059#ifndef RUSAGE_SELF
60# define RUSAGE_SELF 0
61#endif
62#ifndef RUSAGE_CHILDREN
63# define RUSAGE_CHILDREN 0
64#endif
65
Damien Millerfbd884a2001-02-27 08:39:07 +110066#if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS)
67# define SAVED_IDS_WORK_WITH_SETEUID
68#endif
69
Damien Miller14c12cb2000-06-07 22:20:23 +100070#if defined(EGD_SOCKET) || defined(RANDOM_POOL)
71
72#ifdef EGD_SOCKET
Damien Miller040f3832000-04-03 14:50:43 +100073/* Collect entropy from EGD */
Damien Miller64681252000-06-26 13:01:33 +100074int get_random_bytes(unsigned char *buf, int len)
Damien Miller040f3832000-04-03 14:50:43 +100075{
Damien Miller64681252000-06-26 13:01:33 +100076 int fd;
77 char msg[2];
Damien Miller040f3832000-04-03 14:50:43 +100078 struct sockaddr_un addr;
Damien Millerb3ffc5f2001-02-18 12:44:29 +110079 int addr_len, rval, errors;
Damien Millera1072a82001-02-18 15:28:11 +110080 mysig_t old_sigpipe;
Damien Miller040f3832000-04-03 14:50:43 +100081
Damien Miller64681252000-06-26 13:01:33 +100082 /* Sanity checks */
Damien Miller040f3832000-04-03 14:50:43 +100083 if (sizeof(EGD_SOCKET) > sizeof(addr.sun_path))
84 fatal("Random pool path is too long");
Damien Miller040f3832000-04-03 14:50:43 +100085 if (len > 255)
86 fatal("Too many bytes to read from EGD");
Damien Miller64681252000-06-26 13:01:33 +100087
88 memset(&addr, '\0', sizeof(addr));
89 addr.sun_family = AF_UNIX;
90 strlcpy(addr.sun_path, EGD_SOCKET, sizeof(addr.sun_path));
91 addr_len = offsetof(struct sockaddr_un, sun_path) + sizeof(EGD_SOCKET);
Kevin Stevesef4eea92001-02-05 12:42:17 +000092
Damien Millera1072a82001-02-18 15:28:11 +110093 old_sigpipe = mysignal(SIGPIPE, SIG_IGN);
Damien Millerb3ffc5f2001-02-18 12:44:29 +110094
95 errors = rval = 0;
96reopen:
Damien Miller64681252000-06-26 13:01:33 +100097 fd = socket(AF_UNIX, SOCK_STREAM, 0);
98 if (fd == -1) {
99 error("Couldn't create AF_UNIX socket: %s", strerror(errno));
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100100 goto done;
Damien Miller64681252000-06-26 13:01:33 +1000101 }
102
103 if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000104 error("Couldn't connect to EGD socket \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000105 addr.sun_path, strerror(errno));
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100106 goto done;
Damien Miller64681252000-06-26 13:01:33 +1000107 }
108
Damien Miller040f3832000-04-03 14:50:43 +1000109 /* Send blocking read request to EGD */
Damien Miller64681252000-06-26 13:01:33 +1000110 msg[0] = 0x02;
111 msg[1] = len;
Damien Miller040f3832000-04-03 14:50:43 +1000112
Damien Miller64681252000-06-26 13:01:33 +1000113 if (atomicio(write, fd, msg, sizeof(msg)) != sizeof(msg)) {
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100114 if (errno == EPIPE && errors < 10) {
115 close(fd);
116 errors++;
117 goto reopen;
118 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000119 error("Couldn't write to EGD socket \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000120 EGD_SOCKET, strerror(errno));
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100121 goto done;
Damien Miller64681252000-06-26 13:01:33 +1000122 }
Damien Miller040f3832000-04-03 14:50:43 +1000123
Damien Miller64681252000-06-26 13:01:33 +1000124 if (atomicio(read, fd, buf, len) != len) {
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100125 if (errno == EPIPE && errors < 10) {
126 close(fd);
127 errors++;
128 goto reopen;
129 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000130 error("Couldn't read from EGD socket \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000131 EGD_SOCKET, strerror(errno));
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100132 goto done;
Damien Miller64681252000-06-26 13:01:33 +1000133 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000134
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100135 rval = 1;
136done:
Kevin Steves4679f5b2001-02-18 11:34:32 +0000137 mysignal(SIGPIPE, old_sigpipe);
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100138 if (fd != -1)
139 close(fd);
140 return(rval);
Damien Miller040f3832000-04-03 14:50:43 +1000141}
142#else /* !EGD_SOCKET */
143#ifdef RANDOM_POOL
144/* Collect entropy from /dev/urandom or pipe */
Damien Miller64681252000-06-26 13:01:33 +1000145int get_random_bytes(unsigned char *buf, int len)
Damien Miller040f3832000-04-03 14:50:43 +1000146{
Damien Miller64681252000-06-26 13:01:33 +1000147 int random_pool;
Damien Miller040f3832000-04-03 14:50:43 +1000148
Damien Miller64681252000-06-26 13:01:33 +1000149 random_pool = open(RANDOM_POOL, O_RDONLY);
Damien Miller040f3832000-04-03 14:50:43 +1000150 if (random_pool == -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000151 error("Couldn't open random pool \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000152 RANDOM_POOL, strerror(errno));
153 return(0);
Damien Miller040f3832000-04-03 14:50:43 +1000154 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000155
Damien Miller64681252000-06-26 13:01:33 +1000156 if (atomicio(read, random_pool, buf, len) != len) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000157 error("Couldn't read from random pool \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000158 RANDOM_POOL, strerror(errno));
159 close(random_pool);
160 return(0);
161 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000162
Damien Miller64681252000-06-26 13:01:33 +1000163 close(random_pool);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000164
Damien Miller64681252000-06-26 13:01:33 +1000165 return(1);
Damien Miller040f3832000-04-03 14:50:43 +1000166}
167#endif /* RANDOM_POOL */
168#endif /* EGD_SOCKET */
169
Damien Miller14c12cb2000-06-07 22:20:23 +1000170/*
171 * Seed OpenSSL's random number pool from Kernel random number generator
172 * or EGD
173 */
174void
175seed_rng(void)
176{
177 char buf[32];
Kevin Stevesef4eea92001-02-05 12:42:17 +0000178
Damien Miller14c12cb2000-06-07 22:20:23 +1000179 debug("Seeding random number generator");
Damien Miller64681252000-06-26 13:01:33 +1000180
Damien Miller08006472000-06-26 13:55:31 +1000181 if (!get_random_bytes(buf, sizeof(buf))) {
182 if (!RAND_status())
183 fatal("Entropy collection failed and entropy exhausted");
184 } else {
185 RAND_add(buf, sizeof(buf), sizeof(buf));
186 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000187
Damien Miller14c12cb2000-06-07 22:20:23 +1000188 memset(buf, '\0', sizeof(buf));
189}
190
Damien Millerf9b625c2000-07-09 22:42:32 +1000191/* No-op */
192void init_rng(void) {}
193
Damien Miller14c12cb2000-06-07 22:20:23 +1000194#else /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */
195
Kevin Stevesef4eea92001-02-05 12:42:17 +0000196/*
Damien Miller040f3832000-04-03 14:50:43 +1000197 * FIXME: proper entropy estimations. All current values are guesses
Damien Miller4018c192000-04-30 09:30:44 +1000198 * FIXME: (ATL) do estimates at compile time?
Damien Miller040f3832000-04-03 14:50:43 +1000199 * FIXME: More entropy sources
200 */
201
Damien Miller4018c192000-04-30 09:30:44 +1000202/* slow command timeouts (all in milliseconds) */
203/* static int entropy_timeout_default = ENTROPY_TIMEOUT_MSEC; */
204static int entropy_timeout_current = ENTROPY_TIMEOUT_MSEC;
205
Damien Miller0437b332000-05-02 09:56:41 +1000206static int prng_seed_saved = 0;
Damien Millerf9b625c2000-07-09 22:42:32 +1000207static int prng_initialised = 0;
208uid_t original_uid;
Damien Miller040f3832000-04-03 14:50:43 +1000209
210typedef struct
211{
212 /* Proportion of data that is entropy */
213 double rate;
Damien Miller4018c192000-04-30 09:30:44 +1000214 /* Counter goes positive if this command times out */
215 unsigned int badness;
216 /* Increases by factor of two each timeout */
217 unsigned int sticky_badness;
Damien Miller040f3832000-04-03 14:50:43 +1000218 /* Path to executable */
Damien Miller0437b332000-05-02 09:56:41 +1000219 char *path;
Damien Miller040f3832000-04-03 14:50:43 +1000220 /* argv to pass to executable */
Damien Miller0437b332000-05-02 09:56:41 +1000221 char *args[5];
Damien Miller8d1fd572000-05-17 21:34:07 +1000222 /* full command string (debug) */
223 char *cmdstring;
Damien Miller040f3832000-04-03 14:50:43 +1000224} entropy_source_t;
225
Damien Miller4018c192000-04-30 09:30:44 +1000226double stir_from_system(void);
227double stir_from_programs(void);
228double stir_gettimeofday(double entropy_estimate);
229double stir_clock(double entropy_estimate);
230double stir_rusage(int who, double entropy_estimate);
231double hash_output_from_command(entropy_source_t *src, char *hash);
232
Damien Miller0437b332000-05-02 09:56:41 +1000233/* this is initialised from a file, by prng_read_commands() */
234entropy_source_t *entropy_sources = NULL;
Damien Miller040f3832000-04-03 14:50:43 +1000235
Kevin Stevesef4eea92001-02-05 12:42:17 +0000236double
Damien Miller040f3832000-04-03 14:50:43 +1000237stir_from_system(void)
238{
239 double total_entropy_estimate;
240 long int i;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000241
Damien Miller040f3832000-04-03 14:50:43 +1000242 total_entropy_estimate = 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000243
Damien Miller040f3832000-04-03 14:50:43 +1000244 i = getpid();
Damien Miller7b22d652000-06-18 14:07:04 +1000245 RAND_add(&i, sizeof(i), 0.5);
Damien Miller040f3832000-04-03 14:50:43 +1000246 total_entropy_estimate += 0.1;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000247
Damien Miller040f3832000-04-03 14:50:43 +1000248 i = getppid();
Damien Miller7b22d652000-06-18 14:07:04 +1000249 RAND_add(&i, sizeof(i), 0.5);
Damien Miller040f3832000-04-03 14:50:43 +1000250 total_entropy_estimate += 0.1;
251
252 i = getuid();
253 RAND_add(&i, sizeof(i), 0.0);
254 i = getgid();
255 RAND_add(&i, sizeof(i), 0.0);
256
257 total_entropy_estimate += stir_gettimeofday(1.0);
Damien Miller7b22d652000-06-18 14:07:04 +1000258 total_entropy_estimate += stir_clock(0.5);
Damien Miller040f3832000-04-03 14:50:43 +1000259 total_entropy_estimate += stir_rusage(RUSAGE_SELF, 2.0);
260
261 return(total_entropy_estimate);
262}
263
Kevin Stevesef4eea92001-02-05 12:42:17 +0000264double
Damien Miller040f3832000-04-03 14:50:43 +1000265stir_from_programs(void)
266{
267 int i;
268 int c;
269 double entropy_estimate;
270 double total_entropy_estimate;
271 char hash[SHA_DIGEST_LENGTH];
272
Damien Miller040f3832000-04-03 14:50:43 +1000273 total_entropy_estimate = 0;
Damien Miller14c12cb2000-06-07 22:20:23 +1000274 for(i = 0; i < NUM_ENTROPY_RUNS; i++) {
Damien Miller040f3832000-04-03 14:50:43 +1000275 c = 0;
276 while (entropy_sources[c].path != NULL) {
Damien Miller040f3832000-04-03 14:50:43 +1000277
Damien Miller4018c192000-04-30 09:30:44 +1000278 if (!entropy_sources[c].badness) {
279 /* Hash output from command */
280 entropy_estimate = hash_output_from_command(&entropy_sources[c], hash);
281
282 /* Scale back entropy estimate according to command's rate */
283 entropy_estimate *= entropy_sources[c].rate;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000284
Damien Miller4018c192000-04-30 09:30:44 +1000285 /* Upper bound of entropy estimate is SHA_DIGEST_LENGTH */
286 if (entropy_estimate > SHA_DIGEST_LENGTH)
287 entropy_estimate = SHA_DIGEST_LENGTH;
Damien Miller040f3832000-04-03 14:50:43 +1000288
Kevin Stevesef4eea92001-02-05 12:42:17 +0000289 /* Scale back estimates for subsequent passes through list */
Damien Miller14c12cb2000-06-07 22:20:23 +1000290 entropy_estimate /= SCALE_PER_RUN * (i + 1.0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000291
Damien Miller4018c192000-04-30 09:30:44 +1000292 /* Stir it in */
293 RAND_add(hash, sizeof(hash), entropy_estimate);
Damien Miller040f3832000-04-03 14:50:43 +1000294
Kevin Stevesef4eea92001-02-05 12:42:17 +0000295 debug3("Got %0.2f bytes of entropy from '%s'", entropy_estimate,
Damien Miller8d1fd572000-05-17 21:34:07 +1000296 entropy_sources[c].cmdstring);
Damien Miller040f3832000-04-03 14:50:43 +1000297
Damien Miller4018c192000-04-30 09:30:44 +1000298 total_entropy_estimate += entropy_estimate;
Damien Miller040f3832000-04-03 14:50:43 +1000299
300 /* Execution times should be a little unpredictable */
Damien Miller4018c192000-04-30 09:30:44 +1000301 total_entropy_estimate += stir_gettimeofday(0.05);
302 total_entropy_estimate += stir_clock(0.05);
303 total_entropy_estimate += stir_rusage(RUSAGE_SELF, 0.1);
304 total_entropy_estimate += stir_rusage(RUSAGE_CHILDREN, 0.1);
305 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +1100306 debug2("Command '%s' disabled (badness %d)",
Damien Miller8d1fd572000-05-17 21:34:07 +1000307 entropy_sources[c].cmdstring, entropy_sources[c].badness);
Damien Miller4018c192000-04-30 09:30:44 +1000308
309 if (entropy_sources[c].badness > 0)
310 entropy_sources[c].badness--;
311 }
312
Damien Miller040f3832000-04-03 14:50:43 +1000313 c++;
314 }
315 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000316
Damien Miller040f3832000-04-03 14:50:43 +1000317 return(total_entropy_estimate);
318}
319
320double
321stir_gettimeofday(double entropy_estimate)
322{
323 struct timeval tv;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000324
Damien Miller040f3832000-04-03 14:50:43 +1000325 if (gettimeofday(&tv, NULL) == -1)
326 fatal("Couldn't gettimeofday: %s", strerror(errno));
327
328 RAND_add(&tv, sizeof(tv), entropy_estimate);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000329
Damien Miller040f3832000-04-03 14:50:43 +1000330 return(entropy_estimate);
331}
332
333double
334stir_clock(double entropy_estimate)
335{
336#ifdef HAVE_CLOCK
337 clock_t c;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000338
Damien Miller040f3832000-04-03 14:50:43 +1000339 c = clock();
340 RAND_add(&c, sizeof(c), entropy_estimate);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000341
Damien Miller040f3832000-04-03 14:50:43 +1000342 return(entropy_estimate);
343#else /* _HAVE_CLOCK */
344 return(0);
345#endif /* _HAVE_CLOCK */
346}
347
348double
349stir_rusage(int who, double entropy_estimate)
350{
351#ifdef HAVE_GETRUSAGE
352 struct rusage ru;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000353
Damien Miller4018c192000-04-30 09:30:44 +1000354 if (getrusage(who, &ru) == -1)
Damien Miller7b22d652000-06-18 14:07:04 +1000355 return(0);
Damien Miller040f3832000-04-03 14:50:43 +1000356
Damien Miller7b22d652000-06-18 14:07:04 +1000357 RAND_add(&ru, sizeof(ru), entropy_estimate);
Damien Miller040f3832000-04-03 14:50:43 +1000358
359 return(entropy_estimate);
360#else /* _HAVE_GETRUSAGE */
361 return(0);
362#endif /* _HAVE_GETRUSAGE */
363}
364
Damien Miller8d1fd572000-05-17 21:34:07 +1000365
366static
367int
368_get_timeval_msec_difference(struct timeval *t1, struct timeval *t2) {
369 int secdiff, usecdiff;
370
371 secdiff = t2->tv_sec - t1->tv_sec;
372 usecdiff = (secdiff*1000000) + (t2->tv_usec - t1->tv_usec);
373 return (int)(usecdiff / 1000);
374}
375
Damien Miller040f3832000-04-03 14:50:43 +1000376double
Damien Miller4018c192000-04-30 09:30:44 +1000377hash_output_from_command(entropy_source_t *src, char *hash)
Damien Miller040f3832000-04-03 14:50:43 +1000378{
379 static int devnull = -1;
380 int p[2];
Damien Miller4018c192000-04-30 09:30:44 +1000381 fd_set rdset;
382 int cmd_eof = 0, error_abort = 0;
Damien Miller8d1fd572000-05-17 21:34:07 +1000383 struct timeval tv_start, tv_current;
384 int msec_elapsed = 0;
Damien Miller040f3832000-04-03 14:50:43 +1000385 pid_t pid;
386 int status;
Damien Miller8d1fd572000-05-17 21:34:07 +1000387 char buf[16384];
Damien Miller040f3832000-04-03 14:50:43 +1000388 int bytes_read;
389 int total_bytes_read;
390 SHA_CTX sha;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000391
Damien Millercb5e44a2000-09-29 12:12:36 +1100392 debug3("Reading output from \'%s\'", src->cmdstring);
393
Damien Miller040f3832000-04-03 14:50:43 +1000394 if (devnull == -1) {
395 devnull = open("/dev/null", O_RDWR);
396 if (devnull == -1)
397 fatal("Couldn't open /dev/null: %s", strerror(errno));
398 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000399
Damien Miller040f3832000-04-03 14:50:43 +1000400 if (pipe(p) == -1)
401 fatal("Couldn't open pipe: %s", strerror(errno));
402
Damien Miller8d1fd572000-05-17 21:34:07 +1000403 (void)gettimeofday(&tv_start, NULL); /* record start time */
404
Damien Miller040f3832000-04-03 14:50:43 +1000405 switch (pid = fork()) {
406 case -1: /* Error */
407 close(p[0]);
408 close(p[1]);
409 fatal("Couldn't fork: %s", strerror(errno));
410 /* NOTREACHED */
411 case 0: /* Child */
Damien Miller4018c192000-04-30 09:30:44 +1000412 dup2(devnull, STDIN_FILENO);
413 dup2(p[1], STDOUT_FILENO);
414 dup2(p[1], STDERR_FILENO);
Damien Miller040f3832000-04-03 14:50:43 +1000415 close(p[0]);
416 close(p[1]);
417 close(devnull);
418
Damien Millerf9b625c2000-07-09 22:42:32 +1000419 setuid(original_uid);
Damien Miller4018c192000-04-30 09:30:44 +1000420 execv(src->path, (char**)(src->args));
Damien Miller8d1fd572000-05-17 21:34:07 +1000421 debug("(child) Couldn't exec '%s': %s", src->cmdstring,
422 strerror(errno));
Damien Miller040f3832000-04-03 14:50:43 +1000423 _exit(-1);
424 default: /* Parent */
425 break;
426 }
427
428 RAND_add(&pid, sizeof(&pid), 0.0);
429
430 close(p[1]);
431
432 /* Hash output from child */
433 SHA1_Init(&sha);
434 total_bytes_read = 0;
Damien Miller4018c192000-04-30 09:30:44 +1000435
436 while (!error_abort && !cmd_eof) {
437 int ret;
438 struct timeval tv;
Damien Miller8d1fd572000-05-17 21:34:07 +1000439 int msec_remaining;
440
441 (void) gettimeofday(&tv_current, 0);
Damien Miller14c12cb2000-06-07 22:20:23 +1000442 msec_elapsed = _get_timeval_msec_difference(&tv_start, &tv_current);
Damien Miller8d1fd572000-05-17 21:34:07 +1000443 if (msec_elapsed >= entropy_timeout_current) {
444 error_abort=1;
445 continue;
446 }
447 msec_remaining = entropy_timeout_current - msec_elapsed;
Damien Miller4018c192000-04-30 09:30:44 +1000448
449 FD_ZERO(&rdset);
450 FD_SET(p[0], &rdset);
Damien Miller8d1fd572000-05-17 21:34:07 +1000451 tv.tv_sec = msec_remaining / 1000;
452 tv.tv_usec = (msec_remaining % 1000) * 1000;
Damien Miller4018c192000-04-30 09:30:44 +1000453
454 ret = select(p[0]+1, &rdset, NULL, NULL, &tv);
Damien Miller8d1fd572000-05-17 21:34:07 +1000455
Damien Millerf9b625c2000-07-09 22:42:32 +1000456 RAND_add(&tv, sizeof(tv), 0.0);
457
Damien Miller4018c192000-04-30 09:30:44 +1000458 switch (ret) {
459 case 0:
460 /* timer expired */
461 error_abort = 1;
462 break;
Damien Miller4018c192000-04-30 09:30:44 +1000463 case 1:
464 /* command input */
465 bytes_read = read(p[0], buf, sizeof(buf));
Damien Millerf9b625c2000-07-09 22:42:32 +1000466 RAND_add(&bytes_read, sizeof(&bytes_read), 0.0);
Damien Miller4018c192000-04-30 09:30:44 +1000467 if (bytes_read == -1) {
468 error_abort = 1;
469 break;
Damien Millerf9b625c2000-07-09 22:42:32 +1000470 } else if (bytes_read) {
Damien Miller8d1fd572000-05-17 21:34:07 +1000471 SHA1_Update(&sha, buf, bytes_read);
472 total_bytes_read += bytes_read;
Damien Millerf9b625c2000-07-09 22:42:32 +1000473 } else {
Damien Miller8d1fd572000-05-17 21:34:07 +1000474 cmd_eof = 1;
Damien Millerf9b625c2000-07-09 22:42:32 +1000475 }
Damien Miller4018c192000-04-30 09:30:44 +1000476 break;
Damien Miller4018c192000-04-30 09:30:44 +1000477 case -1:
478 default:
Damien Millerf9b625c2000-07-09 22:42:32 +1000479 /* error */
Damien Miller8d1fd572000-05-17 21:34:07 +1000480 debug("Command '%s': select() failed: %s", src->cmdstring,
481 strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000482 error_abort = 1;
483 break;
Damien Millerf9b625c2000-07-09 22:42:32 +1000484 }
485 }
Damien Miller4018c192000-04-30 09:30:44 +1000486
Damien Miller040f3832000-04-03 14:50:43 +1000487 SHA1_Final(hash, &sha);
488
489 close(p[0]);
Damien Miller8d1fd572000-05-17 21:34:07 +1000490
Damien Millercb5e44a2000-09-29 12:12:36 +1100491 debug3("Time elapsed: %d msec", msec_elapsed);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000492
Damien Miller040f3832000-04-03 14:50:43 +1000493 if (waitpid(pid, &status, 0) == -1) {
Damien Millercb5e44a2000-09-29 12:12:36 +1100494 error("Couldn't wait for child '%s' completion: %s", src->cmdstring,
Damien Miller8d1fd572000-05-17 21:34:07 +1000495 strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000496 return(0.0);
Damien Miller040f3832000-04-03 14:50:43 +1000497 }
498
499 RAND_add(&status, sizeof(&status), 0.0);
500
Damien Miller4018c192000-04-30 09:30:44 +1000501 if (error_abort) {
502 /* closing p[0] on timeout causes the entropy command to
503 * SIGPIPE. Take whatever output we got, and mark this command
504 * as slow */
Damien Millercb5e44a2000-09-29 12:12:36 +1100505 debug2("Command '%s' timed out", src->cmdstring);
Damien Miller4018c192000-04-30 09:30:44 +1000506 src->sticky_badness *= 2;
507 src->badness = src->sticky_badness;
Damien Miller040f3832000-04-03 14:50:43 +1000508 return(total_bytes_read);
Damien Miller4018c192000-04-30 09:30:44 +1000509 }
510
511 if (WIFEXITED(status)) {
512 if (WEXITSTATUS(status)==0) {
513 return(total_bytes_read);
514 } else {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000515 debug2("Command '%s' exit status was %d", src->cmdstring,
Damien Miller14c12cb2000-06-07 22:20:23 +1000516 WEXITSTATUS(status));
Damien Miller4018c192000-04-30 09:30:44 +1000517 src->badness = src->sticky_badness = 128;
518 return (0.0);
519 }
520 } else if (WIFSIGNALED(status)) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000521 debug2("Command '%s' returned on uncaught signal %d !", src->cmdstring,
Damien Miller14c12cb2000-06-07 22:20:23 +1000522 status);
Damien Miller4018c192000-04-30 09:30:44 +1000523 src->badness = src->sticky_badness = 128;
524 return(0.0);
525 } else
526 return(0.0);
Damien Miller040f3832000-04-03 14:50:43 +1000527}
Damien Miller4018c192000-04-30 09:30:44 +1000528
529/*
530 * prng seedfile functions
531 */
532int
533prng_check_seedfile(char *filename) {
534
535 struct stat st;
536
537 /* FIXME raceable: eg replace seed between this stat and subsequent open */
538 /* Not such a problem because we don't trust the seed file anyway */
539 if (lstat(filename, &st) == -1) {
Damien Miller52dc96b2000-10-16 20:13:43 +1100540 /* Give up on hard errors */
Damien Miller4018c192000-04-30 09:30:44 +1000541 if (errno != ENOENT)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000542 debug("WARNING: Couldn't stat random seed file \"%s\": %s",
Damien Miller52dc96b2000-10-16 20:13:43 +1100543 filename, strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000544
545 return(0);
546 }
547
548 /* regular file? */
549 if (!S_ISREG(st.st_mode))
550 fatal("PRNG seedfile %.100s is not a regular file", filename);
551
552 /* mode 0600, owned by root or the current user? */
Damien Miller52dc96b2000-10-16 20:13:43 +1100553 if (((st.st_mode & 0177) != 0) || !(st.st_uid == original_uid)) {
554 debug("WARNING: PRNG seedfile %.100s must be mode 0600, owned by uid %d",
Damien Miller4018c192000-04-30 09:30:44 +1000555 filename, getuid());
Damien Miller52dc96b2000-10-16 20:13:43 +1100556 return(0);
557 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000558
Damien Miller4018c192000-04-30 09:30:44 +1000559 return(1);
560}
561
562void
563prng_write_seedfile(void) {
564 int fd;
565 char seed[1024];
566 char filename[1024];
567 struct passwd *pw;
568
569 /* Don't bother if we have already saved a seed */
570 if (prng_seed_saved)
571 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000572
Damien Millerf9b625c2000-07-09 22:42:32 +1000573 setuid(original_uid);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000574
Damien Millerfc0b11b2000-05-02 00:03:55 +1000575 prng_seed_saved = 1;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000576
Damien Millerf9b625c2000-07-09 22:42:32 +1000577 pw = getpwuid(original_uid);
Damien Miller4018c192000-04-30 09:30:44 +1000578 if (pw == NULL)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000579 fatal("Couldn't get password entry for current user (%i): %s",
Damien Millerf9b625c2000-07-09 22:42:32 +1000580 original_uid, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000581
Damien Miller4018c192000-04-30 09:30:44 +1000582 /* Try to ensure that the parent directory is there */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000583 snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
Ben Lindstromcb577332001-01-22 21:06:19 +0000584 _PATH_SSH_USER_DIR);
Damien Miller4018c192000-04-30 09:30:44 +1000585 mkdir(filename, 0700);
586
Kevin Stevesef4eea92001-02-05 12:42:17 +0000587 snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
Damien Miller4018c192000-04-30 09:30:44 +1000588 SSH_PRNG_SEED_FILE);
589
590 debug("writing PRNG seed to file %.100s", filename);
591
592 RAND_bytes(seed, sizeof(seed));
593
594 /* Don't care if the seed doesn't exist */
595 prng_check_seedfile(filename);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000596
Damien Miller52dc96b2000-10-16 20:13:43 +1100597 if ((fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0600)) == -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000598 debug("WARNING: couldn't access PRNG seedfile %.100s (%.100s)",
Damien Miller52dc96b2000-10-16 20:13:43 +1100599 filename, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000600 } else {
Damien Miller52dc96b2000-10-16 20:13:43 +1100601 if (atomicio(write, fd, &seed, sizeof(seed)) != sizeof(seed))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000602 fatal("problem writing PRNG seedfile %.100s (%.100s)", filename,
Damien Miller52dc96b2000-10-16 20:13:43 +1100603 strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000604
Damien Miller52dc96b2000-10-16 20:13:43 +1100605 close(fd);
606 }
Damien Miller4018c192000-04-30 09:30:44 +1000607}
608
609void
610prng_read_seedfile(void) {
611 int fd;
612 char seed[1024];
613 char filename[1024];
614 struct passwd *pw;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000615
Damien Millerf9b625c2000-07-09 22:42:32 +1000616 pw = getpwuid(original_uid);
Damien Miller4018c192000-04-30 09:30:44 +1000617 if (pw == NULL)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000618 fatal("Couldn't get password entry for current user (%i): %s",
Damien Millerf9b625c2000-07-09 22:42:32 +1000619 original_uid, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000620
621 snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
Damien Miller4018c192000-04-30 09:30:44 +1000622 SSH_PRNG_SEED_FILE);
623
624 debug("loading PRNG seed from file %.100s", filename);
625
626 if (!prng_check_seedfile(filename)) {
Damien Miller21de4502001-01-17 09:37:15 +1100627 verbose("Random seed file not found or not valid, ignoring.");
Damien Miller4018c192000-04-30 09:30:44 +1000628 return;
629 }
630
631 /* open the file and read in the seed */
632 fd = open(filename, O_RDONLY);
633 if (fd == -1)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000634 fatal("could not open PRNG seedfile %.100s (%.100s)", filename,
Damien Miller4018c192000-04-30 09:30:44 +1000635 strerror(errno));
636
637 if (atomicio(read, fd, &seed, sizeof(seed)) != sizeof(seed)) {
638 verbose("invalid or short read from PRNG seedfile %.100s - ignoring",
639 filename);
640 memset(seed, '\0', sizeof(seed));
641 }
642 close(fd);
643
644 /* stir in the seed, with estimated entropy zero */
645 RAND_add(&seed, sizeof(seed), 0.0);
646}
647
Damien Miller0437b332000-05-02 09:56:41 +1000648
649/*
650 * entropy command initialisation functions
651 */
Damien Miller0437b332000-05-02 09:56:41 +1000652int
653prng_read_commands(char *cmdfilename)
654{
655 FILE *f;
Damien Miller0437b332000-05-02 09:56:41 +1000656 char *cp;
Damien Miller14c12cb2000-06-07 22:20:23 +1000657 char line[1024];
658 char cmd[1024];
659 char path[256];
Damien Miller0437b332000-05-02 09:56:41 +1000660 int linenum;
Damien Miller0437b332000-05-02 09:56:41 +1000661 int num_cmds = 64;
662 int cur_cmd = 0;
Damien Miller14c12cb2000-06-07 22:20:23 +1000663 double est;
664 entropy_source_t *entcmd;
Damien Miller0437b332000-05-02 09:56:41 +1000665
666 f = fopen(cmdfilename, "r");
667 if (!f) {
668 fatal("couldn't read entropy commands file %.100s: %.100s",
669 cmdfilename, strerror(errno));
670 }
671
Damien Miller0437b332000-05-02 09:56:41 +1000672 entcmd = (entropy_source_t *)xmalloc(num_cmds * sizeof(entropy_source_t));
673 memset(entcmd, '\0', num_cmds * sizeof(entropy_source_t));
674
Damien Miller14c12cb2000-06-07 22:20:23 +1000675 /* Read in file */
676 linenum = 0;
Damien Miller0437b332000-05-02 09:56:41 +1000677 while (fgets(line, sizeof(line), f)) {
Damien Miller14c12cb2000-06-07 22:20:23 +1000678 int arg;
679 char *argv;
680
Damien Miller0437b332000-05-02 09:56:41 +1000681 linenum++;
682
683 /* skip leading whitespace, test for blank line or comment */
684 cp = line + strspn(line, WHITESPACE);
685 if ((*cp == 0) || (*cp == '#'))
686 continue; /* done with this line */
687
Damien Miller14c12cb2000-06-07 22:20:23 +1000688 /* First non-whitespace char should be double quote delimiting */
689 /* commandline */
690 if (*cp != '"') {
Damien Miller0437b332000-05-02 09:56:41 +1000691 error("bad entropy command, %.100s line %d", cmdfilename,
692 linenum);
693 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000694 }
Damien Miller14c12cb2000-06-07 22:20:23 +1000695
696 /* first token, command args (incl. argv[0]) in double quotes */
697 cp = strtok(cp, "\"");
698 if (cp == NULL) {
699 error("missing or bad command string, %.100s line %d -- ignored",
700 cmdfilename, linenum);
701 continue;
702 }
703 strlcpy(cmd, cp, sizeof(cmd));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000704
Damien Miller14c12cb2000-06-07 22:20:23 +1000705 /* second token, full command path */
706 if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
707 error("missing command path, %.100s line %d -- ignored",
708 cmdfilename, linenum);
709 continue;
710 }
711
712 /* did configure mark this as dead? */
713 if (strncmp("undef", cp, 5) == 0)
714 continue;
715
Kevin Stevesef4eea92001-02-05 12:42:17 +0000716 strlcpy(path, cp, sizeof(path));
Damien Miller14c12cb2000-06-07 22:20:23 +1000717
718 /* third token, entropy rate estimate for this command */
719 if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
720 error("missing entropy estimate, %.100s line %d -- ignored",
721 cmdfilename, linenum);
722 continue;
723 }
724 est = strtod(cp, &argv);
725
726 /* end of line */
727 if ((cp = strtok(NULL, WHITESPACE)) != NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000728 error("garbage at end of line %d in %.100s -- ignored", linenum,
Damien Miller14c12cb2000-06-07 22:20:23 +1000729 cmdfilename);
730 continue;
731 }
732
733 /* save the command for debug messages */
734 entcmd[cur_cmd].cmdstring = xstrdup(cmd);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000735
Damien Miller14c12cb2000-06-07 22:20:23 +1000736 /* split the command args */
737 cp = strtok(cmd, WHITESPACE);
738 arg = 0;
739 argv = NULL;
740 do {
741 char *s = (char*)xmalloc(strlen(cp) + 1);
742 strncpy(s, cp, strlen(cp) + 1);
743 entcmd[cur_cmd].args[arg] = s;
744 arg++;
745 } while ((arg < 5) && (cp = strtok(NULL, WHITESPACE)));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000746
Damien Miller14c12cb2000-06-07 22:20:23 +1000747 if (strtok(NULL, WHITESPACE))
748 error("ignored extra command elements (max 5), %.100s line %d",
749 cmdfilename, linenum);
750
751 /* Copy the command path and rate estimate */
752 entcmd[cur_cmd].path = xstrdup(path);
753 entcmd[cur_cmd].rate = est;
754
755 /* Initialise other values */
756 entcmd[cur_cmd].sticky_badness = 1;
757
758 cur_cmd++;
759
760 /* If we've filled the array, reallocate it twice the size */
761 /* Do this now because even if this we're on the last command,
762 we need another slot to mark the last entry */
763 if (cur_cmd == num_cmds) {
764 num_cmds *= 2;
765 entcmd = xrealloc(entcmd, num_cmds * sizeof(entropy_source_t));
Damien Miller0437b332000-05-02 09:56:41 +1000766 }
767 }
768
769 /* zero the last entry */
770 memset(&entcmd[cur_cmd], '\0', sizeof(entropy_source_t));
Damien Miller14c12cb2000-06-07 22:20:23 +1000771
Damien Miller0437b332000-05-02 09:56:41 +1000772 /* trim to size */
773 entropy_sources = xrealloc(entcmd, (cur_cmd+1) * sizeof(entropy_source_t));
774
Damien Millerf9b625c2000-07-09 22:42:32 +1000775 debug("Loaded %d entropy commands from %.100s", cur_cmd, cmdfilename);
Damien Miller0437b332000-05-02 09:56:41 +1000776
777 return (cur_cmd >= MIN_ENTROPY_SOURCES);
778}
779
Damien Miller040f3832000-04-03 14:50:43 +1000780/*
Damien Miller4018c192000-04-30 09:30:44 +1000781 * Write a keyfile at exit
Kevin Stevesef4eea92001-02-05 12:42:17 +0000782 */
Damien Miller4018c192000-04-30 09:30:44 +1000783void
784prng_seed_cleanup(void *junk)
785{
786 prng_write_seedfile();
787}
788
789/*
790 * Conditionally Seed OpenSSL's random number pool from
791 * syscalls and program output
Damien Miller040f3832000-04-03 14:50:43 +1000792 */
793void
794seed_rng(void)
795{
Damien Millera1072a82001-02-18 15:28:11 +1100796 mysig_t old_sigchld_handler;
Damien Miller0437b332000-05-02 09:56:41 +1000797
Damien Millerf9b625c2000-07-09 22:42:32 +1000798 if (!prng_initialised)
799 fatal("RNG not initialised");
Kevin Stevesef4eea92001-02-05 12:42:17 +0000800
Damien Millerf3c6cf12000-05-17 22:08:29 +1000801 /* Make sure some other sigchld handler doesn't reap our entropy */
802 /* commands */
Damien Millera1072a82001-02-18 15:28:11 +1100803 old_sigchld_handler = mysignal(SIGCHLD, SIG_DFL);
Damien Millerf3c6cf12000-05-17 22:08:29 +1000804
Damien Millerf9b625c2000-07-09 22:42:32 +1000805 debug("Seeded RNG with %i bytes from programs", (int)stir_from_programs());
806 debug("Seeded RNG with %i bytes from system calls", (int)stir_from_system());
807
808 if (!RAND_status())
809 fatal("Not enough entropy in RNG");
Damien Miller4018c192000-04-30 09:30:44 +1000810
Damien Millera1072a82001-02-18 15:28:11 +1100811 mysignal(SIGCHLD, old_sigchld_handler);
Damien Millerf3c6cf12000-05-17 22:08:29 +1000812
Damien Miller8d1fd572000-05-17 21:34:07 +1000813 if (!RAND_status())
814 fatal("Couldn't initialise builtin random number generator -- exiting.");
Damien Miller040f3832000-04-03 14:50:43 +1000815}
Damien Millerf9b625c2000-07-09 22:42:32 +1000816
Kevin Stevesef4eea92001-02-05 12:42:17 +0000817void init_rng(void)
Damien Millerf9b625c2000-07-09 22:42:32 +1000818{
Damien Millerd592b632000-11-25 10:09:32 +1100819 int original_euid;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000820
Damien Millerf9b625c2000-07-09 22:42:32 +1000821 original_uid = getuid();
Damien Millerd592b632000-11-25 10:09:32 +1100822 original_euid = geteuid();
Damien Millerf9b625c2000-07-09 22:42:32 +1000823
824 /* Read in collection commands */
825 if (!prng_read_commands(SSH_PRNG_COMMAND_FILE))
826 fatal("PRNG initialisation failed -- exiting.");
827
828 /* Set ourselves up to save a seed upon exit */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000829 prng_seed_saved = 0;
Damien Millerd592b632000-11-25 10:09:32 +1100830
831 /* Give up privs while reading seed file */
Damien Millerbb7c9762001-02-26 20:49:58 +1100832#ifdef SAVED_IDS_WORK_WITH_SETEUID
Damien Millerd592b632000-11-25 10:09:32 +1100833 if ((original_uid != original_euid) && (seteuid(original_uid) == -1))
834 fatal("Couldn't give up privileges");
Damien Millerbb7c9762001-02-26 20:49:58 +1100835#else /* SAVED_IDS_WORK_WITH_SETEUID */
836 /*
837 * Propagate the privileged uid to all of our uids.
838 * Set the effective uid to the given (unprivileged) uid.
839 */
840 if (original_uid != original_euid && setuid(original_euid) == -1 ||
841 seteuid(original_uid) == -1)
842 fatal("Couldn't give up privileges");
843#endif /* SAVED_IDS_WORK_WITH_SETEUID */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000844
Damien Millerf9b625c2000-07-09 22:42:32 +1000845 prng_read_seedfile();
Damien Millerd592b632000-11-25 10:09:32 +1100846
Damien Millerbb7c9762001-02-26 20:49:58 +1100847#ifdef SAVED_IDS_WORK_WITH_SETEUID
Damien Millerd592b632000-11-25 10:09:32 +1100848 if ((original_uid != original_euid) && (seteuid(original_euid) == -1))
849 fatal("Couldn't restore privileges");
Damien Millerbb7c9762001-02-26 20:49:58 +1100850#else /* SAVED_IDS_WORK_WITH_SETEUID */
851 /*
852 * We are unable to restore the real uid to its unprivileged value.
853 * Propagate the real uid (usually more privileged) to effective uid
854 * as well.
855 */
856 if (original_uid != original_euid && seteuid(original_euid) == -1 ||
857 setuid(original_uid) == -1)
858 fatal("Couldn't restore privileges");
859#endif /* SAVED_IDS_WORK_WITH_SETEUID */
Damien Millerd592b632000-11-25 10:09:32 +1100860
Damien Millerf9b625c2000-07-09 22:42:32 +1000861 fatal_add_cleanup(prng_seed_cleanup, NULL);
862 atexit(prng_write_seedfile);
863
864 prng_initialised = 1;
865}
866
Damien Miller040f3832000-04-03 14:50:43 +1000867#endif /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */