source: trunk/src/samhain.c@ 171

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

Include dnmalloc (ticket #108) and fix bugs #106 (EINPROGRESS) and #107 (compressBound).

File size: 49.2 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 1999, 2000 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27#include <fcntl.h>
28
29/* samhainctl */
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <sys/wait.h>
33#include <signal.h>
34#include <errno.h>
35
36
37#if TIME_WITH_SYS_TIME
38#include <sys/time.h>
39#include <time.h>
40#else
41#if HAVE_SYS_TIME_H
42#include <sys/time.h>
43#else
44#include <time.h>
45#endif
46#endif
47
48#ifdef HAVE_MEMORY_H
49#include <memory.h>
50#endif
51
52#ifdef HAVE_SETPRIORITY
53#include <sys/resource.h>
54#endif
55
56#ifndef HAVE_LSTAT
57#define lstat stat
58#endif
59
60/* for FLT_EPSILON
61 */
62#include <float.h>
63
64#include "samhain.h"
65#include "sh_pthread.h"
66#include "sh_files.h"
67#include "sh_utils.h"
68#include "sh_error.h"
69#include "sh_unix.h"
70#include "sh_getopt.h"
71#include "sh_readconf.h"
72#include "sh_hash.h"
73
74#include "sh_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 SH_G_INIT; /* Must precede any use of _() */
1225
1226 SL_ENTER(_("main"));
1227
1228 /* --- Close all but first three file descriptors. ---
1229 */
1230 sh_unix_closeall(3, -1); /* at program start */
1231
1232
1233 if (argc >= 2 && 0 != getuid() &&
1234 (0 == strcmp(argv[1], _("start")) ||
1235 0 == strcmp(argv[1], _("stop")) ||
1236 0 == strcmp(argv[1], _("reload")) ||
1237 0 == strcmp(argv[1], _("force-reload")) ||
1238 0 == strcmp(argv[1], _("status")) ||
1239 0 == strcmp(argv[1], _("restart"))))
1240 {
1241 return 4;
1242 }
1243
1244 if (argc >= 2 && 0 == getuid())
1245 {
1246 /* return codes:
1247 * 0 Success
1248 * 1 Can not send signal / start program
1249 * 2 Pid file does not exist
1250 */
1251 if (0 == strcmp(argv[1], _("start")))
1252 {
1253 (void) samhainctl (1, &argc, argv); /* does not return */
1254 }
1255 else if (0 == strcmp(argv[1], _("stop")))
1256 return (samhainctl (2, &argc, argv));
1257 else if (0 == strcmp(argv[1], _("reload")))
1258 (void) samhainctl (3, &argc, argv); /* does not return */
1259 else if (0 == strcmp(argv[1], _("force-reload")))
1260 (void) samhainctl (3, &argc, argv); /* does not return */
1261 else if (0 == strcmp(argv[1], _("status")))
1262 (void) samhainctl (4, &argc, argv); /* does not return */
1263 else if (0 == strcmp(argv[1], _("restart")))
1264 {
1265 res = samhainctl (2, &argc, argv);
1266 if (res == 0 || res == 7)
1267 {
1268 (void) samhainctl (1, &argc, argv); /* does not return */
1269 }
1270 else
1271 return (res);
1272 }
1273 }
1274
1275 /* if fd 0 is closed, presume that we want to be daemon and
1276 * run in check mode
1277 */
1278 if ((-1) == retry_fcntl(FIL__, __LINE__, 0, F_GETFL, 0) &&
1279 errno == EBADF)
1280 {
1281 sh.flag.opts = S_TRUE;
1282 (void) sh_unix_setdeamon(NULL);
1283#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1284 sh.flag.checkSum = SH_CHECK_CHECK;
1285 /* (void) sh_util_setchecksum(_("check")); */
1286#endif
1287 is_samhainctl_init = S_TRUE;
1288 sh.flag.opts = S_FALSE;
1289 }
1290
1291
1292 /* --- Install the exit handler. ---
1293 */
1294 (void) atexit(exit_handler);
1295
1296 /* --- Zero the mailer key, and fill it. ---
1297 */
1298 memset (ErrFlag, 0, 2*sizeof(UINT32));
1299
1300#ifdef MKA_01
1301 ErrFlag[0] |= (1 << 0);
1302#endif
1303#ifdef MKA_02
1304 ErrFlag[0] |= (1 << 1);
1305#endif
1306#ifdef MKA_03
1307 ErrFlag[0] |= (1 << 2);
1308#endif
1309#ifdef MKA_04
1310 ErrFlag[0] |= (1 << 3);
1311#endif
1312#ifdef MKA_05
1313 ErrFlag[0] |= (1 << 4);
1314#endif
1315#ifdef MKA_06
1316 ErrFlag[0] |= (1 << 5);
1317#endif
1318#ifdef MKA_07
1319 ErrFlag[0] |= (1 << 6);
1320#endif
1321#ifdef MKA_08
1322 ErrFlag[0] |= (1 << 7);
1323#endif
1324
1325#if defined(SCREW_IT_UP)
1326 BREAKEXIT(sh_sigtrap_prepare);
1327 (void) sh_sigtrap_prepare();
1328#endif
1329
1330 /* Save the timezone.
1331 */
1332 if (NULL != (tzptr = getenv("TZ"))) /* flawfinder: ignore */
1333 {
1334 tzlen = strlen(tzptr);
1335 if (tzlen < 1024)
1336 {
1337 sh.timezone = malloc (tzlen + 1);
1338 if (sh.timezone != NULL)
1339 (void) sl_strlcpy (sh.timezone, tzptr, tzlen + 1);
1340 }
1341 else
1342 sh.timezone = NULL;
1343 }
1344 else
1345 sh.timezone = NULL;
1346
1347
1348 /* -------- INIT --------
1349 */
1350
1351 /* Restrict error logging to stderr.
1352 */
1353 sh_error_only_stderr (S_TRUE);
1354
1355 BREAKEXIT(sh_derr);
1356 (void) sh_derr();
1357
1358 /* Check that first three descriptors are open.
1359 */
1360 if ( retry_fcntl(FIL__, __LINE__, 0, F_GETFL, 0) == (-1))
1361 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 0);
1362 if ( retry_fcntl(FIL__, __LINE__, 1, F_GETFL, 0) == (-1))
1363 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 1);
1364 if ( retry_fcntl(FIL__, __LINE__, 2, F_GETFL, 0) == (-1))
1365 (void) aud_open(FIL__, __LINE__, SL_NOPRIV, _("/dev/null"), O_RDWR, 2);
1366
1367 /* --- Set default values. ---
1368 */
1369 BREAKEXIT(sh_init);
1370 sh_init (); /* we are still privileged here, so we can mlock skey */
1371#if (defined (SH_WITH_SERVER) && !defined (SH_WITH_CLIENT))
1372 sh.flag.isserver = S_TRUE;
1373#endif
1374
1375 /* --- Get local hostname. ---
1376 */
1377 BREAKEXIT(sh_unix_localhost);
1378 sh_unix_localhost();
1379
1380 /* --- Read the command line. ---
1381 */
1382 sh.flag.opts = S_TRUE;
1383
1384#if !defined(SH_STEALTH_NOCL)
1385 sh_argc_store = argc;
1386 sh_argv_store = argv;
1387 (void) sh_getopt_get (argc, argv);
1388#else
1389 if (argc > 1 && argv[1] != NULL &&
1390 strlen(argv[1]) > 0 && strlen(NOCL_CODE) > 0)
1391 {
1392 if ( 0 == strcmp(argv[1], NOCL_CODE) )
1393 {
1394#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1395 char * saveptr;
1396#endif
1397 my_argv[0] = argv[0]; ++my_argc;
1398 command_line[0] = '\0';
1399 if (NULL != fgets (command_line, sizeof(command_line), stdin))
1400 command_line[sizeof(command_line)-1] = '\0';
1401
1402 do {
1403#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
1404 my_argv[my_argc] =
1405 strtok_r( (my_argc == 1) ? command_line : NULL, " \n", &saveptr);
1406#else
1407 my_argv[my_argc] =
1408 strtok( (my_argc == 1) ? command_line : NULL, " \n");
1409#endif
1410 if (my_argv[my_argc] != NULL) {
1411 ++my_argc;
1412 } else {
1413 break;
1414 }
1415 } while (my_argc < 32);
1416
1417 sh_argc_store = my_argc;
1418 sh_argv_store = my_argv;
1419
1420 (void) sh_getopt_get (my_argc, my_argv);
1421 }
1422 else
1423 {
1424 /* discard command line */
1425 /* _exit(EXIT_FAILURE) */ ;
1426 }
1427 }
1428#endif
1429 sh.flag.opts = S_FALSE;
1430
1431
1432 /* --- Get user info. ---
1433 */
1434 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1435 if (0 != sh_unix_getUser ())
1436 {
1437 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1438 sh.prg_name);
1439 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1440 }
1441
1442
1443 /* *****************************
1444 *
1445 * Read the configuration file.
1446 *
1447 * *****************************/
1448
1449 TPT((0, FIL__, __LINE__, _("msg=<Read the configuration file.>\n")))
1450 BREAKEXIT(sh_readconf_read);
1451 (void) sh_readconf_read ();
1452
1453#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1454 if (sh.flag.checkSum == SH_CHECK_NONE)
1455 {
1456 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
1457 _("No action specified: init, update, or check"),
1458 _("main"));
1459 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1460 sh.prg_name);
1461 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
1462 }
1463#endif
1464
1465 /* do not append to database if run SUID
1466 */
1467 if ((sh.flag.checkSum == SH_CHECK_INIT) && (0 != sl_is_suid()))
1468 {
1469 (void) dlog(1, FIL__, __LINE__,
1470 _("Cannot initialize database when running with SUID credentials.\nYou need to run this with the user ID %d.\nYour current user ID is %d."),
1471 (int) geteuid(), (int) sh.real.uid);
1472 sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_ACCESS,
1473 (long) sh.real.uid, sh.data.path);
1474 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1475 }
1476
1477 /* avoid daemon mode for initialization
1478 */
1479 if (sh.flag.checkSum == SH_CHECK_INIT)
1480 {
1481 sh.flag.isdaemon = S_FALSE;
1482 sh.flag.loop = S_FALSE;
1483 }
1484
1485 /* --- load database; checksum of database
1486 */
1487#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1488 TPT((0, FIL__, __LINE__, _("msg=<Get checksum of the database.>\n")))
1489 if (sh.flag.checkSum == SH_CHECK_CHECK)
1490 {
1491 if (0 != sl_strcmp(file_path('D', 'R'), _("REQ_FROM_SERVER")))
1492 {
1493 char hashbuf[KEYBUF_SIZE];
1494 (void) sl_strlcpy(sh.data.hash,
1495 sh_tiger_hash (file_path('D', 'R'),
1496 TIGER_FILE, TIGER_NOLIM,
1497 hashbuf, sizeof(hashbuf)),
1498 KEY_LEN+1);
1499 }
1500
1501 /* this eventually fetches the file from server to get checksum
1502 */
1503 sh_hash_init ();
1504 }
1505#endif
1506
1507 /* --- initialize signal handling etc.; fork daemon
1508 */
1509 if (sh_unix_init(sh.flag.isdaemon) == -1)
1510 {
1511 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1512 sh.prg_name);
1513 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1514 }
1515
1516 /* --- drop privileges eventually ---
1517 */
1518#if defined(SH_WITH_SERVER)
1519 sh_create_tcp_socket ();
1520#if defined(INET_SYSLOG)
1521 create_syslog_socket (S_TRUE);
1522#endif
1523 SL_REQUIRE(sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE,
1524 _("sl_policy_get_real(DEFAULT_IDENT) == SL_ENONE"));
1525#else
1526 SL_REQUIRE(sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE,
1527 _("sl_policy_get_user(DEFAULT_IDENT) == SL_ENONE"));
1528#endif
1529
1530 /* --- Get user info (again). ---
1531 */
1532 TPT((0, FIL__, __LINE__, _("msg=<Get user name.>\n")))
1533 if (0 != sh_unix_getUser ())
1534 {
1535 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_EXIT_ABORT1,
1536 sh.prg_name);
1537 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
1538 }
1539
1540 /* --- now check whether we really wanted it; if not, close ---
1541 */
1542#if defined(INET_SYSLOG) && defined(SH_WITH_SERVER)
1543 create_syslog_socket (S_FALSE);
1544#endif
1545
1546
1547 /* --- Enable full error logging ---
1548 */
1549 sh_error_only_stderr (S_FALSE);
1550
1551 /****************************************************
1552 *
1553 * SERVER
1554 *
1555 ****************************************************/
1556
1557#if defined(SH_WITH_SERVER) && !defined(SH_WITH_CLIENT)
1558
1559#if (defined(WITH_GPG) || defined(WITH_PGP))
1560 /* log startup */
1561 sh_gpg_log_startup ();
1562#else
1563 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1564 sh.prg_name, (long) sh.real.uid,
1565 (sh.flag.hidefile == S_TRUE) ?
1566 _("(hidden)") : file_path('C','R'),
1567 sh.conf.hash);
1568#endif
1569
1570#else
1571
1572 /****************************************************
1573 *
1574 * CLIENT/STANDALONE
1575 *
1576 ****************************************************/
1577
1578 BREAKEXIT(sh_error_handle);
1579
1580 if (sh.flag.checkSum == SH_CHECK_CHECK)
1581 {
1582#if (defined(WITH_GPG) || defined(WITH_PGP))
1583 /* log startup */
1584 sh_gpg_log_startup ();
1585#else
1586 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_2H,
1587 sh.prg_name, (long) sh.real.uid,
1588 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash,
1589 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('D', 'R'), sh.data.hash);
1590#endif
1591 }
1592 else
1593 {
1594#if (defined(WITH_GPG) || defined(WITH_PGP))
1595 /* log startup */
1596 sh_gpg_log_startup ();
1597#else
1598 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_START_1H,
1599 sh.prg_name, (long) sh.real.uid,
1600 (sh.flag.hidefile == S_TRUE) ? _("(hidden)") : file_path('C', 'R'), sh.conf.hash);
1601#endif
1602 }
1603#endif
1604
1605
1606 if ((skey == NULL) || (skey->mlock_failed == SL_TRUE))
1607 sh_error_handle ((-1), FIL__, __LINE__, EPERM, MSG_MLOCK);
1608
1609 /* timer
1610 */
1611 tcurrent = time (NULL);
1612 told = tcurrent;
1613 sh.mailTime.alarm_last = told;
1614
1615
1616 /****************************************************
1617 *
1618 * SERVER
1619 *
1620 ****************************************************/
1621
1622#if defined(SH_WITH_SERVER)
1623 TPT((0, FIL__, __LINE__, _("msg=<Start server.>\n")))
1624
1625#if defined (SH_WITH_CLIENT)
1626 if (sh.flag.isserver == S_TRUE)
1627 {
1628 sh_receive();
1629 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1630 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1631 }
1632#else
1633 sh_receive();
1634 TPT((0, FIL__, __LINE__, _("msg=<End server.>\n")))
1635 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1636#endif
1637
1638#endif
1639
1640 /****************************************************
1641 *
1642 * CLIENT/STANDALONE
1643 *
1644 ****************************************************/
1645#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1646
1647
1648 /* --- Initialize modules. ---
1649 */
1650 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")))
1651 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1652 {
1653 status = modList[modnum].mod_init(&(modList[modnum]));
1654 if ( status < 0 )
1655 {
1656 if (status == (-1)) {
1657 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__, status,
1658 MSG_MOD_FAIL,
1659 _(modList[modnum].name),
1660 status);
1661 } else {
1662 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_FAIL,
1663 _(modList[modnum].name),
1664 status);
1665 }
1666 modList[modnum].initval = SH_MOD_FAILED;
1667 }
1668 else
1669 {
1670 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1671 _(modList[modnum].name));
1672 modList[modnum].initval = status;
1673 }
1674 }
1675
1676 /* -------- TEST SETUP ---------
1677 */
1678 (void) sh_files_setrec();
1679 (void) sh_files_test_setup();
1680
1681
1682 /* -------- NICE LEVEL ---------
1683 */
1684 if (0 != sh.flag.nice)
1685 {
1686#ifdef HAVE_SETPRIORITY
1687 /*@-unrecog@*/
1688 (void) setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1689 /*@+unrecog@*/
1690#else
1691 (void) nice(sh.flag.nice);
1692#endif
1693 }
1694
1695 /* -------- MAIN LOOP ---------
1696 */
1697 sh.statistics.bytes_speed = 0;
1698 sh.statistics.bytes_hashed = 0;
1699
1700 while (1 == 1)
1701 {
1702 ++cct;
1703
1704 BREAKEXIT(sh_error_handle);
1705
1706 TPT((0, FIL__, __LINE__, _("msg=<Start main loop.>, iter=<%ld>\n"), cct))
1707
1708 tcurrent = time (NULL);
1709
1710 if (sig_raised > 0)
1711 {
1712
1713 TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
1714
1715 if (sig_termfast == 1) /* SIGTERM */
1716 {
1717 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1718 /* strncpy (sh_sig_msg, _("SIGTERM"), 20); */
1719 --sig_raised; --sig_urgent;
1720 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1721 }
1722
1723 if (sig_force_check == 1) /* SIGTTOU */
1724 {
1725 TPT((0, FIL__, __LINE__, _("msg=<Check run triggered.>\n")));
1726 flag_check_1 = 1;
1727 flag_check_2 = 1;
1728 sig_force_check = 0;
1729 --sig_raised;
1730 }
1731
1732 if (sig_config_read_again == 1 && /* SIGHUP */
1733 sh_global_suspend_flag == 0)
1734 {
1735 TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")))
1736 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
1737
1738 sh_thread_pause_flag = S_TRUE;
1739
1740#if defined(WITH_EXTERNAL)
1741 /* delete list of external tasks
1742 */
1743 (void) sh_ext_cleanup();
1744#endif
1745 /* delete the file list, make all database
1746 * entries visible (allignore = FALSE)
1747 */
1748 (void) sh_files_deldirstack ();
1749 (void) sh_files_delfilestack ();
1750 (void) sh_ignore_clean ();
1751 (void) hash_full_tree ();
1752
1753#if defined(SH_WITH_CLIENT)
1754 reset_count_dev_server();
1755#endif
1756#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
1757
1758
1759 FileSchedOne = free_sched(FileSchedOne);
1760 FileSchedTwo = free_sched(FileSchedTwo);
1761
1762 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1763 {
1764 /* sh_thread_pause_flag is true, and we block in lock
1765 * until check has returned, so we are sure check will
1766 * not run until sh_thread_pause_flag is set to false
1767 */
1768 /* if (modList[modnum].initval >= SH_MOD_ACTIVE) */
1769 (void) modList[modnum].mod_reconf();
1770 }
1771#endif
1772
1773#if defined(SH_WITH_MAIL)
1774 reset_count_dev_mail();
1775#endif
1776 reset_count_dev_console();
1777 reset_count_dev_time();
1778
1779 (void) sh_unix_maskreset();
1780
1781 /* Should this be included ???
1782 * (i.e. should we reload the database ?)
1783 */
1784#ifdef RELOAD_DATABASE
1785 sh_hash_hashdelete();
1786#endif
1787 (void) sl_trust_purge_user();
1788 (void) sh_files_hle_reg (NULL);
1789 (void) sh_prelink_run (NULL, NULL, 0);
1790
1791 /* --------------------------
1792 * --- READ CONFIGURATION ---
1793 * --------------------------
1794 */
1795 (void) sh_readconf_read ();
1796 sig_config_read_again = 0;
1797 (void) sh_files_setrec();
1798 (void) sh_files_test_setup();
1799 if (0 != sh.flag.nice)
1800 {
1801#ifdef HAVE_SETPRIORITY
1802 setpriority(PRIO_PROCESS, 0, sh.flag.nice);
1803#else
1804 nice(sh.flag.nice);
1805#endif
1806 }
1807
1808 if (sh.flag.checkSum == SH_CHECK_INIT)
1809 {
1810 sh.flag.isdaemon = S_FALSE;
1811 sh.flag.loop = S_FALSE;
1812 }
1813
1814
1815 /* --- Initialize modules. ---
1816 */
1817 TPT((0, FIL__, __LINE__, _("msg=<Initialize modules.>\n")));
1818 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
1819 {
1820 status = modList[modnum].mod_init(&(modList[modnum]));
1821
1822 if (status < 0)
1823 {
1824 if (status == (-1)) {
1825 sh_error_handle (SH_ERR_NOTICE, FIL__, __LINE__,
1826 status, MSG_MOD_FAIL,
1827 _(modList[modnum].name),
1828 status);
1829 } else {
1830 sh_error_handle ((-1), FIL__, __LINE__,
1831 status, MSG_MOD_FAIL,
1832 _(modList[modnum].name),
1833 status);
1834 }
1835 modList[modnum].initval = SH_MOD_FAILED;
1836 }
1837 else
1838 {
1839 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MOD_OK,
1840 _(modList[modnum].name));
1841 modList[modnum].initval = status;
1842 }
1843 }
1844
1845 /* module is properly set up now
1846 */
1847 sh_thread_pause_flag = S_FALSE;
1848
1849 --sig_raised;
1850 }
1851
1852 if (sig_fresh_trail == 1) /* SIGIOT */
1853 {
1854 if (sh_global_suspend_flag == 0)
1855 {
1856 SH_MUTEX_LOCK(mutex_thread_nolog);
1857
1858 /* Logfile access
1859 */
1860#ifdef SH_USE_XML
1861 (void) sh_log_file (NULL, NULL);
1862#endif
1863 TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
1864 sh_error_only_stderr (S_TRUE);
1865 (void) sh_unix_rm_lock_file(sh.srvlog.name);
1866 (void) retry_msleep(3, 0);
1867 sh.flag.log_start = S_TRUE;
1868 sh_error_only_stderr (S_FALSE);
1869 sh_thread_pause_flag = S_FALSE;
1870 sig_fresh_trail = 0;
1871 --sig_raised;
1872 SH_MUTEX_UNLOCK(mutex_thread_nolog);
1873 }
1874 }
1875
1876 if (sig_terminate == 1) /* SIGQUIT */
1877 {
1878 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
1879 strncpy (sh_sig_msg, _("Quit"), 20);
1880 --sig_raised; --sig_urgent;
1881 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
1882 }
1883
1884 if (sig_debug_switch == 1) /* SIGUSR1 */
1885 {
1886 TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
1887 sh_error_dbg_switch();
1888 sig_debug_switch = 0;
1889 --sig_raised;
1890 }
1891
1892 if (sig_suspend_switch > 0) /* SIGUSR2 */
1893 {
1894 TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
1895 if (sh_global_suspend_flag != 1) {
1896 SH_MUTEX_LOCK_UNSAFE(mutex_thread_nolog);
1897 sh_global_suspend_flag = 1;
1898 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND,
1899 sh.prg_name);
1900 } else {
1901 sh_global_suspend_flag = 0;
1902 SH_MUTEX_UNLOCK_UNSAFE(mutex_thread_nolog);
1903 }
1904 --sig_suspend_switch;
1905 --sig_raised; --sig_urgent;
1906 }
1907 sig_raised = (sig_raised < 0) ? 0 : sig_raised;
1908 sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
1909 TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
1910 }
1911
1912 if (sh_global_suspend_flag == 1)
1913 {
1914 (void) retry_msleep (1, 0);
1915 continue;
1916 }
1917
1918 /* see whether its time to check files
1919 */
1920 if (sh.flag.checkSum == SH_CHECK_INIT ||
1921 (sh.flag.checkSum == SH_CHECK_CHECK &&
1922 (sh.flag.isdaemon == S_FALSE && sh.flag.loop == S_FALSE)))
1923 {
1924 flag_check_1 = 1;
1925 if (FileSchedTwo != NULL)
1926 flag_check_2 = 1;
1927 }
1928 else if (sh.flag.checkSum == SH_CHECK_CHECK ||
1929 (sh.flag.update == S_TRUE &&
1930 (sh.flag.isdaemon == S_TRUE || sh.flag.loop == S_TRUE)))
1931 {
1932 if (FileSchedOne == NULL)
1933 {
1934 /* use interval if we have no schedule
1935 */
1936 if (tcurrent - sh.fileCheck.alarm_last >=
1937 sh.fileCheck.alarm_interval)
1938 flag_check_1 = 1;
1939 }
1940 else
1941 {
1942 flag_check_1 = test_sched(FileSchedOne);
1943 if (FileSchedTwo != NULL)
1944 flag_check_2 = test_sched(FileSchedTwo);
1945 if (flag_check_2 == 1)
1946 flag_check_1 = 1;
1947 }
1948 }
1949
1950 check_done = 0;
1951
1952 if (sh.flag.checkSum != SH_CHECK_NONE &&
1953 (flag_check_1 == 1 || flag_check_2 == 1))
1954 {
1955 /*
1956 * check directories and files
1957 * ORDER IS IMPORTANT -- DIRZ FIRST
1958 */
1959 sh.statistics.bytes_hashed = 0;
1960 sh.statistics.time_start = time (NULL);
1961 sh.statistics.dirs_checked = 0;
1962 sh.statistics.files_checked = 0;
1963
1964 TPT((0, FIL__, __LINE__, _("msg=<Check directories.>\n")))
1965 BREAKEXIT(sh_dirs_chk);
1966 if (flag_check_1 == 1)
1967 {
1968 (void) sh_dirs_chk (1);
1969#ifndef SH_PROFILE
1970 (void) retry_aud_chdir (FIL__, __LINE__, "/");
1971#endif
1972 }
1973 if (flag_check_2 == 1)
1974 {
1975 (void) sh_dirs_chk (2);
1976#ifndef SH_PROFILE
1977 (void) retry_aud_chdir (FIL__, __LINE__, "/");
1978#endif
1979 }
1980 TPT((0, FIL__, __LINE__, _("msg=<Check files.>\n")))
1981 BREAKEXIT(sh_files_chk);
1982 if (flag_check_1 == 1)
1983 (void) sh_files_chk ();
1984
1985 if (sig_urgent > 0)
1986 continue;
1987
1988 /*
1989 * check for files not visited
1990 */
1991 if (flag_check_2 == 1 || FileSchedTwo == NULL)
1992 {
1993 TPT((0, FIL__, __LINE__, _("msg=<Check for missing files.>\n")))
1994 sh_hash_unvisited (ShDFLevel[SH_ERR_T_FILE]);
1995 }
1996
1997 if (sig_urgent > 0)
1998 continue;
1999
2000 /* reset
2001 */
2002 TPT((0, FIL__, __LINE__, _("msg=<Reset status.>\n")))
2003 sh_dirs_reset ();
2004 if (sig_urgent > 0)
2005 continue;
2006
2007 sh_files_reset ();
2008 flag_check_1 = 0;
2009 flag_check_2 = 0;
2010 check_done = 1;
2011
2012 (void) sh_prelink_run (NULL, NULL, 0);
2013
2014 if (sig_urgent > 0)
2015 continue;
2016
2017 runtim = time(NULL) - sh.statistics.time_start;
2018 sh.statistics.time_check = runtim;
2019
2020 if ((sh.statistics.dirs_checked == 0) &&
2021 (sh.statistics.files_checked == 0))
2022 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_0);
2023
2024 else
2025 {
2026 st_1 = (float) sh.statistics.bytes_hashed;
2027 st_2 = (float) runtim;
2028
2029
2030 if (st_1 > FLT_EPSILON && st_2 > FLT_EPSILON)
2031 st_1 = st_1/st_2;
2032 else if (st_1 > FLT_EPSILON)
2033 st_1 = (float) (st_1 * 1.0);
2034 else
2035 st_1 = 0.0;
2036
2037 sh.statistics.bytes_speed = (unsigned long) st_1;
2038
2039 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_CHECK_1,
2040 (long) runtim,
2041 0.001 * st_1);
2042 }
2043 sh.fileCheck.alarm_last = time (NULL);
2044
2045 if (sig_urgent > 0)
2046 continue;
2047
2048 /*
2049 * flush mail queue
2050 */
2051#if defined(SH_WITH_MAIL)
2052 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
2053 (void) sh_mail_msg (NULL);
2054#endif
2055 }
2056
2057 if (sig_urgent > 0)
2058 continue;
2059
2060 /* execute modules
2061 */
2062 TPT((0, FIL__, __LINE__, _("msg=<Execute modules.>\n")))
2063 for (modnum = 0; modList[modnum].name != NULL; ++modnum)
2064 {
2065 if (modList[modnum].initval == SH_MOD_ACTIVE &&
2066 0 != modList[modnum].mod_timer(tcurrent))
2067 if (0 != (status = modList[modnum].mod_check()))
2068 sh_error_handle ((-1), FIL__, __LINE__, status, MSG_MOD_EXEC,
2069 _(modList[modnum].name), (long) status);
2070 }
2071
2072 /* 27.05.2002 avoid empty database
2073 * 22.10.2002 moved here b/o suid check initialization
2074 */
2075 if (sh.flag.checkSum == SH_CHECK_INIT)
2076 sh_hash_pushdata (NULL, NULL);
2077
2078 /* write out database
2079 */
2080 if (sh.flag.checkSum == SH_CHECK_CHECK &&
2081 sh.flag.update == S_TRUE &&
2082 check_done == 1)
2083 sh_hash_writeout ();
2084
2085 /* no-op unless MEM_LOG is defined in sh_mem.c
2086 */
2087#ifdef MEM_DEBUG
2088 sh_mem_dump ();
2089#endif
2090
2091 /* no loop if not daemon
2092 */
2093 if (sh.flag.isdaemon != S_TRUE && sh.flag.loop == S_FALSE)
2094 break;
2095 if (sig_urgent > 0)
2096 continue;
2097
2098 /* see whether its time to send mail
2099 */
2100#if defined(SH_WITH_MAIL)
2101 if (tcurrent - sh.mailTime.alarm_last >= sh.mailTime.alarm_interval)
2102 {
2103 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
2104 (void) sh_mail_msg (NULL);
2105 sh.mailTime.alarm_last = time (NULL);
2106 }
2107#endif
2108 if (sig_urgent > 0)
2109 continue;
2110
2111 /* log the timestamp
2112 */
2113 if ((int)(tcurrent - told) >= sh.looptime )
2114 {
2115 TPT((0, FIL__, __LINE__, _("msg=<Log the timestamp.>\n")))
2116 told = tcurrent;
2117#ifdef MEM_DEBUG
2118 sh_mem_check();
2119 sh_unix_count_mlock();
2120#else
2121 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_STAMP);
2122#endif
2123 }
2124
2125 /* seed / re-seed the PRNG if required
2126 */
2127 (void) taus_seed();
2128
2129 if (sig_urgent > 0)
2130 continue;
2131
2132 /* go to sleep
2133 */
2134 (void) retry_msleep (1, 0);
2135
2136 BREAKEXIT(sh_derr);
2137 (void) sh_derr();
2138 }
2139
2140 /* ------ END -----------
2141 */
2142
2143
2144
2145 /*
2146 * cleanup
2147 */
2148 TPT((0, FIL__, __LINE__, _("msg=<Cleanup.>\n")));
2149 sh_hash_hashdelete();
2150
2151#if defined(SH_WITH_MAIL)
2152 if (sh.mailNum.alarm_last > 0)
2153 (void)sh_mail_msg (NULL);
2154#endif
2155
2156 /* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
2157#endif
2158
2159#if 0
2160 {
2161 char command[128];
2162 sprintf(command, "/bin/cat /proc/%d/status", (int) getpid());
2163 system(command); /* flawfinder: ignore *//* debug code */
2164 malloc_stats();
2165 }
2166#endif
2167
2168 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
2169 SL_RETURN(0, _("main"));
2170}
Note: See TracBrowser for help on using the repository browser.