source: trunk/src/samhain.c@ 481

Last change on this file since 481 was 481, checked in by katerina, 9 years ago

Enhancements and fixes for tickets #374, #375, #376, #377, #378, and #379.

File size: 52.8 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 (void) sh_unix_self_check();
800
801
802 /* --- Exit Message. ---
803 */
804 if (sh.flag.exit == EXIT_FAILURE && !strcmp(sh_sig_msg, _("None")))
805 sl_strlcpy(sh_sig_msg, _("exit_failure"), SH_MINIBUF);
806 if (sh.flag.exit == EXIT_SUCCESS && !strcmp(sh_sig_msg, _("None")))
807 sl_strlcpy(sh_sig_msg, _("exit_success"), SH_MINIBUF);
808 sh_error_handle ((-1), FIL__, __LINE__, sh.flag.exit, MSG_EXIT_NORMAL,
809 sh.prg_name, sh_sig_msg);
810#ifdef SH_USE_XML
811 (void) sh_log_file (NULL, NULL);
812#endif
813
814
815 /* --- Restrict error logging to stderr. ---
816 */
817 close_ipc ();
818 sh_error_only_stderr (S_TRUE);
819
820 /* --- Remove lock, delete critical information. ---
821 */
822 (void) sh_unix_rm_lock_file (sh.srvlog.name);
823 if (sh.flag.isdaemon == S_TRUE)
824 (void) sh_unix_rm_pid_file ();
825 if (skey != NULL)
826 memset (skey, (int) '\0', sizeof(sh_key_t));
827
828 /* --- Exit. ---
829 */
830 SL_RET0(_("exit_handler"));
831}
832
833/***********************************************************
834 *
835 */
836#ifndef SIGHUP
837#define SIGHUP 1
838#endif
839#ifndef SIGTERM
840#define SIGTERM 15
841#endif
842#ifndef SIGKILL
843#define SIGKILL 9
844#endif
845
846#if defined(__linux__) || defined(sun) || defined(__sun) || defined(__sun__)
847#include <dirent.h>
848static pid_t * procdirSamhain (void)
849{
850 pid_t * pidlist;
851 struct dirent * d;
852 DIR * dp;
853 long ino;
854 struct stat buf;
855 int i;
856 pid_t pid, mypid = getpid();
857 char * tail;
858 char exef[128];
859
860 if (0 != stat(SH_INSTALL_PATH, &buf))
861 {
862 return NULL;
863 }
864
865 ino = (long) buf.st_ino;
866
867 if (NULL == (dp = opendir(_("/proc"))))
868 {
869 return NULL;
870 }
871
872 SH_MUTEX_LOCK(mutex_readdir);
873
874 pidlist = calloc(1, sizeof(pid_t) * 65535);
875 if (!pidlist)
876 goto unlock_and_out;
877
878 for (i = 0; i < 65535; ++i) pidlist[i] = 0;
879
880 i = 0;
881 while (NULL != (d = readdir(dp)) && i < 65535)
882 {
883 if (0 != strcmp(d->d_name, ".") && 0 != strcmp(d->d_name, ".."))
884 {
885 errno = 0;
886 pid = (pid_t) strtol (d->d_name, &tail, 0);
887 if (*tail != '\0' || errno != 0)
888 continue;
889 if (pid == mypid)
890 continue;
891#if defined(__linux__)
892 sprintf(exef, _("/proc/%d/exe"), (int) pid); /* known to fit */
893#else
894 sprintf(exef, _("/proc/%d/object/a.out"), /* known to fit */
895 (int) pid);
896#endif
897 if (0 == stat(exef, &buf) && ino == (long) buf.st_ino)
898 { pidlist[i] = (pid_t) pid; ++i; }
899 }
900 }
901
902 unlock_and_out:
903 ;
904 SH_MUTEX_UNLOCK(mutex_readdir);
905
906 closedir(dp);
907 return pidlist;
908}
909#else
910static pid_t * procdirSamhain (void)
911{
912 return NULL;
913}
914#endif
915
916static int killprocSamhain (pid_t pid)
917{
918 int i;
919
920 /* fprintf(stderr, "Killing %d\n", pid); */
921 if (pid > 0 && 0 == kill (pid, SIGTERM))
922 {
923 for (i = 0; i < 16; ++i)
924 {
925 (void) retry_msleep(1, 0);
926 if (0 != kill (pid, 0) && errno == ESRCH)
927 return (0);
928 }
929
930 (void) kill (pid, SIGKILL);
931 return (0);
932 }
933 if (pid > 0)
934 {
935 if (errno == ESRCH)
936 return 7;
937 if (errno == EPERM)
938 return 4;
939 return 1;
940 }
941 else
942 return (7);
943}
944
945static pid_t pidofSamhain (int flag)
946{
947 FILE * fp;
948 char line[256];
949 char * tail;
950 char * p;
951 pid_t pid;
952 long inpid;
953 struct stat buf;
954
955 fp = fopen (DEFAULT_ERRLOCK, "r");
956
957 if (!fp)
958 { if (errno != ENOENT) perror(_("fopen")); return 0; }
959 if (NULL == fgets(line, sizeof(line), fp))
960 { perror(_("fgets")); (void) sl_fclose(FIL__, __LINE__, fp); return 0; }
961 (void) sl_fclose(FIL__, __LINE__, fp);
962 p = line;
963 while (*p == ' ' || *p == '\f' || *p == '\n' ||
964 *p == '\r' || *p == '\t' || *p == '\v')
965 ++p;
966 errno = 0;
967 inpid = strtol (p, &tail, 0);
968 if (p == tail || errno != 0)
969 { perror(_("strtol")); return 0; }
970
971 pid = (pid_t) inpid;
972 if (inpid != (long) pid)
973 { perror(_("strtol")); return 0; }
974
975 /* remove stale pid file
976 */
977 if (flag == 1 && pid > 0 && 0 != kill(pid, 0) && errno == ESRCH)
978 {
979 if /*@-unrecog@*/ (0 == lstat (DEFAULT_ERRLOCK, &buf))/*@+unrecog@*/
980 {
981 if /*@-usedef@*/(S_ISREG(buf.st_mode))/*@+usedef@*/
982 {
983 (void) unlink(DEFAULT_ERRLOCK);
984 }
985 }
986 else
987 {
988 perror(_("lstat")); return 0;
989 }
990 pid = 0;
991 }
992 return pid;
993}
994
995/* 1: start 2:stop 3:reload 4:status
996 */
997/*@-exitarg@*/
998static int samhainctl(int ctl, int * argc, char * argv[])
999{
1000 char * fullpath;
1001 pid_t pid;
1002 int status;
1003 int res;
1004 pid_t respid;
1005 int times;
1006 char * argp[32];
1007 pid_t * pidlist;
1008 int i;
1009#ifdef WCONTINUED
1010 int wflags = WNOHANG|WUNTRACED|WCONTINUED;
1011#else
1012 int wflags = WNOHANG|WUNTRACED;
1013#endif
1014
1015 fullpath = strdup (SH_INSTALL_PATH);
1016 if (fullpath == NULL)
1017 { perror(_("strdup")); exit (1); }
1018
1019 argp[0] = strdup (SH_INSTALL_PATH);
1020 if (argp[0] == NULL)
1021 { perror(_("strdup")); exit (1); }
1022
1023 for (times = 1; times < 32; ++times) argp[times] = NULL;
1024
1025 res = (*argc > 32) ? 32 : *argc;
1026
1027 for (times = 2; times < res; ++times)
1028 {
1029 argp[times-1] = strdup (argv[times]);
1030 if (argp[times-1] == NULL)
1031 { perror(_("strdup")); exit (1); }
1032 }
1033
1034 if (ctl == 1)
1035 {
1036 pid = pidofSamhain(1);
1037
1038 if (pid != 0 && 0 == kill (pid, 0)) /* already started */
1039 exit (0);
1040
1041 pid = fork();
1042 switch (pid) {
1043 case ((pid_t) -1):
1044 perror(_("fork"));
1045 exit (1);
1046 case 0:
1047 if (0 != sl_close_fd (FIL__, __LINE__, 0))
1048 {
1049 _exit(4);
1050 }
1051 (void) execv(fullpath, argp); /* flawfinder: ignore *//* wtf? */
1052 if (errno == EPERM)
1053 _exit(4);
1054 else if (errno == ENOENT)
1055 _exit(5);
1056 _exit (1);
1057 default:
1058 times = 0;
1059 while (times < 300) {
1060 respid = waitpid(pid, &status, wflags);
1061 if ((pid_t)-1 == respid)
1062 {
1063 perror(_("waitpid"));
1064 exit (1);
1065 }
1066 else if (pid == respid)
1067 {
1068#ifndef USE_UNO
1069 if (0 != WIFEXITED(status))
1070 {
1071 res = WEXITSTATUS(status);
1072 exit (res == 0 ? 0 : res );
1073 }
1074 else
1075 exit (1);
1076#else
1077 exit (1);
1078#endif
1079 }
1080 ++times;
1081 (void) retry_msleep(1, 0);
1082 }
1083 exit (0); /* assume that it runs ok */
1084 }
1085 }
1086
1087 pid = pidofSamhain(0);
1088
1089 if (ctl == 2) /* stop */
1090 {
1091 pidlist = procdirSamhain ();
1092 if (pid == 0 && NULL == pidlist) /* pid file not found */
1093 {
1094 free(fullpath);
1095 return (0);
1096 }
1097
1098 status = 0;
1099 if (pid != 0)
1100 status = killprocSamhain(pid);
1101 if (pidlist != NULL)
1102 {
1103 i = 0;
1104 while (i < 65535 && pidlist[i] != 0)
1105 {
1106 if (pidlist[i] != pid)
1107 status = killprocSamhain(pidlist[i]);
1108 ++i;
1109 }
1110 }
1111 free(fullpath);
1112 if (status == 7)
1113 return 0;
1114 else
1115 return status;
1116 }
1117
1118 if (ctl == 3) /* reload */
1119 {
1120 if (pid == 0)
1121 exit (7);
1122 if (0 == kill (pid, SIGHUP))
1123 exit (0);
1124 else
1125 {
1126 if (errno == EPERM)
1127 exit (4);
1128 if (errno == ESRCH)
1129 exit (7);
1130 exit (1);
1131 }
1132 }
1133
1134 if (ctl == 4) /* status */
1135 {
1136 if (pid == 0)
1137 exit (3);
1138 if (0 == kill (pid, 0))
1139 exit (0);
1140 else
1141 {
1142 if (errno == EPERM)
1143 exit (4);
1144 if (errno == ESRCH)
1145 exit (1);
1146 }
1147 }
1148 free(fullpath); /* silence smatch false positive */
1149 exit (1); /* no exit handler installed yet */
1150 /*@notreached@*/
1151 return (0);
1152}
1153/*@+exitarg@*/
1154
1155#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1156#include "sh_schedule.h"
1157static sh_schedule_t * FileSchedOne = NULL;
1158static sh_schedule_t * FileSchedTwo = NULL;
1159
1160/* free a linked list of schedules
1161 */
1162static sh_schedule_t * free_sched (sh_schedule_t * isched)
1163{
1164 sh_schedule_t * current = isched;
1165 sh_schedule_t * next = NULL;
1166
1167 while (current != NULL)
1168 {
1169 next = current->next;
1170 SH_FREE(current);
1171 current = next;
1172 }
1173 return NULL;
1174}
1175
1176/* Add a new schedule to the linked list of schedules
1177 */
1178static sh_schedule_t * sh_set_schedule_int (const char * str,
1179 sh_schedule_t * FileSchedIn,
1180 /*@out@*/ int * status)
1181{
1182 sh_schedule_t * FileSched;
1183
1184 SL_ENTER(_("sh_set_schedule_int"));
1185
1186 if (0 == sl_strncmp(str, _("NULL"), 4))
1187 {
1188 (void) free_sched(FileSchedIn);
1189 FileSchedIn = NULL;
1190 *status = 0;
1191 return NULL;
1192 }
1193
1194 FileSched = SH_ALLOC(sizeof(sh_schedule_t));
1195 *status = create_sched(str, FileSched);
1196 if (*status != 0)
1197 {
1198 SH_FREE(FileSched);
1199 FileSched = NULL;
1200 SL_RETURN(FileSchedIn , _("sh_set_schedule_int"));
1201 }
1202 FileSched->next = FileSchedIn;
1203 SL_RETURN(FileSched , _("sh_set_schedule_int"));
1204}
1205
1206/* Add a new schedule to the linked list FileSchedOne
1207 */
1208int sh_set_schedule_one (const char * str)
1209{
1210 int status;
1211 FileSchedOne = sh_set_schedule_int (str, FileSchedOne, &status);
1212 return status;
1213}
1214
1215/* Add a new schedule to the linked list FileSchedTwo
1216 */
1217int sh_set_schedule_two (const char * str)
1218{
1219 int status;
1220 FileSchedTwo = sh_set_schedule_int (str, FileSchedTwo, &status);
1221 return status;
1222}
1223
1224
1225void do_reconf()
1226{
1227 int status, modnum;
1228
1229 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
1230
1231 sh_thread_pause_flag = S_TRUE;
1232
1233#if defined(WITH_EXTERNAL)
1234 /* delete list of external tasks
1235 */
1236 (void) sh_ext_cleanup();
1237#endif
1238#if defined(SH_WITH_MAIL)
1239 sh_nmail_free();
1240#endif
1241
1242 /* delete the file list, make all database
1243 * entries visible (allignore = FALSE)
1244 */
1245 (void) sh_files_deldirstack ();
1246 (void) sh_files_delfilestack ();
1247 (void) sh_files_delglobstack ();
1248 (void) sh_ignore_clean ();
1249 (void) hash_full_tree ();
1250 sh_audit_delete_all ();
1251
1252
1253#if defined(SH_WITH_CLIENT)
1254 reset_count_dev_server();
1255#endif
1256#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
1257 sh_restrict_purge ();
1258
1259
1260 FileSchedOne = free_sched(FileSchedOne);
1261 FileSchedTwo = free_sched(FileSchedTwo);
1262
1263 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1264 {
1265 /* sh_thread_pause_flag is true, and we block in lock
1266 * until check has returned, so we are sure check will
1267 * not run until sh_thread_pause_flag is set to false
1268 */
1269 /* if (modList[modnum].initval >= SH_MOD_ACTIVE) */
1270 (void) modList[modnum].mod_reconf();
1271 }
1272#endif
1273
1274 reset_count_dev_console();
1275 reset_count_dev_time();
1276
1277 (void) sh_unix_maskreset();
1278
1279#ifdef RELOAD_DATABASE
1280 sh_hash_hashdelete();
1281
1282 if (0 != sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
1283 {
1284 char hashbuf[KEYBUF_SIZE];
1285 (void) sl_strlcpy(sh.data.hash,
1286 sh_tiger_hash (file_path('D', 'R'),
1287 TIGER_FILE, TIGER_NOLIM,
1288 hashbuf, sizeof(hashbuf)),
1289 KEY_LEN+1);
1290 }
1291#endif
1292 (void) sl_trust_purge_user();
1293 (void) sh_files_hle_reg (NULL);
1294 (void) sh_prelink_run (NULL, NULL, 0, 0);
1295
1296 /* --------------------------
1297 * --- READ CONFIGURATION ---
1298 * --------------------------
1299 */
1300 (void) sh_readconf_read ();
1301 sig_config_read_again = 0;
1302 (void) sh_files_setrec();
1303 (void) sh_files_test_setup();
1304 sh_audit_commit ();
1305
1306 if (0 != sh.flag.nice)
1307 {
1308#ifdef HAVE_SETPRIORITY
1309 setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1310#else
1311 nice(sh.flag.nice);
1312#endif
1313 }
1314
1315 if (sh.flag.checkSum == SH_CHECK_INIT)
1316 {
1317 sh.flag.isdaemon = S_FALSE;
1318 sh.flag.loop = S_FALSE;
1319 }
1320
1321
1322 /* --- Initialize modules. ---
1323 */
1324 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")));
1325 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1326 {
1327 status = modList[modnum].mod_init(&(modList[modnum]));
1328
1329 if (status < 0)
1330 {
1331 if (status == (-1)) {
1332 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__,
1333 status, MSG_MOD_FAIL,
1334 _(modList[modnum].name),
1335 status+SH_MOD_OFFSET);
1336 } else {
1337 sh_error_handle ((-1), FIL__, __LINE__,
1338 status, MSG_MOD_FAIL,
1339 _(modList[modnum].name),
1340 status+SH_MOD_OFFSET);
1341 }
1342 modList[modnum].initval = SH_MOD_FAILED;
1343 }
1344 else
1345 {
1346 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1347 _(modList[modnum].name));
1348 modList[modnum].initval = status;
1349 }
1350 }
1351
1352 /* module is properly set up now
1353 */
1354 sh_thread_pause_flag = S_FALSE;
1355
1356 return;
1357}
1358
1359static void check_signals (volatile int *flag_check_1,
1360 volatile int * flag_check_2)
1361{
1362 if (sig_raised > 0)
1363 {
1364
1365 TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
1366
1367 if (sig_termfast == 1) /* SIGTERM */
1368 {
1369 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1370 --sig_raised; --sig_urgent;
1371 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1372 }
1373
1374 if (sig_force_check == 1) /* SIGTTOU */
1375 {
1376 TPT((0, FIL__, __LINE__, _("msg=<Check run triggered.>\n")));
1377 *flag_check_1 = 1; *flag_check_2 = 1;
1378 sig_force_check = 0; --sig_raised;
1379 sh_sem_trylock();
1380 }
1381
1382 if (sig_config_read_again == 1 && /* SIGHUP */
1383 sh_global_suspend_flag == 0)
1384 {
1385 TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")));
1386 do_reconf();
1387 --sig_raised;
1388 }
1389
1390 if (sig_fresh_trail == 1) /* SIGIOT */
1391 {
1392 if (sh_global_suspend_flag == 0)
1393 {
1394 SH_MUTEX_LOCK(mutex_thread_nolog);
1395
1396 /* Logfile access
1397 */
1398#ifdef SH_USE_XML
1399 (void) sh_log_file (NULL, NULL);
1400#endif
1401 TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
1402 sh_error_only_stderr (S_TRUE);
1403 (void) sh_unix_rm_lock_file(sh.srvlog.name);
1404 (void) retry_msleep(3, 0);
1405 sh.flag.log_start = S_TRUE;
1406 sh_error_only_stderr (S_FALSE);
1407 sh_thread_pause_flag = S_FALSE;
1408 sig_fresh_trail = 0;
1409 --sig_raised;
1410 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1411 }
1412 }
1413
1414 if (sig_terminate == 1) /* SIGQUIT */
1415 {
1416 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1417 strncpy (sh_sig_msg, _("Quit"), 20);
1418 --sig_raised;
1419 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1420 }
1421
1422 if (sig_debug_switch == 1) /* SIGUSR1 */
1423 {
1424 TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
1425 sh_error_dbg_switch();
1426 sig_debug_switch = 0;
1427 --sig_raised;
1428 }
1429
1430 if (sig_suspend_switch > 0) /* SIGUSR2 */
1431 {
1432 TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
1433 if (sh_global_suspend_flag != 1) {
1434 SH_MUTEX_LOCK_UNSAFE(mutex_thread_nolog);
1435 sh_global_suspend_flag = 1;
1436 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND,
1437 sh.prg_name);
1438 } else {
1439 sh_global_suspend_flag = 0;
1440 SH_MUTEX_UNLOCK_UNSAFE(mutex_thread_nolog);
1441 }
1442 --sig_suspend_switch;
1443 --sig_raised; --sig_urgent;
1444 }
1445
1446 if (sh_load_delta_flag > 0 &&
1447 sh_global_suspend_flag == 0) /* DELTA Command */
1448 {
1449 if (0 == sh_dbIO_load_delta())
1450 {
1451 --sh_load_delta_flag; --sig_raised;
1452 }
1453 }
1454
1455 sig_raised = (sig_raised < 0) ? 0 : sig_raised;
1456 sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
1457 TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
1458 }
1459 return;
1460}
1461
1462void check_for_delta_db()
1463{
1464 /* Need to contact the server.
1465 */
1466 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_2);
1467
1468 if (sig_raised > 0 && sh_load_delta_flag > 0) /* DELTA Command */
1469 {
1470 /* There was a DELTA Command
1471 */
1472 if (0 == sh_dbIO_load_delta())
1473 {
1474 --sh_load_delta_flag; --sig_raised;
1475 }
1476 }
1477 return;
1478}
1479
1480#endif
1481
1482/*******************************************************
1483 *
1484 * Main program
1485 *
1486 *******************************************************/
1487#if !defined(SH_CUTEST)
1488int main(int argc, char * argv[])
1489#else
1490int undef_main(int argc, char * argv[])
1491#endif
1492{
1493#if defined(INET_SYSLOG)
1494 extern int sh_xfer_create_syslog_socket (int flag);
1495#endif
1496#if defined(SH_WITH_SERVER)
1497 extern int sh_create_tcp_socket(void);
1498#endif
1499
1500#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1501 int modnum;
1502 time_t runtim;
1503 float st_1, st_2;
1504 int status;
1505 volatile long cct = 0; /* main loop iterations */
1506
1507 volatile int flag_check_1 = 0;
1508 volatile int flag_check_2 = 0;
1509
1510 int check_done = 0;
1511#endif
1512
1513 volatile time_t told;
1514 volatile time_t tcurrent;
1515 size_t tzlen;
1516 char * tzptr;
1517 int res;
1518
1519#if defined (SH_STEALTH_NOCL)
1520 char command_line[256];
1521 int my_argc = 0;
1522 char * my_argv[32];
1523#endif
1524
1525#if !defined(USE_SYSTEM_MALLOC)
1526 typedef void assert_handler_tp(const char * error, const char *file, int line);
1527 extern assert_handler_tp *dnmalloc_set_handler(assert_handler_tp *new);
1528 (void) dnmalloc_set_handler(safe_fatal);
1529#endif
1530
1531 SH_G_INIT; /* Must precede any use of _() */
1532
1533 SL_ENTER(_("main"));
1534
1535 /* --- Close all but first three file descriptors. ---
1536 */
1537 sh_unix_closeall(3, -1, S_FALSE); /* at program start */
1538
1539
1540 if (argc >= 2 && 0 != getuid() &&
1541 (0 == strcmp(argv[1], _("start")) ||
1542 0 == strcmp(argv[1], _("stop")) ||
1543 0 == strcmp(argv[1], _("reload")) ||
1544 0 == strcmp(argv[1], _("force-reload")) ||
1545 0 == strcmp(argv[1], _("status")) ||
1546 0 == strcmp(argv[1], _("restart"))))
1547 {
1548 return 4;
1549 }
1550
1551 if (argc >= 2 && 0 == getuid())
1552 {
1553 /* return codes:
1554 * 0 Success
1555 * 1 Can not send signal / start program
1556 * 2 Pid file does not exist
1557 */
1558 if (0 == strcmp(argv[1], _("start")))
1559 {
1560 (void) samhainctl (1, &argc, argv); /* does not return */
1561 }
1562 else if (0 == strcmp(argv[1], _("stop")))
1563 return (samhainctl (2, &argc, argv));
1564 else if (0 == strcmp(argv[1], _("reload")))
1565 (void) samhainctl (3, &argc, argv); /* does not return */
1566 else if (0 == strcmp(argv[1], _("force-reload")))
1567 (void) samhainctl (3, &argc, argv); /* does not return */
1568 else if (0 == strcmp(argv[1], _("status")))
1569 (void) samhainctl (4, &argc, argv); /* does not return */
1570 else if (0 == strcmp(argv[1], _("restart")))
1571 {
1572 res = samhainctl (2, &argc, argv);
1573 if (res == 0 || res == 7)
1574 {
1575 (void) samhainctl (1, &argc, argv); /* does not return */
1576 }
1577 else
1578 return (res);
1579 }
1580 }
1581
1582 /* if fd 0 is closed, presume that we want to be daemon and
1583 * run in check mode
1584 */
1585 if ((-1) == retry_fcntl(FIL__, __LINE__, 0, F_GETFL, 0) &&
1586 errno == EBADF)
1587 {
1588 sh.flag.opts = S_TRUE;
1589 (void) sh_unix_setdeamon(NULL);
1590#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1591 sh.flag.checkSum = SH_CHECK_CHECK;
1592 /* (void) sh_util_setchecksum(_("check")); */
1593#endif
1594 is_samhainctl_init = S_TRUE;
1595 sh.flag.opts = S_FALSE;
1596 }
1597
1598
1599 /* --- Install the exit handler. ---
1600 */
1601 (void) atexit(exit_handler);
1602
1603 /* --- Zero the mailer key, and fill it. ---
1604 */
1605 memset (ErrFlag, 0, 2*sizeof(UINT32));
1606
1607#ifdef MKA_01
1608 ErrFlag[0] |= (1 << 0);
1609#endif
1610#ifdef MKA_02
1611 ErrFlag[0] |= (1 << 1);
1612#endif
1613#ifdef MKA_03
1614 ErrFlag[0] |= (1 << 2);
1615#endif
1616#ifdef MKA_04
1617 ErrFlag[0] |= (1 << 3);
1618#endif
1619#ifdef MKA_05
1620 ErrFlag[0] |= (1 << 4);
1621#endif
1622#ifdef MKA_06
1623 ErrFlag[0] |= (1 << 5);
1624#endif
1625#ifdef MKA_07
1626 ErrFlag[0] |= (1 << 6);
1627#endif
1628#ifdef MKA_08
1629 ErrFlag[0] |= (1 << 7);
1630#endif
1631
1632#if defined(SCREW_IT_UP)
1633 BREAKEXIT(sh_sigtrap_prepare);
1634 (void) sh_sigtrap_prepare();
1635#endif
1636
1637 /* Save the timezone.
1638 */
1639 if (NULL != (tzptr = getenv("TZ"))) /* flawfinder: ignore */
1640 {
1641 tzlen = strlen(tzptr);
1642 if (tzlen < 1024)
1643 {
1644 sh.timezone = calloc(1, tzlen + 1);
1645 if (sh.timezone != NULL)
1646 (void) sl_strlcpy (sh.timezone, tzptr, tzlen + 1);
1647 }
1648 else
1649 sh.timezone = NULL;
1650 }
1651 else
1652 sh.timezone = NULL;
1653
1654
1655 /* -------- INIT --------
1656 */
1657 sh_unix_ign_sigpipe();
1658
1659 /* Restrict error logging to stderr.
1660 */
1661 sh_error_only_stderr (S_TRUE);
1662
1663 /* Check that first three descriptors are open.
1664 */
1665 if ( retry_fcntl(FIL__, __LINE__, 0, F_GETFL, 0) == (-1))
1666 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 0);
1667 if ( retry_fcntl(FIL__, __LINE__, 1, F_GETFL, 0) == (-1))
1668 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 1);
1669 if ( retry_fcntl(FIL__, __LINE__, 2, F_GETFL, 0) == (-1))
1670 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 2);
1671
1672 /* --- Set default values. ---
1673 */
1674 BREAKEXIT(sh_init);
1675 sh_init (); /* we are still privileged here, so we can mlock skey */
1676#if (defined (SH_WITH_SERVER) && !defined (SH_WITH_CLIENT))
1677 sh.flag.isserver = S_TRUE;
1678#endif
1679
1680 /* --- First check for an attached debugger (after setting
1681 sh.sigtrap_max_duration which has to be done before). ---
1682 */
1683 BREAKEXIT(sh_derr);
1684 (void) sh_derr();
1685
1686 /* --- Get local hostname. ---
1687 */
1688 BREAKEXIT(sh_unix_localhost);
1689 sh_unix_localhost();
1690
1691 /* --- Read the command line. ---
1692 */
1693 sh.flag.opts = S_TRUE;
1694
1695#if !defined(SH_STEALTH_NOCL)
1696 sh_argc_store = argc;
1697 sh_argv_store = argv;
1698 (void) sh_getopt_get (argc, argv);
1699#else
1700 if (argc > 1 && argv[1] != NULL &&
1701 strlen(argv[1]) > 0 && strlen(NOCL_CODE) > 0)
1702 {
1703 if ( 0 == strcmp(argv[1], NOCL_CODE) )
1704 {
1705#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1706 char * saveptr;
1707#endif
1708 my_argv[0] = argv[0]; ++my_argc;
1709 command_line[0] = '\0';
1710 if (NULL != fgets (command_line, sizeof(command_line), stdin))
1711 command_line[sizeof(command_line)-1] = '\0';
1712
1713 do {
1714#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1715 my_argv[my_argc] =
1716 strtok_r( (my_argc == 1) ? command_line : NULL, " \n", &saveptr);
1717#else
1718 my_argv[my_argc] =
1719 strtok( (my_argc == 1) ? command_line : NULL, " \n");
1720#endif
1721 if (my_argv[my_argc] != NULL) {
1722 ++my_argc;
1723 } else {
1724 break;
1725 }
1726 } while (my_argc < 32);
1727
1728 sh_argc_store = my_argc;
1729 sh_argv_store = my_argv;
1730
1731 (void) sh_getopt_get (my_argc, my_argv);
1732 }
1733 else
1734 {
1735 /* discard command line */
1736 /* _exit(EXIT_FAILURE) */ ;
1737 }
1738 }
1739#endif
1740 sh.flag.opts = S_FALSE;
1741
1742
1743 /* --- Get user info. ---
1744 */
1745 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1746 if (0 != sh_unix_getUser ())
1747 {
1748 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1749 sh.prg_name);
1750 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1751 }
1752
1753
1754 /* *****************************
1755 *
1756 * Read the configuration file.
1757 *
1758 * *****************************/
1759
1760 TPT((0, FIL__, __LINE__, _("msg=<Read the configuration file.>\n")))
1761 BREAKEXIT(sh_readconf_read);
1762 (void) sh_readconf_read ();
1763
1764 sh_calls_enable_sub();
1765
1766#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1767 if (sh.flag.checkSum == SH_CHECK_NONE)
1768 {
1769 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
1770 _("No action specified: init, update, or check"),
1771 _("main"));
1772 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1773 sh.prg_name);
1774 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
1775 }
1776#endif
1777
1778 /* do not append to database if run SUID
1779 */
1780 if ((sh.flag.checkSum == SH_CHECK_INIT) && (0 != sl_is_suid()))
1781 {
1782 (void) dlog(1, FIL__, __LINE__,
1783 _("Cannot initialize database when running with SUID credentials.\nYou need to run this with the user ID %d.\nYour current user ID is %d."),
1784 (int) geteuid(), (int) sh.real.uid);
1785 sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_ACCESS,
1786 (long) sh.real.uid, sh.data.path);
1787 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1788 }
1789
1790 /* avoid daemon mode for initialization
1791 */
1792 if (sh.flag.checkSum == SH_CHECK_INIT)
1793 {
1794 sh.flag.isdaemon = S_FALSE;
1795 sh.flag.loop = S_FALSE;
1796 }
1797
1798 /* --- load database; checksum of database
1799 */
1800#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1801 if (0 == sh.delayload)
1802 sh_hash_init_and_checksum();
1803#endif
1804
1805 /* --- initialize signal handling etc.; fork daemon
1806 */
1807 if (sh_unix_init(sh.flag.isdaemon) == -1)
1808 {
1809 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1810 sh.prg_name);
1811 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1812 }
1813
1814#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1815 if (sh.delayload)
1816 {
1817 sleep(sh.delayload);
1818 sh_hash_init_and_checksum();
1819 }
1820#endif
1821
1822 /* --- drop privileges eventually ---
1823 */
1824#if defined(SH_WITH_SERVER)
1825 sh_create_tcp_socket ();
1826#if defined(INET_SYSLOG)
1827 sh_xfer_create_syslog_socket (S_TRUE);
1828#endif
1829 SL_REQUIRE(sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE,
1830 _("sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE"));
1831#else
1832 SL_REQUIRE(sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE,
1833 _("sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE"));
1834#endif
1835
1836 /* --- Get user info (again). ---
1837 */
1838 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1839 if (0 != sh_unix_getUser ())
1840 {
1841 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1842 sh.prg_name);
1843 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1844 }
1845
1846 /* --- now check whether we really wanted it; if not, close ---
1847 */
1848#if defined(INET_SYSLOG) && defined(SH_WITH_SERVER)
1849 sh_xfer_create_syslog_socket (S_FALSE);
1850#endif
1851
1852
1853 /* --- Enable full error logging ---
1854 */
1855 sh_error_only_stderr (S_FALSE);
1856
1857 sh.flag.started = S_TRUE;
1858
1859 /****************************************************
1860 *
1861 * SERVER
1862 *
1863 ****************************************************/
1864
1865#if defined(SH_WITH_SERVER) && !defined(SH_WITH_CLIENT)
1866
1867#if (defined(WITH_GPG) || defined(WITH_PGP))
1868 /* log startup */
1869 sh_gpg_log_startup ();
1870#else
1871 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1872 sh.prg_name, (long) sh.real.uid,
1873 (sh.flag.hidefile == S_TRUE) ?
1874 _("(hidden)") : file_path('C','R'),
1875 sh.conf.hash);
1876#endif
1877
1878#else
1879
1880 /****************************************************
1881 *
1882 * CLIENT/STANDALONE
1883 *
1884 ****************************************************/
1885
1886 BREAKEXIT(sh_error_handle);
1887
1888 if (sh.flag.checkSum == SH_CHECK_CHECK)
1889 {
1890#if (defined(WITH_GPG) || defined(WITH_PGP))
1891 /* log startup */
1892 sh_gpg_log_startup ();
1893#else
1894 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_2H,
1895 sh.prg_name, (long) sh.real.uid,
1896 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash,
1897 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('D', 'R'), sh.data.hash);
1898#endif
1899 }
1900 else
1901 {
1902#if (defined(WITH_GPG) || defined(WITH_PGP))
1903 /* log startup */
1904 sh_gpg_log_startup ();
1905#else
1906 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1907 sh.prg_name, (long) sh.real.uid,
1908 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash);
1909#endif
1910 }
1911#endif
1912
1913
1914 if ((skey == NULL) || (skey->mlock_failed == S_TRUE))
1915 sh_error_handle ((-1), FIL__, __LINE__, EPERM, MSG_MLOCK);
1916
1917 /* timer
1918 */
1919 tcurrent = time (NULL);
1920 told = tcurrent;
1921 sh.mailTime.alarm_last = told;
1922
1923
1924 /****************************************************
1925 *
1926 * SERVER
1927 *
1928 ****************************************************/
1929
1930#if defined(SH_WITH_SERVER)
1931 TPT((0, FIL__, __LINE__, _("msg=<Start server.>\n")))
1932
1933#if defined (SH_WITH_CLIENT)
1934 if (sh.flag.isserver == S_TRUE)
1935 {
1936 sh_xfer_start_server();
1937 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1938 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1939 }
1940#else
1941 sh_xfer_start_server();
1942 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1943 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1944#endif
1945
1946#endif
1947
1948 /****************************************************
1949 *
1950 * CLIENT/STANDALONE
1951 *
1952 ****************************************************/
1953#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1954
1955
1956 /* --- Initialize modules. ---
1957 */
1958 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")))
1959 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1960 {
1961 status = modList[modnum].mod_init(&(modList[modnum]));
1962 if ( status < 0 )
1963 {
1964 if (status == (-1)) {
1965 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, status,
1966 MSG_MOD_FAIL,
1967 _(modList[modnum].name),
1968 status+SH_MOD_OFFSET);
1969 } else {
1970 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_FAIL,
1971 _(modList[modnum].name),
1972 status+SH_MOD_OFFSET);
1973 }
1974 modList[modnum].initval = SH_MOD_FAILED;
1975 }
1976 else
1977 {
1978 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1979 _(modList[modnum].name));
1980 modList[modnum].initval = status;
1981 }
1982 }
1983
1984 /* -------- TEST SETUP ---------
1985 */
1986 (void) sh_files_setrec();
1987 (void) sh_files_test_setup();
1988 sh_audit_commit ();
1989
1990 /* -------- NICE LEVEL ---------
1991 */
1992 if (0 != sh.flag.nice)
1993 {
1994#ifdef HAVE_SETPRIORITY
1995 /*@-unrecog@*/
1996 (void) setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1997 /*@+unrecog@*/
1998#else
1999 (void) nice(sh.flag.nice);
2000#endif
2001 }
2002
2003 /* -------- CREATE SEMAPHORE ---------
2004 */
2005 sh_sem_open();
2006
2007 /* -------- MAIN LOOP ---------
2008 */
2009 sh.statistics.bytes_speed = 0;
2010 sh.statistics.bytes_hashed = 0;
2011 sh.statistics.files_report = 0;
2012 sh.statistics.files_error = 0;
2013 sh.statistics.files_nodir = 0;
2014
2015 while (1 == 1)
2016 {
2017 ++cct;
2018
2019 BREAKEXIT(sh_error_handle);
2020
2021 TPT((0, FIL__, __LINE__, _("msg=<Start main loop.>, iter=<%ld>\n"), cct))
2022
2023 tcurrent = time (NULL);
2024
2025 do {
2026 check_signals(&flag_check_1, &flag_check_2);
2027 if (sh_global_suspend_flag == 1)
2028 (void) retry_msleep (1, 0);
2029 } while (sh_global_suspend_flag == 1);
2030
2031 /* see whether its time to check files
2032 */
2033 if (sh.flag.checkSum == SH_CHECK_INIT ||
2034 (sh.flag.inotify & SH_INOTIFY_DOSCAN) != 0 ||
2035 (sh.flag.checkSum == SH_CHECK_CHECK &&
2036 (sh.flag.isdaemon == S_FALSE && sh.flag.loop == S_FALSE)))
2037 {
2038 flag_check_1 = 1;
2039 if (FileSchedTwo != NULL)
2040 flag_check_2 = 1;
2041 }
2042 else if (sh.flag.checkSum == SH_CHECK_CHECK ||
2043 (sh.flag.update == S_TRUE &&
2044 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE)))
2045 {
2046 if (FileSchedOne == NULL)
2047 {
2048 /* use interval if we have no schedule
2049 */
2050 if (tcurrent - sh.fileCheck.alarm_last >=
2051 sh.fileCheck.alarm_interval)
2052 flag_check_1 = 1;
2053 }
2054 else
2055 {
2056 flag_check_1 = test_sched(FileSchedOne);
2057 if (FileSchedTwo != NULL)
2058 flag_check_2 = test_sched(FileSchedTwo);
2059 if (flag_check_2 == 1)
2060 flag_check_1 = 1;
2061 }
2062 }
2063
2064 check_done = 0;
2065
2066 if (sh.flag.checkSum != SH_CHECK_NONE &&
2067 (flag_check_1 == 1 || flag_check_2 == 1))
2068 {
2069 sh_sem_trylock();
2070
2071 /* Starting a check now, so make sure to fetch delta DB
2072 * if there is one to download.
2073 */
2074 check_for_delta_db();
2075
2076 SH_INOTIFY_IFUSED( sh.flag.inotify |= SH_INOTIFY_INSCAN; );
2077 /* Refresh list files matching glob patterns.
2078 */
2079 if (sh.flag.checkSum != SH_CHECK_INIT)
2080 sh_files_check_globPatterns();
2081
2082 /*
2083 * check directories and files
2084 * ORDER IS IMPORTANT -- DIRZ FIRST
2085 */
2086 sh.statistics.bytes_hashed = 0;
2087 sh.statistics.time_start = time (NULL);
2088 sh.statistics.dirs_checked = 0;
2089 sh.statistics.files_checked = 0;
2090 sh.statistics.files_report = 0;
2091 sh.statistics.files_error = 0;
2092 sh.statistics.files_nodir = 0;
2093
2094 TPT((0, FIL__, __LINE__, _("msg=<Check directories.>\n")))
2095 BREAKEXIT(sh_dirs_chk);
2096 if (flag_check_1 == 1)
2097 {
2098 (void) sh_dirs_chk (1);
2099#ifndef SH_PROFILE
2100 (void) retry_aud_chdir (FIL__, __LINE__, "/");
2101#endif
2102 }
2103 if (flag_check_2 == 1)
2104 {
2105 (void) sh_dirs_chk (2);
2106#ifndef SH_PROFILE
2107 (void) retry_aud_chdir (FIL__, __LINE__, "/");
2108#endif
2109 }
2110 TPT((0, FIL__, __LINE__, _("msg=<Check files.>\n")))
2111 BREAKEXIT(sh_files_chk);
2112 if (flag_check_1 == 1)
2113 (void) sh_files_chk ();
2114
2115 if (sig_urgent > 0) { sh_sem_unlock(sh.statistics.files_report); continue; }
2116 /*
2117 * check for files not visited
2118 */
2119 if (flag_check_2 == 1 || FileSchedTwo == NULL)
2120 {
2121 TPT((0, FIL__, __LINE__, _("msg=<Check for missing files.>\n")))
2122 sh_hash_unvisited (ShDFLevel[SH_ERR_T_FILE]);
2123 }
2124
2125 if (sig_urgent > 0) { sh_sem_unlock(sh.statistics.files_report); continue; }
2126
2127 /* reset
2128 */
2129 TPT((0, FIL__, __LINE__, _("msg=<Reset status.>\n")))
2130 sh_dirs_reset ();
2131
2132 if (sig_urgent > 0) { sh_sem_unlock(sh.statistics.files_report); continue; }
2133
2134 sh_files_reset ();
2135 check_done = 1;
2136
2137 flag_check_1 = 0;
2138 flag_check_2 = 0;
2139 sh_sem_unlock(sh.statistics.files_report);
2140
2141 SH_INOTIFY_IFUSED( sh.flag.inotify &= ~SH_INOTIFY_INSCAN; );
2142 SH_INOTIFY_IFUSED( sh.flag.inotify &= ~SH_INOTIFY_DOSCAN; );
2143
2144 (void) sh_prelink_run (NULL, NULL, 0, 0);
2145
2146 if (sig_urgent > 0) continue;
2147
2148 runtim = time(NULL) - sh.statistics.time_start;
2149 sh.statistics.time_check = runtim;
2150
2151 if ((sh.statistics.dirs_checked == 0) &&
2152 (sh.statistics.files_checked == 0))
2153 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_0);
2154
2155 else
2156 {
2157 st_1 = (float) sh.statistics.bytes_hashed;
2158 st_2 = (float) runtim;
2159
2160
2161 if (st_1 > FLT_EPSILON && st_2 > FLT_EPSILON)
2162 st_1 = st_1/st_2;
2163 else if (st_1 > FLT_EPSILON)
2164 st_1 = (float) (st_1 * 1.0);
2165 else
2166 st_1 = 0.0;
2167
2168 sh.statistics.bytes_speed = (unsigned long) st_1;
2169
2170 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_1,
2171 (long) runtim,
2172 0.001 * st_1);
2173
2174 if (sh.flag.checkSum != SH_CHECK_INIT)
2175 sh_efile_report();
2176 }
2177 sh.fileCheck.alarm_last = time (NULL);
2178
2179 if (sig_urgent > 0) continue;
2180
2181 /*
2182 * flush mail queue
2183 */
2184#if defined(SH_WITH_MAIL)
2185 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
2186 (void) sh_nmail_flush ();
2187#endif
2188 }
2189
2190 if (sig_urgent > 0) continue;
2191
2192 /* execute modules
2193 */
2194 TPT((0, FIL__, __LINE__, _("msg=<Execute modules.>\n")))
2195 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
2196 {
2197 if (modList[modnum].initval == SH_MOD_ACTIVE &&
2198 0 != modList[modnum].mod_timer(tcurrent))
2199 if (0 != (status = modList[modnum].mod_check()))
2200 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_EXEC,
2201 _(modList[modnum].name), (long) (status+SH_MOD_OFFSET));
2202 }
2203
2204 /* 27.05.2002 avoid empty database
2205 * 22.10.2002 moved here b/o suid check initialization
2206 */
2207 if (sh.flag.checkSum == SH_CHECK_INIT)
2208 sh_dbIO_data_write (NULL, NULL);
2209
2210 /* write out database
2211 */
2212 if (sh.flag.checkSum == SH_CHECK_CHECK &&
2213 sh.flag.update == S_TRUE &&
2214 check_done == 1)
2215 sh_dbIO_writeout_update ();
2216
2217 /* no-op unless MEM_LOG is defined in sh_mem.c
2218 */
2219#ifdef MEM_DEBUG
2220 sh_mem_dump ();
2221#endif
2222
2223 {
2224 char * stale;
2225
2226 stale = sl_check_stale();
2227 if (stale)
2228 {
2229 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
2230 stale, _("sl_check_stale"));
2231 }
2232
2233 stale = sl_check_badfd();
2234 if (stale)
2235 {
2236 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
2237 stale, _("sl_check_stale"));
2238 }
2239 }
2240
2241 /* no loop if not daemon
2242 */
2243 if (sh.flag.isdaemon != S_TRUE && sh.flag.loop == S_FALSE) break;
2244 if (sig_urgent > 0) continue;
2245
2246 /* see whether its time to send mail
2247 */
2248#if defined(SH_WITH_MAIL)
2249 if (tcurrent - sh.mailTime.alarm_last >= sh.mailTime.alarm_interval)
2250 {
2251 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
2252 (void) sh_nmail_flush ();
2253 sh.mailTime.alarm_last = time (NULL);
2254 }
2255#endif
2256 if (sig_urgent > 0) continue;
2257
2258 /* log the timestamp
2259 */
2260 if ((int)(tcurrent - told) >= sh.looptime )
2261 {
2262 TPT((0, FIL__, __LINE__, _("msg=<Log the timestamp.>\n")))
2263 told = tcurrent;
2264#ifdef MEM_DEBUG
2265 sh_mem_check();
2266 sh_unix_count_mlock();
2267#else
2268 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_STAMP);
2269#endif
2270 }
2271
2272 /* seed / re-seed the PRNG if required
2273 */
2274 (void) taus_seed();
2275
2276 if (sig_urgent > 0) continue;
2277
2278 /* reset cache
2279 */
2280 sh_userid_destroy();
2281
2282 /* go to sleep
2283 */
2284 (void) retry_msleep (1, 0);
2285
2286 BREAKEXIT(sh_derr);
2287 (void) sh_derr();
2288 }
2289
2290 /* ------ END -----------
2291 */
2292
2293
2294
2295 /*
2296 * cleanup
2297 */
2298 TPT((0, FIL__, __LINE__, _("msg=<Cleanup.>\n")));
2299 sh_hash_hashdelete();
2300
2301#if defined(SH_WITH_MAIL)
2302 if (sh.mailNum.alarm_last > 0)
2303 (void)sh_nmail_flush ();
2304#endif
2305
2306 /* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
2307#endif
2308
2309 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
2310 SL_RETURN(0, _("main"));
2311}
Note: See TracBrowser for help on using the repository browser.