source: trunk/src/sh_unix.c@ 12

Last change on this file since 12 was 10, checked in by rainer, 19 years ago

Support *BSD file flags

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