source: trunk/src/samhain.c@ 494

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

Version 4.1.0 final.

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 SL_RETURN(NULL, _("sh_set_schedule_int"));
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 else {
1213 FileSched->next = FileSchedIn;
1214 }
1215 SL_RETURN(FileSched , _("sh_set_schedule_int"));
1216}
1217
1218/* Add a new schedule to the linked list FileSchedOne
1219 */
1220int sh_set_schedule_one (const char * str)
1221{
1222 int status;
1223 FileSchedOne = sh_set_schedule_int (str, FileSchedOne, &status);
1224 return status;
1225}
1226
1227/* Add a new schedule to the linked list FileSchedTwo
1228 */
1229int sh_set_schedule_two (const char * str)
1230{
1231 int status;
1232 FileSchedTwo = sh_set_schedule_int (str, FileSchedTwo, &status);
1233 return status;
1234}
1235
1236static int sh_flag_silent = S_FALSE;
1237
1238int sh_set_silent_full (const char * str)
1239{
1240 int status = sh_util_flagval(str, &sh_flag_silent);
1241 return status;
1242}
1243
1244
1245void do_reconf()
1246{
1247 int status, modnum;
1248
1249 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
1250
1251 sh_thread_pause_flag = S_TRUE;
1252
1253#if defined(WITH_EXTERNAL)
1254 /* delete list of external tasks
1255 */
1256 (void) sh_ext_cleanup();
1257#endif
1258#if defined(SH_WITH_MAIL)
1259 sh_nmail_free();
1260#endif
1261
1262 /* delete the file list, make all database
1263 * entries visible (allignore = FALSE)
1264 */
1265 (void) sh_files_deldirstack ();
1266 (void) sh_files_delfilestack ();
1267 (void) sh_files_delglobstack ();
1268 (void) sh_ignore_clean ();
1269 (void) hash_full_tree ();
1270 sh_audit_delete_all ();
1271
1272
1273#if defined(SH_WITH_CLIENT)
1274 reset_count_dev_server();
1275#endif
1276#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
1277 sh_restrict_purge ();
1278
1279
1280 FileSchedOne = free_sched(FileSchedOne);
1281 FileSchedTwo = free_sched(FileSchedTwo);
1282
1283 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1284 {
1285 /* sh_thread_pause_flag is true, and we block in lock
1286 * until check has returned, so we are sure check will
1287 * not run until sh_thread_pause_flag is set to false
1288 */
1289 /* if (modList[modnum].initval >= SH_MOD_ACTIVE) */
1290 (void) modList[modnum].mod_reconf();
1291 }
1292#endif
1293
1294 reset_count_dev_console();
1295 reset_count_dev_time();
1296
1297 (void) sh_unix_maskreset();
1298
1299#ifdef RELOAD_DATABASE
1300 sh_hash_hashdelete();
1301
1302 if (0 != sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
1303 {
1304 char hashbuf[KEYBUF_SIZE];
1305 (void) sl_strlcpy(sh.data.hash,
1306 sh_tiger_hash (file_path('D', 'R'),
1307 TIGER_FILE, TIGER_NOLIM,
1308 hashbuf, sizeof(hashbuf)),
1309 KEY_LEN+1);
1310 }
1311#endif
1312 (void) sl_trust_purge_user();
1313 (void) sh_files_hle_reg (NULL);
1314 (void) sh_prelink_run (NULL, NULL, 0, 0);
1315
1316 /* --------------------------
1317 * --- READ CONFIGURATION ---
1318 * --------------------------
1319 */
1320 (void) sh_readconf_read ();
1321 sig_config_read_again = 0;
1322 (void) sh_files_setrec();
1323 (void) sh_files_test_setup();
1324 sh_audit_commit ();
1325
1326 if (0 != sh.flag.nice)
1327 {
1328#ifdef HAVE_SETPRIORITY
1329 setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1330#else
1331 nice(sh.flag.nice);
1332#endif
1333 }
1334
1335 if (sh.flag.checkSum == SH_CHECK_INIT)
1336 {
1337 sh.flag.isdaemon = S_FALSE;
1338 sh.flag.loop = S_FALSE;
1339 }
1340
1341
1342 /* --- Initialize modules. ---
1343 */
1344 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")));
1345 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1346 {
1347 status = modList[modnum].mod_init(&(modList[modnum]));
1348
1349 if (status < 0)
1350 {
1351 if (status == (-1)) {
1352 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__,
1353 status, MSG_MOD_FAIL,
1354 _(modList[modnum].name),
1355 status+SH_MOD_OFFSET);
1356 } else {
1357 sh_error_handle ((-1), FIL__, __LINE__,
1358 status, MSG_MOD_FAIL,
1359 _(modList[modnum].name),
1360 status+SH_MOD_OFFSET);
1361 }
1362 modList[modnum].initval = SH_MOD_FAILED;
1363 }
1364 else
1365 {
1366 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1367 _(modList[modnum].name));
1368 modList[modnum].initval = status;
1369 }
1370 }
1371
1372 /* module is properly set up now
1373 */
1374 sh_thread_pause_flag = S_FALSE;
1375
1376 return;
1377}
1378
1379static void check_signals (volatile int *flag_check_1,
1380 volatile int * flag_check_2)
1381{
1382 if (sig_raised > 0)
1383 {
1384
1385 TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
1386
1387 if (sig_termfast == 1) /* SIGTERM */
1388 {
1389 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1390 --sig_raised; --sig_urgent;
1391 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1392 }
1393
1394 if (sig_force_check == 1) /* SIGTTOU / SIGTSTP */
1395 {
1396 TPT((0, FIL__, __LINE__, _("msg=<Check run triggered.>\n")));
1397 *flag_check_1 = 1; *flag_check_2 = 1;
1398 sig_force_check = 0; --sig_raised;
1399 if (sig_force_silent)
1400 {
1401 sig_force_silent = 0;
1402 sh_global_check_silent = (sh_flag_silent == S_FALSE) ? SH_SILENT_STD : SH_SILENT_FULL;
1403 }
1404 sh_sem_trylock();
1405 }
1406
1407 if (sig_config_read_again == 1 && /* SIGHUP */
1408 sh_global_suspend_flag == 0)
1409 {
1410 TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")));
1411 do_reconf();
1412 --sig_raised;
1413 }
1414
1415 if (sig_fresh_trail == 1) /* SIGIOT */
1416 {
1417 if (sh_global_suspend_flag == 0)
1418 {
1419 SH_MUTEX_LOCK(mutex_thread_nolog);
1420
1421 /* Logfile access
1422 */
1423#ifdef SH_USE_XML
1424 (void) sh_log_file (NULL, NULL);
1425#endif
1426 TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
1427 sh_error_only_stderr (S_TRUE);
1428 (void) sh_unix_rm_lock_file(sh.srvlog.name);
1429 (void) retry_msleep(3, 0);
1430 sh.flag.log_start = S_TRUE;
1431 sh_error_only_stderr (S_FALSE);
1432 sh_thread_pause_flag = S_FALSE;
1433 sig_fresh_trail = 0;
1434 --sig_raised;
1435 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1436 }
1437 }
1438
1439 if (sig_terminate == 1) /* SIGQUIT */
1440 {
1441 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1442 strncpy (sh_sig_msg, _("Quit"), 20);
1443 --sig_raised;
1444 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1445 }
1446
1447 if (sig_debug_switch == 1) /* SIGUSR1 */
1448 {
1449 TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
1450 sh_error_dbg_switch();
1451 sig_debug_switch = 0;
1452 --sig_raised;
1453 }
1454
1455 if (sig_suspend_switch > 0) /* SIGUSR2 */
1456 {
1457 TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
1458 if (sh_global_suspend_flag != 1) {
1459 SH_MUTEX_LOCK_UNSAFE(mutex_thread_nolog);
1460 sh_global_suspend_flag = 1;
1461 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND,
1462 sh.prg_name);
1463 } else {
1464 sh_global_suspend_flag = 0;
1465 SH_MUTEX_UNLOCK_UNSAFE(mutex_thread_nolog);
1466 }
1467 --sig_suspend_switch;
1468 --sig_raised; --sig_urgent;
1469 }
1470
1471 if (sh_load_delta_flag > 0 &&
1472 sh_global_suspend_flag == 0) /* DELTA Command */
1473 {
1474 if (0 == sh_dbIO_load_delta())
1475 {
1476 --sh_load_delta_flag; --sig_raised;
1477 }
1478 }
1479
1480 sig_raised = (sig_raised < 0) ? 0 : sig_raised;
1481 sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
1482 TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
1483 }
1484 return;
1485}
1486
1487void check_for_delta_db()
1488{
1489 /* Need to contact the server.
1490 */
1491 if (sh_global_check_silent < SH_SILENT_FULL)
1492 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_2);
1493
1494 if (sig_raised > 0 && sh_load_delta_flag > 0) /* DELTA Command */
1495 {
1496 /* There was a DELTA Command
1497 */
1498 if (0 == sh_dbIO_load_delta())
1499 {
1500 --sh_load_delta_flag; --sig_raised;
1501 }
1502 }
1503 return;
1504}
1505
1506#endif
1507
1508/*******************************************************
1509 *
1510 * Main program
1511 *
1512 *******************************************************/
1513#if !defined(SH_CUTEST)
1514int main(int argc, char * argv[])
1515#else
1516int undef_main(int argc, char * argv[])
1517#endif
1518{
1519#if defined(INET_SYSLOG)
1520 extern int sh_xfer_create_syslog_socket (int flag);
1521#endif
1522#if defined(SH_WITH_SERVER)
1523 extern int sh_create_tcp_socket(void);
1524#endif
1525
1526#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1527 int modnum;
1528 time_t runtim;
1529 float st_1, st_2;
1530 int status;
1531 volatile long cct = 0; /* main loop iterations */
1532
1533 volatile int flag_check_1 = 0;
1534 volatile int flag_check_2 = 0;
1535
1536 int check_done = 0;
1537#endif
1538
1539 volatile time_t told;
1540 volatile time_t tcurrent;
1541 size_t tzlen;
1542 char * tzptr;
1543 int res;
1544
1545#if defined (SH_STEALTH_NOCL)
1546 char command_line[256];
1547 int my_argc = 0;
1548 char * my_argv[32];
1549#endif
1550
1551#if !defined(USE_SYSTEM_MALLOC)
1552 typedef void assert_handler_tp(const char * error, const char *file, int line);
1553 extern assert_handler_tp *dnmalloc_set_handler(assert_handler_tp *new);
1554 (void) dnmalloc_set_handler(safe_fatal);
1555#endif
1556
1557 SH_G_INIT; /* Must precede any use of _() */
1558
1559 SL_ENTER(_("main"));
1560
1561 /* --- Close all but first three file descriptors. ---
1562 */
1563 sh_unix_closeall(3, -1, S_FALSE); /* at program start */
1564
1565
1566 if (argc >= 2 && 0 != getuid() &&
1567 (0 == strcmp(argv[1], _("start")) ||
1568 0 == strcmp(argv[1], _("stop")) ||
1569 0 == strcmp(argv[1], _("reload")) ||
1570 0 == strcmp(argv[1], _("force-reload")) ||
1571 0 == strcmp(argv[1], _("status")) ||
1572 0 == strcmp(argv[1], _("restart"))))
1573 {
1574 return 4;
1575 }
1576
1577 if (argc >= 2 && 0 == getuid())
1578 {
1579 /* return codes:
1580 * 0 Success
1581 * 1 Can not send signal / start program
1582 * 2 Pid file does not exist
1583 */
1584 if (0 == strcmp(argv[1], _("start")))
1585 {
1586 (void) samhainctl (1, &argc, argv); /* does not return */
1587 }
1588 else if (0 == strcmp(argv[1], _("stop")))
1589 return (samhainctl (2, &argc, argv));
1590 else if (0 == strcmp(argv[1], _("reload")))
1591 (void) samhainctl (3, &argc, argv); /* does not return */
1592 else if (0 == strcmp(argv[1], _("force-reload")))
1593 (void) samhainctl (3, &argc, argv); /* does not return */
1594 else if (0 == strcmp(argv[1], _("status")))
1595 (void) samhainctl (4, &argc, argv); /* does not return */
1596 else if (0 == strcmp(argv[1], _("restart")))
1597 {
1598 res = samhainctl (2, &argc, argv);
1599 if (res == 0 || res == 7)
1600 {
1601 (void) samhainctl (1, &argc, argv); /* does not return */
1602 }
1603 else
1604 return (res);
1605 }
1606 }
1607
1608 /* if fd 0 is closed, presume that we want to be daemon and
1609 * run in check mode
1610 */
1611 if ((-1) == retry_fcntl(FIL__, __LINE__, 0, F_GETFL, 0) &&
1612 errno == EBADF)
1613 {
1614 sh.flag.opts = S_TRUE;
1615 (void) sh_unix_setdeamon(NULL);
1616#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1617 sh.flag.checkSum = SH_CHECK_CHECK;
1618 /* (void) sh_util_setchecksum(_("check")); */
1619#endif
1620 is_samhainctl_init = S_TRUE;
1621 sh.flag.opts = S_FALSE;
1622 }
1623
1624
1625 /* --- Install the exit handler. ---
1626 */
1627 (void) atexit(exit_handler);
1628
1629 /* --- Zero the mailer key, and fill it. ---
1630 */
1631 memset (ErrFlag, 0, 2*sizeof(UINT32));
1632
1633#ifdef MKA_01
1634 ErrFlag[0] |= (1 << 0);
1635#endif
1636#ifdef MKA_02
1637 ErrFlag[0] |= (1 << 1);
1638#endif
1639#ifdef MKA_03
1640 ErrFlag[0] |= (1 << 2);
1641#endif
1642#ifdef MKA_04
1643 ErrFlag[0] |= (1 << 3);
1644#endif
1645#ifdef MKA_05
1646 ErrFlag[0] |= (1 << 4);
1647#endif
1648#ifdef MKA_06
1649 ErrFlag[0] |= (1 << 5);
1650#endif
1651#ifdef MKA_07
1652 ErrFlag[0] |= (1 << 6);
1653#endif
1654#ifdef MKA_08
1655 ErrFlag[0] |= (1 << 7);
1656#endif
1657
1658#if defined(SCREW_IT_UP)
1659 BREAKEXIT(sh_sigtrap_prepare);
1660 (void) sh_sigtrap_prepare();
1661#endif
1662
1663 /* Save the timezone.
1664 */
1665 if (NULL != (tzptr = getenv("TZ"))) /* flawfinder: ignore */
1666 {
1667 tzlen = strlen(tzptr);
1668 if (tzlen < 1024)
1669 {
1670 sh.timezone = calloc(1, tzlen + 1);
1671 if (sh.timezone != NULL)
1672 (void) sl_strlcpy (sh.timezone, tzptr, tzlen + 1);
1673 }
1674 else
1675 sh.timezone = NULL;
1676 }
1677 else
1678 sh.timezone = NULL;
1679
1680
1681 /* -------- INIT --------
1682 */
1683 sh_unix_ign_sigpipe();
1684
1685 /* Restrict error logging to stderr.
1686 */
1687 sh_error_only_stderr (S_TRUE);
1688
1689 /* Check that first three descriptors are open.
1690 */
1691 if ( retry_fcntl(FIL__, __LINE__, 0, F_GETFL, 0) == (-1))
1692 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 0);
1693 if ( retry_fcntl(FIL__, __LINE__, 1, F_GETFL, 0) == (-1))
1694 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 1);
1695 if ( retry_fcntl(FIL__, __LINE__, 2, F_GETFL, 0) == (-1))
1696 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 2);
1697
1698 /* --- Set default values. ---
1699 */
1700 BREAKEXIT(sh_init);
1701 sh_init (); /* we are still privileged here, so we can mlock skey */
1702#if (defined (SH_WITH_SERVER) && !defined (SH_WITH_CLIENT))
1703 sh.flag.isserver = S_TRUE;
1704#endif
1705
1706 /* --- First check for an attached debugger (after setting
1707 sh.sigtrap_max_duration which has to be done before). ---
1708 */
1709 BREAKEXIT(sh_derr);
1710 (void) sh_derr();
1711
1712 /* --- Get local hostname. ---
1713 */
1714 BREAKEXIT(sh_unix_localhost);
1715 sh_unix_localhost();
1716
1717 /* --- Read the command line. ---
1718 */
1719 sh.flag.opts = S_TRUE;
1720
1721#if !defined(SH_STEALTH_NOCL)
1722 sh_argc_store = argc;
1723 sh_argv_store = argv;
1724 (void) sh_getopt_get (argc, argv);
1725#else
1726 if (argc > 1 && argv[1] != NULL &&
1727 strlen(argv[1]) > 0 && strlen(NOCL_CODE) > 0)
1728 {
1729 if ( 0 == strcmp(argv[1], NOCL_CODE) )
1730 {
1731#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1732 char * saveptr;
1733#endif
1734 my_argv[0] = argv[0]; ++my_argc;
1735 command_line[0] = '\0';
1736 if (NULL != fgets (command_line, sizeof(command_line), stdin))
1737 command_line[sizeof(command_line)-1] = '\0';
1738
1739 do {
1740#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1741 my_argv[my_argc] =
1742 strtok_r( (my_argc == 1) ? command_line : NULL, " \n", &saveptr);
1743#else
1744 my_argv[my_argc] =
1745 strtok( (my_argc == 1) ? command_line : NULL, " \n");
1746#endif
1747 if (my_argv[my_argc] != NULL) {
1748 ++my_argc;
1749 } else {
1750 break;
1751 }
1752 } while (my_argc < 32);
1753
1754 sh_argc_store = my_argc;
1755 sh_argv_store = my_argv;
1756
1757 (void) sh_getopt_get (my_argc, my_argv);
1758 }
1759 else
1760 {
1761 /* discard command line */
1762 /* _exit(EXIT_FAILURE) */ ;
1763 }
1764 }
1765#endif
1766 sh.flag.opts = S_FALSE;
1767
1768
1769 /* --- Get user info. ---
1770 */
1771 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1772 if (0 != sh_unix_getUser ())
1773 {
1774 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1775 sh.prg_name);
1776 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1777 }
1778
1779
1780 /* *****************************
1781 *
1782 * Read the configuration file.
1783 *
1784 * *****************************/
1785
1786 TPT((0, FIL__, __LINE__, _("msg=<Read the configuration file.>\n")))
1787 BREAKEXIT(sh_readconf_read);
1788 (void) sh_readconf_read ();
1789
1790 sh_calls_enable_sub();
1791
1792#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1793 if (sh.flag.checkSum == SH_CHECK_NONE)
1794 {
1795 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
1796 _("No action specified: init, update, or check"),
1797 _("main"));
1798 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1799 sh.prg_name);
1800 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
1801 }
1802#endif
1803
1804 /* do not append to database if run SUID
1805 */
1806 if ((sh.flag.checkSum == SH_CHECK_INIT) && (0 != sl_is_suid()))
1807 {
1808 (void) dlog(1, FIL__, __LINE__,
1809 _("Cannot initialize database when running with SUID credentials.\nYou need to run this with the user ID %d.\nYour current user ID is %d."),
1810 (int) geteuid(), (int) sh.real.uid);
1811 sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_ACCESS,
1812 (long) sh.real.uid, sh.data.path);
1813 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1814 }
1815
1816 /* avoid daemon mode for initialization
1817 */
1818 if (sh.flag.checkSum == SH_CHECK_INIT)
1819 {
1820 sh.flag.isdaemon = S_FALSE;
1821 sh.flag.loop = S_FALSE;
1822 }
1823
1824 /* --- load database; checksum of database
1825 */
1826#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1827 if (0 == sh.delayload)
1828 sh_hash_init_and_checksum();
1829#endif
1830
1831 /* --- initialize signal handling etc.; fork daemon
1832 */
1833 if (sh_unix_init(sh.flag.isdaemon) == -1)
1834 {
1835 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1836 sh.prg_name);
1837 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1838 }
1839
1840#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1841 if (sh.delayload)
1842 {
1843 sleep(sh.delayload);
1844 sh_hash_init_and_checksum();
1845 }
1846#endif
1847
1848 /* --- drop privileges eventually ---
1849 */
1850#if defined(SH_WITH_SERVER)
1851 sh_create_tcp_socket ();
1852#if defined(INET_SYSLOG)
1853 sh_xfer_create_syslog_socket (S_TRUE);
1854#endif
1855 SL_REQUIRE(sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE,
1856 _("sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE"));
1857#else
1858 SL_REQUIRE(sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE,
1859 _("sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE"));
1860#endif
1861
1862 /* --- Get user info (again). ---
1863 */
1864 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1865 if (0 != sh_unix_getUser ())
1866 {
1867 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1868 sh.prg_name);
1869 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1870 }
1871
1872 /* --- now check whether we really wanted it; if not, close ---
1873 */
1874#if defined(INET_SYSLOG) && defined(SH_WITH_SERVER)
1875 sh_xfer_create_syslog_socket (S_FALSE);
1876#endif
1877
1878
1879 /* --- Enable full error logging ---
1880 */
1881 sh_error_only_stderr (S_FALSE);
1882
1883 sh.flag.started = S_TRUE;
1884
1885 /****************************************************
1886 *
1887 * SERVER
1888 *
1889 ****************************************************/
1890
1891#if defined(SH_WITH_SERVER) && !defined(SH_WITH_CLIENT)
1892
1893#if (defined(WITH_GPG) || defined(WITH_PGP))
1894 /* log startup */
1895 sh_gpg_log_startup ();
1896#else
1897 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1898 sh.prg_name, (long) sh.real.uid,
1899 (sh.flag.hidefile == S_TRUE) ?
1900 _("(hidden)") : file_path('C','R'),
1901 sh.conf.hash);
1902#endif
1903
1904#else
1905
1906 /****************************************************
1907 *
1908 * CLIENT/STANDALONE
1909 *
1910 ****************************************************/
1911
1912 BREAKEXIT(sh_error_handle);
1913
1914 if (sh.flag.checkSum == SH_CHECK_CHECK)
1915 {
1916#if (defined(WITH_GPG) || defined(WITH_PGP))
1917 /* log startup */
1918 sh_gpg_log_startup ();
1919#else
1920 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_2H,
1921 sh.prg_name, (long) sh.real.uid,
1922 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash,
1923 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('D', 'R'), sh.data.hash);
1924#endif
1925 }
1926 else
1927 {
1928#if (defined(WITH_GPG) || defined(WITH_PGP))
1929 /* log startup */
1930 sh_gpg_log_startup ();
1931#else
1932 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1933 sh.prg_name, (long) sh.real.uid,
1934 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash);
1935#endif
1936 }
1937#endif
1938
1939
1940 if ((skey == NULL) || (skey->mlock_failed == S_TRUE))
1941 sh_error_handle ((-1), FIL__, __LINE__, EPERM, MSG_MLOCK);
1942
1943 /* timer
1944 */
1945 tcurrent = time (NULL);
1946 told = tcurrent;
1947 sh.mailTime.alarm_last = told;
1948
1949
1950 /****************************************************
1951 *
1952 * SERVER
1953 *
1954 ****************************************************/
1955
1956#if defined(SH_WITH_SERVER)
1957 TPT((0, FIL__, __LINE__, _("msg=<Start server.>\n")))
1958
1959#if defined (SH_WITH_CLIENT)
1960 if (sh.flag.isserver == S_TRUE)
1961 {
1962 sh_xfer_start_server();
1963 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1964 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1965 }
1966#else
1967 sh_xfer_start_server();
1968 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1969 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1970#endif
1971
1972#endif
1973
1974 /****************************************************
1975 *
1976 * CLIENT/STANDALONE
1977 *
1978 ****************************************************/
1979#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1980
1981
1982 /* --- Initialize modules. ---
1983 */
1984 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")))
1985 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1986 {
1987 status = modList[modnum].mod_init(&(modList[modnum]));
1988 if ( status < 0 )
1989 {
1990 if (status == (-1)) {
1991 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, status,
1992 MSG_MOD_FAIL,
1993 _(modList[modnum].name),
1994 status+SH_MOD_OFFSET);
1995 } else {
1996 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_FAIL,
1997 _(modList[modnum].name),
1998 status+SH_MOD_OFFSET);
1999 }
2000 modList[modnum].initval = SH_MOD_FAILED;
2001 }
2002 else
2003 {
2004 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
2005 _(modList[modnum].name));
2006 modList[modnum].initval = status;
2007 }
2008 }
2009
2010 /* -------- TEST SETUP ---------
2011 */
2012 (void) sh_files_setrec();
2013 (void) sh_files_test_setup();
2014 sh_audit_commit ();
2015
2016 /* -------- NICE LEVEL ---------
2017 */
2018 if (0 != sh.flag.nice)
2019 {
2020#ifdef HAVE_SETPRIORITY
2021 /*@-unrecog@*/
2022 (void) setpriority(PRIO_PROCESS, 0, sh.flag.nice);
2023 /*@+unrecog@*/
2024#else
2025 (void) nice(sh.flag.nice);
2026#endif
2027 }
2028
2029 /* -------- CREATE SEMAPHORE ---------
2030 */
2031 sh_sem_open();
2032
2033 /* -------- MAIN LOOP ---------
2034 */
2035 sh.statistics.bytes_speed = 0;
2036 sh.statistics.bytes_hashed = 0;
2037 sh.statistics.files_report = 0;
2038 sh.statistics.files_error = 0;
2039 sh.statistics.files_nodir = 0;
2040
2041 while (1 == 1)
2042 {
2043 ++cct;
2044
2045 BREAKEXIT(sh_error_handle);
2046
2047 TPT((0, FIL__, __LINE__, _("msg=<Start main loop.>, iter=<%ld>\n"), cct))
2048
2049 tcurrent = time (NULL);
2050
2051 do {
2052 check_signals(&flag_check_1, &flag_check_2);
2053 if (sh_global_suspend_flag == 1)
2054 (void) retry_msleep (1, 0);
2055 } while (sh_global_suspend_flag == 1);
2056
2057 /* see whether its time to check files
2058 */
2059 if (sh.flag.checkSum == SH_CHECK_INIT ||
2060 (sh.flag.inotify & SH_INOTIFY_DOSCAN) != 0 ||
2061 (sh.flag.checkSum == SH_CHECK_CHECK &&
2062 (sh.flag.isdaemon == S_FALSE && sh.flag.loop == S_FALSE)))
2063 {
2064 flag_check_1 = 1;
2065 if (FileSchedTwo != NULL)
2066 flag_check_2 = 1;
2067 }
2068 else if (sh.flag.checkSum == SH_CHECK_CHECK ||
2069 (sh.flag.update == S_TRUE &&
2070 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE)))
2071 {
2072 if (FileSchedOne == NULL)
2073 {
2074 /* use interval if we have no schedule
2075 */
2076 if (tcurrent - sh.fileCheck.alarm_last >=
2077 sh.fileCheck.alarm_interval)
2078 flag_check_1 = 1;
2079 }
2080 else
2081 {
2082 flag_check_1 = test_sched(FileSchedOne);
2083 if (FileSchedTwo != NULL)
2084 flag_check_2 = test_sched(FileSchedTwo);
2085 if (flag_check_2 == 1)
2086 flag_check_1 = 1;
2087 }
2088 }
2089
2090 check_done = 0;
2091
2092 if (sh.flag.checkSum != SH_CHECK_NONE &&
2093 (flag_check_1 == 1 || flag_check_2 == 1))
2094 {
2095 sh_sem_trylock();
2096
2097 /* Starting a check now, so make sure to fetch delta DB
2098 * if there is one to download.
2099 */
2100 check_for_delta_db();
2101
2102 SH_INOTIFY_IFUSED( sh.flag.inotify |= SH_INOTIFY_INSCAN; );
2103 /* Refresh list files matching glob patterns.
2104 */
2105 if (sh.flag.checkSum != SH_CHECK_INIT)
2106 sh_files_check_globPatterns();
2107
2108 /*
2109 * check directories and files
2110 * ORDER IS IMPORTANT -- DIRZ FIRST
2111 */
2112 sh.statistics.bytes_hashed = 0;
2113 sh.statistics.time_start = time (NULL);
2114 sh.statistics.dirs_checked = 0;
2115 sh.statistics.files_checked = 0;
2116 sh.statistics.files_report = 0;
2117 sh.statistics.files_error = 0;
2118 sh.statistics.files_nodir = 0;
2119
2120 TPT((0, FIL__, __LINE__, _("msg=<Check directories.>\n")))
2121 BREAKEXIT(sh_dirs_chk);
2122 if (flag_check_1 == 1)
2123 {
2124 (void) sh_dirs_chk (1);
2125#ifndef SH_PROFILE
2126 (void) retry_aud_chdir (FIL__, __LINE__, "/");
2127#endif
2128 }
2129 if (flag_check_2 == 1)
2130 {
2131 (void) sh_dirs_chk (2);
2132#ifndef SH_PROFILE
2133 (void) retry_aud_chdir (FIL__, __LINE__, "/");
2134#endif
2135 }
2136 TPT((0, FIL__, __LINE__, _("msg=<Check files.>\n")))
2137 BREAKEXIT(sh_files_chk);
2138 if (flag_check_1 == 1)
2139 (void) sh_files_chk ();
2140
2141 if (sig_urgent > 0) { sh_sem_unlock(sh.statistics.files_report); sh_global_check_silent = 0; continue; }
2142 /*
2143 * check for files not visited
2144 */
2145 if (flag_check_2 == 1 || FileSchedTwo == NULL)
2146 {
2147 TPT((0, FIL__, __LINE__, _("msg=<Check for missing files.>\n")))
2148 sh_hash_unvisited (ShDFLevel[SH_ERR_T_FILE]);
2149 }
2150
2151 if (sig_urgent > 0) { sh_sem_unlock(sh.statistics.files_report); sh_global_check_silent = 0; continue; }
2152
2153 /* reset
2154 */
2155 TPT((0, FIL__, __LINE__, _("msg=<Reset status.>\n")))
2156 sh_dirs_reset ();
2157
2158 if (sig_urgent > 0) { sh_sem_unlock(sh.statistics.files_report); sh_global_check_silent = 0; continue; }
2159
2160 sh_files_reset ();
2161 check_done = 1;
2162
2163 flag_check_1 = 0;
2164 flag_check_2 = 0;
2165 sh_sem_unlock(sh.statistics.files_report);
2166
2167 SH_INOTIFY_IFUSED( sh.flag.inotify &= ~SH_INOTIFY_INSCAN; );
2168 SH_INOTIFY_IFUSED( sh.flag.inotify &= ~SH_INOTIFY_DOSCAN; );
2169
2170 (void) sh_prelink_run (NULL, NULL, 0, 0);
2171
2172 if (sig_urgent > 0) { sh_global_check_silent = 0; continue; }
2173
2174 runtim = time(NULL) - sh.statistics.time_start;
2175 sh.statistics.time_check = runtim;
2176
2177 if ((sh.statistics.dirs_checked == 0) &&
2178 (sh.statistics.files_checked == 0) &&
2179 (sh_global_check_silent < SH_SILENT_FULL))
2180 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_0);
2181
2182 else
2183 {
2184 st_1 = (float) sh.statistics.bytes_hashed;
2185 st_2 = (float) runtim;
2186
2187
2188 if (st_1 > FLT_EPSILON && st_2 > FLT_EPSILON)
2189 st_1 = st_1/st_2;
2190 else if (st_1 > FLT_EPSILON)
2191 st_1 = (float) (st_1 * 1.0);
2192 else
2193 st_1 = 0.0;
2194
2195 sh.statistics.bytes_speed = (unsigned long) st_1;
2196
2197 if (sh_global_check_silent < SH_SILENT_FULL)
2198 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_1,
2199 (long) runtim,
2200 0.001 * st_1);
2201
2202 if (sh.flag.checkSum != SH_CHECK_INIT)
2203 sh_efile_report();
2204 }
2205
2206 if (0 == sh_global_check_silent)
2207 sh.fileCheck.alarm_last = time (NULL);
2208
2209 sh_global_check_silent = 0;
2210
2211 if (sig_urgent > 0) continue;
2212
2213 /*
2214 * flush mail queue
2215 */
2216#if defined(SH_WITH_MAIL)
2217 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
2218 (void) sh_nmail_flush ();
2219#endif
2220 }
2221
2222 if (sig_urgent > 0) continue;
2223
2224 /* execute modules
2225 */
2226 TPT((0, FIL__, __LINE__, _("msg=<Execute modules.>\n")))
2227 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
2228 {
2229 if (modList[modnum].initval == SH_MOD_ACTIVE &&
2230 0 != modList[modnum].mod_timer(tcurrent))
2231 if (0 != (status = modList[modnum].mod_check()))
2232 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_EXEC,
2233 _(modList[modnum].name), (long) (status+SH_MOD_OFFSET));
2234 }
2235
2236 /* 27.05.2002 avoid empty database
2237 * 22.10.2002 moved here b/o suid check initialization
2238 */
2239 if (sh.flag.checkSum == SH_CHECK_INIT)
2240 sh_dbIO_data_write (NULL, NULL);
2241
2242 /* write out database
2243 */
2244 if (sh.flag.checkSum == SH_CHECK_CHECK &&
2245 sh.flag.update == S_TRUE &&
2246 check_done == 1)
2247 sh_dbIO_writeout_update ();
2248
2249 /* no-op unless MEM_LOG is defined in sh_mem.c
2250 */
2251#ifdef MEM_DEBUG
2252 sh_mem_dump ();
2253#endif
2254
2255 {
2256 char * stale;
2257
2258 stale = sl_check_stale();
2259 if (stale)
2260 {
2261 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
2262 stale, _("sl_check_stale"));
2263 }
2264
2265 stale = sl_check_badfd();
2266 if (stale)
2267 {
2268 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
2269 stale, _("sl_check_stale"));
2270 }
2271 }
2272
2273 /* no loop if not daemon
2274 */
2275 if (sh.flag.isdaemon != S_TRUE && sh.flag.loop == S_FALSE) break;
2276 if (sig_urgent > 0) continue;
2277
2278 /* see whether its time to send mail
2279 */
2280#if defined(SH_WITH_MAIL)
2281 if (tcurrent - sh.mailTime.alarm_last >= sh.mailTime.alarm_interval)
2282 {
2283 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
2284 (void) sh_nmail_flush ();
2285 sh.mailTime.alarm_last = time (NULL);
2286 }
2287#endif
2288 if (sig_urgent > 0) continue;
2289
2290 /* log the timestamp
2291 */
2292 if ((int)(tcurrent - told) >= sh.looptime )
2293 {
2294 TPT((0, FIL__, __LINE__, _("msg=<Log the timestamp.>\n")))
2295 told = tcurrent;
2296#ifdef MEM_DEBUG
2297 sh_mem_check();
2298 sh_unix_count_mlock();
2299#else
2300 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_STAMP);
2301#endif
2302 }
2303
2304 /* seed / re-seed the PRNG if required
2305 */
2306 (void) taus_seed();
2307
2308 if (sig_urgent > 0) continue;
2309
2310 /* reset cache
2311 */
2312 sh_userid_destroy();
2313
2314 /* go to sleep
2315 */
2316 (void) retry_msleep (1, 0);
2317
2318 BREAKEXIT(sh_derr);
2319 (void) sh_derr();
2320 }
2321
2322 /* ------ END -----------
2323 */
2324
2325
2326
2327 /*
2328 * cleanup
2329 */
2330 TPT((0, FIL__, __LINE__, _("msg=<Cleanup.>\n")));
2331 sh_hash_hashdelete();
2332
2333#if defined(SH_WITH_MAIL)
2334 if (sh.mailNum.alarm_last > 0)
2335 (void)sh_nmail_flush ();
2336#endif
2337
2338 /* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
2339#endif
2340
2341 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
2342 SL_RETURN(0, _("main"));
2343}
Note: See TracBrowser for help on using the repository browser.