source: trunk/src/sh_unix.c@ 170

Last change on this file since 170 was 170, checked in by katerina, 17 years ago

Plenty of compiler warnings fixed, SQL query length fixed, doc update.

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