source: trunk/src/samhain.c@ 170

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

Plenty of compiler warnings fixed, SQL query length fixed, doc update.

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