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)
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;
61 static log_handler_fn *log_handler;
62 static void *log_handler_ctx;
64 extern char *__progname;
66 #define LOG_SYSLOG_VIS (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL)
67 #define LOG_STDERR_VIS (VIS_SAFE|VIS_OCTAL)
69 /* textual representation of log-facilities/levels */
74 } log_facilities[] = {
75 { "DAEMON", SYSLOG_FACILITY_DAEMON },
76 { "USER", SYSLOG_FACILITY_USER },
77 { "AUTH", SYSLOG_FACILITY_AUTH },
79 { "AUTHPRIV", SYSLOG_FACILITY_AUTHPRIV },
81 { "LOCAL0", SYSLOG_FACILITY_LOCAL0 },
82 { "LOCAL1", SYSLOG_FACILITY_LOCAL1 },
83 { "LOCAL2", SYSLOG_FACILITY_LOCAL2 },
84 { "LOCAL3", SYSLOG_FACILITY_LOCAL3 },
85 { "LOCAL4", SYSLOG_FACILITY_LOCAL4 },
86 { "LOCAL5", SYSLOG_FACILITY_LOCAL5 },
87 { "LOCAL6", SYSLOG_FACILITY_LOCAL6 },
88 { "LOCAL7", SYSLOG_FACILITY_LOCAL7 },
89 { NULL, SYSLOG_FACILITY_NOT_SET }
97 { "QUIET", SYSLOG_LEVEL_QUIET },
98 { "FATAL", SYSLOG_LEVEL_FATAL },
99 { "ERROR", SYSLOG_LEVEL_ERROR },
100 { "INFO", SYSLOG_LEVEL_INFO },
101 { "VERBOSE", SYSLOG_LEVEL_VERBOSE },
102 { "DEBUG", SYSLOG_LEVEL_DEBUG1 },
103 { "DEBUG1", SYSLOG_LEVEL_DEBUG1 },
104 { "DEBUG2", SYSLOG_LEVEL_DEBUG2 },
105 { "DEBUG3", SYSLOG_LEVEL_DEBUG3 },
106 { NULL, SYSLOG_LEVEL_NOT_SET }
110 log_facility_number(char *name)
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;
122 log_facility_name(SyslogFacility facility)
126 for (i = 0; log_facilities[i].name; i++)
127 if (log_facilities[i].val == facility)
128 return log_facilities[i].name;
133 log_level_number(char *name)
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;
145 log_level_name(LogLevel level)
149 for (i = 0; log_levels[i].name != NULL; i++)
150 if (log_levels[i].val == level)
151 return log_levels[i].name;
155 /* Error messages that should be logged. */
158 error(const char *fmt,...)
163 do_log(SYSLOG_LEVEL_ERROR, fmt, args);
168 sigdie(const char *fmt,...)
170 #ifdef DO_LOG_SAFE_IN_SIGHAND
174 do_log(SYSLOG_LEVEL_FATAL, fmt, args);
181 /* Log this message (information that usually should go to the log). */
184 logit(const char *fmt,...)
189 do_log(SYSLOG_LEVEL_INFO, fmt, args);
193 /* More detailed messages (information that does not need to go to the log). */
196 verbose(const char *fmt,...)
201 do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
205 /* Debugging messages that should not be logged during normal operation. */
208 debug(const char *fmt,...)
213 do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
218 debug2(const char *fmt,...)
223 do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
228 debug3(const char *fmt,...)
233 do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
238 * Initialize the log.
242 log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
244 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
245 struct syslog_data sdata = SYSLOG_DATA_INIT;
251 case SYSLOG_LEVEL_QUIET:
252 case SYSLOG_LEVEL_FATAL:
253 case SYSLOG_LEVEL_ERROR:
254 case SYSLOG_LEVEL_INFO:
255 case SYSLOG_LEVEL_VERBOSE:
256 case SYSLOG_LEVEL_DEBUG1:
257 case SYSLOG_LEVEL_DEBUG2:
258 case SYSLOG_LEVEL_DEBUG3:
262 fprintf(stderr, "Unrecognized internal syslog level code %d\n",
268 log_handler_ctx = NULL;
270 log_on_stderr = on_stderr;
275 case SYSLOG_FACILITY_DAEMON:
276 log_facility = LOG_DAEMON;
278 case SYSLOG_FACILITY_USER:
279 log_facility = LOG_USER;
281 case SYSLOG_FACILITY_AUTH:
282 log_facility = LOG_AUTH;
285 case SYSLOG_FACILITY_AUTHPRIV:
286 log_facility = LOG_AUTHPRIV;
289 case SYSLOG_FACILITY_LOCAL0:
290 log_facility = LOG_LOCAL0;
292 case SYSLOG_FACILITY_LOCAL1:
293 log_facility = LOG_LOCAL1;
295 case SYSLOG_FACILITY_LOCAL2:
296 log_facility = LOG_LOCAL2;
298 case SYSLOG_FACILITY_LOCAL3:
299 log_facility = LOG_LOCAL3;
301 case SYSLOG_FACILITY_LOCAL4:
302 log_facility = LOG_LOCAL4;
304 case SYSLOG_FACILITY_LOCAL5:
305 log_facility = LOG_LOCAL5;
307 case SYSLOG_FACILITY_LOCAL6:
308 log_facility = LOG_LOCAL6;
310 case SYSLOG_FACILITY_LOCAL7:
311 log_facility = LOG_LOCAL7;
315 "Unrecognized internal syslog facility code %d\n",
321 * If an external library (eg libwrap) attempts to use syslog
322 * immediately after reexec, syslog may be pointing to the wrong
323 * facility, so we force an open/close of syslog here.
325 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
326 openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
329 openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
335 log_change_level(LogLevel new_log_level)
337 /* no-op if log_init has not been called */
340 log_init(argv0, new_log_level, log_facility, log_on_stderr);
344 log_is_on_stderr(void)
346 return log_on_stderr;
349 /* redirect what would usually get written to stderr to specified file */
351 log_redirect_stderr_to(const char *logfile)
355 if ((fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0600)) == -1) {
356 fprintf(stderr, "Couldn't open logfile %s: %s\n", logfile,
363 #define MSGBUFSIZ 1024
366 set_log_handler(log_handler_fn *handler, void *ctx)
368 log_handler = handler;
369 log_handler_ctx = ctx;
373 do_log2(LogLevel level, const char *fmt,...)
378 do_log(level, fmt, args);
383 do_log(LogLevel level, const char *fmt, va_list args)
385 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
386 struct syslog_data sdata = SYSLOG_DATA_INIT;
388 char msgbuf[MSGBUFSIZ];
389 char fmtbuf[MSGBUFSIZ];
392 int saved_errno = errno;
393 log_handler_fn *tmp_handler;
395 if (level > log_level)
399 case SYSLOG_LEVEL_FATAL:
404 case SYSLOG_LEVEL_ERROR:
409 case SYSLOG_LEVEL_INFO:
412 case SYSLOG_LEVEL_VERBOSE:
415 case SYSLOG_LEVEL_DEBUG1:
419 case SYSLOG_LEVEL_DEBUG2:
423 case SYSLOG_LEVEL_DEBUG3:
428 txt = "internal error";
432 if (txt != NULL && log_handler == NULL) {
433 snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt);
434 vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
436 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
438 strnvis(fmtbuf, msgbuf, sizeof(fmtbuf),
439 log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS);
440 if (log_handler != NULL) {
441 /* Avoid recursion */
442 tmp_handler = log_handler;
444 tmp_handler(level, fmtbuf, log_handler_ctx);
445 log_handler = tmp_handler;
446 } else if (log_on_stderr) {
447 snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf);
448 (void)write(log_stderr_fd, msgbuf, strlen(msgbuf));
450 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
451 openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
452 syslog_r(pri, &sdata, "%.500s", fmtbuf);
455 openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
456 syslog(pri, "%.500s", fmtbuf);