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