3 * $Id: noise.c,v 1.3 1998/01/12 16:46:19 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.3 1998/01/12 16:46:19 mdw
35 * Revision 1.2 1997/08/20 16:19:57 mdw
36 * Fix test for `/dev/random' so that it doesn't close `stdin' if it fails!
38 * Revision 1.1 1997/08/07 09:45:26 mdw
39 * New source file added to acquire environmental noise and add it to the
40 * randomness pool (see `rand.c').
44 /*----- Header files ------------------------------------------------------*/
46 /* --- ANSI headers --- */
55 /* --- Unix headers --- */
57 #include <sys/types.h>
61 #if defined(HAVE_GETRUSAGE)
62 # include <sys/resource.h>
63 #elif defined(HAVE_VTIMES)
64 # include <sys/vtimes.h>
72 /* --- Local headers --- */
78 /*----- Main code ---------------------------------------------------------*/
80 /* --- @noise__shell@ --- *
82 * Arguments: @const char *cmd@ = pointer to a shell command
86 * Use: Adds the output of the shell command to the randomness pool.
87 * Some care is taken to do the Right Thing when running setuid.
90 static void noise__shell(const char *cmd)
95 /* --- Create a pipe for talking to the child --- */
100 /* --- Create the child process --- */
108 char *argv[] = { "/bin/sh", "-c", 0, 0 };
110 "PATH=/bin:/usr/bin:/usr/ucb:/usr/etc:/sbin:/usr/sbin",
114 /* --- Become whoever I'm being run as --- */
118 /* --- Close the old standard streams --- */
124 /* --- Set up stdin and stderr to be empty, and stdout as our pipe --- */
126 if (((fd = open("/dev/null", O_RDONLY)) != 0 &&
127 (fd = dup2(fd, 0)) != 0) ||
128 ((fd = dup2(pfd[1], 1)) != 1) ||
129 ((fd = open("/dev/null", O_WRONLY)) != 2 &&
130 (fd = dup2(fd, 2)) != 2))
133 /* --- Close the original pipe file descriptors --- */
139 /* --- Now run the child process --- */
141 argv[2] = (char *)cmd; /* POSIX screwed up the prototype */
142 execve("/bin/sh", argv, env);
144 /* --- Something went horribly wrong --- */
150 /* --- Now read from the child until it's all done --- */
158 sz = read(pfd[0], buf, sizeof(buf));
159 if (sz == 0 || (sz < 0 && sz != EINTR))
164 rand_add(pfd, sizeof(pfd));
165 burn(buf); burn(pfd);
168 /* --- The child should be dead now, so wait for it --- */
174 rand_add(&st, sizeof(st));
175 rand_add(&pid, sizeof(pid));
179 /* --- @noise_acquire@ --- *
185 * Use: Attempts to acquire an amount of random noise from the
186 * environment. A lot of it's not actually much good, but
187 * it's better than nothing. There's probably a bit or two's
188 * worth in each item which gets added.
191 void noise_acquire(void)
193 /* --- Try a real random number source --- *
195 * Some operating systems (notably Linux) provide a `/dev/random' which
196 * contains distilled random numbers from the outside world.
202 unsigned char buff[64];
205 if ((fd = open("/dev/random", O_RDONLY)) >= 0 &&
206 (f = fcntl(fd, F_GETFL, 0)) >= 0 &&
207 fcntl(fd, F_SETFL, f | O_NONBLOCK) >= 0 &&
208 (sz = read(fd, buff, sizeof(buff))) > 0) {
216 /* --- Squeeze some entropy from the current time --- */
222 gettimeofday(&tv, 0);
224 rand_add(&tv, sizeof(tv));
225 rand_add(&c, sizeof(c));
229 /* --- Try some commands which ask the outside world some questions --- */
231 noise__shell("ps auxww");
232 noise__shell("ps -ef");
234 /* @noise__shell("netstat -a");@ -- takes too long */
236 /* --- Get our resource usage to see if that's at all interesting --- */
238 #if defined(HAVE_GETRUSAGE)
241 getrusage(RUSAGE_SELF, &ru);
242 rand_add(&ru, sizeof(ru));
243 getrusage(RUSAGE_CHILDREN, &ru);
244 rand_add(&ru, sizeof(ru));
247 #elif defined(HAVE_VTIMES)
249 struct vtimes vt, vtc;
251 rand_add(&vt, sizeof(vt));
252 rand_add(&vtc, sizeof(vtc));
257 /* --- Squeeze some more entropy from the current time --- */
263 gettimeofday(&tv, 0);
265 rand_add(&tv, sizeof(tv));
266 rand_add(&c, sizeof(c));
270 /* --- Done -- churn the random pool --- */
275 /*----- That's all, folks -------------------------------------------------*/