3 * $Id: noise.c,v 1.5 1998/04/23 13:25:23 mdw Exp $
5 * Collection of environmental noise
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of `become'
14 * `Become' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `Become' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.5 1998/04/23 13:25:23 mdw
33 * Try to reduce the amount of `ps'ing done under OSF/1, because /dev/kmem
36 * Revision 1.4 1998/02/20 17:52:32 mdw
37 * Don't use `df' for noise gathering, because it gets upset when NFS
38 * servers aren't responding.
40 * Revision 1.3 1998/01/12 16:46:19 mdw
43 * Revision 1.2 1997/08/20 16:19:57 mdw
44 * Fix test for `/dev/random' so that it doesn't close `stdin' if it fails!
46 * Revision 1.1 1997/08/07 09:45:26 mdw
47 * New source file added to acquire environmental noise and add it to the
48 * randomness pool (see `rand.c').
52 /*----- Header files ------------------------------------------------------*/
54 /* --- ANSI headers --- */
63 /* --- Unix headers --- */
65 #include <sys/types.h>
69 #if defined(HAVE_GETRUSAGE)
70 # include <sys/resource.h>
71 #elif defined(HAVE_VTIMES)
72 # include <sys/vtimes.h>
80 /* --- Local headers --- */
86 /*----- Main code ---------------------------------------------------------*/
88 /* --- @noise__shell@ --- *
90 * Arguments: @const char *cmd@ = pointer to a shell command
94 * Use: Adds the output of the shell command to the randomness pool.
95 * Some care is taken to do the Right Thing when running setuid.
98 static void noise__shell(const char *cmd)
103 /* --- Create a pipe for talking to the child --- */
108 /* --- Create the child process --- */
116 char *argv[] = { "/bin/sh", "-c", 0, 0 };
118 "PATH=/bin:/usr/bin:/usr/ucb:/usr/etc:/sbin:/usr/sbin",
122 /* --- Become whoever I'm being run as --- */
126 /* --- Close the old standard streams --- */
132 /* --- Set up stdin and stderr to be empty, and stdout as our pipe --- */
134 if (((fd = open("/dev/null", O_RDONLY)) != 0 &&
135 (fd = dup2(fd, 0)) != 0) ||
136 ((fd = dup2(pfd[1], 1)) != 1) ||
137 ((fd = open("/dev/null", O_WRONLY)) != 2 &&
138 (fd = dup2(fd, 2)) != 2))
141 /* --- Close the original pipe file descriptors --- */
147 /* --- Now run the child process --- */
149 argv[2] = (char *)cmd; /* POSIX screwed up the prototype */
150 execve("/bin/sh", argv, env);
152 /* --- Something went horribly wrong --- */
158 /* --- Now read from the child until it's all done --- */
166 sz = read(pfd[0], buf, sizeof(buf));
167 if (sz == 0 || (sz < 0 && sz != EINTR))
172 rand_add(pfd, sizeof(pfd));
173 burn(buf); burn(pfd);
176 /* --- The child should be dead now, so wait for it --- */
182 rand_add(&st, sizeof(st));
183 rand_add(&pid, sizeof(pid));
187 /* --- @noise_acquire@ --- *
193 * Use: Attempts to acquire an amount of random noise from the
194 * environment. A lot of it's not actually much good, but
195 * it's better than nothing. There's probably a bit or two's
196 * worth in each item which gets added.
199 void noise_acquire(void)
201 /* --- Try a real random number source --- *
203 * Some operating systems (notably Linux) provide a `/dev/random' which
204 * contains distilled random numbers from the outside world.
210 unsigned char buff[64];
213 if ((fd = open("/dev/random", O_RDONLY)) >= 0 &&
214 (f = fcntl(fd, F_GETFL, 0)) >= 0 &&
215 fcntl(fd, F_SETFL, f | O_NONBLOCK) >= 0 &&
216 (sz = read(fd, buff, sizeof(buff))) > 0) {
224 /* --- Squeeze some entropy from the current time --- */
230 gettimeofday(&tv, 0);
232 rand_add(&tv, sizeof(tv));
233 rand_add(&c, sizeof(c));
237 /* --- Try some commands which ask the outside world some questions --- */
239 noise__shell("ps auxww || ps -ef; netstat -an");
241 /* --- Get our resource usage to see if that's at all interesting --- */
243 #if defined(HAVE_GETRUSAGE)
246 getrusage(RUSAGE_SELF, &ru);
247 rand_add(&ru, sizeof(ru));
248 getrusage(RUSAGE_CHILDREN, &ru);
249 rand_add(&ru, sizeof(ru));
252 #elif defined(HAVE_VTIMES)
254 struct vtimes vt, vtc;
256 rand_add(&vt, sizeof(vt));
257 rand_add(&vtc, sizeof(vtc));
262 /* --- Squeeze some more entropy from the current time --- */
268 gettimeofday(&tv, 0);
270 rand_add(&tv, sizeof(tv));
271 rand_add(&c, sizeof(c));
275 /* --- Done -- churn the random pool --- */
280 /*----- That's all, folks -------------------------------------------------*/