xref: /netbsd/lib/libwrap/diag.c (revision 6550d01e)
1 /*	$NetBSD: diag.c,v 1.8 2004/09/07 13:20:40 jrf Exp $	*/
2 
3  /*
4   * Routines to report various classes of problems. Each report is decorated
5   * with the current context (file name and line number), if available.
6   *
7   * tcpd_warn() reports a problem and proceeds.
8   *
9   * tcpd_jump() reports a problem and jumps.
10   *
11   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
12   */
13 
14 #include <sys/cdefs.h>
15 #ifndef lint
16 #if 0
17 static char sccsid[] = "@(#) diag.c 1.1 94/12/28 17:42:20";
18 #else
19 __RCSID("$NetBSD: diag.c,v 1.8 2004/09/07 13:20:40 jrf Exp $");
20 #endif
21 #endif
22 
23 /* System libraries */
24 
25 #include <syslog.h>
26 #include <stdio.h>
27 #include <setjmp.h>
28 #include <string.h>
29 #include <errno.h>
30 
31 /* Local stuff */
32 
33 #include "tcpd.h"
34 #include "mystdarg.h"
35 
36 struct tcpd_context tcpd_context;
37 jmp_buf tcpd_buf;
38 
39 static void tcpd_diag __P((int, char *, char *, va_list))
40 	__attribute__((__format__(__printf__, 3, 0)));
41 
42 /* tcpd_diag - centralize error reporter */
43 
44 static void tcpd_diag(severity, tag, format, ap)
45 int     severity;
46 char   *tag;
47 char   *format;
48 va_list ap;
49 {
50     char    fmt[BUFSIZ];
51     char    buf[BUFSIZ];
52     int     i, o, oerrno;
53 
54     /* save errno in case we need it */
55     oerrno = errno;
56 
57     /* contruct the tag for the log entry */
58     if (tcpd_context.file)
59 	(void)snprintf(buf, sizeof buf, "%s: %s, line %d: ",
60 		tag, tcpd_context.file, tcpd_context.line);
61     else
62 	(void)snprintf(buf, sizeof buf, "%s: ", tag);
63 
64     /* change % to %% in tag before appending the format */
65     for (i = 0, o = 0; buf[i] != '\0'; ) {
66 	if (buf[i] == '%') {
67 	    fmt[o] = '%';
68 	    if (o < sizeof(fmt) - 1)
69 		o++;
70 	}
71 	fmt[o] = buf[i++];
72 	if (o < sizeof(fmt) - 1)
73 	    o++;
74     }
75 
76     /* append format and force null termination */
77     fmt[o] = '\0';
78     (void)strlcat(fmt, format, sizeof(fmt) - o);
79 
80     errno = oerrno;
81     vsyslog(severity, fmt, ap);
82 }
83 
84 /* tcpd_warn - report problem of some sort and proceed */
85 
86 void    VARARGS(tcpd_warn, char *, format)
87 {
88     va_list ap;
89 
90     VASTART(ap, char *, format);
91     tcpd_diag(LOG_ERR, "warning", format, ap);
92     VAEND(ap);
93 }
94 
95 /* tcpd_jump - report serious problem and jump */
96 
97 void    VARARGS(tcpd_jump, char *, format)
98 {
99     va_list ap;
100 
101     VASTART(ap, char *, format);
102     tcpd_diag(LOG_ERR, "error", format, ap);
103     VAEND(ap);
104     longjmp(tcpd_buf, AC_ERROR);
105 }
106