source: trunk/src/sh_error.c@ 278

Last change on this file since 278 was 272, checked in by katerina, 15 years ago

Fixes tickets #190, #191, #192, #193, and #194.

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