source: trunk/src/samhain.c@ 313

Last change on this file since 313 was 310, checked in by katerina, 14 years ago

Add option to skip checksum for certain files (ticket #232). Also fix for #231 (missing warning on invalid recursion depth).

File size: 50.6 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;
985
986
[22]987 fullpath = strdup (SH_INSTALL_PATH);
[1]988 if (fullpath == NULL)
[22]989 { perror(_("strdup")); exit (1); }
[1]990
[22]991 argp[0] = strdup (SH_INSTALL_PATH);
[1]992 if (argp[0] == NULL)
[22]993 { perror(_("strdup")); exit (1); }
[1]994
995 for (times = 1; times < 32; ++times) argp[times] = NULL;
996
997 res = (*argc > 32) ? 32 : *argc;
998
999 for (times = 2; times < res; ++times)
1000 {
[22]1001 argp[times-1] = strdup (argv[times]);
[1]1002 if (argp[times-1] == NULL)
[22]1003 { perror(_("strdup")); exit (1); }
[1]1004 }
1005
1006 if (ctl == 1)
1007 {
1008 pid = pidofSamhain(1);
1009
1010 if (pid != 0 && 0 == kill (pid, 0)) /* already started */
1011 exit (0);
1012
1013 pid = fork();
1014 switch (pid) {
1015 case ((pid_t) -1):
1016 perror(_("fork"));
1017 exit (1);
1018 case 0:
[252]1019 if (0 != sl_close_fd (FIL__, __LINE__, 0))
[1]1020 {
1021 _exit(4);
1022 }
[22]1023 (void) execv(fullpath, argp); /* flawfinder: ignore *//* wtf? */
[1]1024 if (errno == EPERM)
1025 _exit(4);
1026 else if (errno == ENOENT)
1027 _exit(5);
1028 _exit (1);
1029 default:
1030 times = 0;
[92]1031 while (times < 300) {
[1]1032 respid = waitpid(pid, &status, WNOHANG|WUNTRACED);
1033 if ((pid_t)-1 == respid)
1034 {
1035 perror(_("waitpid"));
1036 exit (1);
1037 }
1038 else if (pid == respid)
1039 {
[96]1040#ifndef USE_UNO
[1]1041 if (0 != WIFEXITED(status))
1042 {
1043 res = WEXITSTATUS(status);
1044 exit (res == 0 ? 0 : res );
1045 }
1046 else
1047 exit (1);
[96]1048#else
1049 exit (1);
1050#endif
[1]1051 }
1052 ++times;
1053 (void) retry_msleep(1, 0);
1054 }
[79]1055 exit (0); /* assume that it runs ok */
[1]1056 }
1057 }
1058
1059 pid = pidofSamhain(0);
1060
1061 if (ctl == 2) /* stop */
1062 {
1063 pidlist = procdirSamhain ();
1064 if (pid == 0 && NULL == pidlist) /* pid file not found */
[22]1065 {
1066 free(fullpath);
1067 return (0);
1068 }
[1]1069
1070 status = 0;
1071 if (pid != 0)
1072 status = killprocSamhain(pid);
1073 if (pidlist != NULL)
1074 {
1075 i = 0;
1076 while (i < 65535 && pidlist[i] != 0)
1077 {
1078 if (pidlist[i] != pid)
1079 status = killprocSamhain(pidlist[i]);
1080 ++i;
1081 }
1082 }
[22]1083 free(fullpath);
[1]1084 if (status == 7)
1085 return 0;
1086 else
1087 return status;
1088 }
1089
1090 if (ctl == 3) /* reload */
1091 {
1092 if (pid == 0)
1093 exit (7);
1094 if (0 == kill (pid, SIGHUP))
1095 exit (0);
1096 else
1097 {
1098 if (errno == EPERM)
1099 exit (4);
1100 if (errno == ESRCH)
1101 exit (7);
1102 exit (1);
1103 }
1104 }
1105
1106 if (ctl == 4) /* status */
1107 {
1108 if (pid == 0)
1109 exit (3);
1110 if (0 == kill (pid, 0))
1111 exit (0);
1112 else
1113 {
1114 if (errno == EPERM)
1115 exit (4);
1116 if (errno == ESRCH)
1117 exit (1);
1118 }
1119 }
[22]1120 free(fullpath); /* silence smatch false positive */
[1]1121 exit (1); /* no exit handler installed yet */
1122 /*@notreached@*/
1123 return (0);
1124}
1125/*@+exitarg@*/
1126
1127#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1128#include "sh_schedule.h"
1129static sh_schedule_t * FileSchedOne = NULL;
1130static sh_schedule_t * FileSchedTwo = NULL;
1131
1132/* free a linked list of schedules
1133 */
1134static sh_schedule_t * free_sched (sh_schedule_t * isched)
1135{
1136 sh_schedule_t * current = isched;
1137 sh_schedule_t * next = NULL;
1138
1139 while (current != NULL)
1140 {
1141 next = current->next;
1142 SH_FREE(current);
1143 current = next;
1144 }
1145 return NULL;
1146}
1147
1148/* Add a new schedule to the linked list of schedules
1149 */
[22]1150static sh_schedule_t * sh_set_schedule_int (const char * str,
[1]1151 sh_schedule_t * FileSchedIn,
1152 /*@out@*/ int * status)
1153{
1154 sh_schedule_t * FileSched;
1155
1156 SL_ENTER(_("sh_set_schedule_int"));
1157
1158 if (0 == sl_strncmp(str, _("NULL"), 4))
1159 {
1160 (void) free_sched(FileSchedIn);
1161 FileSchedIn = NULL;
1162 *status = 0;
1163 return 0;
1164 }
1165
1166 FileSched = SH_ALLOC(sizeof(sh_schedule_t));
1167 *status = create_sched(str, FileSched);
1168 if (*status != 0)
1169 {
1170 SH_FREE(FileSched);
1171 FileSched = NULL;
1172 SL_RETURN(FileSchedIn , _("sh_set_schedule_int"));
1173 }
1174 FileSched->next = FileSchedIn;
1175 SL_RETURN(FileSched , _("sh_set_schedule_int"));
1176}
1177
1178/* Add a new schedule to the linked list FileSchedOne
1179 */
[22]1180int sh_set_schedule_one (const char * str)
[1]1181{
1182 int status;
1183 FileSchedOne = sh_set_schedule_int (str, FileSchedOne, &status);
1184 return status;
1185}
1186
1187/* Add a new schedule to the linked list FileSchedTwo
1188 */
[22]1189int sh_set_schedule_two (const char * str)
[1]1190{
1191 int status;
1192 FileSchedTwo = sh_set_schedule_int (str, FileSchedTwo, &status);
1193 return status;
1194}
1195
1196#endif
1197
1198/*******************************************************
1199 *
1200 * Main program
1201 *
1202 *******************************************************/
[18]1203#if !defined(SH_CUTEST)
[1]1204int main(int argc, char * argv[])
[18]1205#else
1206int undef_main(int argc, char * argv[])
1207#endif
[1]1208{
1209#if defined(INET_SYSLOG)
1210 extern int create_syslog_socket (int flag);
1211#endif
1212#if defined(SH_WITH_SERVER)
[170]1213 extern int sh_create_tcp_socket(void);
[1]1214#endif
1215
1216#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1217 int modnum;
1218 time_t runtim;
1219 float st_1, st_2;
1220 int status;
[144]1221 volatile long cct = 0; /* main loop iterations */
[1]1222
[144]1223 volatile int flag_check_1 = 0;
1224 volatile int flag_check_2 = 0;
[1]1225
1226 int check_done = 0;
1227#endif
1228
[144]1229 volatile time_t told;
[170]1230 volatile time_t tcurrent;
[1]1231 size_t tzlen;
1232 char * tzptr;
1233 int res;
1234
1235#if defined (SH_STEALTH_NOCL)
[34]1236 char command_line[256];
[1]1237 int my_argc = 0;
1238 char * my_argv[32];
1239#endif
1240
[172]1241#if !defined(USE_SYSTEM_MALLOC)
1242 typedef void assert_handler_tp(const char * error, const char *file, int line);
1243 extern assert_handler_tp *dnmalloc_set_handler(assert_handler_tp *new);
1244 (void) dnmalloc_set_handler(safe_fatal);
1245#endif
1246
[134]1247 SH_G_INIT; /* Must precede any use of _() */
1248
[1]1249 SL_ENTER(_("main"));
1250
[92]1251 /* --- Close all but first three file descriptors. ---
1252 */
[174]1253 sh_unix_closeall(3, -1, SL_FALSE); /* at program start */
[92]1254
1255
[1]1256 if (argc >= 2 && 0 != getuid() &&
1257 (0 == strcmp(argv[1], _("start")) ||
1258 0 == strcmp(argv[1], _("stop")) ||
1259 0 == strcmp(argv[1], _("reload")) ||
1260 0 == strcmp(argv[1], _("force-reload")) ||
1261 0 == strcmp(argv[1], _("status")) ||
1262 0 == strcmp(argv[1], _("restart"))))
1263 {
1264 return 4;
1265 }
1266
1267 if (argc >= 2 && 0 == getuid())
1268 {
1269 /* return codes:
1270 * 0 Success
1271 * 1 Can not send signal / start program
1272 * 2 Pid file does not exist
1273 */
1274 if (0 == strcmp(argv[1], _("start")))
1275 {
1276 (void) samhainctl (1, &argc, argv); /* does not return */
1277 }
1278 else if (0 == strcmp(argv[1], _("stop")))
1279 return (samhainctl (2, &argc, argv));
1280 else if (0 == strcmp(argv[1], _("reload")))
1281 (void) samhainctl (3, &argc, argv); /* does not return */
1282 else if (0 == strcmp(argv[1], _("force-reload")))
1283 (void) samhainctl (3, &argc, argv); /* does not return */
1284 else if (0 == strcmp(argv[1], _("status")))
1285 (void) samhainctl (4, &argc, argv); /* does not return */
1286 else if (0 == strcmp(argv[1], _("restart")))
1287 {
1288 res = samhainctl (2, &argc, argv);
1289 if (res == 0 || res == 7)
1290 {
1291 (void) samhainctl (1, &argc, argv); /* does not return */
1292 }
1293 else
1294 return (res);
1295 }
1296 }
1297
1298 /* if fd 0 is closed, presume that we want to be daemon and
1299 * run in check mode
1300 */
1301 if ((-1) == retry_fcntl(FIL__, __LINE__, 0, F_GETFL, 0) &&
1302 errno == EBADF)
1303 {
1304 sh.flag.opts = S_TRUE;
1305 (void) sh_unix_setdeamon(NULL);
1306#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1307 sh.flag.checkSum = SH_CHECK_CHECK;
1308 /* (void) sh_util_setchecksum(_("check")); */
1309#endif
1310 is_samhainctl_init = S_TRUE;
1311 sh.flag.opts = S_FALSE;
1312 }
1313
1314
1315 /* --- Install the exit handler. ---
1316 */
1317 (void) atexit(exit_handler);
1318
1319 /* --- Zero the mailer key, and fill it. ---
1320 */
1321 memset (ErrFlag, 0, 2*sizeof(UINT32));
1322
1323#ifdef MKA_01
1324 ErrFlag[0] |= (1 << 0);
1325#endif
1326#ifdef MKA_02
1327 ErrFlag[0] |= (1 << 1);
1328#endif
1329#ifdef MKA_03
1330 ErrFlag[0] |= (1 << 2);
1331#endif
1332#ifdef MKA_04
1333 ErrFlag[0] |= (1 << 3);
1334#endif
1335#ifdef MKA_05
1336 ErrFlag[0] |= (1 << 4);
1337#endif
1338#ifdef MKA_06
1339 ErrFlag[0] |= (1 << 5);
1340#endif
1341#ifdef MKA_07
1342 ErrFlag[0] |= (1 << 6);
1343#endif
1344#ifdef MKA_08
1345 ErrFlag[0] |= (1 << 7);
1346#endif
1347
1348#if defined(SCREW_IT_UP)
1349 BREAKEXIT(sh_sigtrap_prepare);
1350 (void) sh_sigtrap_prepare();
1351#endif
1352
1353 /* Save the timezone.
1354 */
[22]1355 if (NULL != (tzptr = getenv("TZ"))) /* flawfinder: ignore */
[1]1356 {
1357 tzlen = strlen(tzptr);
[22]1358 if (tzlen < 1024)
1359 {
1360 sh.timezone = malloc (tzlen + 1);
1361 if (sh.timezone != NULL)
1362 (void) sl_strlcpy (sh.timezone, tzptr, tzlen + 1);
1363 }
1364 else
1365 sh.timezone = NULL;
[1]1366 }
1367 else
1368 sh.timezone = NULL;
1369
1370
1371 /* -------- INIT --------
1372 */
1373
1374 /* Restrict error logging to stderr.
1375 */
1376 sh_error_only_stderr (S_TRUE);
1377
1378 BREAKEXIT(sh_derr);
1379 (void) sh_derr();
1380
1381 /* Check that first three descriptors are open.
1382 */
1383 if ( retry_fcntl(FIL__, __LINE__, 0, F_GETFL, 0) == (-1))
1384 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 0);
1385 if ( retry_fcntl(FIL__, __LINE__, 1, F_GETFL, 0) == (-1))
1386 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 1);
1387 if ( retry_fcntl(FIL__, __LINE__, 2, F_GETFL, 0) == (-1))
1388 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 2);
1389
1390 /* --- Set default values. ---
1391 */
1392 BREAKEXIT(sh_init);
1393 sh_init (); /* we are still privileged here, so we can mlock skey */
1394#if (defined (SH_WITH_SERVER) && !defined (SH_WITH_CLIENT))
1395 sh.flag.isserver = S_TRUE;
1396#endif
1397
1398 /* --- Get local hostname. ---
1399 */
1400 BREAKEXIT(sh_unix_localhost);
1401 sh_unix_localhost();
1402
1403 /* --- Read the command line. ---
1404 */
1405 sh.flag.opts = S_TRUE;
1406
1407#if !defined(SH_STEALTH_NOCL)
[20]1408 sh_argc_store = argc;
1409 sh_argv_store = argv;
[1]1410 (void) sh_getopt_get (argc, argv);
1411#else
1412 if (argc > 1 && argv[1] != NULL &&
1413 strlen(argv[1]) > 0 && strlen(NOCL_CODE) > 0)
1414 {
[34]1415 if ( 0 == strcmp(argv[1], NOCL_CODE) )
[1]1416 {
[131]1417#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1418 char * saveptr;
1419#endif
[1]1420 my_argv[0] = argv[0]; ++my_argc;
1421 command_line[0] = '\0';
[153]1422 if (NULL != fgets (command_line, sizeof(command_line), stdin))
1423 command_line[sizeof(command_line)-1] = '\0';
1424
[1]1425 do {
[131]1426#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
[1]1427 my_argv[my_argc] =
[131]1428 strtok_r( (my_argc == 1) ? command_line : NULL, " \n", &saveptr);
1429#else
1430 my_argv[my_argc] =
1431 strtok( (my_argc == 1) ? command_line : NULL, " \n");
1432#endif
[1]1433 if (my_argv[my_argc] != NULL) {
1434 ++my_argc;
1435 } else {
1436 break;
1437 }
1438 } while (my_argc < 32);
[20]1439
1440 sh_argc_store = my_argc;
1441 sh_argv_store = my_argv;
1442
[1]1443 (void) sh_getopt_get (my_argc, my_argv);
1444 }
1445 else
1446 {
1447 /* discard command line */
1448 /* _exit(EXIT_FAILURE) */ ;
1449 }
1450 }
1451#endif
1452 sh.flag.opts = S_FALSE;
1453
1454
1455 /* --- Get user info. ---
1456 */
1457 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1458 if (0 != sh_unix_getUser ())
1459 {
1460 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1461 sh.prg_name);
1462 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1463 }
1464
1465
1466 /* *****************************
1467 *
1468 * Read the configuration file.
1469 *
1470 * *****************************/
1471
1472 TPT((0, FIL__, __LINE__, _("msg=<Read the configuration file.>\n")))
1473 BREAKEXIT(sh_readconf_read);
1474 (void) sh_readconf_read ();
1475
1476#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1477 if (sh.flag.checkSum == SH_CHECK_NONE)
1478 {
1479 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
1480 _("No action specified: init, update, or check"),
1481 _("main"));
1482 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1483 sh.prg_name);
1484 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
1485 }
1486#endif
1487
1488 /* do not append to database if run SUID
1489 */
1490 if ((sh.flag.checkSum == SH_CHECK_INIT) && (0 != sl_is_suid()))
1491 {
1492 (void) dlog(1, FIL__, __LINE__,
1493 _("Cannot initialize database when running with SUID credentials.\nYou need to run this with the user ID %d.\nYour current user ID is %d."),
1494 (int) geteuid(), (int) sh.real.uid);
1495 sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_ACCESS,
1496 (long) sh.real.uid, sh.data.path);
1497 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1498 }
1499
1500 /* avoid daemon mode for initialization
1501 */
1502 if (sh.flag.checkSum == SH_CHECK_INIT)
1503 {
1504 sh.flag.isdaemon = S_FALSE;
1505 sh.flag.loop = S_FALSE;
1506 }
1507
[59]1508 /* --- load database; checksum of database
1509 */
1510#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1511 TPT((0, FIL__, __LINE__, _("msg=<Get checksum of the database.>\n")))
1512 if (sh.flag.checkSum == SH_CHECK_CHECK)
1513 {
1514 if (0 != sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
1515 {
[133]1516 char hashbuf[KEYBUF_SIZE];
[59]1517 (void) sl_strlcpy(sh.data.hash,
1518 sh_tiger_hash (file_path('D', 'R'),
[160]1519 TIGER_FILE, TIGER_NOLIM,
[133]1520 hashbuf, sizeof(hashbuf)),
[59]1521 KEY_LEN+1);
1522 }
1523
1524 /* this eventually fetches the file from server to get checksum
1525 */
1526 sh_hash_init ();
1527 }
1528#endif
1529
1530 /* --- initialize signal handling etc.; fork daemon
1531 */
[1]1532 if (sh_unix_init(sh.flag.isdaemon) == -1)
1533 {
1534 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1535 sh.prg_name);
1536 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1537 }
1538
1539 /* --- drop privileges eventually ---
1540 */
1541#if defined(SH_WITH_SERVER)
1542 sh_create_tcp_socket ();
1543#if defined(INET_SYSLOG)
1544 create_syslog_socket (S_TRUE);
1545#endif
1546 SL_REQUIRE(sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE,
1547 _("sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE"));
1548#else
1549 SL_REQUIRE(sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE,
1550 _("sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE"));
1551#endif
1552
1553 /* --- Get user info (again). ---
1554 */
1555 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1556 if (0 != sh_unix_getUser ())
1557 {
1558 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1559 sh.prg_name);
1560 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1561 }
1562
1563 /* --- now check whether we really wanted it; if not, close ---
1564 */
1565#if defined(INET_SYSLOG) && defined(SH_WITH_SERVER)
1566 create_syslog_socket (S_FALSE);
1567#endif
1568
1569
1570 /* --- Enable full error logging ---
1571 */
1572 sh_error_only_stderr (S_FALSE);
1573
[256]1574 sh.flag.started = S_TRUE;
1575
[1]1576 /****************************************************
1577 *
1578 * SERVER
1579 *
1580 ****************************************************/
1581
1582#if defined(SH_WITH_SERVER) && !defined(SH_WITH_CLIENT)
1583
1584#if (defined(WITH_GPG) || defined(WITH_PGP))
[86]1585 /* log startup */
1586 sh_gpg_log_startup ();
[1]1587#else
1588 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1589 sh.prg_name, (long) sh.real.uid,
1590 (sh.flag.hidefile == S_TRUE) ?
1591 _("(hidden)") : file_path('C','R'),
1592 sh.conf.hash);
1593#endif
1594
1595#else
1596
1597 /****************************************************
1598 *
1599 * CLIENT/STANDALONE
1600 *
1601 ****************************************************/
1602
1603 BREAKEXIT(sh_error_handle);
1604
1605 if (sh.flag.checkSum == SH_CHECK_CHECK)
1606 {
1607#if (defined(WITH_GPG) || defined(WITH_PGP))
[86]1608 /* log startup */
1609 sh_gpg_log_startup ();
[1]1610#else
1611 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_2H,
1612 sh.prg_name, (long) sh.real.uid,
1613 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash,
1614 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('D', 'R'), sh.data.hash);
1615#endif
1616 }
1617 else
1618 {
1619#if (defined(WITH_GPG) || defined(WITH_PGP))
[86]1620 /* log startup */
1621 sh_gpg_log_startup ();
[1]1622#else
1623 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1624 sh.prg_name, (long) sh.real.uid,
1625 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash);
1626#endif
1627 }
1628#endif
1629
1630
1631 if ((skey == NULL) || (skey->mlock_failed == SL_TRUE))
1632 sh_error_handle ((-1), FIL__, __LINE__, EPERM, MSG_MLOCK);
1633
1634 /* timer
1635 */
1636 tcurrent = time (NULL);
1637 told = tcurrent;
1638 sh.mailTime.alarm_last = told;
1639
1640
1641 /****************************************************
1642 *
1643 * SERVER
1644 *
1645 ****************************************************/
1646
1647#if defined(SH_WITH_SERVER)
1648 TPT((0, FIL__, __LINE__, _("msg=<Start server.>\n")))
1649
1650#if defined (SH_WITH_CLIENT)
1651 if (sh.flag.isserver == S_TRUE)
1652 {
1653 sh_receive();
1654 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1655 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1656 }
1657#else
1658 sh_receive();
1659 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1660 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1661#endif
1662
1663#endif
1664
1665 /****************************************************
1666 *
1667 * CLIENT/STANDALONE
1668 *
1669 ****************************************************/
1670#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1671
1672
1673 /* --- Initialize modules. ---
1674 */
1675 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")))
1676 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1677 {
[142]1678 status = modList[modnum].mod_init(&(modList[modnum]));
1679 if ( status < 0 )
[1]1680 {
1681 if (status == (-1)) {
1682 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, status,
1683 MSG_MOD_FAIL,
1684 _(modList[modnum].name),
[207]1685 status+SH_MOD_OFFSET);
[1]1686 } else {
1687 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_FAIL,
1688 _(modList[modnum].name),
[207]1689 status+SH_MOD_OFFSET);
[1]1690 }
[142]1691 modList[modnum].initval = SH_MOD_FAILED;
[1]1692 }
1693 else
1694 {
1695 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1696 _(modList[modnum].name));
[142]1697 modList[modnum].initval = status;
[1]1698 }
1699 }
1700
1701 /* -------- TEST SETUP ---------
1702 */
1703 (void) sh_files_setrec();
1704 (void) sh_files_test_setup();
1705
1706
1707 /* -------- NICE LEVEL ---------
1708 */
1709 if (0 != sh.flag.nice)
1710 {
1711#ifdef HAVE_SETPRIORITY
1712 /*@-unrecog@*/
1713 (void) setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1714 /*@+unrecog@*/
1715#else
1716 (void) nice(sh.flag.nice);
1717#endif
1718 }
1719
1720 /* -------- MAIN LOOP ---------
1721 */
1722 sh.statistics.bytes_speed = 0;
1723 sh.statistics.bytes_hashed = 0;
1724
1725 while (1 == 1)
1726 {
1727 ++cct;
1728
1729 BREAKEXIT(sh_error_handle);
1730
1731 TPT((0, FIL__, __LINE__, _("msg=<Start main loop.>, iter=<%ld>\n"), cct))
1732
1733 tcurrent = time (NULL);
1734
1735 if (sig_raised > 0)
1736 {
1737
1738 TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
1739
1740 if (sig_termfast == 1) /* SIGTERM */
1741 {
1742 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1743 /* strncpy (sh_sig_msg, _("SIGTERM"), 20); */
1744 --sig_raised; --sig_urgent;
1745 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1746 }
1747
1748 if (sig_force_check == 1) /* SIGTTOU */
1749 {
1750 TPT((0, FIL__, __LINE__, _("msg=<Check run triggered.>\n")));
1751 flag_check_1 = 1;
1752 flag_check_2 = 1;
1753 sig_force_check = 0;
1754 --sig_raised;
1755 }
1756
[145]1757 if (sig_config_read_again == 1 && /* SIGHUP */
1758 sh_global_suspend_flag == 0)
[1]1759 {
1760 TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")))
1761 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
1762
[143]1763 sh_thread_pause_flag = S_TRUE;
1764
[1]1765#if defined(WITH_EXTERNAL)
1766 /* delete list of external tasks
1767 */
1768 (void) sh_ext_cleanup();
1769#endif
[215]1770#if defined(SH_WITH_MAIL)
1771 sh_nmail_free();
1772#endif
1773
[1]1774 /* delete the file list, make all database
1775 * entries visible (allignore = FALSE)
1776 */
1777 (void) sh_files_deldirstack ();
1778 (void) sh_files_delfilestack ();
[256]1779 (void) sh_files_delglobstack ();
[1]1780 (void) sh_ignore_clean ();
1781 (void) hash_full_tree ();
[294]1782 sh_audit_delete_all ();
[1]1783
[294]1784
[1]1785#if defined(SH_WITH_CLIENT)
1786 reset_count_dev_server();
1787#endif
1788#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
[310]1789 sh_restrict_purge ();
[1]1790
1791
1792 FileSchedOne = free_sched(FileSchedOne);
1793 FileSchedTwo = free_sched(FileSchedTwo);
1794
1795 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1796 {
[149]1797 /* sh_thread_pause_flag is true, and we block in lock
1798 * until check has returned, so we are sure check will
1799 * not run until sh_thread_pause_flag is set to false
1800 */
1801 /* if (modList[modnum].initval >= SH_MOD_ACTIVE) */
1802 (void) modList[modnum].mod_reconf();
[1]1803 }
1804#endif
1805
1806 reset_count_dev_console();
1807 reset_count_dev_time();
1808
1809 (void) sh_unix_maskreset();
1810
1811 /* Should this be included ???
1812 * (i.e. should we reload the database ?)
1813 */
1814#ifdef RELOAD_DATABASE
1815 sh_hash_hashdelete();
1816#endif
1817 (void) sl_trust_purge_user();
1818 (void) sh_files_hle_reg (NULL);
1819 (void) sh_prelink_run (NULL, NULL, 0);
1820
1821 /* --------------------------
1822 * --- READ CONFIGURATION ---
1823 * --------------------------
1824 */
1825 (void) sh_readconf_read ();
1826 sig_config_read_again = 0;
1827 (void) sh_files_setrec();
1828 (void) sh_files_test_setup();
1829 if (0 != sh.flag.nice)
1830 {
1831#ifdef HAVE_SETPRIORITY
1832 setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1833#else
1834 nice(sh.flag.nice);
1835#endif
1836 }
1837
1838 if (sh.flag.checkSum == SH_CHECK_INIT)
1839 {
1840 sh.flag.isdaemon = S_FALSE;
1841 sh.flag.loop = S_FALSE;
1842 }
1843
[143]1844
[1]1845 /* --- Initialize modules. ---
1846 */
1847 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")));
1848 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1849 {
[142]1850 status = modList[modnum].mod_init(&(modList[modnum]));
1851
1852 if (status < 0)
[1]1853 {
1854 if (status == (-1)) {
1855 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__,
1856 status, MSG_MOD_FAIL,
1857 _(modList[modnum].name),
[207]1858 status+SH_MOD_OFFSET);
[1]1859 } else {
1860 sh_error_handle ((-1), FIL__, __LINE__,
1861 status, MSG_MOD_FAIL,
1862 _(modList[modnum].name),
[207]1863 status+SH_MOD_OFFSET);
[1]1864 }
[142]1865 modList[modnum].initval = SH_MOD_FAILED;
[1]1866 }
1867 else
1868 {
1869 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1870 _(modList[modnum].name));
[142]1871 modList[modnum].initval = status;
[1]1872 }
1873 }
[149]1874
1875 /* module is properly set up now
1876 */
1877 sh_thread_pause_flag = S_FALSE;
[1]1878
1879 --sig_raised;
1880 }
1881
1882 if (sig_fresh_trail == 1) /* SIGIOT */
1883 {
[143]1884 if (sh_global_suspend_flag == 0)
1885 {
1886 SH_MUTEX_LOCK(mutex_thread_nolog);
1887
1888 /* Logfile access
1889 */
[1]1890#ifdef SH_USE_XML
[143]1891 (void) sh_log_file (NULL, NULL);
[1]1892#endif
[143]1893 TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
1894 sh_error_only_stderr (S_TRUE);
1895 (void) sh_unix_rm_lock_file(sh.srvlog.name);
1896 (void) retry_msleep(3, 0);
1897 sh.flag.log_start = S_TRUE;
1898 sh_error_only_stderr (S_FALSE);
1899 sh_thread_pause_flag = S_FALSE;
1900 sig_fresh_trail = 0;
1901 --sig_raised;
1902 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1903 }
[1]1904 }
1905
1906 if (sig_terminate == 1) /* SIGQUIT */
1907 {
1908 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1909 strncpy (sh_sig_msg, _("Quit"), 20);
1910 --sig_raised; --sig_urgent;
1911 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1912 }
1913
1914 if (sig_debug_switch == 1) /* SIGUSR1 */
1915 {
1916 TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
1917 sh_error_dbg_switch();
1918 sig_debug_switch = 0;
1919 --sig_raised;
1920 }
1921
[19]1922 if (sig_suspend_switch > 0) /* SIGUSR2 */
[1]1923 {
1924 TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
[143]1925 if (sh_global_suspend_flag != 1) {
1926 SH_MUTEX_LOCK_UNSAFE(mutex_thread_nolog);
1927 sh_global_suspend_flag = 1;
[1]1928 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND,
1929 sh.prg_name);
[143]1930 } else {
1931 sh_global_suspend_flag = 0;
1932 SH_MUTEX_UNLOCK_UNSAFE(mutex_thread_nolog);
[1]1933 }
[19]1934 --sig_suspend_switch;
[1]1935 --sig_raised; --sig_urgent;
1936 }
1937 sig_raised = (sig_raised < 0) ? 0 : sig_raised;
1938 sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
1939 TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
1940 }
1941
1942 if (sh_global_suspend_flag == 1)
1943 {
1944 (void) retry_msleep (1, 0);
1945 continue;
1946 }
1947
1948 /* see whether its time to check files
1949 */
[25]1950 if (sh.flag.checkSum == SH_CHECK_INIT ||
1951 (sh.flag.checkSum == SH_CHECK_CHECK &&
1952 (sh.flag.isdaemon == S_FALSE && sh.flag.loop == S_FALSE)))
[1]1953 {
1954 flag_check_1 = 1;
1955 if (FileSchedTwo != NULL)
1956 flag_check_2 = 1;
1957 }
1958 else if (sh.flag.checkSum == SH_CHECK_CHECK ||
1959 (sh.flag.update == S_TRUE &&
1960 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE)))
1961 {
1962 if (FileSchedOne == NULL)
1963 {
1964 /* use interval if we have no schedule
1965 */
1966 if (tcurrent - sh.fileCheck.alarm_last >=
1967 sh.fileCheck.alarm_interval)
1968 flag_check_1 = 1;
1969 }
1970 else
1971 {
1972 flag_check_1 = test_sched(FileSchedOne);
1973 if (FileSchedTwo != NULL)
1974 flag_check_2 = test_sched(FileSchedTwo);
1975 if (flag_check_2 == 1)
1976 flag_check_1 = 1;
1977 }
1978 }
1979
1980 check_done = 0;
1981
1982 if (sh.flag.checkSum != SH_CHECK_NONE &&
1983 (flag_check_1 == 1 || flag_check_2 == 1))
1984 {
[256]1985 /* Refresh list files matching glob patterns.
1986 */
1987 if (sh.flag.checkSum != SH_CHECK_INIT)
1988 sh_files_check_globPatterns();
1989
[1]1990 /*
1991 * check directories and files
1992 * ORDER IS IMPORTANT -- DIRZ FIRST
1993 */
1994 sh.statistics.bytes_hashed = 0;
1995 sh.statistics.time_start = time (NULL);
1996 sh.statistics.dirs_checked = 0;
1997 sh.statistics.files_checked = 0;
1998
1999 TPT((0, FIL__, __LINE__, _("msg=<Check directories.>\n")))
2000 BREAKEXIT(sh_dirs_chk);
2001 if (flag_check_1 == 1)
2002 {
2003 (void) sh_dirs_chk (1);
2004#ifndef SH_PROFILE
[153]2005 (void) retry_aud_chdir (FIL__, __LINE__, "/");
[1]2006#endif
2007 }
2008 if (flag_check_2 == 1)
2009 {
2010 (void) sh_dirs_chk (2);
2011#ifndef SH_PROFILE
[153]2012 (void) retry_aud_chdir (FIL__, __LINE__, "/");
[1]2013#endif
2014 }
2015 TPT((0, FIL__, __LINE__, _("msg=<Check files.>\n")))
2016 BREAKEXIT(sh_files_chk);
2017 if (flag_check_1 == 1)
2018 (void) sh_files_chk ();
2019
2020 if (sig_urgent > 0)
2021 continue;
2022
2023 /*
2024 * check for files not visited
2025 */
2026 if (flag_check_2 == 1 || FileSchedTwo == NULL)
2027 {
2028 TPT((0, FIL__, __LINE__, _("msg=<Check for missing files.>\n")))
2029 sh_hash_unvisited (ShDFLevel[SH_ERR_T_FILE]);
2030 }
2031
2032 if (sig_urgent > 0)
2033 continue;
2034
2035 /* reset
2036 */
2037 TPT((0, FIL__, __LINE__, _("msg=<Reset status.>\n")))
2038 sh_dirs_reset ();
2039 if (sig_urgent > 0)
2040 continue;
2041
2042 sh_files_reset ();
2043 flag_check_1 = 0;
2044 flag_check_2 = 0;
2045 check_done = 1;
2046
2047 (void) sh_prelink_run (NULL, NULL, 0);
2048
2049 if (sig_urgent > 0)
2050 continue;
2051
2052 runtim = time(NULL) - sh.statistics.time_start;
2053 sh.statistics.time_check = runtim;
2054
2055 if ((sh.statistics.dirs_checked == 0) &&
2056 (sh.statistics.files_checked == 0))
2057 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_0);
2058
2059 else
2060 {
2061 st_1 = (float) sh.statistics.bytes_hashed;
2062 st_2 = (float) runtim;
2063
2064
2065 if (st_1 > FLT_EPSILON && st_2 > FLT_EPSILON)
2066 st_1 = st_1/st_2;
2067 else if (st_1 > FLT_EPSILON)
2068 st_1 = (float) (st_1 * 1.0);
2069 else
2070 st_1 = 0.0;
2071
2072 sh.statistics.bytes_speed = (unsigned long) st_1;
2073
2074 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_1,
2075 (long) runtim,
2076 0.001 * st_1);
2077 }
2078 sh.fileCheck.alarm_last = time (NULL);
2079
2080 if (sig_urgent > 0)
2081 continue;
2082
2083 /*
2084 * flush mail queue
2085 */
2086#if defined(SH_WITH_MAIL)
2087 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
[214]2088 (void) sh_nmail_flush ();
[1]2089#endif
2090 }
2091
2092 if (sig_urgent > 0)
2093 continue;
2094
2095 /* execute modules
2096 */
2097 TPT((0, FIL__, __LINE__, _("msg=<Execute modules.>\n")))
2098 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
2099 {
[142]2100 if (modList[modnum].initval == SH_MOD_ACTIVE &&
[1]2101 0 != modList[modnum].mod_timer(tcurrent))
2102 if (0 != (status = modList[modnum].mod_check()))
2103 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_EXEC,
[207]2104 _(modList[modnum].name), (long) (status+SH_MOD_OFFSET));
[1]2105 }
2106
2107 /* 27.05.2002 avoid empty database
2108 * 22.10.2002 moved here b/o suid check initialization
2109 */
2110 if (sh.flag.checkSum == SH_CHECK_INIT)
2111 sh_hash_pushdata (NULL, NULL);
2112
2113 /* write out database
2114 */
2115 if (sh.flag.checkSum == SH_CHECK_CHECK &&
2116 sh.flag.update == S_TRUE &&
2117 check_done == 1)
2118 sh_hash_writeout ();
2119
[149]2120 /* no-op unless MEM_LOG is defined in sh_mem.c
2121 */
2122#ifdef MEM_DEBUG
2123 sh_mem_dump ();
2124#endif
2125
[248]2126 {
[252]2127 char * stale;
[248]2128
[252]2129 stale = sl_check_stale();
[248]2130 if (stale)
2131 {
2132 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
2133 stale, _("sl_check_stale"));
2134 }
[252]2135
2136 stale = sl_check_badfd();
2137 if (stale)
2138 {
2139 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
2140 stale, _("sl_check_stale"));
2141 }
[248]2142 }
2143
[1]2144 /* no loop if not daemon
2145 */
2146 if (sh.flag.isdaemon != S_TRUE && sh.flag.loop == S_FALSE)
2147 break;
2148 if (sig_urgent > 0)
2149 continue;
2150
2151 /* see whether its time to send mail
2152 */
2153#if defined(SH_WITH_MAIL)
2154 if (tcurrent - sh.mailTime.alarm_last >= sh.mailTime.alarm_interval)
2155 {
2156 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
[214]2157 (void) sh_nmail_flush ();
[1]2158 sh.mailTime.alarm_last = time (NULL);
2159 }
2160#endif
2161 if (sig_urgent > 0)
2162 continue;
2163
2164 /* log the timestamp
2165 */
2166 if ((int)(tcurrent - told) >= sh.looptime )
2167 {
2168 TPT((0, FIL__, __LINE__, _("msg=<Log the timestamp.>\n")))
2169 told = tcurrent;
2170#ifdef MEM_DEBUG
2171 sh_mem_check();
2172 sh_unix_count_mlock();
2173#else
2174 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_STAMP);
2175#endif
2176 }
2177
2178 /* seed / re-seed the PRNG if required
2179 */
2180 (void) taus_seed();
2181
2182 if (sig_urgent > 0)
2183 continue;
2184
[264]2185 /* reset cache
2186 */
2187 sh_userid_destroy();
2188
[1]2189 /* go to sleep
2190 */
2191 (void) retry_msleep (1, 0);
2192
2193 BREAKEXIT(sh_derr);
2194 (void) sh_derr();
2195 }
2196
2197 /* ------ END -----------
2198 */
2199
2200
2201
2202 /*
2203 * cleanup
2204 */
2205 TPT((0, FIL__, __LINE__, _("msg=<Cleanup.>\n")));
2206 sh_hash_hashdelete();
2207
2208#if defined(SH_WITH_MAIL)
2209 if (sh.mailNum.alarm_last > 0)
[214]2210 (void)sh_nmail_flush ();
[1]2211#endif
2212
2213 /* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
2214#endif
2215
[171]2216#if 0
2217 {
2218 char command[128];
2219 sprintf(command, "/bin/cat /proc/%d/status", (int) getpid());
2220 system(command); /* flawfinder: ignore *//* debug code */
2221 malloc_stats();
2222 }
2223#endif
2224
[1]2225 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
2226 SL_RETURN(0, _("main"));
2227}
Note: See TracBrowser for help on using the repository browser.