source: trunk/src/samhain.c@ 145

Last change on this file since 145 was 145, checked in by rainer, 17 years ago

Fix for ticket #81 (compile failure with --enable-static).

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