source: trunk/src/samhain.c@ 138

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

Fix compile errors.

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