source: trunk/src/sh_unix.c@ 103

Last change on this file since 103 was 102, checked in by rainer, 18 years ago

Flush before fork (fix for ticket #60), and fix for kcheck on 2.6.21 (ticket #63).

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