source: trunk/src/samhain.c@ 263

Last change on this file since 263 was 256, checked in by katerina, 15 years ago

Evaluated glob patterns at each check (ticket #173).

File size: 50.2 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_mem_stat();
756#endif
757
758#ifdef MEM_DEBUG
759 sh_unix_count_mlock();
760#endif
761
762 /* --- Checksum of executable. ---
763 */
764 (void) sh_unix_self_check();
765
766
767 /* --- Exit Message. ---
768 */
769 sh_error_handle ((-1), FIL__, __LINE__, sh.flag.exit, MSG_EXIT_NORMAL,
770 sh.prg_name, sh_sig_msg);
771#ifdef SH_USE_XML
772 (void) sh_log_file (NULL, NULL);
773#endif
774
775
776 /* --- Restrict error logging to stderr. ---
777 */
778#ifdef WITH_MESSAGE_QUEUE
779 close_ipc ();
780#endif
781 sh_error_only_stderr (S_TRUE);
782
783
784 /* --- Remove lock, delete critical information. ---
785 */
786 (void) sh_unix_rm_lock_file (sh.srvlog.name);
787 if (sh.flag.isdaemon == S_TRUE)
788 (void) sh_unix_rm_pid_file ();
789 if (skey != NULL)
790 memset (skey, (int) '\0', sizeof(sh_key_t));
791
792 /* --- Exit. ---
793 */
794 SL_RET0(_("exit_handler"));
795}
796
797/***********************************************************
798 *
799 */
800#ifndef SIGHUP
801#define SIGHUP 1
802#endif
803#ifndef SIGTERM
804#define SIGTERM 15
805#endif
806#ifndef SIGKILL
807#define SIGKILL 9
808#endif
809
810#if defined(__linux__) || defined(sun) || defined(__sun) || defined(__sun__)
811#include <dirent.h>
812static pid_t * procdirSamhain (void)
813{
814 pid_t * pidlist;
815 struct dirent * d;
816 DIR * dp;
817 long ino;
818 struct stat buf;
819 int i;
820 pid_t pid, mypid = getpid();
821 char * tail;
822 char exef[128];
823
824 if (0 != stat(SH_INSTALL_PATH, &buf))
825 {
826 return NULL;
827 }
828
829 ino = (long) buf.st_ino;
830
831 if (NULL == (dp = opendir(_("/proc"))))
832 {
833 return NULL;
834 }
835
836 SH_MUTEX_LOCK(mutex_readdir);
837
838 pidlist = malloc(sizeof(pid_t) * 65535);
839 if (!pidlist)
840 goto unlock_and_out;
841
842 for (i = 0; i < 65535; ++i) pidlist[i] = 0;
843
844 i = 0;
845 while (NULL != (d = readdir(dp)) && i < 65535)
846 {
847 if (0 != strcmp(d->d_name, ".") && 0 != strcmp(d->d_name, ".."))
848 {
849 errno = 0;
850 pid = (pid_t) strtol (d->d_name, &tail, 0);
851 if (*tail != '\0' || errno != 0)
852 continue;
853 if (pid == mypid)
854 continue;
855#if defined(__linux__)
856 sprintf(exef, _("/proc/%d/exe"), (int) pid); /* known to fit */
857#else
858 sprintf(exef, _("/proc/%d/object/a.out"), /* known to fit */
859 (int) pid);
860#endif
861 if (0 == stat(exef, &buf) && ino == (long) buf.st_ino)
862 { pidlist[i] = (pid_t) pid; ++i; }
863 }
864 }
865
866 unlock_and_out:
867 ;
868 SH_MUTEX_UNLOCK(mutex_readdir);
869
870 closedir(dp);
871 return pidlist;
872}
873#else
874static pid_t * procdirSamhain (void)
875{
876 return NULL;
877}
878#endif
879
880static int killprocSamhain (pid_t pid)
881{
882 int i;
883
884 /* fprintf(stderr, "Killing %d\n", pid); */
885 if (pid > 0 && 0 == kill (pid, SIGTERM))
886 {
887 for (i = 0; i < 16; ++i)
888 {
889 (void) retry_msleep(1, 0);
890 if (0 != kill (pid, 0) && errno == ESRCH)
891 return (0);
892 }
893
894 (void) kill (pid, SIGKILL);
895 return (0);
896 }
897 if (pid > 0)
898 {
899 if (errno == ESRCH)
900 return 7;
901 if (errno == EPERM)
902 return 4;
903 return 1;
904 }
905 else
906 return (7);
907}
908
909static pid_t pidofSamhain (int flag)
910{
911 FILE * fp;
912 char line[256];
913 char * tail;
914 char * p;
915 pid_t pid;
916 long inpid;
917 struct stat buf;
918
919 fp = fopen (DEFAULT_ERRLOCK, "r");
920
921 if (!fp)
922 { if (errno != ENOENT) perror(_("fopen")); return 0; }
923 if (NULL == fgets(line, sizeof(line), fp))
924 { perror(_("fgets")); (void) sl_fclose(FIL__, __LINE__, fp); return 0; }
925 (void) sl_fclose(FIL__, __LINE__, fp);
926 p = line;
927 while (*p == ' ' || *p == '\f' || *p == '\n' ||
928 *p == '\r' || *p == '\t' || *p == '\v')
929 ++p;
930 errno = 0;
931 inpid = strtol (p, &tail, 0);
932 if (p == tail || errno != 0)
933 { perror(_("strtol")); return 0; }
934
935 pid = (pid_t) inpid;
936 if (inpid != (long) pid)
937 { perror(_("strtol")); return 0; }
938
939 /* remove stale pid file
940 */
941 if (flag == 1 && pid > 0 && 0 != kill(pid, 0) && errno == ESRCH)
942 {
943 if /*@-unrecog@*/ (0 == lstat (DEFAULT_ERRLOCK, &buf))/*@+unrecog@*/
944 {
945 if /*@-usedef@*/(S_ISREG(buf.st_mode))/*@+usedef@*/
946 {
947 (void) unlink(DEFAULT_ERRLOCK);
948 }
949 }
950 else
951 {
952 perror(_("lstat")); return 0;
953 }
954 pid = 0;
955 }
956 return pid;
957}
958
959/* 1: start 2:stop 3:reload 4:status
960 */
961/*@-exitarg@*/
962static int samhainctl(int ctl, int * argc, char * argv[])
963{
964 char * fullpath;
965 pid_t pid;
966 int status;
967 int res;
968 pid_t respid;
969 int times;
970 char * argp[32];
971 pid_t * pidlist;
972 int i;
973
974
975 fullpath = strdup (SH_INSTALL_PATH);
976 if (fullpath == NULL)
977 { perror(_("strdup")); exit (1); }
978
979 argp[0] = strdup (SH_INSTALL_PATH);
980 if (argp[0] == NULL)
981 { perror(_("strdup")); exit (1); }
982
983 for (times = 1; times < 32; ++times) argp[times] = NULL;
984
985 res = (*argc > 32) ? 32 : *argc;
986
987 for (times = 2; times < res; ++times)
988 {
989 argp[times-1] = strdup (argv[times]);
990 if (argp[times-1] == NULL)
991 { perror(_("strdup")); exit (1); }
992 }
993
994 if (ctl == 1)
995 {
996 pid = pidofSamhain(1);
997
998 if (pid != 0 && 0 == kill (pid, 0)) /* already started */
999 exit (0);
1000
1001 pid = fork();
1002 switch (pid) {
1003 case ((pid_t) -1):
1004 perror(_("fork"));
1005 exit (1);
1006 case 0:
1007 if (0 != sl_close_fd (FIL__, __LINE__, 0))
1008 {
1009 _exit(4);
1010 }
1011 (void) execv(fullpath, argp); /* flawfinder: ignore *//* wtf? */
1012 if (errno == EPERM)
1013 _exit(4);
1014 else if (errno == ENOENT)
1015 _exit(5);
1016 _exit (1);
1017 default:
1018 times = 0;
1019 while (times < 300) {
1020 respid = waitpid(pid, &status, WNOHANG|WUNTRACED);
1021 if ((pid_t)-1 == respid)
1022 {
1023 perror(_("waitpid"));
1024 exit (1);
1025 }
1026 else if (pid == respid)
1027 {
1028#ifndef USE_UNO
1029 if (0 != WIFEXITED(status))
1030 {
1031 res = WEXITSTATUS(status);
1032 exit (res == 0 ? 0 : res );
1033 }
1034 else
1035 exit (1);
1036#else
1037 exit (1);
1038#endif
1039 }
1040 ++times;
1041 (void) retry_msleep(1, 0);
1042 }
1043 exit (0); /* assume that it runs ok */
1044 }
1045 }
1046
1047 pid = pidofSamhain(0);
1048
1049 if (ctl == 2) /* stop */
1050 {
1051 pidlist = procdirSamhain ();
1052 if (pid == 0 && NULL == pidlist) /* pid file not found */
1053 {
1054 free(fullpath);
1055 return (0);
1056 }
1057
1058 status = 0;
1059 if (pid != 0)
1060 status = killprocSamhain(pid);
1061 if (pidlist != NULL)
1062 {
1063 i = 0;
1064 while (i < 65535 && pidlist[i] != 0)
1065 {
1066 if (pidlist[i] != pid)
1067 status = killprocSamhain(pidlist[i]);
1068 ++i;
1069 }
1070 }
1071 free(fullpath);
1072 if (status == 7)
1073 return 0;
1074 else
1075 return status;
1076 }
1077
1078 if (ctl == 3) /* reload */
1079 {
1080 if (pid == 0)
1081 exit (7);
1082 if (0 == kill (pid, SIGHUP))
1083 exit (0);
1084 else
1085 {
1086 if (errno == EPERM)
1087 exit (4);
1088 if (errno == ESRCH)
1089 exit (7);
1090 exit (1);
1091 }
1092 }
1093
1094 if (ctl == 4) /* status */
1095 {
1096 if (pid == 0)
1097 exit (3);
1098 if (0 == kill (pid, 0))
1099 exit (0);
1100 else
1101 {
1102 if (errno == EPERM)
1103 exit (4);
1104 if (errno == ESRCH)
1105 exit (1);
1106 }
1107 }
1108 free(fullpath); /* silence smatch false positive */
1109 exit (1); /* no exit handler installed yet */
1110 /*@notreached@*/
1111 return (0);
1112}
1113/*@+exitarg@*/
1114
1115#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1116#include "sh_schedule.h"
1117static sh_schedule_t * FileSchedOne = NULL;
1118static sh_schedule_t * FileSchedTwo = NULL;
1119
1120/* free a linked list of schedules
1121 */
1122static sh_schedule_t * free_sched (sh_schedule_t * isched)
1123{
1124 sh_schedule_t * current = isched;
1125 sh_schedule_t * next = NULL;
1126
1127 while (current != NULL)
1128 {
1129 next = current->next;
1130 SH_FREE(current);
1131 current = next;
1132 }
1133 return NULL;
1134}
1135
1136/* Add a new schedule to the linked list of schedules
1137 */
1138static sh_schedule_t * sh_set_schedule_int (const char * str,
1139 sh_schedule_t * FileSchedIn,
1140 /*@out@*/ int * status)
1141{
1142 sh_schedule_t * FileSched;
1143
1144 SL_ENTER(_("sh_set_schedule_int"));
1145
1146 if (0 == sl_strncmp(str, _("NULL"), 4))
1147 {
1148 (void) free_sched(FileSchedIn);
1149 FileSchedIn = NULL;
1150 *status = 0;
1151 return 0;
1152 }
1153
1154 FileSched = SH_ALLOC(sizeof(sh_schedule_t));
1155 *status = create_sched(str, FileSched);
1156 if (*status != 0)
1157 {
1158 SH_FREE(FileSched);
1159 FileSched = NULL;
1160 SL_RETURN(FileSchedIn , _("sh_set_schedule_int"));
1161 }
1162 FileSched->next = FileSchedIn;
1163 SL_RETURN(FileSched , _("sh_set_schedule_int"));
1164}
1165
1166/* Add a new schedule to the linked list FileSchedOne
1167 */
1168int sh_set_schedule_one (const char * str)
1169{
1170 int status;
1171 FileSchedOne = sh_set_schedule_int (str, FileSchedOne, &status);
1172 return status;
1173}
1174
1175/* Add a new schedule to the linked list FileSchedTwo
1176 */
1177int sh_set_schedule_two (const char * str)
1178{
1179 int status;
1180 FileSchedTwo = sh_set_schedule_int (str, FileSchedTwo, &status);
1181 return status;
1182}
1183
1184#endif
1185
1186/*******************************************************
1187 *
1188 * Main program
1189 *
1190 *******************************************************/
1191#if !defined(SH_CUTEST)
1192int main(int argc, char * argv[])
1193#else
1194int undef_main(int argc, char * argv[])
1195#endif
1196{
1197#if defined(INET_SYSLOG)
1198 extern int create_syslog_socket (int flag);
1199#endif
1200#if defined(SH_WITH_SERVER)
1201 extern int sh_create_tcp_socket(void);
1202#endif
1203
1204#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1205 int modnum;
1206 time_t runtim;
1207 float st_1, st_2;
1208 int status;
1209 volatile long cct = 0; /* main loop iterations */
1210
1211 volatile int flag_check_1 = 0;
1212 volatile int flag_check_2 = 0;
1213
1214 int check_done = 0;
1215#endif
1216
1217 volatile time_t told;
1218 volatile time_t tcurrent;
1219 size_t tzlen;
1220 char * tzptr;
1221 int res;
1222
1223#if defined (SH_STEALTH_NOCL)
1224 char command_line[256];
1225 int my_argc = 0;
1226 char * my_argv[32];
1227#endif
1228
1229#if !defined(USE_SYSTEM_MALLOC)
1230 typedef void assert_handler_tp(const char * error, const char *file, int line);
1231 extern assert_handler_tp *dnmalloc_set_handler(assert_handler_tp *new);
1232 (void) dnmalloc_set_handler(safe_fatal);
1233#endif
1234
1235 SH_G_INIT; /* Must precede any use of _() */
1236
1237 SL_ENTER(_("main"));
1238
1239 /* --- Close all but first three file descriptors. ---
1240 */
1241 sh_unix_closeall(3, -1, SL_FALSE); /* at program start */
1242
1243
1244 if (argc >= 2 && 0 != getuid() &&
1245 (0 == strcmp(argv[1], _("start")) ||
1246 0 == strcmp(argv[1], _("stop")) ||
1247 0 == strcmp(argv[1], _("reload")) ||
1248 0 == strcmp(argv[1], _("force-reload")) ||
1249 0 == strcmp(argv[1], _("status")) ||
1250 0 == strcmp(argv[1], _("restart"))))
1251 {
1252 return 4;
1253 }
1254
1255 if (argc >= 2 && 0 == getuid())
1256 {
1257 /* return codes:
1258 * 0 Success
1259 * 1 Can not send signal / start program
1260 * 2 Pid file does not exist
1261 */
1262 if (0 == strcmp(argv[1], _("start")))
1263 {
1264 (void) samhainctl (1, &argc, argv); /* does not return */
1265 }
1266 else if (0 == strcmp(argv[1], _("stop")))
1267 return (samhainctl (2, &argc, argv));
1268 else if (0 == strcmp(argv[1], _("reload")))
1269 (void) samhainctl (3, &argc, argv); /* does not return */
1270 else if (0 == strcmp(argv[1], _("force-reload")))
1271 (void) samhainctl (3, &argc, argv); /* does not return */
1272 else if (0 == strcmp(argv[1], _("status")))
1273 (void) samhainctl (4, &argc, argv); /* does not return */
1274 else if (0 == strcmp(argv[1], _("restart")))
1275 {
1276 res = samhainctl (2, &argc, argv);
1277 if (res == 0 || res == 7)
1278 {
1279 (void) samhainctl (1, &argc, argv); /* does not return */
1280 }
1281 else
1282 return (res);
1283 }
1284 }
1285
1286 /* if fd 0 is closed, presume that we want to be daemon and
1287 * run in check mode
1288 */
1289 if ((-1) == retry_fcntl(FIL__, __LINE__, 0, F_GETFL, 0) &&
1290 errno == EBADF)
1291 {
1292 sh.flag.opts = S_TRUE;
1293 (void) sh_unix_setdeamon(NULL);
1294#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1295 sh.flag.checkSum = SH_CHECK_CHECK;
1296 /* (void) sh_util_setchecksum(_("check")); */
1297#endif
1298 is_samhainctl_init = S_TRUE;
1299 sh.flag.opts = S_FALSE;
1300 }
1301
1302
1303 /* --- Install the exit handler. ---
1304 */
1305 (void) atexit(exit_handler);
1306
1307 /* --- Zero the mailer key, and fill it. ---
1308 */
1309 memset (ErrFlag, 0, 2*sizeof(UINT32));
1310
1311#ifdef MKA_01
1312 ErrFlag[0] |= (1 << 0);
1313#endif
1314#ifdef MKA_02
1315 ErrFlag[0] |= (1 << 1);
1316#endif
1317#ifdef MKA_03
1318 ErrFlag[0] |= (1 << 2);
1319#endif
1320#ifdef MKA_04
1321 ErrFlag[0] |= (1 << 3);
1322#endif
1323#ifdef MKA_05
1324 ErrFlag[0] |= (1 << 4);
1325#endif
1326#ifdef MKA_06
1327 ErrFlag[0] |= (1 << 5);
1328#endif
1329#ifdef MKA_07
1330 ErrFlag[0] |= (1 << 6);
1331#endif
1332#ifdef MKA_08
1333 ErrFlag[0] |= (1 << 7);
1334#endif
1335
1336#if defined(SCREW_IT_UP)
1337 BREAKEXIT(sh_sigtrap_prepare);
1338 (void) sh_sigtrap_prepare();
1339#endif
1340
1341 /* Save the timezone.
1342 */
1343 if (NULL != (tzptr = getenv("TZ"))) /* flawfinder: ignore */
1344 {
1345 tzlen = strlen(tzptr);
1346 if (tzlen < 1024)
1347 {
1348 sh.timezone = malloc (tzlen + 1);
1349 if (sh.timezone != NULL)
1350 (void) sl_strlcpy (sh.timezone, tzptr, tzlen + 1);
1351 }
1352 else
1353 sh.timezone = NULL;
1354 }
1355 else
1356 sh.timezone = NULL;
1357
1358
1359 /* -------- INIT --------
1360 */
1361
1362 /* Restrict error logging to stderr.
1363 */
1364 sh_error_only_stderr (S_TRUE);
1365
1366 BREAKEXIT(sh_derr);
1367 (void) sh_derr();
1368
1369 /* Check that first three descriptors are open.
1370 */
1371 if ( retry_fcntl(FIL__, __LINE__, 0, F_GETFL, 0) == (-1))
1372 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 0);
1373 if ( retry_fcntl(FIL__, __LINE__, 1, F_GETFL, 0) == (-1))
1374 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 1);
1375 if ( retry_fcntl(FIL__, __LINE__, 2, F_GETFL, 0) == (-1))
1376 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 2);
1377
1378 /* --- Set default values. ---
1379 */
1380 BREAKEXIT(sh_init);
1381 sh_init (); /* we are still privileged here, so we can mlock skey */
1382#if (defined (SH_WITH_SERVER) && !defined (SH_WITH_CLIENT))
1383 sh.flag.isserver = S_TRUE;
1384#endif
1385
1386 /* --- Get local hostname. ---
1387 */
1388 BREAKEXIT(sh_unix_localhost);
1389 sh_unix_localhost();
1390
1391 /* --- Read the command line. ---
1392 */
1393 sh.flag.opts = S_TRUE;
1394
1395#if !defined(SH_STEALTH_NOCL)
1396 sh_argc_store = argc;
1397 sh_argv_store = argv;
1398 (void) sh_getopt_get (argc, argv);
1399#else
1400 if (argc > 1 && argv[1] != NULL &&
1401 strlen(argv[1]) > 0 && strlen(NOCL_CODE) > 0)
1402 {
1403 if ( 0 == strcmp(argv[1], NOCL_CODE) )
1404 {
1405#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1406 char * saveptr;
1407#endif
1408 my_argv[0] = argv[0]; ++my_argc;
1409 command_line[0] = '\0';
1410 if (NULL != fgets (command_line, sizeof(command_line), stdin))
1411 command_line[sizeof(command_line)-1] = '\0';
1412
1413 do {
1414#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1415 my_argv[my_argc] =
1416 strtok_r( (my_argc == 1) ? command_line : NULL, " \n", &saveptr);
1417#else
1418 my_argv[my_argc] =
1419 strtok( (my_argc == 1) ? command_line : NULL, " \n");
1420#endif
1421 if (my_argv[my_argc] != NULL) {
1422 ++my_argc;
1423 } else {
1424 break;
1425 }
1426 } while (my_argc < 32);
1427
1428 sh_argc_store = my_argc;
1429 sh_argv_store = my_argv;
1430
1431 (void) sh_getopt_get (my_argc, my_argv);
1432 }
1433 else
1434 {
1435 /* discard command line */
1436 /* _exit(EXIT_FAILURE) */ ;
1437 }
1438 }
1439#endif
1440 sh.flag.opts = S_FALSE;
1441
1442
1443 /* --- Get user info. ---
1444 */
1445 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1446 if (0 != sh_unix_getUser ())
1447 {
1448 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1449 sh.prg_name);
1450 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1451 }
1452
1453
1454 /* *****************************
1455 *
1456 * Read the configuration file.
1457 *
1458 * *****************************/
1459
1460 TPT((0, FIL__, __LINE__, _("msg=<Read the configuration file.>\n")))
1461 BREAKEXIT(sh_readconf_read);
1462 (void) sh_readconf_read ();
1463
1464#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1465 if (sh.flag.checkSum == SH_CHECK_NONE)
1466 {
1467 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
1468 _("No action specified: init, update, or check"),
1469 _("main"));
1470 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1471 sh.prg_name);
1472 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
1473 }
1474#endif
1475
1476 /* do not append to database if run SUID
1477 */
1478 if ((sh.flag.checkSum == SH_CHECK_INIT) && (0 != sl_is_suid()))
1479 {
1480 (void) dlog(1, FIL__, __LINE__,
1481 _("Cannot initialize database when running with SUID credentials.\nYou need to run this with the user ID %d.\nYour current user ID is %d."),
1482 (int) geteuid(), (int) sh.real.uid);
1483 sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_ACCESS,
1484 (long) sh.real.uid, sh.data.path);
1485 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1486 }
1487
1488 /* avoid daemon mode for initialization
1489 */
1490 if (sh.flag.checkSum == SH_CHECK_INIT)
1491 {
1492 sh.flag.isdaemon = S_FALSE;
1493 sh.flag.loop = S_FALSE;
1494 }
1495
1496 /* --- load database; checksum of database
1497 */
1498#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1499 TPT((0, FIL__, __LINE__, _("msg=<Get checksum of the database.>\n")))
1500 if (sh.flag.checkSum == SH_CHECK_CHECK)
1501 {
1502 if (0 != sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
1503 {
1504 char hashbuf[KEYBUF_SIZE];
1505 (void) sl_strlcpy(sh.data.hash,
1506 sh_tiger_hash (file_path('D', 'R'),
1507 TIGER_FILE, TIGER_NOLIM,
1508 hashbuf, sizeof(hashbuf)),
1509 KEY_LEN+1);
1510 }
1511
1512 /* this eventually fetches the file from server to get checksum
1513 */
1514 sh_hash_init ();
1515 }
1516#endif
1517
1518 /* --- initialize signal handling etc.; fork daemon
1519 */
1520 if (sh_unix_init(sh.flag.isdaemon) == -1)
1521 {
1522 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1523 sh.prg_name);
1524 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1525 }
1526
1527 /* --- drop privileges eventually ---
1528 */
1529#if defined(SH_WITH_SERVER)
1530 sh_create_tcp_socket ();
1531#if defined(INET_SYSLOG)
1532 create_syslog_socket (S_TRUE);
1533#endif
1534 SL_REQUIRE(sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE,
1535 _("sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE"));
1536#else
1537 SL_REQUIRE(sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE,
1538 _("sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE"));
1539#endif
1540
1541 /* --- Get user info (again). ---
1542 */
1543 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1544 if (0 != sh_unix_getUser ())
1545 {
1546 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1547 sh.prg_name);
1548 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1549 }
1550
1551 /* --- now check whether we really wanted it; if not, close ---
1552 */
1553#if defined(INET_SYSLOG) && defined(SH_WITH_SERVER)
1554 create_syslog_socket (S_FALSE);
1555#endif
1556
1557
1558 /* --- Enable full error logging ---
1559 */
1560 sh_error_only_stderr (S_FALSE);
1561
1562 sh.flag.started = S_TRUE;
1563
1564 /****************************************************
1565 *
1566 * SERVER
1567 *
1568 ****************************************************/
1569
1570#if defined(SH_WITH_SERVER) && !defined(SH_WITH_CLIENT)
1571
1572#if (defined(WITH_GPG) || defined(WITH_PGP))
1573 /* log startup */
1574 sh_gpg_log_startup ();
1575#else
1576 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1577 sh.prg_name, (long) sh.real.uid,
1578 (sh.flag.hidefile == S_TRUE) ?
1579 _("(hidden)") : file_path('C','R'),
1580 sh.conf.hash);
1581#endif
1582
1583#else
1584
1585 /****************************************************
1586 *
1587 * CLIENT/STANDALONE
1588 *
1589 ****************************************************/
1590
1591 BREAKEXIT(sh_error_handle);
1592
1593 if (sh.flag.checkSum == SH_CHECK_CHECK)
1594 {
1595#if (defined(WITH_GPG) || defined(WITH_PGP))
1596 /* log startup */
1597 sh_gpg_log_startup ();
1598#else
1599 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_2H,
1600 sh.prg_name, (long) sh.real.uid,
1601 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash,
1602 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('D', 'R'), sh.data.hash);
1603#endif
1604 }
1605 else
1606 {
1607#if (defined(WITH_GPG) || defined(WITH_PGP))
1608 /* log startup */
1609 sh_gpg_log_startup ();
1610#else
1611 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1612 sh.prg_name, (long) sh.real.uid,
1613 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash);
1614#endif
1615 }
1616#endif
1617
1618
1619 if ((skey == NULL) || (skey->mlock_failed == SL_TRUE))
1620 sh_error_handle ((-1), FIL__, __LINE__, EPERM, MSG_MLOCK);
1621
1622 /* timer
1623 */
1624 tcurrent = time (NULL);
1625 told = tcurrent;
1626 sh.mailTime.alarm_last = told;
1627
1628
1629 /****************************************************
1630 *
1631 * SERVER
1632 *
1633 ****************************************************/
1634
1635#if defined(SH_WITH_SERVER)
1636 TPT((0, FIL__, __LINE__, _("msg=<Start server.>\n")))
1637
1638#if defined (SH_WITH_CLIENT)
1639 if (sh.flag.isserver == S_TRUE)
1640 {
1641 sh_receive();
1642 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1643 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1644 }
1645#else
1646 sh_receive();
1647 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1648 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1649#endif
1650
1651#endif
1652
1653 /****************************************************
1654 *
1655 * CLIENT/STANDALONE
1656 *
1657 ****************************************************/
1658#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1659
1660
1661 /* --- Initialize modules. ---
1662 */
1663 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")))
1664 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1665 {
1666 status = modList[modnum].mod_init(&(modList[modnum]));
1667 if ( status < 0 )
1668 {
1669 if (status == (-1)) {
1670 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, status,
1671 MSG_MOD_FAIL,
1672 _(modList[modnum].name),
1673 status+SH_MOD_OFFSET);
1674 } else {
1675 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_FAIL,
1676 _(modList[modnum].name),
1677 status+SH_MOD_OFFSET);
1678 }
1679 modList[modnum].initval = SH_MOD_FAILED;
1680 }
1681 else
1682 {
1683 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1684 _(modList[modnum].name));
1685 modList[modnum].initval = status;
1686 }
1687 }
1688
1689 /* -------- TEST SETUP ---------
1690 */
1691 (void) sh_files_setrec();
1692 (void) sh_files_test_setup();
1693
1694
1695 /* -------- NICE LEVEL ---------
1696 */
1697 if (0 != sh.flag.nice)
1698 {
1699#ifdef HAVE_SETPRIORITY
1700 /*@-unrecog@*/
1701 (void) setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1702 /*@+unrecog@*/
1703#else
1704 (void) nice(sh.flag.nice);
1705#endif
1706 }
1707
1708 /* -------- MAIN LOOP ---------
1709 */
1710 sh.statistics.bytes_speed = 0;
1711 sh.statistics.bytes_hashed = 0;
1712
1713 while (1 == 1)
1714 {
1715 ++cct;
1716
1717 BREAKEXIT(sh_error_handle);
1718
1719 TPT((0, FIL__, __LINE__, _("msg=<Start main loop.>, iter=<%ld>\n"), cct))
1720
1721 tcurrent = time (NULL);
1722
1723 if (sig_raised > 0)
1724 {
1725
1726 TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
1727
1728 if (sig_termfast == 1) /* SIGTERM */
1729 {
1730 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1731 /* strncpy (sh_sig_msg, _("SIGTERM"), 20); */
1732 --sig_raised; --sig_urgent;
1733 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1734 }
1735
1736 if (sig_force_check == 1) /* SIGTTOU */
1737 {
1738 TPT((0, FIL__, __LINE__, _("msg=<Check run triggered.>\n")));
1739 flag_check_1 = 1;
1740 flag_check_2 = 1;
1741 sig_force_check = 0;
1742 --sig_raised;
1743 }
1744
1745 if (sig_config_read_again == 1 && /* SIGHUP */
1746 sh_global_suspend_flag == 0)
1747 {
1748 TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")))
1749 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
1750
1751 sh_thread_pause_flag = S_TRUE;
1752
1753#if defined(WITH_EXTERNAL)
1754 /* delete list of external tasks
1755 */
1756 (void) sh_ext_cleanup();
1757#endif
1758#if defined(SH_WITH_MAIL)
1759 sh_nmail_free();
1760#endif
1761
1762 /* delete the file list, make all database
1763 * entries visible (allignore = FALSE)
1764 */
1765 (void) sh_files_deldirstack ();
1766 (void) sh_files_delfilestack ();
1767 (void) sh_files_delglobstack ();
1768 (void) sh_ignore_clean ();
1769 (void) hash_full_tree ();
1770
1771#if defined(SH_WITH_CLIENT)
1772 reset_count_dev_server();
1773#endif
1774#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
1775
1776
1777 FileSchedOne = free_sched(FileSchedOne);
1778 FileSchedTwo = free_sched(FileSchedTwo);
1779
1780 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1781 {
1782 /* sh_thread_pause_flag is true, and we block in lock
1783 * until check has returned, so we are sure check will
1784 * not run until sh_thread_pause_flag is set to false
1785 */
1786 /* if (modList[modnum].initval >= SH_MOD_ACTIVE) */
1787 (void) modList[modnum].mod_reconf();
1788 }
1789#endif
1790
1791 reset_count_dev_console();
1792 reset_count_dev_time();
1793
1794 (void) sh_unix_maskreset();
1795
1796 /* Should this be included ???
1797 * (i.e. should we reload the database ?)
1798 */
1799#ifdef RELOAD_DATABASE
1800 sh_hash_hashdelete();
1801#endif
1802 (void) sl_trust_purge_user();
1803 (void) sh_files_hle_reg (NULL);
1804 (void) sh_prelink_run (NULL, NULL, 0);
1805
1806 /* --------------------------
1807 * --- READ CONFIGURATION ---
1808 * --------------------------
1809 */
1810 (void) sh_readconf_read ();
1811 sig_config_read_again = 0;
1812 (void) sh_files_setrec();
1813 (void) sh_files_test_setup();
1814 if (0 != sh.flag.nice)
1815 {
1816#ifdef HAVE_SETPRIORITY
1817 setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1818#else
1819 nice(sh.flag.nice);
1820#endif
1821 }
1822
1823 if (sh.flag.checkSum == SH_CHECK_INIT)
1824 {
1825 sh.flag.isdaemon = S_FALSE;
1826 sh.flag.loop = S_FALSE;
1827 }
1828
1829
1830 /* --- Initialize modules. ---
1831 */
1832 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")));
1833 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1834 {
1835 status = modList[modnum].mod_init(&(modList[modnum]));
1836
1837 if (status < 0)
1838 {
1839 if (status == (-1)) {
1840 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__,
1841 status, MSG_MOD_FAIL,
1842 _(modList[modnum].name),
1843 status+SH_MOD_OFFSET);
1844 } else {
1845 sh_error_handle ((-1), FIL__, __LINE__,
1846 status, MSG_MOD_FAIL,
1847 _(modList[modnum].name),
1848 status+SH_MOD_OFFSET);
1849 }
1850 modList[modnum].initval = SH_MOD_FAILED;
1851 }
1852 else
1853 {
1854 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1855 _(modList[modnum].name));
1856 modList[modnum].initval = status;
1857 }
1858 }
1859
1860 /* module is properly set up now
1861 */
1862 sh_thread_pause_flag = S_FALSE;
1863
1864 --sig_raised;
1865 }
1866
1867 if (sig_fresh_trail == 1) /* SIGIOT */
1868 {
1869 if (sh_global_suspend_flag == 0)
1870 {
1871 SH_MUTEX_LOCK(mutex_thread_nolog);
1872
1873 /* Logfile access
1874 */
1875#ifdef SH_USE_XML
1876 (void) sh_log_file (NULL, NULL);
1877#endif
1878 TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
1879 sh_error_only_stderr (S_TRUE);
1880 (void) sh_unix_rm_lock_file(sh.srvlog.name);
1881 (void) retry_msleep(3, 0);
1882 sh.flag.log_start = S_TRUE;
1883 sh_error_only_stderr (S_FALSE);
1884 sh_thread_pause_flag = S_FALSE;
1885 sig_fresh_trail = 0;
1886 --sig_raised;
1887 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1888 }
1889 }
1890
1891 if (sig_terminate == 1) /* SIGQUIT */
1892 {
1893 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1894 strncpy (sh_sig_msg, _("Quit"), 20);
1895 --sig_raised; --sig_urgent;
1896 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1897 }
1898
1899 if (sig_debug_switch == 1) /* SIGUSR1 */
1900 {
1901 TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
1902 sh_error_dbg_switch();
1903 sig_debug_switch = 0;
1904 --sig_raised;
1905 }
1906
1907 if (sig_suspend_switch > 0) /* SIGUSR2 */
1908 {
1909 TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
1910 if (sh_global_suspend_flag != 1) {
1911 SH_MUTEX_LOCK_UNSAFE(mutex_thread_nolog);
1912 sh_global_suspend_flag = 1;
1913 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND,
1914 sh.prg_name);
1915 } else {
1916 sh_global_suspend_flag = 0;
1917 SH_MUTEX_UNLOCK_UNSAFE(mutex_thread_nolog);
1918 }
1919 --sig_suspend_switch;
1920 --sig_raised; --sig_urgent;
1921 }
1922 sig_raised = (sig_raised < 0) ? 0 : sig_raised;
1923 sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
1924 TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
1925 }
1926
1927 if (sh_global_suspend_flag == 1)
1928 {
1929 (void) retry_msleep (1, 0);
1930 continue;
1931 }
1932
1933 /* see whether its time to check files
1934 */
1935 if (sh.flag.checkSum == SH_CHECK_INIT ||
1936 (sh.flag.checkSum == SH_CHECK_CHECK &&
1937 (sh.flag.isdaemon == S_FALSE && sh.flag.loop == S_FALSE)))
1938 {
1939 flag_check_1 = 1;
1940 if (FileSchedTwo != NULL)
1941 flag_check_2 = 1;
1942 }
1943 else if (sh.flag.checkSum == SH_CHECK_CHECK ||
1944 (sh.flag.update == S_TRUE &&
1945 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE)))
1946 {
1947 if (FileSchedOne == NULL)
1948 {
1949 /* use interval if we have no schedule
1950 */
1951 if (tcurrent - sh.fileCheck.alarm_last >=
1952 sh.fileCheck.alarm_interval)
1953 flag_check_1 = 1;
1954 }
1955 else
1956 {
1957 flag_check_1 = test_sched(FileSchedOne);
1958 if (FileSchedTwo != NULL)
1959 flag_check_2 = test_sched(FileSchedTwo);
1960 if (flag_check_2 == 1)
1961 flag_check_1 = 1;
1962 }
1963 }
1964
1965 check_done = 0;
1966
1967 if (sh.flag.checkSum != SH_CHECK_NONE &&
1968 (flag_check_1 == 1 || flag_check_2 == 1))
1969 {
1970 /* Refresh list files matching glob patterns.
1971 */
1972 if (sh.flag.checkSum != SH_CHECK_INIT)
1973 sh_files_check_globPatterns();
1974
1975 /*
1976 * check directories and files
1977 * ORDER IS IMPORTANT -- DIRZ FIRST
1978 */
1979 sh.statistics.bytes_hashed = 0;
1980 sh.statistics.time_start = time (NULL);
1981 sh.statistics.dirs_checked = 0;
1982 sh.statistics.files_checked = 0;
1983
1984 TPT((0, FIL__, __LINE__, _("msg=<Check directories.>\n")))
1985 BREAKEXIT(sh_dirs_chk);
1986 if (flag_check_1 == 1)
1987 {
1988 (void) sh_dirs_chk (1);
1989#ifndef SH_PROFILE
1990 (void) retry_aud_chdir (FIL__, __LINE__, "/");
1991#endif
1992 }
1993 if (flag_check_2 == 1)
1994 {
1995 (void) sh_dirs_chk (2);
1996#ifndef SH_PROFILE
1997 (void) retry_aud_chdir (FIL__, __LINE__, "/");
1998#endif
1999 }
2000 TPT((0, FIL__, __LINE__, _("msg=<Check files.>\n")))
2001 BREAKEXIT(sh_files_chk);
2002 if (flag_check_1 == 1)
2003 (void) sh_files_chk ();
2004
2005 if (sig_urgent > 0)
2006 continue;
2007
2008 /*
2009 * check for files not visited
2010 */
2011 if (flag_check_2 == 1 || FileSchedTwo == NULL)
2012 {
2013 TPT((0, FIL__, __LINE__, _("msg=<Check for missing files.>\n")))
2014 sh_hash_unvisited (ShDFLevel[SH_ERR_T_FILE]);
2015 }
2016
2017 if (sig_urgent > 0)
2018 continue;
2019
2020 /* reset
2021 */
2022 TPT((0, FIL__, __LINE__, _("msg=<Reset status.>\n")))
2023 sh_dirs_reset ();
2024 if (sig_urgent > 0)
2025 continue;
2026
2027 sh_files_reset ();
2028 flag_check_1 = 0;
2029 flag_check_2 = 0;
2030 check_done = 1;
2031
2032 (void) sh_prelink_run (NULL, NULL, 0);
2033
2034 if (sig_urgent > 0)
2035 continue;
2036
2037 runtim = time(NULL) - sh.statistics.time_start;
2038 sh.statistics.time_check = runtim;
2039
2040 if ((sh.statistics.dirs_checked == 0) &&
2041 (sh.statistics.files_checked == 0))
2042 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_0);
2043
2044 else
2045 {
2046 st_1 = (float) sh.statistics.bytes_hashed;
2047 st_2 = (float) runtim;
2048
2049
2050 if (st_1 > FLT_EPSILON && st_2 > FLT_EPSILON)
2051 st_1 = st_1/st_2;
2052 else if (st_1 > FLT_EPSILON)
2053 st_1 = (float) (st_1 * 1.0);
2054 else
2055 st_1 = 0.0;
2056
2057 sh.statistics.bytes_speed = (unsigned long) st_1;
2058
2059 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_1,
2060 (long) runtim,
2061 0.001 * st_1);
2062 }
2063 sh.fileCheck.alarm_last = time (NULL);
2064
2065 if (sig_urgent > 0)
2066 continue;
2067
2068 /*
2069 * flush mail queue
2070 */
2071#if defined(SH_WITH_MAIL)
2072 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
2073 (void) sh_nmail_flush ();
2074#endif
2075 }
2076
2077 if (sig_urgent > 0)
2078 continue;
2079
2080 /* execute modules
2081 */
2082 TPT((0, FIL__, __LINE__, _("msg=<Execute modules.>\n")))
2083 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
2084 {
2085 if (modList[modnum].initval == SH_MOD_ACTIVE &&
2086 0 != modList[modnum].mod_timer(tcurrent))
2087 if (0 != (status = modList[modnum].mod_check()))
2088 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_EXEC,
2089 _(modList[modnum].name), (long) (status+SH_MOD_OFFSET));
2090 }
2091
2092 /* 27.05.2002 avoid empty database
2093 * 22.10.2002 moved here b/o suid check initialization
2094 */
2095 if (sh.flag.checkSum == SH_CHECK_INIT)
2096 sh_hash_pushdata (NULL, NULL);
2097
2098 /* write out database
2099 */
2100 if (sh.flag.checkSum == SH_CHECK_CHECK &&
2101 sh.flag.update == S_TRUE &&
2102 check_done == 1)
2103 sh_hash_writeout ();
2104
2105 /* no-op unless MEM_LOG is defined in sh_mem.c
2106 */
2107#ifdef MEM_DEBUG
2108 sh_mem_dump ();
2109#endif
2110
2111 {
2112 char * stale;
2113
2114 stale = sl_check_stale();
2115 if (stale)
2116 {
2117 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
2118 stale, _("sl_check_stale"));
2119 }
2120
2121 stale = sl_check_badfd();
2122 if (stale)
2123 {
2124 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
2125 stale, _("sl_check_stale"));
2126 }
2127 }
2128
2129 /* no loop if not daemon
2130 */
2131 if (sh.flag.isdaemon != S_TRUE && sh.flag.loop == S_FALSE)
2132 break;
2133 if (sig_urgent > 0)
2134 continue;
2135
2136 /* see whether its time to send mail
2137 */
2138#if defined(SH_WITH_MAIL)
2139 if (tcurrent - sh.mailTime.alarm_last >= sh.mailTime.alarm_interval)
2140 {
2141 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
2142 (void) sh_nmail_flush ();
2143 sh.mailTime.alarm_last = time (NULL);
2144 }
2145#endif
2146 if (sig_urgent > 0)
2147 continue;
2148
2149 /* log the timestamp
2150 */
2151 if ((int)(tcurrent - told) >= sh.looptime )
2152 {
2153 TPT((0, FIL__, __LINE__, _("msg=<Log the timestamp.>\n")))
2154 told = tcurrent;
2155#ifdef MEM_DEBUG
2156 sh_mem_check();
2157 sh_unix_count_mlock();
2158#else
2159 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_STAMP);
2160#endif
2161 }
2162
2163 /* seed / re-seed the PRNG if required
2164 */
2165 (void) taus_seed();
2166
2167 if (sig_urgent > 0)
2168 continue;
2169
2170 /* go to sleep
2171 */
2172 (void) retry_msleep (1, 0);
2173
2174 BREAKEXIT(sh_derr);
2175 (void) sh_derr();
2176 }
2177
2178 /* ------ END -----------
2179 */
2180
2181
2182
2183 /*
2184 * cleanup
2185 */
2186 TPT((0, FIL__, __LINE__, _("msg=<Cleanup.>\n")));
2187 sh_hash_hashdelete();
2188
2189#if defined(SH_WITH_MAIL)
2190 if (sh.mailNum.alarm_last > 0)
2191 (void)sh_nmail_flush ();
2192#endif
2193
2194 /* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
2195#endif
2196
2197#if 0
2198 {
2199 char command[128];
2200 sprintf(command, "/bin/cat /proc/%d/status", (int) getpid());
2201 system(command); /* flawfinder: ignore *//* debug code */
2202 malloc_stats();
2203 }
2204#endif
2205
2206 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
2207 SL_RETURN(0, _("main"));
2208}
Note: See TracBrowser for help on using the repository browser.