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 | memset (kbuf, '\0', sizeof(kbuf));
|
---|
390 |
|
---|
391 | SL_RETURN(0, _("sh_entropy"));
|
---|
392 | }
|
---|
393 | else
|
---|
394 | {
|
---|
395 | SL_RETURN((-1), _("sh_entropy"));
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 | /* HAVE_URANDOM */
|
---|
400 | #endif
|
---|
401 |
|
---|
402 | #ifdef HAVE_UNIX_RANDOM
|
---|
403 |
|
---|
404 | #ifndef FD_SET
|
---|
405 | #define NFDBITS 32
|
---|
406 | #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
|
---|
407 | #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
|
---|
408 | #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
|
---|
409 | #endif /* !FD_SET */
|
---|
410 | #ifndef FD_SETSIZE
|
---|
411 | #define FD_SETSIZE 32
|
---|
412 | #endif
|
---|
413 | #ifndef FD_ZERO
|
---|
414 | #define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
|
---|
415 | #endif
|
---|
416 |
|
---|
417 | #include "sh_static.h"
|
---|
418 | #include "sh_pthread.h"
|
---|
419 |
|
---|
420 | static
|
---|
421 | char * com_path[] = {
|
---|
422 | N_("/usr/bin/xpg4/"),
|
---|
423 | N_("/usr/ucb/"),
|
---|
424 | N_("/bin/"),
|
---|
425 | N_("/sbin/"),
|
---|
426 | N_("/usr/bin/"),
|
---|
427 | N_("/usr/sbin/"),
|
---|
428 | N_("/usr/local/bin/"),
|
---|
429 | NULL
|
---|
430 | };
|
---|
431 |
|
---|
432 |
|
---|
433 | typedef struct {
|
---|
434 | char * command;
|
---|
435 | char * arg;
|
---|
436 | int pipeFD;
|
---|
437 | pid_t pid;
|
---|
438 | int isset;
|
---|
439 | FILE * pipe;
|
---|
440 | } sourcetable_t;
|
---|
441 |
|
---|
442 | static
|
---|
443 | sourcetable_t source_template[] = {
|
---|
444 | { N_("w"),
|
---|
445 | N_("w"),
|
---|
446 | 0,
|
---|
447 | 0,
|
---|
448 | 0,
|
---|
449 | NULL },
|
---|
450 | { N_("netstat"),
|
---|
451 | N_("netstat -n"),
|
---|
452 | 0,
|
---|
453 | 0,
|
---|
454 | 0,
|
---|
455 | NULL },
|
---|
456 | { N_("ps"),
|
---|
457 | N_("ps -ef"),
|
---|
458 | 0,
|
---|
459 | 0,
|
---|
460 | 0,
|
---|
461 | NULL },
|
---|
462 | { N_("arp"),
|
---|
463 | N_("arp -a"),
|
---|
464 | 0,
|
---|
465 | 0,
|
---|
466 | 0,
|
---|
467 | NULL },
|
---|
468 | { N_("free"),
|
---|
469 | N_("free"),
|
---|
470 | 0,
|
---|
471 | 0,
|
---|
472 | 0,
|
---|
473 | NULL },
|
---|
474 | { N_("uptime"),
|
---|
475 | N_("uptime"),
|
---|
476 | 0,
|
---|
477 | 0,
|
---|
478 | 0,
|
---|
479 | NULL },
|
---|
480 | { N_("procinfo"),
|
---|
481 | N_("procinfo -a"),
|
---|
482 | 0,
|
---|
483 | 0,
|
---|
484 | 0,
|
---|
485 | NULL },
|
---|
486 | { N_("vmstat"),
|
---|
487 | N_("vmstat"),
|
---|
488 | 0,
|
---|
489 | 0,
|
---|
490 | 0,
|
---|
491 | NULL },
|
---|
492 | { N_("w"), /* Play it again, Sam. */
|
---|
493 | N_("w"),
|
---|
494 | 0,
|
---|
495 | 0,
|
---|
496 | 0,
|
---|
497 | NULL },
|
---|
498 | { NULL,
|
---|
499 | NULL,
|
---|
500 | 0,
|
---|
501 | 0,
|
---|
502 | 0,
|
---|
503 | NULL }
|
---|
504 | };
|
---|
505 |
|
---|
506 |
|
---|
507 | static FILE * sh_popen (sourcetable_t *source, char * command)
|
---|
508 | {
|
---|
509 | int i;
|
---|
510 | int pipedes[2];
|
---|
511 | FILE *outf = NULL;
|
---|
512 | char * arg[4];
|
---|
513 | char * envp[2];
|
---|
514 | size_t len;
|
---|
515 | char arg0[80];
|
---|
516 | char arg1[80];
|
---|
517 |
|
---|
518 | SL_ENTER(_("sh_popen"));
|
---|
519 |
|
---|
520 | strncpy (arg0, _("/bin/sh"), sizeof(arg0));
|
---|
521 | arg[0] = arg0;
|
---|
522 | strncpy (arg1, _("-c"), sizeof(arg1));
|
---|
523 | arg[1] = arg1;
|
---|
524 | arg[2] = command;
|
---|
525 | arg[3] = NULL;
|
---|
526 |
|
---|
527 | if (sh.timezone != NULL)
|
---|
528 | {
|
---|
529 | len = sl_strlen(sh.timezone) + 4;
|
---|
530 | envp[0] = malloc (len); /* free() ok */
|
---|
531 | if (envp[0] != NULL)
|
---|
532 | sl_snprintf (envp[0], len, "TZ=%s", sh.timezone);
|
---|
533 | else
|
---|
534 | envp[0] = NULL;
|
---|
535 | envp[1] = NULL;
|
---|
536 | }
|
---|
537 | else
|
---|
538 | {
|
---|
539 | envp[0] = NULL;
|
---|
540 | }
|
---|
541 |
|
---|
542 |
|
---|
543 | /* Create the pipe
|
---|
544 | */
|
---|
545 | if (aud_pipe(FIL__, __LINE__, pipedes) < 0) {
|
---|
546 | if (envp[0] != NULL) free(envp[0]);
|
---|
547 | SL_RETURN(NULL, _("sh_popen"));
|
---|
548 | }
|
---|
549 |
|
---|
550 | fflush (NULL);
|
---|
551 |
|
---|
552 | source->pid = aud_fork(FIL__, __LINE__);
|
---|
553 |
|
---|
554 | /* Failure
|
---|
555 | */
|
---|
556 | if (source->pid == (pid_t) - 1) {
|
---|
557 | close(pipedes[0]);
|
---|
558 | close(pipedes[1]);
|
---|
559 | if (envp[0] != NULL) free(envp[0]);
|
---|
560 | SL_RETURN(NULL, _("sh_popen"));
|
---|
561 | }
|
---|
562 |
|
---|
563 | if (source->pid == (pid_t) 0)
|
---|
564 | {
|
---|
565 |
|
---|
566 | /* child - make read side of the pipe stdout
|
---|
567 | */
|
---|
568 | if (retry_aud_dup2(FIL__, __LINE__,
|
---|
569 | pipedes[STDOUT_FILENO], STDOUT_FILENO) < 0)
|
---|
570 | aud__exit(FIL__, __LINE__, EXIT_FAILURE);
|
---|
571 |
|
---|
572 | /* close the pipe descriptors
|
---|
573 | */
|
---|
574 | close (pipedes[STDIN_FILENO]);
|
---|
575 | close (pipedes[STDOUT_FILENO]);
|
---|
576 |
|
---|
577 | /* don't leak file descriptors
|
---|
578 | */
|
---|
579 | sh_unix_closeall (3, -1); /* in child process */
|
---|
580 |
|
---|
581 | /* zero priv info
|
---|
582 | */
|
---|
583 | memset(skey, 0, sizeof(sh_key_t));
|
---|
584 |
|
---|
585 | /* drop root privileges
|
---|
586 | */
|
---|
587 | i = 0;
|
---|
588 | if (0 == geteuid())
|
---|
589 | {
|
---|
590 | #if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
|
---|
591 | struct passwd pwd;
|
---|
592 | char buffer[SH_PWBUF_SIZE];
|
---|
593 | struct passwd * tempres;
|
---|
594 | sh_getpwnam_r(DEFAULT_IDENT, &pwd, buffer, sizeof(buffer), &tempres);
|
---|
595 | #else
|
---|
596 | struct passwd * tempres = sh_getpwnam(DEFAULT_IDENT);
|
---|
597 | #endif
|
---|
598 |
|
---|
599 | if (NULL != tempres) {
|
---|
600 | i = aud_setgid(FIL__, __LINE__, tempres->pw_gid);
|
---|
601 | if (i == 0)
|
---|
602 | i = sh_unix_initgroups(DEFAULT_IDENT ,tempres->pw_gid);
|
---|
603 | if (i == 0)
|
---|
604 | i = aud_setuid(FIL__, __LINE__, tempres->pw_uid);
|
---|
605 | /* make sure we cannot get root again
|
---|
606 | */
|
---|
607 | if ((tempres->pw_uid != 0) && (aud_setuid(FIL__, __LINE__, 0) >= 0))
|
---|
608 | i = -1;
|
---|
609 | } else {
|
---|
610 | i = -1;
|
---|
611 | }
|
---|
612 | }
|
---|
613 |
|
---|
614 | /* some problem ...
|
---|
615 | */
|
---|
616 | if (i == -1) {
|
---|
617 | aud__exit(FIL__, __LINE__, EXIT_FAILURE);
|
---|
618 | }
|
---|
619 |
|
---|
620 | if (NULL != freopen (_("/dev/null"), "r+", stderr))
|
---|
621 | {
|
---|
622 |
|
---|
623 | /* exec the program */
|
---|
624 | retry_aud_execve (FIL__, __LINE__, _("/bin/sh"), arg, envp);
|
---|
625 | }
|
---|
626 |
|
---|
627 | /* failed
|
---|
628 | */
|
---|
629 | aud__exit(FIL__, __LINE__, EXIT_FAILURE);
|
---|
630 | }
|
---|
631 |
|
---|
632 | /* parent
|
---|
633 | */
|
---|
634 | if (envp[0] != NULL)
|
---|
635 | free(envp[0]);
|
---|
636 |
|
---|
637 | close (pipedes[STDOUT_FILENO]);
|
---|
638 | retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
|
---|
639 |
|
---|
640 | outf = fdopen (pipedes[STDIN_FILENO], "r");
|
---|
641 |
|
---|
642 | if (outf == NULL)
|
---|
643 | {
|
---|
644 | aud_kill (FIL__, __LINE__, source->pid, SIGKILL);
|
---|
645 | close (pipedes[STDOUT_FILENO]);
|
---|
646 | waitpid (source->pid, NULL, 0);
|
---|
647 | source->pid = 0;
|
---|
648 | SL_RETURN(NULL, _("sh_popen"));
|
---|
649 | }
|
---|
650 |
|
---|
651 | SL_RETURN(outf, _("sh_popen"));
|
---|
652 | }
|
---|
653 |
|
---|
654 |
|
---|
655 | static int sh_pclose (sourcetable_t *source)
|
---|
656 | {
|
---|
657 | int status = 0;
|
---|
658 | int retval;
|
---|
659 | char msg[128];
|
---|
660 | char errbuf[SH_ERRBUF_SIZE];
|
---|
661 |
|
---|
662 | SL_ENTER(_("sh_pclose"));
|
---|
663 |
|
---|
664 | retval = fclose(source->pipe);
|
---|
665 | if (retval)
|
---|
666 | {
|
---|
667 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, retval,
|
---|
668 | MSG_E_SUBGEN,
|
---|
669 | sh_error_message(retval, errbuf, sizeof(errbuf)),
|
---|
670 | _("sh_pclose"));
|
---|
671 | SL_RETURN((-1), _("sh_pclose"));
|
---|
672 | }
|
---|
673 |
|
---|
674 | retval = waitpid(source->pid, &status, 0);
|
---|
675 | if (retval != source->pid)
|
---|
676 | {
|
---|
677 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, retval,
|
---|
678 | MSG_E_SUBGEN,
|
---|
679 | sh_error_message(retval, errbuf, sizeof(errbuf)),
|
---|
680 | _("sh_pclose"));
|
---|
681 |
|
---|
682 | status = -1;
|
---|
683 | }
|
---|
684 | else if (WIFSIGNALED(status))
|
---|
685 | {
|
---|
686 | sl_snprintf(msg, sizeof(msg), _("Subprocess terminated by signal %d"),
|
---|
687 | WTERMSIG(status));
|
---|
688 | sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, retval,
|
---|
689 | MSG_E_SUBGEN,
|
---|
690 | msg,
|
---|
691 | _("sh_pclose"));
|
---|
692 | status = -1;
|
---|
693 | }
|
---|
694 |
|
---|
695 | source->pipe = NULL;
|
---|
696 | source->pid = 0;
|
---|
697 | SL_RETURN(status, _("sh_pclose"));
|
---|
698 | }
|
---|
699 |
|
---|
700 | #define BUF_ENT 32766
|
---|
701 |
|
---|
702 | /* Poll the system for randomness, mix results with
|
---|
703 | * previous reads using a hash function, and give out
|
---|
704 | * nbytes bytes from the result.
|
---|
705 | */
|
---|
706 | int sh_entropy(int nbytes, char * nbuf)
|
---|
707 | {
|
---|
708 | int caperr;
|
---|
709 | char combuf[80];
|
---|
710 | char * buffer;
|
---|
711 | int i, j, icount;
|
---|
712 | int bufcount = 0;
|
---|
713 | int count;
|
---|
714 |
|
---|
715 | char * keybuf;
|
---|
716 | UINT32 kbuf[KEY_BYT/sizeof(UINT32)];
|
---|
717 | char addbuf[2 * KEY_BYT];
|
---|
718 |
|
---|
719 | struct timeval tv;
|
---|
720 | fd_set fds;
|
---|
721 | unsigned long select_now = 0;
|
---|
722 | int maxFD = 0;
|
---|
723 | int imax, selcount;
|
---|
724 | char errbuf[SH_ERRBUF_SIZE];
|
---|
725 |
|
---|
726 | sourcetable_t *source = NULL;
|
---|
727 |
|
---|
728 | SL_ENTER(_("sh_entropy"));
|
---|
729 |
|
---|
730 | ASSERT((nbytes <= KEY_BYT), _("nbytes <= KEY_BYT"))
|
---|
731 |
|
---|
732 | if (nbytes > KEY_BYT)
|
---|
733 | nbytes = KEY_BYT;
|
---|
734 |
|
---|
735 |
|
---|
736 | /* --- If there is entropy in the pool, return it. ---
|
---|
737 | */
|
---|
738 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
739 | if (skey->poolc >= nbytes)
|
---|
740 | {
|
---|
741 | j = KEY_BYT - skey->poolc;
|
---|
742 | for (i = 0; i < nbytes; ++i)
|
---|
743 | {
|
---|
744 | nbuf[i] = skey->poolv[i+j];
|
---|
745 | --skey->poolc;
|
---|
746 | }
|
---|
747 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey); /* alternative path */
|
---|
748 | SL_RETURN(0, _("sh_entropy"));
|
---|
749 | }
|
---|
750 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
751 |
|
---|
752 |
|
---|
753 | FD_ZERO(&fds);
|
---|
754 |
|
---|
755 | i = 0; icount = 0;
|
---|
756 | buffer = SH_ALLOC(BUF_ENT+2);
|
---|
757 |
|
---|
758 | if (0 != (caperr = sl_get_cap_sub()))
|
---|
759 | {
|
---|
760 | sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
|
---|
761 | sh_error_message (caperr, errbuf, sizeof(errbuf)),
|
---|
762 | _("sl_get_cap_sub"));
|
---|
763 | }
|
---|
764 |
|
---|
765 | while (source_template[i].command != NULL) {
|
---|
766 | ++i;
|
---|
767 | }
|
---|
768 | source = SH_ALLOC(i * sizeof(sourcetable_t));
|
---|
769 | for (j = 0; j < i;++j)
|
---|
770 | memcpy(&source[j], &source_template[j], sizeof(sourcetable_t));
|
---|
771 | i = 0;
|
---|
772 |
|
---|
773 | while (source_template[i].command != NULL) {
|
---|
774 |
|
---|
775 | j = 0;
|
---|
776 | while (com_path[j] != NULL)
|
---|
777 | {
|
---|
778 | sl_strlcpy(combuf, _(com_path[j]), 80);
|
---|
779 | sl_strlcat(combuf, _(source[i].command), 80);
|
---|
780 |
|
---|
781 | /* flawfinder: ignore */
|
---|
782 | if ( access (combuf, X_OK) == 0)
|
---|
783 | {
|
---|
784 | sl_strlcpy(combuf, _(com_path[j]), 80);
|
---|
785 | sl_strlcat(combuf, _(source[i].arg), 80);
|
---|
786 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENSTART,
|
---|
787 | combuf);
|
---|
788 | break;
|
---|
789 | }
|
---|
790 | ++j;
|
---|
791 | }
|
---|
792 |
|
---|
793 | /* Not found, try next command.
|
---|
794 | */
|
---|
795 | if (com_path[j] == NULL)
|
---|
796 | {
|
---|
797 | ++i;
|
---|
798 | continue;
|
---|
799 | }
|
---|
800 |
|
---|
801 | /* Source exists
|
---|
802 | */
|
---|
803 | source[i].pipe = sh_popen ( &source[i], combuf );
|
---|
804 | if (NULL != source[i].pipe)
|
---|
805 | {
|
---|
806 | source[i].pipeFD = fileno ( source[i].pipe );
|
---|
807 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENEXEC,
|
---|
808 | combuf, (long) source[i].pipeFD);
|
---|
809 |
|
---|
810 | maxFD = (source[i].pipeFD > maxFD) ? source[i].pipeFD : maxFD;
|
---|
811 | retry_fcntl( FIL__, __LINE__, source[i].pipeFD, F_SETFL, O_NONBLOCK);
|
---|
812 | FD_SET( source[i].pipeFD, &fds );
|
---|
813 | source[i].isset = 1;
|
---|
814 | ++icount;
|
---|
815 | }
|
---|
816 | else
|
---|
817 | {
|
---|
818 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_ENFAIL,
|
---|
819 | combuf);
|
---|
820 | }
|
---|
821 |
|
---|
822 | ++i;
|
---|
823 | }
|
---|
824 |
|
---|
825 | imax = i;
|
---|
826 | tv.tv_sec = 1;
|
---|
827 | tv.tv_usec = 0;
|
---|
828 | bufcount = 0;
|
---|
829 |
|
---|
830 | while ( (icount > 0) && (bufcount < BUF_ENT) ) {
|
---|
831 |
|
---|
832 | if ( (selcount = select (maxFD+1, &fds, NULL, NULL, &tv)) == -1)
|
---|
833 | break;
|
---|
834 |
|
---|
835 | /* reset timeout for select()
|
---|
836 | */
|
---|
837 | tv.tv_sec = 1;
|
---|
838 | tv.tv_usec = 0;
|
---|
839 |
|
---|
840 | /* timeout - let's not hang on forever
|
---|
841 | */
|
---|
842 | if (selcount == 0)
|
---|
843 | {
|
---|
844 | ++select_now;
|
---|
845 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENTOUT,
|
---|
846 | (unsigned long) select_now);
|
---|
847 | if ( select_now > 9 )
|
---|
848 | break;
|
---|
849 | }
|
---|
850 |
|
---|
851 | for (i = 0; i < imax; ++i) {
|
---|
852 |
|
---|
853 | if ( FD_ISSET (source[i].pipeFD, &fds) ) {
|
---|
854 | count = fread (&buffer[bufcount],
|
---|
855 | 1,
|
---|
856 | BUF_ENT-bufcount,
|
---|
857 | source[i].pipe );
|
---|
858 | if (count == 0)
|
---|
859 | {
|
---|
860 | if (0 != feof(source[i].pipe))
|
---|
861 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_ENCLOS,
|
---|
862 | (long) source[i].pipeFD);
|
---|
863 | else
|
---|
864 | sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_ENCLOS1,
|
---|
865 | (long) source[i].pipeFD);
|
---|
866 | source[i].isset = 0;
|
---|
867 | sh_pclose ( &source[i] );
|
---|
868 | --icount;
|
---|
869 | }
|
---|
870 | else
|
---|
871 | {
|
---|
872 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENREAD,
|
---|
873 | (long) source[i].pipeFD, (long) count);
|
---|
874 | }
|
---|
875 | bufcount += count;
|
---|
876 |
|
---|
877 | }
|
---|
878 | }
|
---|
879 |
|
---|
880 | maxFD = 0;
|
---|
881 | FD_ZERO(&fds);
|
---|
882 |
|
---|
883 | for (i = 0; i < imax; ++i)
|
---|
884 | {
|
---|
885 | if (source[i].isset == 1)
|
---|
886 | {
|
---|
887 | FD_SET( source[i].pipeFD, &fds );
|
---|
888 | maxFD = (source[i].pipeFD > maxFD) ? source[i].pipeFD : maxFD;
|
---|
889 | }
|
---|
890 | }
|
---|
891 | }
|
---|
892 |
|
---|
893 | for (i = 0; i < imax; ++i)
|
---|
894 | {
|
---|
895 | if (source[i].isset == 1)
|
---|
896 | {
|
---|
897 | sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENCLOS1,
|
---|
898 | (long) source[i].pipeFD);
|
---|
899 | sh_pclose ( &source[i] );
|
---|
900 | }
|
---|
901 | }
|
---|
902 | buffer[bufcount] = '\0';
|
---|
903 |
|
---|
904 | SH_FREE(source);
|
---|
905 |
|
---|
906 | if (0 != (caperr = sl_drop_cap_sub()))
|
---|
907 | {
|
---|
908 | sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
|
---|
909 | sh_error_message (caperr, errbuf, sizeof(errbuf)),
|
---|
910 | _("sl_drop_cap_sub"));
|
---|
911 | }
|
---|
912 |
|
---|
913 | if (bufcount > 0)
|
---|
914 | {
|
---|
915 | keybuf = (char *) sh_tiger_hash_uint32 (buffer,
|
---|
916 | TIGER_DATA, sl_strlen(buffer),
|
---|
917 | kbuf, KEY_BYT/sizeof(UINT32));
|
---|
918 |
|
---|
919 | /* add previous entropy into the new pool
|
---|
920 | */
|
---|
921 | memset(addbuf, '\0', sizeof(addbuf));
|
---|
922 | for (i = 0; i < KEY_BYT; ++i)
|
---|
923 | {
|
---|
924 | addbuf[i] = keybuf[i];
|
---|
925 | addbuf[i+KEY_BYT] = skey->poolv[i];
|
---|
926 | }
|
---|
927 | keybuf = (char *) sh_tiger_hash_uint32 (addbuf,
|
---|
928 | TIGER_DATA, sizeof(addbuf),
|
---|
929 | kbuf, KEY_BYT/sizeof(UINT32));
|
---|
930 | memset(addbuf, '\0', sizeof(addbuf));
|
---|
931 |
|
---|
932 | /* store in system pool
|
---|
933 | */
|
---|
934 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
935 | for (i = 0; i < KEY_BYT; ++i)
|
---|
936 | skey->poolv[i] = keybuf[i];
|
---|
937 | skey->poolc = KEY_BYT;
|
---|
938 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
939 | memset (buffer, '\0', BUF_ENT+2);
|
---|
940 | memset (keybuf, '\0', KEY_BYT);
|
---|
941 | SH_FREE(buffer);
|
---|
942 | }
|
---|
943 | else
|
---|
944 | {
|
---|
945 | SH_FREE(buffer);
|
---|
946 | SL_RETURN((-1), _("sh_entropy"));
|
---|
947 | }
|
---|
948 |
|
---|
949 | /* give out nbytes Bytes from the entropy pool
|
---|
950 | */
|
---|
951 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
952 | for (i = 0; i < nbytes; ++i)
|
---|
953 | {
|
---|
954 | nbuf[i] = skey->poolv[i];
|
---|
955 | --skey->poolc;
|
---|
956 | }
|
---|
957 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
958 |
|
---|
959 | SL_RETURN(0, _("sh_entropy"));
|
---|
960 | }
|
---|
961 |
|
---|
962 | /* HAVE_UNIX_RANDOM */
|
---|
963 | #endif
|
---|
964 |
|
---|
965 | #ifdef SH_CUTEST
|
---|
966 | #include "CuTest.h"
|
---|
967 |
|
---|
968 | void Test_entropy (CuTest *tc)
|
---|
969 | {
|
---|
970 | char bufx[9 * sizeof(UINT32) + 1];
|
---|
971 | char bufy[9 * sizeof(UINT32) + 1];
|
---|
972 | int status;
|
---|
973 |
|
---|
974 | memset(skey->poolv, '\0', KEY_BYT);
|
---|
975 |
|
---|
976 | status = sh_entropy (24, bufx);
|
---|
977 | CuAssertTrue(tc, 0 == status);
|
---|
978 |
|
---|
979 | memset(skey->poolv, '\0', KEY_BYT);
|
---|
980 |
|
---|
981 | status = sh_entropy (24, bufy);
|
---|
982 | CuAssertTrue(tc, 0 == status);
|
---|
983 |
|
---|
984 | CuAssertTrue(tc, 0 != memcmp(bufx, bufy, 24));
|
---|
985 | }
|
---|
986 | #endif
|
---|
987 |
|
---|
988 |
|
---|
989 |
|
---|
990 |
|
---|
991 |
|
---|
992 |
|
---|
993 |
|
---|
994 |
|
---|