source: trunk/src/sh_unix.c@ 97

Last change on this file since 97 was 93, checked in by rainer, 18 years ago

Add check for PCI ROMs; fix ticket #51 (symlinks in root directory reported with leading double slash).

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