blob: 68cbfc7d9908eeaaac89cd41a8a29503dbf16aab [file] [log] [blame]
Damien Millere7a1e5c2006-08-05 11:34:19 +10001/* $OpenBSD: log.c,v 1.36 2006/07/26 13:57:17 stevesk Exp $ */
Damien Miller5ce662a1999-11-11 17:57:39 +11002/*
Damien Millere4340be2000-09-16 13:29:08 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
6 *
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".
12 */
13/*
Damien Millere4340be2000-09-16 13:29:08 +110014 * Copyright (c) 2000 Markus Friedl. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
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.
24 *
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.
Damien Miller5428f641999-11-25 11:54:57 +110035 */
Damien Miller5ce662a1999-11-11 17:57:39 +110036
37#include "includes.h"
Damien Miller5ce662a1999-11-11 17:57:39 +110038
Darren Tucker5d196262006-07-12 22:15:16 +100039#include <stdarg.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100040#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100041#include <string.h>
Ben Lindstrom8a432f52001-03-05 07:24:46 +000042#include <syslog.h>
Damien Millere6b3b612006-07-24 14:01:23 +100043#include <unistd.h>
Damien Miller5c3a5582003-09-23 22:12:38 +100044#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
45# include <vis.h>
46#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +000047
Damien Miller194a1cb2006-07-10 21:09:22 +100048#include "log.h"
49#include "xmalloc.h"
50
Ben Lindstrom8a432f52001-03-05 07:24:46 +000051static LogLevel log_level = SYSLOG_LEVEL_INFO;
52static int log_on_stderr = 1;
53static int log_facility = LOG_AUTH;
54static char *argv0;
55
56extern char *__progname;
57
Damien Miller23a70272004-07-21 10:52:13 +100058#define LOG_SYSLOG_VIS (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL)
59#define LOG_STDERR_VIS (VIS_SAFE|VIS_OCTAL)
60
Ben Lindstrom8a432f52001-03-05 07:24:46 +000061/* textual representation of log-facilities/levels */
62
63static struct {
64 const char *name;
65 SyslogFacility val;
66} log_facilities[] = {
67 { "DAEMON", SYSLOG_FACILITY_DAEMON },
68 { "USER", SYSLOG_FACILITY_USER },
69 { "AUTH", SYSLOG_FACILITY_AUTH },
Damien Miller30246a82001-03-05 21:23:31 +110070#ifdef LOG_AUTHPRIV
71 { "AUTHPRIV", SYSLOG_FACILITY_AUTHPRIV },
72#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +000073 { "LOCAL0", SYSLOG_FACILITY_LOCAL0 },
74 { "LOCAL1", SYSLOG_FACILITY_LOCAL1 },
75 { "LOCAL2", SYSLOG_FACILITY_LOCAL2 },
76 { "LOCAL3", SYSLOG_FACILITY_LOCAL3 },
77 { "LOCAL4", SYSLOG_FACILITY_LOCAL4 },
78 { "LOCAL5", SYSLOG_FACILITY_LOCAL5 },
79 { "LOCAL6", SYSLOG_FACILITY_LOCAL6 },
80 { "LOCAL7", SYSLOG_FACILITY_LOCAL7 },
Damien Millerfcd93202002-02-05 12:26:34 +110081 { NULL, SYSLOG_FACILITY_NOT_SET }
Ben Lindstrom8a432f52001-03-05 07:24:46 +000082};
83
84static struct {
85 const char *name;
86 LogLevel val;
87} log_levels[] =
88{
89 { "QUIET", SYSLOG_LEVEL_QUIET },
90 { "FATAL", SYSLOG_LEVEL_FATAL },
91 { "ERROR", SYSLOG_LEVEL_ERROR },
92 { "INFO", SYSLOG_LEVEL_INFO },
93 { "VERBOSE", SYSLOG_LEVEL_VERBOSE },
94 { "DEBUG", SYSLOG_LEVEL_DEBUG1 },
95 { "DEBUG1", SYSLOG_LEVEL_DEBUG1 },
96 { "DEBUG2", SYSLOG_LEVEL_DEBUG2 },
97 { "DEBUG3", SYSLOG_LEVEL_DEBUG3 },
Damien Millerfcd93202002-02-05 12:26:34 +110098 { NULL, SYSLOG_LEVEL_NOT_SET }
Ben Lindstrom8a432f52001-03-05 07:24:46 +000099};
100
101SyslogFacility
102log_facility_number(char *name)
103{
104 int i;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000105
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000106 if (name != NULL)
107 for (i = 0; log_facilities[i].name; i++)
108 if (strcasecmp(log_facilities[i].name, name) == 0)
109 return log_facilities[i].val;
Damien Millerfcd93202002-02-05 12:26:34 +1100110 return SYSLOG_FACILITY_NOT_SET;
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000111}
112
113LogLevel
114log_level_number(char *name)
115{
116 int i;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000117
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000118 if (name != NULL)
119 for (i = 0; log_levels[i].name; i++)
120 if (strcasecmp(log_levels[i].name, name) == 0)
121 return log_levels[i].val;
Damien Millerfcd93202002-02-05 12:26:34 +1100122 return SYSLOG_LEVEL_NOT_SET;
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000123}
Damien Miller5ce662a1999-11-11 17:57:39 +1100124
125/* Error messages that should be logged. */
126
127void
Damien Miller95def091999-11-25 00:26:21 +1100128error(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +1100129{
Damien Miller95def091999-11-25 00:26:21 +1100130 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000131
Damien Miller95def091999-11-25 00:26:21 +1100132 va_start(args, fmt);
133 do_log(SYSLOG_LEVEL_ERROR, fmt, args);
134 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +1100135}
136
137/* Log this message (information that usually should go to the log). */
138
139void
Damien Miller996acd22003-04-09 20:59:48 +1000140logit(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +1100141{
Damien Miller95def091999-11-25 00:26:21 +1100142 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000143
Damien Miller95def091999-11-25 00:26:21 +1100144 va_start(args, fmt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000145 do_log(SYSLOG_LEVEL_INFO, fmt, args);
Damien Miller95def091999-11-25 00:26:21 +1100146 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +1100147}
148
149/* More detailed messages (information that does not need to go to the log). */
150
151void
Damien Miller95def091999-11-25 00:26:21 +1100152verbose(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +1100153{
Damien Miller95def091999-11-25 00:26:21 +1100154 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000155
Damien Miller95def091999-11-25 00:26:21 +1100156 va_start(args, fmt);
157 do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
158 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +1100159}
160
161/* Debugging messages that should not be logged during normal operation. */
162
163void
Damien Miller95def091999-11-25 00:26:21 +1100164debug(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +1100165{
Damien Miller95def091999-11-25 00:26:21 +1100166 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000167
Damien Miller95def091999-11-25 00:26:21 +1100168 va_start(args, fmt);
Damien Millere4340be2000-09-16 13:29:08 +1100169 do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
170 va_end(args);
171}
172
173void
174debug2(const char *fmt,...)
175{
176 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000177
Damien Millere4340be2000-09-16 13:29:08 +1100178 va_start(args, fmt);
179 do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
180 va_end(args);
181}
182
183void
184debug3(const char *fmt,...)
185{
186 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000187
Damien Millere4340be2000-09-16 13:29:08 +1100188 va_start(args, fmt);
189 do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
Damien Miller95def091999-11-25 00:26:21 +1100190 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +1100191}
192
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000193/*
194 * Initialize the log.
195 */
196
197void
198log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
199{
Darren Tucker9b5495d2005-02-01 17:35:09 +1100200#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
201 struct syslog_data sdata = SYSLOG_DATA_INIT;
202#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000203
Darren Tucker835903d2005-03-09 20:12:47 +1100204 argv0 = av0;
205
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000206 switch (level) {
207 case SYSLOG_LEVEL_QUIET:
208 case SYSLOG_LEVEL_FATAL:
209 case SYSLOG_LEVEL_ERROR:
210 case SYSLOG_LEVEL_INFO:
211 case SYSLOG_LEVEL_VERBOSE:
212 case SYSLOG_LEVEL_DEBUG1:
213 case SYSLOG_LEVEL_DEBUG2:
214 case SYSLOG_LEVEL_DEBUG3:
215 log_level = level;
216 break;
217 default:
Kevin Stevesedcd5762001-04-02 13:45:00 +0000218 fprintf(stderr, "Unrecognized internal syslog level code %d\n",
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000219 (int) level);
220 exit(1);
221 }
222
223 log_on_stderr = on_stderr;
224 if (on_stderr)
225 return;
226
227 switch (facility) {
228 case SYSLOG_FACILITY_DAEMON:
229 log_facility = LOG_DAEMON;
230 break;
231 case SYSLOG_FACILITY_USER:
232 log_facility = LOG_USER;
233 break;
234 case SYSLOG_FACILITY_AUTH:
235 log_facility = LOG_AUTH;
236 break;
Damien Miller30246a82001-03-05 21:23:31 +1100237#ifdef LOG_AUTHPRIV
238 case SYSLOG_FACILITY_AUTHPRIV:
239 log_facility = LOG_AUTHPRIV;
240 break;
Ben Lindstrom53f11c62001-03-05 08:18:17 +0000241#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000242 case SYSLOG_FACILITY_LOCAL0:
243 log_facility = LOG_LOCAL0;
244 break;
245 case SYSLOG_FACILITY_LOCAL1:
246 log_facility = LOG_LOCAL1;
247 break;
248 case SYSLOG_FACILITY_LOCAL2:
249 log_facility = LOG_LOCAL2;
250 break;
251 case SYSLOG_FACILITY_LOCAL3:
252 log_facility = LOG_LOCAL3;
253 break;
254 case SYSLOG_FACILITY_LOCAL4:
255 log_facility = LOG_LOCAL4;
256 break;
257 case SYSLOG_FACILITY_LOCAL5:
258 log_facility = LOG_LOCAL5;
259 break;
260 case SYSLOG_FACILITY_LOCAL6:
261 log_facility = LOG_LOCAL6;
262 break;
263 case SYSLOG_FACILITY_LOCAL7:
264 log_facility = LOG_LOCAL7;
265 break;
266 default:
267 fprintf(stderr,
Kevin Stevesedcd5762001-04-02 13:45:00 +0000268 "Unrecognized internal syslog facility code %d\n",
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000269 (int) facility);
270 exit(1);
271 }
Darren Tucker9b5495d2005-02-01 17:35:09 +1100272
273 /*
274 * If an external library (eg libwrap) attempts to use syslog
275 * immediately after reexec, syslog may be pointing to the wrong
276 * facility, so we force an open/close of syslog here.
277 */
278#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
279 openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
280 closelog_r(&sdata);
281#else
282 openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
283 closelog();
284#endif
Damien Miller6162d121999-11-21 13:23:52 +1100285}
286
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000287#define MSGBUFSIZ 1024
288
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000289void
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000290do_log(LogLevel level, const char *fmt, va_list args)
Damien Miller6162d121999-11-21 13:23:52 +1100291{
Damien Miller051b0ac2004-02-18 22:59:43 +1100292#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
Damien Miller74a34422003-05-20 09:24:17 +1000293 struct syslog_data sdata = SYSLOG_DATA_INIT;
294#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000295 char msgbuf[MSGBUFSIZ];
296 char fmtbuf[MSGBUFSIZ];
297 char *txt = NULL;
298 int pri = LOG_INFO;
299
300 if (level > log_level)
301 return;
302
303 switch (level) {
304 case SYSLOG_LEVEL_FATAL:
305 if (!log_on_stderr)
306 txt = "fatal";
307 pri = LOG_CRIT;
308 break;
309 case SYSLOG_LEVEL_ERROR:
310 if (!log_on_stderr)
311 txt = "error";
312 pri = LOG_ERR;
313 break;
314 case SYSLOG_LEVEL_INFO:
315 pri = LOG_INFO;
316 break;
317 case SYSLOG_LEVEL_VERBOSE:
318 pri = LOG_INFO;
319 break;
320 case SYSLOG_LEVEL_DEBUG1:
321 txt = "debug1";
322 pri = LOG_DEBUG;
323 break;
324 case SYSLOG_LEVEL_DEBUG2:
325 txt = "debug2";
326 pri = LOG_DEBUG;
327 break;
328 case SYSLOG_LEVEL_DEBUG3:
329 txt = "debug3";
330 pri = LOG_DEBUG;
331 break;
332 default:
333 txt = "internal error";
334 pri = LOG_ERR;
335 break;
336 }
337 if (txt != NULL) {
338 snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt);
339 vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
340 } else {
341 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
342 }
Damien Miller23a70272004-07-21 10:52:13 +1000343 strnvis(fmtbuf, msgbuf, sizeof(fmtbuf),
344 log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS);
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000345 if (log_on_stderr) {
Damien Millerc11fe252003-05-25 14:38:02 +1000346 snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf);
347 write(STDERR_FILENO, msgbuf, strlen(msgbuf));
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000348 } else {
Damien Miller051b0ac2004-02-18 22:59:43 +1100349#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
Damien Miller74a34422003-05-20 09:24:17 +1000350 openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
Damien Millerc11fe252003-05-25 14:38:02 +1000351 syslog_r(pri, &sdata, "%.500s", fmtbuf);
Damien Miller74a34422003-05-20 09:24:17 +1000352 closelog_r(&sdata);
353#else
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000354 openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
Damien Millerb93addb2003-01-07 17:04:18 +1100355 syslog(pri, "%.500s", fmtbuf);
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000356 closelog();
Damien Miller74a34422003-05-20 09:24:17 +1000357#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000358 }
Damien Miller6162d121999-11-21 13:23:52 +1100359}