source: trunk/src/samhain.c@ 358

Last change on this file since 358 was 354, checked in by katerina, 13 years ago

Fix for ticket #259 (option --enable-db-reload broken).

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