source: trunk/src/sh_entropy.c@ 150

Last change on this file since 150 was 147, checked in by katerina, 17 years ago

Fix regression in the seeding routine of the PRNG

File size: 22.1 KB
Line 
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
78static int
79do_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
97static int
98do_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
117int 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
283int 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 */
318int 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
420static
421char * 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
433typedef struct {
434 char * command;
435 char * arg;
436 int pipeFD;
437 pid_t pid;
438 int isset;
439 FILE * pipe;
440} sourcetable_t;
441
442static
443sourcetable_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
507static 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 freopen (_("/dev/null"), "r+", stderr);
621
622 /* exec the program */
623 retry_aud_execve (FIL__, __LINE__, _("/bin/sh"), arg, envp);
624
625 /* failed
626 */
627 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
628 }
629
630 /* parent
631 */
632 if (envp[0] != NULL)
633 free(envp[0]);
634
635 close (pipedes[STDOUT_FILENO]);
636 retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
637
638 outf = fdopen (pipedes[STDIN_FILENO], "r");
639
640 if (outf == NULL)
641 {
642 aud_kill (FIL__, __LINE__, source->pid, SIGKILL);
643 close (pipedes[STDOUT_FILENO]);
644 waitpid (source->pid, NULL, 0);
645 source->pid = 0;
646 SL_RETURN(NULL, _("sh_popen"));
647 }
648
649 SL_RETURN(outf, _("sh_popen"));
650}
651
652
653static int sh_pclose (sourcetable_t *source)
654{
655 int status = 0;
656 int retval;
657 char msg[128];
658 char errbuf[SH_ERRBUF_SIZE];
659
660 SL_ENTER(_("sh_pclose"));
661
662 retval = fclose(source->pipe);
663 if (retval)
664 {
665 sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, retval,
666 MSG_E_SUBGEN,
667 sh_error_message(retval, errbuf, sizeof(errbuf)),
668 _("sh_pclose"));
669 SL_RETURN((-1), _("sh_pclose"));
670 }
671
672 retval = waitpid(source->pid, &status, 0);
673 if (retval != source->pid)
674 {
675 sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, retval,
676 MSG_E_SUBGEN,
677 sh_error_message(retval, errbuf, sizeof(errbuf)),
678 _("sh_pclose"));
679
680 status = -1;
681 }
682 else if (WIFSIGNALED(status))
683 {
684 sl_snprintf(msg, sizeof(msg), _("Subprocess terminated by signal %d"),
685 WTERMSIG(status));
686 sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, retval,
687 MSG_E_SUBGEN,
688 msg,
689 _("sh_pclose"));
690 status = -1;
691 }
692
693 source->pipe = NULL;
694 source->pid = 0;
695 SL_RETURN(status, _("sh_pclose"));
696}
697
698#define BUF_ENT 32766
699
700/* Poll the system for randomness, mix results with
701 * previous reads using a hash function, and give out
702 * nbytes bytes from the result.
703 */
704int sh_entropy(int nbytes, char * nbuf)
705{
706 int caperr;
707 char combuf[80];
708 char * buffer;
709 int i, j, icount;
710 int bufcount = 0;
711 int count;
712
713 char * keybuf;
714 UINT32 kbuf[KEY_BYT/sizeof(UINT32)];
715 char addbuf[2 * KEY_BYT];
716
717 struct timeval tv;
718 fd_set fds;
719 unsigned long select_now = 0;
720 int maxFD = 0;
721 int imax, selcount;
722 char errbuf[SH_ERRBUF_SIZE];
723
724 sourcetable_t *source = NULL;
725
726 SL_ENTER(_("sh_entropy"));
727
728 ASSERT((nbytes <= KEY_BYT), _("nbytes <= KEY_BYT"))
729
730 if (nbytes > KEY_BYT)
731 nbytes = KEY_BYT;
732
733
734 /* --- If there is entropy in the pool, return it. ---
735 */
736 SH_MUTEX_LOCK_UNSAFE(mutex_skey);
737 if (skey->poolc >= nbytes)
738 {
739 j = KEY_BYT - skey->poolc;
740 for (i = 0; i < nbytes; ++i)
741 {
742 nbuf[i] = skey->poolv[i+j];
743 --skey->poolc;
744 }
745 SH_MUTEX_UNLOCK_UNSAFE(mutex_skey); /* alternative path */
746 SL_RETURN(0, _("sh_entropy"));
747 }
748 SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
749
750
751 FD_ZERO(&fds);
752
753 i = 0; icount = 0;
754 buffer = SH_ALLOC(BUF_ENT+2);
755
756 if (0 != (caperr = sl_get_cap_sub()))
757 {
758 sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
759 sh_error_message (caperr, errbuf, sizeof(errbuf)),
760 _("sl_get_cap_sub"));
761 }
762
763 while (source_template[i].command != NULL) {
764 ++i;
765 }
766 source = SH_ALLOC(i * sizeof(sourcetable_t));
767 for (j = 0; j < i;++j)
768 memcpy(&source[j], &source_template[j], sizeof(sourcetable_t));
769 i = 0;
770
771 while (source[i].command != NULL) {
772
773 j = 0;
774 while (com_path[j] != NULL)
775 {
776 sl_strlcpy(combuf, _(com_path[j]), 80);
777 sl_strlcat(combuf, _(source[i].command), 80);
778
779 /* flawfinder: ignore */
780 if ( access (combuf, X_OK) == 0)
781 {
782 sl_strlcpy(combuf, _(com_path[j]), 80);
783 sl_strlcat(combuf, _(source[i].arg), 80);
784 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENSTART,
785 combuf);
786 break;
787 }
788 ++j;
789 }
790
791 /* Not found, try next command.
792 */
793 if (com_path[j] == NULL)
794 {
795 ++i;
796 continue;
797 }
798
799 /* Source exists
800 */
801 source[i].pipe = sh_popen ( &source[i], combuf );
802 if (NULL != source[i].pipe)
803 {
804 source[i].pipeFD = fileno ( source[i].pipe );
805 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENEXEC,
806 combuf, (long) source[i].pipeFD);
807
808 maxFD = (source[i].pipeFD > maxFD) ? source[i].pipeFD : maxFD;
809 retry_fcntl( FIL__, __LINE__, source[i].pipeFD, F_SETFL, O_NONBLOCK);
810 FD_SET( source[i].pipeFD, &fds );
811 source[i].isset = 1;
812 ++icount;
813 }
814 else
815 {
816 sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_ENFAIL,
817 combuf);
818 }
819
820 ++i;
821 }
822
823 imax = i;
824 tv.tv_sec = 1;
825 tv.tv_usec = 0;
826 bufcount = 0;
827
828 while ( (icount > 0) && (bufcount < BUF_ENT) ) {
829
830 if ( (selcount = select (maxFD+1, &fds, NULL, NULL, &tv)) == -1)
831 break;
832
833 /* reset timeout for select()
834 */
835 tv.tv_sec = 1;
836 tv.tv_usec = 0;
837
838 /* timeout - let's not hang on forever
839 */
840 if (selcount == 0)
841 {
842 ++select_now;
843 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENTOUT,
844 (unsigned long) select_now);
845 if ( select_now > 9 )
846 break;
847 }
848
849 for (i = 0; i < imax; ++i) {
850
851 if ( FD_ISSET (source[i].pipeFD, &fds) ) {
852 count = fread (&buffer[bufcount],
853 1,
854 BUF_ENT-bufcount,
855 source[i].pipe );
856 if (count == 0)
857 {
858 if (0 != feof(source[i].pipe))
859 sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_ENCLOS,
860 (long) source[i].pipeFD);
861 else
862 sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_ENCLOS1,
863 (long) source[i].pipeFD);
864 source[i].isset = 0;
865 sh_pclose ( &source[i] );
866 --icount;
867 }
868 else
869 {
870 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENREAD,
871 (long) source[i].pipeFD, (long) count);
872 }
873 bufcount += count;
874
875 }
876 }
877
878 maxFD = 0;
879 FD_ZERO(&fds);
880
881 for (i = 0; i < imax; ++i)
882 {
883 if (source[i].isset == 1)
884 {
885 FD_SET( source[i].pipeFD, &fds );
886 maxFD = (source[i].pipeFD > maxFD) ? source[i].pipeFD : maxFD;
887 }
888 }
889 }
890
891 for (i = 0; i < imax; ++i)
892 {
893 if (source[i].isset == 1)
894 {
895 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENCLOS1,
896 (long) source[i].pipeFD);
897 sh_pclose ( &source[i] );
898 }
899 }
900 buffer[bufcount] = '\0';
901
902 SH_FREE(source);
903
904 if (0 != (caperr = sl_drop_cap_sub()))
905 {
906 sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
907 sh_error_message (caperr, errbuf, sizeof(errbuf)),
908 _("sl_drop_cap_sub"));
909 }
910
911 if (bufcount > 0)
912 {
913 keybuf = (char *) sh_tiger_hash_uint32 (buffer,
914 TIGER_DATA, sl_strlen(buffer),
915 kbuf, KEY_BYT/sizeof(UINT32));
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 kbuf, KEY_BYT/sizeof(UINT32));
928 memset(addbuf, '\0', sizeof(addbuf));
929
930 /* store in system pool
931 */
932 SH_MUTEX_LOCK_UNSAFE(mutex_skey);
933 for (i = 0; i < KEY_BYT; ++i)
934 skey->poolv[i] = keybuf[i];
935 skey->poolc = KEY_BYT;
936 SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
937 memset (buffer, '\0', BUF_ENT+2);
938 memset (keybuf, '\0', KEY_BYT);
939 SH_FREE(buffer);
940 }
941 else
942 {
943 SH_FREE(buffer);
944 SL_RETURN((-1), _("sh_entropy"));
945 }
946
947 /* give out nbytes Bytes from the entropy pool
948 */
949 SH_MUTEX_LOCK_UNSAFE(mutex_skey);
950 for (i = 0; i < nbytes; ++i)
951 {
952 nbuf[i] = skey->poolv[i];
953 --skey->poolc;
954 }
955 SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
956
957 SL_RETURN(0, _("sh_entropy"));
958}
959
960/* HAVE_UNIX_RANDOM */
961#endif
962
963#ifdef SH_CUTEST
964#include "CuTest.h"
965
966void Test_entropy (CuTest *tc)
967{
968 char bufx[9 * sizeof(UINT32) + 1];
969 char bufy[9 * sizeof(UINT32) + 1];
970 int status;
971
972 memset(skey->poolv, '\0', KEY_BYT);
973
974 status = sh_entropy (24, bufx);
975 CuAssertTrue(tc, 0 == status);
976
977 memset(skey->poolv, '\0', KEY_BYT);
978
979 status = sh_entropy (24, bufy);
980 CuAssertTrue(tc, 0 == status);
981
982 CuAssertTrue(tc, 0 != memcmp(bufx, bufy, 24));
983}
984#endif
985
986
987
988
989
990
991
992
Note: See TracBrowser for help on using the repository browser.