source: trunk/src/sh_unix.c@ 487

Last change on this file since 487 was 487, checked in by katerina, 10 years ago

Fix for ticket #385 (self_test not working with exit on sigterm).

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