source: trunk/src/samhain.c@ 144

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

Fix some more queer compile problems.

File size: 48.4 KB
RevLine 
[1]1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 1999, 2000 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27#include <fcntl.h>
28
29/* samhainctl */
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <sys/wait.h>
33#include <signal.h>
34#include <errno.h>
35
36
37#if TIME_WITH_SYS_TIME
38#include <sys/time.h>
39#include <time.h>
40#else
41#if HAVE_SYS_TIME_H
42#include <sys/time.h>
43#else
44#include <time.h>
45#endif
46#endif
47
48#ifdef HAVE_MEMORY_H
49#include <memory.h>
50#endif
51
52#ifdef HAVE_SETPRIORITY
53#include <sys/resource.h>
54#endif
55
56#ifndef HAVE_LSTAT
57#define lstat stat
58#endif
59
60/* for FLT_EPSILON
61 */
62#include <float.h>
63
64#include "samhain.h"
[131]65#include "sh_pthread.h"
[1]66#include "sh_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 */
[143]116volatile int sh_thread_pause_flag = S_FALSE;
[1]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
[134]141#ifdef HAVE_PTHREAD
[137]142struct gt {
[134]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));
[137]152 if (!ptr)
[134]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
[137]169void sh_g_init()
[134]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 {
[137]179 perror("2");
[134]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
[1]189char * globber(const char * str)
190{
191 size_t i;
192 size_t j;
193
[137]194#ifndef HAVE_PTHREAD
[1]195 static size_t count = 0;
196 static char glob[SH_MAX_GLOBS * (GLOB_LEN+1)];
[137]197#else
198 struct gt * ptr = pthread_getspecific(g_key);
199 size_t count;
200 char * glob;
[1]201
[137]202 if (ptr) {
203 count = ptr->g_count;
204 glob = ptr->g_glob;
205 } else {
[1]206 return NULL;
[137]207 }
208#endif
209
210 if (str != NULL)
211 j = strlen(str);
[1]212 else
[137]213 return NULL;
[1]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;
[134]237#ifdef HAVE_PTHREAD
[137]238 ptr->g_count = count + j + 1;
[134]239#else
[1]240 count = count + j + 1;
[134]241#endif
[1]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
[137]258#else
259/* not stealth */
260#define SH_G_INIT ((void)0)
[1]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);
[22]450 sh.host.system[0] = '\0'; /* flawfinder: ignore *//* ff bug */
[1]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 {
[131]518#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
519 char * saveptr;
[1]520 (void) sl_strncpy(q, DEFAULT_MAILADDRESS, SH_PATHBUF);
[131]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);
[1]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 }
[131]537#endif
[1]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));
[96]625 if (skey != NULL)
[1]626 {
[96]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 {
[1]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 {
[142]707 if (modList[modnum].initval == SH_MOD_ACTIVE)
[1]708 (void) modList[modnum].mod_cleanup();
709 }
[142]710#ifdef HAVE_PTHREAD
711 sh_pthread_cancel_all();
[1]712#endif
[142]713#endif
[1]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();
[59]737 sh_files_hle_reg (NULL);
[1]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{
[144]801 volatile pid_t * pidlist = malloc(sizeof(pid_t) * 65535);
802 pid_t * dummy;
[1]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
[22]812 if (!pidlist)
813 return NULL;
814
[1]815 for (i = 0; i < 65535; ++i) pidlist[i] = 0;
816 i = 0;
817
818 if (0 != stat(SH_INSTALL_PATH, &buf))
[22]819 {
[144]820 dummy = pidlist;
821 free(dummy);
[22]822 return NULL;
823 }
[1]824
825 ino = (long) buf.st_ino;
826
827 if (NULL == (dp = opendir("/proc")))
[22]828 {
[144]829 dummy = pidlist;
830 free(dummy);
[22]831 return NULL;
832 }
[131]833
[137]834 SH_MUTEX_LOCK(mutex_readdir);
[131]835
[1]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 }
[131]856
[137]857 SH_MUTEX_UNLOCK(mutex_readdir);
[131]858
[1]859 closedir(dp);
860 return pidlist;
861}
862#else
863static pid_t * procdirSamhain ()
864{
865 return NULL;
866}
867#endif
868
869static int killprocSamhain (pid_t pid)
870{
871 int i;
872
873 /* fprintf(stderr, "Killing %d\n", pid); */
874 if (pid > 0 && 0 == kill (pid, SIGTERM))
875 {
876 for (i = 0; i < 16; ++i)
877 {
878 (void) retry_msleep(1, 0);
879 if (0 != kill (pid, 0) && errno == ESRCH)
880 return (0);
881 }
882
883 (void) kill (pid, SIGKILL);
884 return (0);
885 }
886 if (pid > 0)
887 {
888 if (errno == ESRCH)
889 return 7;
890 if (errno == EPERM)
891 return 4;
892 return 1;
893 }
894 else
895 return (7);
896}
897
898static pid_t pidofSamhain (int flag)
899{
900 FILE * fp;
901 char line[256];
902 char * tail;
903 char * p;
904 pid_t pid;
905 long inpid;
906 struct stat buf;
907
908 fp = fopen (DEFAULT_ERRLOCK, "r");
909
910 if (!fp)
911 { if (errno != ENOENT) perror(_("fopen")); return 0; }
[34]912 if (NULL == fgets(line, sizeof(line), fp))
[1]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
[22]964 fullpath = strdup (SH_INSTALL_PATH);
[1]965 if (fullpath == NULL)
[22]966 { perror(_("strdup")); exit (1); }
[1]967
[22]968 argp[0] = strdup (SH_INSTALL_PATH);
[1]969 if (argp[0] == NULL)
[22]970 { perror(_("strdup")); exit (1); }
[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 {
[22]978 argp[times-1] = strdup (argv[times]);
[1]979 if (argp[times-1] == NULL)
[22]980 { perror(_("strdup")); exit (1); }
[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 }
[22]1000 (void) execv(fullpath, argp); /* flawfinder: ignore *//* wtf? */
[1]1001 if (errno == EPERM)
1002 _exit(4);
1003 else if (errno == ENOENT)
1004 _exit(5);
1005 _exit (1);
1006 default:
1007 times = 0;
[92]1008 while (times < 300) {
[1]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 {
[96]1017#ifndef USE_UNO
[1]1018 if (0 != WIFEXITED(status))
1019 {
1020 res = WEXITSTATUS(status);
1021 exit (res == 0 ? 0 : res );
1022 }
1023 else
1024 exit (1);
[96]1025#else
1026 exit (1);
1027#endif
[1]1028 }
1029 ++times;
1030 (void) retry_msleep(1, 0);
1031 }
[79]1032 exit (0); /* assume that it runs ok */
[1]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 */
[22]1042 {
1043 free(fullpath);
1044 return (0);
1045 }
[1]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 }
[22]1060 free(fullpath);
[1]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 }
[22]1097 free(fullpath); /* silence smatch false positive */
[1]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 */
[22]1127static sh_schedule_t * sh_set_schedule_int (const char * str,
[1]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 */
[22]1157int sh_set_schedule_one (const char * str)
[1]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 */
[22]1166int sh_set_schedule_two (const char * str)
[1]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 *******************************************************/
[18]1180#if !defined(SH_CUTEST)
[1]1181int main(int argc, char * argv[])
[18]1182#else
1183int undef_main(int argc, char * argv[])
1184#endif
[1]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;
[144]1198 volatile long cct = 0; /* main loop iterations */
[1]1199
[144]1200 volatile int flag_check_1 = 0;
1201 volatile int flag_check_2 = 0;
[1]1202
1203 int check_done = 0;
1204#endif
1205
[144]1206 volatile time_t told;
[1]1207 time_t tcurrent;
1208 size_t tzlen;
1209 char * tzptr;
1210 int res;
1211
1212#if defined (SH_STEALTH_NOCL)
[34]1213 char command_line[256];
[1]1214 int my_argc = 0;
1215 char * my_argv[32];
1216#endif
1217
[134]1218 SH_G_INIT; /* Must precede any use of _() */
1219
[1]1220 SL_ENTER(_("main"));
1221
[92]1222 /* --- Close all but first three file descriptors. ---
1223 */
1224 sh_unix_closeall(3, -1); /* at program start */
1225
1226
[1]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 */
[22]1326 if (NULL != (tzptr = getenv("TZ"))) /* flawfinder: ignore */
[1]1327 {
1328 tzlen = strlen(tzptr);
[22]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;
[1]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)
[20]1379 sh_argc_store = argc;
1380 sh_argv_store = argv;
[1]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 {
[34]1386 if ( 0 == strcmp(argv[1], NOCL_CODE) )
[1]1387 {
[131]1388#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1389 char * saveptr;
1390#endif
[1]1391 my_argv[0] = argv[0]; ++my_argc;
1392 command_line[0] = '\0';
[34]1393 (void*) fgets (command_line, sizeof(command_line), stdin);
1394 command_line[sizeof(command_line)-1] = '\0';
[1]1395 do {
[131]1396#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
[1]1397 my_argv[my_argc] =
[131]1398 strtok_r( (my_argc == 1) ? command_line : NULL, " \n", &saveptr);
1399#else
1400 my_argv[my_argc] =
1401 strtok( (my_argc == 1) ? command_line : NULL, " \n");
1402#endif
[1]1403 if (my_argv[my_argc] != NULL) {
1404 ++my_argc;
1405 } else {
1406 break;
1407 }
1408 } while (my_argc < 32);
[20]1409
1410 sh_argc_store = my_argc;
1411 sh_argv_store = my_argv;
1412
[1]1413 (void) sh_getopt_get (my_argc, my_argv);
1414 }
1415 else
1416 {
1417 /* discard command line */
1418 /* _exit(EXIT_FAILURE) */ ;
1419 }
1420 }
1421#endif
1422 sh.flag.opts = S_FALSE;
1423
1424
1425 /* --- Get user info. ---
1426 */
1427 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1428 if (0 != sh_unix_getUser ())
1429 {
1430 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1431 sh.prg_name);
1432 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1433 }
1434
1435
1436 /* *****************************
1437 *
1438 * Read the configuration file.
1439 *
1440 * *****************************/
1441
1442 TPT((0, FIL__, __LINE__, _("msg=<Read the configuration file.>\n")))
1443 BREAKEXIT(sh_readconf_read);
1444 (void) sh_readconf_read ();
1445
1446#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1447 if (sh.flag.checkSum == SH_CHECK_NONE)
1448 {
1449 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
1450 _("No action specified: init, update, or check"),
1451 _("main"));
1452 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1453 sh.prg_name);
1454 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
1455 }
1456#endif
1457
1458 /* do not append to database if run SUID
1459 */
1460 if ((sh.flag.checkSum == SH_CHECK_INIT) && (0 != sl_is_suid()))
1461 {
1462 (void) dlog(1, FIL__, __LINE__,
1463 _("Cannot initialize database when running with SUID credentials.\nYou need to run this with the user ID %d.\nYour current user ID is %d."),
1464 (int) geteuid(), (int) sh.real.uid);
1465 sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_ACCESS,
1466 (long) sh.real.uid, sh.data.path);
1467 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1468 }
1469
1470 /* avoid daemon mode for initialization
1471 */
1472 if (sh.flag.checkSum == SH_CHECK_INIT)
1473 {
1474 sh.flag.isdaemon = S_FALSE;
1475 sh.flag.loop = S_FALSE;
1476 }
1477
[59]1478 /* --- load database; checksum of database
1479 */
1480#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1481 TPT((0, FIL__, __LINE__, _("msg=<Get checksum of the database.>\n")))
1482 if (sh.flag.checkSum == SH_CHECK_CHECK)
1483 {
1484 if (0 != sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
1485 {
[133]1486 char hashbuf[KEYBUF_SIZE];
[59]1487 (void) sl_strlcpy(sh.data.hash,
1488 sh_tiger_hash (file_path('D', 'R'),
[133]1489 TIGER_FILE, 0,
1490 hashbuf, sizeof(hashbuf)),
[59]1491 KEY_LEN+1);
1492 }
1493
1494 /* this eventually fetches the file from server to get checksum
1495 */
1496 sh_hash_init ();
1497 }
1498#endif
1499
1500 /* --- initialize signal handling etc.; fork daemon
1501 */
[1]1502 if (sh_unix_init(sh.flag.isdaemon) == -1)
1503 {
1504 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1505 sh.prg_name);
1506 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1507 }
1508
1509 /* --- drop privileges eventually ---
1510 */
1511#if defined(SH_WITH_SERVER)
1512 sh_create_tcp_socket ();
1513#if defined(INET_SYSLOG)
1514 create_syslog_socket (S_TRUE);
1515#endif
1516 SL_REQUIRE(sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE,
1517 _("sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE"));
1518#else
1519 SL_REQUIRE(sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE,
1520 _("sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE"));
1521#endif
1522
1523 /* --- Get user info (again). ---
1524 */
1525 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1526 if (0 != sh_unix_getUser ())
1527 {
1528 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1529 sh.prg_name);
1530 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1531 }
1532
1533 /* --- now check whether we really wanted it; if not, close ---
1534 */
1535#if defined(INET_SYSLOG) && defined(SH_WITH_SERVER)
1536 create_syslog_socket (S_FALSE);
1537#endif
1538
1539
1540 /* --- Enable full error logging ---
1541 */
1542 sh_error_only_stderr (S_FALSE);
1543
1544 /****************************************************
1545 *
1546 * SERVER
1547 *
1548 ****************************************************/
1549
1550#if defined(SH_WITH_SERVER) && !defined(SH_WITH_CLIENT)
1551
1552#if (defined(WITH_GPG) || defined(WITH_PGP))
[86]1553 /* log startup */
1554 sh_gpg_log_startup ();
[1]1555#else
1556 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1557 sh.prg_name, (long) sh.real.uid,
1558 (sh.flag.hidefile == S_TRUE) ?
1559 _("(hidden)") : file_path('C','R'),
1560 sh.conf.hash);
1561#endif
1562
1563#else
1564
1565 /****************************************************
1566 *
1567 * CLIENT/STANDALONE
1568 *
1569 ****************************************************/
1570
1571 BREAKEXIT(sh_error_handle);
1572
1573 if (sh.flag.checkSum == SH_CHECK_CHECK)
1574 {
1575#if (defined(WITH_GPG) || defined(WITH_PGP))
[86]1576 /* log startup */
1577 sh_gpg_log_startup ();
[1]1578#else
1579 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_2H,
1580 sh.prg_name, (long) sh.real.uid,
1581 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash,
1582 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('D', 'R'), sh.data.hash);
1583#endif
1584 }
1585 else
1586 {
1587#if (defined(WITH_GPG) || defined(WITH_PGP))
[86]1588 /* log startup */
1589 sh_gpg_log_startup ();
[1]1590#else
1591 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1592 sh.prg_name, (long) sh.real.uid,
1593 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash);
1594#endif
1595 }
1596#endif
1597
1598
1599 if ((skey == NULL) || (skey->mlock_failed == SL_TRUE))
1600 sh_error_handle ((-1), FIL__, __LINE__, EPERM, MSG_MLOCK);
1601
1602 /* timer
1603 */
1604 tcurrent = time (NULL);
1605 told = tcurrent;
1606 sh.mailTime.alarm_last = told;
1607
1608
1609 /****************************************************
1610 *
1611 * SERVER
1612 *
1613 ****************************************************/
1614
1615#if defined(SH_WITH_SERVER)
1616 TPT((0, FIL__, __LINE__, _("msg=<Start server.>\n")))
1617
1618#if defined (SH_WITH_CLIENT)
1619 if (sh.flag.isserver == S_TRUE)
1620 {
1621 sh_receive();
1622 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1623 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1624 }
1625#else
1626 sh_receive();
1627 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1628 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1629#endif
1630
1631#endif
1632
1633 /****************************************************
1634 *
1635 * CLIENT/STANDALONE
1636 *
1637 ****************************************************/
1638#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1639
1640
1641 /* --- Initialize modules. ---
1642 */
1643 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")))
1644 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1645 {
[142]1646 status = modList[modnum].mod_init(&(modList[modnum]));
1647 if ( status < 0 )
[1]1648 {
1649 if (status == (-1)) {
1650 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, status,
1651 MSG_MOD_FAIL,
1652 _(modList[modnum].name),
1653 status);
1654 } else {
1655 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_FAIL,
1656 _(modList[modnum].name),
1657 status);
1658 }
[142]1659 modList[modnum].initval = SH_MOD_FAILED;
[1]1660 }
1661 else
1662 {
1663 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1664 _(modList[modnum].name));
[142]1665 modList[modnum].initval = status;
[1]1666 }
1667 }
1668
1669 /* -------- TEST SETUP ---------
1670 */
1671 (void) sh_files_setrec();
1672 (void) sh_files_test_setup();
1673
1674
1675 /* -------- NICE LEVEL ---------
1676 */
1677 if (0 != sh.flag.nice)
1678 {
1679#ifdef HAVE_SETPRIORITY
1680 /*@-unrecog@*/
1681 (void) setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1682 /*@+unrecog@*/
1683#else
1684 (void) nice(sh.flag.nice);
1685#endif
1686 }
1687
1688 /* -------- MAIN LOOP ---------
1689 */
1690 sh.statistics.bytes_speed = 0;
1691 sh.statistics.bytes_hashed = 0;
1692
1693 while (1 == 1)
1694 {
1695 ++cct;
1696
1697 BREAKEXIT(sh_error_handle);
1698
1699 TPT((0, FIL__, __LINE__, _("msg=<Start main loop.>, iter=<%ld>\n"), cct))
1700
1701 tcurrent = time (NULL);
1702
1703 if (sig_raised > 0)
1704 {
1705
1706 TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
1707
1708 if (sig_termfast == 1) /* SIGTERM */
1709 {
1710 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1711 /* strncpy (sh_sig_msg, _("SIGTERM"), 20); */
1712 --sig_raised; --sig_urgent;
1713 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1714 }
1715
1716 if (sig_force_check == 1) /* SIGTTOU */
1717 {
1718 TPT((0, FIL__, __LINE__, _("msg=<Check run triggered.>\n")));
1719 flag_check_1 = 1;
1720 flag_check_2 = 1;
1721 sig_force_check = 0;
1722 --sig_raised;
1723 }
1724
1725 if (sig_config_read_again == 1) /* SIGHUP */
1726 {
1727 TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")))
1728 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
1729
[143]1730 sh_thread_pause_flag = S_TRUE;
1731
[1]1732#if defined(WITH_EXTERNAL)
1733 /* delete list of external tasks
1734 */
1735 (void) sh_ext_cleanup();
1736#endif
1737 /* delete the file list, make all database
1738 * entries visible (allignore = FALSE)
1739 */
1740 (void) sh_files_deldirstack ();
1741 (void) sh_files_delfilestack ();
1742 (void) sh_ignore_clean ();
1743 (void) hash_full_tree ();
1744
1745#if defined(SH_WITH_CLIENT)
1746 reset_count_dev_server();
1747#endif
1748#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
1749
1750
1751 FileSchedOne = free_sched(FileSchedOne);
1752 FileSchedTwo = free_sched(FileSchedTwo);
1753
1754 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1755 {
[142]1756 if (modList[modnum].initval >= SH_MOD_ACTIVE)
[1]1757 (void) modList[modnum].mod_reconf();
1758 }
1759#endif
1760
1761#if defined(SH_WITH_MAIL)
1762 reset_count_dev_mail();
1763#endif
1764 reset_count_dev_console();
1765 reset_count_dev_time();
1766
1767 (void) sh_unix_maskreset();
1768
1769 /* Should this be included ???
1770 * (i.e. should we reload the database ?)
1771 */
1772#ifdef RELOAD_DATABASE
1773 sh_hash_hashdelete();
1774#endif
1775 (void) sl_trust_purge_user();
1776 (void) sh_files_hle_reg (NULL);
1777 (void) sh_prelink_run (NULL, NULL, 0);
1778
1779 /* --------------------------
1780 * --- READ CONFIGURATION ---
1781 * --------------------------
1782 */
1783 (void) sh_readconf_read ();
1784 sig_config_read_again = 0;
1785 (void) sh_files_setrec();
1786 (void) sh_files_test_setup();
1787 if (0 != sh.flag.nice)
1788 {
1789#ifdef HAVE_SETPRIORITY
1790 setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1791#else
1792 nice(sh.flag.nice);
1793#endif
1794 }
1795
1796 if (sh.flag.checkSum == SH_CHECK_INIT)
1797 {
1798 sh.flag.isdaemon = S_FALSE;
1799 sh.flag.loop = S_FALSE;
1800 }
1801
[143]1802 sh_thread_pause_flag = S_FALSE;
1803
[1]1804 /* --- Initialize modules. ---
1805 */
1806 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")));
1807 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1808 {
[142]1809 status = modList[modnum].mod_init(&(modList[modnum]));
1810
1811 if (status < 0)
[1]1812 {
1813 if (status == (-1)) {
1814 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__,
1815 status, MSG_MOD_FAIL,
1816 _(modList[modnum].name),
1817 status);
1818 } else {
1819 sh_error_handle ((-1), FIL__, __LINE__,
1820 status, MSG_MOD_FAIL,
1821 _(modList[modnum].name),
1822 status);
1823 }
[142]1824 modList[modnum].initval = SH_MOD_FAILED;
[1]1825 }
1826 else
1827 {
1828 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1829 _(modList[modnum].name));
[142]1830 modList[modnum].initval = status;
[1]1831 }
1832 }
1833
1834 --sig_raised;
1835 }
1836
1837 if (sig_fresh_trail == 1) /* SIGIOT */
1838 {
[143]1839 if (sh_global_suspend_flag == 0)
1840 {
1841 SH_MUTEX_LOCK(mutex_thread_nolog);
1842
1843 /* Logfile access
1844 */
[1]1845#ifdef SH_USE_XML
[143]1846 (void) sh_log_file (NULL, NULL);
[1]1847#endif
[143]1848 TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
1849 sh_error_only_stderr (S_TRUE);
1850 (void) sh_unix_rm_lock_file(sh.srvlog.name);
1851 (void) retry_msleep(3, 0);
1852 sh.flag.log_start = S_TRUE;
1853 sh_error_only_stderr (S_FALSE);
1854 sh_thread_pause_flag = S_FALSE;
1855 sig_fresh_trail = 0;
1856 --sig_raised;
1857 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1858 }
[1]1859 }
1860
1861 if (sig_terminate == 1) /* SIGQUIT */
1862 {
1863 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1864 strncpy (sh_sig_msg, _("Quit"), 20);
1865 --sig_raised; --sig_urgent;
1866 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1867 }
1868
1869 if (sig_debug_switch == 1) /* SIGUSR1 */
1870 {
1871 TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
1872 sh_error_dbg_switch();
1873 sig_debug_switch = 0;
1874 --sig_raised;
1875 }
1876
[19]1877 if (sig_suspend_switch > 0) /* SIGUSR2 */
[1]1878 {
1879 TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
[143]1880 if (sh_global_suspend_flag != 1) {
1881 SH_MUTEX_LOCK_UNSAFE(mutex_thread_nolog);
1882 sh_global_suspend_flag = 1;
[1]1883 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND,
1884 sh.prg_name);
[143]1885 } else {
1886 sh_global_suspend_flag = 0;
1887 SH_MUTEX_UNLOCK_UNSAFE(mutex_thread_nolog);
[1]1888 }
[19]1889 --sig_suspend_switch;
[1]1890 --sig_raised; --sig_urgent;
1891 }
1892 sig_raised = (sig_raised < 0) ? 0 : sig_raised;
1893 sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
1894 TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
1895 }
1896
1897 if (sh_global_suspend_flag == 1)
1898 {
1899 (void) retry_msleep (1, 0);
1900 continue;
1901 }
1902
1903 /* see whether its time to check files
1904 */
[25]1905 if (sh.flag.checkSum == SH_CHECK_INIT ||
1906 (sh.flag.checkSum == SH_CHECK_CHECK &&
1907 (sh.flag.isdaemon == S_FALSE && sh.flag.loop == S_FALSE)))
[1]1908 {
1909 flag_check_1 = 1;
1910 if (FileSchedTwo != NULL)
1911 flag_check_2 = 1;
1912 }
1913 else if (sh.flag.checkSum == SH_CHECK_CHECK ||
1914 (sh.flag.update == S_TRUE &&
1915 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE)))
1916 {
1917 if (FileSchedOne == NULL)
1918 {
1919 /* use interval if we have no schedule
1920 */
1921 if (tcurrent - sh.fileCheck.alarm_last >=
1922 sh.fileCheck.alarm_interval)
1923 flag_check_1 = 1;
1924 }
1925 else
1926 {
1927 flag_check_1 = test_sched(FileSchedOne);
1928 if (FileSchedTwo != NULL)
1929 flag_check_2 = test_sched(FileSchedTwo);
1930 if (flag_check_2 == 1)
1931 flag_check_1 = 1;
1932 }
1933 }
1934
1935 check_done = 0;
1936
1937 if (sh.flag.checkSum != SH_CHECK_NONE &&
1938 (flag_check_1 == 1 || flag_check_2 == 1))
1939 {
1940 /*
1941 * check directories and files
1942 * ORDER IS IMPORTANT -- DIRZ FIRST
1943 */
1944 sh.statistics.bytes_hashed = 0;
1945 sh.statistics.time_start = time (NULL);
1946 sh.statistics.dirs_checked = 0;
1947 sh.statistics.files_checked = 0;
1948
1949 TPT((0, FIL__, __LINE__, _("msg=<Check directories.>\n")))
1950 BREAKEXIT(sh_dirs_chk);
1951 if (flag_check_1 == 1)
1952 {
1953 (void) sh_dirs_chk (1);
1954#ifndef SH_PROFILE
1955 (void) chdir ("/");
1956#endif
1957 }
1958 if (flag_check_2 == 1)
1959 {
1960 (void) sh_dirs_chk (2);
1961#ifndef SH_PROFILE
1962 (void) chdir ("/");
1963#endif
1964 }
1965 TPT((0, FIL__, __LINE__, _("msg=<Check files.>\n")))
1966 BREAKEXIT(sh_files_chk);
1967 if (flag_check_1 == 1)
1968 (void) sh_files_chk ();
1969
1970 if (sig_urgent > 0)
1971 continue;
1972
1973 /*
1974 * check for files not visited
1975 */
1976 if (flag_check_2 == 1 || FileSchedTwo == NULL)
1977 {
1978 TPT((0, FIL__, __LINE__, _("msg=<Check for missing files.>\n")))
1979 sh_hash_unvisited (ShDFLevel[SH_ERR_T_FILE]);
1980 }
1981
1982 if (sig_urgent > 0)
1983 continue;
1984
1985 /* reset
1986 */
1987 TPT((0, FIL__, __LINE__, _("msg=<Reset status.>\n")))
1988 sh_dirs_reset ();
1989 if (sig_urgent > 0)
1990 continue;
1991
1992 sh_files_reset ();
1993 flag_check_1 = 0;
1994 flag_check_2 = 0;
1995 check_done = 1;
1996
1997 (void) sh_prelink_run (NULL, NULL, 0);
1998
1999 if (sig_urgent > 0)
2000 continue;
2001
2002 runtim = time(NULL) - sh.statistics.time_start;
2003 sh.statistics.time_check = runtim;
2004
2005 if ((sh.statistics.dirs_checked == 0) &&
2006 (sh.statistics.files_checked == 0))
2007 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_0);
2008
2009 else
2010 {
2011 st_1 = (float) sh.statistics.bytes_hashed;
2012 st_2 = (float) runtim;
2013
2014
2015 if (st_1 > FLT_EPSILON && st_2 > FLT_EPSILON)
2016 st_1 = st_1/st_2;
2017 else if (st_1 > FLT_EPSILON)
2018 st_1 = (float) (st_1 * 1.0);
2019 else
2020 st_1 = 0.0;
2021
2022 sh.statistics.bytes_speed = (unsigned long) st_1;
2023
2024 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_1,
2025 (long) runtim,
2026 0.001 * st_1);
2027 }
2028 sh.fileCheck.alarm_last = time (NULL);
2029
2030 if (sig_urgent > 0)
2031 continue;
2032
2033 /*
2034 * flush mail queue
2035 */
2036#if defined(SH_WITH_MAIL)
2037 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
2038 (void) sh_mail_msg (NULL);
2039#endif
2040 }
2041
2042 if (sig_urgent > 0)
2043 continue;
2044
2045 /* execute modules
2046 */
2047 TPT((0, FIL__, __LINE__, _("msg=<Execute modules.>\n")))
2048 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
2049 {
[142]2050 if (modList[modnum].initval == SH_MOD_ACTIVE &&
[1]2051 0 != modList[modnum].mod_timer(tcurrent))
2052 if (0 != (status = modList[modnum].mod_check()))
2053 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_EXEC,
2054 _(modList[modnum].name), (long) status);
2055 }
2056
2057 /* 27.05.2002 avoid empty database
2058 * 22.10.2002 moved here b/o suid check initialization
2059 */
2060 if (sh.flag.checkSum == SH_CHECK_INIT)
2061 sh_hash_pushdata (NULL, NULL);
2062
2063 /* write out database
2064 */
2065 if (sh.flag.checkSum == SH_CHECK_CHECK &&
2066 sh.flag.update == S_TRUE &&
2067 check_done == 1)
2068 sh_hash_writeout ();
2069
2070 /* no loop if not daemon
2071 */
2072 if (sh.flag.isdaemon != S_TRUE && sh.flag.loop == S_FALSE)
2073 break;
2074 if (sig_urgent > 0)
2075 continue;
2076
2077 /* see whether its time to send mail
2078 */
2079#if defined(SH_WITH_MAIL)
2080 if (tcurrent - sh.mailTime.alarm_last >= sh.mailTime.alarm_interval)
2081 {
2082 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
2083 (void) sh_mail_msg (NULL);
2084 sh.mailTime.alarm_last = time (NULL);
2085 }
2086#endif
2087 if (sig_urgent > 0)
2088 continue;
2089
2090 /* log the timestamp
2091 */
2092 if ((int)(tcurrent - told) >= sh.looptime )
2093 {
2094 TPT((0, FIL__, __LINE__, _("msg=<Log the timestamp.>\n")))
2095 told = tcurrent;
2096#ifdef MEM_DEBUG
2097 sh_mem_check();
2098 sh_unix_count_mlock();
2099#else
2100 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_STAMP);
2101#endif
2102 }
2103
2104 /* seed / re-seed the PRNG if required
2105 */
2106 (void) taus_seed();
2107
2108 if (sig_urgent > 0)
2109 continue;
2110
2111 /* go to sleep
2112 */
2113 (void) retry_msleep (1, 0);
2114
2115 BREAKEXIT(sh_derr);
2116 (void) sh_derr();
2117 }
2118
2119 /* ------ END -----------
2120 */
2121
2122
2123
2124 /*
2125 * cleanup
2126 */
2127 TPT((0, FIL__, __LINE__, _("msg=<Cleanup.>\n")));
2128 sh_hash_hashdelete();
2129
2130#if defined(SH_WITH_MAIL)
2131 if (sh.mailNum.alarm_last > 0)
2132 (void)sh_mail_msg (NULL);
2133#endif
2134
2135 /* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
2136#endif
2137
2138 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
2139 SL_RETURN(0, _("main"));
2140}
Note: See TracBrowser for help on using the repository browser.