source: trunk/src/sh_unix.c@ 249

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

Code to track down originating site for ticket #163.

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