source: trunk/src/samhain.c@ 395

Last change on this file since 395 was 379, checked in by katerina, 13 years ago

Fix for ticket #277 (sigpipe).

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