source: trunk/src/samhain.c@ 371

Last change on this file since 371 was 367, checked in by katerina, 13 years ago

Modifications for ticket #265 (inotify support). Needs testing.

File size: 51.2 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 */
1378
1379 /* Restrict error logging to stderr.
1380 */
1381 sh_error_only_stderr (S_TRUE);
1382
1383 BREAKEXIT(sh_derr);
1384 (void) sh_derr();
1385
1386 /* Check that first three descriptors are open.
1387 */
1388 if ( retry_fcntl(FIL__, __LINE__, 0, F_GETFL, 0) == (-1))
1389 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 0);
1390 if ( retry_fcntl(FIL__, __LINE__, 1, F_GETFL, 0) == (-1))
1391 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 1);
1392 if ( retry_fcntl(FIL__, __LINE__, 2, F_GETFL, 0) == (-1))
1393 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 2);
1394
1395 /* --- Set default values. ---
1396 */
1397 BREAKEXIT(sh_init);
1398 sh_init (); /* we are still privileged here, so we can mlock skey */
1399#if (defined (SH_WITH_SERVER) && !defined (SH_WITH_CLIENT))
1400 sh.flag.isserver = S_TRUE;
1401#endif
1402
1403 /* --- Get local hostname. ---
1404 */
1405 BREAKEXIT(sh_unix_localhost);
1406 sh_unix_localhost();
1407
1408 /* --- Read the command line. ---
1409 */
1410 sh.flag.opts = S_TRUE;
1411
1412#if !defined(SH_STEALTH_NOCL)
[20]1413 sh_argc_store = argc;
1414 sh_argv_store = argv;
[1]1415 (void) sh_getopt_get (argc, argv);
1416#else
1417 if (argc > 1 && argv[1] != NULL &&
1418 strlen(argv[1]) > 0 && strlen(NOCL_CODE) > 0)
1419 {
[34]1420 if ( 0 == strcmp(argv[1], NOCL_CODE) )
[1]1421 {
[131]1422#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1423 char * saveptr;
1424#endif
[1]1425 my_argv[0] = argv[0]; ++my_argc;
1426 command_line[0] = '\0';
[153]1427 if (NULL != fgets (command_line, sizeof(command_line), stdin))
1428 command_line[sizeof(command_line)-1] = '\0';
1429
[1]1430 do {
[131]1431#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
[1]1432 my_argv[my_argc] =
[131]1433 strtok_r( (my_argc == 1) ? command_line : NULL, " \n", &saveptr);
1434#else
1435 my_argv[my_argc] =
1436 strtok( (my_argc == 1) ? command_line : NULL, " \n");
1437#endif
[1]1438 if (my_argv[my_argc] != NULL) {
1439 ++my_argc;
1440 } else {
1441 break;
1442 }
1443 } while (my_argc < 32);
[20]1444
1445 sh_argc_store = my_argc;
1446 sh_argv_store = my_argv;
1447
[1]1448 (void) sh_getopt_get (my_argc, my_argv);
1449 }
1450 else
1451 {
1452 /* discard command line */
1453 /* _exit(EXIT_FAILURE) */ ;
1454 }
1455 }
1456#endif
1457 sh.flag.opts = S_FALSE;
1458
1459
1460 /* --- Get user info. ---
1461 */
1462 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1463 if (0 != sh_unix_getUser ())
1464 {
1465 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1466 sh.prg_name);
1467 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1468 }
1469
1470
1471 /* *****************************
1472 *
1473 * Read the configuration file.
1474 *
1475 * *****************************/
1476
1477 TPT((0, FIL__, __LINE__, _("msg=<Read the configuration file.>\n")))
1478 BREAKEXIT(sh_readconf_read);
1479 (void) sh_readconf_read ();
1480
[317]1481 sh_calls_enable_sub();
1482
[1]1483#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1484 if (sh.flag.checkSum == SH_CHECK_NONE)
1485 {
1486 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
1487 _("No action specified: init, update, or check"),
1488 _("main"));
1489 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1490 sh.prg_name);
1491 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
1492 }
1493#endif
1494
1495 /* do not append to database if run SUID
1496 */
1497 if ((sh.flag.checkSum == SH_CHECK_INIT) && (0 != sl_is_suid()))
1498 {
1499 (void) dlog(1, FIL__, __LINE__,
1500 _("Cannot initialize database when running with SUID credentials.\nYou need to run this with the user ID %d.\nYour current user ID is %d."),
1501 (int) geteuid(), (int) sh.real.uid);
1502 sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_ACCESS,
1503 (long) sh.real.uid, sh.data.path);
1504 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1505 }
1506
1507 /* avoid daemon mode for initialization
1508 */
1509 if (sh.flag.checkSum == SH_CHECK_INIT)
1510 {
1511 sh.flag.isdaemon = S_FALSE;
1512 sh.flag.loop = S_FALSE;
1513 }
1514
[59]1515 /* --- load database; checksum of database
1516 */
1517#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1518 TPT((0, FIL__, __LINE__, _("msg=<Get checksum of the database.>\n")))
1519 if (sh.flag.checkSum == SH_CHECK_CHECK)
1520 {
1521 if (0 != sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
1522 {
[133]1523 char hashbuf[KEYBUF_SIZE];
[59]1524 (void) sl_strlcpy(sh.data.hash,
1525 sh_tiger_hash (file_path('D', 'R'),
[160]1526 TIGER_FILE, TIGER_NOLIM,
[133]1527 hashbuf, sizeof(hashbuf)),
[59]1528 KEY_LEN+1);
1529 }
1530
1531 /* this eventually fetches the file from server to get checksum
1532 */
1533 sh_hash_init ();
1534 }
1535#endif
1536
1537 /* --- initialize signal handling etc.; fork daemon
1538 */
[1]1539 if (sh_unix_init(sh.flag.isdaemon) == -1)
1540 {
1541 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1542 sh.prg_name);
1543 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1544 }
1545
1546 /* --- drop privileges eventually ---
1547 */
1548#if defined(SH_WITH_SERVER)
1549 sh_create_tcp_socket ();
1550#if defined(INET_SYSLOG)
1551 create_syslog_socket (S_TRUE);
1552#endif
1553 SL_REQUIRE(sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE,
1554 _("sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE"));
1555#else
1556 SL_REQUIRE(sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE,
1557 _("sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE"));
1558#endif
1559
1560 /* --- Get user info (again). ---
1561 */
1562 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1563 if (0 != sh_unix_getUser ())
1564 {
1565 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1566 sh.prg_name);
1567 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1568 }
1569
1570 /* --- now check whether we really wanted it; if not, close ---
1571 */
1572#if defined(INET_SYSLOG) && defined(SH_WITH_SERVER)
1573 create_syslog_socket (S_FALSE);
1574#endif
1575
1576
1577 /* --- Enable full error logging ---
1578 */
1579 sh_error_only_stderr (S_FALSE);
1580
[256]1581 sh.flag.started = S_TRUE;
1582
[1]1583 /****************************************************
1584 *
1585 * SERVER
1586 *
1587 ****************************************************/
1588
1589#if defined(SH_WITH_SERVER) && !defined(SH_WITH_CLIENT)
1590
1591#if (defined(WITH_GPG) || defined(WITH_PGP))
[86]1592 /* log startup */
1593 sh_gpg_log_startup ();
[1]1594#else
1595 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1596 sh.prg_name, (long) sh.real.uid,
1597 (sh.flag.hidefile == S_TRUE) ?
1598 _("(hidden)") : file_path('C','R'),
1599 sh.conf.hash);
1600#endif
1601
1602#else
1603
1604 /****************************************************
1605 *
1606 * CLIENT/STANDALONE
1607 *
1608 ****************************************************/
1609
1610 BREAKEXIT(sh_error_handle);
1611
1612 if (sh.flag.checkSum == SH_CHECK_CHECK)
1613 {
1614#if (defined(WITH_GPG) || defined(WITH_PGP))
[86]1615 /* log startup */
1616 sh_gpg_log_startup ();
[1]1617#else
1618 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_2H,
1619 sh.prg_name, (long) sh.real.uid,
1620 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash,
1621 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('D', 'R'), sh.data.hash);
1622#endif
1623 }
1624 else
1625 {
1626#if (defined(WITH_GPG) || defined(WITH_PGP))
[86]1627 /* log startup */
1628 sh_gpg_log_startup ();
[1]1629#else
1630 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1631 sh.prg_name, (long) sh.real.uid,
1632 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash);
1633#endif
1634 }
1635#endif
1636
1637
1638 if ((skey == NULL) || (skey->mlock_failed == SL_TRUE))
1639 sh_error_handle ((-1), FIL__, __LINE__, EPERM, MSG_MLOCK);
1640
1641 /* timer
1642 */
1643 tcurrent = time (NULL);
1644 told = tcurrent;
1645 sh.mailTime.alarm_last = told;
1646
1647
1648 /****************************************************
1649 *
1650 * SERVER
1651 *
1652 ****************************************************/
1653
1654#if defined(SH_WITH_SERVER)
1655 TPT((0, FIL__, __LINE__, _("msg=<Start server.>\n")))
1656
1657#if defined (SH_WITH_CLIENT)
1658 if (sh.flag.isserver == S_TRUE)
1659 {
1660 sh_receive();
1661 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1662 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1663 }
1664#else
1665 sh_receive();
1666 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1667 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1668#endif
1669
1670#endif
1671
1672 /****************************************************
1673 *
1674 * CLIENT/STANDALONE
1675 *
1676 ****************************************************/
1677#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1678
1679
1680 /* --- Initialize modules. ---
1681 */
1682 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")))
1683 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1684 {
[142]1685 status = modList[modnum].mod_init(&(modList[modnum]));
1686 if ( status < 0 )
[1]1687 {
1688 if (status == (-1)) {
1689 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, status,
1690 MSG_MOD_FAIL,
1691 _(modList[modnum].name),
[207]1692 status+SH_MOD_OFFSET);
[1]1693 } else {
1694 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_FAIL,
1695 _(modList[modnum].name),
[207]1696 status+SH_MOD_OFFSET);
[1]1697 }
[142]1698 modList[modnum].initval = SH_MOD_FAILED;
[1]1699 }
1700 else
1701 {
1702 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1703 _(modList[modnum].name));
[142]1704 modList[modnum].initval = status;
[1]1705 }
1706 }
1707
1708 /* -------- TEST SETUP ---------
1709 */
1710 (void) sh_files_setrec();
1711 (void) sh_files_test_setup();
1712
1713 /* -------- NICE LEVEL ---------
1714 */
1715 if (0 != sh.flag.nice)
1716 {
1717#ifdef HAVE_SETPRIORITY
1718 /*@-unrecog@*/
1719 (void) setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1720 /*@+unrecog@*/
1721#else
1722 (void) nice(sh.flag.nice);
1723#endif
1724 }
1725
1726 /* -------- MAIN LOOP ---------
1727 */
1728 sh.statistics.bytes_speed = 0;
1729 sh.statistics.bytes_hashed = 0;
1730
1731 while (1 == 1)
1732 {
1733 ++cct;
1734
1735 BREAKEXIT(sh_error_handle);
1736
1737 TPT((0, FIL__, __LINE__, _("msg=<Start main loop.>, iter=<%ld>\n"), cct))
1738
1739 tcurrent = time (NULL);
1740
1741 if (sig_raised > 0)
1742 {
1743
1744 TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
1745
1746 if (sig_termfast == 1) /* SIGTERM */
1747 {
1748 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1749 /* strncpy (sh_sig_msg, _("SIGTERM"), 20); */
1750 --sig_raised; --sig_urgent;
1751 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1752 }
1753
1754 if (sig_force_check == 1) /* SIGTTOU */
1755 {
1756 TPT((0, FIL__, __LINE__, _("msg=<Check run triggered.>\n")));
1757 flag_check_1 = 1;
1758 flag_check_2 = 1;
1759 sig_force_check = 0;
1760 --sig_raised;
1761 }
1762
[145]1763 if (sig_config_read_again == 1 && /* SIGHUP */
1764 sh_global_suspend_flag == 0)
[1]1765 {
1766 TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")))
1767 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
1768
[143]1769 sh_thread_pause_flag = S_TRUE;
1770
[1]1771#if defined(WITH_EXTERNAL)
1772 /* delete list of external tasks
1773 */
1774 (void) sh_ext_cleanup();
1775#endif
[215]1776#if defined(SH_WITH_MAIL)
1777 sh_nmail_free();
1778#endif
1779
[1]1780 /* delete the file list, make all database
1781 * entries visible (allignore = FALSE)
1782 */
1783 (void) sh_files_deldirstack ();
1784 (void) sh_files_delfilestack ();
[256]1785 (void) sh_files_delglobstack ();
[1]1786 (void) sh_ignore_clean ();
1787 (void) hash_full_tree ();
[294]1788 sh_audit_delete_all ();
[1]1789
[294]1790
[1]1791#if defined(SH_WITH_CLIENT)
1792 reset_count_dev_server();
1793#endif
1794#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
[310]1795 sh_restrict_purge ();
[1]1796
1797
1798 FileSchedOne = free_sched(FileSchedOne);
1799 FileSchedTwo = free_sched(FileSchedTwo);
1800
1801 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1802 {
[149]1803 /* sh_thread_pause_flag is true, and we block in lock
1804 * until check has returned, so we are sure check will
1805 * not run until sh_thread_pause_flag is set to false
1806 */
1807 /* if (modList[modnum].initval >= SH_MOD_ACTIVE) */
1808 (void) modList[modnum].mod_reconf();
[1]1809 }
1810#endif
1811
1812 reset_count_dev_console();
1813 reset_count_dev_time();
1814
1815 (void) sh_unix_maskreset();
1816
1817 /* Should this be included ???
1818 * (i.e. should we reload the database ?)
1819 */
1820#ifdef RELOAD_DATABASE
1821 sh_hash_hashdelete();
[354]1822
1823 if (0 != sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
1824 {
1825 char hashbuf[KEYBUF_SIZE];
1826 (void) sl_strlcpy(sh.data.hash,
1827 sh_tiger_hash (file_path('D', 'R'),
1828 TIGER_FILE, TIGER_NOLIM,
1829 hashbuf, sizeof(hashbuf)),
1830 KEY_LEN+1);
1831 }
[1]1832#endif
1833 (void) sl_trust_purge_user();
1834 (void) sh_files_hle_reg (NULL);
1835 (void) sh_prelink_run (NULL, NULL, 0);
1836
1837 /* --------------------------
1838 * --- READ CONFIGURATION ---
1839 * --------------------------
1840 */
1841 (void) sh_readconf_read ();
1842 sig_config_read_again = 0;
1843 (void) sh_files_setrec();
1844 (void) sh_files_test_setup();
[367]1845
[1]1846 if (0 != sh.flag.nice)
1847 {
1848#ifdef HAVE_SETPRIORITY
1849 setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1850#else
1851 nice(sh.flag.nice);
1852#endif
1853 }
1854
1855 if (sh.flag.checkSum == SH_CHECK_INIT)
1856 {
1857 sh.flag.isdaemon = S_FALSE;
1858 sh.flag.loop = S_FALSE;
1859 }
1860
[143]1861
[1]1862 /* --- Initialize modules. ---
1863 */
1864 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")));
1865 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1866 {
[142]1867 status = modList[modnum].mod_init(&(modList[modnum]));
1868
1869 if (status < 0)
[1]1870 {
1871 if (status == (-1)) {
1872 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__,
1873 status, MSG_MOD_FAIL,
1874 _(modList[modnum].name),
[207]1875 status+SH_MOD_OFFSET);
[1]1876 } else {
1877 sh_error_handle ((-1), FIL__, __LINE__,
1878 status, MSG_MOD_FAIL,
1879 _(modList[modnum].name),
[207]1880 status+SH_MOD_OFFSET);
[1]1881 }
[142]1882 modList[modnum].initval = SH_MOD_FAILED;
[1]1883 }
1884 else
1885 {
1886 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1887 _(modList[modnum].name));
[142]1888 modList[modnum].initval = status;
[1]1889 }
1890 }
[149]1891
1892 /* module is properly set up now
1893 */
1894 sh_thread_pause_flag = S_FALSE;
[1]1895
1896 --sig_raised;
1897 }
1898
1899 if (sig_fresh_trail == 1) /* SIGIOT */
1900 {
[143]1901 if (sh_global_suspend_flag == 0)
1902 {
1903 SH_MUTEX_LOCK(mutex_thread_nolog);
1904
1905 /* Logfile access
1906 */
[1]1907#ifdef SH_USE_XML
[143]1908 (void) sh_log_file (NULL, NULL);
[1]1909#endif
[143]1910 TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
1911 sh_error_only_stderr (S_TRUE);
1912 (void) sh_unix_rm_lock_file(sh.srvlog.name);
1913 (void) retry_msleep(3, 0);
1914 sh.flag.log_start = S_TRUE;
1915 sh_error_only_stderr (S_FALSE);
1916 sh_thread_pause_flag = S_FALSE;
1917 sig_fresh_trail = 0;
1918 --sig_raised;
1919 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1920 }
[1]1921 }
1922
1923 if (sig_terminate == 1) /* SIGQUIT */
1924 {
1925 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1926 strncpy (sh_sig_msg, _("Quit"), 20);
1927 --sig_raised; --sig_urgent;
1928 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1929 }
1930
1931 if (sig_debug_switch == 1) /* SIGUSR1 */
1932 {
1933 TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
1934 sh_error_dbg_switch();
1935 sig_debug_switch = 0;
1936 --sig_raised;
1937 }
1938
[19]1939 if (sig_suspend_switch > 0) /* SIGUSR2 */
[1]1940 {
1941 TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
[143]1942 if (sh_global_suspend_flag != 1) {
1943 SH_MUTEX_LOCK_UNSAFE(mutex_thread_nolog);
1944 sh_global_suspend_flag = 1;
[1]1945 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND,
1946 sh.prg_name);
[143]1947 } else {
1948 sh_global_suspend_flag = 0;
1949 SH_MUTEX_UNLOCK_UNSAFE(mutex_thread_nolog);
[1]1950 }
[19]1951 --sig_suspend_switch;
[1]1952 --sig_raised; --sig_urgent;
1953 }
1954 sig_raised = (sig_raised < 0) ? 0 : sig_raised;
1955 sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
1956 TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
1957 }
1958
1959 if (sh_global_suspend_flag == 1)
1960 {
1961 (void) retry_msleep (1, 0);
1962 continue;
1963 }
1964
1965 /* see whether its time to check files
1966 */
[25]1967 if (sh.flag.checkSum == SH_CHECK_INIT ||
[367]1968 (sh.flag.inotify & SH_INOTIFY_DOSCAN) != 0 ||
[25]1969 (sh.flag.checkSum == SH_CHECK_CHECK &&
1970 (sh.flag.isdaemon == S_FALSE && sh.flag.loop == S_FALSE)))
[1]1971 {
1972 flag_check_1 = 1;
1973 if (FileSchedTwo != NULL)
1974 flag_check_2 = 1;
1975 }
1976 else if (sh.flag.checkSum == SH_CHECK_CHECK ||
1977 (sh.flag.update == S_TRUE &&
1978 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE)))
1979 {
1980 if (FileSchedOne == NULL)
1981 {
1982 /* use interval if we have no schedule
1983 */
1984 if (tcurrent - sh.fileCheck.alarm_last >=
1985 sh.fileCheck.alarm_interval)
1986 flag_check_1 = 1;
1987 }
1988 else
1989 {
1990 flag_check_1 = test_sched(FileSchedOne);
1991 if (FileSchedTwo != NULL)
1992 flag_check_2 = test_sched(FileSchedTwo);
1993 if (flag_check_2 == 1)
1994 flag_check_1 = 1;
1995 }
1996 }
1997
1998 check_done = 0;
1999
2000 if (sh.flag.checkSum != SH_CHECK_NONE &&
2001 (flag_check_1 == 1 || flag_check_2 == 1))
2002 {
[256]2003 /* Refresh list files matching glob patterns.
2004 */
2005 if (sh.flag.checkSum != SH_CHECK_INIT)
2006 sh_files_check_globPatterns();
2007
[1]2008 /*
2009 * check directories and files
2010 * ORDER IS IMPORTANT -- DIRZ FIRST
2011 */
2012 sh.statistics.bytes_hashed = 0;
2013 sh.statistics.time_start = time (NULL);
2014 sh.statistics.dirs_checked = 0;
2015 sh.statistics.files_checked = 0;
2016
2017 TPT((0, FIL__, __LINE__, _("msg=<Check directories.>\n")))
2018 BREAKEXIT(sh_dirs_chk);
2019 if (flag_check_1 == 1)
2020 {
2021 (void) sh_dirs_chk (1);
2022#ifndef SH_PROFILE
[153]2023 (void) retry_aud_chdir (FIL__, __LINE__, "/");
[1]2024#endif
2025 }
2026 if (flag_check_2 == 1)
2027 {
2028 (void) sh_dirs_chk (2);
2029#ifndef SH_PROFILE
[153]2030 (void) retry_aud_chdir (FIL__, __LINE__, "/");
[1]2031#endif
2032 }
2033 TPT((0, FIL__, __LINE__, _("msg=<Check files.>\n")))
2034 BREAKEXIT(sh_files_chk);
2035 if (flag_check_1 == 1)
2036 (void) sh_files_chk ();
2037
2038 if (sig_urgent > 0)
2039 continue;
2040
2041 /*
2042 * check for files not visited
2043 */
2044 if (flag_check_2 == 1 || FileSchedTwo == NULL)
2045 {
2046 TPT((0, FIL__, __LINE__, _("msg=<Check for missing files.>\n")))
2047 sh_hash_unvisited (ShDFLevel[SH_ERR_T_FILE]);
2048 }
2049
2050 if (sig_urgent > 0)
2051 continue;
2052
2053 /* reset
2054 */
2055 TPT((0, FIL__, __LINE__, _("msg=<Reset status.>\n")))
2056 sh_dirs_reset ();
2057 if (sig_urgent > 0)
2058 continue;
2059
2060 sh_files_reset ();
2061 flag_check_1 = 0;
2062 flag_check_2 = 0;
2063 check_done = 1;
[367]2064 sh.flag.inotify &= ~SH_INOTIFY_DOSCAN;
[1]2065
2066 (void) sh_prelink_run (NULL, NULL, 0);
2067
2068 if (sig_urgent > 0)
2069 continue;
2070
2071 runtim = time(NULL) - sh.statistics.time_start;
2072 sh.statistics.time_check = runtim;
2073
2074 if ((sh.statistics.dirs_checked == 0) &&
2075 (sh.statistics.files_checked == 0))
2076 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_0);
2077
2078 else
2079 {
2080 st_1 = (float) sh.statistics.bytes_hashed;
2081 st_2 = (float) runtim;
2082
2083
2084 if (st_1 > FLT_EPSILON && st_2 > FLT_EPSILON)
2085 st_1 = st_1/st_2;
2086 else if (st_1 > FLT_EPSILON)
2087 st_1 = (float) (st_1 * 1.0);
2088 else
2089 st_1 = 0.0;
2090
2091 sh.statistics.bytes_speed = (unsigned long) st_1;
2092
2093 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_1,
2094 (long) runtim,
2095 0.001 * st_1);
2096 }
2097 sh.fileCheck.alarm_last = time (NULL);
2098
2099 if (sig_urgent > 0)
2100 continue;
2101
2102 /*
2103 * flush mail queue
2104 */
2105#if defined(SH_WITH_MAIL)
2106 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
[214]2107 (void) sh_nmail_flush ();
[1]2108#endif
2109 }
2110
2111 if (sig_urgent > 0)
2112 continue;
2113
2114 /* execute modules
2115 */
2116 TPT((0, FIL__, __LINE__, _("msg=<Execute modules.>\n")))
2117 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
2118 {
[142]2119 if (modList[modnum].initval == SH_MOD_ACTIVE &&
[1]2120 0 != modList[modnum].mod_timer(tcurrent))
2121 if (0 != (status = modList[modnum].mod_check()))
2122 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_EXEC,
[207]2123 _(modList[modnum].name), (long) (status+SH_MOD_OFFSET));
[1]2124 }
2125
2126 /* 27.05.2002 avoid empty database
2127 * 22.10.2002 moved here b/o suid check initialization
2128 */
2129 if (sh.flag.checkSum == SH_CHECK_INIT)
2130 sh_hash_pushdata (NULL, NULL);
2131
2132 /* write out database
2133 */
2134 if (sh.flag.checkSum == SH_CHECK_CHECK &&
2135 sh.flag.update == S_TRUE &&
2136 check_done == 1)
2137 sh_hash_writeout ();
2138
[149]2139 /* no-op unless MEM_LOG is defined in sh_mem.c
2140 */
2141#ifdef MEM_DEBUG
2142 sh_mem_dump ();
2143#endif
2144
[248]2145 {
[252]2146 char * stale;
[248]2147
[252]2148 stale = sl_check_stale();
[248]2149 if (stale)
2150 {
2151 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
2152 stale, _("sl_check_stale"));
2153 }
[252]2154
2155 stale = sl_check_badfd();
2156 if (stale)
2157 {
2158 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
2159 stale, _("sl_check_stale"));
2160 }
[248]2161 }
2162
[1]2163 /* no loop if not daemon
2164 */
2165 if (sh.flag.isdaemon != S_TRUE && sh.flag.loop == S_FALSE)
2166 break;
2167 if (sig_urgent > 0)
2168 continue;
2169
2170 /* see whether its time to send mail
2171 */
2172#if defined(SH_WITH_MAIL)
2173 if (tcurrent - sh.mailTime.alarm_last >= sh.mailTime.alarm_interval)
2174 {
2175 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
[214]2176 (void) sh_nmail_flush ();
[1]2177 sh.mailTime.alarm_last = time (NULL);
2178 }
2179#endif
2180 if (sig_urgent > 0)
2181 continue;
2182
2183 /* log the timestamp
2184 */
2185 if ((int)(tcurrent - told) >= sh.looptime )
2186 {
2187 TPT((0, FIL__, __LINE__, _("msg=<Log the timestamp.>\n")))
2188 told = tcurrent;
2189#ifdef MEM_DEBUG
2190 sh_mem_check();
2191 sh_unix_count_mlock();
2192#else
2193 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_STAMP);
2194#endif
2195 }
2196
2197 /* seed / re-seed the PRNG if required
2198 */
2199 (void) taus_seed();
2200
2201 if (sig_urgent > 0)
2202 continue;
2203
[264]2204 /* reset cache
2205 */
2206 sh_userid_destroy();
2207
[1]2208 /* go to sleep
2209 */
2210 (void) retry_msleep (1, 0);
2211
2212 BREAKEXIT(sh_derr);
2213 (void) sh_derr();
2214 }
2215
2216 /* ------ END -----------
2217 */
2218
2219
2220
2221 /*
2222 * cleanup
2223 */
2224 TPT((0, FIL__, __LINE__, _("msg=<Cleanup.>\n")));
2225 sh_hash_hashdelete();
2226
2227#if defined(SH_WITH_MAIL)
2228 if (sh.mailNum.alarm_last > 0)
[214]2229 (void)sh_nmail_flush ();
[1]2230#endif
2231
2232 /* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
2233#endif
2234
[171]2235#if 0
2236 {
2237 char command[128];
2238 sprintf(command, "/bin/cat /proc/%d/status", (int) getpid());
2239 system(command); /* flawfinder: ignore *//* debug code */
2240 malloc_stats();
2241 }
2242#endif
2243
[1]2244 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
2245 SL_RETURN(0, _("main"));
2246}
Note: See TracBrowser for help on using the repository browser.