source: trunk/src/samhain.c@ 116

Last change on this file since 116 was 96, checked in by rainer, 18 years ago

Fix for ticket #54 (samhain_hide module does not work under kernel 2.6.20).

File size: 45.9 KB
RevLine 
[1]1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 1999, 2000 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 <unistd.h>
27#include <fcntl.h>
28
29/* samhainctl */
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <sys/wait.h>
33#include <signal.h>
34#include <errno.h>
35
36
37#if TIME_WITH_SYS_TIME
38#include <sys/time.h>
39#include <time.h>
40#else
41#if HAVE_SYS_TIME_H
42#include <sys/time.h>
43#else
44#include <time.h>
45#endif
46#endif
47
48#ifdef HAVE_MEMORY_H
49#include <memory.h>
50#endif
51
52#ifdef HAVE_SETPRIORITY
53#include <sys/resource.h>
54#endif
55
56#ifndef HAVE_LSTAT
57#define lstat stat
58#endif
59
60/* for FLT_EPSILON
61 */
62#include <float.h>
63
64#include "samhain.h"
65#include "sh_files.h"
66#include "sh_utils.h"
67#include "sh_error.h"
68#include "sh_unix.h"
69#include "sh_getopt.h"
70#include "sh_readconf.h"
71#include "sh_hash.h"
72
73#include "sh_mail.h"
74
75#include "sh_tiger.h"
76#include "sh_gpg.h"
77#include "sh_mem.h"
78#include "sh_forward.h"
79#include "sh_tools.h"
80#include "sh_hash.h"
81#if defined(WITH_EXTERNAL)
82#include "sh_extern.h"
83#endif
84#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
85#include "sh_modules.h"
86#include "sh_ignore.h"
87#include "sh_prelink.h"
88#endif
89
90#undef FIL__
91#define FIL__ _("samhain.c")
92
93
94/**************************************************
95 *
96 * Needed to compile the key into the code.
97 *
98 **************************************************/
99
100extern UINT32 ErrFlag[2];
101#include "sh_MK.h"
102
103/**************************************************
104 *
105 * Variables for signal handling.
106 *
107 **************************************************/
108
109volatile int sig_raised;
110volatile int sig_urgent;
111volatile int sig_debug_switch; /* SIGUSR1 */
112volatile int sig_suspend_switch; /* SIGUSR2 */
113volatile int sh_global_suspend_flag;
114volatile int sig_fresh_trail; /* SIGIOT */
115volatile int sig_config_read_again; /* SIGHUP */
116volatile int sig_terminate; /* SIGQUIT */
117volatile int sig_termfast; /* SIGTERM */
118volatile int sig_force_check; /* SIGTTOU */
119long int eintr__result;
120char sh_sig_msg[SH_MINIBUF];
121
122
123#ifdef SH_STEALTH
124/**************************************************
125 *
126 * The following set of functions is required for
127 * the 'stealth' mode.
128 *
129 **************************************************/
130
131#ifndef SH_MAX_GLOBS
132#define SH_MAX_GLOBS 16
133#endif
134
135#ifndef GLOB_LEN
136#define GLOB_LEN 511
137#endif
138
139char * globber(const char * str)
140{
141 size_t i;
142 size_t j;
143
144 static size_t items = 0;
145 static size_t count = 0;
146 static char glob[SH_MAX_GLOBS * (GLOB_LEN+1)];
147
148 if (str == NULL)
149 return NULL;
150 else
151 j = strlen(str);
152
153 ++items;
154
155 ASSERT((j <= GLOB_LEN), _("j <= GLOB_LEN"))
156
157 if (j > GLOB_LEN)
158 j = GLOB_LEN;
159
160 /* Overwrap the buffer.
161 */
162 if ( (count + j) >= (SH_MAX_GLOBS * (GLOB_LEN+1)))
163 {
164 count = 0;
165 items = 0;
166 }
167
168 for (i = 0; i < j; ++i)
169 {
170 if (str[i] != '\n' && str[i] != '\t' && str[i] != '\r' && str[i] != '"')
171 glob[count + i] = str[i] ^ XOR_CODE;
172 else
173 glob[count + i] = str[i];
174 }
175 glob[count + j] = '\0';
176
177 i = count;
178 count = count + j + 1;
179 return &glob[i];
180}
181
182void sh_do_encode (char * str, int len)
183{
184 register int i;
185
186 /* this is a symmetric operation
187 */
188 for (i = 0; i < len; ++i)
189 {
190 str[i] = str[i] ^ XOR_CODE;
191 }
192 return;
193}
194
195#endif
196
197/**************************************************
198 *
199 * Global variables.
200 *
201 **************************************************/
202
203sh_struct sh;
204/*@null@*/ sh_key_t * skey = NULL;
205
206extern unsigned char TcpFlag[8][PW_LEN+1];
207
208/**************************************************
209 *
210 * Initializing.
211 *
212 **************************************************/
213
214static int is_samhainctl_init = S_FALSE;
215
216static
217void sh_init (void)
218{
219 unsigned char * dez = NULL;
220 int i;
221#if defined(SH_WITH_MAIL)
222 char * p;
223 char q[SH_PATHBUF];
224#endif
225
226 SL_ENTER(_("sh_init"));
227
228#ifdef MKA_09
229 ErrFlag[0] |= (1 << 8);
230#endif
231#ifdef MKA_10
232 ErrFlag[0] |= (1 << 9);
233#endif
234#ifdef MKA_11
235 ErrFlag[0] |= (1 << 10);
236#endif
237#ifdef MKA_12
238 ErrFlag[0] |= (1 << 11);
239#endif
240#ifdef MKA_13
241 ErrFlag[0] |= (1 << 12);
242#endif
243#ifdef MKA_14
244 ErrFlag[0] |= (1 << 13);
245#endif
246#ifdef MKA_15
247 ErrFlag[0] |= (1 << 14);
248#endif
249#ifdef MKA_16
250 ErrFlag[0] |= (1 << 15);
251#endif
252
253 /* Signal handling.
254 */
255 sig_raised = 0;
256 sig_config_read_again = 0; /* SIGHUP */
257 sig_debug_switch = 0; /* SIGUSR1 */
258 sig_suspend_switch = 0; /* SIGUSR2 */
259 sh_global_suspend_flag = 0; /* SIGUSR2 */
260 sig_fresh_trail = 0; /* SIGIOT */
261 sig_terminate = 0; /* SIGQUIT */
262 sig_termfast = 0; /* SIGTERM */
263 sig_force_check = 0; /* SIGTTOU */
264 strcpy ( sh_sig_msg, _("None"));
265
266#ifdef MKB_01
267 ErrFlag[1] |= (1 << 0);
268#endif
269#ifdef MKB_02
270 ErrFlag[1] |= (1 << 1);
271#endif
272#ifdef MKB_03
273 ErrFlag[1] |= (1 << 2);
274#endif
275#ifdef MKB_04
276 ErrFlag[1] |= (1 << 3);
277#endif
278#ifdef MKB_05
279 ErrFlag[1] |= (1 << 4);
280#endif
281#ifdef MKB_06
282 ErrFlag[1] |= (1 << 5);
283#endif
284#ifdef MKB_07
285 ErrFlag[1] |= (1 << 6);
286#endif
287#ifdef MKB_08
288 ErrFlag[1] |= (1 << 7);
289#endif
290
291#if defined(SH_WITH_SERVER) && !defined(SH_WITH_CLIENT)
292 strncpy(sh.prg_name, _("Yule"), 8);
293 sh.prg_name[4] = '\0';
294#else
295 strncpy(sh.prg_name, _("Samhain"), 8);
296 sh.prg_name[7] = '\0';
297#endif
298
299 /* The flags.
300 */
301 if (is_samhainctl_init == S_FALSE)
302 sh.flag.checkSum = SH_CHECK_NONE;
303 sh.flag.update = S_FALSE;
304 sh.flag.opts = S_FALSE;
305 if (is_samhainctl_init == S_FALSE)
306 sh.flag.isdaemon = S_FALSE;
307 sh.flag.isserver = S_FALSE;
308 sh.flag.islocked = S_FALSE;
309 sh.flag.smsg = S_FALSE;
310 sh.flag.log_start = S_TRUE;
311 sh.flag.reportonce = S_TRUE;
312 sh.flag.fulldetail = S_FALSE;
313 sh.flag.audit = S_FALSE;
314 sh.flag.nice = 0;
315 sh.flag.aud_mask = 0xFFFFFFFFUL;
316 sh.flag.client_severity = S_FALSE;
317 sh.flag.client_class = S_FALSE;
318 sh.flag.hidefile = S_FALSE;
319 sh.flag.loop = S_FALSE;
320
321#ifdef MKB_09
322 ErrFlag[1] |= (1 << 8);
323#endif
324#ifdef MKB_10
325 ErrFlag[1] |= (1 << 9);
326#endif
327#ifdef MKB_11
328 ErrFlag[1] |= (1 << 10);
329#endif
330#ifdef MKB_12
331 ErrFlag[1] |= (1 << 11);
332#endif
333#ifdef MKB_13
334 ErrFlag[1] |= (1 << 12);
335#endif
336#ifdef MKB_14
337 ErrFlag[1] |= (1 << 13);
338#endif
339#ifdef MKB_15
340 ErrFlag[1] |= (1 << 14);
341#endif
342#ifdef MKB_16
343 ErrFlag[1] |= (1 << 15);
344#endif
345
346 /* The stats.
347 */
348 sh.statistics.bytes_speed = 0;
349 sh.statistics.bytes_hashed = 0;
350 sh.statistics.mail_success = 0;
351 sh.statistics.mail_failed = 0;
352 sh.statistics.time_start = time(NULL);
353 sh.statistics.time_check = (time_t) 0;
354
355#ifdef MKC_01
356 ErrFlag[0] |= (1 << 16);
357#endif
358#ifdef MKC_02
359 ErrFlag[0] |= (1 << 17);
360#endif
361#ifdef MKC_03
362 ErrFlag[0] |= (1 << 18);
363#endif
364#ifdef MKC_04
365 ErrFlag[0] |= (1 << 19);
366#endif
367#ifdef MKC_05
368 ErrFlag[0] |= (1 << 20);
369#endif
370#ifdef MKC_06
371 ErrFlag[0] |= (1 << 21);
372#endif
373#ifdef MKC_07
374 ErrFlag[0] |= (1 << 22);
375#endif
376#ifdef MKC_08
377 ErrFlag[0] |= (1 << 23);
378#endif
379
380
381 /* The local host.
382 */
383 (void) sl_strlcpy (sh.host.name, _("localhost"), SH_MINIBUF);
[22]384 sh.host.system[0] = '\0'; /* flawfinder: ignore *//* ff bug */
[1]385 sh.host.release[0] = '\0';
386 sh.host.machine[0] = '\0';
387
388#ifdef MKC_09
389 ErrFlag[0] |= (1 << 24);
390#endif
391#ifdef MKC_10
392 ErrFlag[0] |= (1 << 25);
393#endif
394#ifdef MKC_11
395 ErrFlag[0] |= (1 << 26);
396#endif
397#ifdef MKC_12
398 ErrFlag[0] |= (1 << 27);
399#endif
400#ifdef MKC_13
401 ErrFlag[0] |= (1 << 28);
402#endif
403#ifdef MKC_14
404 ErrFlag[0] |= (1 << 29);
405#endif
406#ifdef MKC_15
407 ErrFlag[0] |= (1 << 30);
408#endif
409#ifdef MKC_16
410 ErrFlag[0] |= (1UL << 31);
411#endif
412
413 /* The paths.
414 */
415 (void) sl_strlcpy (sh.conf.path, DEFAULT_CONFIGFILE, SH_PATHBUF);
416 sh.conf.hash[0] = '\0';
417 (void) sl_strlcpy (sh.data.path, DEFAULT_DATA_FILE, SH_PATHBUF);
418 sh.data.hash[0] = '\0';
419 sh.exec.path[0] = '\0';
420 sh.exec.hash[0] = '\0';
421
422#ifdef MKD_01
423 ErrFlag[1] |= (1 << 16);
424#endif
425#ifdef MKD_02
426 ErrFlag[1] |= (1 << 17);
427#endif
428#ifdef MKD_03
429 ErrFlag[1] |= (1 << 18);
430#endif
431#ifdef MKD_04
432 ErrFlag[1] |= (1 << 19);
433#endif
434#ifdef MKD_05
435 ErrFlag[1] |= (1 << 20);
436#endif
437#ifdef MKD_06
438 ErrFlag[1] |= (1 << 21);
439#endif
440#ifdef MKD_07
441 ErrFlag[1] |= (1 << 22);
442#endif
443#ifdef MKD_08
444 ErrFlag[1] |= (1 << 23);
445#endif
446
447 /* The addresses.
448 */
449#if defined(SH_WITH_MAIL)
450 if (0 == strcmp (DEFAULT_MAILADDRESS, _("NULL")))
451 {
452 (void) sl_strncpy(q, DEFAULT_MAILADDRESS, SH_PATHBUF);
453 p = strtok (q, ", \t");
454 if (p)
455 {
456 (void) sh_mail_setaddress_int (p);
457 while (NULL != (p = strtok (NULL, ", \t")))
458 (void) sh_mail_setaddress_int (p);
459 }
460 }
461#endif
462
463 if (0 == strcmp (ALT_TIMESERVER, _("NULL")))
464 sh.srvtime.alt[0] = '\0';
465 else
466 (void) sl_strlcpy (sh.srvtime.alt, ALT_TIMESERVER, SH_PATHBUF);
467 if (0 == strcmp (DEFAULT_TIMESERVER, _("NULL")))
468 sh.srvtime.name[0] = '\0';
469 else
470 (void) sl_strlcpy (sh.srvtime.name, DEFAULT_TIMESERVER, SH_PATHBUF);
471
472
473 if (0 == strcmp (ALT_LOGSERVER, _("NULL")))
474 sh.srvexport.alt[0] = '\0';
475 else
476 (void) sl_strlcpy (sh.srvexport.alt, ALT_LOGSERVER, SH_PATHBUF);
477 if (0 == strcmp (DEFAULT_LOGSERVER, _("NULL")))
478 sh.srvexport.name[0] = '\0';
479 else
480 (void) sl_strlcpy (sh.srvexport.name, DEFAULT_LOGSERVER, SH_PATHBUF);
481
482
483 if (0 == strcmp (DEFAULT_ERRLOCK, _("NULL")))
484 sh.srvlog.alt[0] = '\0';
485 else
486 (void) sl_strlcpy (sh.srvlog.alt, DEFAULT_ERRLOCK, SH_PATHBUF);
487 if (0 == strcmp (DEFAULT_ERRFILE, _("NULL")))
488 sh.srvlog.name[0] = '\0';
489 else
490 (void) sl_strlcpy (sh.srvlog.name, DEFAULT_ERRFILE, SH_PATHBUF);
491
492 if (0 == strcmp (ALT_CONSOLE, _("NULL")))
493 sh.srvcons.alt[0] = '\0';
494 else
495 (void) sl_strlcpy (sh.srvcons.alt, ALT_CONSOLE, SH_PATHBUF);
496#ifndef DEFAULT_CONSOLE
497 (void) sl_strlcpy (sh.srvcons.name, _("/dev/console"), SH_PATHBUF);
498#else
499 if (0 == strcmp (DEFAULT_CONSOLE, _("NULL")))
500 (void) sl_strlcpy (sh.srvcons.name, _("/dev/console"), SH_PATHBUF);
501 else
502 (void) sl_strlcpy (sh.srvcons.name, DEFAULT_CONSOLE, SH_PATHBUF);
503#endif
504
505#ifdef MKD_09
506 ErrFlag[1] |= (1 << 24);
507#endif
508#ifdef MKD_10
509 ErrFlag[1] |= (1 << 25);
510#endif
511#ifdef MKD_11
512 ErrFlag[1] |= (1 << 26);
513#endif
514#ifdef MKD_12
515 ErrFlag[1] |= (1 << 27);
516#endif
517#ifdef MKD_13
518 ErrFlag[1] |= (1 << 28);
519#endif
520#ifdef MKD_14
521 ErrFlag[1] |= (1 << 29);
522#endif
523#ifdef MKD_15
524 ErrFlag[1] |= (1 << 30);
525#endif
526#ifdef MKD_16
527 ErrFlag[1] |= (1UL << 31);
528#endif
529
530
531 /* The timers.
532 */
533 sh.fileCheck.alarm_last = 0;
534 sh.fileCheck.alarm_interval = 600; /* ten minutes */
535
536 sh.mailTime.alarm_last = 0;
537 sh.mailTime.alarm_interval = 86400;
538
539 sh.mailNum.alarm_last = 0;
540 sh.mailNum.alarm_interval = 10;
541
542 sh.looptime = 60;
543
544 /* The struct to hold privileged information.
545 */
546 skey = (sh_key_t *) malloc (sizeof(sh_key_t));
[96]547 if (skey != NULL)
[1]548 {
[96]549
550 skey->mlock_failed = SL_FALSE;
551 skey->rngI = BAD;
552 /* properly initialized later
553 */
554 skey->rng0[0] = 0x03; skey->rng0[1] = 0x09; skey->rng0[2] = 0x17;
555 skey->rng1[0] = 0x03; skey->rng1[1] = 0x09; skey->rng1[2] = 0x17;
556 skey->rng2[0] = 0x03; skey->rng2[1] = 0x09; skey->rng2[2] = 0x17;
557
558 for (i = 0; i < KEY_BYT; ++i)
559 skey->poolv[i] = '\0';
560
561 skey->poolc = 0;
562
563 skey->ErrFlag[0] = ErrFlag[0];
564 ErrFlag[0] = 0;
565 skey->ErrFlag[1] = ErrFlag[1];
566 ErrFlag[1] = 0;
567
568 dez = &(TcpFlag[POS_TF-1][0]);
569 for (i = 0; i < PW_LEN; ++i)
570 {
571 skey->pw[i] = (char) (*dez);
572 (*dez) = '\0';
573 ++dez;
574 }
575
576 skey->sh_sockpass[0] = '\0';
577 skey->sigkey_old[0] = '\0';
578 skey->sigkey_new[0] = '\0';
579 skey->mailkey_old[0] = '\0';
580 skey->mailkey_new[0] = '\0';
581 skey->crypt[0] = '\0'; /* flawfinder: ignore *//* ff bug */
582 skey->session[0] = '\0';
583 skey->vernam[0] = '\0';
584 }
585 else
586 {
[1]587 perror(_("sh_init"));
588 _exit (EXIT_FAILURE);
589 }
590
591 sh_unix_memlock();
592 SL_RET0(_("sh_init"));
593}
594
595
596#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
597#include <sys/mman.h>
598#endif
599
600#if defined(SH_USE_XML)
601extern int sh_log_file (char * message, char * inet_peer);
602#endif
603
604/*******************************************************
605 *
606 * Exit Handler
607 *
608 *******************************************************/
609static void exit_handler(void)
610{
611 /* --- Clean up modules, if any. ---
612 */
613#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
614 int modnum;
615#endif
616#if defined(SH_WITH_SERVER)
617 extern int sh_socket_remove ();
618#endif
619
620 SL_ENTER(_("exit_handler"));
621
622#if defined(SH_WITH_SERVER)
623 sh_socket_remove ();
624#endif
625
626#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
627 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
628 {
629 if (modList[modnum].initval == GOOD)
630 (void) modList[modnum].mod_cleanup();
631 }
632#endif
633
634 /* --- Push out all pending messages. ---
635 */
636#if defined(SH_WITH_MAIL)
637 if (sh.mailNum.alarm_last > 0)
638 {
639 (void) sh_mail_msg (NULL);
640 }
641#endif
642
643 /* --- Write the server stat. ---
644 */
645#if defined(SH_WITH_SERVER)
646 sh_forward_html_write();
647#endif
648
649 /* --- Clean up memory to check for problems. ---
650 */
651#ifdef MEM_DEBUG
652#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
653 sh_files_deldirstack ();
654 sh_files_delfilestack ();
655 sh_hash_hashdelete();
[59]656 sh_files_hle_reg (NULL);
[1]657#endif
658#if defined(SH_WITH_SERVER)
659 sh_forward_free_all ();
660#endif
661 delete_cache();
662 sh_mem_stat();
663#endif
664
665#ifdef MEM_DEBUG
666 sh_unix_count_mlock();
667#endif
668
669 /* --- Checksum of executable. ---
670 */
671 (void) sh_unix_self_check();
672
673
674 /* --- Exit Message. ---
675 */
676 sh_error_handle ((-1), FIL__, __LINE__, sh.flag.exit, MSG_EXIT_NORMAL,
677 sh.prg_name, sh_sig_msg);
678#ifdef SH_USE_XML
679 (void) sh_log_file (NULL, NULL);
680#endif
681
682
683 /* --- Restrict error logging to stderr. ---
684 */
685#ifdef WITH_MESSAGE_QUEUE
686 close_ipc ();
687#endif
688 sh_error_only_stderr (S_TRUE);
689
690
691 /* --- Remove lock, delete critical information. ---
692 */
693 (void) sh_unix_rm_lock_file (sh.srvlog.name);
694 (void) sh_unix_rm_pid_file ();
695 if (skey != NULL)
696 memset (skey, (int) '\0', sizeof(sh_key_t));
697
698 /* --- Exit. ---
699 */
700 SL_RET0(_("exit_handler"));
701}
702
703/***********************************************************
704 *
705 */
706#ifndef SIGHUP
707#define SIGHUP 1
708#endif
709#ifndef SIGTERM
710#define SIGTERM 15
711#endif
712#ifndef SIGKILL
713#define SIGKILL 9
714#endif
715
716#if defined(__linux__) || defined(sun) || defined(__sun) || defined(__sun__)
717#include <dirent.h>
718static pid_t * procdirSamhain ()
719{
720 pid_t * pidlist = malloc(sizeof(pid_t) * 65535);
721 struct dirent * d;
722 DIR * dp;
723 long ino;
724 struct stat buf;
725 int i = 0;
726 pid_t pid, mypid = getpid();
727 char * tail;
728 char exef[128];
729
[22]730 if (!pidlist)
731 return NULL;
732
[1]733 for (i = 0; i < 65535; ++i) pidlist[i] = 0;
734 i = 0;
735
736 if (0 != stat(SH_INSTALL_PATH, &buf))
[22]737 {
738 free(pidlist);
739 return NULL;
740 }
[1]741
742 ino = (long) buf.st_ino;
743
744 if (NULL == (dp = opendir("/proc")))
[22]745 {
746 free(pidlist);
747 return NULL;
748 }
[1]749 while (NULL != (d = readdir(dp)) && i < 65535)
750 {
751 if (0 != strcmp(d->d_name, ".") && 0 != strcmp(d->d_name, ".."))
752 {
753 errno = 0;
754 pid = (pid_t) strtol (d->d_name, &tail, 0);
755 if (*tail != '\0' || errno != 0)
756 continue;
757 if (pid == mypid)
758 continue;
759#if defined(__linux__)
760 sprintf(exef, _("/proc/%d/exe"), (int) pid); /* known to fit */
761#else
762 sprintf(exef, _("/proc/%d/object/a.out"), /* known to fit */
763 (int) pid);
764#endif
765 if (0 == stat(exef, &buf) && ino == (long) buf.st_ino)
766 { pidlist[i] = (pid_t) pid; ++i; }
767 }
768 }
769 closedir(dp);
770 return pidlist;
771}
772#else
773static pid_t * procdirSamhain ()
774{
775 return NULL;
776}
777#endif
778
779static int killprocSamhain (pid_t pid)
780{
781 int i;
782
783 /* fprintf(stderr, "Killing %d\n", pid); */
784 if (pid > 0 && 0 == kill (pid, SIGTERM))
785 {
786 for (i = 0; i < 16; ++i)
787 {
788 (void) retry_msleep(1, 0);
789 if (0 != kill (pid, 0) && errno == ESRCH)
790 return (0);
791 }
792
793 (void) kill (pid, SIGKILL);
794 return (0);
795 }
796 if (pid > 0)
797 {
798 if (errno == ESRCH)
799 return 7;
800 if (errno == EPERM)
801 return 4;
802 return 1;
803 }
804 else
805 return (7);
806}
807
808static pid_t pidofSamhain (int flag)
809{
810 FILE * fp;
811 char line[256];
812 char * tail;
813 char * p;
814 pid_t pid;
815 long inpid;
816 struct stat buf;
817
818 fp = fopen (DEFAULT_ERRLOCK, "r");
819
820 if (!fp)
821 { if (errno != ENOENT) perror(_("fopen")); return 0; }
[34]822 if (NULL == fgets(line, sizeof(line), fp))
[1]823 { perror(_("fgets")); (void) fclose(fp); return 0; }
824 (void) fclose(fp);
825 p = line;
826 while (*p == ' ' || *p == '\f' || *p == '\n' ||
827 *p == '\r' || *p == '\t' || *p == '\v')
828 ++p;
829 errno = 0;
830 inpid = strtol (p, &tail, 0);
831 if (p == tail || errno != 0)
832 { perror(_("strtol")); return 0; }
833
834 pid = (pid_t) inpid;
835 if (inpid != (long) pid)
836 { perror(_("strtol")); return 0; }
837
838 /* remove stale pid file
839 */
840 if (flag == 1 && pid > 0 && 0 != kill(pid, 0) && errno == ESRCH)
841 {
842 if /*@-unrecog@*/ (0 == lstat (DEFAULT_ERRLOCK, &buf))/*@+unrecog@*/
843 {
844 if /*@-usedef@*/(S_ISREG(buf.st_mode))/*@+usedef@*/
845 {
846 (void) unlink(DEFAULT_ERRLOCK);
847 }
848 }
849 else
850 {
851 perror(_("lstat")); return 0;
852 }
853 pid = 0;
854 }
855 return pid;
856}
857
858/* 1: start 2:stop 3:reload 4:status
859 */
860/*@-exitarg@*/
861static int samhainctl(int ctl, int * argc, char * argv[])
862{
863 char * fullpath;
864 pid_t pid;
865 int status;
866 int res;
867 pid_t respid;
868 int times;
869 char * argp[32];
870 pid_t * pidlist;
871 int i;
872
873
[22]874 fullpath = strdup (SH_INSTALL_PATH);
[1]875 if (fullpath == NULL)
[22]876 { perror(_("strdup")); exit (1); }
[1]877
[22]878 argp[0] = strdup (SH_INSTALL_PATH);
[1]879 if (argp[0] == NULL)
[22]880 { perror(_("strdup")); exit (1); }
[1]881
882 for (times = 1; times < 32; ++times) argp[times] = NULL;
883
884 res = (*argc > 32) ? 32 : *argc;
885
886 for (times = 2; times < res; ++times)
887 {
[22]888 argp[times-1] = strdup (argv[times]);
[1]889 if (argp[times-1] == NULL)
[22]890 { perror(_("strdup")); exit (1); }
[1]891 }
892
893 if (ctl == 1)
894 {
895 pid = pidofSamhain(1);
896
897 if (pid != 0 && 0 == kill (pid, 0)) /* already started */
898 exit (0);
899
900 pid = fork();
901 switch (pid) {
902 case ((pid_t) -1):
903 perror(_("fork"));
904 exit (1);
905 case 0:
906 if (0 != close (0))
907 {
908 _exit(4);
909 }
[22]910 (void) execv(fullpath, argp); /* flawfinder: ignore *//* wtf? */
[1]911 if (errno == EPERM)
912 _exit(4);
913 else if (errno == ENOENT)
914 _exit(5);
915 _exit (1);
916 default:
917 times = 0;
[92]918 while (times < 300) {
[1]919 respid = waitpid(pid, &status, WNOHANG|WUNTRACED);
920 if ((pid_t)-1 == respid)
921 {
922 perror(_("waitpid"));
923 exit (1);
924 }
925 else if (pid == respid)
926 {
[96]927#ifndef USE_UNO
[1]928 if (0 != WIFEXITED(status))
929 {
930 res = WEXITSTATUS(status);
931 exit (res == 0 ? 0 : res );
932 }
933 else
934 exit (1);
[96]935#else
936 exit (1);
937#endif
[1]938 }
939 ++times;
940 (void) retry_msleep(1, 0);
941 }
[79]942 exit (0); /* assume that it runs ok */
[1]943 }
944 }
945
946 pid = pidofSamhain(0);
947
948 if (ctl == 2) /* stop */
949 {
950 pidlist = procdirSamhain ();
951 if (pid == 0 && NULL == pidlist) /* pid file not found */
[22]952 {
953 free(fullpath);
954 return (0);
955 }
[1]956
957 status = 0;
958 if (pid != 0)
959 status = killprocSamhain(pid);
960 if (pidlist != NULL)
961 {
962 i = 0;
963 while (i < 65535 && pidlist[i] != 0)
964 {
965 if (pidlist[i] != pid)
966 status = killprocSamhain(pidlist[i]);
967 ++i;
968 }
969 }
[22]970 free(fullpath);
[1]971 if (status == 7)
972 return 0;
973 else
974 return status;
975 }
976
977 if (ctl == 3) /* reload */
978 {
979 if (pid == 0)
980 exit (7);
981 if (0 == kill (pid, SIGHUP))
982 exit (0);
983 else
984 {
985 if (errno == EPERM)
986 exit (4);
987 if (errno == ESRCH)
988 exit (7);
989 exit (1);
990 }
991 }
992
993 if (ctl == 4) /* status */
994 {
995 if (pid == 0)
996 exit (3);
997 if (0 == kill (pid, 0))
998 exit (0);
999 else
1000 {
1001 if (errno == EPERM)
1002 exit (4);
1003 if (errno == ESRCH)
1004 exit (1);
1005 }
1006 }
[22]1007 free(fullpath); /* silence smatch false positive */
[1]1008 exit (1); /* no exit handler installed yet */
1009 /*@notreached@*/
1010 return (0);
1011}
1012/*@+exitarg@*/
1013
1014#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1015#include "sh_schedule.h"
1016static sh_schedule_t * FileSchedOne = NULL;
1017static sh_schedule_t * FileSchedTwo = NULL;
1018
1019/* free a linked list of schedules
1020 */
1021static sh_schedule_t * free_sched (sh_schedule_t * isched)
1022{
1023 sh_schedule_t * current = isched;
1024 sh_schedule_t * next = NULL;
1025
1026 while (current != NULL)
1027 {
1028 next = current->next;
1029 SH_FREE(current);
1030 current = next;
1031 }
1032 return NULL;
1033}
1034
1035/* Add a new schedule to the linked list of schedules
1036 */
[22]1037static sh_schedule_t * sh_set_schedule_int (const char * str,
[1]1038 sh_schedule_t * FileSchedIn,
1039 /*@out@*/ int * status)
1040{
1041 sh_schedule_t * FileSched;
1042
1043 SL_ENTER(_("sh_set_schedule_int"));
1044
1045 if (0 == sl_strncmp(str, _("NULL"), 4))
1046 {
1047 (void) free_sched(FileSchedIn);
1048 FileSchedIn = NULL;
1049 *status = 0;
1050 return 0;
1051 }
1052
1053 FileSched = SH_ALLOC(sizeof(sh_schedule_t));
1054 *status = create_sched(str, FileSched);
1055 if (*status != 0)
1056 {
1057 SH_FREE(FileSched);
1058 FileSched = NULL;
1059 SL_RETURN(FileSchedIn , _("sh_set_schedule_int"));
1060 }
1061 FileSched->next = FileSchedIn;
1062 SL_RETURN(FileSched , _("sh_set_schedule_int"));
1063}
1064
1065/* Add a new schedule to the linked list FileSchedOne
1066 */
[22]1067int sh_set_schedule_one (const char * str)
[1]1068{
1069 int status;
1070 FileSchedOne = sh_set_schedule_int (str, FileSchedOne, &status);
1071 return status;
1072}
1073
1074/* Add a new schedule to the linked list FileSchedTwo
1075 */
[22]1076int sh_set_schedule_two (const char * str)
[1]1077{
1078 int status;
1079 FileSchedTwo = sh_set_schedule_int (str, FileSchedTwo, &status);
1080 return status;
1081}
1082
1083#endif
1084
1085/*******************************************************
1086 *
1087 * Main program
1088 *
1089 *******************************************************/
[18]1090#if !defined(SH_CUTEST)
[1]1091int main(int argc, char * argv[])
[18]1092#else
1093int undef_main(int argc, char * argv[])
1094#endif
[1]1095{
1096#if defined(INET_SYSLOG)
1097 extern int create_syslog_socket (int flag);
1098#endif
1099#if defined(SH_WITH_SERVER)
1100 extern int sh_create_tcp_socket();
1101#endif
1102
1103#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1104 int modnum;
1105 time_t runtim;
1106 float st_1, st_2;
1107 int status;
1108 long cct = 0; /* main loop iterations */
1109
1110 int flag_check_1 = 0;
1111 int flag_check_2 = 0;
1112
1113 int check_done = 0;
1114#endif
1115
1116 time_t told;
1117 time_t tcurrent;
1118 size_t tzlen;
1119 char * tzptr;
1120 int res;
1121
1122#if defined (SH_STEALTH_NOCL)
[34]1123 char command_line[256];
[1]1124 int my_argc = 0;
1125 char * my_argv[32];
1126#endif
1127
1128 SL_ENTER(_("main"));
1129
[92]1130 /* --- Close all but first three file descriptors. ---
1131 */
1132 sh_unix_closeall(3, -1); /* at program start */
1133
1134
[1]1135 if (argc >= 2 && 0 != getuid() &&
1136 (0 == strcmp(argv[1], _("start")) ||
1137 0 == strcmp(argv[1], _("stop")) ||
1138 0 == strcmp(argv[1], _("reload")) ||
1139 0 == strcmp(argv[1], _("force-reload")) ||
1140 0 == strcmp(argv[1], _("status")) ||
1141 0 == strcmp(argv[1], _("restart"))))
1142 {
1143 return 4;
1144 }
1145
1146 if (argc >= 2 && 0 == getuid())
1147 {
1148 /* return codes:
1149 * 0 Success
1150 * 1 Can not send signal / start program
1151 * 2 Pid file does not exist
1152 */
1153 if (0 == strcmp(argv[1], _("start")))
1154 {
1155 (void) samhainctl (1, &argc, argv); /* does not return */
1156 }
1157 else if (0 == strcmp(argv[1], _("stop")))
1158 return (samhainctl (2, &argc, argv));
1159 else if (0 == strcmp(argv[1], _("reload")))
1160 (void) samhainctl (3, &argc, argv); /* does not return */
1161 else if (0 == strcmp(argv[1], _("force-reload")))
1162 (void) samhainctl (3, &argc, argv); /* does not return */
1163 else if (0 == strcmp(argv[1], _("status")))
1164 (void) samhainctl (4, &argc, argv); /* does not return */
1165 else if (0 == strcmp(argv[1], _("restart")))
1166 {
1167 res = samhainctl (2, &argc, argv);
1168 if (res == 0 || res == 7)
1169 {
1170 (void) samhainctl (1, &argc, argv); /* does not return */
1171 }
1172 else
1173 return (res);
1174 }
1175 }
1176
1177 /* if fd 0 is closed, presume that we want to be daemon and
1178 * run in check mode
1179 */
1180 if ((-1) == retry_fcntl(FIL__, __LINE__, 0, F_GETFL, 0) &&
1181 errno == EBADF)
1182 {
1183 sh.flag.opts = S_TRUE;
1184 (void) sh_unix_setdeamon(NULL);
1185#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1186 sh.flag.checkSum = SH_CHECK_CHECK;
1187 /* (void) sh_util_setchecksum(_("check")); */
1188#endif
1189 is_samhainctl_init = S_TRUE;
1190 sh.flag.opts = S_FALSE;
1191 }
1192
1193
1194 /* --- Install the exit handler. ---
1195 */
1196 (void) atexit(exit_handler);
1197
1198 /* --- Zero the mailer key, and fill it. ---
1199 */
1200 memset (ErrFlag, 0, 2*sizeof(UINT32));
1201
1202#ifdef MKA_01
1203 ErrFlag[0] |= (1 << 0);
1204#endif
1205#ifdef MKA_02
1206 ErrFlag[0] |= (1 << 1);
1207#endif
1208#ifdef MKA_03
1209 ErrFlag[0] |= (1 << 2);
1210#endif
1211#ifdef MKA_04
1212 ErrFlag[0] |= (1 << 3);
1213#endif
1214#ifdef MKA_05
1215 ErrFlag[0] |= (1 << 4);
1216#endif
1217#ifdef MKA_06
1218 ErrFlag[0] |= (1 << 5);
1219#endif
1220#ifdef MKA_07
1221 ErrFlag[0] |= (1 << 6);
1222#endif
1223#ifdef MKA_08
1224 ErrFlag[0] |= (1 << 7);
1225#endif
1226
1227#if defined(SCREW_IT_UP)
1228 BREAKEXIT(sh_sigtrap_prepare);
1229 (void) sh_sigtrap_prepare();
1230#endif
1231
1232 /* Save the timezone.
1233 */
[22]1234 if (NULL != (tzptr = getenv("TZ"))) /* flawfinder: ignore */
[1]1235 {
1236 tzlen = strlen(tzptr);
[22]1237 if (tzlen < 1024)
1238 {
1239 sh.timezone = malloc (tzlen + 1);
1240 if (sh.timezone != NULL)
1241 (void) sl_strlcpy (sh.timezone, tzptr, tzlen + 1);
1242 }
1243 else
1244 sh.timezone = NULL;
[1]1245 }
1246 else
1247 sh.timezone = NULL;
1248
1249
1250 /* -------- INIT --------
1251 */
1252
1253 /* Restrict error logging to stderr.
1254 */
1255 sh_error_only_stderr (S_TRUE);
1256
1257 BREAKEXIT(sh_derr);
1258 (void) sh_derr();
1259
1260 /* Check that first three descriptors are open.
1261 */
1262 if ( retry_fcntl(FIL__, __LINE__, 0, F_GETFL, 0) == (-1))
1263 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 0);
1264 if ( retry_fcntl(FIL__, __LINE__, 1, F_GETFL, 0) == (-1))
1265 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 1);
1266 if ( retry_fcntl(FIL__, __LINE__, 2, F_GETFL, 0) == (-1))
1267 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 2);
1268
1269 /* --- Set default values. ---
1270 */
1271 BREAKEXIT(sh_init);
1272 sh_init (); /* we are still privileged here, so we can mlock skey */
1273#if (defined (SH_WITH_SERVER) && !defined (SH_WITH_CLIENT))
1274 sh.flag.isserver = S_TRUE;
1275#endif
1276
1277 /* --- Get local hostname. ---
1278 */
1279 BREAKEXIT(sh_unix_localhost);
1280 sh_unix_localhost();
1281
1282 /* --- Read the command line. ---
1283 */
1284 sh.flag.opts = S_TRUE;
1285
1286#if !defined(SH_STEALTH_NOCL)
[20]1287 sh_argc_store = argc;
1288 sh_argv_store = argv;
[1]1289 (void) sh_getopt_get (argc, argv);
1290#else
1291 if (argc > 1 && argv[1] != NULL &&
1292 strlen(argv[1]) > 0 && strlen(NOCL_CODE) > 0)
1293 {
[34]1294 if ( 0 == strcmp(argv[1], NOCL_CODE) )
[1]1295 {
1296 my_argv[0] = argv[0]; ++my_argc;
1297 command_line[0] = '\0';
[34]1298 (void*) fgets (command_line, sizeof(command_line), stdin);
1299 command_line[sizeof(command_line)-1] = '\0';
[1]1300 do {
1301 my_argv[my_argc] =
1302 strtok( (my_argc == 1) ? command_line : NULL, " \n");
1303 if (my_argv[my_argc] != NULL) {
1304 ++my_argc;
1305 } else {
1306 break;
1307 }
1308 } while (my_argc < 32);
[20]1309
1310 sh_argc_store = my_argc;
1311 sh_argv_store = my_argv;
1312
[1]1313 (void) sh_getopt_get (my_argc, my_argv);
1314 }
1315 else
1316 {
1317 /* discard command line */
1318 /* _exit(EXIT_FAILURE) */ ;
1319 }
1320 }
1321#endif
1322 sh.flag.opts = S_FALSE;
1323
1324
1325 /* --- Get user info. ---
1326 */
1327 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1328 if (0 != sh_unix_getUser ())
1329 {
1330 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1331 sh.prg_name);
1332 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1333 }
1334
1335
1336 /* *****************************
1337 *
1338 * Read the configuration file.
1339 *
1340 * *****************************/
1341
1342 TPT((0, FIL__, __LINE__, _("msg=<Read the configuration file.>\n")))
1343 BREAKEXIT(sh_readconf_read);
1344 (void) sh_readconf_read ();
1345
1346#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1347 if (sh.flag.checkSum == SH_CHECK_NONE)
1348 {
1349 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
1350 _("No action specified: init, update, or check"),
1351 _("main"));
1352 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1353 sh.prg_name);
1354 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
1355 }
1356#endif
1357
1358 /* do not append to database if run SUID
1359 */
1360 if ((sh.flag.checkSum == SH_CHECK_INIT) && (0 != sl_is_suid()))
1361 {
1362 (void) dlog(1, FIL__, __LINE__,
1363 _("Cannot initialize database when running with SUID credentials.\nYou need to run this with the user ID %d.\nYour current user ID is %d."),
1364 (int) geteuid(), (int) sh.real.uid);
1365 sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_ACCESS,
1366 (long) sh.real.uid, sh.data.path);
1367 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1368 }
1369
1370 /* avoid daemon mode for initialization
1371 */
1372 if (sh.flag.checkSum == SH_CHECK_INIT)
1373 {
1374 sh.flag.isdaemon = S_FALSE;
1375 sh.flag.loop = S_FALSE;
1376 }
1377
[59]1378 /* --- load database; checksum of database
1379 */
1380#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1381 TPT((0, FIL__, __LINE__, _("msg=<Get checksum of the database.>\n")))
1382 if (sh.flag.checkSum == SH_CHECK_CHECK)
1383 {
1384 if (0 != sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
1385 {
1386 (void) sl_strlcpy(sh.data.hash,
1387 sh_tiger_hash (file_path('D', 'R'),
1388 TIGER_FILE, 0),
1389 KEY_LEN+1);
1390 }
1391
1392 /* this eventually fetches the file from server to get checksum
1393 */
1394 sh_hash_init ();
1395 }
1396#endif
1397
1398 /* --- initialize signal handling etc.; fork daemon
1399 */
[1]1400 if (sh_unix_init(sh.flag.isdaemon) == -1)
1401 {
1402 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1403 sh.prg_name);
1404 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1405 }
1406
1407 /* --- drop privileges eventually ---
1408 */
1409#if defined(SH_WITH_SERVER)
1410 sh_create_tcp_socket ();
1411#if defined(INET_SYSLOG)
1412 create_syslog_socket (S_TRUE);
1413#endif
1414 SL_REQUIRE(sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE,
1415 _("sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE"));
1416#else
1417 SL_REQUIRE(sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE,
1418 _("sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE"));
1419#endif
1420
1421 /* --- Get user info (again). ---
1422 */
1423 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1424 if (0 != sh_unix_getUser ())
1425 {
1426 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1427 sh.prg_name);
1428 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1429 }
1430
1431 /* --- now check whether we really wanted it; if not, close ---
1432 */
1433#if defined(INET_SYSLOG) && defined(SH_WITH_SERVER)
1434 create_syslog_socket (S_FALSE);
1435#endif
1436
1437
1438 /* --- Enable full error logging ---
1439 */
1440 sh_error_only_stderr (S_FALSE);
1441
1442 /****************************************************
1443 *
1444 * SERVER
1445 *
1446 ****************************************************/
1447
1448#if defined(SH_WITH_SERVER) && !defined(SH_WITH_CLIENT)
1449
1450#if (defined(WITH_GPG) || defined(WITH_PGP))
[86]1451 /* log startup */
1452 sh_gpg_log_startup ();
[1]1453#else
1454 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1455 sh.prg_name, (long) sh.real.uid,
1456 (sh.flag.hidefile == S_TRUE) ?
1457 _("(hidden)") : file_path('C','R'),
1458 sh.conf.hash);
1459#endif
1460
1461#else
1462
1463 /****************************************************
1464 *
1465 * CLIENT/STANDALONE
1466 *
1467 ****************************************************/
1468
1469 BREAKEXIT(sh_error_handle);
1470
1471 if (sh.flag.checkSum == SH_CHECK_CHECK)
1472 {
1473#if (defined(WITH_GPG) || defined(WITH_PGP))
[86]1474 /* log startup */
1475 sh_gpg_log_startup ();
[1]1476#else
1477 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_2H,
1478 sh.prg_name, (long) sh.real.uid,
1479 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash,
1480 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('D', 'R'), sh.data.hash);
1481#endif
1482 }
1483 else
1484 {
1485#if (defined(WITH_GPG) || defined(WITH_PGP))
[86]1486 /* log startup */
1487 sh_gpg_log_startup ();
[1]1488#else
1489 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1490 sh.prg_name, (long) sh.real.uid,
1491 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash);
1492#endif
1493 }
1494#endif
1495
1496
1497 if ((skey == NULL) || (skey->mlock_failed == SL_TRUE))
1498 sh_error_handle ((-1), FIL__, __LINE__, EPERM, MSG_MLOCK);
1499
1500 /* timer
1501 */
1502 tcurrent = time (NULL);
1503 told = tcurrent;
1504 sh.mailTime.alarm_last = told;
1505
1506
1507 /****************************************************
1508 *
1509 * SERVER
1510 *
1511 ****************************************************/
1512
1513#if defined(SH_WITH_SERVER)
1514 TPT((0, FIL__, __LINE__, _("msg=<Start server.>\n")))
1515
1516#if defined (SH_WITH_CLIENT)
1517 if (sh.flag.isserver == S_TRUE)
1518 {
1519 sh_receive();
1520 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1521 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1522 }
1523#else
1524 sh_receive();
1525 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1526 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1527#endif
1528
1529#endif
1530
1531 /****************************************************
1532 *
1533 * CLIENT/STANDALONE
1534 *
1535 ****************************************************/
1536#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1537
1538
1539 /* --- Initialize modules. ---
1540 */
1541 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")))
1542 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1543 {
1544 if (0 != (status = modList[modnum].mod_init()))
1545 {
1546 if (status == (-1)) {
1547 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, status,
1548 MSG_MOD_FAIL,
1549 _(modList[modnum].name),
1550 status);
1551 } else {
1552 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_FAIL,
1553 _(modList[modnum].name),
1554 status);
1555 }
1556 modList[modnum].initval = S_FALSE;
1557 }
1558 else
1559 {
1560 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1561 _(modList[modnum].name));
1562 modList[modnum].initval = S_TRUE;
1563 }
1564 }
1565
1566 /* -------- TEST SETUP ---------
1567 */
1568 (void) sh_files_setrec();
1569 (void) sh_files_test_setup();
1570
1571
1572 /* -------- NICE LEVEL ---------
1573 */
1574 if (0 != sh.flag.nice)
1575 {
1576#ifdef HAVE_SETPRIORITY
1577 /*@-unrecog@*/
1578 (void) setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1579 /*@+unrecog@*/
1580#else
1581 (void) nice(sh.flag.nice);
1582#endif
1583 }
1584
1585 /* -------- MAIN LOOP ---------
1586 */
1587 sh.statistics.bytes_speed = 0;
1588 sh.statistics.bytes_hashed = 0;
1589
1590 while (1 == 1)
1591 {
1592 ++cct;
1593
1594 BREAKEXIT(sh_error_handle);
1595
1596 TPT((0, FIL__, __LINE__, _("msg=<Start main loop.>, iter=<%ld>\n"), cct))
1597
1598 tcurrent = time (NULL);
1599
1600 if (sig_raised > 0)
1601 {
1602
1603 TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
1604
1605 if (sig_termfast == 1) /* SIGTERM */
1606 {
1607 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1608 /* strncpy (sh_sig_msg, _("SIGTERM"), 20); */
1609 --sig_raised; --sig_urgent;
1610 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1611 }
1612
1613 if (sig_force_check == 1) /* SIGTTOU */
1614 {
1615 TPT((0, FIL__, __LINE__, _("msg=<Check run triggered.>\n")));
1616 flag_check_1 = 1;
1617 flag_check_2 = 1;
1618 sig_force_check = 0;
1619 --sig_raised;
1620 }
1621
1622 if (sig_config_read_again == 1) /* SIGHUP */
1623 {
1624 TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")))
1625 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
1626
1627#if defined(WITH_EXTERNAL)
1628 /* delete list of external tasks
1629 */
1630 (void) sh_ext_cleanup();
1631#endif
1632 /* delete the file list, make all database
1633 * entries visible (allignore = FALSE)
1634 */
1635 (void) sh_files_deldirstack ();
1636 (void) sh_files_delfilestack ();
1637 (void) sh_ignore_clean ();
1638 (void) hash_full_tree ();
1639
1640#if defined(SH_WITH_CLIENT)
1641 reset_count_dev_server();
1642#endif
1643#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
1644
1645
1646 FileSchedOne = free_sched(FileSchedOne);
1647 FileSchedTwo = free_sched(FileSchedTwo);
1648
1649 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1650 {
1651 if (modList[modnum].initval == GOOD)
1652 (void) modList[modnum].mod_reconf();
1653 }
1654#endif
1655
1656#if defined(SH_WITH_MAIL)
1657 reset_count_dev_mail();
1658#endif
1659 reset_count_dev_console();
1660 reset_count_dev_time();
1661
1662 (void) sh_unix_maskreset();
1663
1664 /* Should this be included ???
1665 * (i.e. should we reload the database ?)
1666 */
1667#ifdef RELOAD_DATABASE
1668 sh_hash_hashdelete();
1669#endif
1670 (void) sl_trust_purge_user();
1671 (void) sh_files_hle_reg (NULL);
1672 (void) sh_prelink_run (NULL, NULL, 0);
1673
1674 /* --------------------------
1675 * --- READ CONFIGURATION ---
1676 * --------------------------
1677 */
1678 (void) sh_readconf_read ();
1679 sig_config_read_again = 0;
1680 (void) sh_files_setrec();
1681 (void) sh_files_test_setup();
1682 if (0 != sh.flag.nice)
1683 {
1684#ifdef HAVE_SETPRIORITY
1685 setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1686#else
1687 nice(sh.flag.nice);
1688#endif
1689 }
1690
1691 if (sh.flag.checkSum == SH_CHECK_INIT)
1692 {
1693 sh.flag.isdaemon = S_FALSE;
1694 sh.flag.loop = S_FALSE;
1695 }
1696
1697 /* --- Initialize modules. ---
1698 */
1699 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")));
1700 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1701 {
1702 if (0 != (status = modList[modnum].mod_init()))
1703 {
1704 if (status == (-1)) {
1705 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__,
1706 status, MSG_MOD_FAIL,
1707 _(modList[modnum].name),
1708 status);
1709 } else {
1710 sh_error_handle ((-1), FIL__, __LINE__,
1711 status, MSG_MOD_FAIL,
1712 _(modList[modnum].name),
1713 status);
1714 }
1715 modList[modnum].initval = S_FALSE;
1716 }
1717 else
1718 {
1719 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1720 _(modList[modnum].name));
1721 modList[modnum].initval = S_TRUE;
1722 }
1723 }
1724
1725 --sig_raised;
1726 }
1727
1728 if (sig_fresh_trail == 1) /* SIGIOT */
1729 {
1730 /* Logfile access
1731 */
1732#ifdef SH_USE_XML
1733 (void) sh_log_file (NULL, NULL);
1734#endif
1735 TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
1736 sh_error_only_stderr (S_TRUE);
1737 (void) sh_unix_rm_lock_file(sh.srvlog.name);
1738 (void) retry_msleep(3, 0);
1739 sh.flag.log_start = S_TRUE;
1740 sh_error_only_stderr (S_FALSE);
1741 sig_fresh_trail = 0;
1742 --sig_raised;
1743 }
1744
1745 if (sig_terminate == 1) /* SIGQUIT */
1746 {
1747 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1748 strncpy (sh_sig_msg, _("Quit"), 20);
1749 --sig_raised; --sig_urgent;
1750 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1751 }
1752
1753 if (sig_debug_switch == 1) /* SIGUSR1 */
1754 {
1755 TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
1756 sh_error_dbg_switch();
1757 sig_debug_switch = 0;
1758 --sig_raised;
1759 }
1760
[19]1761 if (sig_suspend_switch > 0) /* SIGUSR2 */
[1]1762 {
1763 TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
1764 if (sh_global_suspend_flag == 1) {
1765 sh_global_suspend_flag = 0;
1766 } else {
1767 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND,
1768 sh.prg_name);
1769 sh_global_suspend_flag = 1;
1770 }
[19]1771 --sig_suspend_switch;
[1]1772 --sig_raised; --sig_urgent;
1773 }
1774 sig_raised = (sig_raised < 0) ? 0 : sig_raised;
1775 sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
1776 TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
1777 }
1778
1779 if (sh_global_suspend_flag == 1)
1780 {
1781 (void) retry_msleep (1, 0);
1782 continue;
1783 }
1784
1785 /* see whether its time to check files
1786 */
[25]1787 if (sh.flag.checkSum == SH_CHECK_INIT ||
1788 (sh.flag.checkSum == SH_CHECK_CHECK &&
1789 (sh.flag.isdaemon == S_FALSE && sh.flag.loop == S_FALSE)))
[1]1790 {
1791 flag_check_1 = 1;
1792 if (FileSchedTwo != NULL)
1793 flag_check_2 = 1;
1794 }
1795 else if (sh.flag.checkSum == SH_CHECK_CHECK ||
1796 (sh.flag.update == S_TRUE &&
1797 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE)))
1798 {
1799 if (FileSchedOne == NULL)
1800 {
1801 /* use interval if we have no schedule
1802 */
1803 if (tcurrent - sh.fileCheck.alarm_last >=
1804 sh.fileCheck.alarm_interval)
1805 flag_check_1 = 1;
1806 }
1807 else
1808 {
1809 flag_check_1 = test_sched(FileSchedOne);
1810 if (FileSchedTwo != NULL)
1811 flag_check_2 = test_sched(FileSchedTwo);
1812 if (flag_check_2 == 1)
1813 flag_check_1 = 1;
1814 }
1815 }
1816
1817 check_done = 0;
1818
1819 if (sh.flag.checkSum != SH_CHECK_NONE &&
1820 (flag_check_1 == 1 || flag_check_2 == 1))
1821 {
1822 /*
1823 * check directories and files
1824 * ORDER IS IMPORTANT -- DIRZ FIRST
1825 */
1826 sh.statistics.bytes_hashed = 0;
1827 sh.statistics.time_start = time (NULL);
1828 sh.statistics.dirs_checked = 0;
1829 sh.statistics.files_checked = 0;
1830
1831 TPT((0, FIL__, __LINE__, _("msg=<Check directories.>\n")))
1832 BREAKEXIT(sh_dirs_chk);
1833 if (flag_check_1 == 1)
1834 {
1835 (void) sh_dirs_chk (1);
1836#ifndef SH_PROFILE
1837 (void) chdir ("/");
1838#endif
1839 }
1840 if (flag_check_2 == 1)
1841 {
1842 (void) sh_dirs_chk (2);
1843#ifndef SH_PROFILE
1844 (void) chdir ("/");
1845#endif
1846 }
1847 TPT((0, FIL__, __LINE__, _("msg=<Check files.>\n")))
1848 BREAKEXIT(sh_files_chk);
1849 if (flag_check_1 == 1)
1850 (void) sh_files_chk ();
1851
1852 if (sig_urgent > 0)
1853 continue;
1854
1855 /*
1856 * check for files not visited
1857 */
1858 if (flag_check_2 == 1 || FileSchedTwo == NULL)
1859 {
1860 TPT((0, FIL__, __LINE__, _("msg=<Check for missing files.>\n")))
1861 sh_hash_unvisited (ShDFLevel[SH_ERR_T_FILE]);
1862 }
1863
1864 if (sig_urgent > 0)
1865 continue;
1866
1867 /* reset
1868 */
1869 TPT((0, FIL__, __LINE__, _("msg=<Reset status.>\n")))
1870 sh_dirs_reset ();
1871 if (sig_urgent > 0)
1872 continue;
1873
1874 sh_files_reset ();
1875 flag_check_1 = 0;
1876 flag_check_2 = 0;
1877 check_done = 1;
1878
1879 (void) sh_prelink_run (NULL, NULL, 0);
1880
1881 if (sig_urgent > 0)
1882 continue;
1883
1884 runtim = time(NULL) - sh.statistics.time_start;
1885 sh.statistics.time_check = runtim;
1886
1887 if ((sh.statistics.dirs_checked == 0) &&
1888 (sh.statistics.files_checked == 0))
1889 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_0);
1890
1891 else
1892 {
1893 st_1 = (float) sh.statistics.bytes_hashed;
1894 st_2 = (float) runtim;
1895
1896
1897 if (st_1 > FLT_EPSILON && st_2 > FLT_EPSILON)
1898 st_1 = st_1/st_2;
1899 else if (st_1 > FLT_EPSILON)
1900 st_1 = (float) (st_1 * 1.0);
1901 else
1902 st_1 = 0.0;
1903
1904 sh.statistics.bytes_speed = (unsigned long) st_1;
1905
1906 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_1,
1907 (long) runtim,
1908 0.001 * st_1);
1909 }
1910 sh.fileCheck.alarm_last = time (NULL);
1911
1912 if (sig_urgent > 0)
1913 continue;
1914
1915 /*
1916 * flush mail queue
1917 */
1918#if defined(SH_WITH_MAIL)
1919 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
1920 (void) sh_mail_msg (NULL);
1921#endif
1922 }
1923
1924 if (sig_urgent > 0)
1925 continue;
1926
1927 /* execute modules
1928 */
1929 TPT((0, FIL__, __LINE__, _("msg=<Execute modules.>\n")))
1930 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1931 {
1932 if (modList[modnum].initval == GOOD &&
1933 0 != modList[modnum].mod_timer(tcurrent))
1934 if (0 != (status = modList[modnum].mod_check()))
1935 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_EXEC,
1936 _(modList[modnum].name), (long) status);
1937 }
1938
1939 /* 27.05.2002 avoid empty database
1940 * 22.10.2002 moved here b/o suid check initialization
1941 */
1942 if (sh.flag.checkSum == SH_CHECK_INIT)
1943 sh_hash_pushdata (NULL, NULL);
1944
1945 /* write out database
1946 */
1947 if (sh.flag.checkSum == SH_CHECK_CHECK &&
1948 sh.flag.update == S_TRUE &&
1949 check_done == 1)
1950 sh_hash_writeout ();
1951
1952 /* no loop if not daemon
1953 */
1954 if (sh.flag.isdaemon != S_TRUE && sh.flag.loop == S_FALSE)
1955 break;
1956 if (sig_urgent > 0)
1957 continue;
1958
1959 /* see whether its time to send mail
1960 */
1961#if defined(SH_WITH_MAIL)
1962 if (tcurrent - sh.mailTime.alarm_last >= sh.mailTime.alarm_interval)
1963 {
1964 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
1965 (void) sh_mail_msg (NULL);
1966 sh.mailTime.alarm_last = time (NULL);
1967 }
1968#endif
1969 if (sig_urgent > 0)
1970 continue;
1971
1972 /* log the timestamp
1973 */
1974 if ((int)(tcurrent - told) >= sh.looptime )
1975 {
1976 TPT((0, FIL__, __LINE__, _("msg=<Log the timestamp.>\n")))
1977 told = tcurrent;
1978#ifdef MEM_DEBUG
1979 sh_mem_check();
1980 sh_unix_count_mlock();
1981#else
1982 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_STAMP);
1983#endif
1984 }
1985
1986 /* seed / re-seed the PRNG if required
1987 */
1988 (void) taus_seed();
1989
1990 if (sig_urgent > 0)
1991 continue;
1992
1993 /* go to sleep
1994 */
1995 (void) retry_msleep (1, 0);
1996
1997 BREAKEXIT(sh_derr);
1998 (void) sh_derr();
1999 }
2000
2001 /* ------ END -----------
2002 */
2003
2004
2005
2006 /*
2007 * cleanup
2008 */
2009 TPT((0, FIL__, __LINE__, _("msg=<Cleanup.>\n")));
2010 sh_hash_hashdelete();
2011
2012#if defined(SH_WITH_MAIL)
2013 if (sh.mailNum.alarm_last > 0)
2014 (void)sh_mail_msg (NULL);
2015#endif
2016
2017 /* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
2018#endif
2019
2020 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
2021 SL_RETURN(0, _("main"));
2022}
Note: See TracBrowser for help on using the repository browser.