3 * $Id: noise.c,v 1.2 1997/08/20 16:19:57 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.2 1997/08/20 16:19:57 mdw
33 * Fix test for `/dev/random' so that it doesn't close `stdin' if it fails!
35 * Revision 1.1 1997/08/07 09:45:26 mdw
36 * New source file added to acquire environmental noise and add it to the
37 * randomness pool (see `rand.c').
41 /*----- Header files ------------------------------------------------------*/
43 /* --- ANSI headers --- */
52 /* --- Unix headers --- */
54 #include <sys/types.h>
58 #if defined(HAVE_GETRUSAGE)
59 # include <sys/resource.h>
60 #elif defined(HAVE_VTIMES)
61 # include <sys/vtimes.h>
69 /* --- Local headers --- */
75 /*----- Main code ---------------------------------------------------------*/
77 /* --- @noise__shell@ --- *
79 * Arguments: @const char *cmd@ = pointer to a shell command
83 * Use: Adds the output of the shell command to the randomness pool.
84 * Some care is taken to do the Right Thing when running setuid.
87 static void noise__shell(const char *cmd
)
92 /* --- Create a pipe for talking to the child --- */
97 /* --- Create the child process --- */
105 char *argv
[] = { "/bin/sh", "-c", 0, 0 };
107 "PATH=/bin:/usr/bin:/usr/ucb:/usr/etc:/sbin:/usr/sbin",
111 /* --- Become whoever I'm being run as --- */
115 /* --- Close the old standard streams --- */
121 /* --- Set up stdin and stderr to be empty, and stdout as our pipe --- */
123 if (((fd
= open("/dev/null", O_RDONLY
)) != 0 &&
124 (fd
= dup2(fd
, 0)) != 0) ||
125 ((fd
= dup2(pfd
[1], 1)) != 1) ||
126 ((fd
= open("/dev/null", O_WRONLY
)) != 2 &&
127 (fd
= dup2(fd
, 2)) != 2))
130 /* --- Close the original pipe file descriptors --- */
136 /* --- Now run the child process --- */
138 argv
[2] = (char *)cmd
; /* POSIX screwed up the prototype */
139 execve("/bin/sh", argv
, env
);
141 /* --- Something went horribly wrong --- */
147 /* --- Now read from the child until it's all done --- */
155 sz
= read(pfd
[0], buf
, sizeof(buf
));
156 if (sz
== 0 || (sz
< 0 && sz
!= EINTR
))
161 rand_add(pfd
, sizeof(pfd
));
162 burn(buf
); burn(pfd
);
165 /* --- The child should be dead now, so wait for it --- */
171 rand_add(&st
, sizeof(st
));
172 rand_add(&pid
, sizeof(pid
));
176 /* --- @noise_acquire@ --- *
182 * Use: Attempts to acquire an amount of random noise from the
183 * environment. A lot of it's not actually much good, but
184 * it's better than nothing. There's probably a bit or two's
185 * worth in each item which gets added.
188 void noise_acquire(void)
190 /* --- Try a real random number source --- *
192 * Some operating systems (notably Linux) provide a `/dev/random' which
193 * contains distilled random numbers from the outside world.
199 unsigned char buff
[64];
202 if ((fd
= open("/dev/random", O_RDONLY
)) >= 0 &&
203 (f
= fcntl(fd
, F_GETFL
, 0)) >= 0 &&
204 fcntl(fd
, F_SETFL
, f
| O_NONBLOCK
) >= 0 &&
205 (sz
= read(fd
, buff
, sizeof(buff
))) > 0) {
213 /* --- Squeeze some entropy from the current time --- */
219 gettimeofday(&tv
, 0);
221 rand_add(&tv
, sizeof(tv
));
222 rand_add(&c
, sizeof(c
));
226 /* --- Try some commands which ask the outside world some questions --- */
228 noise__shell("ps auxww");
229 noise__shell("ps -ef");
231 /* @noise__shell("netstat -a");@ -- takes too long */
233 /* --- Get our resource usage to see if that's at all interesting --- */
235 #if defined(HAVE_GETRUSAGE)
238 getrusage(RUSAGE_SELF
, &ru
);
239 rand_add(&ru
, sizeof(ru
));
240 getrusage(RUSAGE_CHILDREN
, &ru
);
241 rand_add(&ru
, sizeof(ru
));
244 #elif defined(HAVE_VTIMES)
246 struct vtimes vt
, vtc
;
248 rand_add(&vt
, sizeof(vt
));
249 rand_add(&vtc
, sizeof(vtc
));
254 /* --- Squeeze some more entropy from the current time --- */
260 gettimeofday(&tv
, 0);
262 rand_add(&tv
, sizeof(tv
));
263 rand_add(&c
, sizeof(c
));
267 /* --- Done -- churn the random pool --- */
272 /*----- That's all, folks -------------------------------------------------*/