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