xref: /netbsd-src/crypto/external/bsd/openssh/dist/log.c (revision 212397c69a103ae7e5eafa8731ddfae671d2dee7)
1 /*	$NetBSD: log.c,v 1.13 2015/08/13 10:33:21 christos Exp $	*/
2 /* $OpenBSD: log.c,v 1.46 2015/07/08 19:04:21 markus Exp $ */
3 /*
4  * Author: Tatu Ylonen <ylo@cs.hut.fi>
5  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6  *                    All rights reserved
7  *
8  * As far as I am concerned, the code I have written for this software
9  * can be used freely for any purpose.  Any derived versions of this
10  * software must be clearly marked as such, and if the derived work is
11  * incompatible with the protocol description in the RFC file, it must be
12  * called by a name other than "ssh" or "Secure Shell".
13  */
14 /*
15  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include "includes.h"
39 __RCSID("$NetBSD: log.c,v 1.13 2015/08/13 10:33:21 christos Exp $");
40 #include <sys/types.h>
41 #include <sys/uio.h>
42 
43 #include <fcntl.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <syslog.h>
49 #include <unistd.h>
50 #include <errno.h>
51 #include <vis.h>
52 
53 #include "log.h"
54 
55 static LogLevel log_level = SYSLOG_LEVEL_INFO;
56 static int log_on_stderr = 1;
57 static int log_stderr_fd = STDERR_FILENO;
58 static int log_facility = LOG_AUTH;
59 static const char *argv0;
60 static log_handler_fn *log_handler;
61 static void *log_handler_ctx;
62 
63 extern char *__progname;
64 
65 /* textual representation of log-facilities/levels */
66 
67 static struct {
68 	const char *name;
69 	SyslogFacility val;
70 } log_facilities[] = {
71 	{ "DAEMON",	SYSLOG_FACILITY_DAEMON },
72 	{ "USER",	SYSLOG_FACILITY_USER },
73 	{ "AUTH",	SYSLOG_FACILITY_AUTH },
74 	{ "LOCAL0",	SYSLOG_FACILITY_LOCAL0 },
75 	{ "LOCAL1",	SYSLOG_FACILITY_LOCAL1 },
76 	{ "LOCAL2",	SYSLOG_FACILITY_LOCAL2 },
77 	{ "LOCAL3",	SYSLOG_FACILITY_LOCAL3 },
78 	{ "LOCAL4",	SYSLOG_FACILITY_LOCAL4 },
79 	{ "LOCAL5",	SYSLOG_FACILITY_LOCAL5 },
80 	{ "LOCAL6",	SYSLOG_FACILITY_LOCAL6 },
81 	{ "LOCAL7",	SYSLOG_FACILITY_LOCAL7 },
82 	{ NULL,		SYSLOG_FACILITY_NOT_SET }
83 };
84 
85 static struct {
86 	const char *name;
87 	LogLevel val;
88 } log_levels[] =
89 {
90 	{ "QUIET",	SYSLOG_LEVEL_QUIET },
91 	{ "FATAL",	SYSLOG_LEVEL_FATAL },
92 	{ "ERROR",	SYSLOG_LEVEL_ERROR },
93 	{ "INFO",	SYSLOG_LEVEL_INFO },
94 	{ "VERBOSE",	SYSLOG_LEVEL_VERBOSE },
95 	{ "DEBUG",	SYSLOG_LEVEL_DEBUG1 },
96 	{ "DEBUG1",	SYSLOG_LEVEL_DEBUG1 },
97 	{ "DEBUG2",	SYSLOG_LEVEL_DEBUG2 },
98 	{ "DEBUG3",	SYSLOG_LEVEL_DEBUG3 },
99 	{ NULL,		SYSLOG_LEVEL_NOT_SET }
100 };
101 
102 SyslogFacility
103 log_facility_number(char *name)
104 {
105 	int i;
106 
107 	if (name != NULL)
108 		for (i = 0; log_facilities[i].name; i++)
109 			if (strcasecmp(log_facilities[i].name, name) == 0)
110 				return log_facilities[i].val;
111 	return SYSLOG_FACILITY_NOT_SET;
112 }
113 
114 const char *
115 log_facility_name(SyslogFacility facility)
116 {
117 	u_int i;
118 
119 	for (i = 0;  log_facilities[i].name; i++)
120 		if (log_facilities[i].val == facility)
121 			return log_facilities[i].name;
122 	return NULL;
123 }
124 
125 LogLevel
126 log_level_number(char *name)
127 {
128 	int i;
129 
130 	if (name != NULL)
131 		for (i = 0; log_levels[i].name; i++)
132 			if (strcasecmp(log_levels[i].name, name) == 0)
133 				return log_levels[i].val;
134 	return SYSLOG_LEVEL_NOT_SET;
135 }
136 
137 const char *
138 log_level_name(LogLevel level)
139 {
140 	u_int i;
141 
142 	for (i = 0; log_levels[i].name != NULL; i++)
143 		if (log_levels[i].val == level)
144 			return log_levels[i].name;
145 	return NULL;
146 }
147 
148 /* Error messages that should be logged. */
149 
150 void
151 error(const char *fmt,...)
152 {
153 	va_list args;
154 
155 	va_start(args, fmt);
156 	do_log(SYSLOG_LEVEL_ERROR, fmt, args);
157 	va_end(args);
158 }
159 
160 void
161 sigdie(const char *fmt,...)
162 {
163 	va_list args;
164 
165 	va_start(args, fmt);
166 	do_log(SYSLOG_LEVEL_FATAL, fmt, args);
167 	va_end(args);
168 	_exit(1);
169 }
170 
171 
172 /* Log this message (information that usually should go to the log). */
173 
174 void
175 logit(const char *fmt,...)
176 {
177 	va_list args;
178 
179 	va_start(args, fmt);
180 	do_log(SYSLOG_LEVEL_INFO, fmt, args);
181 	va_end(args);
182 }
183 
184 /* More detailed messages (information that does not need to go to the log). */
185 
186 void
187 verbose(const char *fmt,...)
188 {
189 	va_list args;
190 
191 	va_start(args, fmt);
192 	do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
193 	va_end(args);
194 }
195 
196 /* Debugging messages that should not be logged during normal operation. */
197 
198 void
199 debug(const char *fmt,...)
200 {
201 	va_list args;
202 
203 	va_start(args, fmt);
204 	do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
205 	va_end(args);
206 }
207 
208 void
209 debug2(const char *fmt,...)
210 {
211 	va_list args;
212 
213 	va_start(args, fmt);
214 	do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
215 	va_end(args);
216 }
217 
218 void
219 debug3(const char *fmt,...)
220 {
221 	va_list args;
222 
223 	va_start(args, fmt);
224 	do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
225 	va_end(args);
226 }
227 
228 /*
229  * Initialize the log.
230  */
231 
232 void
233 log_init(const char *av0, LogLevel level, SyslogFacility facility,
234     int on_stderr)
235 {
236 	argv0 = av0;
237 
238 	switch (level) {
239 	case SYSLOG_LEVEL_QUIET:
240 	case SYSLOG_LEVEL_FATAL:
241 	case SYSLOG_LEVEL_ERROR:
242 	case SYSLOG_LEVEL_INFO:
243 	case SYSLOG_LEVEL_VERBOSE:
244 	case SYSLOG_LEVEL_DEBUG1:
245 	case SYSLOG_LEVEL_DEBUG2:
246 	case SYSLOG_LEVEL_DEBUG3:
247 		log_level = level;
248 		break;
249 	default:
250 		fprintf(stderr, "Unrecognized internal syslog level code %d\n",
251 		    (int) level);
252 		exit(1);
253 	}
254 
255 	log_handler = NULL;
256 	log_handler_ctx = NULL;
257 
258 	log_on_stderr = on_stderr;
259 	if (on_stderr)
260 		return;
261 
262 	switch (facility) {
263 	case SYSLOG_FACILITY_DAEMON:
264 		log_facility = LOG_DAEMON;
265 		break;
266 	case SYSLOG_FACILITY_USER:
267 		log_facility = LOG_USER;
268 		break;
269 	case SYSLOG_FACILITY_AUTH:
270 		log_facility = LOG_AUTH;
271 		break;
272 	case SYSLOG_FACILITY_LOCAL0:
273 		log_facility = LOG_LOCAL0;
274 		break;
275 	case SYSLOG_FACILITY_LOCAL1:
276 		log_facility = LOG_LOCAL1;
277 		break;
278 	case SYSLOG_FACILITY_LOCAL2:
279 		log_facility = LOG_LOCAL2;
280 		break;
281 	case SYSLOG_FACILITY_LOCAL3:
282 		log_facility = LOG_LOCAL3;
283 		break;
284 	case SYSLOG_FACILITY_LOCAL4:
285 		log_facility = LOG_LOCAL4;
286 		break;
287 	case SYSLOG_FACILITY_LOCAL5:
288 		log_facility = LOG_LOCAL5;
289 		break;
290 	case SYSLOG_FACILITY_LOCAL6:
291 		log_facility = LOG_LOCAL6;
292 		break;
293 	case SYSLOG_FACILITY_LOCAL7:
294 		log_facility = LOG_LOCAL7;
295 		break;
296 	default:
297 		fprintf(stderr,
298 		    "Unrecognized internal syslog facility code %d\n",
299 		    (int) facility);
300 		exit(1);
301 	}
302 }
303 
304 void
305 log_change_level(LogLevel new_log_level)
306 {
307 	/* no-op if log_init has not been called */
308 	if (argv0 == NULL)
309 		return;
310 	log_init(argv0, new_log_level, log_facility, log_on_stderr);
311 }
312 
313 int
314 log_is_on_stderr(void)
315 {
316 	return log_on_stderr;
317 }
318 
319 /* redirect what would usually get written to stderr to specified file */
320 void
321 log_redirect_stderr_to(const char *logfile)
322 {
323 	int fd;
324 
325 	if ((fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600)) == -1) {
326 		fprintf(stderr, "Couldn't open logfile %s: %s\n", logfile,
327 		     strerror(errno));
328 		exit(1);
329 	}
330 	log_stderr_fd = fd;
331 }
332 
333 #define MSGBUFSIZ 1024
334 
335 void
336 set_log_handler(log_handler_fn *handler, void *ctx)
337 {
338 	log_handler = handler;
339 	log_handler_ctx = ctx;
340 }
341 
342 void
343 do_log2(LogLevel level, const char *fmt,...)
344 {
345 	va_list args;
346 
347 	va_start(args, fmt);
348 	do_log(level, fmt, args);
349 	va_end(args);
350 }
351 
352 void
353 do_log(LogLevel level, const char *fmt, va_list args)
354 {
355 #ifdef SYSLOG_DATA_INIT
356 	struct syslog_data sdata = SYSLOG_DATA_INIT;
357 #endif
358 	char msgbuf[MSGBUFSIZ], *msgbufp;
359 	char visbuf[MSGBUFSIZ * 4 + 1];
360 	size_t len, len2;
361 	const char *txt = NULL;
362 	int pri = LOG_INFO;
363 	int saved_errno = errno;
364 	log_handler_fn *tmp_handler;
365 
366 	if (level > log_level)
367 		return;
368 
369 	switch (level) {
370 	case SYSLOG_LEVEL_FATAL:
371 		if (!log_on_stderr)
372 			txt = "fatal";
373 		pri = LOG_CRIT;
374 		break;
375 	case SYSLOG_LEVEL_ERROR:
376 		if (!log_on_stderr)
377 			txt = "error";
378 		pri = LOG_ERR;
379 		break;
380 	case SYSLOG_LEVEL_INFO:
381 		pri = LOG_INFO;
382 		break;
383 	case SYSLOG_LEVEL_VERBOSE:
384 		pri = LOG_INFO;
385 		break;
386 	case SYSLOG_LEVEL_DEBUG1:
387 		txt = "debug1";
388 		pri = LOG_DEBUG;
389 		break;
390 	case SYSLOG_LEVEL_DEBUG2:
391 		txt = "debug2";
392 		pri = LOG_DEBUG;
393 		break;
394 	case SYSLOG_LEVEL_DEBUG3:
395 		txt = "debug3";
396 		pri = LOG_DEBUG;
397 		break;
398 	default:
399 		txt = "internal error";
400 		pri = LOG_ERR;
401 		break;
402 	}
403 	len = sizeof(msgbuf);
404 	msgbufp = msgbuf;
405 	if (txt != NULL && log_handler == NULL) {
406 		len2 = strlen(txt);
407 		if (len2 > len - 2)
408 			len2 = len - 2;
409 		memcpy(msgbufp, txt, len2);
410 		msgbufp += len2;
411 		*msgbufp++ = ':';
412 		*msgbufp++ = ' ';
413 		len -= len2 + 2;
414 	}
415 	vsnprintf(msgbufp, len, fmt, args);
416 	strnvis(visbuf, sizeof(visbuf), msgbuf, VIS_SAFE|VIS_OCTAL);
417 	if (log_handler != NULL) {
418 		/* Avoid recursion */
419 		tmp_handler = log_handler;
420 		log_handler = NULL;
421 		tmp_handler(level, visbuf, log_handler_ctx);
422 		log_handler = tmp_handler;
423 	} else if (log_on_stderr) {
424 		snprintf(msgbuf, sizeof msgbuf, "%s\r\n", visbuf);
425 		(void)write(log_stderr_fd, msgbuf, strlen(msgbuf));
426 	} else {
427 #ifdef SYSLOG_DATA_INIT
428 		openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
429 		syslog_r(pri, &sdata, "%.500s", visbuf);
430 		closelog_r(&sdata);
431 #else
432 		openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
433 		syslog(pri, "%.500s", visbuf);
434 		closelog();
435 #endif
436 	}
437 	errno = saved_errno;
438 }
439