3 * $Id: noise.c,v 1.4 1998/02/20 17:52:32 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.4 1998/02/20 17:52:32 mdw
33 * Don't use `df' for noise gathering, because it gets upset when NFS
34 * servers aren't responding.
36 * Revision 1.3 1998/01/12 16:46:19 mdw
39 * Revision 1.2 1997/08/20 16:19:57 mdw
40 * Fix test for `/dev/random' so that it doesn't close `stdin' if it fails!
42 * Revision 1.1 1997/08/07 09:45:26 mdw
43 * New source file added to acquire environmental noise and add it to the
44 * randomness pool (see `rand.c').
48 /*----- Header files ------------------------------------------------------*/
50 /* --- ANSI headers --- */
59 /* --- Unix headers --- */
61 #include <sys/types.h>
65 #if defined(HAVE_GETRUSAGE)
66 # include <sys/resource.h>
67 #elif defined(HAVE_VTIMES)
68 # include <sys/vtimes.h>
76 /* --- Local headers --- */
82 /*----- Main code ---------------------------------------------------------*/
84 /* --- @noise__shell@ --- *
86 * Arguments: @const char *cmd@ = pointer to a shell command
90 * Use: Adds the output of the shell command to the randomness pool.
91 * Some care is taken to do the Right Thing when running setuid.
94 static void noise__shell(const char *cmd)
99 /* --- Create a pipe for talking to the child --- */
104 /* --- Create the child process --- */
112 char *argv[] = { "/bin/sh", "-c", 0, 0 };
114 "PATH=/bin:/usr/bin:/usr/ucb:/usr/etc:/sbin:/usr/sbin",
118 /* --- Become whoever I'm being run as --- */
122 /* --- Close the old standard streams --- */
128 /* --- Set up stdin and stderr to be empty, and stdout as our pipe --- */
130 if (((fd = open("/dev/null", O_RDONLY)) != 0 &&
131 (fd = dup2(fd, 0)) != 0) ||
132 ((fd = dup2(pfd[1], 1)) != 1) ||
133 ((fd = open("/dev/null", O_WRONLY)) != 2 &&
134 (fd = dup2(fd, 2)) != 2))
137 /* --- Close the original pipe file descriptors --- */
143 /* --- Now run the child process --- */
145 argv[2] = (char *)cmd; /* POSIX screwed up the prototype */
146 execve("/bin/sh", argv, env);
148 /* --- Something went horribly wrong --- */
154 /* --- Now read from the child until it's all done --- */
162 sz = read(pfd[0], buf, sizeof(buf));
163 if (sz == 0 || (sz < 0 && sz != EINTR))
168 rand_add(pfd, sizeof(pfd));
169 burn(buf); burn(pfd);
172 /* --- The child should be dead now, so wait for it --- */
178 rand_add(&st, sizeof(st));
179 rand_add(&pid, sizeof(pid));
183 /* --- @noise_acquire@ --- *
189 * Use: Attempts to acquire an amount of random noise from the
190 * environment. A lot of it's not actually much good, but
191 * it's better than nothing. There's probably a bit or two's
192 * worth in each item which gets added.
195 void noise_acquire(void)
197 /* --- Try a real random number source --- *
199 * Some operating systems (notably Linux) provide a `/dev/random' which
200 * contains distilled random numbers from the outside world.
206 unsigned char buff[64];
209 if ((fd = open("/dev/random", O_RDONLY)) >= 0 &&
210 (f = fcntl(fd, F_GETFL, 0)) >= 0 &&
211 fcntl(fd, F_SETFL, f | O_NONBLOCK) >= 0 &&
212 (sz = read(fd, buff, sizeof(buff))) > 0) {
220 /* --- Squeeze some entropy from the current time --- */
226 gettimeofday(&tv, 0);
228 rand_add(&tv, sizeof(tv));
229 rand_add(&c, sizeof(c));
233 /* --- Try some commands which ask the outside world some questions --- */
235 noise__shell("ps auxww");
236 noise__shell("ps -ef");
237 /* @noise__shell("df");@ -- irritates NFS */
238 noise__shell("netstat -an");
240 /* --- Get our resource usage to see if that's at all interesting --- */
242 #if defined(HAVE_GETRUSAGE)
245 getrusage(RUSAGE_SELF, &ru);
246 rand_add(&ru, sizeof(ru));
247 getrusage(RUSAGE_CHILDREN, &ru);
248 rand_add(&ru, sizeof(ru));
251 #elif defined(HAVE_VTIMES)
253 struct vtimes vt, vtc;
255 rand_add(&vt, sizeof(vt));
256 rand_add(&vtc, sizeof(vtc));
261 /* --- Squeeze some more entropy from the current time --- */
267 gettimeofday(&tv, 0);
269 rand_add(&tv, sizeof(tv));
270 rand_add(&c, sizeof(c));
274 /* --- Done -- churn the random pool --- */
279 /*----- That's all, folks -------------------------------------------------*/