source: trunk/src/samhain.c@ 526

Last change on this file since 526 was 511, checked in by katerina, 8 years ago

New option PortCheckDevice =... (ticket #404).

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