1 /* $OpenBSD: log.c,v 1.45 2013/05/16 09:08:41 dtucker Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
14 * Copyright (c) 2000 Markus Friedl. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/types.h>
49 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
57 static LogLevel log_level = SYSLOG_LEVEL_INFO;
58 static int log_on_stderr = 1;
59 static int log_stderr_fd = STDERR_FILENO;
60 static int log_facility = LOG_AUTH;
62 static log_handler_fn *log_handler;
63 static void *log_handler_ctx;
65 extern char *__progname;
67 #define LOG_SYSLOG_VIS (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL)
68 #define LOG_STDERR_VIS (VIS_SAFE|VIS_OCTAL)
70 /* textual representation of log-facilities/levels */
75 } log_facilities[] = {
76 { "DAEMON", SYSLOG_FACILITY_DAEMON },
77 { "USER", SYSLOG_FACILITY_USER },
78 { "AUTH", SYSLOG_FACILITY_AUTH },
80 { "AUTHPRIV", SYSLOG_FACILITY_AUTHPRIV },
82 { "LOCAL0", SYSLOG_FACILITY_LOCAL0 },
83 { "LOCAL1", SYSLOG_FACILITY_LOCAL1 },
84 { "LOCAL2", SYSLOG_FACILITY_LOCAL2 },
85 { "LOCAL3", SYSLOG_FACILITY_LOCAL3 },
86 { "LOCAL4", SYSLOG_FACILITY_LOCAL4 },
87 { "LOCAL5", SYSLOG_FACILITY_LOCAL5 },
88 { "LOCAL6", SYSLOG_FACILITY_LOCAL6 },
89 { "LOCAL7", SYSLOG_FACILITY_LOCAL7 },
90 { NULL, SYSLOG_FACILITY_NOT_SET }
98 { "QUIET", SYSLOG_LEVEL_QUIET },
99 { "FATAL", SYSLOG_LEVEL_FATAL },
100 { "ERROR", SYSLOG_LEVEL_ERROR },
101 { "INFO", SYSLOG_LEVEL_INFO },
102 { "VERBOSE", SYSLOG_LEVEL_VERBOSE },
103 { "DEBUG", SYSLOG_LEVEL_DEBUG1 },
104 { "DEBUG1", SYSLOG_LEVEL_DEBUG1 },
105 { "DEBUG2", SYSLOG_LEVEL_DEBUG2 },
106 { "DEBUG3", SYSLOG_LEVEL_DEBUG3 },
107 { NULL, SYSLOG_LEVEL_NOT_SET }
111 log_facility_number(char *name)
116 for (i = 0; log_facilities[i].name; i++)
117 if (strcasecmp(log_facilities[i].name, name) == 0)
118 return log_facilities[i].val;
119 return SYSLOG_FACILITY_NOT_SET;
123 log_facility_name(SyslogFacility facility)
127 for (i = 0; log_facilities[i].name; i++)
128 if (log_facilities[i].val == facility)
129 return log_facilities[i].name;
134 log_level_number(char *name)
139 for (i = 0; log_levels[i].name; i++)
140 if (strcasecmp(log_levels[i].name, name) == 0)
141 return log_levels[i].val;
142 return SYSLOG_LEVEL_NOT_SET;
146 log_level_name(LogLevel level)
150 for (i = 0; log_levels[i].name != NULL; i++)
151 if (log_levels[i].val == level)
152 return log_levels[i].name;
156 /* Error messages that should be logged. */
159 error(const char *fmt,...)
164 do_log(SYSLOG_LEVEL_ERROR, fmt, args);
169 sigdie(const char *fmt,...)
171 #ifdef DO_LOG_SAFE_IN_SIGHAND
175 do_log(SYSLOG_LEVEL_FATAL, fmt, args);
182 /* Log this message (information that usually should go to the log). */
185 logit(const char *fmt,...)
190 do_log(SYSLOG_LEVEL_INFO, fmt, args);
194 /* More detailed messages (information that does not need to go to the log). */
197 verbose(const char *fmt,...)
202 do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
206 /* Debugging messages that should not be logged during normal operation. */
209 debug(const char *fmt,...)
214 do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
219 debug2(const char *fmt,...)
224 do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
229 debug3(const char *fmt,...)
234 do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
239 * Initialize the log.
243 log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
245 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
246 struct syslog_data sdata = SYSLOG_DATA_INIT;
252 case SYSLOG_LEVEL_QUIET:
253 case SYSLOG_LEVEL_FATAL:
254 case SYSLOG_LEVEL_ERROR:
255 case SYSLOG_LEVEL_INFO:
256 case SYSLOG_LEVEL_VERBOSE:
257 case SYSLOG_LEVEL_DEBUG1:
258 case SYSLOG_LEVEL_DEBUG2:
259 case SYSLOG_LEVEL_DEBUG3:
263 fprintf(stderr, "Unrecognized internal syslog level code %d\n",
269 log_handler_ctx = NULL;
271 log_on_stderr = on_stderr;
276 case SYSLOG_FACILITY_DAEMON:
277 log_facility = LOG_DAEMON;
279 case SYSLOG_FACILITY_USER:
280 log_facility = LOG_USER;
282 case SYSLOG_FACILITY_AUTH:
283 log_facility = LOG_AUTH;
286 case SYSLOG_FACILITY_AUTHPRIV:
287 log_facility = LOG_AUTHPRIV;
290 case SYSLOG_FACILITY_LOCAL0:
291 log_facility = LOG_LOCAL0;
293 case SYSLOG_FACILITY_LOCAL1:
294 log_facility = LOG_LOCAL1;
296 case SYSLOG_FACILITY_LOCAL2:
297 log_facility = LOG_LOCAL2;
299 case SYSLOG_FACILITY_LOCAL3:
300 log_facility = LOG_LOCAL3;
302 case SYSLOG_FACILITY_LOCAL4:
303 log_facility = LOG_LOCAL4;
305 case SYSLOG_FACILITY_LOCAL5:
306 log_facility = LOG_LOCAL5;
308 case SYSLOG_FACILITY_LOCAL6:
309 log_facility = LOG_LOCAL6;
311 case SYSLOG_FACILITY_LOCAL7:
312 log_facility = LOG_LOCAL7;
316 "Unrecognized internal syslog facility code %d\n",
322 * If an external library (eg libwrap) attempts to use syslog
323 * immediately after reexec, syslog may be pointing to the wrong
324 * facility, so we force an open/close of syslog here.
326 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
327 openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
330 openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
336 log_change_level(LogLevel new_log_level)
338 /* no-op if log_init has not been called */
341 log_init(argv0, new_log_level, log_facility, log_on_stderr);
345 log_is_on_stderr(void)
347 return log_on_stderr;
350 /* redirect what would usually get written to stderr to specified file */
352 log_redirect_stderr_to(const char *logfile)
356 if ((fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600)) == -1) {
357 fprintf(stderr, "Couldn't open logfile %s: %s\n", logfile,
364 #define MSGBUFSIZ 1024
367 set_log_handler(log_handler_fn *handler, void *ctx)
369 log_handler = handler;
370 log_handler_ctx = ctx;
374 do_log2(LogLevel level, const char *fmt,...)
379 do_log(level, fmt, args);
384 do_log(LogLevel level, const char *fmt, va_list args)
386 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
387 struct syslog_data sdata = SYSLOG_DATA_INIT;
389 char msgbuf[MSGBUFSIZ];
390 char fmtbuf[MSGBUFSIZ];
394 int saved_errno = errno;
395 log_handler_fn *tmp_handler;
397 if (level > log_level)
401 case SYSLOG_LEVEL_FATAL:
406 case SYSLOG_LEVEL_ERROR:
411 case SYSLOG_LEVEL_INFO:
414 case SYSLOG_LEVEL_VERBOSE:
417 case SYSLOG_LEVEL_DEBUG1:
421 case SYSLOG_LEVEL_DEBUG2:
425 case SYSLOG_LEVEL_DEBUG3:
430 txt = "internal error";
434 if (txt != NULL && log_handler == NULL) {
435 snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt);
436 vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
438 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
440 strnvis(fmtbuf, msgbuf, sizeof(fmtbuf),
441 log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS);
442 if (log_handler != NULL) {
443 /* Avoid recursion */
444 tmp_handler = log_handler;
446 tmp_handler(level, fmtbuf, log_handler_ctx);
447 log_handler = tmp_handler;
448 } else if (log_on_stderr) {
449 snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf);
450 (void)write(log_stderr_fd, msgbuf, strlen(msgbuf));
452 /* Prevent a race between the grace_alarm
453 * which writes a log message and terminates
454 * and main sshd code that leads to deadlock
455 * as syslog is not async safe.
458 sigaddset(&nset, SIGALRM);
459 sigprocmask(SIG_BLOCK, &nset, &oset);
460 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
461 openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
462 syslog_r(pri, &sdata, "%.500s", fmtbuf);
465 openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
466 syslog(pri, "%.500s", fmtbuf);
469 sigprocmask(SIG_SETMASK, &oset, NULL);