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