blob: 8702c3e523d6f7c6b09683a798e33f41a9f6b99c [file] [log] [blame]
Darren Tucker5d196262006-07-12 22:15:16 +10001/* $OpenBSD: log.c,v 1.33 2006/07/10 16:37:36 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>
Ben Lindstrom8a432f52001-03-05 07:24:46 +000040#include <syslog.h>
Damien Miller5c3a5582003-09-23 22:12:38 +100041#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
42# include <vis.h>
43#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +000044
Damien Miller194a1cb2006-07-10 21:09:22 +100045#include "log.h"
46#include "xmalloc.h"
47
Ben Lindstrom8a432f52001-03-05 07:24:46 +000048static LogLevel log_level = SYSLOG_LEVEL_INFO;
49static int log_on_stderr = 1;
50static int log_facility = LOG_AUTH;
51static char *argv0;
52
53extern char *__progname;
54
Damien Miller23a70272004-07-21 10:52:13 +100055#define LOG_SYSLOG_VIS (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL)
56#define LOG_STDERR_VIS (VIS_SAFE|VIS_OCTAL)
57
Ben Lindstrom8a432f52001-03-05 07:24:46 +000058/* textual representation of log-facilities/levels */
59
60static struct {
61 const char *name;
62 SyslogFacility val;
63} log_facilities[] = {
64 { "DAEMON", SYSLOG_FACILITY_DAEMON },
65 { "USER", SYSLOG_FACILITY_USER },
66 { "AUTH", SYSLOG_FACILITY_AUTH },
Damien Miller30246a82001-03-05 21:23:31 +110067#ifdef LOG_AUTHPRIV
68 { "AUTHPRIV", SYSLOG_FACILITY_AUTHPRIV },
69#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +000070 { "LOCAL0", SYSLOG_FACILITY_LOCAL0 },
71 { "LOCAL1", SYSLOG_FACILITY_LOCAL1 },
72 { "LOCAL2", SYSLOG_FACILITY_LOCAL2 },
73 { "LOCAL3", SYSLOG_FACILITY_LOCAL3 },
74 { "LOCAL4", SYSLOG_FACILITY_LOCAL4 },
75 { "LOCAL5", SYSLOG_FACILITY_LOCAL5 },
76 { "LOCAL6", SYSLOG_FACILITY_LOCAL6 },
77 { "LOCAL7", SYSLOG_FACILITY_LOCAL7 },
Damien Millerfcd93202002-02-05 12:26:34 +110078 { NULL, SYSLOG_FACILITY_NOT_SET }
Ben Lindstrom8a432f52001-03-05 07:24:46 +000079};
80
81static struct {
82 const char *name;
83 LogLevel val;
84} log_levels[] =
85{
86 { "QUIET", SYSLOG_LEVEL_QUIET },
87 { "FATAL", SYSLOG_LEVEL_FATAL },
88 { "ERROR", SYSLOG_LEVEL_ERROR },
89 { "INFO", SYSLOG_LEVEL_INFO },
90 { "VERBOSE", SYSLOG_LEVEL_VERBOSE },
91 { "DEBUG", SYSLOG_LEVEL_DEBUG1 },
92 { "DEBUG1", SYSLOG_LEVEL_DEBUG1 },
93 { "DEBUG2", SYSLOG_LEVEL_DEBUG2 },
94 { "DEBUG3", SYSLOG_LEVEL_DEBUG3 },
Damien Millerfcd93202002-02-05 12:26:34 +110095 { NULL, SYSLOG_LEVEL_NOT_SET }
Ben Lindstrom8a432f52001-03-05 07:24:46 +000096};
97
98SyslogFacility
99log_facility_number(char *name)
100{
101 int i;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000102
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000103 if (name != NULL)
104 for (i = 0; log_facilities[i].name; i++)
105 if (strcasecmp(log_facilities[i].name, name) == 0)
106 return log_facilities[i].val;
Damien Millerfcd93202002-02-05 12:26:34 +1100107 return SYSLOG_FACILITY_NOT_SET;
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000108}
109
110LogLevel
111log_level_number(char *name)
112{
113 int i;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000114
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000115 if (name != NULL)
116 for (i = 0; log_levels[i].name; i++)
117 if (strcasecmp(log_levels[i].name, name) == 0)
118 return log_levels[i].val;
Damien Millerfcd93202002-02-05 12:26:34 +1100119 return SYSLOG_LEVEL_NOT_SET;
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000120}
Damien Miller5ce662a1999-11-11 17:57:39 +1100121
122/* Error messages that should be logged. */
123
124void
Damien Miller95def091999-11-25 00:26:21 +1100125error(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +1100126{
Damien Miller95def091999-11-25 00:26:21 +1100127 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000128
Damien Miller95def091999-11-25 00:26:21 +1100129 va_start(args, fmt);
130 do_log(SYSLOG_LEVEL_ERROR, fmt, args);
131 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +1100132}
133
134/* Log this message (information that usually should go to the log). */
135
136void
Damien Miller996acd22003-04-09 20:59:48 +1000137logit(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +1100138{
Damien Miller95def091999-11-25 00:26:21 +1100139 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000140
Damien Miller95def091999-11-25 00:26:21 +1100141 va_start(args, fmt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000142 do_log(SYSLOG_LEVEL_INFO, fmt, args);
Damien Miller95def091999-11-25 00:26:21 +1100143 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +1100144}
145
146/* More detailed messages (information that does not need to go to the log). */
147
148void
Damien Miller95def091999-11-25 00:26:21 +1100149verbose(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +1100150{
Damien Miller95def091999-11-25 00:26:21 +1100151 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000152
Damien Miller95def091999-11-25 00:26:21 +1100153 va_start(args, fmt);
154 do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
155 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +1100156}
157
158/* Debugging messages that should not be logged during normal operation. */
159
160void
Damien Miller95def091999-11-25 00:26:21 +1100161debug(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +1100162{
Damien Miller95def091999-11-25 00:26:21 +1100163 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000164
Damien Miller95def091999-11-25 00:26:21 +1100165 va_start(args, fmt);
Damien Millere4340be2000-09-16 13:29:08 +1100166 do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
167 va_end(args);
168}
169
170void
171debug2(const char *fmt,...)
172{
173 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000174
Damien Millere4340be2000-09-16 13:29:08 +1100175 va_start(args, fmt);
176 do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
177 va_end(args);
178}
179
180void
181debug3(const char *fmt,...)
182{
183 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000184
Damien Millere4340be2000-09-16 13:29:08 +1100185 va_start(args, fmt);
186 do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
Damien Miller95def091999-11-25 00:26:21 +1100187 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +1100188}
189
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000190/*
191 * Initialize the log.
192 */
193
194void
195log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
196{
Darren Tucker9b5495d2005-02-01 17:35:09 +1100197#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
198 struct syslog_data sdata = SYSLOG_DATA_INIT;
199#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000200
Darren Tucker835903d2005-03-09 20:12:47 +1100201 argv0 = av0;
202
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000203 switch (level) {
204 case SYSLOG_LEVEL_QUIET:
205 case SYSLOG_LEVEL_FATAL:
206 case SYSLOG_LEVEL_ERROR:
207 case SYSLOG_LEVEL_INFO:
208 case SYSLOG_LEVEL_VERBOSE:
209 case SYSLOG_LEVEL_DEBUG1:
210 case SYSLOG_LEVEL_DEBUG2:
211 case SYSLOG_LEVEL_DEBUG3:
212 log_level = level;
213 break;
214 default:
Kevin Stevesedcd5762001-04-02 13:45:00 +0000215 fprintf(stderr, "Unrecognized internal syslog level code %d\n",
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000216 (int) level);
217 exit(1);
218 }
219
220 log_on_stderr = on_stderr;
221 if (on_stderr)
222 return;
223
224 switch (facility) {
225 case SYSLOG_FACILITY_DAEMON:
226 log_facility = LOG_DAEMON;
227 break;
228 case SYSLOG_FACILITY_USER:
229 log_facility = LOG_USER;
230 break;
231 case SYSLOG_FACILITY_AUTH:
232 log_facility = LOG_AUTH;
233 break;
Damien Miller30246a82001-03-05 21:23:31 +1100234#ifdef LOG_AUTHPRIV
235 case SYSLOG_FACILITY_AUTHPRIV:
236 log_facility = LOG_AUTHPRIV;
237 break;
Ben Lindstrom53f11c62001-03-05 08:18:17 +0000238#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000239 case SYSLOG_FACILITY_LOCAL0:
240 log_facility = LOG_LOCAL0;
241 break;
242 case SYSLOG_FACILITY_LOCAL1:
243 log_facility = LOG_LOCAL1;
244 break;
245 case SYSLOG_FACILITY_LOCAL2:
246 log_facility = LOG_LOCAL2;
247 break;
248 case SYSLOG_FACILITY_LOCAL3:
249 log_facility = LOG_LOCAL3;
250 break;
251 case SYSLOG_FACILITY_LOCAL4:
252 log_facility = LOG_LOCAL4;
253 break;
254 case SYSLOG_FACILITY_LOCAL5:
255 log_facility = LOG_LOCAL5;
256 break;
257 case SYSLOG_FACILITY_LOCAL6:
258 log_facility = LOG_LOCAL6;
259 break;
260 case SYSLOG_FACILITY_LOCAL7:
261 log_facility = LOG_LOCAL7;
262 break;
263 default:
264 fprintf(stderr,
Kevin Stevesedcd5762001-04-02 13:45:00 +0000265 "Unrecognized internal syslog facility code %d\n",
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000266 (int) facility);
267 exit(1);
268 }
Darren Tucker9b5495d2005-02-01 17:35:09 +1100269
270 /*
271 * If an external library (eg libwrap) attempts to use syslog
272 * immediately after reexec, syslog may be pointing to the wrong
273 * facility, so we force an open/close of syslog here.
274 */
275#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
276 openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
277 closelog_r(&sdata);
278#else
279 openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
280 closelog();
281#endif
Damien Miller6162d121999-11-21 13:23:52 +1100282}
283
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000284#define MSGBUFSIZ 1024
285
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000286void
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000287do_log(LogLevel level, const char *fmt, va_list args)
Damien Miller6162d121999-11-21 13:23:52 +1100288{
Damien Miller051b0ac2004-02-18 22:59:43 +1100289#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
Damien Miller74a34422003-05-20 09:24:17 +1000290 struct syslog_data sdata = SYSLOG_DATA_INIT;
291#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000292 char msgbuf[MSGBUFSIZ];
293 char fmtbuf[MSGBUFSIZ];
294 char *txt = NULL;
295 int pri = LOG_INFO;
296
297 if (level > log_level)
298 return;
299
300 switch (level) {
301 case SYSLOG_LEVEL_FATAL:
302 if (!log_on_stderr)
303 txt = "fatal";
304 pri = LOG_CRIT;
305 break;
306 case SYSLOG_LEVEL_ERROR:
307 if (!log_on_stderr)
308 txt = "error";
309 pri = LOG_ERR;
310 break;
311 case SYSLOG_LEVEL_INFO:
312 pri = LOG_INFO;
313 break;
314 case SYSLOG_LEVEL_VERBOSE:
315 pri = LOG_INFO;
316 break;
317 case SYSLOG_LEVEL_DEBUG1:
318 txt = "debug1";
319 pri = LOG_DEBUG;
320 break;
321 case SYSLOG_LEVEL_DEBUG2:
322 txt = "debug2";
323 pri = LOG_DEBUG;
324 break;
325 case SYSLOG_LEVEL_DEBUG3:
326 txt = "debug3";
327 pri = LOG_DEBUG;
328 break;
329 default:
330 txt = "internal error";
331 pri = LOG_ERR;
332 break;
333 }
334 if (txt != NULL) {
335 snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt);
336 vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
337 } else {
338 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
339 }
Damien Miller23a70272004-07-21 10:52:13 +1000340 strnvis(fmtbuf, msgbuf, sizeof(fmtbuf),
341 log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS);
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000342 if (log_on_stderr) {
Damien Millerc11fe252003-05-25 14:38:02 +1000343 snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf);
344 write(STDERR_FILENO, msgbuf, strlen(msgbuf));
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000345 } else {
Damien Miller051b0ac2004-02-18 22:59:43 +1100346#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
Damien Miller74a34422003-05-20 09:24:17 +1000347 openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
Damien Millerc11fe252003-05-25 14:38:02 +1000348 syslog_r(pri, &sdata, "%.500s", fmtbuf);
Damien Miller74a34422003-05-20 09:24:17 +1000349 closelog_r(&sdata);
350#else
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000351 openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
Damien Millerb93addb2003-01-07 17:04:18 +1100352 syslog(pri, "%.500s", fmtbuf);
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000353 closelog();
Damien Miller74a34422003-05-20 09:24:17 +1000354#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000355 }
Damien Miller6162d121999-11-21 13:23:52 +1100356}