1 | /* SAMHAIN file system integrity testing */
|
---|
2 | /* Copyright (C) 1999, 2000 Rainer Wichmann */
|
---|
3 | /* */
|
---|
4 | /* This program is free software; you can redistribute it */
|
---|
5 | /* and/or modify */
|
---|
6 | /* it under the terms of the GNU General Public License as */
|
---|
7 | /* published by */
|
---|
8 | /* the Free Software Foundation; either version 2 of the License, or */
|
---|
9 | /* (at your option) any later version. */
|
---|
10 | /* */
|
---|
11 | /* This program is distributed in the hope that it will be useful, */
|
---|
12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
---|
13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
---|
14 | /* GNU General Public License for more details. */
|
---|
15 | /* */
|
---|
16 | /* You should have received a copy of the GNU General Public License */
|
---|
17 | /* along with this program; if not, write to the Free Software */
|
---|
18 | /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
---|
19 |
|
---|
20 |
|
---|
21 | #include "config_xor.h"
|
---|
22 |
|
---|
23 | #include <stdio.h>
|
---|
24 | #include <string.h>
|
---|
25 |
|
---|
26 | #include <sys/types.h>
|
---|
27 |
|
---|
28 | #ifdef HAVE_MEMORY_H
|
---|
29 | #include <memory.h>
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #if TIME_WITH_SYS_TIME
|
---|
33 | #include <sys/time.h>
|
---|
34 | #include <time.h>
|
---|
35 | #else
|
---|
36 | #if HAVE_SYS_TIME_H
|
---|
37 | #include <sys/time.h>
|
---|
38 | #else
|
---|
39 | #include <time.h>
|
---|
40 | #endif
|
---|
41 | #endif
|
---|
42 |
|
---|
43 |
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include <pwd.h>
|
---|
46 | #include <unistd.h>
|
---|
47 | #include <fcntl.h>
|
---|
48 | #include <signal.h>
|
---|
49 | #include <sys/stat.h>
|
---|
50 | #include <errno.h>
|
---|
51 | #include <sys/wait.h>
|
---|
52 |
|
---|
53 |
|
---|
54 | #ifdef HAVE_SYS_SELECT_H
|
---|
55 | #include <sys/select.h>
|
---|
56 | #endif
|
---|
57 | #include <sys/types.h>
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 | #include "samhain.h"
|
---|
62 | #include "sh_utils.h"
|
---|
63 | #include "sh_unix.h"
|
---|
64 | #include "sh_tiger.h"
|
---|
65 | #include "sh_calls.h"
|
---|
66 |
|
---|
67 | #undef FIL__
|
---|
68 | #define FIL__ _("sh_entropy.c")
|
---|
69 |
|
---|
70 | #if defined (HAVE_EGD_RANDOM)
|
---|
71 | /* rndegd.c - interface to the EGD
|
---|
72 | * Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
|
---|
73 | */
|
---|
74 | #include <stddef.h>
|
---|
75 | #include <sys/socket.h>
|
---|
76 | #include <sys/un.h>
|
---|
77 |
|
---|
78 | static int
|
---|
79 | do_write( int fd, void *buf, size_t nbytes )
|
---|
80 | {
|
---|
81 | size_t nleft = nbytes;
|
---|
82 | int nwritten;
|
---|
83 |
|
---|
84 | while( nleft > 0 ) {
|
---|
85 | nwritten = write( fd, buf, nleft);
|
---|
86 | if( nwritten < 0 ) {
|
---|
87 | if( errno == EINTR )
|
---|
88 | continue;
|
---|
89 | return -1;
|
---|
90 | }
|
---|
91 | nleft -= nwritten;
|
---|
92 | buf = (char*)buf + nwritten;
|
---|
93 | }
|
---|
94 | return 0;
|
---|
95 | }
|
---|
96 |
|
---|
97 | static int
|
---|
98 | do_read( int fd, void *buf, int nbytes )
|
---|
99 | {
|
---|
100 | int n, nread = 0;
|
---|
101 |
|
---|
102 | if (nbytes < 0)
|
---|
103 | return 0;
|
---|
104 |
|
---|
105 | do {
|
---|
106 | do {
|
---|
107 | n = read(fd, (char*)buf + nread, nbytes );
|
---|
108 | } while( n == -1 && errno == EINTR );
|
---|
109 | if( n == -1 )
|
---|
110 | return -1;
|
---|
111 | nread += n;
|
---|
112 | } while( nread < nbytes );
|
---|
113 | return nbytes;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | int sh_entropy(int getbytes, char * nbuf)
|
---|
118 | {
|
---|
119 | static int fd = -1;
|
---|
120 | int n;
|
---|
121 | byte buffer[256+2];
|
---|
122 | int nbytes;
|
---|
123 | int do_restart = 0;
|
---|
124 | int myerror = 0;
|
---|
125 | int length;
|
---|
126 | char * p = nbuf;
|
---|
127 | int i;
|
---|
128 |
|
---|
129 | SL_ENTER(_("sh_entropy"));
|
---|
130 |
|
---|
131 | if( getbytes <= 0)
|
---|
132 | SL_RETURN( -1, _("sh_entropy"));
|
---|
133 | if (getbytes > KEY_BYT)
|
---|
134 | getbytes = KEY_BYT;
|
---|
135 | length = getbytes;
|
---|
136 |
|
---|
137 | restart:
|
---|
138 | if( do_restart ) {
|
---|
139 | if( fd != -1 ) {
|
---|
140 | close( fd );
|
---|
141 | fd = -1;
|
---|
142 | }
|
---|
143 | }
|
---|
144 | if( fd == -1 ) {
|
---|
145 | const char *bname = NULL;
|
---|
146 | char *name;
|
---|
147 | struct sockaddr_un addr;
|
---|
148 | int addr_len;
|
---|
149 |
|
---|
150 | #ifdef EGD_SOCKET_NAME
|
---|
151 | bname = EGD_SOCKET_NAME;
|
---|
152 | #endif
|
---|
153 | if ( !bname || !*bname )
|
---|
154 | bname = _("=entropy");
|
---|
155 |
|
---|
156 | if ( *bname == '=' && bname[1] )
|
---|
157 | name = sh_util_strconcat ( DEFAULT_DATAROOT, "/", bname+1 , NULL );
|
---|
158 | else
|
---|
159 | name = sh_util_strconcat ( bname , NULL );
|
---|
160 |
|
---|
161 | if ( strlen(name)+1 >= sizeof(addr.sun_path) )
|
---|
162 | {
|
---|
163 | sh_error_handle ((-1), FIL__, __LINE__, ENAMETOOLONG, MSG_E_SUBGEN,
|
---|
164 | _("EGD socketname is too long"),
|
---|
165 | _("sh_entropy") );
|
---|
166 | SH_FREE(name);
|
---|
167 | SL_RETURN( -1, _("sh_entropy") );
|
---|
168 | }
|
---|
169 |
|
---|
170 | memset( &addr, 0, sizeof(addr) );
|
---|
171 | addr.sun_family = AF_UNIX;
|
---|
172 | sl_strlcpy( addr.sun_path, name, sizeof(addr.sun_path) );
|
---|
173 | addr_len = offsetof( struct sockaddr_un, sun_path )
|
---|
174 | + strlen( addr.sun_path );
|
---|
175 |
|
---|
176 | fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
---|
177 | if( fd == -1 )
|
---|
178 | {
|
---|
179 | myerror = errno;
|
---|
180 | sh_error_handle ((-1), FIL__, __LINE__, myerror, MSG_E_SUBGEN,
|
---|
181 | _("cannot create unix domain socket"),
|
---|
182 | _("sh_entropy") );
|
---|
183 | SH_FREE(name);
|
---|
184 | SL_RETURN( -1, _("sh_entropy") );
|
---|
185 | }
|
---|
186 | if( connect( fd, (struct sockaddr*)&addr, addr_len) == -1 )
|
---|
187 | {
|
---|
188 | myerror = errno;
|
---|
189 | sh_error_handle ((-1), FIL__, __LINE__, myerror, MSG_E_SUBGEN,
|
---|
190 | _("cannot connect to unix domain socket"),
|
---|
191 | _("sh_entropy") );
|
---|
192 | SH_FREE(name);
|
---|
193 | SL_RETURN( -1, _("sh_entropy") );
|
---|
194 | }
|
---|
195 | SH_FREE(name);
|
---|
196 | }
|
---|
197 | do_restart = 0;
|
---|
198 |
|
---|
199 | nbytes = length < 255? length : 255;
|
---|
200 | /* first time we do it with a non blocking request */
|
---|
201 | buffer[0] = 1; /* non blocking */
|
---|
202 | buffer[1] = nbytes;
|
---|
203 | if( do_write( fd, buffer, 2 ) == -1 )
|
---|
204 | {
|
---|
205 | myerror = errno;
|
---|
206 | sh_error_handle ((-1), FIL__, __LINE__, myerror, MSG_E_SUBGEN,
|
---|
207 | _("cannot write to EGD"),
|
---|
208 | _("sh_entropy") );
|
---|
209 | SL_RETURN( -1, _("sh_entropy") );
|
---|
210 | }
|
---|
211 | n = do_read( fd, buffer, 1 );
|
---|
212 | if( n == -1 ) {
|
---|
213 | myerror = errno;
|
---|
214 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, myerror, MSG_E_SUBGEN,
|
---|
215 | _("read error on EGD"),
|
---|
216 | _("sh_entropy") );
|
---|
217 | do_restart = 1;
|
---|
218 | goto restart;
|
---|
219 | }
|
---|
220 | n = buffer[0];
|
---|
221 | if( n ) {
|
---|
222 | n = do_read( fd, buffer, n );
|
---|
223 | if( n == -1 ) {
|
---|
224 | myerror = errno;
|
---|
225 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, myerror,MSG_E_SUBGEN,
|
---|
226 | _("read error on EGD"),
|
---|
227 | _("sh_entropy") );
|
---|
228 | do_restart = 1;
|
---|
229 | goto restart;
|
---|
230 | }
|
---|
231 | for (i = 0; i < n; ++i)
|
---|
232 | {
|
---|
233 | if (getbytes >= 0)
|
---|
234 | { *p = buffer[i]; ++p; --getbytes; }
|
---|
235 | }
|
---|
236 | length -= n;
|
---|
237 | }
|
---|
238 |
|
---|
239 | while( length ) {
|
---|
240 | nbytes = length < 255? length : 255;
|
---|
241 |
|
---|
242 | buffer[0] = 2; /* blocking */
|
---|
243 | buffer[1] = nbytes;
|
---|
244 | if( do_write( fd, buffer, 2 ) == -1 )
|
---|
245 | {
|
---|
246 | myerror = errno;
|
---|
247 | sh_error_handle ((-1), FIL__, __LINE__, myerror, MSG_E_SUBGEN,
|
---|
248 | _("cannot write to EGD"),
|
---|
249 | _("sh_entropy") );
|
---|
250 | SL_RETURN( -1, _("sh_entropy") );
|
---|
251 | }
|
---|
252 | n = do_read( fd, buffer, nbytes );
|
---|
253 | if( n == -1 ) {
|
---|
254 | myerror = errno;
|
---|
255 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, myerror,MSG_E_SUBGEN,
|
---|
256 | _("read error on EGD"),
|
---|
257 | _("sh_entropy") );
|
---|
258 | do_restart = 1;
|
---|
259 | goto restart;
|
---|
260 | }
|
---|
261 | for (i = 0; i < n; ++i)
|
---|
262 | {
|
---|
263 | if (getbytes >= 0)
|
---|
264 | { *p = buffer[i]; ++p; --getbytes; }
|
---|
265 | }
|
---|
266 | length -= n;
|
---|
267 | }
|
---|
268 | memset(buffer, 0, sizeof(buffer) );
|
---|
269 |
|
---|
270 | SL_RETURN( 0, _("sh_entropy") ); /* success */
|
---|
271 | }
|
---|
272 |
|
---|
273 | /* HAVE_EGD_RANDOM */
|
---|
274 | #endif
|
---|
275 |
|
---|
276 | #if defined (HAVE_URANDOM)
|
---|
277 |
|
---|
278 | #include <setjmp.h>
|
---|
279 |
|
---|
280 | static jmp_buf entropy_timeout;
|
---|
281 |
|
---|
282 | static void sh_entropy_alarmhandle (int mysignal)
|
---|
283 | {
|
---|
284 | (void) mysignal; /* avoid compiler warning */
|
---|
285 | longjmp (entropy_timeout, 1);
|
---|
286 | }
|
---|
287 |
|
---|
288 |
|
---|
289 | int read_mbytes(int timeout_val, char * path, char * nbuf, int nbytes)
|
---|
290 | {
|
---|
291 | int count, m_count;
|
---|
292 | int fd2;
|
---|
293 |
|
---|
294 | struct sigaction new_act;
|
---|
295 | sigset_t unblock;
|
---|
296 |
|
---|
297 | struct sigaction old_act;
|
---|
298 | volatile unsigned int old_alarm = 0;
|
---|
299 |
|
---|
300 | new_act.sa_handler = sh_entropy_alarmhandle;
|
---|
301 | sigemptyset( &new_act.sa_mask ); /* set an empty mask */
|
---|
302 | new_act.sa_flags = 0; /* init sa_flags */
|
---|
303 |
|
---|
304 | sigemptyset(&unblock);
|
---|
305 | sigaddset (&unblock, SIGALRM);
|
---|
306 |
|
---|
307 | SL_ENTER(_("read_mbytes"));
|
---|
308 |
|
---|
309 | if ((fd2 = aud_open (FIL__, __LINE__, SL_NOPRIV, path, O_RDONLY, 0)) >= 0)
|
---|
310 | {
|
---|
311 | /* Test whether file is a character device, and is
|
---|
312 | * not world writeable.
|
---|
313 | */
|
---|
314 | if (0 == sh_unix_file_exists(fd2))
|
---|
315 | {
|
---|
316 |
|
---|
317 | /* alarm was triggered
|
---|
318 | */
|
---|
319 | if (setjmp(entropy_timeout) != 0)
|
---|
320 | {
|
---|
321 | alarm(0);
|
---|
322 | sigaction (SIGALRM, &old_act, NULL);
|
---|
323 | alarm(old_alarm);
|
---|
324 | sigprocmask(SIG_UNBLOCK, &unblock, NULL);
|
---|
325 | TPT((0,FIL__,__LINE__, _("msg=<read_mbytes: timeout>\n")));
|
---|
326 | close (fd2);
|
---|
327 | SL_RETURN(0, _("read_mbytes"));
|
---|
328 | }
|
---|
329 |
|
---|
330 | /* timeout after 30 seconds
|
---|
331 | */
|
---|
332 | old_alarm = alarm(0);
|
---|
333 | sigaction (SIGALRM, &new_act, &old_act);
|
---|
334 | alarm(timeout_val);
|
---|
335 |
|
---|
336 | m_count = 0;
|
---|
337 |
|
---|
338 | while (m_count < nbytes)
|
---|
339 | {
|
---|
340 | errno = 0; /* paranoia */
|
---|
341 | count = read (fd2, &nbuf[m_count], nbytes-m_count);
|
---|
342 |
|
---|
343 | switch (count)
|
---|
344 | {
|
---|
345 | case -1:
|
---|
346 | #ifdef EWOULDBLOCK
|
---|
347 | if (errno == EINTR || errno == EAGAIN ||
|
---|
348 | errno == EWOULDBLOCK)
|
---|
349 | #else
|
---|
350 | if (errno == EINTR || errno == EAGAIN)
|
---|
351 | #endif
|
---|
352 | continue;
|
---|
353 |
|
---|
354 | /* if errno == -1 && no continue: fallthrough to this */
|
---|
355 | case 0:
|
---|
356 | break;
|
---|
357 | default:
|
---|
358 | m_count += count;
|
---|
359 | }
|
---|
360 | }
|
---|
361 | close (fd2);
|
---|
362 |
|
---|
363 | alarm(0);
|
---|
364 | sigaction (SIGALRM, &old_act, NULL);
|
---|
365 | alarm(old_alarm);
|
---|
366 | sigprocmask(SIG_UNBLOCK, &unblock, NULL);
|
---|
367 | }
|
---|
368 | else
|
---|
369 | m_count = 0;
|
---|
370 | }
|
---|
371 | else
|
---|
372 | m_count = 0;
|
---|
373 |
|
---|
374 | TPT((0, FIL__, __LINE__, _("msg=<read_mbytes: OK>\n")));
|
---|
375 | SL_RETURN(m_count, _("read_mbytes"));
|
---|
376 | }
|
---|
377 |
|
---|
378 | /* Read nbytes bytes from /dev/random, mix them with
|
---|
379 | * previous reads using a hash function, and give out
|
---|
380 | * nbytes bytes from the result.
|
---|
381 | */
|
---|
382 | int sh_entropy(int nbytes, char * nbuf)
|
---|
383 | {
|
---|
384 | int i, m_count = 0;
|
---|
385 | char * keybuf;
|
---|
386 | char addbuf[2 * KEY_BYT];
|
---|
387 |
|
---|
388 | SL_ENTER(_("sh_entropy"));
|
---|
389 |
|
---|
390 | ASSERT((nbytes <= KEY_BYT), _("nbytes <= KEY_BYT"))
|
---|
391 |
|
---|
392 | if (nbytes > KEY_BYT)
|
---|
393 | nbytes = KEY_BYT;
|
---|
394 |
|
---|
395 | memset(nbuf, '\0', nbytes);
|
---|
396 |
|
---|
397 | #ifdef NAME_OF_DEV_URANDOM
|
---|
398 | m_count = read_mbytes (30, NAME_OF_DEV_RANDOM, nbuf, nbytes);
|
---|
399 | #else
|
---|
400 | m_count = read_mbytes (300, NAME_OF_DEV_RANDOM, nbuf, nbytes);
|
---|
401 | #endif
|
---|
402 |
|
---|
403 | if (m_count == 0)
|
---|
404 | {
|
---|
405 | #ifdef NAME_OF_DEV_URANDOM
|
---|
406 | sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, EIO, MSG_NODEV,
|
---|
407 | (long) sh.real.uid, NAME_OF_DEV_RANDOM);
|
---|
408 | #else
|
---|
409 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_NODEV,
|
---|
410 | (long) sh.real.uid, NAME_OF_DEV_RANDOM);
|
---|
411 | #endif
|
---|
412 | }
|
---|
413 |
|
---|
414 | #ifdef NAME_OF_DEV_URANDOM
|
---|
415 | if (m_count < nbytes)
|
---|
416 | {
|
---|
417 | i = read_mbytes(30, NAME_OF_DEV_URANDOM, &nbuf[m_count], nbytes-m_count);
|
---|
418 | if (i == 0)
|
---|
419 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_NODEV,
|
---|
420 | (long) sh.real.uid, NAME_OF_DEV_URANDOM);
|
---|
421 | else
|
---|
422 | m_count += i;
|
---|
423 | }
|
---|
424 | #endif
|
---|
425 |
|
---|
426 |
|
---|
427 | if (m_count > 0)
|
---|
428 | {
|
---|
429 | /* -- Add previous entropy into the new pool. --
|
---|
430 | */
|
---|
431 | memset(addbuf, '\0', sizeof(addbuf));
|
---|
432 | for (i = 0; i < m_count; ++i)
|
---|
433 | addbuf[i] = nbuf[i];
|
---|
434 | for (i = 0; i < KEY_BYT; ++i)
|
---|
435 | addbuf[i+KEY_BYT] = skey->poolv[i];
|
---|
436 | keybuf = (char *) sh_tiger_hash_uint32 (addbuf,
|
---|
437 | TIGER_DATA, 2 * KEY_BYT);
|
---|
438 | memset(addbuf, '\0', sizeof(addbuf));
|
---|
439 |
|
---|
440 | /* -- Give out nbytes bytes from the new pool. --
|
---|
441 | */
|
---|
442 | for (i = 0; i < KEY_BYT; ++i)
|
---|
443 | {
|
---|
444 | skey->poolv[i] = keybuf[i];
|
---|
445 | if (i < nbytes)
|
---|
446 | nbuf[i] = keybuf[i];
|
---|
447 | }
|
---|
448 | memset (keybuf, '\0', KEY_BYT);
|
---|
449 |
|
---|
450 | SL_RETURN(0, _("sh_entropy"));
|
---|
451 | }
|
---|
452 | else
|
---|
453 | {
|
---|
454 | SL_RETURN((-1), _("sh_entropy"));
|
---|
455 | }
|
---|
456 | }
|
---|
457 |
|
---|
458 | /* HAVE_URANDOM */
|
---|
459 | #endif
|
---|
460 |
|
---|
461 | #ifdef HAVE_UNIX_RANDOM
|
---|
462 |
|
---|
463 | #ifndef FD_SET
|
---|
464 | #define NFDBITS 32
|
---|
465 | #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
|
---|
466 | #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
|
---|
467 | #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
|
---|
468 | #endif /* !FD_SET */
|
---|
469 | #ifndef FD_SETSIZE
|
---|
470 | #define FD_SETSIZE 32
|
---|
471 | #endif
|
---|
472 | #ifndef FD_ZERO
|
---|
473 | #define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
|
---|
474 | #endif
|
---|
475 |
|
---|
476 | #include "sh_static.h"
|
---|
477 |
|
---|
478 | static
|
---|
479 | char * com_path[] = {
|
---|
480 | N_("/usr/ucb/"),
|
---|
481 | N_("/bin/"),
|
---|
482 | N_("/sbin/"),
|
---|
483 | N_("/usr/bin/"),
|
---|
484 | N_("/usr/sbin/"),
|
---|
485 | N_("/usr/local/bin/"),
|
---|
486 | NULL
|
---|
487 | };
|
---|
488 |
|
---|
489 |
|
---|
490 | typedef struct {
|
---|
491 | char * command;
|
---|
492 | char * arg;
|
---|
493 | int pipeFD;
|
---|
494 | pid_t pid;
|
---|
495 | int isset;
|
---|
496 | FILE * pipe;
|
---|
497 | } sourcetable_t;
|
---|
498 |
|
---|
499 | static
|
---|
500 | sourcetable_t source[] = {
|
---|
501 | { N_("w"),
|
---|
502 | N_("w"),
|
---|
503 | 0,
|
---|
504 | 0,
|
---|
505 | 0,
|
---|
506 | NULL },
|
---|
507 | { N_("netstat"),
|
---|
508 | N_("netstat -n"),
|
---|
509 | 0,
|
---|
510 | 0,
|
---|
511 | 0,
|
---|
512 | NULL },
|
---|
513 | { N_("ps"),
|
---|
514 | N_("ps -ef"),
|
---|
515 | 0,
|
---|
516 | 0,
|
---|
517 | 0,
|
---|
518 | NULL },
|
---|
519 | { N_("arp"),
|
---|
520 | N_("arp -a"),
|
---|
521 | 0,
|
---|
522 | 0,
|
---|
523 | 0,
|
---|
524 | NULL },
|
---|
525 | { N_("free"),
|
---|
526 | N_("free"),
|
---|
527 | 0,
|
---|
528 | 0,
|
---|
529 | 0,
|
---|
530 | NULL },
|
---|
531 | { N_("uptime"),
|
---|
532 | N_("uptime"),
|
---|
533 | 0,
|
---|
534 | 0,
|
---|
535 | 0,
|
---|
536 | NULL },
|
---|
537 | { N_("procinfo"),
|
---|
538 | N_("procinfo -a"),
|
---|
539 | 0,
|
---|
540 | 0,
|
---|
541 | 0,
|
---|
542 | NULL },
|
---|
543 | { N_("vmstat"),
|
---|
544 | N_("vmstat"),
|
---|
545 | 0,
|
---|
546 | 0,
|
---|
547 | 0,
|
---|
548 | NULL },
|
---|
549 | { N_("w"), /* Play it again, Sam. */
|
---|
550 | N_("w"),
|
---|
551 | 0,
|
---|
552 | 0,
|
---|
553 | 0,
|
---|
554 | NULL },
|
---|
555 | { NULL,
|
---|
556 | NULL,
|
---|
557 | 0,
|
---|
558 | 0,
|
---|
559 | 0,
|
---|
560 | NULL }
|
---|
561 | };
|
---|
562 |
|
---|
563 |
|
---|
564 | static FILE * sh_popen (sourcetable_t *source, char * command)
|
---|
565 | {
|
---|
566 | int i;
|
---|
567 | int pipedes[2];
|
---|
568 | FILE *outf = NULL;
|
---|
569 | struct passwd * tempres;
|
---|
570 | char * arg[4];
|
---|
571 | char * envp[2];
|
---|
572 | size_t len;
|
---|
573 |
|
---|
574 | SL_ENTER(_("sh_popen"));
|
---|
575 |
|
---|
576 | arg[0] = _("/bin/sh");
|
---|
577 | arg[1] = _("-c");
|
---|
578 | arg[2] = command;
|
---|
579 | arg[3] = NULL;
|
---|
580 |
|
---|
581 | if (sh.timezone != NULL)
|
---|
582 | {
|
---|
583 | len = sl_strlen(sh.timezone) + 4;
|
---|
584 | envp[0] = malloc (len); /* free() ok */
|
---|
585 | if (envp[0] != NULL)
|
---|
586 | sl_snprintf (envp[0], len, "TZ=%s", sh.timezone);
|
---|
587 | else
|
---|
588 | envp[0] = NULL;
|
---|
589 | envp[1] = NULL;
|
---|
590 | }
|
---|
591 | else
|
---|
592 | {
|
---|
593 | envp[0] = NULL;
|
---|
594 | }
|
---|
595 |
|
---|
596 |
|
---|
597 | /* Create the pipe
|
---|
598 | */
|
---|
599 | if (aud_pipe(FIL__, __LINE__, pipedes) < 0) {
|
---|
600 | if (envp[0] != NULL) free(envp[0]);
|
---|
601 | SL_RETURN(NULL, _("sh_popen"));
|
---|
602 | }
|
---|
603 |
|
---|
604 | source->pid = aud_fork(FIL__, __LINE__);
|
---|
605 |
|
---|
606 | /* Failure
|
---|
607 | */
|
---|
608 | if (source->pid == (pid_t) - 1) {
|
---|
609 | close(pipedes[0]);
|
---|
610 | close(pipedes[1]);
|
---|
611 | if (envp[0] != NULL) free(envp[0]);
|
---|
612 | SL_RETURN(NULL, _("sh_popen"));
|
---|
613 | }
|
---|
614 |
|
---|
615 | if (source->pid == (pid_t) 0)
|
---|
616 | {
|
---|
617 |
|
---|
618 | /* child - make read side of the pipe stdout
|
---|
619 | */
|
---|
620 | if (retry_aud_dup2(FIL__, __LINE__,
|
---|
621 | pipedes[STDOUT_FILENO], STDOUT_FILENO) < 0)
|
---|
622 | aud__exit(FIL__, __LINE__, EXIT_FAILURE);
|
---|
623 |
|
---|
624 | /* close the pipe descriptors
|
---|
625 | */
|
---|
626 | close (pipedes[STDIN_FILENO]);
|
---|
627 | close (pipedes[STDOUT_FILENO]);
|
---|
628 |
|
---|
629 | /* don't leak file descriptors
|
---|
630 | */
|
---|
631 | sh_unix_closeall (3, -1); /* in child process */
|
---|
632 |
|
---|
633 | /* zero priv info
|
---|
634 | */
|
---|
635 | memset(skey, 0, sizeof(sh_key_t));
|
---|
636 |
|
---|
637 | /* drop root privileges
|
---|
638 | */
|
---|
639 | i = 0;
|
---|
640 | if (0 == geteuid()) {
|
---|
641 | tempres = sh_getpwnam(DEFAULT_IDENT);
|
---|
642 | if (NULL != tempres) {
|
---|
643 | i = aud_setgid(FIL__, __LINE__, tempres->pw_gid);
|
---|
644 | if (i == 0)
|
---|
645 | i = sh_unix_initgroups(DEFAULT_IDENT ,tempres->pw_gid);
|
---|
646 | if (i == 0)
|
---|
647 | i = aud_setuid(FIL__, __LINE__, tempres->pw_uid);
|
---|
648 | /* make sure we cannot get root again
|
---|
649 | */
|
---|
650 | if ((tempres->pw_uid != 0) && (aud_setuid(FIL__, __LINE__, 0) >= 0))
|
---|
651 | i = -1;
|
---|
652 | } else {
|
---|
653 | i = -1;
|
---|
654 | }
|
---|
655 | }
|
---|
656 |
|
---|
657 | /* some problem ...
|
---|
658 | */
|
---|
659 | if (i == -1) {
|
---|
660 | aud__exit(FIL__, __LINE__, EXIT_FAILURE);
|
---|
661 | }
|
---|
662 |
|
---|
663 | freopen (_("/dev/null"), "r+", stderr);
|
---|
664 |
|
---|
665 | /* exec the program */
|
---|
666 | retry_aud_execve (FIL__, __LINE__, _("/bin/sh"), arg, envp);
|
---|
667 |
|
---|
668 | /* failed
|
---|
669 | */
|
---|
670 | aud__exit(FIL__, __LINE__, EXIT_FAILURE);
|
---|
671 | }
|
---|
672 |
|
---|
673 | /* parent
|
---|
674 | */
|
---|
675 | if (envp[0] != NULL)
|
---|
676 | free(envp[0]);
|
---|
677 |
|
---|
678 | close (pipedes[STDOUT_FILENO]);
|
---|
679 | retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
|
---|
680 |
|
---|
681 | outf = fdopen (pipedes[STDIN_FILENO], "r");
|
---|
682 |
|
---|
683 | if (outf == NULL)
|
---|
684 | {
|
---|
685 | aud_kill (FIL__, __LINE__, source->pid, SIGKILL);
|
---|
686 | close (pipedes[STDOUT_FILENO]);
|
---|
687 | waitpid (source->pid, NULL, 0);
|
---|
688 | source->pid = 0;
|
---|
689 | SL_RETURN(NULL, _("sh_popen"));
|
---|
690 | }
|
---|
691 |
|
---|
692 | SL_RETURN(outf, _("sh_popen"));
|
---|
693 | }
|
---|
694 |
|
---|
695 |
|
---|
696 | static int sh_pclose (sourcetable_t *source)
|
---|
697 | {
|
---|
698 | int status = 0;
|
---|
699 |
|
---|
700 | SL_ENTER(_("sh_pclose"));
|
---|
701 |
|
---|
702 | status = fclose(source->pipe);
|
---|
703 | if (status)
|
---|
704 | {
|
---|
705 | SL_RETURN((-1), _("sh_pclose"));
|
---|
706 | }
|
---|
707 |
|
---|
708 | if (waitpid(source->pid, NULL, 0) != source->pid)
|
---|
709 | status = -1;
|
---|
710 |
|
---|
711 | source->pipe = NULL;
|
---|
712 | source->pid = 0;
|
---|
713 | SL_RETURN(status, _("sh_pclose"));
|
---|
714 | }
|
---|
715 |
|
---|
716 | #define BUF_ENT 32766
|
---|
717 |
|
---|
718 | /* Poll the system for randomness, mix results with
|
---|
719 | * previous reads using a hash function, and give out
|
---|
720 | * nbytes bytes from the result.
|
---|
721 | */
|
---|
722 | int sh_entropy(int nbytes, char * nbuf)
|
---|
723 | {
|
---|
724 | int caperr;
|
---|
725 | char combuf[80];
|
---|
726 | char * buffer;
|
---|
727 | int i, j, icount;
|
---|
728 | int bufcount = 0;
|
---|
729 | int count;
|
---|
730 |
|
---|
731 | char * keybuf;
|
---|
732 | char addbuf[2 * KEY_BYT];
|
---|
733 |
|
---|
734 | struct timeval tv;
|
---|
735 | fd_set fds;
|
---|
736 | unsigned long select_now = 0;
|
---|
737 | int maxFD = 0;
|
---|
738 | int imax, selcount;
|
---|
739 |
|
---|
740 | SL_ENTER(_("sh_entropy"));
|
---|
741 |
|
---|
742 | ASSERT((nbytes <= KEY_BYT), _("nbytes <= KEY_BYT"))
|
---|
743 |
|
---|
744 | if (nbytes > KEY_BYT)
|
---|
745 | nbytes = KEY_BYT;
|
---|
746 |
|
---|
747 |
|
---|
748 | /* --- If there is entropy in the pool, return it. ---
|
---|
749 | */
|
---|
750 | if (skey->poolc >= nbytes)
|
---|
751 | {
|
---|
752 | j = KEY_BYT - skey->poolc;
|
---|
753 | for (i = 0; i < nbytes; ++i)
|
---|
754 | {
|
---|
755 | nbuf[i] = skey->poolv[i+j];
|
---|
756 | --skey->poolc;
|
---|
757 | }
|
---|
758 | SL_RETURN(0, _("sh_entropy"));
|
---|
759 | }
|
---|
760 |
|
---|
761 |
|
---|
762 | FD_ZERO(&fds);
|
---|
763 |
|
---|
764 | i = 0; icount = 0;
|
---|
765 | buffer = SH_ALLOC(BUF_ENT+2);
|
---|
766 |
|
---|
767 | if (0 != (caperr = sl_get_cap_sub()))
|
---|
768 | {
|
---|
769 | sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
|
---|
770 | sh_error_message (caperr),
|
---|
771 | _("sl_get_cap_sub"));
|
---|
772 | }
|
---|
773 |
|
---|
774 | while (source[i].command != NULL) {
|
---|
775 |
|
---|
776 | j = 0;
|
---|
777 | while (com_path[j] != NULL)
|
---|
778 | {
|
---|
779 | sl_strlcpy(combuf, _(com_path[j]), 80);
|
---|
780 | sl_strlcat(combuf, _(source[i].command), 80);
|
---|
781 |
|
---|
782 | /* flawfinder: ignore */
|
---|
783 | if ( access (combuf, X_OK) == 0)
|
---|
784 | {
|
---|
785 | sl_strlcpy(combuf, _(com_path[j]), 80);
|
---|
786 | sl_strlcat(combuf, _(source[i].arg), 80);
|
---|
787 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENSTART,
|
---|
788 | combuf);
|
---|
789 | break;
|
---|
790 | }
|
---|
791 | ++j;
|
---|
792 | }
|
---|
793 |
|
---|
794 | /* Not found, try next command.
|
---|
795 | */
|
---|
796 | if (com_path[j] == NULL)
|
---|
797 | {
|
---|
798 | ++i;
|
---|
799 | continue;
|
---|
800 | }
|
---|
801 |
|
---|
802 | /* Source exists
|
---|
803 | */
|
---|
804 | source[i].pipe = sh_popen ( &source[i], combuf );
|
---|
805 | if (NULL != source[i].pipe)
|
---|
806 | {
|
---|
807 | source[i].pipeFD = fileno ( source[i].pipe );
|
---|
808 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENEXEC,
|
---|
809 | combuf, (long) source[i].pipeFD);
|
---|
810 |
|
---|
811 | maxFD = (source[i].pipeFD > maxFD) ? source[i].pipeFD : maxFD;
|
---|
812 | retry_fcntl( FIL__, __LINE__, source[i].pipeFD, F_SETFL, O_NONBLOCK);
|
---|
813 | FD_SET( source[i].pipeFD, &fds );
|
---|
814 | source[i].isset = 1;
|
---|
815 | ++icount;
|
---|
816 | }
|
---|
817 | else
|
---|
818 | {
|
---|
819 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_ENFAIL,
|
---|
820 | combuf);
|
---|
821 | }
|
---|
822 |
|
---|
823 | ++i;
|
---|
824 | }
|
---|
825 |
|
---|
826 | imax = i;
|
---|
827 | tv.tv_sec = 1;
|
---|
828 | tv.tv_usec = 0;
|
---|
829 | bufcount = 0;
|
---|
830 |
|
---|
831 | while ( (icount > 0) && (bufcount < BUF_ENT) ) {
|
---|
832 |
|
---|
833 | if ( (selcount = select (maxFD+1, &fds, NULL, NULL, &tv)) == -1)
|
---|
834 | break;
|
---|
835 |
|
---|
836 | /* reset timeout for select()
|
---|
837 | */
|
---|
838 | tv.tv_sec = 1;
|
---|
839 | tv.tv_usec = 0;
|
---|
840 |
|
---|
841 | /* timeout - let's not hang on forever
|
---|
842 | */
|
---|
843 | if (selcount == 0)
|
---|
844 | {
|
---|
845 | ++select_now;
|
---|
846 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENTOUT,
|
---|
847 | (unsigned long) select_now);
|
---|
848 | if ( select_now > 9 )
|
---|
849 | break;
|
---|
850 | }
|
---|
851 |
|
---|
852 | for (i = 0; i < imax; ++i) {
|
---|
853 |
|
---|
854 | if ( FD_ISSET (source[i].pipeFD, &fds) ) {
|
---|
855 | count = fread (&buffer[bufcount],
|
---|
856 | 1,
|
---|
857 | BUF_ENT-bufcount,
|
---|
858 | source[i].pipe );
|
---|
859 | if (count == 0)
|
---|
860 | {
|
---|
861 | if (0 != feof(source[i].pipe))
|
---|
862 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_ENCLOS,
|
---|
863 | (long) source[i].pipeFD);
|
---|
864 | else
|
---|
865 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_ENCLOS1,
|
---|
866 | (long) source[i].pipeFD);
|
---|
867 | source[i].isset = 0;
|
---|
868 | sh_pclose ( &source[i] );
|
---|
869 | --icount;
|
---|
870 | }
|
---|
871 | else
|
---|
872 | {
|
---|
873 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENREAD,
|
---|
874 | (long) source[i].pipeFD, (long) count);
|
---|
875 | }
|
---|
876 | bufcount += count;
|
---|
877 |
|
---|
878 | }
|
---|
879 | }
|
---|
880 |
|
---|
881 | maxFD = 0;
|
---|
882 | FD_ZERO(&fds);
|
---|
883 |
|
---|
884 | for (i = 0; i < imax; ++i)
|
---|
885 | {
|
---|
886 | if (source[i].isset == 1)
|
---|
887 | {
|
---|
888 | FD_SET( source[i].pipeFD, &fds );
|
---|
889 | maxFD = (source[i].pipeFD > maxFD) ? source[i].pipeFD : maxFD;
|
---|
890 | }
|
---|
891 | }
|
---|
892 | }
|
---|
893 |
|
---|
894 | for (i = 0; i < imax; ++i)
|
---|
895 | {
|
---|
896 | if (source[i].isset == 1)
|
---|
897 | {
|
---|
898 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENCLOS1,
|
---|
899 | (long) source[i].pipeFD);
|
---|
900 | sh_pclose ( &source[i] );
|
---|
901 | }
|
---|
902 | }
|
---|
903 | buffer[bufcount] = '\0';
|
---|
904 |
|
---|
905 | if (0 != (caperr = sl_drop_cap_sub()))
|
---|
906 | {
|
---|
907 | sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
|
---|
908 | sh_error_message (caperr),
|
---|
909 | _("sl_drop_cap_sub"));
|
---|
910 | }
|
---|
911 |
|
---|
912 | if (bufcount > 0)
|
---|
913 | {
|
---|
914 | keybuf = (char *) sh_tiger_hash_uint32 (buffer,
|
---|
915 | TIGER_DATA, sl_strlen(buffer));
|
---|
916 |
|
---|
917 | /* add previous entropy into the new pool
|
---|
918 | */
|
---|
919 | memset(addbuf, '\0', sizeof(addbuf));
|
---|
920 | for (i = 0; i < KEY_BYT; ++i)
|
---|
921 | {
|
---|
922 | addbuf[i] = keybuf[i];
|
---|
923 | addbuf[i+KEY_BYT] = skey->poolv[i];
|
---|
924 | }
|
---|
925 | keybuf = (char *) sh_tiger_hash_uint32 (addbuf,
|
---|
926 | TIGER_DATA, sizeof(addbuf));
|
---|
927 | memset(addbuf, '\0', sizeof(addbuf));
|
---|
928 |
|
---|
929 | /* store in system pool
|
---|
930 | */
|
---|
931 | for (i = 0; i < KEY_BYT; ++i)
|
---|
932 | skey->poolv[i] = keybuf[i];
|
---|
933 | skey->poolc = KEY_BYT;
|
---|
934 | memset (buffer, '\0', BUF_ENT+2);
|
---|
935 | memset (keybuf, '\0', KEY_BYT);
|
---|
936 | SH_FREE(buffer);
|
---|
937 | }
|
---|
938 | else
|
---|
939 | {
|
---|
940 | SH_FREE(buffer);
|
---|
941 | SL_RETURN((-1), _("sh_entropy"));
|
---|
942 | }
|
---|
943 |
|
---|
944 | /* give out nbytes Bytes from the entropy pool
|
---|
945 | */
|
---|
946 | for (i = 0; i < nbytes; ++i)
|
---|
947 | {
|
---|
948 | nbuf[i] = skey->poolv[i];
|
---|
949 | --skey->poolc;
|
---|
950 | }
|
---|
951 |
|
---|
952 | SL_RETURN(0, _("sh_entropy"));
|
---|
953 | }
|
---|
954 |
|
---|
955 | /* HAVE_UNIX_RANDOM */
|
---|
956 | #endif
|
---|
957 |
|
---|
958 |
|
---|
959 |
|
---|
960 |
|
---|
961 |
|
---|
962 |
|
---|
963 |
|
---|