source: trunk/src/samhain.c@ 488

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

Fix for tickets #386 (silent check) and #387 (linux audit support).

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