source: trunk/src/samhain.c@ 20

Last change on this file since 20 was 20, checked in by rainer, 19 years ago

Enable command-line parsing for prelude, and make prelude regression test safer.

File size: 45.7 KB
Line 
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);
384 sh.host.system[0] = '\0';
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));
547 if (skey == NULL)
548 {
549 perror(_("sh_init"));
550 _exit (EXIT_FAILURE);
551 }
552
553 skey->mlock_failed = SL_FALSE;
554 skey->rngI = BAD;
555 /* properly initialized later
556 */
557 skey->rng0[0] = 0x03; skey->rng0[1] = 0x09; skey->rng0[2] = 0x17;
558 skey->rng1[0] = 0x03; skey->rng1[1] = 0x09; skey->rng1[2] = 0x17;
559 skey->rng2[0] = 0x03; skey->rng2[1] = 0x09; skey->rng2[2] = 0x17;
560
561 for (i = 0; i < KEY_BYT; ++i)
562 skey->poolv[i] = '\0';
563
564 skey->poolc = 0;
565
566 skey->ErrFlag[0] = ErrFlag[0];
567 ErrFlag[0] = 0;
568 skey->ErrFlag[1] = ErrFlag[1];
569 ErrFlag[1] = 0;
570
571 dez = &(TcpFlag[POS_TF-1][0]);
572 for (i = 0; i < PW_LEN; ++i)
573 {
574 skey->pw[i] = (char) (*dez);
575 (*dez) = '\0';
576 ++dez;
577 }
578
579 skey->sh_sockpass[0] = '\0';
580 skey->sigkey_old[0] = '\0';
581 skey->sigkey_new[0] = '\0';
582 skey->mailkey_old[0] = '\0';
583 skey->mailkey_new[0] = '\0';
584 skey->crypt[0] = '\0';
585 skey->session[0] = '\0';
586 skey->vernam[0] = '\0';
587
588
589 sh_unix_memlock();
590 SL_RET0(_("sh_init"));
591}
592
593
594#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
595#include <sys/mman.h>
596#endif
597
598#if defined(SH_USE_XML)
599extern int sh_log_file (char * message, char * inet_peer);
600#endif
601
602/*******************************************************
603 *
604 * Exit Handler
605 *
606 *******************************************************/
607static void exit_handler(void)
608{
609 /* --- Clean up modules, if any. ---
610 */
611#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
612 int modnum;
613#endif
614#if defined(SH_WITH_SERVER)
615 extern int sh_socket_remove ();
616#endif
617
618 SL_ENTER(_("exit_handler"));
619
620#if defined(SH_WITH_SERVER)
621 sh_socket_remove ();
622#endif
623
624#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
625 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
626 {
627 if (modList[modnum].initval == GOOD)
628 (void) modList[modnum].mod_cleanup();
629 }
630#endif
631
632 /* --- Push out all pending messages. ---
633 */
634#if defined(SH_WITH_MAIL)
635 if (sh.mailNum.alarm_last > 0)
636 {
637 (void) sh_mail_msg (NULL);
638 }
639#endif
640
641 /* --- Write the server stat. ---
642 */
643#if defined(SH_WITH_SERVER)
644 sh_forward_html_write();
645#endif
646
647 /* --- Clean up memory to check for problems. ---
648 */
649#ifdef MEM_DEBUG
650#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
651 sh_files_deldirstack ();
652 sh_files_delfilestack ();
653 sh_hash_hashdelete();
654#endif
655#if defined(SH_WITH_SERVER)
656 sh_forward_free_all ();
657#endif
658 delete_cache();
659 sh_mem_stat();
660#endif
661
662#ifdef MEM_DEBUG
663 sh_unix_count_mlock();
664#endif
665
666 /* --- Checksum of executable. ---
667 */
668 (void) sh_unix_self_check();
669
670
671 /* --- Exit Message. ---
672 */
673 sh_error_handle ((-1), FIL__, __LINE__, sh.flag.exit, MSG_EXIT_NORMAL,
674 sh.prg_name, sh_sig_msg);
675#ifdef SH_USE_XML
676 (void) sh_log_file (NULL, NULL);
677#endif
678
679
680 /* --- Restrict error logging to stderr. ---
681 */
682#ifdef WITH_MESSAGE_QUEUE
683 close_ipc ();
684#endif
685 sh_error_only_stderr (S_TRUE);
686
687
688 /* --- Remove lock, delete critical information. ---
689 */
690 (void) sh_unix_rm_lock_file (sh.srvlog.name);
691 (void) sh_unix_rm_pid_file ();
692 if (skey != NULL)
693 memset (skey, (int) '\0', sizeof(sh_key_t));
694
695 /* --- Exit. ---
696 */
697 SL_RET0(_("exit_handler"));
698}
699
700/***********************************************************
701 *
702 */
703#ifndef SIGHUP
704#define SIGHUP 1
705#endif
706#ifndef SIGTERM
707#define SIGTERM 15
708#endif
709#ifndef SIGKILL
710#define SIGKILL 9
711#endif
712
713#if defined(__linux__) || defined(sun) || defined(__sun) || defined(__sun__)
714#include <dirent.h>
715static pid_t * procdirSamhain ()
716{
717 pid_t * pidlist = malloc(sizeof(pid_t) * 65535);
718 struct dirent * d;
719 DIR * dp;
720 long ino;
721 struct stat buf;
722 int i = 0;
723 pid_t pid, mypid = getpid();
724 char * tail;
725 char exef[128];
726
727 for (i = 0; i < 65535; ++i) pidlist[i] = 0;
728 i = 0;
729
730 if (0 != stat(SH_INSTALL_PATH, &buf))
731 return NULL;
732
733 ino = (long) buf.st_ino;
734
735 if (NULL == (dp = opendir("/proc")))
736 return NULL;
737 while (NULL != (d = readdir(dp)) && i < 65535)
738 {
739 if (0 != strcmp(d->d_name, ".") && 0 != strcmp(d->d_name, ".."))
740 {
741 errno = 0;
742 pid = (pid_t) strtol (d->d_name, &tail, 0);
743 if (*tail != '\0' || errno != 0)
744 continue;
745 if (pid == mypid)
746 continue;
747#if defined(__linux__)
748 sprintf(exef, _("/proc/%d/exe"), (int) pid); /* known to fit */
749#else
750 sprintf(exef, _("/proc/%d/object/a.out"), /* known to fit */
751 (int) pid);
752#endif
753 if (0 == stat(exef, &buf) && ino == (long) buf.st_ino)
754 { pidlist[i] = (pid_t) pid; ++i; }
755 }
756 }
757 closedir(dp);
758 return pidlist;
759}
760#else
761static pid_t * procdirSamhain ()
762{
763 return NULL;
764}
765#endif
766
767static int killprocSamhain (pid_t pid)
768{
769 int i;
770
771 /* fprintf(stderr, "Killing %d\n", pid); */
772 if (pid > 0 && 0 == kill (pid, SIGTERM))
773 {
774 for (i = 0; i < 16; ++i)
775 {
776 (void) retry_msleep(1, 0);
777 if (0 != kill (pid, 0) && errno == ESRCH)
778 return (0);
779 }
780
781 (void) kill (pid, SIGKILL);
782 return (0);
783 }
784 if (pid > 0)
785 {
786 if (errno == ESRCH)
787 return 7;
788 if (errno == EPERM)
789 return 4;
790 return 1;
791 }
792 else
793 return (7);
794}
795
796static pid_t pidofSamhain (int flag)
797{
798 FILE * fp;
799 char line[256];
800 char * tail;
801 char * p;
802 pid_t pid;
803 long inpid;
804 struct stat buf;
805
806 fp = fopen (DEFAULT_ERRLOCK, "r");
807
808 if (!fp)
809 { if (errno != ENOENT) perror(_("fopen")); return 0; }
810 if (NULL == fgets(line, 255, fp))
811 { perror(_("fgets")); (void) fclose(fp); return 0; }
812 (void) fclose(fp);
813 p = line;
814 while (*p == ' ' || *p == '\f' || *p == '\n' ||
815 *p == '\r' || *p == '\t' || *p == '\v')
816 ++p;
817 errno = 0;
818 inpid = strtol (p, &tail, 0);
819 if (p == tail || errno != 0)
820 { perror(_("strtol")); return 0; }
821
822 pid = (pid_t) inpid;
823 if (inpid != (long) pid)
824 { perror(_("strtol")); return 0; }
825
826 /* remove stale pid file
827 */
828 if (flag == 1 && pid > 0 && 0 != kill(pid, 0) && errno == ESRCH)
829 {
830 if /*@-unrecog@*/ (0 == lstat (DEFAULT_ERRLOCK, &buf))/*@+unrecog@*/
831 {
832 if /*@-usedef@*/(S_ISREG(buf.st_mode))/*@+usedef@*/
833 {
834 (void) unlink(DEFAULT_ERRLOCK);
835 }
836 }
837 else
838 {
839 perror(_("lstat")); return 0;
840 }
841 pid = 0;
842 }
843 return pid;
844}
845
846/* 1: start 2:stop 3:reload 4:status
847 */
848/*@-exitarg@*/
849static int samhainctl(int ctl, int * argc, char * argv[])
850{
851 char * fullpath;
852 pid_t pid;
853 int status;
854 int res;
855 pid_t respid;
856 int times;
857 char * argp[32];
858 pid_t * pidlist;
859 int i;
860
861
862 fullpath = malloc(strlen(SH_INSTALL_PATH)+1);
863 if (fullpath == NULL)
864 { perror(_("malloc")); exit (1); }
865 else
866 strcpy(fullpath, SH_INSTALL_PATH); /* known to fit */
867
868 argp[0] = malloc(strlen(SH_INSTALL_PATH)+1);
869 if (argp[0] == NULL)
870 { perror(_("malloc")); exit (1); }
871 else
872 strcpy(argp[0], SH_INSTALL_PATH); /* known to fit */
873
874
875 for (times = 1; times < 32; ++times) argp[times] = NULL;
876
877 res = (*argc > 32) ? 32 : *argc;
878
879 for (times = 2; times < res; ++times)
880 {
881 argp[times-1] = malloc(strlen(argv[times])+1);
882 if (argp[times-1] == NULL)
883 { perror(_("malloc")); exit (1); }
884 else
885 strcpy(argp[times-1], argv[times]); /* known to fit */
886 }
887
888 if (ctl == 1)
889 {
890 pid = pidofSamhain(1);
891
892 if (pid != 0 && 0 == kill (pid, 0)) /* already started */
893 exit (0);
894
895 pid = fork();
896 switch (pid) {
897 case ((pid_t) -1):
898 perror(_("fork"));
899 exit (1);
900 case 0:
901 if (0 != close (0))
902 {
903 _exit(4);
904 }
905 (void) execv(fullpath, argp);
906 if (errno == EPERM)
907 _exit(4);
908 else if (errno == ENOENT)
909 _exit(5);
910 _exit (1);
911 default:
912 times = 0;
913 while (times < 64) {
914 respid = waitpid(pid, &status, WNOHANG|WUNTRACED);
915 if ((pid_t)-1 == respid)
916 {
917 perror(_("waitpid"));
918 exit (1);
919 }
920 else if (pid == respid)
921 {
922 if (0 != WIFEXITED(status))
923 {
924 res = WEXITSTATUS(status);
925 exit (res == 0 ? 0 : res );
926 }
927 else
928 exit (1);
929 }
930 ++times;
931 (void) retry_msleep(1, 0);
932 }
933 exit (1);
934 }
935 }
936
937 pid = pidofSamhain(0);
938
939 if (ctl == 2) /* stop */
940 {
941 pidlist = procdirSamhain ();
942 if (pid == 0 && NULL == pidlist) /* pid file not found */
943 return (0);
944
945 status = 0;
946 if (pid != 0)
947 status = killprocSamhain(pid);
948 if (pidlist != NULL)
949 {
950 i = 0;
951 while (i < 65535 && pidlist[i] != 0)
952 {
953 if (pidlist[i] != pid)
954 status = killprocSamhain(pidlist[i]);
955 ++i;
956 }
957 }
958 if (status == 7)
959 return 0;
960 else
961 return status;
962 }
963
964 if (ctl == 3) /* reload */
965 {
966 if (pid == 0)
967 exit (7);
968 if (0 == kill (pid, SIGHUP))
969 exit (0);
970 else
971 {
972 if (errno == EPERM)
973 exit (4);
974 if (errno == ESRCH)
975 exit (7);
976 exit (1);
977 }
978 }
979
980 if (ctl == 4) /* status */
981 {
982 if (pid == 0)
983 exit (3);
984 if (0 == kill (pid, 0))
985 exit (0);
986 else
987 {
988 if (errno == EPERM)
989 exit (4);
990 if (errno == ESRCH)
991 exit (1);
992 }
993 }
994
995 exit (1); /* no exit handler installed yet */
996 /*@notreached@*/
997 return (0);
998}
999/*@+exitarg@*/
1000
1001#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1002#include "sh_schedule.h"
1003static sh_schedule_t * FileSchedOne = NULL;
1004static sh_schedule_t * FileSchedTwo = NULL;
1005
1006/* free a linked list of schedules
1007 */
1008static sh_schedule_t * free_sched (sh_schedule_t * isched)
1009{
1010 sh_schedule_t * current = isched;
1011 sh_schedule_t * next = NULL;
1012
1013 while (current != NULL)
1014 {
1015 next = current->next;
1016 SH_FREE(current);
1017 current = next;
1018 }
1019 return NULL;
1020}
1021
1022/* Add a new schedule to the linked list of schedules
1023 */
1024static sh_schedule_t * sh_set_schedule_int (char * str,
1025 sh_schedule_t * FileSchedIn,
1026 /*@out@*/ int * status)
1027{
1028 sh_schedule_t * FileSched;
1029
1030 SL_ENTER(_("sh_set_schedule_int"));
1031
1032 if (0 == sl_strncmp(str, _("NULL"), 4))
1033 {
1034 (void) free_sched(FileSchedIn);
1035 FileSchedIn = NULL;
1036 *status = 0;
1037 return 0;
1038 }
1039
1040 FileSched = SH_ALLOC(sizeof(sh_schedule_t));
1041 *status = create_sched(str, FileSched);
1042 if (*status != 0)
1043 {
1044 SH_FREE(FileSched);
1045 FileSched = NULL;
1046 SL_RETURN(FileSchedIn , _("sh_set_schedule_int"));
1047 }
1048 FileSched->next = FileSchedIn;
1049 SL_RETURN(FileSched , _("sh_set_schedule_int"));
1050}
1051
1052/* Add a new schedule to the linked list FileSchedOne
1053 */
1054int sh_set_schedule_one (char * str)
1055{
1056 int status;
1057 FileSchedOne = sh_set_schedule_int (str, FileSchedOne, &status);
1058 return status;
1059}
1060
1061/* Add a new schedule to the linked list FileSchedTwo
1062 */
1063int sh_set_schedule_two (char * str)
1064{
1065 int status;
1066 FileSchedTwo = sh_set_schedule_int (str, FileSchedTwo, &status);
1067 return status;
1068}
1069
1070#endif
1071
1072/*******************************************************
1073 *
1074 * Main program
1075 *
1076 *******************************************************/
1077#if !defined(SH_CUTEST)
1078int main(int argc, char * argv[])
1079#else
1080int undef_main(int argc, char * argv[])
1081#endif
1082{
1083#if defined(INET_SYSLOG)
1084 extern int create_syslog_socket (int flag);
1085#endif
1086#if defined(SH_WITH_SERVER)
1087 extern int sh_create_tcp_socket();
1088#endif
1089
1090#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1091 int modnum;
1092 time_t runtim;
1093 float st_1, st_2;
1094 int status;
1095 long cct = 0; /* main loop iterations */
1096
1097 int flag_check_1 = 0;
1098 int flag_check_2 = 0;
1099
1100 int check_done = 0;
1101#endif
1102
1103 time_t told;
1104 time_t tcurrent;
1105 size_t tzlen;
1106 char * tzptr;
1107 int res;
1108
1109#if defined (SH_STEALTH_NOCL)
1110 char * command_line;
1111 int my_argc = 0;
1112 char * my_argv[32];
1113#endif
1114
1115 SL_ENTER(_("main"));
1116
1117 if (argc >= 2 && 0 != getuid() &&
1118 (0 == strcmp(argv[1], _("start")) ||
1119 0 == strcmp(argv[1], _("stop")) ||
1120 0 == strcmp(argv[1], _("reload")) ||
1121 0 == strcmp(argv[1], _("force-reload")) ||
1122 0 == strcmp(argv[1], _("status")) ||
1123 0 == strcmp(argv[1], _("restart"))))
1124 {
1125 return 4;
1126 }
1127
1128 if (argc >= 2 && 0 == getuid())
1129 {
1130 /* return codes:
1131 * 0 Success
1132 * 1 Can not send signal / start program
1133 * 2 Pid file does not exist
1134 */
1135 if (0 == strcmp(argv[1], _("start")))
1136 {
1137 (void) samhainctl (1, &argc, argv); /* does not return */
1138 }
1139 else if (0 == strcmp(argv[1], _("stop")))
1140 return (samhainctl (2, &argc, argv));
1141 else if (0 == strcmp(argv[1], _("reload")))
1142 (void) samhainctl (3, &argc, argv); /* does not return */
1143 else if (0 == strcmp(argv[1], _("force-reload")))
1144 (void) samhainctl (3, &argc, argv); /* does not return */
1145 else if (0 == strcmp(argv[1], _("status")))
1146 (void) samhainctl (4, &argc, argv); /* does not return */
1147 else if (0 == strcmp(argv[1], _("restart")))
1148 {
1149 res = samhainctl (2, &argc, argv);
1150 if (res == 0 || res == 7)
1151 {
1152 (void) samhainctl (1, &argc, argv); /* does not return */
1153 }
1154 else
1155 return (res);
1156 }
1157 }
1158
1159 /* if fd 0 is closed, presume that we want to be daemon and
1160 * run in check mode
1161 */
1162 if ((-1) == retry_fcntl(FIL__, __LINE__, 0, F_GETFL, 0) &&
1163 errno == EBADF)
1164 {
1165 sh.flag.opts = S_TRUE;
1166 (void) sh_unix_setdeamon(NULL);
1167#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1168 sh.flag.checkSum = SH_CHECK_CHECK;
1169 /* (void) sh_util_setchecksum(_("check")); */
1170#endif
1171 is_samhainctl_init = S_TRUE;
1172 sh.flag.opts = S_FALSE;
1173 }
1174
1175
1176 /* --- Install the exit handler. ---
1177 */
1178 (void) atexit(exit_handler);
1179
1180 /* --- Zero the mailer key, and fill it. ---
1181 */
1182 memset (ErrFlag, 0, 2*sizeof(UINT32));
1183
1184#ifdef MKA_01
1185 ErrFlag[0] |= (1 << 0);
1186#endif
1187#ifdef MKA_02
1188 ErrFlag[0] |= (1 << 1);
1189#endif
1190#ifdef MKA_03
1191 ErrFlag[0] |= (1 << 2);
1192#endif
1193#ifdef MKA_04
1194 ErrFlag[0] |= (1 << 3);
1195#endif
1196#ifdef MKA_05
1197 ErrFlag[0] |= (1 << 4);
1198#endif
1199#ifdef MKA_06
1200 ErrFlag[0] |= (1 << 5);
1201#endif
1202#ifdef MKA_07
1203 ErrFlag[0] |= (1 << 6);
1204#endif
1205#ifdef MKA_08
1206 ErrFlag[0] |= (1 << 7);
1207#endif
1208
1209#if defined(SCREW_IT_UP)
1210 BREAKEXIT(sh_sigtrap_prepare);
1211 (void) sh_sigtrap_prepare();
1212#endif
1213
1214 /* Save the timezone.
1215 */
1216 if ((tzptr = getenv("TZ")) != NULL)
1217 {
1218 tzlen = strlen(tzptr);
1219 sh.timezone = malloc (tzlen + 1);
1220 if (sh.timezone != NULL)
1221 (void) sl_strlcpy (sh.timezone, tzptr, tzlen + 1);
1222 }
1223 else
1224 sh.timezone = NULL;
1225
1226
1227 /* -------- INIT --------
1228 */
1229
1230 /* Restrict error logging to stderr.
1231 */
1232 sh_error_only_stderr (S_TRUE);
1233
1234 BREAKEXIT(sh_derr);
1235 (void) sh_derr();
1236
1237 /* Check that first three descriptors are open.
1238 */
1239 if ( retry_fcntl(FIL__, __LINE__, 0, F_GETFL, 0) == (-1))
1240 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 0);
1241 if ( retry_fcntl(FIL__, __LINE__, 1, F_GETFL, 0) == (-1))
1242 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 1);
1243 if ( retry_fcntl(FIL__, __LINE__, 2, F_GETFL, 0) == (-1))
1244 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 2);
1245
1246 /* --- Set default values. ---
1247 */
1248 BREAKEXIT(sh_init);
1249 sh_init (); /* we are still privileged here, so we can mlock skey */
1250#if (defined (SH_WITH_SERVER) && !defined (SH_WITH_CLIENT))
1251 sh.flag.isserver = S_TRUE;
1252#endif
1253
1254 /* --- Get local hostname. ---
1255 */
1256 BREAKEXIT(sh_unix_localhost);
1257 sh_unix_localhost();
1258
1259 /* --- Read the command line. ---
1260 */
1261 sh.flag.opts = S_TRUE;
1262
1263#if !defined(SH_STEALTH_NOCL)
1264 sh_argc_store = argc;
1265 sh_argv_store = argv;
1266 (void) sh_getopt_get (argc, argv);
1267#else
1268 if (argc > 1 && argv[1] != NULL &&
1269 strlen(argv[1]) > 0 && strlen(NOCL_CODE) > 0)
1270 {
1271 if ( 0 == strcmp(argv[1], NOCL_CODE) &&
1272 NULL != (command_line = (char *) SH_ALLOC(256 * sizeof(char))))
1273 {
1274 my_argv[0] = argv[0]; ++my_argc;
1275 command_line[0] = '\0';
1276 (void*) fgets (command_line, 255, stdin);
1277 command_line[255] = '\0';
1278 do {
1279 my_argv[my_argc] =
1280 strtok( (my_argc == 1) ? command_line : NULL, " \n");
1281 if (my_argv[my_argc] != NULL) {
1282 ++my_argc;
1283 } else {
1284 break;
1285 }
1286 } while (my_argc < 32);
1287
1288 sh_argc_store = my_argc;
1289 sh_argv_store = my_argv;
1290
1291 (void) sh_getopt_get (my_argc, my_argv);
1292 SH_FREE (command_line);
1293 }
1294 else
1295 {
1296 /* discard command line */
1297 /* _exit(EXIT_FAILURE) */ ;
1298 }
1299 }
1300#endif
1301 sh.flag.opts = S_FALSE;
1302
1303
1304 /* close all other files
1305 */
1306 sh_unix_closeall(3, -1); /* after processing CL options */
1307
1308
1309 /* --- Get user info. ---
1310 */
1311 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1312 if (0 != sh_unix_getUser ())
1313 {
1314 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1315 sh.prg_name);
1316 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1317 }
1318
1319
1320 /* *****************************
1321 *
1322 * Read the configuration file.
1323 *
1324 * *****************************/
1325
1326 TPT((0, FIL__, __LINE__, _("msg=<Read the configuration file.>\n")))
1327 BREAKEXIT(sh_readconf_read);
1328 (void) sh_readconf_read ();
1329
1330#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1331 if (sh.flag.checkSum == SH_CHECK_NONE)
1332 {
1333 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
1334 _("No action specified: init, update, or check"),
1335 _("main"));
1336 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1337 sh.prg_name);
1338 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
1339 }
1340#endif
1341
1342 /* do not append to database if run SUID
1343 */
1344 if ((sh.flag.checkSum == SH_CHECK_INIT) && (0 != sl_is_suid()))
1345 {
1346 (void) dlog(1, FIL__, __LINE__,
1347 _("Cannot initialize database when running with SUID credentials.\nYou need to run this with the user ID %d.\nYour current user ID is %d."),
1348 (int) geteuid(), (int) sh.real.uid);
1349 sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_ACCESS,
1350 (long) sh.real.uid, sh.data.path);
1351 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1352 }
1353
1354 /* avoid daemon mode for initialization
1355 */
1356 if (sh.flag.checkSum == SH_CHECK_INIT)
1357 {
1358 sh.flag.isdaemon = S_FALSE;
1359 sh.flag.loop = S_FALSE;
1360 }
1361
1362 /* initialize signal handling etc
1363 */
1364 if (sh.flag.isdaemon == S_TRUE)
1365 sh_error_only_stderr (BAD);
1366
1367 if (sh_unix_init(sh.flag.isdaemon) == -1)
1368 {
1369 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1370 sh.prg_name);
1371 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1372 }
1373
1374 /* --- drop privileges eventually ---
1375 */
1376#if defined(SH_WITH_SERVER)
1377 sh_create_tcp_socket ();
1378#if defined(INET_SYSLOG)
1379 create_syslog_socket (S_TRUE);
1380#endif
1381 SL_REQUIRE(sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE,
1382 _("sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE"));
1383#else
1384 SL_REQUIRE(sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE,
1385 _("sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE"));
1386#endif
1387
1388 /* --- Get user info (again). ---
1389 */
1390 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1391 if (0 != sh_unix_getUser ())
1392 {
1393 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1394 sh.prg_name);
1395 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1396 }
1397
1398 /* --- now check whether we really wanted it; if not, close ---
1399 */
1400#if defined(INET_SYSLOG) && defined(SH_WITH_SERVER)
1401 create_syslog_socket (S_FALSE);
1402#endif
1403
1404
1405 /* checksum of database
1406 */
1407#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1408 TPT((0, FIL__, __LINE__, _("msg=<Get checksum of the database.>\n")))
1409 if (sh.flag.checkSum == SH_CHECK_CHECK)
1410 {
1411 if (0 == sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
1412 {
1413 /* fetch the file from server to get checksum
1414 */
1415 sh_hash_init ();
1416 /* sh_hash_hashdelete (); */
1417 }
1418 else
1419 {
1420 (void) sl_strlcpy(sh.data.hash,
1421 sh_tiger_hash (file_path('D', 'R'),
1422 TIGER_FILE, 0),
1423 KEY_LEN+1);
1424 }
1425 }
1426#endif
1427
1428
1429 /* --- Enable full error logging ---
1430 */
1431 sh_error_only_stderr (S_FALSE);
1432
1433 /****************************************************
1434 *
1435 * SERVER
1436 *
1437 ****************************************************/
1438
1439#if defined(SH_WITH_SERVER) && !defined(SH_WITH_CLIENT)
1440
1441#if (defined(WITH_GPG) || defined(WITH_PGP))
1442 /* do nothing -- we exit earlier if error
1443 if (0 != sh_gpg_check_sign (1))
1444 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1445 */
1446#else
1447 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1448 sh.prg_name, (long) sh.real.uid,
1449 (sh.flag.hidefile == S_TRUE) ?
1450 _("(hidden)") : file_path('C','R'),
1451 sh.conf.hash);
1452#endif
1453
1454#else
1455
1456 /****************************************************
1457 *
1458 * CLIENT/STANDALONE
1459 *
1460 ****************************************************/
1461
1462 BREAKEXIT(sh_error_handle);
1463
1464 if (sh.flag.checkSum == SH_CHECK_CHECK)
1465 {
1466#if (defined(WITH_GPG) || defined(WITH_PGP))
1467 /* do nothing -- we exit earlier if error
1468 if (0 != sh_gpg_check_sign (2))
1469 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1470 */
1471 ;
1472#else
1473 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_2H,
1474 sh.prg_name, (long) sh.real.uid,
1475 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash,
1476 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('D', 'R'), sh.data.hash);
1477#endif
1478 }
1479 else
1480 {
1481#if (defined(WITH_GPG) || defined(WITH_PGP))
1482 /* do nothing -- we exit earlier if error
1483 if (0 != sh_gpg_check_sign (1))
1484 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1485 */
1486 ;
1487#else
1488 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1489 sh.prg_name, (long) sh.real.uid,
1490 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash);
1491#endif
1492 }
1493#endif
1494
1495
1496 if ((skey == NULL) || (skey->mlock_failed == SL_TRUE))
1497 sh_error_handle ((-1), FIL__, __LINE__, EPERM, MSG_MLOCK);
1498
1499 /* timer
1500 */
1501 tcurrent = time (NULL);
1502 told = tcurrent;
1503 sh.mailTime.alarm_last = told;
1504
1505
1506 /****************************************************
1507 *
1508 * SERVER
1509 *
1510 ****************************************************/
1511
1512#if defined(SH_WITH_SERVER)
1513 TPT((0, FIL__, __LINE__, _("msg=<Start server.>\n")))
1514
1515#if defined (SH_WITH_CLIENT)
1516 if (sh.flag.isserver == S_TRUE)
1517 {
1518 sh_receive();
1519 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1520 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1521 }
1522#else
1523 sh_receive();
1524 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1525 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1526#endif
1527
1528#endif
1529
1530 /****************************************************
1531 *
1532 * CLIENT/STANDALONE
1533 *
1534 ****************************************************/
1535#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1536
1537
1538 /* --- Initialize modules. ---
1539 */
1540 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")))
1541 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1542 {
1543 if (0 != (status = modList[modnum].mod_init()))
1544 {
1545 if (status == (-1)) {
1546 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, status,
1547 MSG_MOD_FAIL,
1548 _(modList[modnum].name),
1549 status);
1550 } else {
1551 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_FAIL,
1552 _(modList[modnum].name),
1553 status);
1554 }
1555 modList[modnum].initval = S_FALSE;
1556 }
1557 else
1558 {
1559 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1560 _(modList[modnum].name));
1561 modList[modnum].initval = S_TRUE;
1562 }
1563 }
1564
1565 /* -------- TEST SETUP ---------
1566 */
1567 (void) sh_files_setrec();
1568 (void) sh_files_test_setup();
1569
1570
1571 /* -------- NICE LEVEL ---------
1572 */
1573 if (0 != sh.flag.nice)
1574 {
1575#ifdef HAVE_SETPRIORITY
1576 /*@-unrecog@*/
1577 (void) setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1578 /*@+unrecog@*/
1579#else
1580 (void) nice(sh.flag.nice);
1581#endif
1582 }
1583
1584 /* -------- MAIN LOOP ---------
1585 */
1586 sh.statistics.bytes_speed = 0;
1587 sh.statistics.bytes_hashed = 0;
1588
1589 while (1 == 1)
1590 {
1591 ++cct;
1592
1593 BREAKEXIT(sh_error_handle);
1594
1595 TPT((0, FIL__, __LINE__, _("msg=<Start main loop.>, iter=<%ld>\n"), cct))
1596
1597 tcurrent = time (NULL);
1598
1599 if (sig_raised > 0)
1600 {
1601
1602 TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
1603
1604 if (sig_termfast == 1) /* SIGTERM */
1605 {
1606 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1607 /* strncpy (sh_sig_msg, _("SIGTERM"), 20); */
1608 --sig_raised; --sig_urgent;
1609 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1610 }
1611
1612 if (sig_force_check == 1) /* SIGTTOU */
1613 {
1614 TPT((0, FIL__, __LINE__, _("msg=<Check run triggered.>\n")));
1615 flag_check_1 = 1;
1616 flag_check_2 = 1;
1617 sig_force_check = 0;
1618 --sig_raised;
1619 }
1620
1621 if (sig_config_read_again == 1) /* SIGHUP */
1622 {
1623 TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")))
1624 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
1625
1626#if defined(WITH_EXTERNAL)
1627 /* delete list of external tasks
1628 */
1629 (void) sh_ext_cleanup();
1630#endif
1631 /* delete the file list, make all database
1632 * entries visible (allignore = FALSE)
1633 */
1634 (void) sh_files_deldirstack ();
1635 (void) sh_files_delfilestack ();
1636 (void) sh_ignore_clean ();
1637 (void) hash_full_tree ();
1638
1639#if defined(SH_WITH_CLIENT)
1640 reset_count_dev_server();
1641#endif
1642#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
1643
1644
1645 FileSchedOne = free_sched(FileSchedOne);
1646 FileSchedTwo = free_sched(FileSchedTwo);
1647
1648 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1649 {
1650 if (modList[modnum].initval == GOOD)
1651 (void) modList[modnum].mod_reconf();
1652 }
1653#endif
1654
1655#if defined(SH_WITH_MAIL)
1656 reset_count_dev_mail();
1657#endif
1658 reset_count_dev_console();
1659 reset_count_dev_time();
1660
1661 (void) sh_unix_maskreset();
1662
1663 /* Should this be included ???
1664 * (i.e. should we reload the database ?)
1665 */
1666#ifdef RELOAD_DATABASE
1667 sh_hash_hashdelete();
1668#endif
1669 (void) sl_trust_purge_user();
1670 (void) sh_files_hle_reg (NULL);
1671 (void) sh_prelink_run (NULL, NULL, 0);
1672
1673 /* --------------------------
1674 * --- READ CONFIGURATION ---
1675 * --------------------------
1676 */
1677 (void) sh_readconf_read ();
1678 sig_config_read_again = 0;
1679 (void) sh_files_setrec();
1680 (void) sh_files_test_setup();
1681 if (0 != sh.flag.nice)
1682 {
1683#ifdef HAVE_SETPRIORITY
1684 setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1685#else
1686 nice(sh.flag.nice);
1687#endif
1688 }
1689
1690 if (sh.flag.checkSum == SH_CHECK_INIT)
1691 {
1692 sh.flag.isdaemon = S_FALSE;
1693 sh.flag.loop = S_FALSE;
1694 }
1695
1696 /* --- Initialize modules. ---
1697 */
1698 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")));
1699 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1700 {
1701 if (0 != (status = modList[modnum].mod_init()))
1702 {
1703 if (status == (-1)) {
1704 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__,
1705 status, MSG_MOD_FAIL,
1706 _(modList[modnum].name),
1707 status);
1708 } else {
1709 sh_error_handle ((-1), FIL__, __LINE__,
1710 status, MSG_MOD_FAIL,
1711 _(modList[modnum].name),
1712 status);
1713 }
1714 modList[modnum].initval = S_FALSE;
1715 }
1716 else
1717 {
1718 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1719 _(modList[modnum].name));
1720 modList[modnum].initval = S_TRUE;
1721 }
1722 }
1723
1724 --sig_raised;
1725 }
1726
1727 if (sig_fresh_trail == 1) /* SIGIOT */
1728 {
1729 /* Logfile access
1730 */
1731#ifdef SH_USE_XML
1732 (void) sh_log_file (NULL, NULL);
1733#endif
1734 TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
1735 sh_error_only_stderr (S_TRUE);
1736 (void) sh_unix_rm_lock_file(sh.srvlog.name);
1737 (void) retry_msleep(3, 0);
1738 sh.flag.log_start = S_TRUE;
1739 sh_error_only_stderr (S_FALSE);
1740 sig_fresh_trail = 0;
1741 --sig_raised;
1742 }
1743
1744 if (sig_terminate == 1) /* SIGQUIT */
1745 {
1746 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1747 strncpy (sh_sig_msg, _("Quit"), 20);
1748 --sig_raised; --sig_urgent;
1749 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1750 }
1751
1752 if (sig_debug_switch == 1) /* SIGUSR1 */
1753 {
1754 TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
1755 sh_error_dbg_switch();
1756 sig_debug_switch = 0;
1757 --sig_raised;
1758 }
1759
1760 if (sig_suspend_switch > 0) /* SIGUSR2 */
1761 {
1762 TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
1763 if (sh_global_suspend_flag == 1) {
1764 sh_global_suspend_flag = 0;
1765 } else {
1766 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND,
1767 sh.prg_name);
1768 sh_global_suspend_flag = 1;
1769 }
1770 --sig_suspend_switch;
1771 --sig_raised; --sig_urgent;
1772 }
1773 sig_raised = (sig_raised < 0) ? 0 : sig_raised;
1774 sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
1775 TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
1776 }
1777
1778 if (sh_global_suspend_flag == 1)
1779 {
1780 (void) retry_msleep (1, 0);
1781 continue;
1782 }
1783
1784 /* see whether its time to check files
1785 */
1786 if (sh.flag.checkSum == SH_CHECK_INIT)
1787 {
1788 flag_check_1 = 1;
1789 if (FileSchedTwo != NULL)
1790 flag_check_2 = 1;
1791 }
1792 else if (sh.flag.checkSum == SH_CHECK_CHECK ||
1793 (sh.flag.update == S_TRUE &&
1794 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE)))
1795 {
1796 if (FileSchedOne == NULL)
1797 {
1798 /* use interval if we have no schedule
1799 */
1800 if (tcurrent - sh.fileCheck.alarm_last >=
1801 sh.fileCheck.alarm_interval)
1802 flag_check_1 = 1;
1803 }
1804 else
1805 {
1806 flag_check_1 = test_sched(FileSchedOne);
1807 if (FileSchedTwo != NULL)
1808 flag_check_2 = test_sched(FileSchedTwo);
1809 if (flag_check_2 == 1)
1810 flag_check_1 = 1;
1811 }
1812 }
1813
1814 check_done = 0;
1815
1816 if (sh.flag.checkSum != SH_CHECK_NONE &&
1817 (flag_check_1 == 1 || flag_check_2 == 1))
1818 {
1819 /*
1820 * check directories and files
1821 * ORDER IS IMPORTANT -- DIRZ FIRST
1822 */
1823 sh.statistics.bytes_hashed = 0;
1824 sh.statistics.time_start = time (NULL);
1825 sh.statistics.dirs_checked = 0;
1826 sh.statistics.files_checked = 0;
1827
1828 TPT((0, FIL__, __LINE__, _("msg=<Check directories.>\n")))
1829 BREAKEXIT(sh_dirs_chk);
1830 if (flag_check_1 == 1)
1831 {
1832 (void) sh_dirs_chk (1);
1833#ifndef SH_PROFILE
1834 (void) chdir ("/");
1835#endif
1836 }
1837 if (flag_check_2 == 1)
1838 {
1839 (void) sh_dirs_chk (2);
1840#ifndef SH_PROFILE
1841 (void) chdir ("/");
1842#endif
1843 }
1844 TPT((0, FIL__, __LINE__, _("msg=<Check files.>\n")))
1845 BREAKEXIT(sh_files_chk);
1846 if (flag_check_1 == 1)
1847 (void) sh_files_chk ();
1848
1849 if (sig_urgent > 0)
1850 continue;
1851
1852 /*
1853 * check for files not visited
1854 */
1855 if (flag_check_2 == 1 || FileSchedTwo == NULL)
1856 {
1857 TPT((0, FIL__, __LINE__, _("msg=<Check for missing files.>\n")))
1858 sh_hash_unvisited (ShDFLevel[SH_ERR_T_FILE]);
1859 }
1860
1861 if (sig_urgent > 0)
1862 continue;
1863
1864 /* reset
1865 */
1866 TPT((0, FIL__, __LINE__, _("msg=<Reset status.>\n")))
1867 sh_dirs_reset ();
1868 if (sig_urgent > 0)
1869 continue;
1870
1871 sh_files_reset ();
1872 flag_check_1 = 0;
1873 flag_check_2 = 0;
1874 check_done = 1;
1875
1876 (void) sh_prelink_run (NULL, NULL, 0);
1877
1878 if (sig_urgent > 0)
1879 continue;
1880
1881 runtim = time(NULL) - sh.statistics.time_start;
1882 sh.statistics.time_check = runtim;
1883
1884 if ((sh.statistics.dirs_checked == 0) &&
1885 (sh.statistics.files_checked == 0))
1886 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_0);
1887
1888 else
1889 {
1890 st_1 = (float) sh.statistics.bytes_hashed;
1891 st_2 = (float) runtim;
1892
1893
1894 if (st_1 > FLT_EPSILON && st_2 > FLT_EPSILON)
1895 st_1 = st_1/st_2;
1896 else if (st_1 > FLT_EPSILON)
1897 st_1 = (float) (st_1 * 1.0);
1898 else
1899 st_1 = 0.0;
1900
1901 sh.statistics.bytes_speed = (unsigned long) st_1;
1902
1903 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_1,
1904 (long) runtim,
1905 0.001 * st_1);
1906 }
1907 sh.fileCheck.alarm_last = time (NULL);
1908
1909 if (sig_urgent > 0)
1910 continue;
1911
1912 /*
1913 * flush mail queue
1914 */
1915#if defined(SH_WITH_MAIL)
1916 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
1917 (void) sh_mail_msg (NULL);
1918#endif
1919 }
1920
1921 if (sig_urgent > 0)
1922 continue;
1923
1924 /* execute modules
1925 */
1926 TPT((0, FIL__, __LINE__, _("msg=<Execute modules.>\n")))
1927 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1928 {
1929 if (modList[modnum].initval == GOOD &&
1930 0 != modList[modnum].mod_timer(tcurrent))
1931 if (0 != (status = modList[modnum].mod_check()))
1932 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_EXEC,
1933 _(modList[modnum].name), (long) status);
1934 }
1935
1936 /* 27.05.2002 avoid empty database
1937 * 22.10.2002 moved here b/o suid check initialization
1938 */
1939 if (sh.flag.checkSum == SH_CHECK_INIT)
1940 sh_hash_pushdata (NULL, NULL);
1941
1942 /* write out database
1943 */
1944 if (sh.flag.checkSum == SH_CHECK_CHECK &&
1945 sh.flag.update == S_TRUE &&
1946 check_done == 1)
1947 sh_hash_writeout ();
1948
1949 /* no loop if not daemon
1950 */
1951 if (sh.flag.isdaemon != S_TRUE && sh.flag.loop == S_FALSE)
1952 break;
1953 if (sig_urgent > 0)
1954 continue;
1955
1956 /* see whether its time to send mail
1957 */
1958#if defined(SH_WITH_MAIL)
1959 if (tcurrent - sh.mailTime.alarm_last >= sh.mailTime.alarm_interval)
1960 {
1961 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
1962 (void) sh_mail_msg (NULL);
1963 sh.mailTime.alarm_last = time (NULL);
1964 }
1965#endif
1966 if (sig_urgent > 0)
1967 continue;
1968
1969 /* log the timestamp
1970 */
1971 if ((int)(tcurrent - told) >= sh.looptime )
1972 {
1973 TPT((0, FIL__, __LINE__, _("msg=<Log the timestamp.>\n")))
1974 told = tcurrent;
1975#ifdef MEM_DEBUG
1976 sh_mem_check();
1977 sh_unix_count_mlock();
1978#else
1979 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_STAMP);
1980#endif
1981 }
1982
1983 /* seed / re-seed the PRNG if required
1984 */
1985 (void) taus_seed();
1986
1987 if (sig_urgent > 0)
1988 continue;
1989
1990 /* go to sleep
1991 */
1992 (void) retry_msleep (1, 0);
1993
1994 BREAKEXIT(sh_derr);
1995 (void) sh_derr();
1996 }
1997
1998 /* ------ END -----------
1999 */
2000
2001
2002
2003 /*
2004 * cleanup
2005 */
2006 TPT((0, FIL__, __LINE__, _("msg=<Cleanup.>\n")));
2007 sh_hash_hashdelete();
2008
2009#if defined(SH_WITH_MAIL)
2010 if (sh.mailNum.alarm_last > 0)
2011 (void)sh_mail_msg (NULL);
2012#endif
2013
2014 /* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
2015#endif
2016
2017 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
2018 SL_RETURN(0, _("main"));
2019}
Note: See TracBrowser for help on using the repository browser.