3 * $Id: noise.c,v 1.1 1997/08/07 09:45:26 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.1 1997/08/07 09:45:26 mdw
33 * New source file added to acquire environmental noise and add it to the
34 * randomness pool (see `rand.c').
38 /*----- Header files ------------------------------------------------------*/
40 /* --- ANSI headers --- */
49 /* --- Unix headers --- */
51 #include <sys/types.h>
55 #if defined(HAVE_GETRUSAGE)
56 # include <sys/resource.h>
57 #elif defined(HAVE_VTIMES)
58 # include <sys/vtimes.h>
66 /* --- Local headers --- */
72 /*----- Main code ---------------------------------------------------------*/
74 /* --- @noise__shell@ --- *
76 * Arguments: @const char *cmd@ = pointer to a shell command
80 * Use: Adds the output of the shell command to the randomness pool.
81 * Some care is taken to do the Right Thing when running setuid.
84 static void noise__shell(const char *cmd
)
89 /* --- Create a pipe for talking to the child --- */
94 /* --- Create the child process --- */
102 char *argv
[] = { "/bin/sh", "-c", 0, 0 };
104 "PATH=/bin:/usr/bin:/usr/ucb:/usr/etc:/sbin:/usr/sbin",
108 /* --- Become whoever I'm being run as --- */
112 /* --- Close the old standard streams --- */
118 /* --- Set up stdin and stderr to be empty, and stdout as our pipe --- */
120 if (((fd
= open("/dev/null", O_RDONLY
)) != 0 &&
121 (fd
= dup2(fd
, 0)) != 0) ||
122 ((fd
= dup2(pfd
[1], 1)) != 1) ||
123 ((fd
= open("/dev/null", O_WRONLY
)) != 2 &&
124 (fd
= dup2(fd
, 2)) != 2))
127 /* --- Close the original pipe file descriptors --- */
133 /* --- Now run the child process --- */
135 argv
[2] = (char *)cmd
; /* POSIX screwed up the prototype */
136 execve("/bin/sh", argv
, env
);
138 /* --- Something went horribly wrong --- */
144 /* --- Now read from the child until it's all done --- */
152 sz
= read(pfd
[0], buf
, sizeof(buf
));
153 if (sz
== 0 || (sz
< 0 && sz
!= EINTR
))
158 rand_add(pfd
, sizeof(pfd
));
159 burn(buf
); burn(pfd
);
162 /* --- The child should be dead now, so wait for it --- */
168 rand_add(&st
, sizeof(st
));
169 rand_add(&pid
, sizeof(pid
));
173 /* --- @noise_acquire@ --- *
179 * Use: Attempts to acquire an amount of random noise from the
180 * environment. A lot of it's not actually much good, but
181 * it's better than nothing. There's probably a bit or two's
182 * worth in each item which gets added.
185 void noise_acquire(void)
187 /* --- Try a real random number source --- *
189 * Some operating systems (notably Linux) provide a `/dev/random' which
190 * contains distilled random numbers from the outside world.
196 unsigned char buff
[64];
199 if ((fd
= open("/dev/random", O_RDONLY
) >= 0) &&
200 (f
= fcntl(fd
, F_GETFL
, 0)) >= 0 &&
201 fcntl(fd
, F_SETFL
, f
| O_NONBLOCK
) >= 0 &&
202 (sz
= read(fd
, buff
, sizeof(buff
))) > 0) {
210 /* --- Squeeze some entropy from the current time --- */
216 gettimeofday(&tv
, 0);
218 rand_add(&tv
, sizeof(tv
));
219 rand_add(&c
, sizeof(c
));
223 /* --- Try some commands which ask the outside world some questions --- */
225 noise__shell("ps auxww");
226 noise__shell("ps -ef");
228 /* @noise__shell("netstat -a");@ -- takes too long */
230 /* --- Get our resource usage to see if that's at all interesting --- */
232 #if defined(HAVE_GETRUSAGE)
235 getrusage(RUSAGE_SELF
, &ru
);
236 rand_add(&ru
, sizeof(ru
));
237 getrusage(RUSAGE_CHILDREN
, &ru
);
238 rand_add(&ru
, sizeof(ru
));
241 #elif defined(HAVE_VTIMES)
243 struct vtimes vt
, vtc
;
245 rand_add(&vt
, sizeof(vt
));
246 rand_add(&vtc
, sizeof(vtc
));
251 /* --- Squeeze some more entropy from the current time --- */
257 gettimeofday(&tv
, 0);
259 rand_add(&tv
, sizeof(tv
));
260 rand_add(&c
, sizeof(c
));
264 /* --- Done -- churn the random pool --- */
269 /*----- That's all, folks -------------------------------------------------*/