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