xref: /openbsd-src/usr.sbin/snmpd/log.c (revision cb39b41371628601fbe4c618205356d538b9d08a)
1 /*	$OpenBSD: log.c,v 1.8 2015/02/08 23:28:48 tedu Exp $	*/
2 
3 /*
4  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/tree.h>
23 
24 #include <netinet/in.h>
25 #include <netinet/ip.h>
26 #include <net/if.h>
27 
28 #include <arpa/inet.h>
29 
30 #include <errno.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <syslog.h>
36 #include <event.h>
37 #include <netdb.h>
38 
39 #include <openssl/ssl.h>
40 
41 #include "snmpd.h"
42 
43 int	 debug;
44 int	 verbose;
45 
46 void	 vlog(int, const char *, va_list);
47 void	 logit(int, const char *, ...);
48 
49 void
50 log_init(int n_debug)
51 {
52 	extern char	*__progname;
53 
54 	debug = n_debug;
55 	verbose = n_debug;
56 
57 	if (!debug)
58 		openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
59 
60 	tzset();
61 }
62 
63 void
64 log_verbose(int v)
65 {
66 	verbose = v;
67 }
68 
69 void
70 logit(int pri, const char *fmt, ...)
71 {
72 	va_list	ap;
73 
74 	va_start(ap, fmt);
75 	vlog(pri, fmt, ap);
76 	va_end(ap);
77 }
78 
79 void
80 vlog(int pri, const char *fmt, va_list ap)
81 {
82 	char	*nfmt;
83 
84 	if (debug) {
85 		/* best effort in out of mem situations */
86 		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
87 			vfprintf(stderr, fmt, ap);
88 			fprintf(stderr, "\n");
89 		} else {
90 			vfprintf(stderr, nfmt, ap);
91 			free(nfmt);
92 		}
93 		fflush(stderr);
94 	} else
95 		vsyslog(pri, fmt, ap);
96 }
97 
98 
99 void
100 log_warn(const char *emsg, ...)
101 {
102 	char	*nfmt;
103 	va_list	 ap;
104 
105 	/* best effort to even work in out of memory situations */
106 	if (emsg == NULL)
107 		logit(LOG_CRIT, "%s", strerror(errno));
108 	else {
109 		va_start(ap, emsg);
110 
111 		if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
112 			/* we tried it... */
113 			vlog(LOG_CRIT, emsg, ap);
114 			logit(LOG_CRIT, "%s", strerror(errno));
115 		} else {
116 			vlog(LOG_CRIT, nfmt, ap);
117 			free(nfmt);
118 		}
119 		va_end(ap);
120 	}
121 }
122 
123 void
124 log_warnx(const char *emsg, ...)
125 {
126 	va_list	 ap;
127 
128 	va_start(ap, emsg);
129 	vlog(LOG_CRIT, emsg, ap);
130 	va_end(ap);
131 }
132 
133 void
134 log_info(const char *emsg, ...)
135 {
136 	va_list	 ap;
137 
138 	va_start(ap, emsg);
139 	vlog(LOG_INFO, emsg, ap);
140 	va_end(ap);
141 }
142 
143 void
144 log_debug(const char *emsg, ...)
145 {
146 	va_list	 ap;
147 
148 	if (verbose > 1) {
149 		va_start(ap, emsg);
150 		vlog(LOG_DEBUG, emsg, ap);
151 		va_end(ap);
152 	}
153 }
154 
155 void
156 print_debug(const char *emsg, ...)
157 {
158 	va_list	 ap;
159 
160 	if (debug && verbose > 2) {
161 		va_start(ap, emsg);
162 		vfprintf(stderr, emsg, ap);
163 		va_end(ap);
164 	}
165 }
166 
167 void
168 print_verbose(const char *emsg, ...)
169 {
170 	va_list	 ap;
171 
172 	if (verbose) {
173 		va_start(ap, emsg);
174 		vfprintf(stderr, emsg, ap);
175 		va_end(ap);
176 	}
177 }
178 
179 void
180 fatal(const char *emsg)
181 {
182 	if (emsg == NULL)
183 		logit(LOG_CRIT, "fatal: %s", strerror(errno));
184 	else {
185 		if (errno)
186 			logit(LOG_CRIT, "fatal: %s: %s",
187 			    emsg, strerror(errno));
188 		else
189 			logit(LOG_CRIT, "fatal: %s", emsg);
190 	}
191 
192 	exit(1);
193 }
194 
195 void
196 fatalx(const char *emsg)
197 {
198 	errno = 0;
199 	fatal(emsg);
200 }
201 
202 const char *
203 log_in6addr(const struct in6_addr *addr)
204 {
205 	static char		buf[NI_MAXHOST];
206 	struct sockaddr_in6	sa_in6;
207 	u_int16_t		tmp16;
208 
209 	bzero(&sa_in6, sizeof(sa_in6));
210 	sa_in6.sin6_len = sizeof(sa_in6);
211 	sa_in6.sin6_family = AF_INET6;
212 	memcpy(&sa_in6.sin6_addr, addr, sizeof(sa_in6.sin6_addr));
213 
214 	/* XXX thanks, KAME, for this ugliness... adopted from route/show.c */
215 	if (IN6_IS_ADDR_LINKLOCAL(&sa_in6.sin6_addr) ||
216 	    IN6_IS_ADDR_MC_LINKLOCAL(&sa_in6.sin6_addr)) {
217 		memcpy(&tmp16, &sa_in6.sin6_addr.s6_addr[2], sizeof(tmp16));
218 		sa_in6.sin6_scope_id = ntohs(tmp16);
219 		sa_in6.sin6_addr.s6_addr[2] = 0;
220 		sa_in6.sin6_addr.s6_addr[3] = 0;
221 	}
222 
223 	return (print_host((struct sockaddr_storage *)&sa_in6, buf,
224 	    NI_MAXHOST));
225 }
226 
227 const char *
228 print_host(struct sockaddr_storage *ss, char *buf, size_t len)
229 {
230 	if (getnameinfo((struct sockaddr *)ss, ss->ss_len,
231 	    buf, len, NULL, 0, NI_NUMERICHOST) != 0) {
232 		buf[0] = '\0';
233 		return (NULL);
234 	}
235 	return (buf);
236 }
237