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