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