Fix copyright date.
[become] / src / noise.c
CommitLineData
d9fd0b20 1/* -*-c-*-
2 *
c758e654 3 * $Id: noise.c,v 1.3 1998/01/12 16:46:19 mdw Exp $
d9fd0b20 4 *
5 * Collection of environmental noise
6 *
c758e654 7 * (c) 1998 EBI
d9fd0b20 8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of `become'
13 *
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.
18 *
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.
23 *
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.
27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: noise.c,v $
c758e654 32 * Revision 1.3 1998/01/12 16:46:19 mdw
33 * Fix copyright date.
34 *
35 * Revision 1.2 1997/08/20 16:19:57 mdw
435d981e 36 * Fix test for `/dev/random' so that it doesn't close `stdin' if it fails!
37 *
d9fd0b20 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').
41 *
42 */
43
44/*----- Header files ------------------------------------------------------*/
45
46/* --- ANSI headers --- */
47
48#include <ctype.h>
49#include <errno.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <time.h>
54
55/* --- Unix headers --- */
56
57#include <sys/types.h>
58#include <sys/time.h>
59
60#include "config.h"
61#if defined(HAVE_GETRUSAGE)
62# include <sys/resource.h>
63#elif defined(HAVE_VTIMES)
64# include <sys/vtimes.h>
65#endif
66
67#include <sys/wait.h>
68
69#include <fcntl.h>
70#include <unistd.h>
71
72/* --- Local headers --- */
73
74#include "noise.h"
75#include "rand.h"
76#include "utils.h"
77
78/*----- Main code ---------------------------------------------------------*/
79
80/* --- @noise__shell@ --- *
81 *
82 * Arguments: @const char *cmd@ = pointer to a shell command
83 *
84 * Returns: ---
85 *
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.
88 */
89
90static void noise__shell(const char *cmd)
91{
92 int pfd[2];
93 pid_t pid;
94
95 /* --- Create a pipe for talking to the child --- */
96
97 if (pipe(pfd))
98 return;
99
100 /* --- Create the child process --- */
101
102 pid = fork();
103 if (pid < 0)
104 return;
105
106 if (pid == 0) {
107 int fd;
108 char *argv[] = { "/bin/sh", "-c", 0, 0 };
109 char *env[] = {
110 "PATH=/bin:/usr/bin:/usr/ucb:/usr/etc:/sbin:/usr/sbin",
111 0
112 };
113
114 /* --- Become whoever I'm being run as --- */
115
116 setuid(getuid());
117
118 /* --- Close the old standard streams --- */
119
120 close(0);
121 close(1);
122 close(2);
123
124 /* --- Set up stdin and stderr to be empty, and stdout as our pipe --- */
125
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))
131 goto child_fail;
132
133 /* --- Close the original pipe file descriptors --- */
134
135 close(pfd[0]);
136 close(pfd[1]);
137 burn(pfd);
138
139 /* --- Now run the child process --- */
140
141 argv[2] = (char *)cmd; /* POSIX screwed up the prototype */
142 execve("/bin/sh", argv, env);
143
144 /* --- Something went horribly wrong --- */
145
146 child_fail:
147 _exit(127);
148 }
149
150 /* --- Now read from the child until it's all done --- */
151
152 {
153 char buf[1024];
154 ssize_t sz;
155
156 close(pfd[1]);
157 for (;;) {
158 sz = read(pfd[0], buf, sizeof(buf));
159 if (sz == 0 || (sz < 0 && sz != EINTR))
160 break;
161 rand_add(buf, sz);
162 }
163 close(pfd[0]);
164 rand_add(pfd, sizeof(pfd));
165 burn(buf); burn(pfd);
166 }
167
168 /* --- The child should be dead now, so wait for it --- */
169
170 {
171 int st;
172
173 wait(&st);
174 rand_add(&st, sizeof(st));
175 rand_add(&pid, sizeof(pid));
176 }
177}
178
179/* --- @noise_acquire@ --- *
180 *
181 * Arguments: ---
182 *
183 * Returns: ---
184 *
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.
189 */
190
191void noise_acquire(void)
192{
193 /* --- Try a real random number source --- *
194 *
195 * Some operating systems (notably Linux) provide a `/dev/random' which
196 * contains distilled random numbers from the outside world.
197 */
198
199 {
200 int fd;
201 int f;
202 unsigned char buff[64];
203 ssize_t sz;
204
435d981e 205 if ((fd = open("/dev/random", O_RDONLY)) >= 0 &&
d9fd0b20 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) {
209 rand_add(buff, sz);
210 burn(buff);
211 }
212 if (fd >= 0)
213 close(fd);
214 }
215
216 /* --- Squeeze some entropy from the current time --- */
217
218 {
219 struct timeval tv;
220 clock_t c;
221
222 gettimeofday(&tv, 0);
223 c = clock();
224 rand_add(&tv, sizeof(tv));
225 rand_add(&c, sizeof(c));
226 burn(tv); burn(c);
227 }
228
229 /* --- Try some commands which ask the outside world some questions --- */
230
231 noise__shell("ps auxww");
232 noise__shell("ps -ef");
233 noise__shell("df");
234 /* @noise__shell("netstat -a");@ -- takes too long */
235
236 /* --- Get our resource usage to see if that's at all interesting --- */
237
238#if defined(HAVE_GETRUSAGE)
239 {
240 struct rusage ru;
241 getrusage(RUSAGE_SELF, &ru);
242 rand_add(&ru, sizeof(ru));
243 getrusage(RUSAGE_CHILDREN, &ru);
244 rand_add(&ru, sizeof(ru));
245 burn(ru);
246 }
247#elif defined(HAVE_VTIMES)
248 {
249 struct vtimes vt, vtc;
250 vtimes(&vt, &vtc);
251 rand_add(&vt, sizeof(vt));
252 rand_add(&vtc, sizeof(vtc));
253 burn(vt); burn(vtc);
254 }
255#endif
256
257 /* --- Squeeze some more entropy from the current time --- */
258
259 {
260 struct timeval tv;
261 clock_t c;
262
263 gettimeofday(&tv, 0);
264 c = clock();
265 rand_add(&tv, sizeof(tv));
266 rand_add(&c, sizeof(c));
267 burn(tv); burn(c);
268 }
269
270 /* --- Done -- churn the random pool --- */
271
272 rand_churn();
273}
274
275/*----- That's all, folks -------------------------------------------------*/