source: trunk/src/samhain.c@ 166

Last change on this file since 166 was 162, checked in by katerina, 17 years ago

Fix for ticket #89, #90, and #91 (locking,compile failure).

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