source: trunk/src/sh_entropy.c@ 6

Last change on this file since 6 was 1, checked in by katerina, 19 years ago

Initial import

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 strcpy( addr.sun_path, name ); /* known to fit */
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/ucb/"),
481 N_("/bin/"),
482 N_("/sbin/"),
483 N_("/usr/bin/"),
484 N_("/usr/sbin/"),
485 N_("/usr/local/bin/"),
486 NULL
487};
488
489
490typedef struct {
491 char * command;
492 char * arg;
493 int pipeFD;
494 pid_t pid;
495 int isset;
496 FILE * pipe;
497} sourcetable_t;
498
499static
500sourcetable_t source[] = {
501 { N_("w"),
502 N_("w"),
503 0,
504 0,
505 0,
506 NULL },
507 { N_("netstat"),
508 N_("netstat -n"),
509 0,
510 0,
511 0,
512 NULL },
513 { N_("ps"),
514 N_("ps -ef"),
515 0,
516 0,
517 0,
518 NULL },
519 { N_("arp"),
520 N_("arp -a"),
521 0,
522 0,
523 0,
524 NULL },
525 { N_("free"),
526 N_("free"),
527 0,
528 0,
529 0,
530 NULL },
531 { N_("uptime"),
532 N_("uptime"),
533 0,
534 0,
535 0,
536 NULL },
537 { N_("procinfo"),
538 N_("procinfo -a"),
539 0,
540 0,
541 0,
542 NULL },
543 { N_("vmstat"),
544 N_("vmstat"),
545 0,
546 0,
547 0,
548 NULL },
549 { N_("w"), /* Play it again, Sam. */
550 N_("w"),
551 0,
552 0,
553 0,
554 NULL },
555 { NULL,
556 NULL,
557 0,
558 0,
559 0,
560 NULL }
561};
562
563
564static FILE * sh_popen (sourcetable_t *source, char * command)
565{
566 int i;
567 int pipedes[2];
568 FILE *outf = NULL;
569 struct passwd * tempres;
570 char * arg[4];
571 char * envp[2];
572
573 SL_ENTER(_("sh_popen"));
574
575 arg[0] = _("/bin/sh");
576 arg[1] = _("-c");
577 arg[2] = command;
578 arg[3] = NULL;
579
580 if (sh.timezone != NULL)
581 {
582 envp[0] = malloc (sl_strlen(sh.timezone) + 4); /* free() ok */
583 if (envp[0] != NULL)
584 sprintf (envp[0], "TZ=%s", sh.timezone); /* known to fit */
585 else
586 envp[0] = NULL;
587 envp[1] = NULL;
588 }
589 else
590 {
591 envp[0] = NULL;
592 }
593
594
595 /* Create the pipe
596 */
597 if (aud_pipe(FIL__, __LINE__, pipedes) < 0) {
598 if (envp[0] != NULL) free(envp[0]);
599 SL_RETURN(NULL, _("sh_popen"));
600 }
601
602 source->pid = aud_fork(FIL__, __LINE__);
603
604 /* Failure
605 */
606 if (source->pid == (pid_t) - 1) {
607 close(pipedes[0]);
608 close(pipedes[1]);
609 if (envp[0] != NULL) free(envp[0]);
610 SL_RETURN(NULL, _("sh_popen"));
611 }
612
613 if (source->pid == (pid_t) 0)
614 {
615
616 /* child - make read side of the pipe stdout
617 */
618 if (retry_aud_dup2(FIL__, __LINE__,
619 pipedes[STDOUT_FILENO], STDOUT_FILENO) < 0)
620 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
621
622 /* close the pipe descriptors
623 */
624 close (pipedes[STDIN_FILENO]);
625 close (pipedes[STDOUT_FILENO]);
626
627 /* don't leak file descriptors
628 */
629 sh_unix_closeall (3, -1); /* in child process */
630
631 /* zero priv info
632 */
633 memset(skey, 0, sizeof(sh_key_t));
634
635 /* drop root privileges
636 */
637 i = 0;
638 if (0 == geteuid()) {
639 tempres = sh_getpwnam(DEFAULT_IDENT);
640 if (NULL != tempres) {
641 i = aud_setgid(FIL__, __LINE__, tempres->pw_gid);
642 if (i == 0)
643 i = sh_unix_initgroups(DEFAULT_IDENT ,tempres->pw_gid);
644 if (i == 0)
645 i = aud_setuid(FIL__, __LINE__, tempres->pw_uid);
646 /* make sure we cannot get root again
647 */
648 if ((tempres->pw_uid != 0) && (aud_setuid(FIL__, __LINE__, 0) >= 0))
649 i = -1;
650 } else {
651 i = -1;
652 }
653 }
654
655 /* some problem ...
656 */
657 if (i == -1) {
658 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
659 }
660
661 freopen (_("/dev/null"), "r+", stderr);
662
663 /* exec the program */
664 retry_aud_execve (FIL__, __LINE__, _("/bin/sh"), arg, envp);
665
666 /* failed
667 */
668 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
669 }
670
671 /* parent
672 */
673 if (envp[0] != NULL)
674 free(envp[0]);
675
676 close (pipedes[STDOUT_FILENO]);
677 retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
678
679 outf = fdopen (pipedes[STDIN_FILENO], "r");
680
681 if (outf == NULL)
682 {
683 aud_kill (FIL__, __LINE__, source->pid, SIGKILL);
684 close (pipedes[STDOUT_FILENO]);
685 waitpid (source->pid, NULL, 0);
686 source->pid = 0;
687 SL_RETURN(NULL, _("sh_popen"));
688 }
689
690 SL_RETURN(outf, _("sh_popen"));
691}
692
693
694static int sh_pclose (sourcetable_t *source)
695{
696 int status = 0;
697
698 SL_ENTER(_("sh_pclose"));
699
700 status = fclose(source->pipe);
701 if (status)
702 {
703 SL_RETURN((-1), _("sh_pclose"));
704 }
705
706 if (waitpid(source->pid, NULL, 0) != source->pid)
707 status = -1;
708
709 source->pipe = NULL;
710 source->pid = 0;
711 SL_RETURN(status, _("sh_pclose"));
712}
713
714#define BUF_ENT 32766
715
716/* Poll the system for randomness, mix results with
717 * previous reads using a hash function, and give out
718 * nbytes bytes from the result.
719 */
720int sh_entropy(int nbytes, char * nbuf)
721{
722 int caperr;
723 char combuf[80];
724 char * buffer;
725 int i, j, icount;
726 int bufcount = 0;
727 int count;
728
729 char * keybuf;
730 char addbuf[2 * KEY_BYT];
731
732 struct timeval tv;
733 fd_set fds;
734 unsigned long select_now = 0;
735 int maxFD = 0;
736 int imax, selcount;
737
738 SL_ENTER(_("sh_entropy"));
739
740 ASSERT((nbytes <= KEY_BYT), _("nbytes <= KEY_BYT"))
741
742 if (nbytes > KEY_BYT)
743 nbytes = KEY_BYT;
744
745
746 /* --- If there is entropy in the pool, return it. ---
747 */
748 if (skey->poolc >= nbytes)
749 {
750 j = KEY_BYT - skey->poolc;
751 for (i = 0; i < nbytes; ++i)
752 {
753 nbuf[i] = skey->poolv[i+j];
754 --skey->poolc;
755 }
756 SL_RETURN(0, _("sh_entropy"));
757 }
758
759
760 FD_ZERO(&fds);
761
762 i = 0; icount = 0;
763 buffer = SH_ALLOC(BUF_ENT+2);
764
765 if (0 != (caperr = sl_get_cap_sub()))
766 {
767 sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
768 sh_error_message (caperr),
769 _("sl_get_cap_sub"));
770 }
771
772 while (source[i].command != NULL) {
773
774 j = 0;
775 while (com_path[j] != NULL)
776 {
777 sl_strlcpy(combuf, _(com_path[j]), 80);
778 sl_strlcat(combuf, _(source[i].command), 80);
779
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 if (0 != (caperr = sl_drop_cap_sub()))
903 {
904 sh_error_handle((-1), FIL__, __LINE__, caperr, MSG_E_SUBGEN,
905 sh_error_message (caperr),
906 _("sl_drop_cap_sub"));
907 }
908
909 if (bufcount > 0)
910 {
911 keybuf = (char *) sh_tiger_hash_uint32 (buffer,
912 TIGER_DATA, sl_strlen(buffer));
913
914 /* add previous entropy into the new pool
915 */
916 memset(addbuf, '\0', sizeof(addbuf));
917 for (i = 0; i < KEY_BYT; ++i)
918 {
919 addbuf[i] = keybuf[i];
920 addbuf[i+KEY_BYT] = skey->poolv[i];
921 }
922 keybuf = (char *) sh_tiger_hash_uint32 (addbuf,
923 TIGER_DATA, sizeof(addbuf));
924 memset(addbuf, '\0', sizeof(addbuf));
925
926 /* store in system pool
927 */
928 for (i = 0; i < KEY_BYT; ++i)
929 skey->poolv[i] = keybuf[i];
930 skey->poolc = KEY_BYT;
931 memset (buffer, '\0', BUF_ENT+2);
932 memset (keybuf, '\0', KEY_BYT);
933 SH_FREE(buffer);
934 }
935 else
936 {
937 SH_FREE(buffer);
938 SL_RETURN((-1), _("sh_entropy"));
939 }
940
941 /* give out nbytes Bytes from the entropy pool
942 */
943 for (i = 0; i < nbytes; ++i)
944 {
945 nbuf[i] = skey->poolv[i];
946 --skey->poolc;
947 }
948
949 SL_RETURN(0, _("sh_entropy"));
950}
951
952/* HAVE_UNIX_RANDOM */
953#endif
954
955
956
957
958
959
960
Note: See TracBrowser for help on using the repository browser.