source: trunk/src/samhain.c@ 161

Last change on this file since 161 was 160, checked in by katerina, 17 years ago

Some more instances of missing TIGER_NOLIM fixed.

File size: 48.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 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 = (pid_t *)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 = (pid_t *)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 (pid_t *)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 if (NULL != fgets (command_line, sizeof(command_line), stdin))
1394 command_line[sizeof(command_line)-1] = '\0';
1395
1396 do {
1397#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1398 my_argv[my_argc] =
1399 strtok_r( (my_argc == 1) ? command_line : NULL, " \n", &saveptr);
1400#else
1401 my_argv[my_argc] =
1402 strtok( (my_argc == 1) ? command_line : NULL, " \n");
1403#endif
1404 if (my_argv[my_argc] != NULL) {
1405 ++my_argc;
1406 } else {
1407 break;
1408 }
1409 } while (my_argc < 32);
1410
1411 sh_argc_store = my_argc;
1412 sh_argv_store = my_argv;
1413
1414 (void) sh_getopt_get (my_argc, my_argv);
1415 }
1416 else
1417 {
1418 /* discard command line */
1419 /* _exit(EXIT_FAILURE) */ ;
1420 }
1421 }
1422#endif
1423 sh.flag.opts = S_FALSE;
1424
1425
1426 /* --- Get user info. ---
1427 */
1428 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1429 if (0 != sh_unix_getUser ())
1430 {
1431 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1432 sh.prg_name);
1433 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1434 }
1435
1436
1437 /* *****************************
1438 *
1439 * Read the configuration file.
1440 *
1441 * *****************************/
1442
1443 TPT((0, FIL__, __LINE__, _("msg=<Read the configuration file.>\n")))
1444 BREAKEXIT(sh_readconf_read);
1445 (void) sh_readconf_read ();
1446
1447#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1448 if (sh.flag.checkSum == SH_CHECK_NONE)
1449 {
1450 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
1451 _("No action specified: init, update, or check"),
1452 _("main"));
1453 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1454 sh.prg_name);
1455 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
1456 }
1457#endif
1458
1459 /* do not append to database if run SUID
1460 */
1461 if ((sh.flag.checkSum == SH_CHECK_INIT) && (0 != sl_is_suid()))
1462 {
1463 (void) dlog(1, FIL__, __LINE__,
1464 _("Cannot initialize database when running with SUID credentials.\nYou need to run this with the user ID %d.\nYour current user ID is %d."),
1465 (int) geteuid(), (int) sh.real.uid);
1466 sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_ACCESS,
1467 (long) sh.real.uid, sh.data.path);
1468 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1469 }
1470
1471 /* avoid daemon mode for initialization
1472 */
1473 if (sh.flag.checkSum == SH_CHECK_INIT)
1474 {
1475 sh.flag.isdaemon = S_FALSE;
1476 sh.flag.loop = S_FALSE;
1477 }
1478
1479 /* --- load database; checksum of database
1480 */
1481#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1482 TPT((0, FIL__, __LINE__, _("msg=<Get checksum of the database.>\n")))
1483 if (sh.flag.checkSum == SH_CHECK_CHECK)
1484 {
1485 if (0 != sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
1486 {
1487 char hashbuf[KEYBUF_SIZE];
1488 (void) sl_strlcpy(sh.data.hash,
1489 sh_tiger_hash (file_path('D', 'R'),
1490 TIGER_FILE, TIGER_NOLIM,
1491 hashbuf, sizeof(hashbuf)),
1492 KEY_LEN+1);
1493 }
1494
1495 /* this eventually fetches the file from server to get checksum
1496 */
1497 sh_hash_init ();
1498 }
1499#endif
1500
1501 /* --- initialize signal handling etc.; fork daemon
1502 */
1503 if (sh_unix_init(sh.flag.isdaemon) == -1)
1504 {
1505 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1506 sh.prg_name);
1507 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1508 }
1509
1510 /* --- drop privileges eventually ---
1511 */
1512#if defined(SH_WITH_SERVER)
1513 sh_create_tcp_socket ();
1514#if defined(INET_SYSLOG)
1515 create_syslog_socket (S_TRUE);
1516#endif
1517 SL_REQUIRE(sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE,
1518 _("sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE"));
1519#else
1520 SL_REQUIRE(sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE,
1521 _("sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE"));
1522#endif
1523
1524 /* --- Get user info (again). ---
1525 */
1526 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1527 if (0 != sh_unix_getUser ())
1528 {
1529 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1530 sh.prg_name);
1531 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1532 }
1533
1534 /* --- now check whether we really wanted it; if not, close ---
1535 */
1536#if defined(INET_SYSLOG) && defined(SH_WITH_SERVER)
1537 create_syslog_socket (S_FALSE);
1538#endif
1539
1540
1541 /* --- Enable full error logging ---
1542 */
1543 sh_error_only_stderr (S_FALSE);
1544
1545 /****************************************************
1546 *
1547 * SERVER
1548 *
1549 ****************************************************/
1550
1551#if defined(SH_WITH_SERVER) && !defined(SH_WITH_CLIENT)
1552
1553#if (defined(WITH_GPG) || defined(WITH_PGP))
1554 /* log startup */
1555 sh_gpg_log_startup ();
1556#else
1557 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1558 sh.prg_name, (long) sh.real.uid,
1559 (sh.flag.hidefile == S_TRUE) ?
1560 _("(hidden)") : file_path('C','R'),
1561 sh.conf.hash);
1562#endif
1563
1564#else
1565
1566 /****************************************************
1567 *
1568 * CLIENT/STANDALONE
1569 *
1570 ****************************************************/
1571
1572 BREAKEXIT(sh_error_handle);
1573
1574 if (sh.flag.checkSum == SH_CHECK_CHECK)
1575 {
1576#if (defined(WITH_GPG) || defined(WITH_PGP))
1577 /* log startup */
1578 sh_gpg_log_startup ();
1579#else
1580 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_2H,
1581 sh.prg_name, (long) sh.real.uid,
1582 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash,
1583 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('D', 'R'), sh.data.hash);
1584#endif
1585 }
1586 else
1587 {
1588#if (defined(WITH_GPG) || defined(WITH_PGP))
1589 /* log startup */
1590 sh_gpg_log_startup ();
1591#else
1592 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1593 sh.prg_name, (long) sh.real.uid,
1594 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash);
1595#endif
1596 }
1597#endif
1598
1599
1600 if ((skey == NULL) || (skey->mlock_failed == SL_TRUE))
1601 sh_error_handle ((-1), FIL__, __LINE__, EPERM, MSG_MLOCK);
1602
1603 /* timer
1604 */
1605 tcurrent = time (NULL);
1606 told = tcurrent;
1607 sh.mailTime.alarm_last = told;
1608
1609
1610 /****************************************************
1611 *
1612 * SERVER
1613 *
1614 ****************************************************/
1615
1616#if defined(SH_WITH_SERVER)
1617 TPT((0, FIL__, __LINE__, _("msg=<Start server.>\n")))
1618
1619#if defined (SH_WITH_CLIENT)
1620 if (sh.flag.isserver == S_TRUE)
1621 {
1622 sh_receive();
1623 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1624 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1625 }
1626#else
1627 sh_receive();
1628 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1629 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1630#endif
1631
1632#endif
1633
1634 /****************************************************
1635 *
1636 * CLIENT/STANDALONE
1637 *
1638 ****************************************************/
1639#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1640
1641
1642 /* --- Initialize modules. ---
1643 */
1644 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")))
1645 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1646 {
1647 status = modList[modnum].mod_init(&(modList[modnum]));
1648 if ( status < 0 )
1649 {
1650 if (status == (-1)) {
1651 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, status,
1652 MSG_MOD_FAIL,
1653 _(modList[modnum].name),
1654 status);
1655 } else {
1656 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_FAIL,
1657 _(modList[modnum].name),
1658 status);
1659 }
1660 modList[modnum].initval = SH_MOD_FAILED;
1661 }
1662 else
1663 {
1664 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1665 _(modList[modnum].name));
1666 modList[modnum].initval = status;
1667 }
1668 }
1669
1670 /* -------- TEST SETUP ---------
1671 */
1672 (void) sh_files_setrec();
1673 (void) sh_files_test_setup();
1674
1675
1676 /* -------- NICE LEVEL ---------
1677 */
1678 if (0 != sh.flag.nice)
1679 {
1680#ifdef HAVE_SETPRIORITY
1681 /*@-unrecog@*/
1682 (void) setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1683 /*@+unrecog@*/
1684#else
1685 (void) nice(sh.flag.nice);
1686#endif
1687 }
1688
1689 /* -------- MAIN LOOP ---------
1690 */
1691 sh.statistics.bytes_speed = 0;
1692 sh.statistics.bytes_hashed = 0;
1693
1694 while (1 == 1)
1695 {
1696 ++cct;
1697
1698 BREAKEXIT(sh_error_handle);
1699
1700 TPT((0, FIL__, __LINE__, _("msg=<Start main loop.>, iter=<%ld>\n"), cct))
1701
1702 tcurrent = time (NULL);
1703
1704 if (sig_raised > 0)
1705 {
1706
1707 TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
1708
1709 if (sig_termfast == 1) /* SIGTERM */
1710 {
1711 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1712 /* strncpy (sh_sig_msg, _("SIGTERM"), 20); */
1713 --sig_raised; --sig_urgent;
1714 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1715 }
1716
1717 if (sig_force_check == 1) /* SIGTTOU */
1718 {
1719 TPT((0, FIL__, __LINE__, _("msg=<Check run triggered.>\n")));
1720 flag_check_1 = 1;
1721 flag_check_2 = 1;
1722 sig_force_check = 0;
1723 --sig_raised;
1724 }
1725
1726 if (sig_config_read_again == 1 && /* SIGHUP */
1727 sh_global_suspend_flag == 0)
1728 {
1729 TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")))
1730 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
1731
1732 sh_thread_pause_flag = S_TRUE;
1733
1734#if defined(WITH_EXTERNAL)
1735 /* delete list of external tasks
1736 */
1737 (void) sh_ext_cleanup();
1738#endif
1739 /* delete the file list, make all database
1740 * entries visible (allignore = FALSE)
1741 */
1742 (void) sh_files_deldirstack ();
1743 (void) sh_files_delfilestack ();
1744 (void) sh_ignore_clean ();
1745 (void) hash_full_tree ();
1746
1747#if defined(SH_WITH_CLIENT)
1748 reset_count_dev_server();
1749#endif
1750#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
1751
1752
1753 FileSchedOne = free_sched(FileSchedOne);
1754 FileSchedTwo = free_sched(FileSchedTwo);
1755
1756 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1757 {
1758 /* sh_thread_pause_flag is true, and we block in lock
1759 * until check has returned, so we are sure check will
1760 * not run until sh_thread_pause_flag is set to false
1761 */
1762 /* if (modList[modnum].initval >= SH_MOD_ACTIVE) */
1763 (void) modList[modnum].mod_reconf();
1764 }
1765#endif
1766
1767#if defined(SH_WITH_MAIL)
1768 reset_count_dev_mail();
1769#endif
1770 reset_count_dev_console();
1771 reset_count_dev_time();
1772
1773 (void) sh_unix_maskreset();
1774
1775 /* Should this be included ???
1776 * (i.e. should we reload the database ?)
1777 */
1778#ifdef RELOAD_DATABASE
1779 sh_hash_hashdelete();
1780#endif
1781 (void) sl_trust_purge_user();
1782 (void) sh_files_hle_reg (NULL);
1783 (void) sh_prelink_run (NULL, NULL, 0);
1784
1785 /* --------------------------
1786 * --- READ CONFIGURATION ---
1787 * --------------------------
1788 */
1789 (void) sh_readconf_read ();
1790 sig_config_read_again = 0;
1791 (void) sh_files_setrec();
1792 (void) sh_files_test_setup();
1793 if (0 != sh.flag.nice)
1794 {
1795#ifdef HAVE_SETPRIORITY
1796 setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1797#else
1798 nice(sh.flag.nice);
1799#endif
1800 }
1801
1802 if (sh.flag.checkSum == SH_CHECK_INIT)
1803 {
1804 sh.flag.isdaemon = S_FALSE;
1805 sh.flag.loop = S_FALSE;
1806 }
1807
1808
1809 /* --- Initialize modules. ---
1810 */
1811 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")));
1812 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1813 {
1814 status = modList[modnum].mod_init(&(modList[modnum]));
1815
1816 if (status < 0)
1817 {
1818 if (status == (-1)) {
1819 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__,
1820 status, MSG_MOD_FAIL,
1821 _(modList[modnum].name),
1822 status);
1823 } else {
1824 sh_error_handle ((-1), FIL__, __LINE__,
1825 status, MSG_MOD_FAIL,
1826 _(modList[modnum].name),
1827 status);
1828 }
1829 modList[modnum].initval = SH_MOD_FAILED;
1830 }
1831 else
1832 {
1833 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1834 _(modList[modnum].name));
1835 modList[modnum].initval = status;
1836 }
1837 }
1838
1839 /* module is properly set up now
1840 */
1841 sh_thread_pause_flag = S_FALSE;
1842
1843 --sig_raised;
1844 }
1845
1846 if (sig_fresh_trail == 1) /* SIGIOT */
1847 {
1848 if (sh_global_suspend_flag == 0)
1849 {
1850 SH_MUTEX_LOCK(mutex_thread_nolog);
1851
1852 /* Logfile access
1853 */
1854#ifdef SH_USE_XML
1855 (void) sh_log_file (NULL, NULL);
1856#endif
1857 TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
1858 sh_error_only_stderr (S_TRUE);
1859 (void) sh_unix_rm_lock_file(sh.srvlog.name);
1860 (void) retry_msleep(3, 0);
1861 sh.flag.log_start = S_TRUE;
1862 sh_error_only_stderr (S_FALSE);
1863 sh_thread_pause_flag = S_FALSE;
1864 sig_fresh_trail = 0;
1865 --sig_raised;
1866 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1867 }
1868 }
1869
1870 if (sig_terminate == 1) /* SIGQUIT */
1871 {
1872 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1873 strncpy (sh_sig_msg, _("Quit"), 20);
1874 --sig_raised; --sig_urgent;
1875 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1876 }
1877
1878 if (sig_debug_switch == 1) /* SIGUSR1 */
1879 {
1880 TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
1881 sh_error_dbg_switch();
1882 sig_debug_switch = 0;
1883 --sig_raised;
1884 }
1885
1886 if (sig_suspend_switch > 0) /* SIGUSR2 */
1887 {
1888 TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
1889 if (sh_global_suspend_flag != 1) {
1890 SH_MUTEX_LOCK_UNSAFE(mutex_thread_nolog);
1891 sh_global_suspend_flag = 1;
1892 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND,
1893 sh.prg_name);
1894 } else {
1895 sh_global_suspend_flag = 0;
1896 SH_MUTEX_UNLOCK_UNSAFE(mutex_thread_nolog);
1897 }
1898 --sig_suspend_switch;
1899 --sig_raised; --sig_urgent;
1900 }
1901 sig_raised = (sig_raised < 0) ? 0 : sig_raised;
1902 sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
1903 TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
1904 }
1905
1906 if (sh_global_suspend_flag == 1)
1907 {
1908 (void) retry_msleep (1, 0);
1909 continue;
1910 }
1911
1912 /* see whether its time to check files
1913 */
1914 if (sh.flag.checkSum == SH_CHECK_INIT ||
1915 (sh.flag.checkSum == SH_CHECK_CHECK &&
1916 (sh.flag.isdaemon == S_FALSE && sh.flag.loop == S_FALSE)))
1917 {
1918 flag_check_1 = 1;
1919 if (FileSchedTwo != NULL)
1920 flag_check_2 = 1;
1921 }
1922 else if (sh.flag.checkSum == SH_CHECK_CHECK ||
1923 (sh.flag.update == S_TRUE &&
1924 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE)))
1925 {
1926 if (FileSchedOne == NULL)
1927 {
1928 /* use interval if we have no schedule
1929 */
1930 if (tcurrent - sh.fileCheck.alarm_last >=
1931 sh.fileCheck.alarm_interval)
1932 flag_check_1 = 1;
1933 }
1934 else
1935 {
1936 flag_check_1 = test_sched(FileSchedOne);
1937 if (FileSchedTwo != NULL)
1938 flag_check_2 = test_sched(FileSchedTwo);
1939 if (flag_check_2 == 1)
1940 flag_check_1 = 1;
1941 }
1942 }
1943
1944 check_done = 0;
1945
1946 if (sh.flag.checkSum != SH_CHECK_NONE &&
1947 (flag_check_1 == 1 || flag_check_2 == 1))
1948 {
1949 /*
1950 * check directories and files
1951 * ORDER IS IMPORTANT -- DIRZ FIRST
1952 */
1953 sh.statistics.bytes_hashed = 0;
1954 sh.statistics.time_start = time (NULL);
1955 sh.statistics.dirs_checked = 0;
1956 sh.statistics.files_checked = 0;
1957
1958 TPT((0, FIL__, __LINE__, _("msg=<Check directories.>\n")))
1959 BREAKEXIT(sh_dirs_chk);
1960 if (flag_check_1 == 1)
1961 {
1962 (void) sh_dirs_chk (1);
1963#ifndef SH_PROFILE
1964 (void) retry_aud_chdir (FIL__, __LINE__, "/");
1965#endif
1966 }
1967 if (flag_check_2 == 1)
1968 {
1969 (void) sh_dirs_chk (2);
1970#ifndef SH_PROFILE
1971 (void) retry_aud_chdir (FIL__, __LINE__, "/");
1972#endif
1973 }
1974 TPT((0, FIL__, __LINE__, _("msg=<Check files.>\n")))
1975 BREAKEXIT(sh_files_chk);
1976 if (flag_check_1 == 1)
1977 (void) sh_files_chk ();
1978
1979 if (sig_urgent > 0)
1980 continue;
1981
1982 /*
1983 * check for files not visited
1984 */
1985 if (flag_check_2 == 1 || FileSchedTwo == NULL)
1986 {
1987 TPT((0, FIL__, __LINE__, _("msg=<Check for missing files.>\n")))
1988 sh_hash_unvisited (ShDFLevel[SH_ERR_T_FILE]);
1989 }
1990
1991 if (sig_urgent > 0)
1992 continue;
1993
1994 /* reset
1995 */
1996 TPT((0, FIL__, __LINE__, _("msg=<Reset status.>\n")))
1997 sh_dirs_reset ();
1998 if (sig_urgent > 0)
1999 continue;
2000
2001 sh_files_reset ();
2002 flag_check_1 = 0;
2003 flag_check_2 = 0;
2004 check_done = 1;
2005
2006 (void) sh_prelink_run (NULL, NULL, 0);
2007
2008 if (sig_urgent > 0)
2009 continue;
2010
2011 runtim = time(NULL) - sh.statistics.time_start;
2012 sh.statistics.time_check = runtim;
2013
2014 if ((sh.statistics.dirs_checked == 0) &&
2015 (sh.statistics.files_checked == 0))
2016 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_0);
2017
2018 else
2019 {
2020 st_1 = (float) sh.statistics.bytes_hashed;
2021 st_2 = (float) runtim;
2022
2023
2024 if (st_1 > FLT_EPSILON && st_2 > FLT_EPSILON)
2025 st_1 = st_1/st_2;
2026 else if (st_1 > FLT_EPSILON)
2027 st_1 = (float) (st_1 * 1.0);
2028 else
2029 st_1 = 0.0;
2030
2031 sh.statistics.bytes_speed = (unsigned long) st_1;
2032
2033 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_1,
2034 (long) runtim,
2035 0.001 * st_1);
2036 }
2037 sh.fileCheck.alarm_last = time (NULL);
2038
2039 if (sig_urgent > 0)
2040 continue;
2041
2042 /*
2043 * flush mail queue
2044 */
2045#if defined(SH_WITH_MAIL)
2046 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
2047 (void) sh_mail_msg (NULL);
2048#endif
2049 }
2050
2051 if (sig_urgent > 0)
2052 continue;
2053
2054 /* execute modules
2055 */
2056 TPT((0, FIL__, __LINE__, _("msg=<Execute modules.>\n")))
2057 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
2058 {
2059 if (modList[modnum].initval == SH_MOD_ACTIVE &&
2060 0 != modList[modnum].mod_timer(tcurrent))
2061 if (0 != (status = modList[modnum].mod_check()))
2062 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_EXEC,
2063 _(modList[modnum].name), (long) status);
2064 }
2065
2066 /* 27.05.2002 avoid empty database
2067 * 22.10.2002 moved here b/o suid check initialization
2068 */
2069 if (sh.flag.checkSum == SH_CHECK_INIT)
2070 sh_hash_pushdata (NULL, NULL);
2071
2072 /* write out database
2073 */
2074 if (sh.flag.checkSum == SH_CHECK_CHECK &&
2075 sh.flag.update == S_TRUE &&
2076 check_done == 1)
2077 sh_hash_writeout ();
2078
2079 /* no-op unless MEM_LOG is defined in sh_mem.c
2080 */
2081#ifdef MEM_DEBUG
2082 sh_mem_dump ();
2083#endif
2084
2085 /* no loop if not daemon
2086 */
2087 if (sh.flag.isdaemon != S_TRUE && sh.flag.loop == S_FALSE)
2088 break;
2089 if (sig_urgent > 0)
2090 continue;
2091
2092 /* see whether its time to send mail
2093 */
2094#if defined(SH_WITH_MAIL)
2095 if (tcurrent - sh.mailTime.alarm_last >= sh.mailTime.alarm_interval)
2096 {
2097 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
2098 (void) sh_mail_msg (NULL);
2099 sh.mailTime.alarm_last = time (NULL);
2100 }
2101#endif
2102 if (sig_urgent > 0)
2103 continue;
2104
2105 /* log the timestamp
2106 */
2107 if ((int)(tcurrent - told) >= sh.looptime )
2108 {
2109 TPT((0, FIL__, __LINE__, _("msg=<Log the timestamp.>\n")))
2110 told = tcurrent;
2111#ifdef MEM_DEBUG
2112 sh_mem_check();
2113 sh_unix_count_mlock();
2114#else
2115 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_STAMP);
2116#endif
2117 }
2118
2119 /* seed / re-seed the PRNG if required
2120 */
2121 (void) taus_seed();
2122
2123 if (sig_urgent > 0)
2124 continue;
2125
2126 /* go to sleep
2127 */
2128 (void) retry_msleep (1, 0);
2129
2130 BREAKEXIT(sh_derr);
2131 (void) sh_derr();
2132 }
2133
2134 /* ------ END -----------
2135 */
2136
2137
2138
2139 /*
2140 * cleanup
2141 */
2142 TPT((0, FIL__, __LINE__, _("msg=<Cleanup.>\n")));
2143 sh_hash_hashdelete();
2144
2145#if defined(SH_WITH_MAIL)
2146 if (sh.mailNum.alarm_last > 0)
2147 (void)sh_mail_msg (NULL);
2148#endif
2149
2150 /* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
2151#endif
2152
2153 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
2154 SL_RETURN(0, _("main"));
2155}
Note: See TracBrowser for help on using the repository browser.