source: trunk/src/samhain.c@ 274

Last change on this file since 274 was 265, checked in by katerina, 15 years ago

Enhance logfile monitoring (tickets #183, #184, #185).

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