source: trunk/src/sh_unix.c@ 272

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

Enhance logfile monitoring (tickets #183, #184, #185).

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