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