source: trunk/src/samhain.c@ 191

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

Fix for tickets #112, #113 (dnmalloc deadlock on fork, hostname portability in test script).

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