source: trunk/src/sh_unix.c@ 493

Last change on this file since 493 was 493, checked in by katerina, 9 years ago

Fix for ticket #391 (timezone offset calculation).

File size: 132.9 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 1999 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#include "config_xor.h"
21
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <ctype.h>
27#ifdef HAVE_LINUX_FS_H
28#include <linux/fs.h>
29#endif
30
31#ifdef HAVE_MEMORY_H
32#include <memory.h>
33#endif
34
35#ifdef HAVE_UNISTD_H
36#include <errno.h>
37#include <signal.h>
38#include <pwd.h>
39#include <grp.h>
40#include <sys/types.h>
41#include <sys/stat.h>
42#include <sys/resource.h>
43#include <fcntl.h>
44#include <unistd.h>
45/* need to undef these, since the #define's may be picked up from
46 * linux/wait.h, and will clash with a typedef in sys/wait.h
47 */
48#undef P_ALL
49#undef P_PID
50#undef P_PGID
51#include <sys/wait.h>
52
53/*********************
54#ifdef HAVE_SYS_VFS_H
55#include <sys/vfs.h>
56#endif
57**********************/
58#endif
59
60#if TIME_WITH_SYS_TIME
61#include <sys/time.h>
62#include <time.h>
63#else
64#if HAVE_SYS_TIME_H
65#include <sys/time.h>
66#else
67#include <time.h>
68#endif
69#endif
70
71#ifdef HAVE_SYS_SELECT_H
72#include <sys/select.h>
73#endif
74
75#ifndef FD_SET
76#define NFDBITS 32
77#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
78#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
79#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
80#endif /* !FD_SET */
81#ifndef FD_SETSIZE
82#define FD_SETSIZE 32
83#endif
84#ifndef FD_ZERO
85#define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
86#endif
87
88
89#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
90#include <sys/mman.h>
91#endif
92
93#include "samhain.h"
94#include "sh_error.h"
95#include "sh_unix.h"
96#include "sh_utils.h"
97#include "sh_mem.h"
98#include "sh_hash.h"
99#include "sh_tools.h"
100#include "sh_restrict.h"
101#include "sh_ipvx.h"
102#include "sh_tiger.h"
103#include "sh_prelink.h"
104#include "sh_pthread.h"
105#include "sh_sem.h"
106
107/* moved here from far below
108 */
109#include <netdb.h>
110
111#define SH_NEED_PWD_GRP
112#define SH_NEED_GETHOSTBYXXX
113#include "sh_static.h"
114
115#ifndef HAVE_LSTAT
116#define lstat stat
117#endif
118
119#if defined(S_IFLNK) && !defined(S_ISLNK)
120#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
121#else
122#if !defined(S_ISLNK)
123#define S_ISLNK(mode) (0)
124#endif
125#endif
126
127#if defined(S_IFSOCK) && !defined(S_ISSOCK)
128#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
129#else
130#if !defined(S_ISSOCK)
131#define S_ISSOCK(mode) (0)
132#endif
133#endif
134
135#if defined(S_IFDOOR) && !defined(S_ISDOOR)
136#define S_ISDOOR(mode) (((mode) & S_IFMT) == S_IFDOOR)
137#else
138#if !defined(S_ISDOOR)
139#define S_ISDOOR(mode) (0)
140#endif
141#endif
142
143#if defined(S_IFPORT) && !defined(S_ISPORT)
144#define S_ISPORT(mode) (((mode) & S_IFMT) == S_IFPORT)
145#else
146#if !defined(S_ISPORT)
147#define S_ISPORT(mode) (0)
148#endif
149#endif
150
151#define SH_KEY_NULL _("000000000000000000000000000000000000000000000000")
152
153#undef FIL__
154#define FIL__ _("sh_unix.c")
155
156unsigned long mask_PRELINK = MASK_PRELINK_;
157unsigned long mask_USER0 = MASK_USER_;
158unsigned long mask_USER1 = MASK_USER_;
159unsigned long mask_USER2 = MASK_USER_;
160unsigned long mask_USER3 = MASK_USER_;
161unsigned long mask_USER4 = MASK_USER_;
162unsigned long mask_ALLIGNORE = MASK_ALLIGNORE_;
163unsigned long mask_ATTRIBUTES = MASK_ATTRIBUTES_;
164unsigned long mask_LOGFILES = MASK_LOGFILES_;
165unsigned long mask_LOGGROW = MASK_LOGGROW_;
166unsigned long mask_READONLY = MASK_READONLY_;
167unsigned long mask_NOIGNORE = MASK_NOIGNORE_;
168
169
170extern char **environ;
171
172int sh_unix_maskreset()
173{
174 mask_PRELINK = MASK_PRELINK_;
175 mask_USER0 = MASK_USER_;
176 mask_USER1 = MASK_USER_;
177 mask_USER2 = MASK_USER_;
178 mask_USER3 = MASK_USER_;
179 mask_USER4 = MASK_USER_;
180 mask_ALLIGNORE = MASK_ALLIGNORE_;
181 mask_ATTRIBUTES = MASK_ATTRIBUTES_;
182 mask_LOGFILES = MASK_LOGFILES_;
183 mask_LOGGROW = MASK_LOGGROW_;
184 mask_READONLY = MASK_READONLY_;
185 mask_NOIGNORE = MASK_NOIGNORE_;
186 return 0;
187}
188
189
190#ifdef SYS_SIGLIST_DECLARED
191/* extern const char * const sys_siglist[]; */
192#else
193char * sh_unix_siglist (int signum)
194{
195 switch (signum)
196 {
197#ifdef SIGHUP
198 case SIGHUP:
199 return _("Hangup");
200#endif
201#ifdef SIGINT
202 case SIGINT:
203 return _("Interrupt");
204#endif
205#ifdef SIGQUIT
206 case SIGQUIT:
207 return _("Quit");
208#endif
209#ifdef SIGILL
210 case SIGILL:
211 return _("Illegal instruction");
212#endif
213#ifdef SIGTRAP
214 case SIGTRAP:
215 return _("Trace/breakpoint trap");
216#endif
217#ifdef SIGABRT
218 case SIGABRT:
219 return _("IOT trap/Abort");
220#endif
221#ifdef SIGBUS
222 case SIGBUS:
223 return _("Bus error");
224#endif
225#ifdef SIGFPE
226 case SIGFPE:
227 return _("Floating point exception");
228#endif
229#ifdef SIGUSR1
230 case SIGUSR1:
231 return _("User defined signal 1");
232#endif
233#ifdef SIGSEGV
234 case SIGSEGV:
235 return _("Segmentation fault");
236#endif
237#ifdef SIGUSR2
238 case SIGUSR2:
239 return _("User defined signal 2");
240#endif
241#ifdef SIGPIPE
242 case SIGPIPE:
243 return _("Broken pipe");
244#endif
245#ifdef SIGALRM
246 case SIGALRM:
247 return _("Alarm clock");
248#endif
249#ifdef SIGTERM
250 case SIGTERM:
251 return _("Terminated");
252#endif
253#ifdef SIGSTKFLT
254 case SIGSTKFLT:
255 return _("Stack fault");
256#endif
257#ifdef SIGCHLD
258 case SIGCHLD:
259 return _("Child exited");
260#endif
261#ifdef SIGCONT
262 case SIGCONT:
263 return _("Continued");
264#endif
265#ifdef SIGSTOP
266 case SIGSTOP:
267 return _("Stopped");
268#endif
269#ifdef SIGTSTP
270 case SIGTSTP:
271 return _("Stop typed at tty");
272#endif
273#ifdef SIGTTIN
274 case SIGTTIN:
275 return _("Stopped (tty input)");
276#endif
277#ifdef SIGTTOU
278 case SIGTTOU:
279 return _("Stopped (tty output)");
280#endif
281#ifdef SIGURG
282 case SIGURG:
283 return _("Urgent condition");
284#endif
285#ifdef SIGXCPU
286 case SIGXCPU:
287 return _("CPU time limit exceeded");
288#endif
289#ifdef SIGXFSZ
290 case SIGXFSZ:
291 return _("File size limit exceeded");
292#endif
293#ifdef SIGVTALRM
294 case SIGVTALRM:
295 return _("Virtual time alarm");
296#endif
297#ifdef SIGPROF
298 case SIGPROF:
299 return _("Profile signal");
300#endif
301#ifdef SIGWINCH
302 case SIGWINCH:
303 return _("Window size changed");
304#endif
305#ifdef SIGIO
306 case SIGIO:
307 return _("Possible I/O");
308#endif
309#ifdef SIGPWR
310 case SIGPWR:
311 return _("Power failure");
312#endif
313#ifdef SIGUNUSED
314 case SIGUNUSED:
315 return _("Unused signal");
316#endif
317 }
318 return _("Unknown");
319}
320#endif
321
322
323/* Log from within a signal handler without using any
324 * functions that are not async signal safe.
325 *
326 * This is the safe_itoa helper function.
327 */
328char * safe_itoa(int i, char * str, int size)
329{
330 unsigned int u;
331 int iisneg = 0;
332 char *p = &str[size-1];
333
334 *p = '\0';
335 if (i < 0) {
336 iisneg = 1;
337 u = ((unsigned int)(-(1+i))) + 1;
338 } else {
339 u = i;
340 }
341 do {
342 --p;
343 *p = '0' + (u % 10);
344 u /= 10;
345 } while (u && (p != str));
346 if ((iisneg == 1) && (p != str)) {
347 --p;
348 *p = '-';
349 }
350 return p;
351}
352
353/* Log from within a signal handler without using any
354 * functions that are not async signal safe.
355 *
356 * This is the safe_logger function.
357 * Arguments: signal (signal number), method (0=logger, 1=stderr), thepid (pid)
358 */
359extern int OnlyStderr;
360
361int safe_logger (int thesignal, int method, char * details)
362{
363 unsigned int i = 0;
364 int status = -1;
365 struct stat buf;
366 pid_t newpid;
367 char str[128];
368 char * p;
369
370 char l0[64], l1[64], l2[64], l3[64];
371 char a0[32];
372 char e0[128];
373 char msg[128];
374
375 char * locations[] = { NULL, NULL, NULL, NULL, NULL };
376 char * envp[] = { NULL, NULL };
377 char * argp[] = { NULL, NULL, NULL };
378
379 pid_t thepid = getpid();
380
381 if ((sh.flag.isdaemon == S_FALSE) || (OnlyStderr == S_TRUE))
382 method = 1;
383
384 /* seems that solaris cc needs this way of initializing ...
385 */
386 locations[0] = l0;
387 locations[1] = l1;
388 locations[2] = l2;
389 locations[3] = l3;
390
391 envp[0] = e0;
392 argp[0] = a0;
393
394 sl_strlcpy(msg, _("samhain["), 128);
395 p = safe_itoa((int) thepid, str, 128);
396 if (p && *p)
397 sl_strlcat(msg, p, 128);
398 if (thesignal == 0)
399 {
400 if (details == NULL) {
401 sl_strlcat(msg, _("]: out of memory"), 128);
402 } else {
403 sl_strlcat(msg, _("]: "), 128);
404 sl_strlcat(msg, details, 128);
405 }
406 }
407 else
408 {
409 sl_strlcat(msg, _("]: exit on signal "), 128);
410 p = safe_itoa(thesignal, str, 128);
411 if (p && *p)
412 sl_strlcat(msg, p, 128);
413 }
414
415 if (method == 1) {
416#ifndef STDERR_FILENO
417#define STDERR_FILENO 2
418#endif
419 int retval = 0;
420 do {
421 retval = write(STDERR_FILENO, msg, strlen(msg));
422 } while (retval < 0 && errno == EINTR);
423 do {
424 retval = write(STDERR_FILENO, "\n", 1);
425 } while (retval < 0 && errno == EINTR);
426 return 0;
427 }
428
429 sl_strlcpy (l0, _("/usr/bin/logger"), 64);
430 sl_strlcpy (l1, _("/usr/sbin/logger"), 64);
431 sl_strlcpy (l2, _("/usr/ucb/logger"), 64);
432 sl_strlcpy (l3, _("/bin/logger"), 64);
433
434 sl_strlcpy (a0, _("logger"), 32);
435 sl_strlcpy (e0,
436 _("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/ucb:/usr/local/bin"),
437 128);
438
439 while (locations[i] != NULL) {
440 status = stat(locations[i], &buf);
441 if (status == 0)
442 break;
443 ++i;
444 }
445
446 if (locations[i] != NULL) {
447 argp[1] = msg;
448 newpid = fork();
449 if (newpid == 0) {
450 execve(locations[i], argp, envp);
451 _exit(1);
452 }
453 else if (newpid > 0) {
454 waitpid(newpid, &status, WUNTRACED);
455 }
456 }
457 return 0;
458}
459
460void safe_fatal (const char * details,
461 const char * file, int line)
462{
463 char msg[128];
464 char str[128];
465 char * p;
466 int thesignal = 0;
467 int method = 0;
468
469 p = safe_itoa((int) line, str, 128);
470 sl_strlcpy(msg, _("FATAL: "), 128);
471 sl_strlcat(msg, file, 128);
472 sl_strlcat(msg, ": ", 128);
473 if (p && (*p)) {
474 sl_strlcat(msg, p , 128);
475 sl_strlcat(msg, ": ", 128);
476 }
477 sl_strlcat(msg, details, 128);
478 (void) safe_logger (thesignal, method, msg);
479
480 close_ipc ();
481 raise(SIGKILL);
482}
483
484extern char sh_sig_msg[64];
485
486volatile int immediate_exit_normal = 0;
487
488#if defined(SA_SIGACTION_WORKS)
489static
490void sh_unix_sigexit (int mysignal, siginfo_t * signal_info, void * signal_add)
491#else
492static
493void sh_unix_sigexit (int mysignal)
494#endif
495{
496
497#if defined(SA_SIGACTION_WORKS)
498 if (signal_info != NULL && signal_info->si_code == SI_USER &&
499 mysignal != SIGTERM && mysignal != SIGINT)
500 {
501 return;
502 }
503
504 /* avoid compiler warning (unused var)
505 */
506 (void) signal_add;
507#endif
508
509 /*
510 * Block re-entry
511 */
512 if (immediate_exit_normal > 0)
513 {
514 ++immediate_exit_normal;
515 if ((skey != NULL) && (immediate_exit_normal == 2))
516 memset (skey, '\0', sizeof(sh_key_t));
517 if (immediate_exit_normal == 2)
518 {
519 int val_return;
520
521 do {
522 val_return = chdir ("/");
523 } while (val_return < 0 && errno == EINTR);
524
525 close_ipc ();
526 safe_logger (mysignal, 0, NULL);
527 }
528 raise(SIGKILL);
529 }
530 else
531 {
532 immediate_exit_normal = 1;
533 }
534
535#ifdef SYS_SIGLIST_DECLARED
536 strncpy (sh_sig_msg, sys_siglist[mysignal], 40);
537#else
538 strncpy (sh_sig_msg, sh_unix_siglist(mysignal), 40);
539#endif
540 sh_sig_msg[63] = '\0';
541
542 ++sig_raised;
543 ++sig_urgent;
544 sig_termfast = 1;
545 return;
546}
547
548volatile int immediate_exit_fast = 0;
549
550#if defined(SA_SIGACTION_WORKS)
551static
552void sh_unix_sigexit_fast (int mysignal, siginfo_t * signal_info,
553 void * signal_add)
554#else
555static
556void sh_unix_sigexit_fast (int mysignal)
557#endif
558{
559#if defined(SL_DEBUG) && (defined(USE_SYSTEM_MALLOC) || !defined(USE_MALLOC_LOCK))
560 int retval;
561#endif
562
563#if defined(SA_SIGACTION_WORKS)
564 if (signal_info != NULL && signal_info->si_code == SI_USER)
565 {
566 return;
567 }
568#endif
569
570 /* avoid compiler warning (unused var)
571 */
572#if defined(SA_SIGACTION_WORKS)
573 (void) signal_add;
574#endif
575
576 /* Check whether the heap is ok; otherwise _exit
577 */
578#if !defined(SL_DEBUG) || (!defined(USE_SYSTEM_MALLOC) && defined(USE_MALLOC_LOCK))
579 ++immediate_exit_fast;
580 if (skey != NULL && immediate_exit_fast < 2)
581 memset (skey, '\0', sizeof(sh_key_t));
582 if (immediate_exit_fast < 2)
583 safe_logger (mysignal, 0, NULL);
584 raise(SIGKILL);
585#else
586
587 /* debug code
588 */
589 if (immediate_exit_fast == 1)
590 {
591 ++immediate_exit_fast;
592 if (skey != NULL)
593 memset (skey, '\0', sizeof(sh_key_t));
594 close_ipc ();
595 safe_logger (mysignal, 0, NULL);
596 do {
597 retval = chdir ("/");
598 } while (retval < 0 && errno == EINTR);
599 raise(SIGFPE);
600 }
601 else if (immediate_exit_fast == 2)
602 {
603 do {
604 retval = chdir ("/");
605 } while (retval < 0 && errno == EINTR);
606 raise(SIGFPE);
607 }
608 else if (immediate_exit_fast != 0)
609 {
610 raise(SIGKILL);
611 }
612
613 ++immediate_exit_fast;
614
615 /* The FPE|BUS|SEGV|ILL signals leave the system in an undefined
616 * state, thus it is best to exit immediately.
617 */
618#ifdef SYS_SIGLIST_DECLARED
619 strncpy (sh_sig_msg, sys_siglist[mysignal], 40);
620#else
621 strncpy (sh_sig_msg, sh_unix_siglist(mysignal), 40);
622#endif
623 sh_sig_msg[63] = '\0';
624
625 sl_stack_print();
626
627 /* Try to push out an error message.
628 */
629 sh_error_handle ((-1), FIL__, __LINE__, mysignal, MSG_EXIT_NORMAL,
630 sh.prg_name, sh_sig_msg);
631
632 if (skey != NULL)
633 memset (skey, '\0', sizeof(sh_key_t));
634 close_ipc ();
635
636 do {
637 retval = chdir ("/");
638 } while (retval < 0 && errno == EINTR);
639
640 raise(SIGFPE);
641#endif
642}
643
644
645static
646void sh_unix_sigaction (int mysignal)
647{
648 ++sig_raised;
649#ifdef SIGUSR1
650 if (mysignal == SIGUSR1)
651 sig_debug_switch = 1;
652#endif
653#ifdef SIGUSR2
654 if (mysignal == SIGUSR2)
655 {
656 ++sig_suspend_switch;
657 ++sig_urgent;
658 }
659#endif
660#ifdef SIGHUP
661 if (mysignal == SIGHUP)
662 sig_config_read_again = 1;
663#endif
664#ifdef SIGTTOU
665 if (mysignal == SIGTTOU) {
666 sig_force_check = 1; sh_sem_trylock(); }
667#endif
668#ifdef SIGTSTP
669 if (mysignal == SIGTSTP) {
670 sig_force_check = 1; sig_force_silent = 1; sh_sem_trylock(); }
671#endif
672#ifdef SIGTTIN
673 if (mysignal == SIGTTIN)
674 sig_fresh_trail = 1;
675#endif
676#ifdef SIGABRT
677 if (mysignal == SIGABRT)
678 sig_fresh_trail = 1;
679#endif
680#ifdef SIGQUIT
681 if (mysignal == SIGQUIT)
682 sig_terminate = 1;
683#endif
684#ifdef SIGTERM
685 if (mysignal == SIGTERM)
686 {
687 strncpy (sh_sig_msg, _("Terminated"), 40);
688 sig_termfast = 1;
689 ++sig_urgent;
690 }
691#endif
692
693 return;
694}
695
696void sh_unix_ign_sigpipe()
697{
698 struct sigaction ignact;
699
700 ignact.sa_handler = SIG_IGN; /* signal action */
701 sigemptyset( &ignact.sa_mask ); /* set an empty mask */
702 ignact.sa_flags = 0; /* init sa_flags */
703
704#ifdef SIGPIPE
705 retry_sigaction(FIL__, __LINE__, SIGPIPE, &ignact, NULL);
706#endif
707
708 return;
709}
710static
711void sh_unix_siginstall (int goDaemon)
712{
713 struct sigaction act, act_fast, act2, oldact, ignact;
714#if defined (SH_WITH_SERVER)
715 (void) goDaemon;
716#endif
717
718 SL_ENTER(_("sh_unix_siginstall"));
719
720 ignact.sa_handler = SIG_IGN; /* signal action */
721 sigemptyset( &ignact.sa_mask ); /* set an empty mask */
722 ignact.sa_flags = 0; /* init sa_flags */
723
724#if defined(SA_SIGACTION_WORKS)
725 act.sa_sigaction = &sh_unix_sigexit; /* signal action */
726#else
727 act.sa_handler = &sh_unix_sigexit; /* signal action */
728#endif
729
730 sigfillset ( &act.sa_mask ); /* set a full mask */
731
732
733 /* Block all but deadly signals.
734 */
735#ifdef SIGILL
736 sigdelset ( &act.sa_mask, SIGILL );
737#endif
738#ifndef SL_DEBUG
739#ifdef SIGFPE
740 sigdelset ( &act.sa_mask, SIGFPE );
741#endif
742#endif
743#ifdef SIGSEGV
744 sigdelset ( &act.sa_mask, SIGSEGV );
745#endif
746#ifdef SIGBUS
747 sigdelset ( &act.sa_mask, SIGBUS );
748#endif
749
750#if defined(SA_SIGACTION_WORKS)
751 act_fast.sa_sigaction = &sh_unix_sigexit_fast; /* signal action */
752#else
753 act_fast.sa_handler = &sh_unix_sigexit_fast; /* signal action */
754#endif
755
756 sigfillset ( &act_fast.sa_mask ); /* set a full mask */
757
758#ifdef SIGILL
759 sigdelset ( &act_fast.sa_mask, SIGILL );
760#endif
761#ifndef SL_DEBUG
762#ifdef SIGFPE
763 sigdelset ( &act_fast.sa_mask, SIGFPE );
764#endif
765#endif
766#ifdef SIGSEGV
767 sigdelset ( &act_fast.sa_mask, SIGSEGV );
768#endif
769#ifdef SIGBUS
770 sigdelset ( &act_fast.sa_mask, SIGBUS );
771#endif
772
773
774 /* Use siginfo to verify origin of signal, if possible.
775 */
776#if defined(SA_SIGACTION_WORKS)
777 act.sa_flags = SA_SIGINFO;
778 act_fast.sa_flags = SA_SIGINFO;
779#else
780 act.sa_flags = 0;
781 act_fast.sa_flags = 0;
782#endif
783
784 /* Do not block the signal from being received in its handler ...
785 * (is this a good or a bad idea ??).
786 */
787#if defined(SA_NOMASK)
788 act_fast.sa_flags |= SA_NOMASK;
789#elif defined(SA_NODEFER)
790 act_fast.sa_flags |= SA_NODEFER;
791#endif
792
793
794 act2.sa_handler = &sh_unix_sigaction; /* signal action */
795 sigemptyset( &act2.sa_mask ); /* set an empty mask */
796 act2.sa_flags = 0; /* init sa_flags */
797
798 /* signals to control the daemon */
799
800#ifdef SIGHUP
801 retry_sigaction(FIL__, __LINE__, SIGHUP, &act2, &oldact);
802#endif
803#ifdef SIGABRT
804 retry_sigaction(FIL__, __LINE__, SIGABRT, &act2, &oldact);
805#endif
806#ifdef SIGUSR1
807 retry_sigaction(FIL__, __LINE__, SIGUSR1, &act2, &oldact);
808#endif
809#ifdef SIGUSR2
810 retry_sigaction(FIL__, __LINE__, SIGUSR2, &act2, &oldact);
811#endif
812#ifdef SIGQUIT
813 retry_sigaction(FIL__, __LINE__, SIGQUIT, &act2, &oldact);
814#endif
815#ifdef SIGTERM
816 retry_sigaction(FIL__, __LINE__, SIGTERM, &act, &oldact);
817#endif
818
819 /* fatal signals that may cause termination */
820
821#ifdef SIGILL
822 retry_sigaction(FIL__, __LINE__, SIGILL, &act_fast, &oldact);
823#endif
824#ifndef SL_DEBUG
825#ifdef SIGFPE
826 retry_sigaction(FIL__, __LINE__, SIGFPE, &act_fast, &oldact);
827#endif
828#endif
829#ifdef SIGSEGV
830 retry_sigaction(FIL__, __LINE__, SIGSEGV, &act_fast, &oldact);
831#endif
832#ifdef SIGBUS
833 retry_sigaction(FIL__, __LINE__, SIGBUS, &act_fast, &oldact);
834#endif
835
836 /* other signals */
837
838#ifdef SIGINT
839 retry_sigaction(FIL__, __LINE__, SIGINT, &act, &oldact);
840#endif
841#ifdef SIGPIPE
842 retry_sigaction(FIL__, __LINE__, SIGPIPE, &ignact, &oldact);
843#endif
844#ifdef SIGALRM
845 retry_sigaction(FIL__, __LINE__, SIGALRM, &ignact, &oldact);
846#endif
847
848#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
849#ifdef SIGTTOU
850 if (goDaemon == 1)
851 retry_sigaction(FIL__, __LINE__, SIGTTOU, &act2, &oldact);
852 else
853 retry_sigaction(FIL__, __LINE__, SIGTTOU, &ignact, &oldact);
854#endif
855#ifdef SIGTSTP
856 if (goDaemon == 1)
857 retry_sigaction(FIL__, __LINE__, SIGTSTP, &act2, &oldact);
858 else
859 retry_sigaction(FIL__, __LINE__, SIGTSTP, &ignact, &oldact);
860#endif
861#ifdef SIGTTIN
862 if (goDaemon == 1)
863 retry_sigaction(FIL__, __LINE__, SIGTTIN, &act2, &oldact);
864 else
865 retry_sigaction(FIL__, __LINE__, SIGTTIN, &ignact, &oldact);
866#endif
867#else
868#ifdef SIGTSTP
869 retry_sigaction(FIL__, __LINE__, SIGTSTP, &ignact, &oldact);
870#endif
871#ifdef SIGTTOU
872 retry_sigaction(FIL__, __LINE__, SIGTTOU, &ignact, &oldact);
873#endif
874#ifdef SIGTTIN
875 retry_sigaction(FIL__, __LINE__, SIGTTIN, &ignact, &oldact);
876#endif
877#endif
878
879#ifdef SIGTRAP
880#if !defined(SCREW_IT_UP)
881 retry_sigaction(FIL__, __LINE__, SIGTRAP, &act, &oldact);
882#endif
883#endif
884
885#ifdef SIGPOLL
886 retry_sigaction(FIL__, __LINE__, SIGPOLL, &ignact, &oldact);
887#endif
888#if defined(SIGPROF) && !defined(SH_PROFILE)
889 retry_sigaction(FIL__, __LINE__, SIGPROF, &ignact, &oldact);
890#endif
891#ifdef SIGSYS
892 retry_sigaction(FIL__, __LINE__, SIGSYS, &act, &oldact);
893#endif
894#ifdef SIGURG
895 retry_sigaction(FIL__, __LINE__, SIGURG, &ignact, &oldact);
896#endif
897#if defined(SIGVTALRM) && !defined(SH_PROFILE)
898 retry_sigaction(FIL__, __LINE__, SIGVTALRM, &ignact, &oldact);
899#endif
900#ifdef SIGXCPU
901 retry_sigaction(FIL__, __LINE__, SIGXCPU, &act, &oldact);
902#endif
903#ifdef SIGXFSZ
904 retry_sigaction(FIL__, __LINE__, SIGXFSZ, &act, &oldact);
905#endif
906
907#ifdef SIGEMT
908 retry_sigaction(FIL__, __LINE__, SIGEMT, &ignact, &oldact);
909#endif
910#ifdef SIGSTKFLT
911 retry_sigaction(FIL__, __LINE__, SIGSTKFLT, &act, &oldact);
912#endif
913#ifdef SIGIO
914 retry_sigaction(FIL__, __LINE__, SIGIO, &ignact, &oldact);
915#endif
916#ifdef SIGPWR
917 retry_sigaction(FIL__, __LINE__, SIGPWR, &act, &oldact);
918#endif
919
920#ifdef SIGLOST
921 retry_sigaction(FIL__, __LINE__, SIGLOST, &ignact, &oldact);
922#endif
923#ifdef SIGUNUSED
924 retry_sigaction(FIL__, __LINE__, SIGUNUSED, &ignact, &oldact);
925#endif
926
927 SL_RET0(_("sh_unix_siginstall"));
928}
929
930/* ---------------------------------------------------------------- */
931
932/* checksum the own binary
933 */
934int sh_unix_self_hash (const char * c)
935{
936 char message[512];
937 char hashbuf[KEYBUF_SIZE];
938
939 SL_ENTER(_("sh_unix_self_hash"));
940
941 if (c == NULL)
942 {
943 sh.exec.path[0] = '\0';
944 SL_RETURN((0), _("sh_unix_self_hash"));
945 }
946 sl_strlcpy(sh.exec.path, c, SH_PATHBUF);
947
948 sl_strlcpy(sh.exec.hash,
949 sh_tiger_hash (c, TIGER_FILE, TIGER_NOLIM, hashbuf, sizeof(hashbuf)),
950 KEY_LEN+1);
951 sl_snprintf(message, 512, _("%s has checksum: %s"),
952 sh.exec.path, sh.exec.hash);
953 message[511] = '\0';
954 sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
955 message, _("sh_unix_self_hash"));
956 if (0 == sl_strcmp(sh.exec.hash, SH_KEY_NULL ))
957 {
958 dlog(1, FIL__, __LINE__,
959 _("Could not checksum my own executable because of the\nfollowing error: %s: %s\n\nPossible reasons include:\n Wrong path in configure file option SamhainPath=/path/to/executable\n No read permission for the effective UID: %d\n"),
960 sh.exec.path, sl_get_errmsg(), (int) sl_ret_euid());
961 sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_NOACCESS,
962 (long) sh.real.uid, c);
963 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
964 }
965 SL_RETURN((0), _("sh_unix_self_hash"));
966}
967
968int sh_unix_self_check ()
969{
970 char newhash[KEY_LEN+1];
971 char message[512];
972 char hashbuf[KEYBUF_SIZE];
973
974 SL_ENTER(_("sh_unix_self_check"));
975 if (sh.exec.path[0] == '\0')
976 SL_RETURN((0), _("sh_unix_self_check"));
977
978 sl_strlcpy(newhash,
979 sh_tiger_hash (sh.exec.path, TIGER_FILE, TIGER_NOLIM, hashbuf, sizeof(hashbuf)),
980 KEY_LEN+1);
981 if (0 == sl_strncmp(sh.exec.hash, newhash, KEY_LEN))
982 {
983 sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
984 _("Checksum ok"), _("sh_unix_self_check"));
985 SL_RETURN((0), _("sh_unix_self_check"));
986 }
987
988 dlog(1, FIL__, __LINE__,
989 _("The checksum of the executable: %s has changed since startup (%s -> %s).\n"),
990 sh.exec.path, sh.exec.hash, newhash);
991
992 sl_snprintf(message, 512,
993 _("The checksum of %s has changed since startup (%s -> %s)"),
994 sh.exec.path, sh.exec.hash, newhash);
995 message[511] = '\0';
996
997 sh_error_handle(SH_ERR_INFO, FIL__, __LINE__, 0, MSG_E_SUBGEN,
998 message, _("sh_unix_self_check"));
999 sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_E_AUTH,
1000 sh.exec.path);
1001 SL_RETURN((-1), _("sh_unix_self_check"));
1002}
1003
1004
1005/* ---------------------------------------------------------------- */
1006
1007long sh_group_to_gid (const char * g, int * fail)
1008{
1009 struct group * w;
1010 gid_t gid = 0;
1011 int status = 0;
1012
1013#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
1014 struct group grp;
1015 char * buffer;
1016 static size_t gbufsize = SH_GRBUF_SIZE;
1017#endif
1018
1019 *fail = -1;
1020
1021 if (g)
1022 {
1023 size_t i;
1024 size_t len = strlen(g);
1025
1026 *fail = 0;
1027
1028 for (i = 0; i < len; ++i)
1029 {
1030 char c = g[i];
1031
1032 if (!isdigit((int) c))
1033 goto is_a_name;
1034 }
1035 return atol(g);
1036
1037 is_a_name:
1038
1039#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
1040
1041 buffer = SH_ALLOC(gbufsize);
1042 status = sh_getgrnam_r(g, &grp, buffer, gbufsize, &w);
1043
1044 if ((status == ERANGE) && (w == NULL))
1045 {
1046 if (S_TRUE == sl_ok_adds( gbufsize, SH_GRBUF_SIZE ))
1047 {
1048 SH_FREE(buffer);
1049 gbufsize += SH_GRBUF_SIZE;
1050 goto is_a_name;
1051 }
1052 }
1053
1054#else
1055
1056 errno = 0;
1057 w = sh_getgrnam(g);
1058 status = errno;
1059
1060#endif
1061
1062 if ((status == ERANGE) && (w == NULL))
1063 {
1064 static int seen = 0;
1065
1066 if (seen == 0)
1067 {
1068 char errbuf[SH_ERRBUF_SIZE];
1069
1070 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
1071 sh_error_message(status, errbuf, sizeof(errbuf)),
1072 _("sh_group_to_gid"), (long) -1, _("line too long in group entry"));
1073 ++seen;
1074 }
1075 *fail = -1;
1076 }
1077 else if (w == NULL)
1078 {
1079 char * tmp = sh_util_strdup(g);
1080 sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
1081 _("sh_group_to_gid"), tmp);
1082 SH_FREE(tmp);
1083 *fail = -1;
1084 }
1085 else
1086 {
1087 gid = w->gr_gid;
1088 }
1089#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
1090 SH_FREE(buffer);
1091#endif
1092 }
1093
1094 return gid;
1095}
1096
1097/* ---------------------------------------------------------------- */
1098
1099
1100/* added Tue Feb 22 10:36:44 NFT 2000 Rainer Wichmann */
1101static int tf_add_trusted_user_int(const char * c)
1102{
1103 struct passwd * w;
1104 int count;
1105 uid_t pwid = (uid_t)-1;
1106
1107#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
1108 struct passwd pwd;
1109 char * buffer;
1110#endif
1111
1112 SL_ENTER(_("tf_add_trusted_user_int"));
1113
1114 /* First check for a user name.
1115 */
1116#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
1117 buffer = SH_ALLOC(SH_PWBUF_SIZE);
1118 sh_getpwnam_r(c, &pwd, buffer, SH_PWBUF_SIZE, &w);
1119#else
1120 w = sh_getpwnam(c);
1121#endif
1122
1123 if ((w != NULL) && ((pwid = w->pw_uid) > 0))
1124 goto succe;
1125
1126 /* Failed, so check for a numerical value.
1127 */
1128 pwid = strtol(c, (char **)NULL, 10);
1129 if (pwid > 0 && pwid < 65535)
1130 goto succe;
1131
1132 sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
1133 _("add trusted user"), c);
1134#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
1135 SH_FREE(buffer);
1136#endif
1137 SL_RETURN((-1), _("tf_add_trusted_user_int"));
1138
1139 succe:
1140 count = sl_trust_add_user(pwid);
1141#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
1142 SH_FREE(buffer);
1143#endif
1144 SL_RETURN((count), _("tf_add_trusted_user_int"));
1145}
1146
1147int tf_add_trusted_user(const char * c)
1148{
1149 int i;
1150 char * q;
1151 char * p = sh_util_strdup (c);
1152#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1153 char * saveptr;
1154#endif
1155
1156 SL_ENTER(_("tf_add_trusted_user"));
1157
1158#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1159 q = strtok_r(p, ", \t", &saveptr);
1160#else
1161 q = strtok(p, ", \t");
1162#endif
1163 if (!q)
1164 {
1165 SH_FREE(p);
1166 SL_RETURN((-1), _("tf_add_trusted_user"));
1167 }
1168 while (q)
1169 {
1170 i = tf_add_trusted_user_int(q);
1171 if (SL_ISERROR(i))
1172 {
1173 SH_FREE(p);
1174 SL_RETURN((i), _("tf_add_trusted_user"));
1175 }
1176#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1177 q = strtok_r(NULL, ", \t", &saveptr);
1178#else
1179 q = strtok(NULL, ", \t");
1180#endif
1181 }
1182 SH_FREE(p);
1183 SL_RETURN((0), _("tf_add_trusted_user"));
1184}
1185
1186extern uid_t sl_trust_baduid(void);
1187extern gid_t sl_trust_badgid(void);
1188
1189#if defined(HOST_IS_CYGWIN) || defined(__cygwin__) || defined(__CYGWIN32__) || defined(__CYGWIN__)
1190int tf_trust_check (const char * file, int mode)
1191{
1192 (void) file;
1193 (void) mode;
1194 return 0;
1195}
1196#else
1197int tf_trust_check (const char * file, int mode)
1198{
1199 char * tmp;
1200 char * tmp2;
1201 char * p;
1202 int status;
1203 int level;
1204 uid_t ff_euid = (uid_t) -1;
1205
1206 SL_ENTER(_("tf_trust_check"));
1207
1208 if (mode == SL_YESPRIV)
1209 sl_get_euid(&ff_euid);
1210 else
1211 sl_get_ruid(&ff_euid);
1212
1213#if defined(SH_WITH_SERVER)
1214 if (0 == sl_ret_euid()) /* privileges not dropped yet */
1215 {
1216#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
1217 struct passwd pwd;
1218 char * buffer = SH_ALLOC(SH_PWBUF_SIZE);
1219 struct passwd * tempres;
1220 sh_getpwnam_r(DEFAULT_IDENT, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
1221#else
1222 struct passwd * tempres = sh_getpwnam(DEFAULT_IDENT);
1223#endif
1224
1225 if (!tempres)
1226 {
1227 dlog(1, FIL__, __LINE__,
1228 _("User %s does not exist. Please add the user to your system.\n"),
1229 DEFAULT_IDENT);
1230 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
1231 }
1232 else
1233 {
1234 ff_euid = tempres->pw_uid;
1235 }
1236#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
1237 SH_FREE(buffer);
1238#endif
1239 }
1240#endif
1241
1242 status = sl_trustfile_euid(file, ff_euid);
1243
1244 if ( SL_ENONE != status)
1245 {
1246 if (status == SL_ESTAT)
1247 level = SH_ERR_ALL;
1248 else
1249 level = SH_ERR_ERR;
1250
1251 tmp = sh_util_safe_name (file);
1252 p = sl_trust_errfile();
1253 if (p && *p != '\0')
1254 {
1255 tmp2 = sh_util_safe_name (sl_trust_errfile());
1256 sh_error_handle(level, FIL__, __LINE__, status, MSG_E_TRUST2,
1257 sl_error_string(status), tmp, tmp2);
1258 SH_FREE(tmp2);
1259 }
1260 else
1261 {
1262 sh_error_handle(level, FIL__, __LINE__, status, MSG_E_TRUST1,
1263 sl_error_string(status), tmp);
1264 }
1265 SH_FREE(tmp);
1266
1267 if (status == SL_EBADUID || status == SL_EBADGID ||
1268 status == SL_EBADOTH || status == SL_ETRUNC ||
1269 status == SL_EINTERNAL )
1270 {
1271 switch (status) {
1272 case SL_EINTERNAL:
1273 dlog(1, FIL__, __LINE__,
1274 _("An internal error occured in the trustfile function.\n"));
1275 break;
1276 case SL_ETRUNC:
1277 tmp = sh_util_safe_name (file);
1278 dlog(1, FIL__, __LINE__,
1279 _("A filename truncation occured in the trustfile function.\nProbably the normalized filename for %s\nis too long. This may be due e.g. to deep or circular softlinks.\n"),
1280 tmp);
1281 SH_FREE(tmp);
1282 break;
1283 case SL_EBADOTH:
1284 tmp = sh_util_safe_name (file);
1285 p = sl_trust_errfile();
1286 dlog(1, FIL__, __LINE__,
1287 _("The path element: %s\nin the filename: %s is world writeable.\n"),
1288 p, tmp);
1289 SH_FREE(tmp);
1290 break;
1291 case SL_EBADUID:
1292 tmp = sh_util_safe_name (file);
1293 p = sl_trust_errfile();
1294 dlog(1, FIL__, __LINE__,
1295 _("The owner (UID = %ld) of the path element: %s\nin the filename: %s\nis not in the list of trusted users.\nTo fix the problem, you can:\n - run ./configure again with the option --with-trusted=0,...,UID\n where UID is the UID of the untrusted user, or\n - use the option TrustedUser=UID in the configuration file.\n"),
1296 (UID_CAST)sl_trust_baduid(), p, tmp);
1297 SH_FREE(tmp);
1298 break;
1299 case SL_EBADGID:
1300 tmp = sh_util_safe_name (file);
1301 p = sl_trust_errfile();
1302 dlog(1, FIL__, __LINE__,
1303 _("The path element: %s\nin the filename: %s\nis group writeable (GID = %ld), and at least one of the group\nmembers (UID = %ld) is not in the list of trusted users.\nTo fix the problem, you can:\n - run ./configure again with the option --with-trusted=0,...,UID\n where UID is the UID of the untrusted user, or\n - use the option TrustedUser=UID in the configuration file.\n"),
1304 p, tmp, (UID_CAST)sl_trust_badgid(),
1305 (UID_CAST)sl_trust_baduid());
1306 SH_FREE(tmp);
1307 break;
1308 default:
1309 break;
1310 }
1311
1312 SL_RETURN((-1), _("tf_trust_check"));
1313 }
1314 }
1315
1316 SL_RETURN((0), _("tf_trust_check"));
1317}
1318#endif
1319
1320#ifdef HAVE_INITGROUPS
1321#ifdef HOST_IS_OSF
1322int sh_unix_initgroups ( char * in_user, gid_t in_gid)
1323#else
1324int sh_unix_initgroups (const char * in_user, gid_t in_gid)
1325#endif
1326{
1327 int status = -1;
1328 status = sh_initgroups (in_user, in_gid);
1329 if (status < 0)
1330 {
1331 if (errno == EPERM)
1332 return 0;
1333 if (errno == EINVAL)
1334 return 0;
1335 return -1;
1336 }
1337 return 0;
1338}
1339#else
1340int sh_unix_initgroups (const char * in_user, gid_t in_gid)
1341{
1342 (void) in_user;
1343 (void) in_gid;
1344 return 0;
1345}
1346#endif
1347
1348#ifdef HAVE_INITGROUPS
1349char * sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len);
1350int sh_unix_initgroups2 (uid_t in_pid, gid_t in_gid)
1351{
1352 int status = -1;
1353 char user[SH_MINIBUF];
1354
1355 SL_ENTER(_("sh_unix_initgroups2"));
1356
1357 if (NULL == sh_unix_getUIDname (SH_ERR_ERR, in_pid, user, sizeof(user)))
1358 SL_RETURN((-1), _("sh_unix_initgroups2"));
1359 status = sh_initgroups (user, in_gid);
1360 if (status < 0)
1361 {
1362 if (errno == EPERM)
1363 status = 0;
1364 if (errno == EINVAL)
1365 status = 0;
1366 }
1367 SL_RETURN((status), _("sh_unix_initgroups2"));
1368}
1369#else
1370int sh_unix_initgroups2 (uid_t in_pid, gid_t in_gid)
1371{
1372 (void) in_pid;
1373 (void) in_gid;
1374 return 0;
1375}
1376#endif
1377
1378void sh_unix_closeall (int fd, int except, int inchild)
1379{
1380 int fdx = fd;
1381#ifdef _SC_OPEN_MAX
1382 int fdlimit = sysconf (_SC_OPEN_MAX);
1383#else
1384#ifdef OPEN_MAX
1385 int fdlimit = OPEN_MAX;
1386#else
1387 int fdlimit = _POSIX_OPEN_MAX;
1388#endif
1389#endif
1390
1391 SL_ENTER(_("sh_unix_closeall"));
1392
1393 /* can't happen - so fix it :-(
1394 */
1395 if (fdlimit < 0)
1396 fdlimit = 20; /* POSIX lower limit */
1397
1398 if (fdlimit > 65536)
1399 fdlimit = 65536;
1400
1401 if (!inchild)
1402 sl_dropall (fdx, except);
1403 else
1404 sl_dropall_dirty (fdx, except);
1405
1406 /* Close everything from fd (inclusive) up to fdlimit (exclusive).
1407 */
1408 while (fd < fdlimit)
1409 {
1410 if (fd == except)
1411 fd++;
1412 else if (slib_do_trace != 0 && fd == slib_trace_fd)
1413 fd++;
1414 else
1415 sl_close_fd(FIL__, __LINE__, fd++);
1416 }
1417
1418 SL_RET0(_("sh_unix_closeall"));
1419}
1420
1421static void sh_unix_setlimits(void)
1422{
1423 struct rlimit limits;
1424
1425 SL_ENTER(_("sh_unix_setlimits"));
1426
1427 limits.rlim_cur = RLIM_INFINITY;
1428 limits.rlim_max = RLIM_INFINITY;
1429
1430#ifdef RLIMIT_CPU
1431 setrlimit (RLIMIT_CPU, &limits);
1432#endif
1433#ifdef RLIMIT_FSIZE
1434 setrlimit (RLIMIT_FSIZE, &limits);
1435#endif
1436#ifdef RLIMIT_DATA
1437 setrlimit (RLIMIT_DATA, &limits);
1438#endif
1439#ifdef RLIMIT_STACK
1440 setrlimit (RLIMIT_STACK, &limits);
1441#endif
1442#ifdef RLIMIT_RSS
1443 setrlimit (RLIMIT_RSS, &limits);
1444#endif
1445#ifdef RLIMIT_NPROC
1446 setrlimit (RLIMIT_NPROC, &limits);
1447#endif
1448#ifdef RLIMIT_MEMLOCK
1449 setrlimit (RLIMIT_MEMLOCK, &limits);
1450#endif
1451
1452#if !defined(SL_DEBUG)
1453 /* no core dumps
1454 */
1455 limits.rlim_cur = 0;
1456 limits.rlim_max = 0;
1457#ifdef RLIMIT_CORE
1458 setrlimit (RLIMIT_CORE, &limits);
1459#endif
1460#else
1461#ifdef RLIMIT_CORE
1462 setrlimit (RLIMIT_CORE, &limits);
1463#endif
1464#endif
1465
1466 limits.rlim_cur = 1024;
1467 limits.rlim_max = 1024;
1468
1469#if defined(RLIMIT_NOFILE)
1470 setrlimit (RLIMIT_NOFILE, &limits);
1471#elif defined(RLIMIT_OFILE)
1472 setrlimit (RLIMIT_OFILE, &limits);
1473#endif
1474
1475 SL_RET0(_("sh_unix_setlimits"));
1476}
1477
1478static void sh_unix_copyenv(void)
1479{
1480 char ** env0 = environ;
1481 char ** env1;
1482 int envlen = 0;
1483 size_t len;
1484
1485 SL_ENTER(_("sh_unix_copyenv"));
1486
1487 while (env0 != NULL && env0[envlen] != NULL) {
1488 /* printf("%2d: %s\n", envlen, env0[envlen]); */
1489 ++envlen;
1490 }
1491 ++envlen;
1492
1493 /* printf("-> %2d: slots allocated\n", envlen); */
1494 env1 = calloc(1,sizeof(char *) * envlen); /* only once */
1495 if (env1 == NULL)
1496 {
1497 fprintf(stderr, _("%s: %d: Out of memory\n"), FIL__, __LINE__);
1498 SL_RET0(_("sh_unix_copyenv"));
1499 }
1500 env0 = environ;
1501 envlen = 0;
1502
1503 while (env0 != NULL && env0[envlen] != NULL) {
1504 len = strlen(env0[envlen]) + 1;
1505 env1[envlen] = calloc(1,len); /* only once */
1506 if (env1[envlen] == NULL)
1507 {
1508 int i;
1509 fprintf(stderr, _("%s: %d: Out of memory\n"), FIL__, __LINE__);
1510 for (i = 0; i < envlen; ++i) free(env1[len]);
1511 free(env1);
1512 SL_RET0(_("sh_unix_copyenv"));
1513 }
1514 sl_strlcpy(env1[envlen], env0[envlen], len);
1515 ++envlen;
1516 }
1517 env1[envlen] = NULL;
1518
1519 environ = env1;
1520 SL_RET0(_("sh_unix_copyenv"));
1521}
1522
1523/* delete all environment variables
1524 */
1525static void sh_unix_zeroenv(void)
1526{
1527 char * c;
1528 char ** env;
1529
1530 SL_ENTER(_("sh_unix_zeroenv"));
1531
1532 sh_unix_copyenv();
1533 env = environ;
1534
1535 while (env != NULL && *env != NULL) {
1536 c = strchr ((*env), '=');
1537#ifdef WITH_MYSQL
1538 /*
1539 * Skip the MYSQL_UNIX_PORT environment variable; MySQL may need it.
1540 */
1541 if (0 == sl_strncmp((*env), _("MYSQL_UNIX_PORT="), 16))
1542 {
1543 ++(env);
1544 continue;
1545 }
1546 if (0 == sl_strncmp((*env), _("MYSQL_TCP_PORT="), 15))
1547 {
1548 ++(env);
1549 continue;
1550 }
1551 if (0 == sl_strncmp((*env), _("MYSQL_HOME="), 11))
1552 {
1553 ++(env);
1554 continue;
1555 }
1556#endif
1557#ifdef WITH_ORACLE
1558 /*
1559 * Skip the ORACLE_HOME and TNS_ADMIN environment variables;
1560 * Oracle may need them.
1561 */
1562 if (0 == sl_strncmp((*env), _("ORACLE_HOME="), 12))
1563 {
1564 ++(env);
1565 continue;
1566 }
1567 if (0 == sl_strncmp((*env), _("TNS_ADMIN="), 10))
1568 {
1569 ++(env);
1570 continue;
1571 }
1572#endif
1573 /*
1574 * Skip the TZ environment variable.
1575 */
1576 if (0 == sl_strncmp((*env), _("TZ="), 3))
1577 {
1578 ++(env);
1579 continue;
1580 }
1581 ++(env);
1582 if (c != NULL)
1583 {
1584 ++c;
1585 while ((*c) != '\0') {
1586 (*c) = '\0';
1587 ++c;
1588 }
1589 }
1590 }
1591
1592#ifdef HAVE_TZSET
1593 tzset();
1594#endif
1595
1596 SL_RET0(_("sh_unix_zeroenv"));
1597}
1598
1599
1600static void sh_unix_resettimer(void)
1601{
1602 struct itimerval this_timer;
1603
1604 SL_ENTER(_("sh_unix_resettimer"));
1605
1606 this_timer.it_value.tv_sec = 0;
1607 this_timer.it_value.tv_usec = 0;
1608
1609 this_timer.it_interval.tv_sec = 0;
1610 this_timer.it_interval.tv_usec = 0;
1611
1612 setitimer(ITIMER_REAL, &this_timer, NULL);
1613#if !defined(SH_PROFILE)
1614 setitimer(ITIMER_VIRTUAL, &this_timer, NULL);
1615 setitimer(ITIMER_PROF, &this_timer, NULL);
1616#endif
1617
1618 SL_RET0(_("sh_unix_resettimer"));
1619}
1620
1621static void sh_unix_resetsignals(void)
1622{
1623 int sig_num;
1624#ifdef NSIG
1625 int max_sig = NSIG;
1626#else
1627 int max_sig = 255;
1628#endif
1629 int test;
1630 int status;
1631 struct sigaction act;
1632#if !defined(SH_PROFILE)
1633 struct sigaction oldact;
1634#endif
1635
1636 sigset_t set_proc;
1637
1638 SL_ENTER(_("sh_unix_resetsignals"));
1639 /*
1640 * Reset the current signal mask (inherited from parent process).
1641 */
1642
1643 sigfillset(&set_proc);
1644
1645 do {
1646 errno = 0;
1647 test = SH_SETSIGMASK(SIG_UNBLOCK, &set_proc, NULL);
1648 } while (test < 0 && errno == EINTR);
1649
1650 /*
1651 * Reset signal handling.
1652 */
1653
1654 act.sa_handler = SIG_DFL; /* signal action */
1655 sigemptyset( &act.sa_mask ); /* set an empty mask */
1656 act.sa_flags = 0; /* init sa_flags */
1657
1658 for (sig_num = 1; sig_num <= max_sig; ++sig_num)
1659 {
1660#if !defined(SH_PROFILE)
1661 test = retry_sigaction(FIL__, __LINE__, sig_num, &act, &oldact);
1662#else
1663 test = 0;
1664#endif
1665 if ((test == -1) && (errno != EINVAL))
1666 {
1667 char errbuf[SH_ERRBUF_SIZE];
1668 status = errno;
1669 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_W_SIG,
1670 sh_error_message (status, errbuf, sizeof(errbuf)), sig_num);
1671 }
1672 }
1673
1674 SL_RET0(_("sh_unix_resetsignals"));
1675}
1676
1677/* Get the local hostname (FQDN)
1678 */
1679static char * sh_tolower (char * s)
1680{
1681 char * ret = s;
1682 if (s)
1683 {
1684 for (; *s; ++s)
1685 {
1686 *s = tolower((unsigned char) *s);
1687 }
1688 }
1689 return ret;
1690}
1691
1692
1693#include <sys/socket.h>
1694
1695/* Required for BSD
1696 */
1697#ifdef HAVE_NETINET_IN_H
1698#include <netinet/in.h>
1699#endif
1700
1701#include <arpa/inet.h>
1702
1703const char * sh_unix_h_name (struct hostent * host_entry)
1704{
1705 char ** p;
1706 if (strchr(host_entry->h_name, '.')) {
1707 return host_entry->h_name;
1708 } else {
1709 for (p = host_entry->h_aliases; *p; ++p) {
1710 if (strchr(*p, '.'))
1711 return *p;
1712 }
1713 }
1714 return host_entry->h_name;
1715}
1716
1717/* uname() on FreeBSD is broken, because the 'nodename' buf is too small
1718 * to hold a valid (leftmost) domain label.
1719 */
1720#if defined(HAVE_UNAME) && !defined(HOST_IS_FREEBSD)
1721#include <sys/utsname.h>
1722void sh_unix_localhost()
1723{
1724 struct utsname buf;
1725 int i;
1726 unsigned int ddot;
1727 int len;
1728 char * p;
1729 char hostname[256];
1730 char numeric[SH_IP_BUF];
1731 char * canonical;
1732
1733
1734 SL_ENTER(_("sh_unix_localhost"));
1735
1736 (void) uname (&buf);
1737 /* flawfinder: ignore */ /* ff bug, ff sees system() */
1738 sl_strlcpy (sh.host.system, buf.sysname, SH_MINIBUF);
1739 sl_strlcpy (sh.host.release, buf.release, SH_MINIBUF);
1740 sl_strlcpy (sh.host.machine, buf.machine, SH_MINIBUF);
1741
1742 /* Workaround for cases where nodename could be
1743 * a truncated FQDN.
1744 */
1745 if (strlen(buf.nodename) == (sizeof(buf.nodename)-1))
1746 {
1747 p = strchr(buf.nodename, '.');
1748 if (NULL != p) {
1749 *p = '\0';
1750 sl_strlcpy(hostname, buf.nodename, 256);
1751 } else {
1752#ifdef HAVE_GETHOSTNAME
1753 if (0 != gethostname(hostname, 256))
1754 {
1755 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1756 _("nodename returned by uname may be truncated"),
1757 _("sh_unix_localhost"));
1758 sl_strlcpy (hostname, buf.nodename, 256);
1759 }
1760 else
1761 {
1762 hostname[255] = '\0';
1763 }
1764#else
1765 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN,
1766 _("nodename returned by uname may be truncated"),
1767 _("sh_unix_localhost"));
1768 sl_strlcpy(hostname, buf.nodename, 256);
1769#endif
1770 }
1771 }
1772 else
1773 {
1774 sl_strlcpy(hostname, buf.nodename, 256);
1775 }
1776
1777 canonical = sh_ipvx_canonical(hostname, numeric, sizeof(numeric));
1778
1779 if (canonical == NULL)
1780 {
1781 sl_strlcpy (sh.host.name, hostname, SH_PATHBUF);
1782 sh_tolower (sh.host.name);
1783 }
1784 else
1785 {
1786 sl_strlcpy (sh.host.name, canonical, SH_PATHBUF);
1787 SH_FREE(canonical);
1788 }
1789
1790 /* check whether it looks like a FQDN
1791 */
1792 len = sl_strlen(sh.host.name);
1793 ddot = 0;
1794 for (i = 0; i < len; ++i)
1795 if (sh.host.name[i] == '.') ++ddot;
1796
1797 if (ddot == 0)
1798 {
1799 dlog(1, FIL__, __LINE__,
1800 _("According to uname, your nodename is %s, but your resolver\nlibrary cannot resolve this nodename to a FQDN.\nRather, it resolves this to %s.\nFor more information, see the entry about self-resolving under\n'Most frequently' in the FAQ that you will find in the docs/ subdirectory.\n"),
1801 hostname, sh.host.name);
1802 sl_strlcpy (sh.host.name, numeric, SH_PATHBUF);
1803 SL_RET0(_("sh_unix_localhost"));
1804 }
1805
1806 if (sh_ipvx_is_numeric(sh.host.name))
1807 {
1808 dlog(1, FIL__, __LINE__,
1809 _("According to uname, your nodename is %s, but your resolver\nlibrary cannot resolve this nodename to a FQDN.\nRather, it resolves this to %s.\nFor more information, see the entry about self-resolving under\n'Most frequently' in the FAQ that you will find in the docs/ subdirectory.\n"),
1810 hostname, sh.host.name);
1811 }
1812
1813 SL_RET0(_("sh_unix_localhost"));
1814}
1815
1816#else
1817
1818/*
1819 * --FreeBSD code
1820 */
1821#if defined(HAVE_UNAME)
1822#include <sys/utsname.h>
1823#endif
1824void sh_unix_localhost()
1825{
1826#if defined(HAVE_UNAME)
1827 struct utsname buf;
1828#endif
1829 int i;
1830 int ddot;
1831 int len;
1832 char hostname[1024];
1833 char numeric[SH_IP_BUF];
1834 char * canonical;
1835
1836 SL_ENTER(_("sh_unix_localhost"));
1837
1838#if defined(HAVE_UNAME)
1839 (void) uname (&buf);
1840 /* flawfinder: ignore */ /* ff bug, ff sees system() */
1841 sl_strlcpy (sh.host.system, buf.sysname, SH_MINIBUF);
1842 sl_strlcpy (sh.host.release, buf.release, SH_MINIBUF);
1843 sl_strlcpy (sh.host.machine, buf.machine, SH_MINIBUF);
1844#endif
1845
1846 (void) gethostname (hostname, 1024);
1847 hostname[1023] = '\0';
1848
1849 canonical = sh_ipvx_canonical(hostname, numeric, sizeof(numeric));
1850
1851 if (canonical == NULL)
1852 {
1853 sl_strlcpy (sh.host.name, hostname, SH_PATHBUF);
1854 sh_tolower (sh.host.name);
1855 }
1856 else
1857 {
1858 sl_strlcpy (sh.host.name, canonical, SH_PATHBUF);
1859 SH_FREE(canonical);
1860 }
1861
1862 /* check whether it looks like a FQDN
1863 */
1864 len = sl_strlen(sh.host.name);
1865 ddot = 0;
1866 for (i = 0; i < len; ++i)
1867 if (sh.host.name[i] == '.') ++ddot;
1868 if (ddot == 0)
1869 {
1870 dlog(1, FIL__, __LINE__,
1871 _("According to uname, your nodename is %s, but your resolver\nlibrary cannot resolve this nodename to a FQDN.\nRather, it resolves this to %s.\nFor more information, see the entry about self-resolving under\n'Most frequently' in the FAQ that you will find in the docs/ subdirectory.\n"),
1872 hostname, sh.host.name);
1873 sl_strlcpy (sh.host.name, numeric, SH_PATHBUF);
1874 SL_RET0(_("sh_unix_localhost"));
1875 }
1876
1877 if (sh_ipvx_is_numeric(sh.host.name))
1878 {
1879 dlog(1, FIL__, __LINE__,
1880 _("According to uname, your nodename is %s, but your resolver\nlibrary cannot resolve this nodename to a FQDN.\nRather, it resolves this to %s.\nFor more information, see the entry about self-resolving under\n'Most frequently' in the FAQ that you will find in the docs/ subdirectory.\n"),
1881 hostname, sh.host.name);
1882 }
1883
1884 SL_RET0(_("sh_unix_localhost"));
1885}
1886#endif
1887
1888
1889void sh_unix_memlock()
1890{
1891 SL_ENTER(_("sh_unix_memlock"));
1892
1893 /* do this before dropping privileges
1894 */
1895#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
1896 if (skey->mlock_failed == S_FALSE)
1897 {
1898 if ( (-1) == sh_unix_mlock( FIL__, __LINE__,
1899 (char *) skey, sizeof (sh_key_t)) )
1900 {
1901 SH_MUTEX_LOCK_UNSAFE(mutex_skey);
1902 skey->mlock_failed = S_TRUE;
1903 SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
1904 }
1905 }
1906#else
1907 if (skey->mlock_failed == S_FALSE)
1908 {
1909 SH_MUTEX_LOCK_UNSAFE(mutex_skey);
1910 skey->mlock_failed = S_TRUE;
1911 SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
1912 }
1913#endif
1914
1915 SL_RET0(_("sh_unix_memlock"));
1916}
1917
1918#ifdef SH_WITH_SERVER
1919char * chroot_dir = NULL;
1920
1921int sh_unix_set_chroot(const char * str)
1922{
1923 size_t len;
1924 static int block = 0;
1925
1926 if (block == 1)
1927 return 0;
1928
1929 if (str && *str == '/')
1930 {
1931 len = strlen(str) + 1;
1932 chroot_dir = calloc(1,strlen(str) + 1); /* only once */
1933 if (!chroot_dir)
1934 {
1935 fprintf(stderr, _("%s: %d: Out of memory\n"), FIL__, __LINE__);
1936 return 1;
1937 }
1938 sl_strlcpy(chroot_dir, str, len);
1939 block = 1;
1940 return 0;
1941 }
1942 return 1;
1943}
1944
1945int sh_unix_chroot(void)
1946{
1947 int status;
1948
1949 if (chroot_dir != NULL)
1950 {
1951 status = retry_aud_chdir(FIL__, __LINE__, chroot_dir);
1952 if ( (-1) == status )
1953 {
1954 char errbuf[SH_ERRBUF_SIZE];
1955 status = errno;
1956 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_W_CHDIR,
1957 sh_error_message (status, errbuf, sizeof(errbuf)), chroot_dir);
1958 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1959 }
1960 /* flawfinder: ignore */
1961 return (chroot(chroot_dir));
1962 }
1963 return 0;
1964}
1965/* #ifdef SH_WITH_SERVER */
1966#else
1967int sh_unix_chroot(void) { return 0; }
1968#endif
1969
1970/* daemon mode
1971 */
1972static int block_setdeamon = 0;
1973
1974int sh_unix_setdeamon(const char * dummy)
1975{
1976 int res = 0;
1977
1978 SL_ENTER(_("sh_unix_setdeamon"));
1979
1980 if (block_setdeamon != 0)
1981 SL_RETURN((0),_("sh_unix_setdeamon"));
1982
1983 if (dummy == NULL)
1984 sh.flag.isdaemon = S_TRUE;
1985 else
1986 res = sh_util_flagval (dummy, &sh.flag.isdaemon);
1987
1988 if (sh.flag.opts == S_TRUE)
1989 block_setdeamon = 1;
1990
1991 SL_RETURN(res, _("sh_unix_setdeamon"));
1992}
1993#if defined(HAVE_LIBPRELUDE)
1994#include "sh_prelude.h"
1995#endif
1996
1997int sh_unix_setnodeamon(const char * dummy)
1998{
1999 int res = 0;
2000
2001 SL_ENTER(_("sh_unix_setnodeamon"));
2002
2003 if (block_setdeamon != 0)
2004 SL_RETURN((0),_("sh_unix_setmodeamon"));
2005
2006 if (dummy == NULL)
2007 sh.flag.isdaemon = S_FALSE;
2008 else
2009 res = sh_util_flagval (dummy, &sh.flag.isdaemon);
2010
2011 if (sh.flag.opts == S_TRUE)
2012 block_setdeamon = 1;
2013
2014 SL_RETURN(res, _("sh_unix_setnodeamon"));
2015}
2016
2017int sh_unix_init(int goDaemon)
2018{
2019 int status;
2020 uid_t uid;
2021 pid_t oldpid = getpid();
2022#if defined(SH_WITH_SERVER)
2023 extern int sh_socket_open_int (void);
2024#endif
2025 char errbuf[SH_ERRBUF_SIZE];
2026
2027 extern void sh_kill_sub();
2028
2029 SL_ENTER(_("sh_unix_init"));
2030
2031 /* fork twice, exit the parent process
2032 */
2033 if (goDaemon == 1) {
2034
2035 switch (aud_fork(FIL__, __LINE__)) {
2036 case 0: break; /* child process continues */
2037 case -1: SL_RETURN((-1),_("sh_unix_init")); /* error */
2038 default: /* parent process exits */
2039 sh_kill_sub();
2040 aud__exit(FIL__, __LINE__, 0);
2041 }
2042
2043 /* Child processes do not inherit page locks across a fork.
2044 * Error in next fork would return in this (?) thread of execution.
2045 */
2046 sh_unix_memlock();
2047
2048 setsid(); /* should not fail */
2049 sh.pid = (UINT64) getpid();
2050
2051 switch (aud_fork(FIL__, __LINE__)) {
2052 case 0: break; /* child process continues */
2053 case -1: SL_RETURN((-1),_("sh_unix_init")); /* error */
2054 default: /* parent process exits */
2055 sh_kill_sub();
2056 aud__exit(FIL__, __LINE__, 0);
2057 }
2058
2059 /* Child processes do not inherit page locks across a fork.
2060 */
2061 sh_unix_memlock();
2062 sh.pid = (UINT64) getpid();
2063
2064 } else {
2065 setsid(); /* should not fail */
2066 }
2067
2068 /* set working directory
2069 */
2070#ifdef SH_PROFILE
2071 status = 0;
2072#else
2073 status = retry_aud_chdir(FIL__, __LINE__, "/");
2074#endif
2075 if ( (-1) == status )
2076 {
2077 status = errno;
2078 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_W_CHDIR,
2079 sh_error_message (status, errbuf, sizeof(errbuf)), "/");
2080 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
2081 }
2082
2083 /* reset timers
2084 */
2085 sh_unix_resettimer();
2086
2087 /* signal handlers
2088 */
2089 sh_unix_resetsignals();
2090#if defined(SCREW_IT_UP)
2091 sh_sigtrap_prepare();
2092#endif
2093 sh_unix_siginstall (goDaemon);
2094
2095 /* set file creation mask
2096 */
2097 (void) umask (0); /* should not fail */
2098
2099 /* set resource limits to maximum, and
2100 * core dump size to zero
2101 */
2102 sh_unix_setlimits();
2103
2104 /* zero out the environment (like PATH='\0')
2105 */
2106 sh_unix_zeroenv();
2107
2108 if (goDaemon == 1)
2109 {
2110 /* Close first tree file descriptors
2111 */
2112 sl_close_fd (FIL__, __LINE__, 0); /* if running as daemon */
2113 sl_close_fd (FIL__, __LINE__, 1); /* if running as daemon */
2114 sl_close_fd (FIL__, __LINE__, 2); /* if running as daemon */
2115
2116 /* Enable full error logging
2117 */
2118 sh_error_only_stderr (S_FALSE);
2119
2120 /* open first three streams to /dev/null
2121 */
2122 status = aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 0);
2123 if (status < 0)
2124 {
2125 status = errno;
2126 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
2127 sh_error_message(status, errbuf, sizeof(errbuf)), _("open"));
2128 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
2129 }
2130
2131 status = retry_aud_dup(FIL__, __LINE__, 0);
2132 if (status >= 0)
2133 retry_aud_dup(FIL__, __LINE__, 0);
2134
2135 if (status < 0)
2136 {
2137 status = errno;
2138 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
2139 sh_error_message(status, errbuf, sizeof(errbuf)), _("dup"));
2140 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
2141 }
2142
2143 sh_error_enable_unsafe (S_TRUE);
2144#if defined(HAVE_LIBPRELUDE)
2145 sh_prelude_reset ();
2146#endif
2147
2148 /* --- wait until parent has exited ---
2149 */
2150 while (1 == 1)
2151 {
2152 errno = 0;
2153 if (0 > aud_kill (FIL__, __LINE__, oldpid, 0) && errno == ESRCH)
2154 {
2155 break;
2156 }
2157 retry_msleep(0, 1);
2158 }
2159
2160 /* write PID file
2161 */
2162 status = sh_unix_write_pid_file();
2163 if (status < 0)
2164 {
2165 sl_get_euid(&uid);
2166 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_PIDFILE,
2167 (long) uid, sh.srvlog.alt);
2168 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
2169 }
2170#if defined(SH_WITH_SERVER)
2171 sh_socket_open_int ();
2172#endif
2173 }
2174 else
2175 {
2176 sh_error_enable_unsafe (S_TRUE);
2177#if defined(HAVE_LIBPRELUDE)
2178 sh_prelude_reset ();
2179#endif
2180#if defined(SH_WITH_SERVER)
2181 sh_socket_open_int ();
2182#endif
2183 }
2184
2185 /* chroot (this is a no-op if no chroot dir is specified
2186 */
2187 status = sh_unix_chroot();
2188 if (status < 0)
2189 {
2190 status = errno;
2191 sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGEN,
2192 sh_error_message(status, errbuf, sizeof(errbuf)), _("chroot"));
2193 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
2194 }
2195
2196 /* drop capabilities
2197 */
2198 sl_drop_cap();
2199
2200 SL_RETURN((0),_("sh_unix_init"));
2201}
2202
2203/* --- run a command, securely --- */
2204
2205int sh_unix_run_command (const char * str)
2206{
2207 pid_t pid;
2208 char * arg[4];
2209 char * env[5];
2210 char * path = sh_util_strdup(_("/bin/sh"));
2211
2212 int status = -1;
2213
2214 arg[0] = sh_util_strdup(_("/bin/sh"));
2215 arg[1] = sh_util_strdup(_("-c"));
2216 arg[2] = sh_util_strdup(str);
2217 arg[3] = NULL;
2218
2219 env[0] = sh_util_strdup(_("PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/ucb"));
2220 env[1] = sh_util_strdup(_("SHELL=/bin/sh"));
2221 env[2] = sh_util_strdup(_("IFS= \t\n"));
2222 if (getenv("TZ")) { /* flawfinder: ignore */
2223 char * tz = sh_util_strdup(getenv("TZ")); /* flawfinder: ignore */
2224 size_t tzlen = strlen(tz);
2225 if (S_TRUE == sl_ok_adds (4, tzlen)) {
2226 env[3] = SH_ALLOC(4+tzlen);
2227 sl_strlcpy(env[3], "TZ=", 4);
2228 sl_strlcat(env[3], tz , 4+tzlen);
2229 } else {
2230 env[3] = NULL;
2231 }
2232 } else {
2233 env[3] = NULL;
2234 }
2235 env[4] = NULL;
2236
2237 pid = fork();
2238
2239 if (pid == (pid_t)(-1))
2240 {
2241 return -1;
2242 }
2243
2244 else if (pid == 0) /* child */
2245 {
2246 memset(skey, 0, sizeof(sh_key_t));
2247 (void) umask(S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
2248 sh_unix_closeall (3, -1, S_TRUE); /* in child process */
2249 execve(path, arg, env);
2250 _exit(EXIT_FAILURE);
2251 }
2252
2253 else /* parent */
2254 {
2255 int r;
2256
2257 while((r = waitpid(pid, &status, WUNTRACED)) != pid && r != -1) ;
2258
2259#if !defined(USE_UNO)
2260 if (r == -1 || !WIFEXITED(status))
2261 {
2262 status = -1;
2263 }
2264 else
2265 {
2266 status = WEXITSTATUS(status);
2267 }
2268#endif
2269 }
2270
2271 return status;
2272}
2273
2274/********************************************************
2275 *
2276 * TIME
2277 *
2278 ********************************************************/
2279
2280/* Figure out the time offset of the current timezone
2281 * in a portable way.
2282 */
2283char * t_zone(const time_t * xx)
2284{
2285 struct tm aa;
2286 struct tm bb;
2287
2288 struct tm * aptr;
2289 struct tm * bptr;
2290
2291 int sign = 0;
2292 int diff = 0;
2293 int hh, mm;
2294 static char tz[64];
2295
2296 SL_ENTER(_("t_zone"));
2297
2298#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GMTIME_R)
2299 aptr = gmtime_r (xx, &aa);
2300#else
2301 aptr = gmtime(xx);
2302 if (aptr)
2303 memcpy (&aa, aptr, sizeof(struct tm));
2304#endif
2305
2306#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
2307 bptr = localtime_r (xx, &bb);
2308#else
2309 bptr = localtime(xx);
2310 if (bptr)
2311 memcpy (&bb, bptr, sizeof(struct tm));
2312#endif
2313
2314 if (bptr && aptr)
2315 {
2316 /* Check for datum wrap-around.
2317 */
2318 if ((aa.tm_mday == 1) && (aa.tm_mday < bb.tm_mday))
2319 sign = ( 1);
2320 else if (aa.tm_year < bb.tm_year)
2321 sign = (-1);
2322 else if (aa.tm_mon < bb.tm_mon)
2323 sign = (-1);
2324 else if (aa.tm_mday < bb.tm_mday)
2325 sign = (-1);
2326 else if (bb.tm_year < aa.tm_year)
2327 sign = ( 1);
2328 else if (bb.tm_mon < aa.tm_mon)
2329 sign = ( 1);
2330 else if (bb.tm_mday < aa.tm_mday)
2331 sign = ( 1);
2332
2333 diff = aa.tm_hour * 60 + aa.tm_min;
2334 diff = (bb.tm_hour * 60 + bb.tm_min) - diff;
2335 diff = diff - (sign * 24 * 60); /* datum wrap-around correction */
2336 hh = diff / 60;
2337 mm = diff - (hh * 60);
2338 sprintf (tz, _("%+03d%02d"), hh, mm); /* known to fit */
2339 }
2340 else
2341 {
2342 sprintf (tz, _("%+03d%02d"), 0, 0);
2343 }
2344 SL_RETURN(tz, _("t_zone"));
2345}
2346
2347unsigned long sh_unix_longtime ()
2348{
2349 return ((unsigned long)time(NULL));
2350}
2351
2352#ifdef HAVE_GETTIMEOFDAY
2353unsigned long sh_unix_notime ()
2354{
2355 struct timeval tv;
2356
2357 gettimeofday (&tv, NULL);
2358
2359 return ((unsigned long)(tv.tv_sec + tv.tv_usec * 10835 + getpid() + getppid()));
2360
2361}
2362#endif
2363
2364static int count_dev_time = 0;
2365
2366void reset_count_dev_time(void)
2367{
2368 count_dev_time = 0;
2369 return;
2370}
2371
2372int sh_unix_settimeserver (const char * address)
2373{
2374
2375 SL_ENTER(_("sh_unix_settimeserver"));
2376
2377 if (address != NULL && count_dev_time < 2
2378 && sl_strlen(address) < SH_PATHBUF)
2379 {
2380 if (count_dev_time == 0)
2381 sl_strlcpy (sh.srvtime.name, address, SH_PATHBUF);
2382 else
2383 sl_strlcpy (sh.srvtime.alt, address, SH_PATHBUF);
2384
2385 ++count_dev_time;
2386 SL_RETURN((0), _("sh_unix_settimeserver"));
2387 }
2388 SL_RETURN((-1), _("sh_unix_settimeserver"));
2389}
2390
2391
2392#ifdef HAVE_NTIME
2393#define UNIXEPOCH 2208988800UL /* difference between Unix time and net time
2394 * The UNIX EPOCH starts in 1970.
2395 */
2396#include <sys/socket.h>
2397#include <netinet/in.h>
2398#include <arpa/inet.h>
2399#include <netdb.h>
2400#include <ctype.h>
2401#endif
2402
2403/* Timeserver service. */
2404/* define is missing on HP-UX 10.20 */
2405#ifndef IPPORT_TIMESERVER
2406#define IPPORT_TIMESERVER 37
2407#endif
2408
2409char * sh_unix_time (time_t thetime, char * buffer, size_t len)
2410{
2411
2412 int status;
2413 char AsciiTime[81]; /* local time */
2414 time_t time_now;
2415 struct tm * time_ptr;
2416#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
2417 struct tm time_tm;
2418#endif
2419#ifdef SH_USE_XML
2420 static char deftime[] = N_("0000-00-00T00:00:00"); /* default time */
2421#else
2422 static char deftime[] = N_("[0000-00-00T00:00:00]"); /* default time */
2423#endif
2424
2425#ifdef HAVE_NTIME
2426 int fd; /* network file descriptor */
2427 u_char net_time[4]; /* remote time in network format */
2428 static int failerr = 0; /* no net time */
2429 int fail = 0; /* no net time */
2430 int errflag;
2431 char errmsg[256];
2432 char error_call[SH_MINIBUF];
2433 int error_num;
2434#endif
2435
2436 SL_ENTER(_("sh_unix_time"));
2437
2438#ifdef HAVE_NTIME
2439 if (thetime == 0)
2440 {
2441 if (sh.srvtime.name[0] == '\0')
2442 {
2443 fail = 1;
2444 (void) time (&time_now);
2445 }
2446 else /* have a timeserver address */
2447 {
2448 /* don't call timeserver more than once per second */
2449 static time_t time_old = 0;
2450 time_t time_new;
2451 static time_t time_saved = 0;
2452 (void) time (&time_new);
2453 if ((time_new == time_old) && (time_saved != 0))
2454 {
2455 time_now = time_saved;
2456 goto end;
2457 }
2458 time_old = time_new;
2459
2460
2461 fd = connect_port_2 (sh.srvtime.name, sh.srvtime.alt,
2462 IPPORT_TIMESERVER,
2463 error_call, &error_num, errmsg, sizeof(errmsg));
2464 if (fd >= 0)
2465 {
2466 if (4 != read_port (fd, (char *) net_time, 4, &errflag, 2))
2467 {
2468 fail = 1;
2469 sh_error_handle ((-1), FIL__, __LINE__, errflag,
2470 MSG_E_NLOST,
2471 _("time"), sh.srvtime.name);
2472 }
2473 sl_close_fd(FIL__, __LINE__, fd);
2474 }
2475 else
2476 {
2477 sh_error_handle ((-1), FIL__, __LINE__, error_num,
2478 MSG_E_NET, errmsg, error_call,
2479 _("time"), sh.srvtime.name);
2480 fail = 1;
2481 }
2482
2483 if (fail == 0)
2484 {
2485 unsigned long ltmp;
2486 UINT32 ttmp;
2487 memcpy(&ttmp, net_time, sizeof(UINT32)); ltmp = ttmp;
2488 time_now = ntohl(ltmp) - UNIXEPOCH;
2489 time_saved = time_now;
2490
2491 if (failerr == 1) {
2492 failerr = 0;
2493 sh_error_handle ((-1), FIL__, __LINE__, 0,
2494 MSG_E_NEST,
2495 _("time"), sh.srvtime.name);
2496 }
2497 }
2498 else
2499 {
2500 (void) time (&time_now);
2501 time_saved = 0;
2502
2503 if (failerr == 0)
2504 {
2505 failerr = 1;
2506 sh_error_handle ((-1), FIL__, __LINE__, errflag,
2507 MSG_SRV_FAIL,
2508 _("time"), sh.srvtime.name);
2509 }
2510 }
2511 end:
2512 ; /* 'label at end of compound statement' */
2513 }
2514 }
2515 else
2516 {
2517 time_now = thetime;
2518 }
2519
2520 /* #ifdef HAVE_NTIME */
2521#else
2522
2523 if (thetime == 0)
2524 {
2525 (void) time (&time_now);
2526 }
2527 else
2528 {
2529 time_now = thetime;
2530 }
2531
2532 /* #ifdef HAVE_NTIME */
2533#endif
2534
2535 if (time_now == (-1) )
2536 {
2537 sl_strlcpy(buffer, _(deftime), len);
2538 SL_RETURN(buffer, _("sh_unix_time"));
2539 }
2540 else
2541 {
2542#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
2543 time_ptr = localtime_r (&time_now, &time_tm);
2544#else
2545 time_ptr = localtime (&time_now);
2546#endif
2547 }
2548 if (time_ptr != NULL)
2549 {
2550 status = strftime (AsciiTime, sizeof(AsciiTime),
2551#ifdef SH_USE_XML
2552 _("%Y-%m-%dT%H:%M:%S%%s"),
2553#else
2554 _("[%Y-%m-%dT%H:%M:%S%%s]"),
2555#endif
2556 time_ptr);
2557
2558 sl_snprintf(buffer, len, AsciiTime, t_zone(&time_now));
2559
2560 if ( (status == 0) || (status == sizeof(AsciiTime)) )
2561 {
2562 sl_strlcpy(buffer, _(deftime), len);
2563 SL_RETURN( buffer, _("sh_unix_time"));
2564 }
2565 else
2566 {
2567 SL_RETURN(buffer, _("sh_unix_time"));
2568 }
2569 }
2570
2571 /* last resort
2572 */
2573 sl_strlcpy(buffer, _(deftime), len);
2574 SL_RETURN( buffer, _("sh_unix_time"));
2575}
2576
2577static int sh_unix_use_localtime = S_FALSE;
2578
2579/* whether to use localtime for file timestamps in logs
2580 */
2581int sh_unix_uselocaltime (const char * c)
2582{
2583 int i;
2584 SL_ENTER(_("sh_unix_uselocaltime"));
2585 i = sh_util_flagval(c, &(sh_unix_use_localtime));
2586
2587 SL_RETURN(i, _("sh_unix_uselocaltime"));
2588}
2589
2590char * sh_unix_gmttime (time_t thetime, char * buffer, size_t len)
2591{
2592
2593 int status;
2594
2595 struct tm * time_ptr;
2596#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS)
2597 struct tm time_tm;
2598#endif
2599 char AsciiTime[81]; /* GMT time */
2600#ifdef SH_USE_XML
2601 static char deftime[] = N_("0000-00-00T00:00:00"); /* default time */
2602#else
2603 static char deftime[] = N_("[0000-00-00T00:00:00]"); /* default time */
2604#endif
2605
2606 SL_ENTER(_("sh_unix_gmttime"));
2607
2608 if (sh_unix_use_localtime == S_FALSE)
2609 {
2610#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GMTIME_R)
2611 time_ptr = gmtime_r (&thetime, &time_tm);
2612#else
2613 time_ptr = gmtime (&thetime);
2614#endif
2615 }
2616 else
2617 {
2618#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_LOCALTIME_R)
2619 time_ptr = localtime_r (&thetime, &time_tm);
2620#else
2621 time_ptr = localtime (&thetime);
2622#endif
2623 }
2624 if (time_ptr != NULL)
2625 {
2626 status = strftime (AsciiTime, 80,
2627#ifdef SH_USE_XML
2628 _("%Y-%m-%dT%H:%M:%S"),
2629#else
2630 _("[%Y-%m-%dT%H:%M:%S]"),
2631#endif
2632 time_ptr);
2633
2634 if ( (status == 0) || (status == 80) )
2635 sl_strlcpy(buffer, _(deftime), len);
2636 else
2637 sl_strlcpy(buffer, AsciiTime, len);
2638 SL_RETURN( buffer, _("sh_unix_gmttime"));
2639 }
2640
2641 /* last resort
2642 */
2643 sl_strlcpy(buffer, _(deftime), len);
2644 SL_RETURN( buffer, _("sh_unix_gmttime"));
2645}
2646
2647
2648char * sh_unix_getUIDdir (int level, uid_t uid, char * out, size_t len)
2649{
2650 struct passwd * tempres;
2651 int status = 0;
2652
2653#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
2654 struct passwd pwd;
2655 char * buffer;
2656#endif
2657 char errbuf[SH_ERRBUF_SIZE];
2658
2659 SL_ENTER(_("sh_unix_getUIDdir"));
2660
2661#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
2662 buffer = SH_ALLOC(SH_PWBUF_SIZE);
2663 sh_getpwuid_r(uid, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
2664#else
2665 errno = 0;
2666 tempres = sh_getpwuid(uid);
2667 status = errno;
2668#endif
2669
2670 if (tempres == NULL) {
2671 sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
2672 sh_error_message(status, errbuf, sizeof(errbuf)),
2673 _("getpwuid"), (long) uid, _("completely missing"));
2674#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
2675 SH_FREE(buffer);
2676#endif
2677 SL_RETURN( NULL, _("sh_unix_getUIDdir"));
2678 }
2679
2680 if (tempres->pw_dir != NULL) {
2681 sl_strlcpy(out, tempres->pw_dir, len);
2682#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
2683 SH_FREE(buffer);
2684#endif
2685 SL_RETURN( out, _("sh_unix_getUIDdir"));
2686 } else {
2687 sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
2688 sh_error_message(status, errbuf, sizeof(errbuf)),
2689 _("getpwuid"), (long) uid, _("pw_dir"));
2690#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
2691 SH_FREE(buffer);
2692#endif
2693 SL_RETURN( NULL, _("sh_unix_getUIDdir"));
2694 }
2695}
2696
2697/* ------------------- Caching ----------------*/
2698#include "zAVLTree.h"
2699
2700#define CACHE_GID 0
2701#define CACHE_UID 1
2702
2703struct user_id {
2704 char * name;
2705 uid_t id;
2706 struct user_id * next;
2707};
2708
2709static struct user_id * uid_list = NULL;
2710static struct user_id * gid_list = NULL;
2711
2712SH_MUTEX_STATIC(mutex_cache, PTHREAD_MUTEX_INITIALIZER);
2713
2714static void sh_userid_free(struct user_id * item)
2715{
2716 while (item)
2717 {
2718 struct user_id * user = item;
2719 item = item->next;
2720
2721 SH_FREE(user->name);
2722 SH_FREE(user);
2723 }
2724 return;
2725}
2726
2727void sh_userid_destroy ()
2728{
2729 struct user_id * tmp_uid;
2730 struct user_id * tmp_gid;
2731
2732 SH_MUTEX_LOCK_UNSAFE(mutex_cache);
2733 tmp_gid = gid_list;
2734 gid_list = NULL;
2735 tmp_uid = uid_list;
2736 uid_list = NULL;
2737 SH_MUTEX_UNLOCK_UNSAFE(mutex_cache);
2738
2739 sh_userid_free(tmp_uid);
2740 sh_userid_free(tmp_gid);
2741 return;
2742}
2743
2744static void sh_userid_additem(struct user_id * list, struct user_id * item)
2745{
2746 if (list)
2747 {
2748 while (list && list->next)
2749 list = list->next;
2750 list->next = item;
2751 }
2752 return;
2753}
2754
2755static void sh_userid_add(uid_t id, char * username, int which)
2756{
2757 size_t len;
2758 struct user_id * user = SH_ALLOC(sizeof(struct user_id));
2759
2760 if (username)
2761 len = strlen(username) + 1;
2762 else
2763 len = 1;
2764
2765 user->name = SH_ALLOC(len);
2766 user->id = id;
2767 if (username)
2768 sl_strlcpy(user->name, username, len);
2769 else
2770 user->name[0] = '\0';
2771 user->next = NULL;
2772
2773 SH_MUTEX_LOCK(mutex_cache);
2774 if (which == CACHE_UID)
2775 {
2776 if (!uid_list)
2777 uid_list = user;
2778 else
2779 sh_userid_additem(uid_list, user);
2780 }
2781 else
2782 {
2783 if (!gid_list)
2784 gid_list = user;
2785 else
2786 sh_userid_additem(gid_list, user);
2787 }
2788 SH_MUTEX_UNLOCK(mutex_cache);
2789
2790 return;
2791}
2792
2793static char * sh_userid_search(struct user_id * list, uid_t id)
2794{
2795 while (list)
2796 {
2797 if (list->id == id)
2798 return list->name;
2799 list = list->next;
2800 }
2801 return NULL;
2802}
2803
2804static char * sh_userid_get (uid_t id, int which, char * out, size_t len)
2805{
2806 char * user = NULL;
2807
2808 SH_MUTEX_LOCK_UNSAFE(mutex_cache);
2809 if (which == CACHE_UID)
2810 user = sh_userid_search(uid_list, id);
2811 else
2812 user = sh_userid_search(gid_list, id);
2813 if (user)
2814 {
2815 sl_strlcpy(out, user, len);
2816 user = out;
2817 }
2818 SH_MUTEX_UNLOCK_UNSAFE(mutex_cache);
2819
2820 return user;
2821}
2822
2823/* --------- end caching code --------- */
2824
2825char * sh_unix_getUIDname (int level, uid_t uid, char * out, size_t len)
2826{
2827 struct passwd * tempres;
2828#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
2829 struct passwd pwd;
2830 char * buffer;
2831#endif
2832 int status = 0;
2833 char errbuf[SH_ERRBUF_SIZE];
2834 char * tmp;
2835
2836 SL_ENTER(_("sh_unix_getUIDname"));
2837
2838 tmp = sh_userid_get(uid, CACHE_UID, out, len);
2839
2840 if (tmp)
2841 {
2842 if (tmp[0] != '\0')
2843 {
2844 SL_RETURN( out, _("sh_unix_getUIDname"));
2845 }
2846 else
2847 {
2848 SL_RETURN( NULL, _("sh_unix_getUIDname"));
2849 }
2850 }
2851
2852#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
2853 buffer = SH_ALLOC(SH_PWBUF_SIZE);
2854 sh_getpwuid_r(uid, &pwd, buffer, SH_PWBUF_SIZE, &tempres);
2855#else
2856 errno = 0;
2857 tempres = sh_getpwuid(uid);
2858 status = errno;
2859#endif
2860
2861 if (tempres == NULL)
2862 {
2863 sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
2864 sh_error_message(status, errbuf, sizeof(errbuf)),
2865 _("getpwuid"), (long) uid, _("completely missing"));
2866#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
2867 SH_FREE(buffer);
2868#endif
2869 sh_userid_add(uid, NULL, CACHE_UID);
2870 SL_RETURN( NULL, _("sh_unix_getUIDname"));
2871 }
2872
2873
2874 if (tempres->pw_name != NULL)
2875 {
2876
2877 sl_strlcpy(out, tempres->pw_name, len);
2878 sh_userid_add(uid, out, CACHE_UID);
2879
2880#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
2881 SH_FREE(buffer);
2882#endif
2883
2884 SL_RETURN( out, _("sh_unix_getUIDname"));
2885 }
2886 else
2887 {
2888 sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_PWNULL,
2889 sh_error_message(status, errbuf, sizeof(errbuf)),
2890 _("getpwuid"), (long) uid, _("pw_user"));
2891#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
2892 SH_FREE(buffer);
2893#endif
2894 SL_RETURN( NULL, _("sh_unix_getUIDname"));
2895 }
2896 /* notreached */
2897}
2898
2899char * sh_unix_getGIDname (int level, gid_t gid, char * out, size_t len)
2900{
2901 struct group * tempres;
2902 int status = 0;
2903
2904#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
2905 struct group grp;
2906 char * buffer;
2907#endif
2908 char errbuf[SH_ERRBUF_SIZE];
2909 char * tmp;
2910
2911 SL_ENTER(_("sh_unix_getGIDname"));
2912
2913 tmp = sh_userid_get((uid_t)gid, CACHE_GID, out, len);
2914
2915 if (tmp)
2916 {
2917 if (tmp[0] != '\0')
2918 {
2919 SL_RETURN( out, _("sh_unix_getGIDname"));
2920 }
2921 else
2922 {
2923 SL_RETURN( NULL, _("sh_unix_getGIDname"));
2924 }
2925 }
2926
2927#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
2928 buffer = SH_ALLOC(SH_GRBUF_SIZE);
2929 status = sh_getgrgid_r(gid, &grp, buffer, SH_GRBUF_SIZE, &tempres);
2930#else
2931 errno = 0;
2932 tempres = sh_getgrgid(gid);
2933 status = errno;
2934#endif
2935
2936 if (status == ERANGE)
2937 {
2938 static int seen = 0;
2939
2940 if (seen == 0)
2941 {
2942 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
2943 sh_error_message(status, errbuf, sizeof(errbuf)),
2944 _("getgrgid"), (long) gid, _("line too long in group entry"));
2945 ++seen;
2946 }
2947
2948#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
2949 SH_FREE(buffer);
2950#endif
2951
2952 sh_userid_add(gid, NULL, CACHE_GID);
2953 SL_RETURN( NULL, _("sh_unix_getGIDname"));
2954 }
2955
2956 if (tempres == NULL)
2957 {
2958 sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
2959 sh_error_message(status, errbuf, sizeof(errbuf)),
2960 _("getgrgid"), (long) gid, _("completely missing"));
2961
2962#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
2963 SH_FREE(buffer);
2964#endif
2965
2966 sh_userid_add(gid, NULL, CACHE_GID);
2967 SL_RETURN( NULL, _("sh_unix_getGIDname"));
2968 }
2969
2970 if (tempres->gr_name != NULL)
2971 {
2972
2973 sl_strlcpy(out, tempres->gr_name, len);
2974 sh_userid_add((uid_t)gid, out, CACHE_GID);
2975
2976#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
2977 SH_FREE(buffer);
2978#endif
2979
2980 SL_RETURN( out, _("sh_unix_getGIDname"));
2981 }
2982 else
2983 {
2984 sh_error_handle (level, FIL__, __LINE__, EINVAL, MSG_E_GRNULL,
2985 sh_error_message(status, errbuf, sizeof(errbuf)),
2986 _("getgrgid"), (long) gid, _("gr_name"));
2987
2988#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
2989 SH_FREE(buffer);
2990#endif
2991
2992 SL_RETURN( NULL, _("sh_unix_getGIDname"));
2993 }
2994 /* notreached */
2995}
2996
2997int sh_unix_getUser ()
2998{
2999 char * p;
3000 uid_t seuid, sruid;
3001 char user[USER_MAX];
3002 char dir[SH_PATHBUF];
3003
3004 SL_ENTER(_("sh_unix_getUser"));
3005
3006 seuid = geteuid();
3007
3008 sh.effective.uid = seuid;
3009
3010 p = sh_unix_getUIDdir (SH_ERR_ERR, seuid, dir, sizeof(dir));
3011
3012 if (p == NULL)
3013 SL_RETURN((-1), _("sh_unix_getUser"));
3014 else
3015 {
3016 if (sl_strlen(p) >= SH_PATHBUF) {
3017 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, EINVAL, MSG_E_PWLONG,
3018 _("getpwuid"), (long) seuid, _("pw_home"));
3019 SL_RETURN((-1), _("sh_unix_getUser"));
3020 } else {
3021 sl_strlcpy ( sh.effective.home, p, SH_PATHBUF);
3022 }
3023 }
3024
3025 sruid = getuid();
3026
3027 sh.real.uid = sruid;
3028
3029 p = sh_unix_getUIDname (SH_ERR_ERR, sruid, user, sizeof(user));
3030 if (p == NULL)
3031 SL_RETURN((-1), _("sh_unix_getUser"));
3032 else
3033 {
3034 if (sl_strlen(p) >= USER_MAX) {
3035 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, EINVAL, MSG_E_PWLONG,
3036 _("getpwuid"), (long) sruid, _("pw_user"));
3037 SL_RETURN((-1), _("sh_unix_getUser"));
3038 } else {
3039 sl_strlcpy ( sh.real.user, p, USER_MAX);
3040 }
3041 }
3042
3043 p = sh_unix_getUIDdir (SH_ERR_ERR, sruid, dir, sizeof(dir));
3044
3045 if (p == NULL)
3046 SL_RETURN((-1), _("sh_unix_getUser"));
3047 else
3048 {
3049 if (sl_strlen(p) >= SH_PATHBUF) {
3050 sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, EINVAL, MSG_E_PWLONG,
3051 _("getpwuid"), (long) sruid, _("pw_home"));
3052 SL_RETURN((-1), _("sh_unix_getUser"));
3053 } else {
3054 sl_strlcpy ( sh.real.home, p, SH_PATHBUF);
3055 }
3056 }
3057
3058 SL_RETURN((0), _("sh_unix_getUser"));
3059
3060 /* notreached */
3061}
3062
3063
3064int sh_unix_getline (SL_TICKET fd, char * line, int sizeofline)
3065{
3066 register int count;
3067 register int n = 0;
3068 char c;
3069
3070 SL_ENTER(_("sh_unix_getline"));
3071
3072 if (sizeofline < 2) {
3073 line[0] = '\0';
3074 SL_RETURN((0), _("sh_unix_getline"));
3075 }
3076
3077 --sizeofline;
3078
3079 while (n < sizeofline) {
3080
3081 count = sl_read (fd, &c, 1);
3082
3083 /* end of file
3084 */
3085 if (count < 1) {
3086 line[n] = '\0';
3087 n = -1;
3088 break;
3089 }
3090
3091 if (/* c != '\0' && */ c != '\n') {
3092 line[n] = c;
3093 ++n;
3094 } else if (c == '\n') {
3095 if (n > 0) {
3096 line[n] = '\0';
3097 break;
3098 } else {
3099 line[n] = '\n'; /* get newline only if only char on line */
3100 ++n;
3101 line[n] = '\0';
3102 break;
3103 }
3104 } else {
3105 line[n] = '\0';
3106 break;
3107 }
3108
3109 }
3110
3111
3112 line[sizeofline] = '\0'; /* make sure line is terminated */
3113 SL_RETURN((n), _("sh_unix_getline"));
3114}
3115
3116
3117#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
3118
3119/**************************************************************
3120 *
3121 * --- FILE INFO ---
3122 *
3123 **************************************************************/
3124
3125#if (defined(__linux__) && (defined(HAVE_LINUX_EXT2_FS_H) || defined(HAVE_EXT2FS_EXT2_FS_H))) || defined(HAVE_STAT_FLAGS)
3126
3127#if defined(__linux__)
3128
3129/* --- Determine ext2fs file attributes. ---
3130 */
3131#include <sys/ioctl.h>
3132#if defined(HAVE_EXT2FS_EXT2_FS_H)
3133#include <ext2fs/ext2_fs.h>
3134#else
3135#include <linux/ext2_fs.h>
3136#endif
3137
3138/* __linux__ includes */
3139#endif
3140
3141static
3142int sh_unix_getinfo_attr (char * name,
3143 unsigned long * flags,
3144 char * c_attr,
3145 int fd, struct stat * buf)
3146{
3147
3148/* TAKEN FROM:
3149 *
3150 * lsattr.c - List file attributes on an ext2 file system
3151 *
3152 * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
3153 * Laboratoire MASI, Institut Blaise Pascal
3154 * Universite Pierre et Marie Curie (Paris VI)
3155 *
3156 * This file can be redistributed under the terms of the GNU General
3157 * Public License
3158 */
3159
3160#ifdef HAVE_STAT_FLAGS
3161
3162 SL_ENTER(_("sh_unix_getinfo_attr"));
3163
3164 *flags = 0;
3165
3166 /* cast to void to avoid compiler warning about unused parameters */
3167 (void) fd;
3168 (void) name;
3169
3170#ifdef UF_NODUMP
3171 if (buf->st_flags & UF_NODUMP) {
3172 *flags |= UF_NODUMP;
3173 c_attr[0] = 'd';
3174 }
3175#endif
3176#ifdef UF_IMMUTABLE
3177 if (buf->st_flags & UF_IMMUTABLE) {
3178 *flags |= UF_IMMUTABLE;
3179 c_attr[1] = 'i';
3180 }
3181#endif
3182#ifdef UF_APPEND
3183 if (buf->st_flags & UF_APPEND) {
3184 *flags |= UF_APPEND;
3185 c_attr[2] = 'a';
3186 }
3187#endif
3188#ifdef UF_NOUNLINK
3189 if (buf->st_flags & UF_NOUNLINK) {
3190 *flags |= UF_NOUNLINK;
3191 c_attr[3] = 'u';
3192 }
3193#endif
3194#ifdef UF_OPAQUE
3195 if (buf->st_flags & UF_OPAQUE) {
3196 *flags |= UF_OPAQUE;
3197 c_attr[4] = 'o';
3198 }
3199#endif
3200#ifdef SF_ARCHIVED
3201 if (buf->st_flags & SF_ARCHIVED) {
3202 *flags |= SF_ARCHIVED;
3203 c_attr[5] = 'R';
3204 }
3205
3206#endif
3207#ifdef SF_IMMUTABLE
3208 if (buf->st_flags & SF_IMMUTABLE) {
3209 *flags |= SF_IMMUTABLE;
3210 c_attr[6] = 'I';
3211 }
3212#endif
3213#ifdef SF_APPEND
3214 if (buf->st_flags & SF_APPEND) {
3215 *flags |= SF_APPEND;
3216 c_attr[7] = 'A';
3217 }
3218#endif
3219#ifdef SF_NOUNLINK
3220 if (buf->st_flags & SF_NOUNLINK) {
3221 *flags |= SF_NOUNLINK;
3222 c_attr[8] = 'U';
3223 }
3224#endif
3225
3226 /* ! HAVE_STAT_FLAGS */
3227#else
3228
3229#ifdef HAVE_EXT2_IOCTLS
3230 int /* fd, */ r, f;
3231
3232 SL_ENTER(_("sh_unix_getinfo_attr"));
3233
3234 *flags = 0;
3235 (void) buf;
3236
3237 /* open() -> aud_open() R.Wichmann
3238 fd = aud_open (FIL__, __LINE__, SL_YESPRIV, name, O_RDONLY|O_NONBLOCK, 0);
3239 */
3240
3241 if (fd == -1 || name == NULL)
3242 SL_RETURN(-1, _("sh_unix_getinfo_attr"));
3243
3244
3245 r = ioctl (fd, EXT2_IOC_GETFLAGS, &f);
3246 /* sl_close_fd (FIL__, __LINE__, fd); */
3247
3248 if (r == -1)
3249 SL_RETURN(-1, _("sh_unix_getinfo_attr"));
3250
3251 if (f == 0)
3252 SL_RETURN(0, _("sh_unix_getinfo_attr"));
3253
3254 *flags = f;
3255
3256/* ! HAVE_EXT2_IOCTLS */
3257#else
3258
3259 SL_ENTER(_("sh_unix_getinfo_attr"));
3260
3261 *flags = 0; /* modified by R.Wichmann */
3262
3263/* ! HAVE_EXT2_IOCTLS */
3264#endif
3265/*
3266 * END
3267 *
3268 * lsattr.c - List file attributes on an ext2 file system
3269 */
3270
3271 if (*flags == 0)
3272 goto theend;
3273
3274#ifdef EXT2_SECRM_FL
3275 if ( (*flags & EXT2_SECRM_FL) != 0 ) c_attr[0] = 's';
3276#endif
3277#ifdef EXT2_UNRM_FL
3278 if ( (*flags & EXT2_UNRM_FL) != 0 ) c_attr[1] = 'u';
3279#endif
3280#ifdef EXT2_SYNC_FL
3281 if ( (*flags & EXT2_SYNC_FL) != 0 ) c_attr[2] = 'S';
3282#endif
3283#ifdef EXT2_IMMUTABLE_FL
3284 if ( (*flags & EXT2_IMMUTABLE_FL) != 0) c_attr[3] = 'i';
3285#endif
3286#ifdef EXT2_APPEND_FL
3287 if ( (*flags & EXT2_APPEND_FL) != 0 ) c_attr[4] = 'a';
3288#endif
3289#ifdef EXT2_NODUMP_FL
3290 if ( (*flags & EXT2_NODUMP_FL) != 0 ) c_attr[5] = 'd';
3291#endif
3292#ifdef EXT2_NOATIME_FL
3293 if ( (*flags & EXT2_NOATIME_FL) != 0) c_attr[6] = 'A';
3294#endif
3295#ifdef EXT2_COMPR_FL
3296 if ( (*flags & EXT2_COMPR_FL) != 0 ) c_attr[7] = 'c';
3297#endif
3298
3299#ifdef EXT2_TOPDIR_FL
3300 if ( (*flags & EXT2_TOPDIR_FL) != 0 ) c_attr[8] = 'T';
3301#endif
3302#ifdef EXT2_DIRSYNC_FL
3303 if ( (*flags & EXT2_DIRSYNC_FL) != 0 ) c_attr[9] = 'D';
3304#endif
3305#ifdef EXT2_NOTAIL_FL
3306 if ( (*flags & EXT2_NOTAIL_FL) != 0 ) c_attr[10] = 't';
3307#endif
3308#ifdef EXT2_JOURNAL_DATA_FL
3309 if ( (*flags & EXT2_JOURNAL_DATA_FL) != 0) c_attr[11] = 'j';
3310#endif
3311
3312 theend:
3313 /* ext2 */
3314#endif
3315
3316 c_attr[12] = '\0';
3317
3318 SL_RETURN(0, _("sh_unix_getinfo_attr"));
3319}
3320
3321/* defined(__linux__) || defined(HAVE_STAT_FLAGS) */
3322#endif
3323
3324/* determine file type
3325 */
3326static
3327int sh_unix_getinfo_type (struct stat * buf,
3328 ShFileType * type,
3329 char * c_mode)
3330{
3331 SL_ENTER(_("sh_unix_getinfo_type"));
3332
3333 if ( S_ISREG(buf->st_mode) ) {
3334 (*type) = SH_FILE_REGULAR;
3335 c_mode[0] = '-';
3336 }
3337 else if ( S_ISLNK(buf->st_mode) ) {
3338 (*type) = SH_FILE_SYMLINK;
3339 c_mode[0] = 'l';
3340 }
3341 else if ( S_ISDIR(buf->st_mode) ) {
3342 (*type) = SH_FILE_DIRECTORY;
3343 c_mode[0] = 'd';
3344 }
3345 else if ( S_ISCHR(buf->st_mode) ) {
3346 (*type) = SH_FILE_CDEV;
3347 c_mode[0] = 'c';
3348 }
3349 else if ( S_ISBLK(buf->st_mode) ) {
3350 (*type) = SH_FILE_BDEV;
3351 c_mode[0] = 'b';
3352 }
3353 else if ( S_ISFIFO(buf->st_mode) ) {
3354 (*type) = SH_FILE_FIFO;
3355 c_mode[0] = '|';
3356 }
3357 else if ( S_ISSOCK(buf->st_mode) ) {
3358 (*type) = SH_FILE_SOCKET;
3359 c_mode[0] = 's';
3360 }
3361 else if ( S_ISDOOR(buf->st_mode) ) {
3362 (*type) = SH_FILE_DOOR;
3363 c_mode[0] = 'D';
3364 }
3365 else if ( S_ISPORT(buf->st_mode) ) {
3366 (*type) = SH_FILE_PORT;
3367 c_mode[0] = 'P';
3368 }
3369 else {
3370 (*type) = SH_FILE_UNKNOWN;
3371 c_mode[0] = '?';
3372 }
3373
3374 SL_RETURN(0, _("sh_unix_getinfo_type"));
3375}
3376
3377int sh_unix_get_ftype(char * fullpath)
3378{
3379 char c_mode[CMODE_SIZE];
3380 struct stat buf;
3381 ShFileType type;
3382 int res;
3383
3384 SL_ENTER(_("sh_unix_get_ftype"));
3385
3386 res = retry_lstat(FIL__, __LINE__, fullpath, &buf);
3387
3388 if (res < 0)
3389 SL_RETURN(SH_FILE_UNKNOWN, _("sh_unix_getinfo_type"));
3390
3391 sh_unix_getinfo_type (&buf, &type, c_mode);
3392
3393 SL_RETURN(type, _("sh_unix_get_ftype"));
3394}
3395
3396
3397static
3398int sh_unix_getinfo_mode (struct stat *buf,
3399 unsigned int * mode,
3400 char * c_mode)
3401{
3402
3403 SL_ENTER(_("sh_unix_getinfo_mode"));
3404
3405 (*mode) = buf->st_mode;
3406
3407 /* make 'ls'-like string */
3408
3409 if ( (buf->st_mode & S_IRUSR) != 0 ) c_mode[1] = 'r';
3410 if ( (buf->st_mode & S_IWUSR) != 0 ) c_mode[2] = 'w';
3411 if ( (buf->st_mode & S_IXUSR) != 0 ) {
3412 if ((buf->st_mode & S_ISUID) != 0 ) c_mode[3] = 's';
3413 else c_mode[3] = 'x';
3414 } else {
3415 if ((buf->st_mode & S_ISUID) != 0 ) c_mode[3] = 'S';
3416 }
3417
3418 if ( (buf->st_mode & S_IRGRP) != 0 ) c_mode[4] = 'r';
3419 if ( (buf->st_mode & S_IWGRP) != 0 ) c_mode[5] = 'w';
3420 if ( (buf->st_mode & S_IXGRP) != 0 ) {
3421 if ((buf->st_mode & S_ISGID) != 0 ) c_mode[6] = 's';
3422 else c_mode[6] = 'x';
3423 } else {
3424 if ((buf->st_mode & S_ISGID) != 0 ) c_mode[6] = 'S';
3425 }
3426
3427 if ( (buf->st_mode & S_IROTH) != 0 ) c_mode[7] = 'r';
3428 if ( (buf->st_mode & S_IWOTH) != 0 ) c_mode[8] = 'w';
3429#ifdef S_ISVTX /* not POSIX */
3430 if ( (buf->st_mode & S_IXOTH) != 0 ) {
3431 if ((buf->st_mode & S_ISVTX) != 0 ) c_mode[9] = 't';
3432 else c_mode[9] = 'x';
3433 } else {
3434 if ((buf->st_mode & S_ISVTX) != 0 ) c_mode[9] = 'T';
3435 }
3436#else
3437 if ( (buf->st_mode & S_IXOTH) != 0 ) c_mode[9] = 'x';
3438#endif
3439
3440 SL_RETURN(0, _("sh_unix_getinfo_mode"));
3441}
3442
3443
3444long IO_Limit = 0;
3445
3446void sh_unix_io_pause ()
3447{
3448 long runtime;
3449 float someval;
3450 unsigned long sometime;
3451
3452 if (IO_Limit == 0)
3453 {
3454 return;
3455 }
3456 else
3457 {
3458 runtime = (long) (time(NULL) - sh.statistics.time_start);
3459
3460 if (runtime > 0 && (long)(sh.statistics.bytes_hashed/runtime) > IO_Limit)
3461 {
3462 someval = sh.statistics.bytes_hashed - (IO_Limit * runtime);
3463 someval /= (float) IO_Limit;
3464 if (someval < 1.0)
3465 {
3466 someval *= 1000; /* milliseconds in a second */
3467 sometime = (unsigned long) someval;
3468 retry_msleep(0, sometime);
3469 }
3470 else
3471 {
3472 sometime = (unsigned long) someval;
3473 retry_msleep (sometime, 0);
3474 }
3475 }
3476 }
3477 return;
3478}
3479
3480int sh_unix_set_io_limit (const char * c)
3481{
3482 long val;
3483
3484 SL_ENTER(_("sh_unix_set_io_limit"));
3485
3486 val = strtol (c, (char **)NULL, 10);
3487 if (val < 0)
3488 sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
3489 _("set I/O limit"), c);
3490
3491 val = (val < 0 ? 0 : val);
3492
3493 IO_Limit = val * 1024;
3494 SL_RETURN( 0, _("sh_unix_set_io_limit"));
3495}
3496
3497/* obtain file info
3498 */
3499extern int flag_err_debug;
3500
3501#include "sh_ignore.h"
3502
3503int sh_unix_checksum_size (char * filename, off_t size, int is_max_size,
3504 char * fileHash, int alert_timeout, SL_TICKET fd, unsigned long mask)
3505{
3506 file_type * tmpFile;
3507 int status;
3508
3509 SL_ENTER(_("sh_unix_checksum_size"));
3510
3511 tmpFile = SH_ALLOC(sizeof(file_type));
3512 tmpFile->link_path = NULL;
3513
3514 if (sh.flag.checkSum != SH_CHECK_INIT)
3515 {
3516 /* lookup file in database */
3517 if (is_max_size == S_TRUE) {
3518 status = sh_hash_get_it (filename, tmpFile, NULL);
3519 if ((status != 0) || (tmpFile->size > size)) {
3520 goto out;
3521 }
3522 } else {
3523 tmpFile->size = size;
3524 }
3525 }
3526 else
3527 {
3528 tmpFile->size = size;
3529 }
3530
3531 /* if last <= current get checksum */
3532 if (tmpFile->size <= size)
3533 {
3534 char hashbuf[KEYBUF_SIZE];
3535 UINT64 local_length = (UINT64) (tmpFile->size < 0 ? 0 : tmpFile->size);
3536 if (sh.flag.opts == S_TRUE) sh_tiger_set_hashtype_mask(mask);
3537 sl_strlcpy(fileHash,
3538 sh_tiger_generic_hash (filename, fd, &(local_length),
3539 alert_timeout, hashbuf, sizeof(hashbuf)),
3540 KEY_LEN+1);
3541
3542 /* return */
3543 if (tmpFile->link_path) SH_FREE(tmpFile->link_path);
3544 SH_FREE(tmpFile);
3545 SL_RETURN( 0, _("sh_unix_checksum_size"));
3546 }
3547
3548 out:
3549 if (tmpFile->link_path) SH_FREE(tmpFile->link_path);
3550 SH_FREE(tmpFile);
3551 sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
3552 SL_RETURN( -1, _("sh_unix_checksum_size"));
3553}
3554
3555/********************************************************
3556 * Search rotated logfile
3557 */
3558extern char * sh_rotated_log_search(const char * path, struct stat * buf);
3559
3560int sh_check_rotated_log (const char * path,
3561 UINT64 old_size, UINT64 old_inode, const char * old_hash, unsigned long mask)
3562{
3563 struct stat obuf;
3564 UINT64 length_nolim = TIGER_NOLIM;
3565 int retval = S_FALSE;
3566
3567 if (old_size != length_nolim)
3568 {
3569 char hashbuf[KEYBUF_SIZE];
3570 char * rotated_file;
3571
3572 obuf.st_ino = old_inode;
3573 rotated_file = sh_rotated_log_search(path, &obuf);
3574
3575 if (rotated_file && (0 != strcmp(path, rotated_file)))
3576 {
3577 SL_TICKET fd = sl_open_fastread (FIL__, __LINE__, rotated_file, SL_YESPRIV);
3578 if (!SL_ISERROR(fd))
3579 {
3580 sh_unix_checksum_size (rotated_file, old_size, S_FALSE,
3581 hashbuf, 120 /* alert_timeout */, fd, mask);
3582
3583 sl_close(fd);
3584
3585 if (strncmp (old_hash, hashbuf, KEY_LEN) == 0) {
3586 retval = S_TRUE;
3587 }
3588 }
3589 SH_FREE(rotated_file);
3590 }
3591 }
3592 return retval;
3593}
3594
3595
3596int sh_unix_check_selinux = S_FALSE;
3597int sh_unix_check_acl = S_FALSE;
3598
3599#ifdef USE_ACL
3600
3601#include <sys/acl.h>
3602static char * sh_unix_getinfo_acl (char * path, int fd, struct stat * buf)
3603{
3604 /* system.posix_acl_access, system.posix_acl_default
3605 */
3606 char * out = NULL;
3607 char * collect = NULL;
3608 char * tmp;
3609 char * out_compact;
3610 ssize_t len;
3611 acl_t result;
3612
3613 SL_ENTER(_("sh_unix_getinfo_acl"));
3614
3615 result = (fd == -1) ?
3616 acl_get_file (path, ACL_TYPE_ACCESS) :
3617 acl_get_fd (fd);
3618
3619 if (result)
3620 {
3621 out = acl_to_text (result, &len);
3622 if (out && (len > 0)) {
3623 out_compact = sh_util_acl_compact (out, len);
3624 acl_free(out);
3625 if (out_compact)
3626 {
3627 collect = sh_util_strconcat (_("acl_access:"), out_compact, NULL);
3628 SH_FREE(out_compact);
3629 }
3630 }
3631 acl_free(result);
3632 }
3633
3634
3635 if ( S_ISDIR(buf->st_mode) )
3636 {
3637 result = acl_get_file (path, ACL_TYPE_DEFAULT);
3638
3639 if (result)
3640 {
3641 out = acl_to_text (result, &len);
3642 if (out && (len > 0)) {
3643 out_compact = sh_util_acl_compact (out, len);
3644 acl_free(out);
3645 if (out_compact) {
3646 if (collect) {
3647 tmp = sh_util_strconcat (_("acl_default:"),
3648 out_compact, ":", collect, NULL);
3649 SH_FREE(collect);
3650 }
3651 else {
3652 tmp = sh_util_strconcat (_("acl_default:"), out_compact, NULL);
3653 }
3654 SH_FREE(out_compact);
3655 collect = tmp;
3656 }
3657 }
3658 acl_free(result);
3659 }
3660 }
3661
3662 SL_RETURN((collect),_("sh_unix_getinfo_acl"));
3663}
3664#endif
3665
3666#ifdef USE_XATTR
3667
3668#include <attr/xattr.h>
3669static char * sh_unix_getinfo_xattr_int (char * path, int fd, char * name)
3670{
3671 char * out = NULL;
3672 char * tmp = NULL;
3673 size_t size = 256;
3674 ssize_t result;
3675
3676 SL_ENTER(_("sh_unix_getinfo_xattr_int"));
3677
3678 out = SH_ALLOC(size);
3679
3680 result = (fd == -1) ?
3681 lgetxattr (path, name, out, size-1) :
3682 fgetxattr (fd, name, out, size-1);
3683
3684 if (result == -1 && errno == ERANGE)
3685 {
3686 SH_FREE(out);
3687 result = (fd == -1) ?
3688 lgetxattr (path, name, NULL, 0) :
3689 fgetxattr (fd, name, NULL, 0);
3690 size = result + 1;
3691 out = SH_ALLOC(size);
3692 result = (fd == -1) ?
3693 lgetxattr (path, name, out, size-1) :
3694 fgetxattr (fd, name, out, size-1);
3695 }
3696
3697 if ((result > 0) && ((size_t)result < size))
3698 {
3699 out[size-1] = '\0';
3700 tmp = out;
3701 }
3702 else
3703 {
3704 SH_FREE(out);
3705 }
3706
3707 SL_RETURN((tmp),_("sh_unix_getinfo_xattr_int"));
3708}
3709
3710
3711static char * sh_unix_getinfo_xattr (char * path, int fd, struct stat * buf)
3712{
3713 /* system.posix_acl_access, system.posix_acl_default, security.selinux
3714 */
3715 char * tmp;
3716 char * out = NULL;
3717 char * collect = NULL;
3718
3719 SL_ENTER(_("sh_unix_getinfo_xattr"));
3720
3721#ifdef USE_ACL
3722 /*
3723 * we need the acl_get_fd/acl_get_file functions, getxattr will only
3724 * yield the raw bytes
3725 */
3726 if (sh_unix_check_acl == S_TRUE)
3727 {
3728 out = sh_unix_getinfo_acl(path, fd, buf);
3729
3730 if (out)
3731 {
3732 collect = out;
3733 }
3734 }
3735#else
3736 (void) buf;
3737#endif
3738
3739 if (sh_unix_check_selinux == S_TRUE)
3740 {
3741 out = sh_unix_getinfo_xattr_int(path, fd, _("security.selinux"));
3742
3743 if (out)
3744 {
3745 if (collect) {
3746 tmp = sh_util_strconcat(_("selinux:"), out, ":", collect, NULL);
3747 SH_FREE(collect);
3748 }
3749 else {
3750 tmp = sh_util_strconcat(_("selinux:"), out, NULL);
3751 }
3752 SH_FREE(out);
3753 collect = tmp;
3754 }
3755 }
3756
3757 SL_RETURN((collect),_("sh_unix_getinfo_xattr"));
3758}
3759#endif
3760
3761#ifdef USE_XATTR
3762int sh_unix_setcheckselinux (const char * c)
3763{
3764 int i;
3765 SL_ENTER(_("sh_unix_setcheckselinux"));
3766 i = sh_util_flagval(c, &(sh_unix_check_selinux));
3767
3768 SL_RETURN(i, _("sh_unix_setcheckselinux"));
3769}
3770#endif
3771
3772#ifdef USE_ACL
3773int sh_unix_setcheckacl (const char * c)
3774{
3775 int i;
3776 SL_ENTER(_("sh_unix_setcheckacl"));
3777 i = sh_util_flagval(c, &(sh_unix_check_acl));
3778
3779 SL_RETURN(i, _("sh_unix_setcheckacl"));
3780}
3781#endif
3782
3783#ifdef HAVE_LIBZ
3784#include <zlib.h>
3785#endif
3786
3787
3788static void * sh_dummy_filename;
3789static void * sh_dummy_tmp;
3790static void * sh_dummy_tmp2;
3791
3792int sh_unix_getinfo (int level, const char * filename, file_type * theFile,
3793 char * fileHash, int policy)
3794{
3795 char timestr[81];
3796 long runtim;
3797 struct stat buf;
3798 struct stat lbuf;
3799 struct stat fbuf;
3800 volatile int stat_return;
3801 volatile int stat_errno = 0;
3802
3803 ShFileType type;
3804 unsigned int mode;
3805 char * tmp;
3806 char * tmp2;
3807
3808 char * linknamebuf;
3809 volatile int linksize;
3810
3811 extern int get_the_fd (SL_TICKET ticket);
3812
3813 volatile SL_TICKET rval_open;
3814 volatile int err_open = 0;
3815
3816 volatile int fd;
3817 volatile int fstat_return;
3818 volatile int fstat_errno = 0;
3819 volatile int try = 0;
3820
3821 sh_string * content = NULL;
3822
3823 time_t tend;
3824 time_t tstart;
3825
3826
3827 char * path = NULL;
3828
3829 volatile int alert_timeout = 120;
3830
3831 path = theFile->fullpath;
3832
3833 SL_ENTER(_("sh_unix_getinfo"));
3834
3835 if (!MODI_INITIALIZED(theFile->check_flags))
3836 {
3837 tmp2 = sh_util_safe_name (theFile->fullpath);
3838 SH_MUTEX_LOCK(mutex_thread_nolog);
3839 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGPATH,
3840 _("Uninitialized check mask"), _("sh_unix_getinfo"),
3841 tmp2);
3842 SH_MUTEX_UNLOCK(mutex_thread_nolog);
3843 SH_FREE(tmp2);
3844 SL_RETURN((-1),_("sh_unix_getinfo"));
3845 }
3846
3847 /* Take the address to keep gcc from putting it into a register.
3848 * Avoids the 'clobbered by longjmp' warning.
3849 */
3850 sh_dummy_filename = (void *) &filename;
3851 sh_dummy_tmp = (void *) &tmp;
3852 sh_dummy_tmp2 = (void *) &tmp2;
3853
3854 /* --- Stat the file, and get checksum. ---
3855 */
3856 tstart = time(NULL);
3857
3858 stat_return = retry_lstat (FIL__, __LINE__,
3859 path /* theFile->fullpath */, &buf);
3860
3861 if (stat_return)
3862 stat_errno = errno;
3863
3864 theFile->link_path = NULL;
3865
3866 try_again:
3867
3868 fd = -1;
3869 fstat_return = -1;
3870 rval_open = -1;
3871
3872 if (stat_return == 0 && S_ISREG(buf.st_mode))
3873 {
3874 rval_open = sl_open_fastread (FIL__, __LINE__,
3875 path /* theFile->fullpath */, SL_YESPRIV);
3876 if (SL_ISERROR(rval_open))
3877 {
3878 char * stale = sl_check_stale();
3879
3880 if (stale)
3881 {
3882 SH_MUTEX_LOCK(mutex_thread_nolog);
3883 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, err_open, MSG_E_SUBGEN,
3884 stale, _("sh_unix_getinfo_open"));
3885 SH_MUTEX_UNLOCK(mutex_thread_nolog);
3886 }
3887
3888 if (errno == EBADF && try == 0) /* obsolete, but we keep this, just in case */
3889 {
3890 ++try;
3891 goto try_again;
3892 }
3893 err_open = errno;
3894 }
3895
3896 alert_timeout = 120; /* this is per 8K block now ! */
3897
3898 if (path[1] == 'p' && path[5] == '/' && path[2] == 'r' &&
3899 path[3] == 'o' && path[4] == 'c' && path[0] == '/')
3900 {
3901 /* seven is magic */
3902 alert_timeout = 7;
3903 }
3904
3905 fd = get_the_fd(rval_open);
3906 }
3907
3908 tend = time(NULL);
3909
3910 /* An unprivileged user may slow lstat/open to a crawl
3911 * with clever path/symlink setup
3912 */
3913 if ((tend - tstart) > (time_t) /* 60 */ 6)
3914 {
3915 tmp2 = sh_util_safe_name (theFile->fullpath);
3916 SH_MUTEX_LOCK(mutex_thread_nolog);
3917 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_TOOLATE,
3918 (long)(tend - tstart), tmp2);
3919 SH_MUTEX_UNLOCK(mutex_thread_nolog);
3920 SH_FREE(tmp2);
3921 }
3922
3923 if (fd >= 0)
3924 {
3925 fstat_return = retry_fstat (FIL__, __LINE__, fd, &fbuf);
3926
3927 if (fstat_return)
3928 {
3929 char * stale;
3930
3931 fstat_errno = errno;
3932
3933 stale = sl_check_stale();
3934
3935 if (stale)
3936 {
3937 SH_MUTEX_LOCK(mutex_thread_nolog);
3938 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, fstat_errno,
3939 MSG_E_SUBGEN,
3940 stale, _("sh_unix_getinfo_fstat"));
3941 SH_MUTEX_UNLOCK(mutex_thread_nolog);
3942 }
3943
3944 if (try == 0) /* obsolete, but we keep this, just in case */
3945 {
3946 ++try;
3947 sl_close(rval_open);
3948 goto try_again;
3949 }
3950 }
3951 }
3952 else
3953 {
3954 fd = -1;
3955 }
3956
3957
3958 /* --- case 1: lstat failed ---
3959 */
3960 if (stat_return != 0)
3961 {
3962 stat_return = errno;
3963 if (!SL_ISERROR(rval_open))
3964 sl_close(rval_open);
3965 if (sh.flag.checkSum == SH_CHECK_INIT ||
3966 (sh_hash_have_it (theFile->fullpath) >= 0 &&
3967 (!SH_FFLAG_REPORTED_SET(theFile->file_reported))))
3968 {
3969 if (S_FALSE == sh_ignore_chk_del(theFile->fullpath)) {
3970 int flags = sh_hash_getflags (theFile->fullpath);
3971
3972 if ((flags >= 0) && (flags & SH_FFLAG_ENOENT) == 0) {
3973 char errbuf[SH_ERRBUF_SIZE];
3974 uid_t euid;
3975 (void) sl_get_euid(&euid);
3976 tmp2 = sh_util_safe_name (theFile->fullpath);
3977 SH_MUTEX_LOCK(mutex_thread_nolog);
3978 sh_error_handle (level, FIL__, __LINE__, stat_return, MSG_FI_STAT,
3979 _("lstat"),
3980 sh_error_message (stat_errno, errbuf, sizeof(errbuf)),
3981 (long) euid,
3982 tmp2);
3983 SH_MUTEX_UNLOCK(mutex_thread_nolog);
3984 SH_FREE(tmp2);
3985 sh_hash_set_flag (theFile->fullpath, SH_FFLAG_ENOENT);
3986 }
3987 }
3988 }
3989 SL_RETURN((-1),_("sh_unix_getinfo"));
3990 }
3991
3992 /* --- case 2: not a regular file ---
3993 */
3994 else if (! S_ISREG(buf.st_mode))
3995 {
3996 if (fileHash != NULL)
3997 sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
3998 }
3999
4000 /* --- case 3a: a regular file, fstat ok ---
4001 */
4002 else if (fstat_return == 0 &&
4003 buf.st_mode == fbuf.st_mode &&
4004 buf.st_ino == fbuf.st_ino &&
4005 buf.st_uid == fbuf.st_uid &&
4006 buf.st_gid == fbuf.st_gid &&
4007 buf.st_dev == fbuf.st_dev )
4008 {
4009 if (fileHash != NULL)
4010 {
4011 if ((theFile->check_flags & MODI_CHK) == 0 ||
4012 sh_restrict_this(theFile->fullpath, (UINT64) fbuf.st_size,
4013 (UINT64) fbuf.st_mode, rval_open))
4014 {
4015 sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
4016 }
4017 else if ((theFile->check_flags & MODI_PREL) != 0 &&
4018 S_TRUE == sh_prelink_iself(rval_open, fbuf.st_size,
4019 alert_timeout, theFile->fullpath))
4020 {
4021 if (0 != sh_prelink_run (theFile->fullpath,
4022 fileHash, alert_timeout, theFile->check_flags))
4023 sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
4024 }
4025 else
4026 {
4027 char hashbuf[KEYBUF_SIZE];
4028 UINT64 length_current = TIGER_NOLIM;
4029
4030 if (MODI_TXT_ENABLED(theFile->check_flags) && fbuf.st_size < (10 * SH_TXT_MAX))
4031 {
4032 sl_init_content (rval_open, fbuf.st_size);
4033 }
4034
4035 if (sh.flag.opts == S_TRUE) sh_tiger_set_hashtype_mask(theFile->check_flags);
4036 sl_strlcpy(fileHash,
4037 sh_tiger_generic_hash (theFile->fullpath,
4038 rval_open, &length_current,
4039 alert_timeout,
4040 hashbuf, sizeof(hashbuf)),
4041 KEY_LEN+1);
4042
4043 content = sl_get_content(rval_open);
4044 content = sh_string_copy(content);
4045
4046 if ((theFile->check_flags & MODI_SGROW) != 0)
4047 {
4048 /* Update size so it matches the one for which the checksum
4049 has been computed */
4050 fbuf.st_size = length_current;
4051 buf.st_size = fbuf.st_size;
4052 sl_rewind(rval_open);
4053 sh_unix_checksum_size (theFile->fullpath, length_current, S_TRUE,
4054 &fileHash[KEY_LEN + 1],
4055 alert_timeout, rval_open, theFile->check_flags);
4056 }
4057 }
4058 }
4059 }
4060
4061 /* --- case 3b: a regular file, fstat ok, but different ---
4062 */
4063 else if (fstat_return == 0 && S_ISREG(fbuf.st_mode))
4064 {
4065 memcpy (&buf, &fbuf, sizeof( struct stat ));
4066
4067 if (fileHash != NULL)
4068 {
4069 if ((theFile->check_flags & MODI_CHK) == 0 ||
4070 sh_restrict_this(theFile->fullpath, (UINT64) fbuf.st_size,
4071 (UINT64) fbuf.st_mode, rval_open))
4072 {
4073 sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
4074 }
4075 else if (policy == SH_LEVEL_PRELINK &&
4076 S_TRUE == sh_prelink_iself(rval_open, fbuf.st_size,
4077 alert_timeout, theFile->fullpath))
4078 {
4079 if (0 != sh_prelink_run (theFile->fullpath,
4080 fileHash, alert_timeout, theFile->check_flags))
4081 sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
4082 }
4083 else
4084 {
4085 char hashbuf[KEYBUF_SIZE];
4086 UINT64 length_current = TIGER_NOLIM;
4087
4088 if (MODI_TXT_ENABLED(theFile->check_flags) && fbuf.st_size < (10 * SH_TXT_MAX))
4089 {
4090 sl_init_content (rval_open, fbuf.st_size);
4091 }
4092
4093 if (sh.flag.opts == S_TRUE) sh_tiger_set_hashtype_mask(theFile->check_flags);
4094 sl_strlcpy(fileHash,
4095 sh_tiger_generic_hash (theFile->fullpath, rval_open,
4096 &length_current,
4097 alert_timeout,
4098 hashbuf, sizeof(hashbuf)),
4099 KEY_LEN + 1);
4100
4101 content = sl_get_content(rval_open);
4102 content = sh_string_copy(content);
4103
4104 if ((theFile->check_flags & MODI_SGROW) != 0)
4105 {
4106 /* Update size so it matches the one for which the checksum
4107 has been computed */
4108 fbuf.st_size = length_current;
4109 buf.st_size = fbuf.st_size;
4110 sl_rewind(rval_open);
4111 sh_unix_checksum_size (theFile->fullpath, length_current, S_TRUE,
4112 &fileHash[KEY_LEN + 1],
4113 alert_timeout, rval_open, theFile->check_flags);
4114 }
4115 }
4116 }
4117 }
4118
4119 /* --- case 4: a regular file, fstat failed ---
4120 */
4121
4122 else /* fstat_return != 0 or !S_ISREG(fbuf.st_mode) or open() failed */
4123 {
4124 uid_t euid;
4125
4126 if (fileHash != NULL)
4127 sl_strlcpy(fileHash, SH_KEY_NULL, KEY_LEN+1);
4128
4129 if ((theFile->check_flags & MODI_CHK) != 0)
4130 {
4131 tmp2 = sh_util_safe_name (theFile->fullpath);
4132
4133
4134 if (fd >= 0 && fstat_return != 0)
4135 {
4136 char errbuf[SH_ERRBUF_SIZE];
4137 (void) sl_get_euid(&euid);
4138
4139 SH_MUTEX_LOCK(mutex_thread_nolog);
4140 sh_error_handle (level, FIL__, __LINE__, stat_return, MSG_FI_STAT,
4141 _("fstat"),
4142 sh_error_message (fstat_errno, errbuf, sizeof(errbuf)),
4143 (long) euid,
4144 tmp2);
4145 SH_MUTEX_UNLOCK(mutex_thread_nolog);
4146 }
4147 else if (fd >= 0 && !S_ISREG(fbuf.st_mode))
4148 {
4149 SH_MUTEX_LOCK(mutex_thread_nolog);
4150 sh_error_handle (level, FIL__, __LINE__, fstat_errno,
4151 MSG_E_NOTREG, tmp2);
4152 SH_MUTEX_UNLOCK(mutex_thread_nolog);
4153 }
4154 else
4155 {
4156 char errbuf[SH_ERRBUF_SIZE];
4157 char errbuf2[SH_ERRBUF_SIZE];
4158 sl_strlcpy(errbuf, sl_error_string(rval_open), sizeof(errbuf));
4159 sh_error_message(err_open, errbuf2, sizeof(errbuf2));
4160 SH_MUTEX_LOCK(mutex_thread_nolog);
4161 sh_error_handle (level, FIL__, __LINE__, err_open,
4162 MSG_E_READ, errbuf, errbuf2, tmp2);
4163 SH_MUTEX_UNLOCK(mutex_thread_nolog);
4164 }
4165 SH_FREE(tmp2);
4166 }
4167 }
4168
4169
4170 /* --- Determine file type. ---
4171 */
4172 memset (theFile->c_mode, '-', CMODE_SIZE-1);
4173 theFile->c_mode[CMODE_SIZE-1] = '\0';
4174
4175 memset (theFile->link_c_mode, '-', CMODE_SIZE-1);
4176 theFile->link_c_mode[CMODE_SIZE-1] = '\0';
4177
4178 sh_unix_getinfo_type (&buf, &type, theFile->c_mode);
4179 theFile->type = type;
4180
4181#if defined(__linux__) || defined(HAVE_STAT_FLAGS)
4182
4183 /* --- Determine file attributes. ---
4184 */
4185 memset (theFile->c_attributes, '-', ATTRBUF_SIZE);
4186 theFile->c_attributes[ATTRBUF_USED] = '\0';
4187 theFile->attributes = 0;
4188
4189#if (defined(__linux__) && (defined(HAVE_LINUX_EXT2_FS_H) || defined(HAVE_EXT2FS_EXT2_FS_H))) || defined(HAVE_STAT_FLAGS)
4190 if (theFile->c_mode[0] != 'c' && theFile->c_mode[0] != 'b' &&
4191 theFile->c_mode[0] != 'l' )
4192 sh_unix_getinfo_attr(theFile->fullpath,
4193 &theFile->attributes, theFile->c_attributes,
4194 fd, &buf);
4195#endif
4196#endif
4197
4198#if defined(USE_XATTR) && defined(USE_ACL)
4199 if (sh_unix_check_selinux == S_TRUE || sh_unix_check_acl == S_TRUE)
4200 theFile->attr_string = sh_unix_getinfo_xattr (theFile->fullpath, fd, &buf);
4201#elif defined(USE_XATTR)
4202 if (sh_unix_check_selinux == S_TRUE)
4203 theFile->attr_string = sh_unix_getinfo_xattr (theFile->fullpath, fd, &buf);
4204#elif defined(USE_ACL)
4205 if (sh_unix_check_acl == S_TRUE)
4206 theFile->attr_string = sh_unix_getinfo_acl (theFile->fullpath, fd, &buf);
4207#else
4208 theFile->attr_string = NULL;
4209#endif
4210
4211 if (!SL_ISERROR(rval_open))
4212 sl_close(rval_open);
4213
4214
4215 /* --- I/O limit. ---
4216 */
4217 if (IO_Limit > 0)
4218 {
4219 runtim = (long) (time(NULL) - sh.statistics.time_start);
4220
4221 if (runtim > 0 && (long)(sh.statistics.bytes_hashed/runtim) > IO_Limit)
4222 retry_msleep(1, 0);
4223 }
4224
4225 /* --- Determine permissions. ---
4226 */
4227 sh_unix_getinfo_mode (&buf, &mode, theFile->c_mode);
4228
4229 /* --- Trivia. ---
4230 */
4231 theFile->dev = buf.st_dev;
4232 theFile->ino = buf.st_ino;
4233 theFile->mode = buf.st_mode;
4234 theFile->hardlinks = buf.st_nlink;
4235 theFile->owner = buf.st_uid;
4236 theFile->group = buf.st_gid;
4237 theFile->rdev = buf.st_rdev;
4238 theFile->size = buf.st_size;
4239 theFile->blksize = (unsigned long) buf.st_blksize;
4240 theFile->blocks = (unsigned long) buf.st_blocks;
4241 theFile->atime = buf.st_atime;
4242 theFile->mtime = buf.st_mtime;
4243 theFile->ctime = buf.st_ctime;
4244
4245
4246 /* --- Owner and group. ---
4247 */
4248
4249 if (NULL == sh_unix_getGIDname(SH_ERR_ALL, buf.st_gid, theFile->c_group, GROUP_MAX+1)) {
4250
4251 tmp2 = sh_util_safe_name (theFile->fullpath);
4252
4253 if (policy == SH_LEVEL_ALLIGNORE)
4254 {
4255 SH_MUTEX_LOCK(mutex_thread_nolog);
4256 sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, ENOENT,
4257 MSG_FI_NOGRP,
4258 (long) buf.st_gid, tmp2);
4259 SH_MUTEX_UNLOCK(mutex_thread_nolog);
4260 }
4261 else
4262 {
4263 SH_MUTEX_LOCK(mutex_thread_nolog);
4264 sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, ENOENT,
4265 MSG_FI_NOGRP,
4266 (long) buf.st_gid, tmp2);
4267 SH_MUTEX_UNLOCK(mutex_thread_nolog);
4268 }
4269 SH_FREE(tmp2);
4270 sl_snprintf(theFile->c_group, GROUP_MAX+1, "%d", (long) buf.st_gid);
4271 }
4272
4273
4274 if (NULL == sh_unix_getUIDname(SH_ERR_ALL, buf.st_uid, theFile->c_owner, USER_MAX+1)) {
4275
4276 tmp2 = sh_util_safe_name (theFile->fullpath);
4277
4278 if (policy == SH_LEVEL_ALLIGNORE)
4279 {
4280 SH_MUTEX_LOCK(mutex_thread_nolog);
4281 sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, ENOENT,
4282 MSG_FI_NOUSR,
4283 (long) buf.st_uid, tmp2);
4284 SH_MUTEX_UNLOCK(mutex_thread_nolog);
4285 }
4286 else
4287 {
4288 SH_MUTEX_LOCK(mutex_thread_nolog);
4289 sh_error_handle (ShDFLevel[SH_ERR_T_NAME], FIL__, __LINE__, ENOENT,
4290 MSG_FI_NOUSR,
4291 (long) buf.st_uid, tmp2);
4292 SH_MUTEX_UNLOCK(mutex_thread_nolog);
4293 }
4294 SH_FREE(tmp2);
4295 sl_snprintf(theFile->c_owner, USER_MAX+1, "%d", (long) buf.st_uid);
4296 }
4297
4298 /* --- Output the file. ---
4299 */
4300 if (flag_err_debug == S_TRUE)
4301 {
4302 tmp2 = sh_util_safe_name ((filename == NULL) ?
4303 theFile->fullpath : filename);
4304 (void) sh_unix_time(theFile->mtime, timestr, sizeof(timestr));
4305 SH_MUTEX_LOCK(mutex_thread_nolog);
4306 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_LIST,
4307 theFile->c_mode,
4308 theFile->hardlinks,
4309 theFile->c_owner,
4310 theFile->c_group,
4311 (unsigned long) theFile->size,
4312 timestr,
4313 tmp2);
4314 SH_MUTEX_UNLOCK(mutex_thread_nolog);
4315 SH_FREE(tmp2);
4316 }
4317
4318 /* --- Check for links. ---
4319 */
4320 if (theFile->c_mode[0] == 'l')
4321 {
4322 linknamebuf = SH_ALLOC(PATH_MAX);
4323
4324 /* flawfinder: ignore */
4325 linksize = readlink (theFile->fullpath, linknamebuf, PATH_MAX-1);
4326
4327 if (linksize < (PATH_MAX-1) && linksize >= 0)
4328 linknamebuf[linksize] = '\0';
4329 else
4330 linknamebuf[PATH_MAX-1] = '\0';
4331
4332 if (linksize < 0)
4333 {
4334 char errbuf[SH_ERRBUF_SIZE];
4335 linksize = errno;
4336 tmp2 = sh_util_safe_name (theFile->fullpath);
4337 SH_MUTEX_LOCK(mutex_thread_nolog);
4338 sh_error_handle (level, FIL__, __LINE__, linksize, MSG_FI_RDLNK,
4339 sh_error_message (linksize, errbuf, sizeof(errbuf)), tmp2);
4340 SH_MUTEX_UNLOCK(mutex_thread_nolog);
4341 SH_FREE(tmp2);
4342 SH_FREE(linknamebuf);
4343 theFile->link_path = sh_util_strdup("-");
4344 SL_RETURN((-1),_("sh_unix_getinfo"));
4345 }
4346
4347 if (linknamebuf[0] == '/')
4348 {
4349 theFile->link_path = sh_util_strdup (linknamebuf);
4350 }
4351 else
4352 {
4353 tmp = sh_util_dirname(theFile->fullpath);
4354 if (tmp) {
4355 theFile->link_path = SH_ALLOC(PATH_MAX);
4356 sl_strlcpy (theFile->link_path, tmp, PATH_MAX);
4357 SH_FREE(tmp);
4358 } else {
4359 theFile->link_path = SH_ALLOC(PATH_MAX);
4360 theFile->link_path[0] = '\0';
4361 }
4362 /*
4363 * Only attach '/' if not root directory. Handle "//", which
4364 * according to POSIX is implementation-defined, and may be
4365 * different from "/" (however, three or more '/' will collapse
4366 * to one).
4367 */
4368 tmp = theFile->link_path; while (*tmp == '/') ++tmp;
4369 if (*tmp != '\0')
4370 {
4371 sl_strlcat (theFile->link_path, "/", PATH_MAX);
4372 }
4373 sl_strlcat (theFile->link_path, linknamebuf, PATH_MAX);
4374 }
4375
4376 /* stat the link
4377 */
4378 stat_return = retry_lstat (FIL__, __LINE__, theFile->link_path, &lbuf);
4379
4380 /* check for error
4381 */
4382 if (stat_return != 0)
4383 {
4384 stat_return = errno;
4385 tmp = sh_util_safe_name (theFile->fullpath);
4386 tmp2 = sh_util_safe_name (theFile->link_path);
4387 if (stat_return != ENOENT)
4388 {
4389 uid_t euid;
4390 char errbuf[SH_ERRBUF_SIZE];
4391
4392 (void) sl_get_euid(&euid);
4393 SH_MUTEX_LOCK(mutex_thread_nolog);
4394 sh_error_handle (level, FIL__, __LINE__, stat_return,
4395 MSG_FI_STAT,
4396 _("lstat (link target)"),
4397 sh_error_message (stat_return,errbuf, sizeof(errbuf)),
4398 (long) euid,
4399 tmp2);
4400 SH_MUTEX_UNLOCK(mutex_thread_nolog);
4401 }
4402 else
4403 {
4404 /* a dangling link -- everybody seems to have plenty of them
4405 */
4406 SH_MUTEX_LOCK(mutex_thread_nolog);
4407 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DLNK,
4408 tmp, tmp2);
4409 SH_MUTEX_UNLOCK(mutex_thread_nolog);
4410 }
4411 theFile->linkisok = BAD;
4412 SH_FREE(tmp);
4413 SH_FREE(tmp2);
4414 SH_FREE(linknamebuf);
4415 /*
4416 * changed Tue Feb 10 16:16:13 CET 2004:
4417 * add dangling symlinks into database
4418 * SL_RETURN((-1),_("sh_unix_getinfo"));
4419 */
4420 theFile->linkmode = 0;
4421 SL_RETURN((0),_("sh_unix_getinfo"));
4422 }
4423
4424 theFile->linkisok = GOOD;
4425
4426
4427 /* --- Determine file type. ---
4428 */
4429 sh_unix_getinfo_type (&lbuf, &type, theFile->link_c_mode);
4430 theFile->type = type;
4431
4432 /* --- Determine permissions. ---
4433 */
4434 sh_unix_getinfo_mode (&lbuf, &mode, theFile->link_c_mode);
4435 theFile->linkmode = lbuf.st_mode;
4436
4437 /* --- Output the link. ---
4438 */
4439 if (theFile->linkisok == GOOD)
4440 {
4441 tmp2 = sh_util_safe_name (linknamebuf);
4442 SH_MUTEX_LOCK(mutex_thread_nolog);
4443 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_LLNK,
4444 theFile->link_c_mode, tmp2);
4445 SH_MUTEX_UNLOCK(mutex_thread_nolog);
4446 SH_FREE(tmp2);
4447 }
4448 SH_FREE(linknamebuf);
4449 }
4450 else /* not a link, theFile->c_mode[0] != 'l' */
4451 {
4452 if (content)
4453 {
4454#ifdef HAVE_LIBZ
4455 unsigned long clen;
4456 unsigned char * compressed;
4457#ifdef HAVE_COMPRESSBOUND
4458 clen = compressBound(sh_string_len(content));
4459#else
4460 if (sh_string_len(content) > 10*SH_TXT_MAX)
4461 clen = SH_TXT_MAX;
4462 else
4463 clen = 13 + (int)(1.0001*sh_string_len(content));
4464#endif
4465 compressed = SH_ALLOC(clen);
4466 if (Z_OK == compress(compressed, &clen,
4467 (unsigned char *) sh_string_str(content),
4468 sh_string_len(content)))
4469 {
4470 if (clen < SH_TXT_MAX)
4471 {
4472 sh_util_base64_enc_alloc (&(theFile->link_path),
4473 (char *) compressed, clen);
4474 }
4475 else
4476 {
4477 char tmsg[128];
4478 char * tpath = sh_util_safe_name (theFile->fullpath);
4479 sl_snprintf(tmsg, sizeof(tmsg),
4480 _("compressed file too large (%lu bytes)"),
4481 clen);
4482 SH_MUTEX_LOCK(mutex_thread_nolog);
4483 sh_error_handle (SH_ERR_WARN, FIL__, __LINE__, -1,
4484 MSG_E_SUBGPATH, tmsg,
4485 _("sh_unix_getinfo"), tpath);
4486 SH_MUTEX_UNLOCK(mutex_thread_nolog);
4487 SH_FREE(tpath);
4488 }
4489 }
4490 SH_FREE(compressed);
4491#endif
4492 sh_string_destroy(&content);
4493 }
4494 }
4495 SL_RETURN((0),_("sh_unix_getinfo"));
4496}
4497
4498/* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
4499#endif
4500
4501int sh_unix_unlock(char * lockfile, char * flag)
4502{
4503 int error = 0;
4504
4505 SL_ENTER(_("sh_unix_unlock"));
4506
4507 if (sh.flag.isdaemon == S_FALSE && flag == NULL)
4508 SL_RETURN((0),_("sh_unix_unlock"));
4509
4510 /* --- Logfile is not locked to us. ---
4511 */
4512 if (sh.flag.islocked == BAD && flag != NULL)
4513 SL_RETURN((-1),_("sh_unix_unlock"));
4514
4515 /* --- Check whether the directory is secure. ---
4516 */
4517 if (0 != tf_trust_check (lockfile, SL_YESPRIV))
4518 SL_RETURN((-1),_("sh_unix_unlock"));
4519
4520 /* --- Delete the lock file. ---
4521 */
4522 error = retry_aud_unlink (FIL__, __LINE__, lockfile);
4523
4524 if (error == 0)
4525 {
4526 if (flag != NULL)
4527 sh.flag.islocked = BAD; /* not locked anymore */
4528 }
4529 else if (flag != NULL)
4530 {
4531 char errbuf[SH_ERRBUF_SIZE];
4532 error = errno;
4533 sh_error_handle ((-1), FIL__, __LINE__, error, MSG_E_UNLNK,
4534 sh_error_message(error, errbuf, sizeof(errbuf)),
4535 lockfile);
4536 SL_RETURN((-1),_("sh_unix_unlock"));
4537 }
4538 SL_RETURN((0),_("sh_unix_unlock"));
4539}
4540
4541int sh_unix_check_piddir (char * pidpath)
4542{
4543 static struct stat buf;
4544 int status = 0;
4545 char * pid_dir;
4546
4547 SL_ENTER(_("sh_unix_check_piddir"));
4548
4549 pid_dir = sh_util_dirname (pidpath);
4550
4551 status = retry_lstat (FIL__, __LINE__, pid_dir, &buf);
4552
4553 if (status < 0 && errno == ENOENT)
4554 {
4555 status = mkdir (pid_dir, 0777);
4556 if (status < 0)
4557 {
4558 sh_error_handle ((-1), FIL__, __LINE__, status,
4559 MSG_E_SUBGEN,
4560 _("Cannot create PID directory"),
4561 _("sh_unix_check_piddir"));
4562 SH_FREE(pid_dir);
4563 SL_RETURN((-1),_("sh_unix_check_piddir"));
4564 }
4565 }
4566 else if (!S_ISDIR(buf.st_mode))
4567 {
4568 sh_error_handle ((-1), FIL__, __LINE__, status,
4569 MSG_E_SUBGEN,
4570 _("Path of PID directory refers to a non-directory object"),
4571 _("sh_unix_check_piddir"));
4572 SH_FREE(pid_dir);
4573 SL_RETURN((-1),_("sh_unix_check_piddir"));
4574 }
4575 SH_FREE(pid_dir);
4576 SL_RETURN((0),_("sh_unix_check_piddir"));
4577}
4578
4579int sh_unix_lock (char * lockfile, char * flag)
4580{
4581 int filed;
4582 int errnum;
4583 char myPid[64];
4584 SL_TICKET fd;
4585 extern int get_the_fd (SL_TICKET ticket);
4586
4587 SL_ENTER(_("sh_unix_lock"));
4588
4589 sprintf (myPid, "%ld\n", (long) sh.pid); /* known to fit */
4590
4591 if (flag == NULL) /* PID file, check for directory */
4592 {
4593 if (0 != sh_unix_check_piddir (lockfile))
4594 {
4595 SL_RETURN((-1),_("sh_unix_lock"));
4596 }
4597 }
4598
4599 fd = sl_open_safe_rdwr (FIL__, __LINE__,
4600 lockfile, SL_YESPRIV); /* fails if file exists */
4601
4602 if (!SL_ISERROR(fd))
4603 {
4604 errnum = sl_write (fd, myPid, sl_strlen(myPid));
4605 filed = get_the_fd(fd);
4606 fchmod (filed, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
4607 sl_close (fd);
4608
4609 if (!SL_ISERROR(errnum))
4610 {
4611 if (flag != NULL)
4612 sh.flag.islocked = GOOD;
4613 SL_RETURN((0),_("sh_unix_lock"));
4614 }
4615 }
4616
4617 TPT((0, FIL__, __LINE__, _("msg=<open pid file failed>\n")));
4618 if (flag != NULL)
4619 sh.flag.islocked = BAD;
4620 SL_RETURN((-1),_("sh_unix_lock"));
4621
4622 /* notreached */
4623}
4624
4625
4626/* check whether file is locked
4627 */
4628int sh_unix_test_and_lock (char * filename, char * lockfile)
4629{
4630 static struct stat buf;
4631 int status = 0;
4632
4633
4634 SL_TICKET fd;
4635 char line_in[128];
4636
4637 SL_ENTER(_("sh_unix_test_and_lock"));
4638
4639 status = retry_lstat (FIL__, __LINE__, lockfile, &buf);
4640
4641 /* --- No lock file found, try to lock. ---
4642 */
4643
4644 if (status < 0 && errno == ENOENT)
4645 {
4646 if (0 == sh_unix_lock (lockfile, filename))
4647 {
4648 if (filename != NULL)
4649 sh.flag.islocked = GOOD;
4650 SL_RETURN((0),_("sh_unix_test_and_lock"));
4651 }
4652 else
4653 {
4654 sh_error_handle ((-1), FIL__, __LINE__, status,
4655 MSG_E_SUBGEN,
4656 (filename == NULL) ? _("Cannot create PID file (1)") : _("Cannot create lock file (1)"),
4657 _("sh_unix_test_and_lock"));
4658 SL_RETURN((-1),_("sh_unix_test_and_lock"));
4659 }
4660 }
4661 else if (status == 0 && buf.st_size == 0)
4662 {
4663 if (filename != NULL)
4664 sh.flag.islocked = GOOD;
4665 sh_unix_unlock (lockfile, filename);
4666 if (filename != NULL)
4667 sh.flag.islocked = BAD;
4668 if (0 == sh_unix_lock (lockfile, filename))
4669 {
4670 if (filename != NULL)
4671 sh.flag.islocked = GOOD;
4672 SL_RETURN((0),_("sh_unix_test_and_lock"));
4673 }
4674 else
4675 {
4676 sh_error_handle ((-1), FIL__, __LINE__, status,
4677 MSG_E_SUBGEN,
4678 (filename == NULL) ? _("Cannot create PID file (2)") : _("Cannot create lock file (2)"),
4679 _("sh_unix_test_and_lock"));
4680 SL_RETURN((-1),_("sh_unix_test_and_lock"));
4681 }
4682 }
4683
4684 /* --- Check on lock. ---
4685 */
4686
4687 if (status >= 0)
4688 {
4689 fd = sl_open_read (FIL__, __LINE__, lockfile, SL_YESPRIV);
4690 if (SL_ISERROR(fd))
4691 sh_error_handle ((-1), FIL__, __LINE__, fd,
4692 MSG_E_SUBGEN,
4693 (filename == NULL) ? _("Cannot open PID file for read") : _("Cannot open lock file for read"),
4694 _("sh_unix_test_and_lock"));
4695 }
4696 else
4697 fd = -1;
4698
4699 if (!SL_ISERROR(fd))
4700 {
4701 /* read the PID in the lock file
4702 */
4703 status = sl_read (fd, line_in, sizeof(line_in));
4704 line_in[sizeof(line_in)-1] = '\0';
4705
4706 /* convert to numeric
4707 */
4708 if (status > 0)
4709 {
4710 errno = 0;
4711 status = strtol(line_in, (char **)NULL, 10);
4712 if (errno == ERANGE || status <= 0)
4713 {
4714 sh_error_handle ((-1), FIL__, __LINE__, status,
4715 MSG_E_SUBGEN,
4716 (filename == NULL) ? _("Bad PID in PID file") : _("Bad PID in lock file"),
4717 _("sh_unix_test_and_lock"));
4718
4719 status = -1;
4720 }
4721 }
4722 else
4723 {
4724 sh_error_handle ((-1), FIL__, __LINE__, status,
4725 MSG_E_SUBGEN,
4726 (filename == NULL) ? _("Cannot read PID file") : _("Cannot read lock file"),
4727 _("sh_unix_test_and_lock"));
4728 }
4729 sl_close(fd);
4730
4731 if (status > 0 && (unsigned int) status == sh.pid)
4732 {
4733 if (filename != NULL)
4734 sh.flag.islocked = GOOD;
4735 SL_RETURN((0),_("sh_unix_test_and_lock"));
4736 }
4737
4738
4739 /* --- Check whether the process exists. ---
4740 */
4741 if (status > 0)
4742 {
4743 errno = 0;
4744 status = aud_kill (FIL__, __LINE__, status, 0);
4745
4746 /* Does not exist, so remove the stale lock
4747 * and create a new one.
4748 */
4749 if (status < 0 && errno == ESRCH)
4750 {
4751 if (filename != NULL)
4752 sh.flag.islocked = GOOD;
4753 if (0 != sh_unix_unlock(lockfile, filename) && (filename !=NULL))
4754 sh.flag.islocked = BAD;
4755 else
4756 {
4757 if (0 == sh_unix_lock (lockfile, filename))
4758 {
4759 if (filename != NULL)
4760 sh.flag.islocked = GOOD;
4761 SL_RETURN((0),_("sh_unix_test_and_lock"));
4762 }
4763 else
4764 {
4765 sh_error_handle ((-1), FIL__, __LINE__, status,
4766 MSG_E_SUBGEN,
4767 (filename == NULL) ? _("Cannot create PID file (3)") : _("Cannot create lock file (3)"),
4768 _("sh_unix_test_and_lock"));
4769 }
4770 if (filename != NULL)
4771 sh.flag.islocked = BAD;
4772 }
4773 }
4774 else
4775 {
4776 sh_error_handle ((-1), FIL__, __LINE__, status,
4777 MSG_E_SUBGEN,
4778 (filename == NULL) ? _("Cannot remove stale PID file, PID may be a running process") : _("Cannot remove stale lock file, PID may be a running process"),
4779 _("sh_unix_test_and_lock"));
4780 if (filename != NULL)
4781 sh.flag.islocked = BAD;
4782 }
4783 }
4784 }
4785 SL_RETURN((-1),_("sh_unix_testlock"));
4786}
4787
4788/* write the PID file
4789 */
4790int sh_unix_write_pid_file()
4791{
4792 return sh_unix_test_and_lock(NULL, sh.srvlog.alt);
4793}
4794
4795/* write lock for filename
4796 */
4797int sh_unix_write_lock_file(char * filename)
4798{
4799 size_t len;
4800 int res;
4801 char * lockfile;
4802
4803 if (filename == NULL)
4804 return (-1);
4805
4806 len = sl_strlen(filename);
4807 if (sl_ok_adds(len, 6))
4808 len += 6;
4809 lockfile = SH_ALLOC(len);
4810 sl_strlcpy(lockfile, filename, len);
4811 sl_strlcat(lockfile, _(".lock"), len);
4812 res = sh_unix_test_and_lock(filename, lockfile);
4813 SH_FREE(lockfile);
4814 return res;
4815}
4816
4817/* rm lock for filename
4818 */
4819int sh_unix_rm_lock_file(char * filename)
4820{
4821 size_t len;
4822 int res;
4823 char * lockfile;
4824
4825 if (filename == NULL)
4826 return (-1);
4827
4828 len = sl_strlen(filename);
4829 if (sl_ok_adds(len, 6))
4830 len += 6;
4831 lockfile = SH_ALLOC(len);
4832 sl_strlcpy(lockfile, filename, len);
4833 sl_strlcat(lockfile, _(".lock"), len);
4834
4835 res = sh_unix_unlock(lockfile, filename);
4836 SH_FREE(lockfile);
4837 return res;
4838}
4839
4840/* rm lock for filename
4841 */
4842int sh_unix_rm_pid_file()
4843{
4844 return sh_unix_unlock(sh.srvlog.alt, NULL);
4845}
4846
4847/* Test whether file exists
4848 */
4849int sh_unix_file_exists(char * path)
4850{
4851 struct stat buf;
4852
4853 SL_ENTER(_("sh_unix_file_exists"));
4854
4855 if (0 == retry_lstat(FIL__, __LINE__, path, &buf))
4856 SL_RETURN( S_TRUE, _("sh_unix_file_exists"));
4857 else
4858 SL_RETURN( S_FALSE, _("sh_unix_file_exists"));
4859}
4860
4861
4862/* Test whether file exists, is a character device, and allows read
4863 * access.
4864 */
4865int sh_unix_device_readable(int fd)
4866{
4867 struct stat buf;
4868
4869 SL_ENTER(_("sh_unix_device_readable"));
4870
4871 if (retry_fstat(FIL__, __LINE__, fd, &buf) == -1)
4872 SL_RETURN( (-1), _("sh_unix_device_readable"));
4873 else if ( S_ISCHR(buf.st_mode) && 0 != (S_IROTH & buf.st_mode) )
4874 SL_RETURN( (0), _("sh_unix_device_readable"));
4875 else
4876 SL_RETURN( (-1), _("sh_unix_device_readable"));
4877}
4878
4879static char preq[16];
4880
4881/* return true if database is remote
4882 */
4883int file_is_remote ()
4884{
4885 static int init = 0;
4886 struct stat buf;
4887
4888 SL_ENTER(_("file_is_remote"));
4889
4890 if (init == 0)
4891 {
4892 sl_strlcpy(preq, _("REQ_FROM_SERVER"), 16);
4893 ++init;
4894 }
4895 if (0 == sl_strncmp (sh.data.path, preq, 15))
4896 {
4897 if (sh.data.path[15] != '\0') /* should be start of path */
4898 {
4899 if (0 == stat(&(sh.data.path[15]), &buf))
4900 {
4901 SL_RETURN( S_FALSE, _("file_is_remote"));
4902 }
4903 else
4904 {
4905 char * tmp = sh_util_safe_name (&(sh.data.path[15]));
4906 sh_error_handle((-1), FIL__, __LINE__, S_FALSE, MSG_E_SUBGPATH,
4907 _("No local baseline database at expected path"),
4908 _("file_is_remote"),
4909 tmp);
4910 SH_FREE(tmp);
4911 }
4912 }
4913 else
4914 {
4915 sh_error_handle((-1), FIL__, __LINE__, S_FALSE, MSG_E_SUBGEN,
4916 _("No local baseline database path known"),
4917 _("file_is_remote"));
4918 }
4919 SL_RETURN( S_TRUE, _("file_is_remote"));
4920 }
4921 SL_RETURN( S_FALSE, _("file_is_remote"));
4922}
4923
4924/* Return the path to the configuration/database file.
4925 */
4926char * file_path(char what, char flag)
4927{
4928 static int init = 0;
4929
4930 SL_ENTER(_("file_path"));
4931
4932 if (init == 0)
4933 {
4934 sl_strlcpy(preq, _("REQ_FROM_SERVER"), 16);
4935 ++init;
4936 }
4937
4938 switch (what)
4939 {
4940
4941 case 'C':
4942 if (0 == sl_strncmp (sh.conf.path, preq, 15))
4943 {
4944#if defined(SH_WITH_SERVER)
4945 if (sh.flag.isserver == S_TRUE && sl_strlen(sh.conf.path) == 15)
4946 SL_RETURN( NULL, _("file_path"));
4947 if (sh.flag.isserver == S_TRUE)
4948 SL_RETURN( &(sh.conf.path[15]), _("file_path"));
4949#endif
4950 if (flag == 'R')
4951 SL_RETURN( preq, _("file_path"));
4952 if (flag == 'I')
4953 {
4954 if (sl_strlen(sh.conf.path) == 15)
4955 SL_RETURN( NULL, _("file_path"));
4956 else
4957 SL_RETURN( &(sh.conf.path[15]), _("file_path"));
4958 }
4959 SL_RETURN ( preq, _("file_path"));
4960 }
4961 else
4962 SL_RETURN( sh.conf.path, _("file_path"));
4963 /* break; *//* unreachable */
4964
4965 case 'D':
4966 if (0 == sl_strncmp (sh.data.path, preq, 15))
4967 {
4968 if (flag == 'R')
4969 SL_RETURN( preq, _("file_path"));
4970 if (flag == 'W' && sl_strlen(sh.data.path) == 15)
4971 SL_RETURN (NULL, _("file_path"));
4972 if (flag == 'W')
4973 SL_RETURN( &(sh.data.path[15]), _("file_path"));
4974 }
4975 else
4976 SL_RETURN( sh.data.path, _("file_path"));
4977 break;
4978
4979 default:
4980 SL_RETURN( NULL, _("file_path"));
4981 }
4982
4983 return NULL; /* notreached */
4984}
4985/************************************************/
4986/**** Mlock Utilities ****/
4987/************************************************/
4988
4989#include <limits.h>
4990
4991int sh_unix_pagesize()
4992{
4993 int pagesize = 4096;
4994#if defined(_SC_PAGESIZE)
4995 pagesize = sysconf(_SC_PAGESIZE);
4996#elif defined(_SC_PAGE_SIZE)
4997 pagesize = sysconf(_SC_PAGE_SIZE);
4998#elif defined(HAVE_GETPAGESIZE)
4999 pagesize = getpagesize();
5000#elif defined(PAGESIZE)
5001 pagesize = PAGESIZE;
5002#endif
5003
5004 return ((pagesize > 0) ? pagesize : 4096);
5005}
5006
5007typedef struct sh_page_lt {
5008 unsigned long page_start;
5009 int page_refcount;
5010 char file[64];
5011 int line;
5012 struct sh_page_lt * next;
5013} sh_page_l;
5014
5015sh_page_l * sh_page_locked = NULL;
5016volatile int page_locking = 0;
5017
5018unsigned long sh_unix_lookup_page (void * in_addr, size_t len, int * num_pages)
5019{
5020 int pagesize = sh_unix_pagesize();
5021 unsigned long addr = (unsigned long) in_addr;
5022
5023 unsigned long pagebase;
5024 unsigned long pagediff;
5025 unsigned long pagenum = addr / pagesize;
5026
5027 SL_ENTER(_("sh_unix_lookup_page"));
5028#if 0
5029 fprintf(stderr, "mlock: --> base %ld, pagenum: %ld\n",
5030 addr, pagenum);
5031#endif
5032
5033 /* address of first page
5034 */
5035 pagebase = pagenum * pagesize;
5036
5037 /* number of pages
5038 */
5039 pagediff = (addr + len) - pagebase;
5040 pagenum = pagediff / pagesize;
5041 if (pagenum * pagesize < pagediff)
5042 ++pagenum;
5043
5044#if 0
5045 fprintf(stderr, "mlock: --> pagebase %ld, pagediff %ld, (addr + len) %ld\n",
5046 pagebase, pagediff, (addr + len));
5047#endif
5048
5049 *num_pages = pagenum;
5050 SL_RETURN((pagebase), _("sh_unix_lookup_page"));
5051}
5052
5053
5054#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
5055
5056SH_MUTEX_STATIC(mutex_mlock,PTHREAD_MUTEX_INITIALIZER);
5057
5058int sh_unix_mlock (const char * file, int line, void * in_addr, size_t len)
5059{
5060 int num_pages;
5061 int status = 0;
5062 int pagesize;
5063 sh_page_l * page_list;
5064 unsigned long addr;
5065#ifdef TEST_MLOCK
5066 int i = 0;
5067#endif
5068
5069 SL_ENTER(_("sh_unix_mlock"));
5070
5071 /* There's no cancellation point here, except if tracing is on
5072 */
5073 SH_MUTEX_LOCK_UNSAFE(mutex_mlock);
5074
5075 page_list = sh_page_locked;
5076
5077 if (0 != page_locking)
5078 {
5079 status = -1;
5080 goto exit_mlock;
5081 }
5082
5083 page_locking = 1;
5084
5085 pagesize = sh_unix_pagesize();
5086 addr = sh_unix_lookup_page (in_addr, len, &num_pages);
5087
5088#ifdef TEST_MLOCK
5089 fprintf(stderr, "mlock: addr %ld, base %ld, pages: %d, length %d\n",
5090 (unsigned long) in_addr, addr, num_pages, len);
5091#endif
5092
5093 /* increase refcount of locked pages
5094 * addr is first page; num_pages is #(consecutive pages) to lock
5095 */
5096
5097 while ((page_list != NULL) && (num_pages > 0))
5098 {
5099#ifdef TEST_MLOCK
5100 fprintf(stderr, "mlock: check page %d: %ld [%d]\n",
5101 i, page_list->page_start, page_list->page_refcount);
5102#endif
5103 if (page_list->page_start == addr)
5104 {
5105 page_list->page_refcount += 1;
5106 num_pages -= 1;
5107 addr += pagesize;
5108#ifdef TEST_MLOCK
5109 fprintf(stderr, "mlock: found page %d: %ld [%d], next page %ld\n",
5110 i, page_list->page_start, page_list->page_refcount, addr);
5111#endif
5112 }
5113#ifdef TEST_MLOCK
5114 ++i;
5115#endif
5116 page_list = page_list->next;
5117 }
5118
5119 /* mlock some more pages, if needed
5120 */
5121 while (num_pages > 0)
5122 {
5123#ifdef TEST_MLOCK
5124 fprintf(stderr, "mlock: lock page %d: mlock %ld [num_pages %d]\n",
5125 i, addr, num_pages);
5126 ++i;
5127#endif
5128 page_list = SH_ALLOC(sizeof(sh_page_l));
5129 page_list->page_start = addr;
5130 page_list->page_refcount = 1;
5131 sl_strlcpy(page_list->file, file, 64);
5132 page_list->line = line;
5133 status = mlock( (void *) addr, pagesize);
5134 if (status != 0)
5135 {
5136#ifdef TEST_MLOCK
5137 char errbuf[SH_ERRBUF_SIZE];
5138 fprintf(stderr, "mlock: error: %s\n",
5139 sh_error_message(errno, errbuf, sizeof(errbuf)));
5140#endif
5141 SH_FREE(page_list);
5142 page_locking = 0;
5143 goto exit_mlock;
5144 }
5145 page_list->next = sh_page_locked;
5146 sh_page_locked = page_list;
5147 num_pages -= 1;
5148 addr += pagesize;
5149 }
5150 page_locking = 0;
5151
5152 exit_mlock:
5153 SH_MUTEX_UNLOCK_UNSAFE(mutex_mlock);
5154
5155 SL_RETURN((status), _("sh_unix_mlock"));
5156}
5157#else
5158int sh_unix_mlock (const char * file, int line, void * in_addr, size_t len)
5159{
5160 (void) file; (void) line;
5161 (void) in_addr; (void) len;
5162 return -1;
5163}
5164#endif
5165
5166#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
5167int sh_unix_munlock (void * in_addr, size_t len)
5168{
5169 int num_pages;
5170 int unlocked;
5171 int status;
5172 int pagesize;
5173 sh_page_l * page_list;
5174 sh_page_l * page_last;
5175 unsigned long addr;
5176
5177 int test_count;
5178 int test_status;
5179 int test_pages;
5180
5181#ifdef TEST_MLOCK
5182 int i = 0;
5183#endif
5184
5185 SL_ENTER(_("sh_unix_munlock"));
5186
5187 /* There's no cancellation point here, except if tracing is on
5188 */
5189 SH_MUTEX_LOCK_UNSAFE(mutex_mlock);
5190
5191 unlocked = 0;
5192 status = 0;
5193 page_list = sh_page_locked;
5194
5195 if (0 != page_locking)
5196 {
5197 status = -1;
5198 goto exit_munlock;
5199 }
5200 page_locking = 1;
5201
5202 pagesize = sh_unix_pagesize();
5203 addr = sh_unix_lookup_page (in_addr, len, &num_pages);
5204
5205#ifdef TEST_MLOCK
5206 fprintf(stderr, "munlock: in_addr %ld, addr %ld, pages: %d, length %d\n",
5207 (unsigned long) in_addr, addr, num_pages, len);
5208#endif
5209
5210 test_pages = num_pages;
5211
5212 /* reduce refcount of locked pages
5213 * addr is first page; num_pages is #(consecutive pages) to lock
5214 */
5215 while ((page_list != NULL) && (num_pages > 0))
5216 {
5217#ifdef TEST_MLOCK
5218 fprintf(stderr, "munlock: page %d: %ld [%d]\n",
5219 i, page_list->page_start, page_list->page_refcount);
5220#endif
5221
5222 test_status = 0;
5223 for (test_count = 0; test_count < test_pages; ++test_count)
5224 {
5225 if (page_list->page_start == (addr + (test_count * pagesize)))
5226 {
5227 test_status = 1;
5228 break;
5229 }
5230 }
5231
5232 if (test_status == 1)
5233 {
5234 page_list->page_refcount -= 1;
5235 if (page_list->page_refcount == 0)
5236 {
5237 status = munlock ( (void *) addr, pagesize);
5238 ++unlocked;
5239 }
5240 num_pages -= 1;
5241#ifdef TEST_MLOCK
5242 fprintf(stderr,
5243 "munlock: page %d: %ld [refcount %d], refcount reduced\n",
5244 i, page_list->page_start, page_list->page_refcount);
5245#endif
5246 }
5247#ifdef TEST_MLOCK
5248 ++i;
5249#endif
5250 page_list = page_list->next;
5251 }
5252
5253#ifdef TEST_MLOCK
5254 i = 0;
5255#endif
5256
5257 if (unlocked > 0)
5258 {
5259 page_list = sh_page_locked;
5260 page_last = sh_page_locked;
5261
5262 while ((page_list != NULL) && (unlocked > 0))
5263 {
5264 if (page_list->page_refcount == 0)
5265 {
5266#ifdef TEST_MLOCK
5267 fprintf(stderr, "munlock: remove page %d: %ld [refcount %d]\n",
5268 i, page_list->page_start, page_list->page_refcount);
5269#endif
5270 if (page_last != page_list)
5271 {
5272 page_last->next = page_list->next;
5273 SH_FREE(page_list);
5274 page_list = page_last->next;
5275 }
5276 else
5277 {
5278 page_last = page_list->next;
5279 if (page_list == sh_page_locked)
5280 sh_page_locked = page_list->next;
5281 SH_FREE(page_list);
5282 page_list = page_last;
5283 }
5284 --unlocked;
5285 }
5286 else
5287 {
5288#ifdef TEST_MLOCK
5289 fprintf(stderr, "munlock: skip page %d: %ld [refcount %d]\n",
5290 i, page_list->page_start, page_list->page_refcount);
5291#endif
5292
5293 page_last = page_list;
5294 page_list = page_list->next;
5295 }
5296#ifdef TEST_MLOCK
5297 ++i;
5298#endif
5299 }
5300 }
5301
5302 page_locking = 0;
5303
5304 exit_munlock:
5305 SH_MUTEX_UNLOCK_UNSAFE(mutex_mlock);
5306 SL_RETURN((status), _("sh_unix_munlock"));
5307}
5308#else
5309int sh_unix_munlock (void * in_addr, size_t len)
5310{
5311 (void) in_addr; (void) len;
5312 return -1;
5313}
5314#endif
5315
5316int sh_unix_count_mlock()
5317{
5318 unsigned int i = 0;
5319 char str[32][64];
5320 sh_page_l * page_list;
5321
5322 SL_ENTER(_("sh_unix_count_mlock"));
5323
5324#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
5325 /* There's no cancellation point here, except if tracing is on
5326 */
5327 SH_MUTEX_LOCK_UNSAFE(mutex_mlock);
5328#endif
5329
5330 page_list = sh_page_locked;
5331
5332 while (page_list != NULL)
5333 {
5334#ifdef WITH_TPT
5335 if (i < 32)
5336 sl_snprintf(str[i], 64, _("file: %s line: %d page: %d"),
5337 page_list->file, page_list->line, i+1);
5338#endif
5339 page_list = page_list->next;
5340 ++i;
5341 }
5342
5343#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
5344 SH_MUTEX_UNLOCK_UNSAFE(mutex_mlock);
5345#endif
5346
5347#ifdef WITH_TPT
5348 {
5349 unsigned int j = 0;
5350 while (j < i && j < 32)
5351 {
5352 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, j, MSG_E_SUBGEN,
5353 str[j], _("sh_unix_count_mlock"));
5354 ++j;
5355 }
5356 }
5357#endif
5358
5359 sl_snprintf(str[0], 64, _("%d pages locked"), i);
5360 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, i, MSG_E_SUBGEN,
5361 str[0], _("sh_unix_count_mlock"));
5362 SL_RETURN((i), _("sh_unix_count_mlock"));
5363}
5364
5365/************************************************/
5366/************************************************/
5367/**** Stealth Utilities ****/
5368/************************************************/
5369/************************************************/
5370#ifdef SH_STEALTH
5371
5372void sh_unix_xor_code (char * str, int len)
5373{
5374 register int i;
5375
5376 for (i = 0; i < len; ++i) str[i] ^= (char) XOR_CODE;
5377 return;
5378}
5379
5380#if !defined(SH_STEALTH_MICRO)
5381
5382
5383int hideout_hex_block(SL_TICKET fd, unsigned char * str, int len,
5384 unsigned long * bytes_read);
5385unsigned long first_hex_block(SL_TICKET fd, unsigned long * max);
5386
5387/*
5388 * --- Get hidden data from a block of hex data. ---
5389 */
5390int sh_unix_getline_stealth (SL_TICKET fd, char * str, int len)
5391{
5392 int add_off = 0, llen;
5393 static unsigned long off_data = 0;
5394 static unsigned long max_data = 0;
5395 static unsigned long bytes_read = 0;
5396 static int stealth_init = BAD;
5397
5398 SL_ENTER(_("sh_unix_getline_stealth"));
5399
5400 if (str == NULL)
5401 {
5402 off_data = 0;
5403 max_data = 0;
5404 bytes_read = 0;
5405 stealth_init = BAD;
5406 SL_RETURN(0, _("sh_unix_getline_stealth"));
5407 }
5408
5409 /* --- Initialize. ---
5410 */
5411 if (stealth_init == BAD)
5412 {
5413 off_data = first_hex_block(fd, &max_data);
5414 if (off_data == 0)
5415 {
5416 dlog(1, FIL__, __LINE__,
5417 _("The stealth config file does not contain any steganographically\nhidden data. This file must be an image file in _uncompressed_\npostscript format.\nTo hide data in it, use:\n samhain_stealth -s postscript_file orig_config_file\n mv postscript_file /path/to/config/file\n"));
5418 sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_P_NODATA,
5419 _("Stealth config file."));
5420 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
5421 }
5422 stealth_init = GOOD;
5423 max_data += off_data;
5424 }
5425
5426 /* --- Seek to proper position. ---
5427 */
5428 if (bytes_read >= max_data || add_off < 0)
5429 {
5430 dlog(1, FIL__, __LINE__,
5431 _("The capacity of the container image file for the stealth config file seems to be too small. Your config file is likely truncated.\n"));
5432 sh_error_handle ((-1), FIL__, __LINE__, EIO, MSG_P_NODATA,
5433 _("Stealth config file."));
5434 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
5435 }
5436 sl_seek(fd, off_data);
5437
5438 /* --- Read one line. ---
5439 */
5440 add_off = hideout_hex_block(fd, (unsigned char *) str, len, &bytes_read);
5441 off_data += add_off;
5442
5443 llen = sl_strlen(str);
5444 SL_RETURN(llen, _("sh_unix_getline_stealth"));
5445}
5446
5447int hideout_hex_block(SL_TICKET fd, unsigned char * str, int len,
5448 unsigned long * bytes_read)
5449{
5450
5451 register int i, j, k;
5452 unsigned char c, e;
5453 register int num;
5454 unsigned char mask[9] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
5455 unsigned long here = 0;
5456 unsigned long retval = 0;
5457 unsigned long bread = 0;
5458
5459 SL_ENTER(_("hideout_hex_block"));
5460
5461 ASSERT_RET((len > 1), _("len > 1"), (0));
5462
5463 --len;
5464
5465 i = 0;
5466 while (i < len)
5467 {
5468 for (j = 0; j < 8; ++j)
5469 {
5470
5471 /* --- Get a low byte, modify, read back. ---
5472 */
5473 for (k = 0; k < 2; ++k)
5474 {
5475 /* -- Skip whitespace. ---
5476 */
5477 c = ' ';
5478 do {
5479 do {
5480 num = sl_read (fd, &c, 1);
5481 } while (num == 0 && errno == EINTR);
5482 if (num > 0)
5483 ++here;
5484 else if (num == 0)
5485 SL_RETURN((0), _("hideout_hex_block"));
5486 else
5487 SL_RETURN((-1), _("hideout_hex_block"));
5488 } while (c == '\n' || c == '\t' || c == '\r' ||
5489 c == ' ');
5490 }
5491
5492
5493 /* --- e is the value of the low byte. ---
5494 */
5495 e = (unsigned char) sh_util_hexchar( c );
5496 if ((e & mask[7]) != 0) /* bit is set */
5497 str[i] |= mask[j];
5498 else /* bit is not set */
5499 str[i] &= ~mask[j];
5500
5501 bread += 1;
5502 }
5503 if (str[i] == '\n') break;
5504 ++i;
5505 }
5506
5507 if (i != 0)
5508 str[i] = '\0';
5509 else
5510 str[i+1] = '\0'; /* keep newline and terminate */
5511 retval += here;
5512 *bytes_read += (bread/8);
5513
5514 SL_RETURN(retval, _("hideout_hex_block"));
5515}
5516
5517/* --- Get offset of first data block. ---
5518 */
5519unsigned long first_hex_block(SL_TICKET fd, unsigned long * max)
5520{
5521 unsigned int i;
5522 long num = 1;
5523 unsigned long lnum;
5524 char c;
5525 int nothex = 0;
5526 unsigned long retval = 0;
5527 unsigned int this_line = 0;
5528 char theline[SH_BUFSIZE];
5529
5530 SL_ENTER(_("first_hex_block"));
5531
5532 *max = 0;
5533
5534 while (1)
5535 {
5536 theline[0] = '\0';
5537 this_line = 0;
5538 c = '\0';
5539 while (c != '\n' && this_line < (sizeof(theline)-1))
5540 {
5541 do {
5542 num = sl_read (fd, &c, 1);
5543 } while (num == 0 && errno == EINTR);
5544 if (num > 0)
5545 theline[this_line] = c;
5546 else
5547 SL_RETURN((0), _("first_hex_block"));
5548 ++this_line;
5549 }
5550 theline[this_line] = '\0';
5551
5552 /* not only 'newline' */
5553 if (this_line > 60)
5554 {
5555 nothex = 0;
5556 i = 0;
5557 while (nothex == 0 && i < (this_line-1))
5558 {
5559 if (! isxdigit((int)theline[i])) nothex = 1;
5560 ++i;
5561 }
5562 if (nothex == 1) retval += this_line;
5563 }
5564 else
5565 {
5566 nothex = 1;
5567 retval += this_line;
5568 }
5569
5570 if (nothex == 0)
5571 {
5572 *max = 0;
5573 do {
5574 do {
5575 num = sl_read (fd, theline, SH_BUFSIZE);
5576 } while (num == 0 && errno == EINTR);
5577 if (num > 0)
5578 {
5579 lnum = (unsigned long) num;
5580 for (i = 0; i < lnum; ++i)
5581 {
5582 c = theline[i];
5583 if (c == '\n' || c == '\t' || c == '\r' || c == ' ')
5584 ;
5585 else if (!isxdigit((int)c))
5586 break;
5587 else
5588 *max += 1;
5589 }
5590 }
5591 } while (num > 0);
5592
5593 *max /= 16;
5594 SL_RETURN((retval), _("first_hex_block"));
5595 }
5596
5597 }
5598 /* SL_RETURN((0), _("first_hex_block")); *//* unreachable */
5599}
5600
5601 /* if !defined(SH_STEALTH_MICRO) */
5602#endif
5603
5604 /* ifdef SH_STEALTH */
5605#endif
5606
5607/*
5608 * anti-debugger code
5609 */
5610#if defined(SCREW_IT_UP)
5611
5612#if defined(HAVE_PTHREAD)
5613
5614static pthread_key_t gSigtrapVariables_key;
5615static pthread_once_t gSigtrapVariables_key_once = PTHREAD_ONCE_INIT;
5616
5617static inline void make_gSigtrapVariables_key()
5618{
5619 (void) pthread_key_create(&gSigtrapVariables_key, free);
5620}
5621
5622struct sh_sigtrap_variables * sh_sigtrap_variables_get()
5623{
5624 void * ptr;
5625
5626 (void) pthread_once(&gSigtrapVariables_key_once, make_gSigtrapVariables_key);
5627
5628 ptr = pthread_getspecific(gSigtrapVariables_key);
5629 if (ptr == NULL) {
5630 ptr = calloc(1,sizeof(struct sh_sigtrap_variables));
5631 if (ptr == NULL) {
5632 return NULL;
5633 }
5634 (void) pthread_setspecific(gSigtrapVariables_key, ptr);
5635 }
5636
5637 return (struct sh_sigtrap_variables *) ptr;
5638}
5639
5640/* !defined(HAVE_PTHREAD) */
5641#else
5642
5643static struct sh_sigtrap_variables global_sigtrap_variables;
5644struct sh_sigtrap_variables * sh_sigtrap_variables_get()
5645{
5646 return &global_sigtrap_variables;
5647}
5648
5649#endif
5650
5651int sh_sigtrap_max_duration_set (const char * str)
5652{
5653 /* For security (prevent reloading with larger value)
5654 * this value can only be set once.
5655 */
5656 static int once = 0;
5657 int i;
5658
5659 SL_ENTER(_("sh_sigtrap_max_duration_set"));
5660
5661 i = atoi (str);
5662
5663 if (i >= 0 && once == 0)
5664 {
5665 sh.sigtrap_max_duration = i;
5666 once = 1;
5667 }
5668 else
5669 {
5670 SL_RETURN ((-1), _("sh_sigtrap_max_duration_set"));
5671 }
5672 SL_RETURN( (0), _("sh_sigtrap_max_duration_set"));
5673}
5674
5675void sh_sigtrap_handler (int signum)
5676{
5677 struct sh_sigtrap_variables * sigtrap_variables;
5678 sigtrap_variables = sh_sigtrap_variables_get();
5679 if (sigtrap_variables == NULL) {
5680 /* Perhaps, it's better to not die, and to continue using Samhain,
5681 even if this part does not work. */
5682 return;
5683 }
5684
5685#ifdef HAVE_GETTIMEOFDAY
5686 {
5687 struct timeval tv;
5688 long difftv;
5689
5690 gettimeofday(&tv, NULL);
5691 difftv = (tv.tv_sec - sigtrap_variables->save_tv.tv_sec) * 1000000 +
5692 (tv.tv_usec - sigtrap_variables->save_tv.tv_usec);
5693 if (difftv > sh.sigtrap_max_duration)
5694 raise(SIGKILL);
5695 }
5696#endif
5697
5698 sigtrap_variables->not_traced = signum;
5699 /* cppcheck-suppress memleak */
5700 return;
5701}
5702#endif
Note: See TracBrowser for help on using the repository browser.