source: trunk/src/samhain.c@ 479

Last change on this file since 479 was 473, checked in by katerina, 9 years ago

Fix for ticket #371 (use cppcheck instead of uno for static checking).

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