source: trunk/src/sh_unix.c@ 175

Last change on this file since 175 was 175, checked in by katerina, 16 years ago

Fix for ticket #114 (compile failure on win2k/cygwin)

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