source: trunk/src/sh_unix.c@ 410

Last change on this file since 410 was 410, checked in by katerina, 12 years ago

Improvements for ticket #303 (option SetReportFile for writing out summary after file check).

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