source: trunk/src/sh_error.c@ 494

Last change on this file since 494 was 488, checked in by katerina, 9 years ago

Fix for tickets #386 (silent check) and #387 (linux audit support).

File size: 42.9 KB
RevLine 
[1]1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 1999, 2000 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
[144]22/* Required on Linux to get the correct strerror_r function. Also
23 * for recursive mutexes (_XOPEN_SOURCE >= 500). Gives funny error
24 * on Solaris 10/gcc ('c99' compiler required - huh? Isn't gcc
25 * good enough?).
[132]26 */
[144]27#if !defined(__sun__) && !defined(__sun)
[132]28#define _XOPEN_SOURCE 600
[352]29#undef _GNU_SOURCE
[144]30#endif
[132]31#include <string.h>
[1]32#include <stdio.h>
33#include <stdlib.h>
34#include <stdarg.h>
35#include <ctype.h>
36#include <limits.h>
37#include <errno.h>
38
39/* Required on FreeBSD
40 */
41#include <sys/types.h>
42
43#ifdef HAVE_MEMORY_H
44#include <memory.h>
45#endif
46
47#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
48#include <sys/mman.h>
49#endif
50
51
52
53#include "samhain.h"
54
55#include "sh_cat.h"
56#include "sh_database.h"
57#include "sh_error.h"
58#include "sh_utils.h"
59#include "sh_unix.h"
60#include "sh_tiger.h"
[214]61#include "sh_nmail.h"
[481]62#include "sh_xfer.h"
[1]63#include "sh_prelude.h"
[140]64#include "sh_pthread.h"
[1]65
66#if defined(WITH_DATABASE)
67#include "sh_tools.h"
68#endif
69
70#if defined(WITH_EXTERNAL)
71#include "sh_extern.h"
72#endif
73
74#undef FIL__
75#define FIL__ _("sh_error.c")
76/*@-noret -compmempass@*/
77extern int clt_class;
78
[481]79int flag_err_debug = S_FALSE;
80int flag_err_info = S_FALSE;
[1]81
82int ShDFLevel[SH_ERR_T_END];
83
84typedef struct _log_t {
85 char file[SH_PATHBUF];
86 char format[SH_PATHBUF];
87 /*@null@*/char * msg;
88 size_t msg_len;
89 int severity;
90 int class;
91 int pid;
92 long status;
93 long line;
94 char timestamp[TIM_MAX];
95} sh_log_t;
96
97
98struct _errFlags errFlags;
99
100static int sh_error_init (void);
101
[481]102inline
103static const char * get_format(unsigned long msg_id, int * priority,
104 unsigned int * class);
[1]105
106static int sh_error_string (struct _log_t * lmsg, va_list vl);
107
[170]108extern int sh_log_console (/*@null@*/const char *message);
[1]109extern int sh_log_syslog (int severity, /*@null@*/char *message);
110extern int sh_log_file (/*@null@*/char *message,
111 /*@null@*/char * inet_peer);
112/* convert a string to a numeric priority
113 */
[22]114int sh_error_convert_level (const char * str_s);
[1]115
116static int IsInitialized = BAD;
117
118/* --- Only log to stderr. ---
119 */
120int OnlyStderr = S_TRUE;
121
[86]122/* --- Enable facilities not safe for closeall(). ---
123 */
124int enableUnsafe = S_FALSE;
[1]125
126/*********************************************
127 * utility functions for verifying entries
128 *********************************************/
129
[20]130int sh_error_verify (const char * s)
[1]131{
132 char * foo;
[133]133 char hashbuf[KEYBUF_SIZE];
[1]134
135 if (s[0] == '/')
136 {
[159]137 foo = sh_tiger_hash_gpg (s, TIGER_FILE, TIGER_NOLIM);
[1]138 fprintf (stdout, _("%s\n"), foo);
139 SH_FREE(foo);
140 }
141 else
142 {
143 fprintf (stdout, _("string=<%s>, hash=<%s>\n"),
144 s, sh_tiger_hash (s, TIGER_DATA,
[133]145 (unsigned long) sl_strlen(s),
146 hashbuf, sizeof(hashbuf))
147 );
[1]148 }
149 (void) fflush(stdout);
150 _exit (EXIT_SUCCESS);
151 /*@i@*/return 0;
152}
153
154
155
156/*********************************************
157 * end utility functions
158 *********************************************/
159
160void sh_error_only_stderr (int flag)
161{
162 OnlyStderr = flag;
163 return;
164}
165
[86]166void sh_error_enable_unsafe (int flag)
167{
168 enableUnsafe = flag;
169 return;
170}
171
[1]172static int dbg_store = 0;
173static int dbg_flag = 0;
174
175static
[170]176void compute_flag_err_debug(void)
[1]177{
178 if ((errFlags.loglevel & SH_ERR_ALL) != 0)
[481]179 flag_err_debug = S_TRUE;
[1]180 else if ((errFlags.printlevel & SH_ERR_ALL) != 0)
[481]181 flag_err_debug = S_TRUE;
[1]182 else if ((errFlags.maillevel & SH_ERR_ALL) != 0)
[481]183 flag_err_debug = S_TRUE;
[1]184 else if ((errFlags.exportlevel & SH_ERR_ALL) != 0)
[481]185 flag_err_debug = S_TRUE;
[1]186 else if ((errFlags.sysloglevel & SH_ERR_ALL) != 0)
[481]187 flag_err_debug = S_TRUE;
[1]188 else if ((errFlags.externallevel & SH_ERR_ALL) != 0)
[481]189 flag_err_debug = S_TRUE;
[1]190 else if ((errFlags.databaselevel & SH_ERR_ALL) != 0)
[481]191 flag_err_debug = S_TRUE;
[1]192 else if ((errFlags.preludelevel & SH_ERR_ALL) != 0)
[481]193 flag_err_debug = S_TRUE;
[1]194 else
[481]195 flag_err_debug = S_FALSE;
[1]196 return;
197}
198
199static
[170]200void compute_flag_err_info(void)
[1]201{
202 if ((errFlags.loglevel & SH_ERR_INFO) != 0)
[481]203 flag_err_info = S_TRUE;
[1]204 else if ((errFlags.printlevel & SH_ERR_INFO) != 0)
[481]205 flag_err_info = S_TRUE;
[1]206 else if ((errFlags.maillevel & SH_ERR_INFO) != 0)
[481]207 flag_err_info = S_TRUE;
[1]208 else if ((errFlags.exportlevel & SH_ERR_INFO) != 0)
[481]209 flag_err_info = S_TRUE;
[1]210 else if ((errFlags.sysloglevel & SH_ERR_INFO) != 0)
[481]211 flag_err_info = S_TRUE;
[1]212 else if ((errFlags.externallevel & SH_ERR_INFO) != 0)
[481]213 flag_err_info = S_TRUE;
[1]214 else if ((errFlags.databaselevel & SH_ERR_INFO) != 0)
[481]215 flag_err_info = S_TRUE;
[1]216 else if ((errFlags.preludelevel & SH_ERR_INFO) != 0)
[481]217 flag_err_info = S_TRUE;
[1]218 else
[481]219 flag_err_info = S_FALSE;
[1]220 return;
221}
222
[170]223void sh_error_dbg_switch(void)
[1]224{
225 if (dbg_flag == 0)
226 {
227 dbg_store = errFlags.printlevel;
228 errFlags.printlevel = (SH_ERR_ALL | SH_ERR_INFO | SH_ERR_NOTICE |
229 SH_ERR_WARN | SH_ERR_STAMP | SH_ERR_ERR |
230 SH_ERR_SEVERE | SH_ERR_FATAL);
231 dbg_flag = 1;
[481]232 flag_err_debug = S_TRUE;
[1]233 }
234 else {
235 errFlags.printlevel = dbg_store;
236 dbg_store = 0;
237 dbg_flag = 0;
238 compute_flag_err_debug();
239 }
240 return;
241}
242
[22]243static int sh_error_set_classmask (const char * str, int * facility_mask)
[1]244{
245 char * p;
246 int num = 0;
247 unsigned int i;
248 size_t len;
[22]249 char * c;
[1]250
251 SL_ENTER(_("sh_error_set_classmask"));
252
[22]253 if (str == NULL)
[1]254 SL_RETURN( -1, _("sh_error_set_classmask"));
255
256 if (IsInitialized == BAD)
257 (void) sh_error_init();
258
[22]259 if (str[0] == (char) 34)
260 ++str;
261 len = strlen(str);
262
263 c = SH_ALLOC(len+1);
264 sl_strlcpy(c, str, len+1);
265
[1]266 if (c[len-1] == (char) 34)
267 c[len-1] = '\0';
268
269 *facility_mask = 0;
270
271 do {
[131]272#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_STRTOK_R)
273 char * saveptr;
274 if (num == 0) {
275 p = strtok_r (c, " ,\t", &saveptr);
276 ++num;
277 } else {
278 p = strtok_r (NULL, " ,\t", &saveptr);
279 }
280#else
281 if (num == 0) {
282 p = strtok (c, " ,\t");
283 ++num;
284 } else {
[1]285 p = strtok (NULL, " ,\t");
[131]286 }
287#endif
[1]288
289 if (p == NULL)
290 break;
291
292 for (i = 0; i < SH_CLA_MAX; ++i)
293 {
294 if (i < SH_CLA_RAW_MAX) {
295 if (0 == strcmp(p, _(class_cat[i])))
296 *facility_mask |= (1 << i);
297 } else {
298 if (0 == strcmp(p, _(class_cat[SH_CLA_RAW_MAX + 0])))
299 *facility_mask |= OTHER_CLA;
300 if (0 == strcmp(p, _(class_cat[SH_CLA_RAW_MAX + 1])))
301 *facility_mask |= RUN_NEW;
302 if (0 == strcmp(p, _(class_cat[SH_CLA_RAW_MAX + 2])))
303 *facility_mask |= FIL_NEW;
304 if (0 == strcmp(p, _(class_cat[SH_CLA_RAW_MAX + 3])))
305 *facility_mask |= ERROR_CLA;
306 }
307 }
308
309 } while (p);
310
[22]311 SH_FREE(c);
[1]312 SL_RETURN( 0, _("sh_error_set_classmask"));
313}
314
[22]315int sh_error_log_mask (const char * c)
[1]316{
317 return (sh_error_set_classmask(c, &(errFlags.log_class)));
318}
[22]319int sh_error_mail_mask (const char * c)
[1]320{
321 return (sh_error_set_classmask(c, &(errFlags.mail_class)));
322}
[22]323int sh_error_print_mask (const char * c)
[1]324{
325 return (sh_error_set_classmask(c, &(errFlags.print_class)));
326}
[22]327int sh_error_export_mask (const char * c)
[1]328{
329 return (sh_error_set_classmask(c, &(errFlags.export_class)));
330}
[22]331int sh_error_syslog_mask (const char * c)
[1]332{
333 return (sh_error_set_classmask(c, &(errFlags.syslog_class)));
334}
[22]335int sh_error_external_mask (const char * c)
[1]336{
337 return (sh_error_set_classmask(c, &(errFlags.external_class)));
338}
[22]339int sh_error_database_mask (const char * c)
[1]340{
341 return (sh_error_set_classmask(c, &(errFlags.database_class)));
342}
[22]343int sh_error_prelude_mask (const char * c)
[1]344{
345 return (sh_error_set_classmask(c, &(errFlags.prelude_class)));
346}
347
348
349
[132]350char * sh_error_message (int tellme, char * str, size_t len)
[1]351{
352
[132]353#if defined(HAVE_STRERROR_R)
[352]354 if (len > 0) str[0] = '\0';
[132]355 strerror_r(tellme, str, len);
356 return str;
357#elif defined(HAVE_STRERROR)
358 sl_strlcpy(str, strerror(tellme), len);
359 return str;
[1]360#else
361
[132]362 char *p = NULL;
[1]363#ifdef EACCES
[132]364 if (tellme == EACCES) p = _("Permission denied.");
[1]365#endif
366#ifdef EAGAIN
[132]367 if (tellme == EAGAIN) p = _("Try again.");
[1]368#endif
369#ifdef EBADF
[132]370 if (tellme == EBADF) p = _("File descriptor in bad state.");
[1]371#endif
372#ifdef EEXIST
[132]373 if (tellme == EEXIST) p = _("File exists.");
[1]374#endif
375#ifdef EFAULT
[132]376 if (tellme == EFAULT) p = _("Bad address.");
[1]377#endif
378#ifdef EINVAL
[132]379 if (tellme == EINVAL) p = _("Invalid argument.");
[1]380#endif
381#ifdef EISDIR
[132]382 if (tellme == EISDIR) p = _("Is a directory.");
[1]383#endif
384#ifdef EINTR
[132]385 if (tellme == EINTR) p = _("System call was interrupted.");
[1]386#endif
387#ifdef EIO
[132]388 if (tellme == EIO) p = _("Low-level I/O error.");
[1]389#endif
390#ifdef ELOOP
[132]391 if (tellme == ELOOP) p = _("Too many symbolic links encountered.");
[1]392#endif
393#ifdef EMFILE
[132]394 if (tellme == EMFILE) p = _("Too many open files.");
[1]395#endif
396#ifdef EMLINK
[132]397 if (tellme == EMLINK) p = _("Too many links.");
[1]398#endif
399#ifdef ENAMETOOLONG
400 if (tellme == ENAMETOOLONG)
[132]401 p = _("File name too long.");
[1]402#endif
403#ifdef ENFILE
[132]404 if (tellme == ENFILE) p = _("File table overflow.");
[1]405#endif
406#ifdef ENOENT
[132]407 if (tellme == ENOENT) p = _("File does not exist.");
[1]408#endif
409#ifdef ENOMEM
[132]410 if (tellme == ENOMEM) p = _("Out of memory.");
[1]411#endif
412#ifdef ENOSPC
[132]413 if (tellme == ENOSPC) p = _("No space on device.");
[1]414#endif
415#ifdef ENOTDIR
[132]416 if (tellme == ENOTDIR) p = _("Not a directory.");
[1]417#endif
418#ifdef ENOTSOCK
[132]419 if (tellme == ENOTSOCK) p = _("Not a socket.");
[1]420#endif
421#ifdef EOPNOTSUPP
[132]422 if (tellme == EOPNOTSUPP) p = _("Socket is not of type SOCK_STREAM.");
[1]423#endif
424#ifdef EPERM
[132]425 if (tellme == EPERM) p = _("Permission denied.");
[1]426#endif
427#ifdef EPIPE
[132]428 if (tellme == EPIPE) p = _("No read on pipe.");
[1]429#endif
430#ifdef EROFS
[132]431 if (tellme == EROFS) p = _("Read-only file system.");
[1]432#endif
433#ifdef ETXTBSY
[132]434 if (tellme == ETXTBSY) p = _("Text file busy.");
[1]435#endif
436#ifdef EWOULDBLOCK
437 if (tellme == EWOULDBLOCK)
[132]438 p = _("No connections on non-blocking socket.");
[1]439#endif
440#ifdef EXDEV
[132]441 if (tellme == EXDEV) p = _("Not on same file system.");
[1]442#endif
[132]443 if (!p) p = _("Unknown error");
444 sl_strlcpy(str, p, len);
445 return str;
[1]446#endif /* ifndef HAVE_STRERROR */
447}
448
449
450/* switch off file log
451 */
452void sh_error_logoff()
453{
454 errFlags.HaveLog = BAD;
455 return;
456}
457
458/* switch on file log
459 */
460void sh_error_logrestore()
461{
462 errFlags.HaveLog = GOOD;
463 return;
464}
465
466/* --- Relate priority levels to literals. ---
467 */
468typedef struct eef
469{
[170]470 const char * str;
[1]471 int val;
472} eef_struc;
473
474static eef_struc eef_tab[] =
475{
476 { N_("none"), SH_ERR_NOT },
477 { N_("debug"), SH_ERR_ALL },
478 { N_("info"), SH_ERR_INFO },
479 { N_("notice"), SH_ERR_NOTICE },
480 { N_("warn"), SH_ERR_WARN },
481 { N_("mark"), SH_ERR_STAMP },
482 { N_("err"), SH_ERR_ERR },
483 { N_("crit"), SH_ERR_SEVERE },
484 { N_("alert"), SH_ERR_FATAL },
485#if defined(SH_WITH_SERVER)
486#define SH_EEF_MAX 10
487 { N_("inet"), SH_ERR_INET },
488#else
489#define SH_EEF_MAX 9
490#endif
491};
492
[22]493int sh_error_convert_level (const char * str_s)
[1]494{
495 int i;
496 int level = (-1);
497
498 SL_ENTER(_("sh_error_convert_level"));
499
500 if (str_s == NULL)
501 SL_RETURN( -1, _("sh_error_convert_level"));
502
503 for (i = 0; i < SH_EEF_MAX; ++i)
504 {
505 if (0 == sl_strncmp(str_s, _(eef_tab[i].str),
506 sl_strlen(eef_tab[i].str)))
507 {
508 level = eef_tab[i].val;
509 break;
510 }
511 }
512
513 SL_RETURN( level, _("sh_error_convert_level"));
514}
515
516
517/* --- Set severity levels. ---
518 */
[22]519int sh_error_set_iv (int iv, const char * str_s)
[1]520{
521 int level = (-1);
522
523 SL_ENTER(_("sh_error_set_iv"));
524
525 if (IsInitialized == BAD)
526 (void) sh_error_init();
527
528 level = sh_error_convert_level (str_s);
529
530 if (level == (-1))
531 {
532 sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
533 _("severity"),
534 str_s != NULL ? str_s : _("(NULL)"));
535 SL_RETURN (-1, _("sh_error_set_iv"));
536 }
537
538 if (iv > SH_ERR_T_START && iv < SH_ERR_T_END)
539 {
540 ShDFLevel[iv] = level;
541 }
542 else
543 {
544 sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALL,
545 _("severity"), (long) iv);
546 SL_RETURN (-1, _("sh_error_set_iv"));
547 }
548 SL_RETURN (0, _("sh_error_set_iv"));
549}
550
[20]551int sh_error_set_level(const char * str_in, int * facility)
[1]552{
553 register int i, j, f = BAD;
554
555 int old_facility;
[20]556 const char * str_s = str_in;
[1]557
558 SL_ENTER(_("sh_error_set_level"));
559
560 if (IsInitialized == BAD)
561 (void) sh_error_init();
562
563 old_facility = *facility;
564 *facility = 0;
565
566 checkstr:
567
568 if (str_s != NULL)
569 {
570 if (0 == sl_strncmp(str_s, _(eef_tab[0].str), sl_strlen(eef_tab[0].str)))
571 {
572 *facility |= eef_tab[0].val; /* This is 'none' */
573 for (i = 1; i < SH_EEF_MAX; ++i)
574 *facility &= ~eef_tab[i].val;
575 f = GOOD;
576 }
577 else if (str_s[0] == '*') /* all */
578 {
579 for (i = 1; i < SH_EEF_MAX; ++i)
580 *facility |= eef_tab[i].val;
581 f = GOOD;
582 }
583 else if (str_s[0] == '=')
584 {
585 for (i = 1; i < SH_EEF_MAX; ++i)
586 if (0 == sl_strncmp(&str_s[1], _(eef_tab[i].str),
587 sl_strlen(eef_tab[i].str)))
588 {
589 *facility |= eef_tab[i].val;
590 f = GOOD;
591 }
592 }
593 else if (str_s[0] == '!')
594 {
595 if (str_s[1] == '*' ||
596 0 == sl_strncmp(&str_s[1], _(eef_tab[1].str),
597 sl_strlen(eef_tab[1].str)))
598 {
599 *facility |= eef_tab[0].val; /* This is 'none' */
600 for (i = 1; i < SH_EEF_MAX; ++i)
601 *facility &= ~eef_tab[i].val;
602 f = GOOD;
603 }
604 else if (str_s[1] == '=')
605 {
606 for (i = 1; i < SH_EEF_MAX; ++i)
607 {
608 if (0 == sl_strncmp(&str_s[2], _(eef_tab[i].str),
609 sl_strlen(eef_tab[i].str)))
610 {
611 *facility &= ~eef_tab[i].val;
612 f = GOOD;
613 }
614 }
615 }
616 else
617 {
618 for (i = 1; i < SH_EEF_MAX; ++i)
619 {
620 if (0 == sl_strncmp(&str_s[1], _(eef_tab[i].str),
621 sl_strlen(eef_tab[i].str)))
622 {
623 for (j = i; j < SH_EEF_MAX; ++j)
624 {
625 *facility &= ~eef_tab[j].val;
626 }
627 f = GOOD;
628 }
629 }
630 }
631 }
632 else /* plain severity name */
633 {
634 for (i = 1; i < SH_EEF_MAX; ++i)
635 {
636 if (0 == sl_strncmp(str_s, _(eef_tab[i].str),
637 sl_strlen(eef_tab[i].str)))
638 {
639 for (j = i; j < SH_EEF_MAX; ++j)
640 {
641 *facility |= eef_tab[j].val;
642 }
643 f = GOOD;
644 break;
645 }
646 }
647 }
648 }
649
[383]650 if (!str_s)
651 {
652 SL_RETURN ((-1), _("sh_error_set_level"));
653 }
[1]654 /* skip to end of string
655 */
656 while (*str_s != '\0' && *str_s != ';' && *str_s != ',' &&
657 *str_s != ' ' && *str_s != '\t')
658 ++str_s;
659
660 /* skip seperator
661 */
662 while ((*str_s != '\0') &&
663 (*str_s == ';' || *str_s == ',' || *str_s == ' ' || *str_s == '\t'))
664 ++str_s;
665
666 if (*str_s != '\0')
667 {
668 f = BAD;
669 goto checkstr;
670 }
671
672 if (f == BAD)
673 {
674 *facility = old_facility;
675 sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
676 _("priority"), str_in);
677 SL_RETURN (-1, _("sh_error_set_level"));
678 }
679 compute_flag_err_debug();
680 compute_flag_err_info();
681 SL_RETURN (0, _("sh_error_set_level"));
682}
683
[27]684#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
[1]685/* set severity for TCP export
686 */
[20]687int sh_error_setexport(const char * str_s)
[1]688{
689 static int reject = 0;
690 if (reject == 1)
691 return (0);
692
693 if (sh.flag.opts == S_TRUE)
694 reject = 1;
695
696 return (sh_error_set_level(str_s, &errFlags.exportlevel));
697}
698#endif
699
700/* set severity for printing
701 */
702extern void dlog_set_active(int flag);
703
[20]704int sh_error_setprint(const char * str_s)
[1]705{
706 static int reject = 0;
707 int retval;
708
709 if (reject == 1)
710 return (0);
711
712 if (sh.flag.opts == S_TRUE)
713 reject = 1;
714
715 retval = sh_error_set_level(str_s, &errFlags.printlevel);
716
717 if (0 != (errFlags.printlevel & SH_ERR_INFO))
718 dlog_set_active(1);
719 if (0 != (errFlags.printlevel & SH_ERR_ALL))
720 dlog_set_active(2);
721 return retval;
722}
723
724
725/* set level for error logging
726 */
[20]727int sh_error_setlog(const char * str_s)
[1]728{
729 static int reject = 0;
730 if (reject == 1)
731 return (0);
732
733 if (sh.flag.opts == S_TRUE)
734 reject = 1;
735
736 return ( sh_error_set_level(str_s, &errFlags.loglevel) );
737}
738
739
740/* set severity for syslog
741 */
[20]742int sh_error_set_syslog (const char * str_s)
[1]743{
744 static int reject = 0;
745 if (reject == 1)
746 return (0);
747
748 if (sh.flag.opts == S_TRUE)
749 reject = 1;
750
751 return (sh_error_set_level(str_s, &errFlags.sysloglevel));
752}
753
754#if defined(WITH_EXTERNAL)
755/* set severity for external
756 */
[20]757int sh_error_set_external (const char * str_s)
[1]758{
759 static int reject = 0;
760 if (reject == 1)
761 return (0);
762
763 if (sh.flag.opts == S_TRUE)
764 reject = 1;
765
766 return (sh_error_set_level(str_s, &errFlags.externallevel));
767}
768#endif
769
770#if defined(WITH_DATABASE)
771/* set severity for database
772 */
[20]773int sh_error_set_database (const char * str_s)
[1]774{
775 static int reject = 0;
776 if (reject == 1)
777 return (0);
778
779 if (sh.flag.opts == S_TRUE)
780 reject = 1;
781
782 return (sh_error_set_level(str_s, &errFlags.databaselevel));
783}
784#endif
785
786#if defined(HAVE_LIBPRELUDE)
787/* set severity for prelude
788 */
[20]789int sh_error_set_prelude (const char * str_s)
[1]790{
791 static int reject = 0;
792
793 if (reject == 1)
794 return (0);
795
796 if (sh.flag.opts == S_TRUE)
797 reject = 1;
798
799 return sh_error_set_level(str_s, &errFlags.preludelevel);
800}
801#endif
802
803/* init or re-init log facilities that need it
804 */
[170]805void sh_error_fixup(void)
[1]806{
[181]807#if defined(HAVE_LIBPRELUDE)
[1]808 if ((errFlags.preludelevel & SH_ERR_NOT) == 0)
809 sh_prelude_init();
810 else
811 sh_prelude_stop();
812#endif
813#ifdef WITH_DATABASE
814 sh_database_reset();
815#endif
816 return;
817}
818
819/* to be called from sh_prelude_reset
820 */
[170]821void sh_error_init_prelude(void)
[1]822{
[181]823#if defined(HAVE_LIBPRELUDE)
[1]824 if ((errFlags.preludelevel & SH_ERR_NOT) == 0)
825 sh_prelude_init();
826 else
827 sh_prelude_stop();
828#endif
829 return;
830}
831
832
833/* set severity for mailing
834 */
[20]835int sh_error_setseverity (const char * str_s)
[1]836{
837 static int reject = 0;
838 if (reject == 1)
839 return (0);
840
841 if (sh.flag.opts == S_TRUE)
842 reject = 1;
843
844 return (sh_error_set_level(str_s, &errFlags.maillevel));
845}
846
847#ifdef SH_WITH_SERVER
848static char inet_peer[SH_MINIBUF] = { '\0' };
[283]849#ifdef HAVE_LIBPRELUDE
850static char inet_peer_ip[SH_IP_BUF] = { '\0' };
[1]851
[283]852void sh_error_set_peer_ip(const char * str)
853{
854 if (str == NULL)
855 inet_peer_ip[0] = '\0';
856 else
857 sl_strlcpy(inet_peer_ip, str, sizeof(inet_peer_ip));
858}
859#endif
860
[1]861void sh_error_set_peer(const char * str)
862{
863 if (str == NULL)
[11]864 inet_peer[0] = '\0';
[1]865 else
[283]866 sl_strlcpy(inet_peer, str, sizeof(inet_peer));
[1]867}
868#endif
869
[444]870#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
871#include "sh_checksum.h"
872static char * sh_error_replace(const char * msg)
873{
874 char * ret = NULL;
875
876 if (sh_tiger_get_hashtype () == SH_SHA256)
877 {
878 char * store = NULL;
879
880#ifdef SH_USE_XML
881 char c_end = '"';
882 char * str = _("chksum_old=\"");
883 char * str2 = _("chksum_new=\"");
884#else
885 char c_end = '>';
886 char * str = _("chksum_old=<");
887 char * str2 = _("chksum_new=<");
888#endif
889
890 ret = SHA256_ReplaceBaseByHex(msg, str, c_end);
891
892 if (ret) {
893 store = ret;
894 ret = SHA256_ReplaceBaseByHex(ret, str2, c_end);
895 if (ret)
896 SH_FREE(store);
897 else
898 ret = store;
899 } else {
900 ret = SHA256_ReplaceBaseByHex(msg, str2, c_end);
901 }
902 }
903 return ret;
904}
905static void sh_replace_free(char * msg)
906{
907 if (msg)
908 SH_FREE(msg);
909 return;
910}
911#else
912static char * sh_error_replace(const char * msg) { (void) msg; return NULL; }
913static void sh_replace_free(char * msg) { (void) msg; return; }
914#endif
915
[1]916/**********************************************************
917 **********************************************************
918 *
919 * -------- MAIN ERROR HANDLING FUNCTION -----------------
920 *
921 *
922 * this function should be called to report an error
923 *
924 **********************************************************
925 **********************************************************/
926
[140]927SH_MUTEX_RECURSIVE(mutex_err_handle);
928
[440]929void sh_error_handle (int sev1, const char * file, long line,
[1]930 long status, unsigned long msg_id, ...)
931{
932 va_list vl; /* argument list */
933 struct _log_t * lmsg;
934
935 int severity;
936 unsigned int class;
[481]937 const char * fmt;
[440]938 volatile int sev = sev1; /* Avoids the 'clobbered by longjmp' warning. */
[1]939
[170]940 int flag_inet;
[1]941
942#ifdef SH_WITH_SERVER
[140]943 int class_inet = clt_class; /* initialize from global */
[1]944 char local_inet_peer[SH_MINIBUF];
[283]945#ifdef HAVE_LIBPRELUDE
946 char local_inet_peer_ip[SH_IP_BUF];
947#endif
[1]948#endif
949
[27]950#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
[1]951 char * ex_msg;
952#endif
953#if defined(WITH_DATABASE)
954 char * escape_msg;
955#endif
956
[444]957 char * hexmsg = NULL;
958
[1]959 static int own_block = 0;
960
961 /*
962 * Block a facility for errors generated
963 * within that facility.
964 */
965 static int print_block = 0;
966#if defined(SH_WITH_MAIL)
967 static int mail_block = 0;
968#endif
969 static int syslog_block = 0;
970 static int log_block = 0;
[27]971#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
[1]972 static int export_block = 0;
973#endif
974#if defined(WITH_EXTERNAL)
975 static int external_block = 0;
976#endif
977#if defined(WITH_DATABASE)
978 static int database_block = 0;
979#endif
980#ifdef HAVE_LIBPRELUDE
981 static int prelude_block = 0;
982#endif
983
984 SL_ENTER(_("sh_error_handle"));
985
[140]986 SH_MUTEX_RECURSIVE_INIT(mutex_err_handle);
987 SH_MUTEX_RECURSIVE_LOCK(mutex_err_handle);
988
[1]989#ifdef SH_WITH_SERVER
990 /* copy the global string into a local array
991 */
992 if ((msg_id == MSG_TCP_MSG) && (inet_peer[0] != '\0'))
993 {
[283]994 sl_strlcpy(local_inet_peer, inet_peer, sizeof(local_inet_peer));
[1]995 sh_error_set_peer(NULL);
996 }
997 else
998 local_inet_peer[0] = '\0';
999
[283]1000#ifdef HAVE_LIBPRELUDE
1001 if ((msg_id == MSG_TCP_MSG) && (inet_peer_ip[0] != '\0'))
1002 {
1003 sl_strlcpy(local_inet_peer_ip, inet_peer_ip, sizeof(local_inet_peer_ip));
1004 sh_error_set_peer_ip(NULL);
1005 }
1006 else
1007 local_inet_peer_ip[0] = '\0';
1008#endif
1009
[1]1010 clt_class = (-1); /* reset global */
[140]1011#endif
[1]1012
[140]1013
[1]1014 if (own_block == 1)
1015 {
[140]1016 goto exit_here;
[1]1017 }
1018
1019 /* --- Initialize to default values. ---
1020 */
1021 if (IsInitialized == BAD)
1022 (void) sh_error_init();
1023
[140]1024 /* Returns pointer to (constant|thread-specific) static memory
[1]1025 */
1026 fmt = /*@i@*/get_format (msg_id, &severity, &class);
1027
[140]1028#ifdef SH_WITH_SERVER
[1]1029 if (class_inet != (-1))
1030 class = (unsigned int) class_inet;
[140]1031#endif
[1]1032
[140]1033 /* --- Consistency check. ---
1034 */
[1]1035 ASSERT((fmt != NULL), _("fmt != NULL"))
1036 if (fmt == NULL)
1037 {
1038 fprintf(stderr,
1039 _("ERROR: msg=<NULL format>, file=<%s>, line=<%ld>\n"),
1040 file, line);
[140]1041 goto exit_here;
[1]1042 }
1043
1044 /* --- Override the catalogue severity. ---
1045 */
1046 if (sev != (-1))
1047 severity = sev;
1048
[405]1049 /* --- Some statistics. ---
1050 */
1051#if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
1052 if ( ((1 << class) & ERROR_CLA) &&
1053 (severity & (SH_ERR_ERR|SH_ERR_SEVERE|SH_ERR_FATAL)))
1054 {
1055 ++sh.statistics.files_error;
1056 }
1057#endif
1058
[27]1059 /* these are messages from remote sources
1060 */
[1]1061 if ((severity & SH_ERR_INET) != 0)
1062 {
1063 flag_inet = S_TRUE;
1064 }
[170]1065 else
1066 {
1067 flag_inet = S_FALSE;
1068 }
[1]1069
1070 /* --- Messages not wanted for logging. ---
1071 */
1072 if ( ( (errFlags.printlevel & severity ) == 0 ||
1073 (errFlags.print_class & (1 << class)) == 0 ) &&
1074 ( (errFlags.loglevel & severity ) == 0 ||
1075 (errFlags.log_class & (1 << class)) == 0 ) &&
1076 ( (errFlags.sysloglevel & severity ) == 0 ||
1077 (errFlags.syslog_class & (1 << class)) == 0 ) &&
[27]1078#if defined(SH_WITH_CLIENT) || defined(SH_WITH_CLIENT)
[1]1079 ( (errFlags.exportlevel & severity ) == 0 ||
1080 (errFlags.export_class & (1 << class)) == 0 ) &&
1081#endif
1082#ifdef WITH_EXTERNAL
1083 ( (errFlags.externallevel & severity ) == 0 ||
1084 (errFlags.external_class & (1 << class)) == 0 ) &&
1085#endif
1086#ifdef HAVE_LIBPRELUDE
1087 ( (errFlags.preludelevel & severity ) == 0 ||
1088 (errFlags.prelude_class & (1 << class)) == 0 ) &&
1089#endif
1090#ifdef WITH_DATABASE
1091 ( (errFlags.databaselevel & severity ) == 0 ||
1092 (errFlags.database_class & (1 << class)) == 0 ) &&
1093#endif
1094 ( (errFlags.maillevel & severity ) == 0 ||
1095 (errFlags.mail_class & (1 << class)) == 0 )
1096#ifdef SH_WITH_SERVER
[27]1097 && (flag_inet == S_FALSE) /* still log messages from remote sources */
[1]1098#endif
1099 )
1100 {
[140]1101 goto exit_here;
[1]1102 }
1103
1104 if ((severity & SH_ERR_NOT) != 0)
1105 {
[140]1106 goto exit_here;
[1]1107 }
[488]1108
1109#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
1110 if ((sh_global_check_silent > SH_SILENT_STD) &&
1111 (((1 << FIL) & (1 << class)) != 0))
1112 {
1113 goto exit_here;
1114 }
1115#endif
[1]1116
1117 /* Allocate space for the message.
1118 */
1119 own_block = 1;
1120 lmsg = (struct _log_t *) SH_ALLOC(sizeof(struct _log_t));
1121 MLOCK( (char *) lmsg, sizeof(struct _log_t));
1122 /*@i@*/lmsg->msg = NULL;
1123
1124 /*@i@*/(void) sl_strlcpy(lmsg->format, fmt, SH_PATHBUF);
1125 (void) sl_strlcpy(lmsg->file, file, SH_PATHBUF);
1126 lmsg->severity = severity;
1127 lmsg->class = (int) class;
1128 lmsg->line = line;
1129 lmsg->status = status;
1130
1131 /* Format the log message with timestamp etc.
1132 * Allocate lmsg->msg
1133 */
1134 va_start (vl, msg_id);
1135 (void) sh_error_string (lmsg, vl);
1136 va_end (vl);
1137 own_block = 0;
1138
[444]1139 hexmsg = sh_error_replace(lmsg->msg);
[1]1140
1141 /* Log to stderr.
1142 */
1143 if ( ((errFlags.printlevel & severity) != 0 &&
1144 (errFlags.print_class & (1 << class)) != 0 &&
1145 (errFlags.printlevel & SH_ERR_NOT) == 0)
1146#ifdef SH_WITH_SERVER
1147 || (flag_inet == S_TRUE)
1148#endif
1149 )
1150 {
1151 if (print_block == 0 && (errFlags.printlevel & SH_ERR_NOT) == 0)
1152 {
1153 /* no truncation
1154 */
1155 print_block = 1;
1156 TPT(( 0, FIL__, __LINE__, lmsg->msg));
1157 /*
1158 * Reports first error after failure. Always tries.
1159 */
[444]1160 (void) sh_log_console (hexmsg ? hexmsg : lmsg->msg);
[1]1161 print_block = 0;
1162 }
1163 }
1164
1165
1166 /* Full logging enabled.
1167 */
[86]1168 if (OnlyStderr == S_FALSE) /* full error logging enabled */
[1]1169 {
1170
1171 /* Log to syslog.
1172 */
1173 if ( (errFlags.sysloglevel & severity) != 0 &&
1174 (errFlags.syslog_class & (1 << class)) != 0 &&
1175#ifndef INET_SYSLOG
1176 (flag_inet != S_TRUE) && /* !inet->syslog */
1177#endif
1178 (errFlags.sysloglevel & SH_ERR_NOT) == 0 )
1179 {
1180 /* will truncate to 1023 bytes
1181 */
1182 if (syslog_block == 0)
1183 {
1184 syslog_block = 1;
1185 /*
1186 * Ignores errors. Always tries.
1187 */
[444]1188 (void) sh_log_syslog (lmsg->severity, hexmsg ? hexmsg : lmsg->msg);
[1]1189 syslog_block = 0;
1190 }
1191 }
1192
1193#if defined(WITH_EXTERNAL)
1194 /*
1195 * -- external facility
1196 */
1197 if ((errFlags.externallevel & severity) != 0 &&
1198 (errFlags.external_class & (1 << class)) != 0 &&
1199 (errFlags.externallevel & SH_ERR_NOT) == 0 &&
1200 class != AUD)
1201 {
1202 if (external_block == 0)
1203 {
1204 /* no truncation
1205 */
1206 external_block = 1;
1207 /*
1208 * Reports first error after failure. Always tries.
1209 */
[444]1210 (void) sh_ext_execute ('l', 'o', 'g', hexmsg ? hexmsg : lmsg->msg, 0);
[1]1211 external_block = 0;
1212 }
1213 }
1214#endif
1215
1216#if defined(WITH_DATABASE)
1217 /*
1218 * -- database facility
1219 */
1220 if ((errFlags.databaselevel & severity) != 0 &&
1221 (errFlags.database_class & (1 << class)) != 0 &&
1222 (errFlags.databaselevel & SH_ERR_NOT) == 0 &&
1223 class != AUD)
1224 {
[86]1225 if (database_block == 0 && enableUnsafe == S_TRUE)
[1]1226 {
1227 /* truncates; query_max is 16k
1228 */
1229 database_block = 1;
1230#ifndef SH_STANDALONE
1231 if (msg_id == MSG_TCP_MSG
1232#ifdef INET_SYSLOG
1233 || msg_id == MSG_INET_SYSLOG
1234#endif
1235 )
1236 {
1237 /* do not escape twice
1238 */
1239 /*
1240 * Reports failure every 60 min. Always tries.
1241 */
1242 (void) sh_database_insert (lmsg->msg);
1243 }
1244 else
1245#endif
1246 {
1247 escape_msg = sh_tools_safe_name(lmsg->msg, 0);
1248 /*
1249 * Reports failure every 60 min. Always tries.
1250 */
1251 (void) sh_database_insert (escape_msg);
1252 SH_FREE(escape_msg);
1253 }
1254 database_block = 0;
1255 }
1256 }
1257#endif
1258
1259 /****************************************************
1260 * Optionally include client code for TCP forwarding
1261 * to log server
1262 ****************************************************/
[27]1263#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
[1]1264 /* Export by TCP.
1265 */
[27]1266
1267 if ( ((errFlags.exportlevel & severity ) != 0 &&
1268 (errFlags.export_class & (1 << class)) != 0 &&
1269 (errFlags.exportlevel & SH_ERR_NOT) == 0 &&
1270 class != AUD )
1271#ifdef SH_WITH_SERVER
[65]1272 /* always log inet to export */
1273 || (flag_inet == S_TRUE && sh.srvexport.name[0] != '\0')
[27]1274#endif
1275 /* sh.flag.isserver != GOOD && */
1276 /* (flag_inet == S_FALSE) */ /* don't log inet to export */
1277 )
[1]1278 {
1279 if (export_block == 0)
1280 {
1281 int retval;
[22]1282 size_t ex_len;
[1]1283
1284 /* will truncate to 65280 bytes
1285 */
1286 export_block = 1;
[34]1287 /* ex_len = 64 + sl_strlen(lmsg->msg) + 1; */
1288 ex_len = sl_strlen(lmsg->msg);
1289 if (sl_ok_adds(ex_len, 65))
1290 ex_len = 64 + ex_len + 1;
[22]1291 ex_msg = SH_ALLOC (ex_len);
1292
1293 sl_snprintf(ex_msg, ex_len, _("%d?%u?%s"),
1294 severity, class, lmsg->msg);
[481]1295 retval = sh_xfer_report (ex_msg);
[1]1296 SH_FREE(ex_msg);
1297 export_block = 0;
1298 if (retval == -2)
1299 {
1300 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_QUEUE_FULL,
1301 _("log server"));
1302 }
1303 }
1304 }
1305#endif
1306
1307
1308 /* Log to mail.
1309 */
1310#if defined(SH_WITH_MAIL)
1311 if ((errFlags.maillevel & severity ) != 0 &&
1312 (errFlags.mail_class & (1 << class)) != 0 &&
1313 (errFlags.maillevel & SH_ERR_NOT) == 0 &&
1314 class != AUD &&
1315 (flag_inet == S_FALSE) ) /* don't log inet to email */
1316 {
1317 if (mail_block == 0)
1318 {
1319 int retval;
1320
1321 /* will truncate to 998 bytes
1322 */
1323 mail_block = 1;
1324
[214]1325 BREAKEXIT(sh_nmail_msg);
[1]1326 if ( (severity & SH_ERR_FATAL) == 0)
[444]1327 retval = sh_nmail_pushstack (severity, hexmsg ? hexmsg : lmsg->msg, NULL);
[1]1328 else
[444]1329 retval = sh_nmail_msg (severity, hexmsg ? hexmsg : lmsg->msg, NULL);
[1]1330
1331 mail_block = 0;
1332 if (retval == -2)
1333 {
1334 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_QUEUE_FULL,
1335 _("email"));
1336 }
1337 }
1338 }
1339#endif
1340
1341#ifdef HAVE_LIBPRELUDE
1342 if (((errFlags.preludelevel & severity ) != 0 &&
1343 (errFlags.prelude_class & (1 << class)) != 0 &&
1344 (errFlags.preludelevel & SH_ERR_NOT) == 0 &&
1345 (class != AUD))
1346#ifdef SH_WITH_SERVER
1347 || (flag_inet == S_TRUE)
1348#endif
1349 )
1350 {
[86]1351 if (prelude_block == 0 && enableUnsafe == S_TRUE)
[1]1352 {
1353 /* will truncate to 998 bytes
1354 */
1355 prelude_block = 1;
1356
1357 BREAKEXIT(sh_prelude_alert);
1358 /*
1359 * Reports first error after failure. Always tries.
1360 */
[283]1361#if defined(HAVE_LIBPRELUDE) && defined(SH_WITH_SERVER)
1362 (void) sh_prelude_alert (severity, (int) class,
[444]1363 hexmsg ? hexmsg : lmsg->msg, lmsg->status, msg_id,
[283]1364 local_inet_peer_ip);
1365#else
1366 (void) sh_prelude_alert (severity, (int) class,
[444]1367 hexmsg ? hexmsg : lmsg->msg, lmsg->status, msg_id,
[283]1368 NULL);
1369#endif
[1]1370 prelude_block = 0;
1371 }
1372 }
1373#endif
1374
1375 /* Log to logfile
1376 */
1377
1378 if ( ( ( (errFlags.loglevel & severity) != 0 &&
1379 (errFlags.log_class & (1 << class)) != 0 &&
1380 (errFlags.loglevel & SH_ERR_NOT) == 0 )
1381#ifdef SH_WITH_SERVER
1382 || (flag_inet == S_TRUE)
1383#endif
1384 ) &&
1385 class != AUD &&
1386 (errFlags.HaveLog != BAD) && /* temporary switched off */
1387 (severity & SH_ERR_NOT) == 0 /* paranoia */
1388 )
1389 {
1390 if (log_block == 0)
1391 {
1392 /* no truncation
1393 */
1394 log_block = 1;
1395 BREAKEXIT(sh_log_file);
1396#ifdef SH_WITH_SERVER
1397 if (0 != sl_ret_euid())
1398 {
1399 /*
1400 * Reports first error after failure. Always tries.
1401 */
1402 if (local_inet_peer[0] == '\0')
1403 (void) sh_log_file (lmsg->msg, NULL);
1404 else
[283]1405 (void) sh_log_file (lmsg->msg, local_inet_peer);
[1]1406 }
1407#else
[444]1408 (void) sh_log_file (hexmsg ? hexmsg : lmsg->msg, NULL);
[1]1409#endif
1410 /* sh_log_file (lmsg->msg); */
1411 log_block = 0;
1412 }
1413 }
1414
1415 }
1416
1417 /* Cleanup.
1418 */
1419 own_block = 1;
1420
1421 if (lmsg->msg)
1422 SH_FREE( lmsg->msg );
[444]1423 sh_replace_free(hexmsg);
[1]1424
1425 memset ( lmsg, (int) '\0', sizeof(struct _log_t) );
1426 MUNLOCK( (char *) lmsg, sizeof(struct _log_t) );
1427 SH_FREE( lmsg );
1428 own_block = 0;
1429
[140]1430 exit_here:
[144]1431 ; /* label at end of compound statement */
[140]1432 SH_MUTEX_RECURSIVE_UNLOCK(mutex_err_handle);
1433
[1]1434 /*@i@*/SL_RET0(_("sh_error_handle"));
1435/*@i@*/}
1436
[272]1437#if defined(SH_WITH_MAIL)
1438void sh_error_mail (const char * alias, int sev,
1439 const char * file, long line,
1440 long status, unsigned long msg_id, ...)
1441{
1442 va_list vl; /* argument list */
1443 struct _log_t * lmsg;
[1]1444
[272]1445 int severity;
1446 unsigned int class;
[481]1447 const char * fmt;
[272]1448 int retval;
1449
1450 SL_ENTER(_("sh_error_mail"));
1451
1452 /* Returns pointer to (constant|thread-specific) static memory
1453 */
1454 fmt = /*@i@*/get_format (msg_id, &severity, &class);
1455
1456 if (!fmt)
1457 {
1458 SL_RET0(_("sh_error_mail"));
1459 }
1460
1461 /* --- Override the catalogue severity. ---
1462 */
1463 if (sev != (-1))
1464 severity = sev;
1465
1466 /* --- Build the message. ---
1467 */
1468 lmsg = (struct _log_t *) SH_ALLOC(sizeof(struct _log_t));
1469 MLOCK( (char *) lmsg, sizeof(struct _log_t));
1470 /*@i@*/lmsg->msg = NULL;
1471
1472 /*@i@*/(void) sl_strlcpy(lmsg->format, fmt, SH_PATHBUF);
1473 (void) sl_strlcpy(lmsg->file, file, SH_PATHBUF);
1474 lmsg->severity = severity;
1475 lmsg->class = (int) class;
1476 lmsg->line = line;
1477 lmsg->status = status;
1478
1479 /* Format the log message with timestamp etc.
1480 * Allocate lmsg->msg
1481 */
1482 va_start (vl, msg_id);
1483 (void) sh_error_string (lmsg, vl);
1484 va_end (vl);
1485
1486 if ( (severity & SH_ERR_FATAL) == 0)
1487 retval = sh_nmail_pushstack (severity, lmsg->msg, alias);
1488 else
1489 retval = sh_nmail_msg (severity, lmsg->msg, alias);
1490
1491 if (retval == -2)
1492 {
1493 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_QUEUE_FULL,
1494 _("email"));
1495 }
1496 SL_RET0(_("sh_error_mail"));
1497}
1498#else
1499void sh_error_mail (const char * alias, int sev,
1500 const char * file, long line,
1501 long status, unsigned long msg_id, ...)
1502{
1503 (void) alias;
1504 (void) sev;
1505 (void) file;
1506 (void) line;
1507 (void) status;
1508 (void) msg_id;
1509
1510 return;
1511}
1512/* defined(SH_WITH_MAIL) */
1513#endif
1514
[1]1515/* -------------------------
1516 *
1517 * private functions below
1518 *
1519 * -------------------------
1520 */
1521
1522
1523/* --- Get the format from the message catalog. ---
1524 */
1525/*@owned@*/ /*@null@*/inline
[481]1526static const char * get_format(unsigned long msg_id, /*@out@*/ int * priority,
[1]1527 /*@out@*/unsigned int * class)
1528{
1529 int i = 0;
1530
1531 SL_ENTER(_("get_format"));
1532 while (1 == 1)
1533 {
1534 if ( msg_cat[i].format == NULL )
1535 break;
1536
1537 if ( (unsigned long) msg_cat[i].id == msg_id)
1538 {
1539 *priority = (int) msg_cat[i].priority;
1540 *class = (unsigned int) msg_cat[i].class;
[481]1541 SL_RETURN (((const char *) _(msg_cat[i].format)), _("get_format"));
[1]1542 }
1543 ++i;
1544 }
1545 *priority = SH_ERR_ERR;
1546 *class = ERR;
1547 SL_RETURN (NULL, _("get_format"));
1548}
1549
1550/*@null@*//*@only@*/static char * ehead_format = NULL;
1551
1552/* allocate space for user-defined message header
1553 */
[22]1554int sh_error_ehead (/*@null@*/const char * str_s)
[1]1555{
1556 size_t size;
[22]1557 const char * s;
[1]1558
1559 SL_ENTER(_("sh_error_ehead"));
1560
1561 if (str_s == NULL)
1562 {
1563 SL_RETURN (-1, _("sh_error_ehead"));
1564 }
1565
1566 /* ascii 34 ist t\"ttelchen
1567 */
1568 /*@i@*/ if (str_s[0] == 34) s = &str_s[1];
1569 else s = str_s;
1570
1571 size = /*@i@*/strlen(s);
[22]1572 if (/*@i@*/s[size-1] == (char) 34) --size; /* truncate */
[1]1573
1574 if (ehead_format != NULL)
1575 SH_FREE(ehead_format);
1576
1577 /*@i@*/ehead_format = SH_ALLOC(size+1);
1578 /*@i@*/ (void) sl_strlcpy(ehead_format, s, size+1);
1579
1580 SL_RETURN( 0, _("sh_error_ehead"));
1581}
1582
[481]1583#if !defined(VA_COPY)
1584#if defined(__GNUC__) && defined(__PPC__) && (defined(_CALL_SYSV) || defined(_WIN32))
1585#define VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
1586#elif defined(VA_COPY_AS_ARRAY)
1587#define VA_COPY(ap1, ap2) memmove ((ap1), (ap2), sizeof (va_list))
1588#else /* va_list is a pointer */
1589#define VA_COPY(ap1, ap2) ((ap1) = (ap2))
1590#endif
1591#endif
[1]1592
[481]1593
[1]1594/* print an error into string
1595 */
1596static int sh_error_string (struct _log_t * lmsg, va_list vl)
1597{
1598 size_t len;
1599 int required;
1600 unsigned long line;
1601 char sev[16] = "";
1602 char cla[16] = "";
1603 char tst[64] = "";
1604 char *p;
1605 va_list vl2;
1606
1607 st_format rep_ehead_tab[] = {
1608 { 'S', S_FMT_STRING, 0, 0, NULL}, /* severity */
1609 { 'T', S_FMT_STRING, 0, 0, NULL}, /* timestamp */
1610 { 'F', S_FMT_STRING, 0, 0, NULL}, /* file */
1611 { 'L', S_FMT_ULONG, 0, 0, NULL}, /* line */
1612 { 'C', S_FMT_STRING, 0, 0, NULL}, /* class */
1613 { 'E', S_FMT_LONG, 0, 0, NULL}, /* status */
1614 {'\0', S_FMT_ULONG, 0, 0, NULL},
1615 };
1616
1617 SL_ENTER(_("sh_error_string"));
1618
1619 if (ehead_format == NULL)
1620 {
1621 ehead_format = SH_ALLOC(64);
1622#ifdef SH_USE_XML
1623 if ((errFlags.printlevel & SH_ERR_ALL) == 0)
1624 (void) sl_strlcpy(ehead_format,
1625 _("<log sev=\"%S\" tstamp=\"%T\" "), 64);
1626 else
1627 (void) sl_strlcpy(ehead_format,
1628 _("<log sev=\"%S\" tstamp=\"%T\" p.f=\"%F\" p.l=\"%L\" p.s=\"%E\" "), 64);
1629#else
1630 if ((errFlags.printlevel & SH_ERR_ALL) == 0)
1631 (void) sl_strlcpy(ehead_format, _("%S %T "), 64);
1632 else
1633 (void) sl_strlcpy(ehead_format, _("%S %T (%F, %L, %E) "), 64);
1634#endif
1635 }
1636
1637 /* header of error message
1638 */
1639#ifdef SH_USE_XML
1640 if ( (lmsg->severity & SH_ERR_INET) != 0)
1641 (void) sl_strlcpy (sev, _("RCVT"), 11);
1642 else if ( (lmsg->severity & SH_ERR_ALL) != 0)
1643 (void) sl_strlcpy (sev, _("DEBG"), 11);
1644 else if ( (lmsg->severity & SH_ERR_INFO) != 0)
1645 (void) sl_strlcpy (sev, _("INFO"), 11);
1646 else if ( (lmsg->severity & SH_ERR_NOTICE) != 0)
1647 (void) sl_strlcpy (sev, _("NOTE"), 11);
1648 else if ( (lmsg->severity & SH_ERR_WARN) != 0)
1649 (void) sl_strlcpy (sev, _("WARN"), 11);
1650 else if ( (lmsg->severity & SH_ERR_STAMP) != 0)
1651 (void) sl_strlcpy (sev, _("MARK"), 11);
1652 else if ( (lmsg->severity & SH_ERR_ERR) != 0)
1653 (void) sl_strlcpy (sev, _("ERRO"), 11);
1654 else if ( (lmsg->severity & SH_ERR_SEVERE) != 0)
1655 (void) sl_strlcpy (sev, _("CRIT"), 11);
1656 else if ( (lmsg->severity & SH_ERR_FATAL) != 0)
1657 (void) sl_strlcpy (sev, _("ALRT"), 11);
1658 else {
1659 (void) sl_strlcpy (sev, _("????"), 11);
1660#else
1661#if defined(INET_SYSLOG)
1662 if ( (lmsg->severity & SH_ERR_INET) != 0)
1663 (void) sl_strlcpy (sev, _("<NET> : "), 11);
1664#else
1665 if ( (lmsg->severity & SH_ERR_INET) != 0)
1666 (void) sl_strlcpy (sev, _("<TCP> : "), 11);
1667#endif
1668 else if ( (lmsg->severity & SH_ERR_ALL) != 0)
1669 (void) sl_strlcpy (sev, _("DEBUG : "), 11);
1670 else if ( (lmsg->severity & SH_ERR_INFO) != 0)
1671 (void) sl_strlcpy (sev, _("INFO : "), 11);
1672 else if ( (lmsg->severity & SH_ERR_NOTICE) != 0)
1673 (void) sl_strlcpy (sev, _("NOTICE : "), 11);
1674 else if ( (lmsg->severity & SH_ERR_WARN) != 0)
1675 (void) sl_strlcpy (sev, _("WARN : "), 11);
1676 else if ( (lmsg->severity & SH_ERR_STAMP) != 0)
1677 (void) sl_strlcpy (sev, _("MARK : "), 11);
1678 else if ( (lmsg->severity & SH_ERR_ERR) != 0)
1679 (void) sl_strlcpy (sev, _("ERROR : "), 11);
1680 else if ( (lmsg->severity & SH_ERR_SEVERE) != 0)
1681 (void) sl_strlcpy (sev, _("CRIT : "), 11);
1682 else if ( (lmsg->severity & SH_ERR_FATAL) != 0)
1683 (void) sl_strlcpy (sev, _("ALERT : "), 11);
1684 else {
1685 (void) sl_strlcpy (sev, _("??? : "), 11);
1686#endif
1687 }
1688
[132]1689 (void) sh_unix_time (0, tst, 64);
[1]1690 line = (unsigned long) lmsg->line;
1691 (void) sl_strlcpy (cla, _(class_cat[lmsg->class]), 11);
1692
1693 /*@i@*/rep_ehead_tab[0].data_str = sev;
1694 /*@i@*/rep_ehead_tab[1].data_str = tst;
1695 /*@i@*/rep_ehead_tab[2].data_str = lmsg->file;
1696 /*@i@*/rep_ehead_tab[3].data_ulong = line;
1697 /*@i@*/rep_ehead_tab[4].data_str = cla;
1698 /*@i@*/rep_ehead_tab[5].data_long = lmsg->status;
1699
1700 p = /*@i@*/sh_util_formatted(ehead_format, rep_ehead_tab);
1701
1702 /* --- copy the header to lmsg->msg ---
1703 */
1704 /*@i@*/lmsg->msg = SH_ALLOC(SH_BUFSIZE);
1705 lmsg->msg_len = SH_BUFSIZE;
1706
1707 if (p)
1708 {
1709 (void) sl_strlcpy (lmsg->msg, p, SH_BUFSIZE);
1710 SH_FREE(p);
1711 }
1712 else
1713 {
1714 lmsg->msg[0] = '\0';
1715 }
1716
1717
1718 /* --- copy message to lmsg->msg ---
1719 */
1720 if ( NULL == strchr(lmsg->format, '%') )
1721 {
1722 (void) sl_strlcat (lmsg->msg, lmsg->format, (size_t) lmsg->msg_len);
1723 }
1724 else
1725 {
[481]1726 /* use VA_COPY */
1727 /*@i@*/VA_COPY(vl2, vl);
[1]1728 len = sl_strlen(lmsg->msg);
1729 /*@i@*/required = sl_vsnprintf(&(lmsg->msg[len]),
[22]1730 (lmsg->msg_len - len), lmsg->format, vl);
[170]1731
[34]1732 if ((required >= 0) &&
1733 sl_ok_adds(required, len) &&
1734 sl_ok_adds((required+len), 4) &&
1735 ((required + len) > (lmsg->msg_len - 4)) )
[1]1736 {
1737 /*@i@*/p = SH_ALLOC(required + len + 4);
1738 (void) sl_strlcpy (p, lmsg->msg, required + len + 1);
1739 SH_FREE(lmsg->msg);
1740 lmsg->msg = p;
1741 lmsg->msg_len = required + len + 4;
1742 (void) sl_vsnprintf(&(lmsg->msg[len]),
[170]1743 (required + 1), lmsg->format, vl2);
[1]1744 }
1745 va_end(vl2);
1746 }
1747
1748#ifdef SH_USE_XML
1749 /* closing tag
1750 */
1751 if (lmsg->msg[sl_strlen(lmsg->msg)-1] != '>')
1752 (void) sl_strlcat (lmsg->msg, _(" />"), lmsg->msg_len);
1753#endif
1754
1755 SL_RETURN(0, _("sh_error_string"));
1756}
1757
1758
1759
1760
1761/* --- Initialize. ---
1762 */
1763static int sh_error_init ()
1764{
1765 register int j;
1766
1767 SL_ENTER(_("sh_error_init"));
1768
1769 errFlags.debug = 0;
1770 errFlags.HaveLog = GOOD;
1771 errFlags.sysloglevel = SH_ERR_NOT;
1772#if defined(SH_STEALTH)
1773 errFlags.loglevel = SH_ERR_NOT;
1774#else
1775 errFlags.loglevel = (SH_ERR_STAMP | SH_ERR_ERR | SH_ERR_SEVERE |
1776 SH_ERR_FATAL);
1777#endif
1778 errFlags.externallevel = SH_ERR_NOT;
1779 errFlags.databaselevel = SH_ERR_NOT;
1780 errFlags.preludelevel = SH_ERR_NOT;
1781 errFlags.maillevel = SH_ERR_FATAL;
1782#if defined(SH_STEALTH)
1783 errFlags.printlevel = SH_ERR_NOT;
1784#else
1785 errFlags.printlevel = (SH_ERR_INFO | SH_ERR_NOTICE | SH_ERR_WARN |
1786 SH_ERR_STAMP | SH_ERR_ERR | SH_ERR_SEVERE |
1787 SH_ERR_FATAL);
[481]1788 flag_err_info = S_TRUE;
[1]1789#endif
[65]1790
1791#if defined(SH_WITH_SERVER)
1792 errFlags.exportlevel = SH_ERR_NOT;
1793#else
[1]1794 errFlags.exportlevel = (SH_ERR_STAMP | SH_ERR_ERR | SH_ERR_SEVERE |
1795 SH_ERR_FATAL);
[65]1796#endif
[1]1797
1798 errFlags.log_class = 0xFFFF;
1799 errFlags.print_class = 0xFFFF;
1800 errFlags.mail_class = 0xFFFF;
1801 errFlags.export_class = 0xFFFF;
1802 errFlags.syslog_class = 0xFFFF;
1803 errFlags.external_class = 0xFFFF;
1804 errFlags.database_class = 0xFFFF;
1805 errFlags.prelude_class = 0xFFFF;
1806
1807
1808 for (j = 0; j < SH_ERR_T_END; ++j)
1809 ShDFLevel[j] = SH_ERR_SEVERE;
1810
1811 IsInitialized = GOOD;
1812 SL_RETURN (0, _("sh_error_init"));
1813}
Note: See TracBrowser for help on using the repository browser.