source: trunk/src/sh_unix.c@ 136

Last change on this file since 136 was 134, checked in by rainer, 17 years ago

More thread-safety changes.

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