source: trunk/src/samhain.c@ 442

Last change on this file since 442 was 433, checked in by katerina, 12 years ago

Fix for ticket #338 (steady growth of memory usage).

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