source: trunk/src/sh_entropy.c@ 30

Last change on this file since 30 was 30, checked in by rainer, 19 years ago

Release candidate 3 for version 2.2.0

File size: 21.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 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
280static jmp_buf entropy_timeout;
281
282static void sh_entropy_alarmhandle (int mysignal)
283{
284 (void) mysignal; /* avoid compiler warning */
285 longjmp (entropy_timeout, 1);
286}
287
288
289int 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 */
382int 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
478static
479char * com_path[] = {
480 N_("/usr/bin/xpg4/"),
481 N_("/usr/ucb/"),
482 N_("/bin/"),
483 N_("/sbin/"),
484 N_("/usr/bin/"),
485 N_("/usr/sbin/"),
486 N_("/usr/local/bin/"),
487 NULL
488};
489
490
491typedef struct {
492 char * command;
493 char * arg;
494 int pipeFD;
495 pid_t pid;
496 int isset;
497 FILE * pipe;
498} sourcetable_t;
499
500static
501sourcetable_t source[] = {
502 { N_("w"),
503 N_("w"),
504 0,
505 0,
506 0,
507 NULL },
508 { N_("netstat"),
509 N_("netstat -n"),
510 0,
511 0,
512 0,
513 NULL },
514 { N_("ps"),
515 N_("ps -ef"),
516 0,
517 0,
518 0,
519 NULL },
520 { N_("arp"),
521 N_("arp -a"),
522 0,
523 0,
524 0,
525 NULL },
526 { N_("free"),
527 N_("free"),
528 0,
529 0,
530 0,
531 NULL },
532 { N_("uptime"),
533 N_("uptime"),
534 0,
535 0,
536 0,
537 NULL },
538 { N_("procinfo"),
539 N_("procinfo -a"),
540 0,
541 0,
542 0,
543 NULL },
544 { N_("vmstat"),
545 N_("vmstat"),
546 0,
547 0,
548 0,
549 NULL },
550 { N_("w"), /* Play it again, Sam. */
551 N_("w"),
552 0,
553 0,
554 0,
555 NULL },
556 { NULL,
557 NULL,
558 0,
559 0,
560 0,
561 NULL }
562};
563
564
565static FILE * sh_popen (sourcetable_t *source, char * command)
566{
567 int i;
568 int pipedes[2];
569 FILE *outf = NULL;
570 struct passwd * tempres;
571 char * arg[4];
572 char * envp[2];
573 size_t len;
574
575 SL_ENTER(_("sh_popen"));
576
577 arg[0] = _("/bin/sh");
578 arg[1] = _("-c");
579 arg[2] = command;
580 arg[3] = NULL;
581
582 if (sh.timezone != NULL)
583 {
584 len = sl_strlen(sh.timezone) + 4;
585 envp[0] = malloc (len); /* free() ok */
586 if (envp[0] != NULL)
587 sl_snprintf (envp[0], len, "TZ=%s", sh.timezone);
588 else
589 envp[0] = NULL;
590 envp[1] = NULL;
591 }
592 else
593 {
594 envp[0] = NULL;
595 }
596
597
598 /* Create the pipe
599 */
600 if (aud_pipe(FIL__, __LINE__, pipedes) < 0) {
601 if (envp[0] != NULL) free(envp[0]);
602 SL_RETURN(NULL, _("sh_popen"));
603 }
604
605 source->pid = aud_fork(FIL__, __LINE__);
606
607 /* Failure
608 */
609 if (source->pid == (pid_t) - 1) {
610 close(pipedes[0]);
611 close(pipedes[1]);
612 if (envp[0] != NULL) free(envp[0]);
613 SL_RETURN(NULL, _("sh_popen"));
614 }
615
616 if (source->pid == (pid_t) 0)
617 {
618
619 /* child - make read side of the pipe stdout
620 */
621 if (retry_aud_dup2(FIL__, __LINE__,
622 pipedes[STDOUT_FILENO], STDOUT_FILENO) < 0)
623 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
624
625 /* close the pipe descriptors
626 */
627 close (pipedes[STDIN_FILENO]);
628 close (pipedes[STDOUT_FILENO]);
629
630 /* don't leak file descriptors
631 */
632 sh_unix_closeall (3, -1); /* in child process */
633
634 /* zero priv info
635 */
636 memset(skey, 0, sizeof(sh_key_t));
637
638 /* drop root privileges
639 */
640 i = 0;
641 if (0 == geteuid()) {
642 tempres = sh_getpwnam(DEFAULT_IDENT);
643 if (NULL != tempres) {
644 i = aud_setgid(FIL__, __LINE__, tempres->pw_gid);
645 if (i == 0)
646 i = sh_unix_initgroups(DEFAULT_IDENT ,tempres->pw_gid);
647 if (i == 0)
648 i = aud_setuid(FIL__, __LINE__, tempres->pw_uid);
649 /* make sure we cannot get root again
650 */
651 if ((tempres->pw_uid != 0) && (aud_setuid(FIL__, __LINE__, 0) >= 0))
652 i = -1;
653 } else {
654 i = -1;
655 }
656 }
657
658 /* some problem ...
659 */
660 if (i == -1) {
661 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
662 }
663
664 freopen (_("/dev/null"), "r+", stderr);
665
666 /* exec the program */
667 retry_aud_execve (FIL__, __LINE__, _("/bin/sh"), arg, envp);
668
669 /* failed
670 */
671 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
672 }
673
674 /* parent
675 */
676 if (envp[0] != NULL)
677 free(envp[0]);
678
679 close (pipedes[STDOUT_FILENO]);
680 retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
681
682 outf = fdopen (pipedes[STDIN_FILENO], "r");
683
684 if (outf == NULL)
685 {
686 aud_kill (FIL__, __LINE__, source->pid, SIGKILL);
687 close (pipedes[STDOUT_FILENO]);
688 waitpid (source->pid, NULL, 0);
689 source->pid = 0;
690 SL_RETURN(NULL, _("sh_popen"));
691 }
692
693 SL_RETURN(outf, _("sh_popen"));
694}
695
696
697static int sh_pclose (sourcetable_t *source)
698{
699 int status = 0;
700
701 SL_ENTER(_("sh_pclose"));
702
703 status = fclose(source->pipe);
704 if (status)
705 {
706 SL_RETURN((-1), _("sh_pclose"));
707 }
708
709 if (waitpid(source->pid, NULL, 0) != source->pid)
710 status = -1;
711
712 source->pipe = NULL;
713 source->pid = 0;
714 SL_RETURN(status, _("sh_pclose"));
715}
716
717#define BUF_ENT 32766
718
719/* Poll the system for randomness, mix results with
720 * previous reads using a hash function, and give out
721 * nbytes bytes from the result.
722 */
723int sh_entropy(int nbytes, char * nbuf)
724{
725 int caperr;
726 char combuf[80];
727 char * buffer;
728 int i, j, icount;
729 int bufcount = 0;
730 int count;
731
732 char * keybuf;
733 char addbuf[2 * KEY_BYT];
734
735 struct timeval tv;
736 fd_set fds;
737 unsigned long select_now = 0;
738 int maxFD = 0;
739 int imax, selcount;
740
741 SL_ENTER(_("sh_entropy"));
742
743 ASSERT((nbytes <= KEY_BYT), _("nbytes <= KEY_BYT"))
744
745 if (nbytes > KEY_BYT)
746 nbytes = KEY_BYT;
747
748
749 /* --- If there is entropy in the pool, return it. ---
750 */
751 if (skey->poolc >= nbytes)
752 {
753 j = KEY_BYT - skey->poolc;
754 for (i = 0; i < nbytes; ++i)
755 {
756 nbuf[i] = skey->poolv[i+j];
757 --skey->poolc;
758 }
759 SL_RETURN(0, _("sh_entropy"));
760 }
761
762
763 FD_ZERO(&fds);
764
765 i = 0; icount = 0;
766 buffer = SH_ALLOC(BUF_ENT+2);
767
768 if (0 != (caperr = sl_get_cap_sub()))
769 {
770 sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
771 sh_error_message (caperr),
772 _("sl_get_cap_sub"));
773 }
774
775 while (source[i].command != NULL) {
776
777 j = 0;
778 while (com_path[j] != NULL)
779 {
780 sl_strlcpy(combuf, _(com_path[j]), 80);
781 sl_strlcat(combuf, _(source[i].command), 80);
782
783 /* flawfinder: ignore */
784 if ( access (combuf, X_OK) == 0)
785 {
786 sl_strlcpy(combuf, _(com_path[j]), 80);
787 sl_strlcat(combuf, _(source[i].arg), 80);
788 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENSTART,
789 combuf);
790 break;
791 }
792 ++j;
793 }
794
795 /* Not found, try next command.
796 */
797 if (com_path[j] == NULL)
798 {
799 ++i;
800 continue;
801 }
802
803 /* Source exists
804 */
805 source[i].pipe = sh_popen ( &source[i], combuf );
806 if (NULL != source[i].pipe)
807 {
808 source[i].pipeFD = fileno ( source[i].pipe );
809 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENEXEC,
810 combuf, (long) source[i].pipeFD);
811
812 maxFD = (source[i].pipeFD > maxFD) ? source[i].pipeFD : maxFD;
813 retry_fcntl( FIL__, __LINE__, source[i].pipeFD, F_SETFL, O_NONBLOCK);
814 FD_SET( source[i].pipeFD, &fds );
815 source[i].isset = 1;
816 ++icount;
817 }
818 else
819 {
820 sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_ENFAIL,
821 combuf);
822 }
823
824 ++i;
825 }
826
827 imax = i;
828 tv.tv_sec = 1;
829 tv.tv_usec = 0;
830 bufcount = 0;
831
832 while ( (icount > 0) && (bufcount < BUF_ENT) ) {
833
834 if ( (selcount = select (maxFD+1, &fds, NULL, NULL, &tv)) == -1)
835 break;
836
837 /* reset timeout for select()
838 */
839 tv.tv_sec = 1;
840 tv.tv_usec = 0;
841
842 /* timeout - let's not hang on forever
843 */
844 if (selcount == 0)
845 {
846 ++select_now;
847 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENTOUT,
848 (unsigned long) select_now);
849 if ( select_now > 9 )
850 break;
851 }
852
853 for (i = 0; i < imax; ++i) {
854
855 if ( FD_ISSET (source[i].pipeFD, &fds) ) {
856 count = fread (&buffer[bufcount],
857 1,
858 BUF_ENT-bufcount,
859 source[i].pipe );
860 if (count == 0)
861 {
862 if (0 != feof(source[i].pipe))
863 sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_ENCLOS,
864 (long) source[i].pipeFD);
865 else
866 sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_ENCLOS1,
867 (long) source[i].pipeFD);
868 source[i].isset = 0;
869 sh_pclose ( &source[i] );
870 --icount;
871 }
872 else
873 {
874 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENREAD,
875 (long) source[i].pipeFD, (long) count);
876 }
877 bufcount += count;
878
879 }
880 }
881
882 maxFD = 0;
883 FD_ZERO(&fds);
884
885 for (i = 0; i < imax; ++i)
886 {
887 if (source[i].isset == 1)
888 {
889 FD_SET( source[i].pipeFD, &fds );
890 maxFD = (source[i].pipeFD > maxFD) ? source[i].pipeFD : maxFD;
891 }
892 }
893 }
894
895 for (i = 0; i < imax; ++i)
896 {
897 if (source[i].isset == 1)
898 {
899 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_ENCLOS1,
900 (long) source[i].pipeFD);
901 sh_pclose ( &source[i] );
902 }
903 }
904 buffer[bufcount] = '\0';
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),
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
918 /* add previous entropy into the new pool
919 */
920 memset(addbuf, '\0', sizeof(addbuf));
921 for (i = 0; i < KEY_BYT; ++i)
922 {
923 addbuf[i] = keybuf[i];
924 addbuf[i+KEY_BYT] = skey->poolv[i];
925 }
926 keybuf = (char *) sh_tiger_hash_uint32 (addbuf,
927 TIGER_DATA, sizeof(addbuf));
928 memset(addbuf, '\0', sizeof(addbuf));
929
930 /* store in system pool
931 */
932 for (i = 0; i < KEY_BYT; ++i)
933 skey->poolv[i] = keybuf[i];
934 skey->poolc = KEY_BYT;
935 memset (buffer, '\0', BUF_ENT+2);
936 memset (keybuf, '\0', KEY_BYT);
937 SH_FREE(buffer);
938 }
939 else
940 {
941 SH_FREE(buffer);
942 SL_RETURN((-1), _("sh_entropy"));
943 }
944
945 /* give out nbytes Bytes from the entropy pool
946 */
947 for (i = 0; i < nbytes; ++i)
948 {
949 nbuf[i] = skey->poolv[i];
950 --skey->poolc;
951 }
952
953 SL_RETURN(0, _("sh_entropy"));
954}
955
956/* HAVE_UNIX_RANDOM */
957#endif
958
959
960
961
962
963
964
Note: See TracBrowser for help on using the repository browser.