source: trunk/src/sh_unix.c@ 251

Last change on this file since 251 was 251, checked in by katerina, 15 years ago

Improved logic for interpreting /proc/net/udp,tcp

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