source: trunk/src/samhain.c@ 169

Last change on this file since 169 was 169, checked in by katerina, 16 years ago

Fixes for tickes #93 to #104 (yes, big commit, bad, bad,...).

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