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 | 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 | close(fd);
|
---|
194 | SL_RETURN( -1, _("sh_entropy") );
|
---|
195 | }
|
---|
196 | SH_FREE(name);
|
---|
197 | }
|
---|
198 | do_restart = 0;
|
---|
199 |
|
---|
200 | nbytes = length < 255? length : 255;
|
---|
201 | /* first time we do it with a non blocking request */
|
---|
202 | buffer[0] = 1; /* non blocking */
|
---|
203 | buffer[1] = nbytes;
|
---|
204 | if( do_write( fd, buffer, 2 ) == -1 )
|
---|
205 | {
|
---|
206 | myerror = errno;
|
---|
207 | sh_error_handle ((-1), FIL__, __LINE__, myerror, MSG_E_SUBGEN,
|
---|
208 | _("cannot write to EGD"),
|
---|
209 | _("sh_entropy") );
|
---|
210 | close(fd);
|
---|
211 | SL_RETURN( -1, _("sh_entropy") );
|
---|
212 | }
|
---|
213 | n = do_read( fd, buffer, 1 );
|
---|
214 | if( n == -1 ) {
|
---|
215 | myerror = errno;
|
---|
216 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, myerror, MSG_E_SUBGEN,
|
---|
217 | _("read error on EGD"),
|
---|
218 | _("sh_entropy") );
|
---|
219 | do_restart = 1;
|
---|
220 | goto restart;
|
---|
221 | }
|
---|
222 | n = buffer[0];
|
---|
223 | if( n ) {
|
---|
224 | n = do_read( fd, buffer, n );
|
---|
225 | if( n == -1 ) {
|
---|
226 | myerror = errno;
|
---|
227 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, myerror,MSG_E_SUBGEN,
|
---|
228 | _("read error on EGD"),
|
---|
229 | _("sh_entropy") );
|
---|
230 | do_restart = 1;
|
---|
231 | goto restart;
|
---|
232 | }
|
---|
233 | for (i = 0; i < n; ++i)
|
---|
234 | {
|
---|
235 | if (getbytes >= 0)
|
---|
236 | { *p = buffer[i]; ++p; --getbytes; }
|
---|
237 | }
|
---|
238 | length -= n;
|
---|
239 | }
|
---|
240 |
|
---|
241 | while( length ) {
|
---|
242 | nbytes = length < 255? length : 255;
|
---|
243 |
|
---|
244 | buffer[0] = 2; /* blocking */
|
---|
245 | buffer[1] = nbytes;
|
---|
246 | if( do_write( fd, buffer, 2 ) == -1 )
|
---|
247 | {
|
---|
248 | myerror = errno;
|
---|
249 | sh_error_handle ((-1), FIL__, __LINE__, myerror, MSG_E_SUBGEN,
|
---|
250 | _("cannot write to EGD"),
|
---|
251 | _("sh_entropy") );
|
---|
252 | close(fd);
|
---|
253 | SL_RETURN( -1, _("sh_entropy") );
|
---|
254 | }
|
---|
255 | n = do_read( fd, buffer, nbytes );
|
---|
256 | if( n == -1 ) {
|
---|
257 | myerror = errno;
|
---|
258 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, myerror,MSG_E_SUBGEN,
|
---|
259 | _("read error on EGD"),
|
---|
260 | _("sh_entropy") );
|
---|
261 | do_restart = 1;
|
---|
262 | goto restart;
|
---|
263 | }
|
---|
264 | for (i = 0; i < n; ++i)
|
---|
265 | {
|
---|
266 | if (getbytes >= 0)
|
---|
267 | { *p = buffer[i]; ++p; --getbytes; }
|
---|
268 | }
|
---|
269 | length -= n;
|
---|
270 | }
|
---|
271 | memset(buffer, 0, sizeof(buffer) );
|
---|
272 | close(fd);
|
---|
273 | SL_RETURN( 0, _("sh_entropy") ); /* success */
|
---|
274 | }
|
---|
275 |
|
---|
276 | /* HAVE_EGD_RANDOM */
|
---|
277 | #endif
|
---|
278 |
|
---|
279 | #if defined (HAVE_URANDOM)
|
---|
280 |
|
---|
281 | #include "sh_pthread.h"
|
---|
282 |
|
---|
283 | int read_mbytes(int timeout_val, char * path, char * nbuf, int nbytes)
|
---|
284 | {
|
---|
285 | int m_count;
|
---|
286 | int fd2;
|
---|
287 |
|
---|
288 | SL_ENTER(_("read_mbytes"));
|
---|
289 |
|
---|
290 | if ((fd2 = aud_open (FIL__, __LINE__, SL_NOPRIV, path, O_RDONLY, 0)) >= 0)
|
---|
291 | {
|
---|
292 | /* Test whether file is a character device, and is
|
---|
293 | * readable.
|
---|
294 | */
|
---|
295 | if (0 == sh_unix_device_readable(fd2))
|
---|
296 | {
|
---|
297 | m_count = sl_read_timeout_fd(fd2, &nbuf, nbytes,
|
---|
298 | timeout_val, SL_FALSE);
|
---|
299 | if (m_count < 0)
|
---|
300 | m_count = 0;
|
---|
301 | }
|
---|
302 | else
|
---|
303 | m_count = 0;
|
---|
304 | }
|
---|
305 | else
|
---|
306 | m_count = 0;
|
---|
307 |
|
---|
308 | close(fd2);
|
---|
309 |
|
---|
310 | TPT((0, FIL__, __LINE__, _("msg=<read_mbytes: OK>\n")));
|
---|
311 | SL_RETURN(m_count, _("read_mbytes"));
|
---|
312 | }
|
---|
313 |
|
---|
314 | /* Read nbytes bytes from /dev/random, mix them with
|
---|
315 | * previous reads using a hash function, and give out
|
---|
316 | * nbytes bytes from the result.
|
---|
317 | */
|
---|
318 | int sh_entropy(int nbytes, char * nbuf)
|
---|
319 | {
|
---|
320 | int i, m_count = 0;
|
---|
321 | char * keybuf;
|
---|
322 | UINT32 kbuf[KEY_BYT/sizeof(UINT32)];
|
---|
323 | char addbuf[2 * KEY_BYT];
|
---|
324 |
|
---|
325 | SL_ENTER(_("sh_entropy"));
|
---|
326 |
|
---|
327 | ASSERT((nbytes <= KEY_BYT), _("nbytes <= KEY_BYT"))
|
---|
328 |
|
---|
329 | if (nbytes > KEY_BYT)
|
---|
330 | nbytes = KEY_BYT;
|
---|
331 |
|
---|
332 | memset(nbuf, '\0', nbytes);
|
---|
333 |
|
---|
334 | #ifdef NAME_OF_DEV_URANDOM
|
---|
335 | m_count = read_mbytes ( 1, NAME_OF_DEV_RANDOM, nbuf, nbytes);
|
---|
336 | #else
|
---|
337 | m_count = read_mbytes (300, NAME_OF_DEV_RANDOM, nbuf, nbytes);
|
---|
338 | #endif
|
---|
339 |
|
---|
340 | if (m_count == 0)
|
---|
341 | {
|
---|
342 | #ifdef NAME_OF_DEV_URANDOM
|
---|
343 | sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, EIO, MSG_NODEV,
|
---|
344 | (long) sh.real.uid, NAME_OF_DEV_RANDOM);
|
---|
345 | #else
|
---|
346 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_NODEV,
|
---|
347 | (long) sh.real.uid, NAME_OF_DEV_RANDOM);
|
---|
348 | #endif
|
---|
349 | }
|
---|
350 |
|
---|
351 | #ifdef NAME_OF_DEV_URANDOM
|
---|
352 | if (m_count < nbytes)
|
---|
353 | {
|
---|
354 | i = read_mbytes(30, NAME_OF_DEV_URANDOM, &nbuf[m_count], nbytes-m_count);
|
---|
355 | if (i == 0)
|
---|
356 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_NODEV,
|
---|
357 | (long) sh.real.uid, NAME_OF_DEV_URANDOM);
|
---|
358 | else
|
---|
359 | m_count += i;
|
---|
360 | }
|
---|
361 | #endif
|
---|
362 |
|
---|
363 |
|
---|
364 | if (m_count > 0)
|
---|
365 | {
|
---|
366 | /* -- Add previous entropy into the new pool. --
|
---|
367 | */
|
---|
368 | memset(addbuf, '\0', sizeof(addbuf));
|
---|
369 | for (i = 0; i < m_count; ++i)
|
---|
370 | addbuf[i] = nbuf[i];
|
---|
371 | for (i = 0; i < KEY_BYT; ++i)
|
---|
372 | addbuf[i+KEY_BYT] = skey->poolv[i];
|
---|
373 | keybuf = (char *) sh_tiger_hash_uint32 (addbuf,
|
---|
374 | TIGER_DATA, 2 * KEY_BYT,
|
---|
375 | kbuf, KEY_BYT/sizeof(UINT32));
|
---|
376 | memset(addbuf, '\0', sizeof(addbuf));
|
---|
377 |
|
---|
378 | /* -- Give out nbytes bytes from the new pool. --
|
---|
379 | */
|
---|
380 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
381 | for (i = 0; i < KEY_BYT; ++i)
|
---|
382 | {
|
---|
383 | skey->poolv[i] = keybuf[i];
|
---|
384 | if (i < nbytes)
|
---|
385 | nbuf[i] = keybuf[i];
|
---|
386 | }
|
---|
387 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
388 | memset (keybuf, '\0', KEY_BYT);
|
---|
389 |
|
---|
390 | SL_RETURN(0, _("sh_entropy"));
|
---|
391 | }
|
---|
392 | else
|
---|
393 | {
|
---|
394 | SL_RETURN((-1), _("sh_entropy"));
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | /* HAVE_URANDOM */
|
---|
399 | #endif
|
---|
400 |
|
---|
401 | #ifdef HAVE_UNIX_RANDOM
|
---|
402 |
|
---|
403 | #ifndef FD_SET
|
---|
404 | #define NFDBITS 32
|
---|
405 | #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
|
---|
406 | #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
|
---|
407 | #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
|
---|
408 | #endif /* !FD_SET */
|
---|
409 | #ifndef FD_SETSIZE
|
---|
410 | #define FD_SETSIZE 32
|
---|
411 | #endif
|
---|
412 | #ifndef FD_ZERO
|
---|
413 | #define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
|
---|
414 | #endif
|
---|
415 |
|
---|
416 | #include "sh_static.h"
|
---|
417 | #include "sh_pthread.h"
|
---|
418 |
|
---|
419 | static
|
---|
420 | char * com_path[] = {
|
---|
421 | N_("/usr/bin/xpg4/"),
|
---|
422 | N_("/usr/ucb/"),
|
---|
423 | N_("/bin/"),
|
---|
424 | N_("/sbin/"),
|
---|
425 | N_("/usr/bin/"),
|
---|
426 | N_("/usr/sbin/"),
|
---|
427 | N_("/usr/local/bin/"),
|
---|
428 | NULL
|
---|
429 | };
|
---|
430 |
|
---|
431 |
|
---|
432 | typedef struct {
|
---|
433 | char * command;
|
---|
434 | char * arg;
|
---|
435 | int pipeFD;
|
---|
436 | pid_t pid;
|
---|
437 | int isset;
|
---|
438 | FILE * pipe;
|
---|
439 | } sourcetable_t;
|
---|
440 |
|
---|
441 | static
|
---|
442 | sourcetable_t source_template[] = {
|
---|
443 | { N_("w"),
|
---|
444 | N_("w"),
|
---|
445 | 0,
|
---|
446 | 0,
|
---|
447 | 0,
|
---|
448 | NULL },
|
---|
449 | { N_("netstat"),
|
---|
450 | N_("netstat -n"),
|
---|
451 | 0,
|
---|
452 | 0,
|
---|
453 | 0,
|
---|
454 | NULL },
|
---|
455 | { N_("ps"),
|
---|
456 | N_("ps -ef"),
|
---|
457 | 0,
|
---|
458 | 0,
|
---|
459 | 0,
|
---|
460 | NULL },
|
---|
461 | { N_("arp"),
|
---|
462 | N_("arp -a"),
|
---|
463 | 0,
|
---|
464 | 0,
|
---|
465 | 0,
|
---|
466 | NULL },
|
---|
467 | { N_("free"),
|
---|
468 | N_("free"),
|
---|
469 | 0,
|
---|
470 | 0,
|
---|
471 | 0,
|
---|
472 | NULL },
|
---|
473 | { N_("uptime"),
|
---|
474 | N_("uptime"),
|
---|
475 | 0,
|
---|
476 | 0,
|
---|
477 | 0,
|
---|
478 | NULL },
|
---|
479 | { N_("procinfo"),
|
---|
480 | N_("procinfo -a"),
|
---|
481 | 0,
|
---|
482 | 0,
|
---|
483 | 0,
|
---|
484 | NULL },
|
---|
485 | { N_("vmstat"),
|
---|
486 | N_("vmstat"),
|
---|
487 | 0,
|
---|
488 | 0,
|
---|
489 | 0,
|
---|
490 | NULL },
|
---|
491 | { N_("w"), /* Play it again, Sam. */
|
---|
492 | N_("w"),
|
---|
493 | 0,
|
---|
494 | 0,
|
---|
495 | 0,
|
---|
496 | NULL },
|
---|
497 | { NULL,
|
---|
498 | NULL,
|
---|
499 | 0,
|
---|
500 | 0,
|
---|
501 | 0,
|
---|
502 | NULL }
|
---|
503 | };
|
---|
504 |
|
---|
505 |
|
---|
506 | static FILE * sh_popen (sourcetable_t *source, char * command)
|
---|
507 | {
|
---|
508 | int i;
|
---|
509 | int pipedes[2];
|
---|
510 | FILE *outf = NULL;
|
---|
511 | char * arg[4];
|
---|
512 | char * envp[2];
|
---|
513 | size_t len;
|
---|
514 | char arg0[80];
|
---|
515 | char arg1[80];
|
---|
516 |
|
---|
517 | SL_ENTER(_("sh_popen"));
|
---|
518 |
|
---|
519 | strncpy (arg0, _("/bin/sh"), sizeof(arg0));
|
---|
520 | arg[0] = arg0;
|
---|
521 | strncpy (arg1, _("-c"), sizeof(arg1));
|
---|
522 | arg[1] = arg1;
|
---|
523 | arg[2] = command;
|
---|
524 | arg[3] = NULL;
|
---|
525 |
|
---|
526 | if (sh.timezone != NULL)
|
---|
527 | {
|
---|
528 | len = sl_strlen(sh.timezone) + 4;
|
---|
529 | envp[0] = malloc (len); /* free() ok */
|
---|
530 | if (envp[0] != NULL)
|
---|
531 | sl_snprintf (envp[0], len, "TZ=%s", sh.timezone);
|
---|
532 | else
|
---|
533 | envp[0] = NULL;
|
---|
534 | envp[1] = NULL;
|
---|
535 | }
|
---|
536 | else
|
---|
537 | {
|
---|
538 | envp[0] = NULL;
|
---|
539 | }
|
---|
540 |
|
---|
541 |
|
---|
542 | /* Create the pipe
|
---|
543 | */
|
---|
544 | if (aud_pipe(FIL__, __LINE__, pipedes) < 0) {
|
---|
545 | if (envp[0] != NULL) free(envp[0]);
|
---|
546 | SL_RETURN(NULL, _("sh_popen"));
|
---|
547 | }
|
---|
548 |
|
---|
549 | fflush (NULL);
|
---|
550 |
|
---|
551 | source->pid = aud_fork(FIL__, __LINE__);
|
---|
552 |
|
---|
553 | /* Failure
|
---|
554 | */
|
---|
555 | if (source->pid == (pid_t) - 1) {
|
---|
556 | close(pipedes[0]);
|
---|
557 | close(pipedes[1]);
|
---|
558 | if (envp[0] != NULL) free(envp[0]);
|
---|
559 | SL_RETURN(NULL, _("sh_popen"));
|
---|
560 | }
|
---|
561 |
|
---|
562 | if (source->pid == (pid_t) 0)
|
---|
563 | {
|
---|
564 |
|
---|
565 | /* child - make read side of the pipe stdout
|
---|
566 | */
|
---|
567 | if (retry_aud_dup2(FIL__, __LINE__,
|
---|
568 | pipedes[STDOUT_FILENO], STDOUT_FILENO) < 0)
|
---|
569 | aud__exit(FIL__, __LINE__, EXIT_FAILURE);
|
---|
570 |
|
---|
571 | /* close the pipe descriptors
|
---|
572 | */
|
---|
573 | close (pipedes[STDIN_FILENO]);
|
---|
574 | close (pipedes[STDOUT_FILENO]);
|
---|
575 |
|
---|
576 | /* don't leak file descriptors
|
---|
577 | */
|
---|
578 | sh_unix_closeall (3, -1); /* in child process */
|
---|
579 |
|
---|
580 | /* zero priv info
|
---|
581 | */
|
---|
582 | memset(skey, 0, sizeof(sh_key_t));
|
---|
583 |
|
---|
584 | /* drop root privileges
|
---|
585 | */
|
---|
586 | i = 0;
|
---|
587 | if (0 == geteuid())
|
---|
588 | {
|
---|
589 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
|
---|
590 | struct passwd pwd;
|
---|
591 | char buffer[SH_PWBUF_SIZE];
|
---|
592 | struct passwd * tempres;
|
---|
593 | sh_getpwnam_r(DEFAULT_IDENT, &pwd, buffer, sizeof(buffer), &tempres);
|
---|
594 | #else
|
---|
595 | struct passwd * tempres = sh_getpwnam(DEFAULT_IDENT);
|
---|
596 | #endif
|
---|
597 |
|
---|
598 | if (NULL != tempres) {
|
---|
599 | i = aud_setgid(FIL__, __LINE__, tempres->pw_gid);
|
---|
600 | if (i == 0)
|
---|
601 | i = sh_unix_initgroups(DEFAULT_IDENT ,tempres->pw_gid);
|
---|
602 | if (i == 0)
|
---|
603 | i = aud_setuid(FIL__, __LINE__, tempres->pw_uid);
|
---|
604 | /* make sure we cannot get root again
|
---|
605 | */
|
---|
606 | if ((tempres->pw_uid != 0) && (aud_setuid(FIL__, __LINE__, 0) >= 0))
|
---|
607 | i = -1;
|
---|
608 | } else {
|
---|
609 | i = -1;
|
---|
610 | }
|
---|
611 | }
|
---|
612 |
|
---|
613 | /* some problem ...
|
---|
614 | */
|
---|
615 | if (i == -1) {
|
---|
616 | aud__exit(FIL__, __LINE__, EXIT_FAILURE);
|
---|
617 | }
|
---|
618 |
|
---|
619 | freopen (_("/dev/null"), "r+", stderr);
|
---|
620 |
|
---|
621 | /* exec the program */
|
---|
622 | retry_aud_execve (FIL__, __LINE__, _("/bin/sh"), arg, envp);
|
---|
623 |
|
---|
624 | /* failed
|
---|
625 | */
|
---|
626 | aud__exit(FIL__, __LINE__, EXIT_FAILURE);
|
---|
627 | }
|
---|
628 |
|
---|
629 | /* parent
|
---|
630 | */
|
---|
631 | if (envp[0] != NULL)
|
---|
632 | free(envp[0]);
|
---|
633 |
|
---|
634 | close (pipedes[STDOUT_FILENO]);
|
---|
635 | retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
|
---|
636 |
|
---|
637 | outf = fdopen (pipedes[STDIN_FILENO], "r");
|
---|
638 |
|
---|
639 | if (outf == NULL)
|
---|
640 | {
|
---|
641 | aud_kill (FIL__, __LINE__, source->pid, SIGKILL);
|
---|
642 | close (pipedes[STDOUT_FILENO]);
|
---|
643 | waitpid (source->pid, NULL, 0);
|
---|
644 | source->pid = 0;
|
---|
645 | SL_RETURN(NULL, _("sh_popen"));
|
---|
646 | }
|
---|
647 |
|
---|
648 | SL_RETURN(outf, _("sh_popen"));
|
---|
649 | }
|
---|
650 |
|
---|
651 |
|
---|
652 | static int sh_pclose (sourcetable_t *source)
|
---|
653 | {
|
---|
654 | int status = 0;
|
---|
655 | int retval;
|
---|
656 | char msg[128];
|
---|
657 | char errbuf[SH_ERRBUF_SIZE];
|
---|
658 |
|
---|
659 | SL_ENTER(_("sh_pclose"));
|
---|
660 |
|
---|
661 | retval = fclose(source->pipe);
|
---|
662 | if (retval)
|
---|
663 | {
|
---|
664 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, retval,
|
---|
665 | MSG_E_SUBGEN,
|
---|
666 | sh_error_message(retval, errbuf, sizeof(errbuf)),
|
---|
667 | _("sh_pclose"));
|
---|
668 | SL_RETURN((-1), _("sh_pclose"));
|
---|
669 | }
|
---|
670 |
|
---|
671 | retval = waitpid(source->pid, &status, 0);
|
---|
672 | if (retval != source->pid)
|
---|
673 | {
|
---|
674 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, retval,
|
---|
675 | MSG_E_SUBGEN,
|
---|
676 | sh_error_message(retval, errbuf, sizeof(errbuf)),
|
---|
677 | _("sh_pclose"));
|
---|
678 |
|
---|
679 | status = -1;
|
---|
680 | }
|
---|
681 | else if (WIFSIGNALED(status))
|
---|
682 | {
|
---|
683 | sl_snprintf(msg, sizeof(msg), _("Subprocess terminated by signal %d"),
|
---|
684 | WTERMSIG(status));
|
---|
685 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, retval,
|
---|
686 | MSG_E_SUBGEN,
|
---|
687 | msg,
|
---|
688 | _("sh_pclose"));
|
---|
689 | status = -1;
|
---|
690 | }
|
---|
691 |
|
---|
692 | source->pipe = NULL;
|
---|
693 | source->pid = 0;
|
---|
694 | SL_RETURN(status, _("sh_pclose"));
|
---|
695 | }
|
---|
696 |
|
---|
697 | #define BUF_ENT 32766
|
---|
698 |
|
---|
699 | /* Poll the system for randomness, mix results with
|
---|
700 | * previous reads using a hash function, and give out
|
---|
701 | * nbytes bytes from the result.
|
---|
702 | */
|
---|
703 | int sh_entropy(int nbytes, char * nbuf)
|
---|
704 | {
|
---|
705 | int caperr;
|
---|
706 | char combuf[80];
|
---|
707 | char * buffer;
|
---|
708 | int i, j, icount;
|
---|
709 | int bufcount = 0;
|
---|
710 | int count;
|
---|
711 |
|
---|
712 | char * keybuf;
|
---|
713 | UINT32 kbuf[KEY_BYT/sizeof(UINT32)];
|
---|
714 | char addbuf[2 * KEY_BYT];
|
---|
715 |
|
---|
716 | struct timeval tv;
|
---|
717 | fd_set fds;
|
---|
718 | unsigned long select_now = 0;
|
---|
719 | int maxFD = 0;
|
---|
720 | int imax, selcount;
|
---|
721 | char errbuf[SH_ERRBUF_SIZE];
|
---|
722 |
|
---|
723 | sourcetable_t *source = NULL;
|
---|
724 |
|
---|
725 | SL_ENTER(_("sh_entropy"));
|
---|
726 |
|
---|
727 | ASSERT((nbytes <= KEY_BYT), _("nbytes <= KEY_BYT"))
|
---|
728 |
|
---|
729 | if (nbytes > KEY_BYT)
|
---|
730 | nbytes = KEY_BYT;
|
---|
731 |
|
---|
732 |
|
---|
733 | /* --- If there is entropy in the pool, return it. ---
|
---|
734 | */
|
---|
735 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
736 | if (skey->poolc >= nbytes)
|
---|
737 | {
|
---|
738 | j = KEY_BYT - skey->poolc;
|
---|
739 | for (i = 0; i < nbytes; ++i)
|
---|
740 | {
|
---|
741 | nbuf[i] = skey->poolv[i+j];
|
---|
742 | --skey->poolc;
|
---|
743 | }
|
---|
744 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey); /* alternative path */
|
---|
745 | SL_RETURN(0, _("sh_entropy"));
|
---|
746 | }
|
---|
747 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
748 |
|
---|
749 |
|
---|
750 | FD_ZERO(&fds);
|
---|
751 |
|
---|
752 | i = 0; icount = 0;
|
---|
753 | buffer = SH_ALLOC(BUF_ENT+2);
|
---|
754 |
|
---|
755 | if (0 != (caperr = sl_get_cap_sub()))
|
---|
756 | {
|
---|
757 | sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
|
---|
758 | sh_error_message (caperr, errbuf, sizeof(errbuf)),
|
---|
759 | _("sl_get_cap_sub"));
|
---|
760 | }
|
---|
761 |
|
---|
762 | while (source_template[i].command != NULL) {
|
---|
763 | ++i;
|
---|
764 | }
|
---|
765 | source = SH_ALLOC(i * sizeof(sourcetable_t));
|
---|
766 | for (j = 0; j < i;++j)
|
---|
767 | memcpy(&source[j], &source_template[j], sizeof(sourcetable_t));
|
---|
768 | i = 0;
|
---|
769 |
|
---|
770 | while (source[i].command != NULL) {
|
---|
771 |
|
---|
772 | j = 0;
|
---|
773 | while (com_path[j] != NULL)
|
---|
774 | {
|
---|
775 | sl_strlcpy(combuf, _(com_path[j]), 80);
|
---|
776 | sl_strlcat(combuf, _(source[i].command), 80);
|
---|
777 |
|
---|
778 | /* flawfinder: ignore */
|
---|
779 | if ( access (combuf, X_OK) == 0)
|
---|
780 | {
|
---|
781 | sl_strlcpy(combuf, _(com_path[j]), 80);
|
---|
782 | sl_strlcat(combuf, _(source[i].arg), 80);
|
---|
783 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENSTART,
|
---|
784 | combuf);
|
---|
785 | break;
|
---|
786 | }
|
---|
787 | ++j;
|
---|
788 | }
|
---|
789 |
|
---|
790 | /* Not found, try next command.
|
---|
791 | */
|
---|
792 | if (com_path[j] == NULL)
|
---|
793 | {
|
---|
794 | ++i;
|
---|
795 | continue;
|
---|
796 | }
|
---|
797 |
|
---|
798 | /* Source exists
|
---|
799 | */
|
---|
800 | source[i].pipe = sh_popen ( &source[i], combuf );
|
---|
801 | if (NULL != source[i].pipe)
|
---|
802 | {
|
---|
803 | source[i].pipeFD = fileno ( source[i].pipe );
|
---|
804 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENEXEC,
|
---|
805 | combuf, (long) source[i].pipeFD);
|
---|
806 |
|
---|
807 | maxFD = (source[i].pipeFD > maxFD) ? source[i].pipeFD : maxFD;
|
---|
808 | retry_fcntl( FIL__, __LINE__, source[i].pipeFD, F_SETFL, O_NONBLOCK);
|
---|
809 | FD_SET( source[i].pipeFD, &fds );
|
---|
810 | source[i].isset = 1;
|
---|
811 | ++icount;
|
---|
812 | }
|
---|
813 | else
|
---|
814 | {
|
---|
815 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_ENFAIL,
|
---|
816 | combuf);
|
---|
817 | }
|
---|
818 |
|
---|
819 | ++i;
|
---|
820 | }
|
---|
821 |
|
---|
822 | imax = i;
|
---|
823 | tv.tv_sec = 1;
|
---|
824 | tv.tv_usec = 0;
|
---|
825 | bufcount = 0;
|
---|
826 |
|
---|
827 | while ( (icount > 0) && (bufcount < BUF_ENT) ) {
|
---|
828 |
|
---|
829 | if ( (selcount = select (maxFD+1, &fds, NULL, NULL, &tv)) == -1)
|
---|
830 | break;
|
---|
831 |
|
---|
832 | /* reset timeout for select()
|
---|
833 | */
|
---|
834 | tv.tv_sec = 1;
|
---|
835 | tv.tv_usec = 0;
|
---|
836 |
|
---|
837 | /* timeout - let's not hang on forever
|
---|
838 | */
|
---|
839 | if (selcount == 0)
|
---|
840 | {
|
---|
841 | ++select_now;
|
---|
842 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENTOUT,
|
---|
843 | (unsigned long) select_now);
|
---|
844 | if ( select_now > 9 )
|
---|
845 | break;
|
---|
846 | }
|
---|
847 |
|
---|
848 | for (i = 0; i < imax; ++i) {
|
---|
849 |
|
---|
850 | if ( FD_ISSET (source[i].pipeFD, &fds) ) {
|
---|
851 | count = fread (&buffer[bufcount],
|
---|
852 | 1,
|
---|
853 | BUF_ENT-bufcount,
|
---|
854 | source[i].pipe );
|
---|
855 | if (count == 0)
|
---|
856 | {
|
---|
857 | if (0 != feof(source[i].pipe))
|
---|
858 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_ENCLOS,
|
---|
859 | (long) source[i].pipeFD);
|
---|
860 | else
|
---|
861 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_ENCLOS1,
|
---|
862 | (long) source[i].pipeFD);
|
---|
863 | source[i].isset = 0;
|
---|
864 | sh_pclose ( &source[i] );
|
---|
865 | --icount;
|
---|
866 | }
|
---|
867 | else
|
---|
868 | {
|
---|
869 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENREAD,
|
---|
870 | (long) source[i].pipeFD, (long) count);
|
---|
871 | }
|
---|
872 | bufcount += count;
|
---|
873 |
|
---|
874 | }
|
---|
875 | }
|
---|
876 |
|
---|
877 | maxFD = 0;
|
---|
878 | FD_ZERO(&fds);
|
---|
879 |
|
---|
880 | for (i = 0; i < imax; ++i)
|
---|
881 | {
|
---|
882 | if (source[i].isset == 1)
|
---|
883 | {
|
---|
884 | FD_SET( source[i].pipeFD, &fds );
|
---|
885 | maxFD = (source[i].pipeFD > maxFD) ? source[i].pipeFD : maxFD;
|
---|
886 | }
|
---|
887 | }
|
---|
888 | }
|
---|
889 |
|
---|
890 | for (i = 0; i < imax; ++i)
|
---|
891 | {
|
---|
892 | if (source[i].isset == 1)
|
---|
893 | {
|
---|
894 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENCLOS1,
|
---|
895 | (long) source[i].pipeFD);
|
---|
896 | sh_pclose ( &source[i] );
|
---|
897 | }
|
---|
898 | }
|
---|
899 | buffer[bufcount] = '\0';
|
---|
900 |
|
---|
901 | SH_FREE(source);
|
---|
902 |
|
---|
903 | if (0 != (caperr = sl_drop_cap_sub()))
|
---|
904 | {
|
---|
905 | sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
|
---|
906 | sh_error_message (caperr, errbuf, sizeof(errbuf)),
|
---|
907 | _("sl_drop_cap_sub"));
|
---|
908 | }
|
---|
909 |
|
---|
910 | if (bufcount > 0)
|
---|
911 | {
|
---|
912 | keybuf = (char *) sh_tiger_hash_uint32 (buffer,
|
---|
913 | TIGER_DATA, sl_strlen(buffer),
|
---|
914 | kbuf, KEY_BYT/sizeof(UINT32));
|
---|
915 |
|
---|
916 | /* add previous entropy into the new pool
|
---|
917 | */
|
---|
918 | memset(addbuf, '\0', sizeof(addbuf));
|
---|
919 | for (i = 0; i < KEY_BYT; ++i)
|
---|
920 | {
|
---|
921 | addbuf[i] = keybuf[i];
|
---|
922 | addbuf[i+KEY_BYT] = skey->poolv[i];
|
---|
923 | }
|
---|
924 | keybuf = (char *) sh_tiger_hash_uint32 (addbuf,
|
---|
925 | TIGER_DATA, sizeof(addbuf),
|
---|
926 | kbuf, KEY_BYT/sizeof(UINT32));
|
---|
927 | memset(addbuf, '\0', sizeof(addbuf));
|
---|
928 |
|
---|
929 | /* store in system pool
|
---|
930 | */
|
---|
931 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
932 | for (i = 0; i < KEY_BYT; ++i)
|
---|
933 | skey->poolv[i] = keybuf[i];
|
---|
934 | skey->poolc = KEY_BYT;
|
---|
935 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
936 | memset (buffer, '\0', BUF_ENT+2);
|
---|
937 | memset (keybuf, '\0', KEY_BYT);
|
---|
938 | SH_FREE(buffer);
|
---|
939 | }
|
---|
940 | else
|
---|
941 | {
|
---|
942 | SH_FREE(buffer);
|
---|
943 | SL_RETURN((-1), _("sh_entropy"));
|
---|
944 | }
|
---|
945 |
|
---|
946 | /* give out nbytes Bytes from the entropy pool
|
---|
947 | */
|
---|
948 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
949 | for (i = 0; i < nbytes; ++i)
|
---|
950 | {
|
---|
951 | nbuf[i] = skey->poolv[i];
|
---|
952 | --skey->poolc;
|
---|
953 | }
|
---|
954 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
955 |
|
---|
956 | SL_RETURN(0, _("sh_entropy"));
|
---|
957 | }
|
---|
958 |
|
---|
959 | /* HAVE_UNIX_RANDOM */
|
---|
960 | #endif
|
---|
961 |
|
---|
962 |
|
---|
963 |
|
---|
964 |
|
---|
965 |
|
---|
966 |
|
---|
967 |
|
---|