source: branches/samhain_3_1/src/sh_unix.c@ 541

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

Fix for ticket #372 (Replace obsolete smatch by clang in test suite).

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