source: trunk/src/samhain.c@ 487

Last change on this file since 487 was 487, checked in by katerina, 10 years ago

Fix for ticket #385 (self_test not working with exit on sigterm).

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