source: trunk/src/samhain.c@ 412

Last change on this file since 412 was 411, checked in by katerina, 12 years ago

More fixes for ticket #311 (thread safety of --enable-ptrace) and an option for setting the time difference.

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