source: trunk/src/sh_unix.c@ 111

Last change on this file since 111 was 107, checked in by rainer, 17 years ago

Fix incorrect handling of files with zero size in GrowingLogFiles.

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