blob: fb89a965bff1a493a223119cbd4d7b89c543c728 [file] [log] [blame]
Damien Miller5ce662a1999-11-11 17:57:39 +11001/*
Damien Millere4340be2000-09-16 13:29:08 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5 *
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
11 */
12/*
Damien Millere4340be2000-09-16 13:29:08 +110013 * Copyright (c) 2000 Markus Friedl. All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller5428f641999-11-25 11:54:57 +110034 */
Damien Miller5ce662a1999-11-11 17:57:39 +110035
36#include "includes.h"
Damien Millerc11fe252003-05-25 14:38:02 +100037RCSID("$OpenBSD: log.c,v 1.28 2003/05/24 09:02:22 djm Exp $");
Damien Miller5ce662a1999-11-11 17:57:39 +110038
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "log.h"
Damien Miller5ce662a1999-11-11 17:57:39 +110040#include "xmalloc.h"
41
Ben Lindstrom8a432f52001-03-05 07:24:46 +000042#include <syslog.h>
Damien Millerc11fe252003-05-25 14:38:02 +100043#include <vis.h>
Ben Lindstrom8a432f52001-03-05 07:24:46 +000044
45static LogLevel log_level = SYSLOG_LEVEL_INFO;
46static int log_on_stderr = 1;
47static int log_facility = LOG_AUTH;
48static char *argv0;
49
50extern char *__progname;
51
52/* textual representation of log-facilities/levels */
53
54static struct {
55 const char *name;
56 SyslogFacility val;
57} log_facilities[] = {
58 { "DAEMON", SYSLOG_FACILITY_DAEMON },
59 { "USER", SYSLOG_FACILITY_USER },
60 { "AUTH", SYSLOG_FACILITY_AUTH },
Damien Miller30246a82001-03-05 21:23:31 +110061#ifdef LOG_AUTHPRIV
62 { "AUTHPRIV", SYSLOG_FACILITY_AUTHPRIV },
63#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +000064 { "LOCAL0", SYSLOG_FACILITY_LOCAL0 },
65 { "LOCAL1", SYSLOG_FACILITY_LOCAL1 },
66 { "LOCAL2", SYSLOG_FACILITY_LOCAL2 },
67 { "LOCAL3", SYSLOG_FACILITY_LOCAL3 },
68 { "LOCAL4", SYSLOG_FACILITY_LOCAL4 },
69 { "LOCAL5", SYSLOG_FACILITY_LOCAL5 },
70 { "LOCAL6", SYSLOG_FACILITY_LOCAL6 },
71 { "LOCAL7", SYSLOG_FACILITY_LOCAL7 },
Damien Millerfcd93202002-02-05 12:26:34 +110072 { NULL, SYSLOG_FACILITY_NOT_SET }
Ben Lindstrom8a432f52001-03-05 07:24:46 +000073};
74
75static struct {
76 const char *name;
77 LogLevel val;
78} log_levels[] =
79{
80 { "QUIET", SYSLOG_LEVEL_QUIET },
81 { "FATAL", SYSLOG_LEVEL_FATAL },
82 { "ERROR", SYSLOG_LEVEL_ERROR },
83 { "INFO", SYSLOG_LEVEL_INFO },
84 { "VERBOSE", SYSLOG_LEVEL_VERBOSE },
85 { "DEBUG", SYSLOG_LEVEL_DEBUG1 },
86 { "DEBUG1", SYSLOG_LEVEL_DEBUG1 },
87 { "DEBUG2", SYSLOG_LEVEL_DEBUG2 },
88 { "DEBUG3", SYSLOG_LEVEL_DEBUG3 },
Damien Millerfcd93202002-02-05 12:26:34 +110089 { NULL, SYSLOG_LEVEL_NOT_SET }
Ben Lindstrom8a432f52001-03-05 07:24:46 +000090};
91
92SyslogFacility
93log_facility_number(char *name)
94{
95 int i;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +000096
Ben Lindstrom8a432f52001-03-05 07:24:46 +000097 if (name != NULL)
98 for (i = 0; log_facilities[i].name; i++)
99 if (strcasecmp(log_facilities[i].name, name) == 0)
100 return log_facilities[i].val;
Damien Millerfcd93202002-02-05 12:26:34 +1100101 return SYSLOG_FACILITY_NOT_SET;
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000102}
103
104LogLevel
105log_level_number(char *name)
106{
107 int i;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000108
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000109 if (name != NULL)
110 for (i = 0; log_levels[i].name; i++)
111 if (strcasecmp(log_levels[i].name, name) == 0)
112 return log_levels[i].val;
Damien Millerfcd93202002-02-05 12:26:34 +1100113 return SYSLOG_LEVEL_NOT_SET;
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000114}
Damien Miller5ce662a1999-11-11 17:57:39 +1100115
116/* Error messages that should be logged. */
117
118void
Damien Miller95def091999-11-25 00:26:21 +1100119error(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +1100120{
Damien Miller95def091999-11-25 00:26:21 +1100121 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000122
Damien Miller95def091999-11-25 00:26:21 +1100123 va_start(args, fmt);
124 do_log(SYSLOG_LEVEL_ERROR, fmt, args);
125 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +1100126}
127
128/* Log this message (information that usually should go to the log). */
129
130void
Damien Miller996acd22003-04-09 20:59:48 +1000131logit(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +1100132{
Damien Miller95def091999-11-25 00:26:21 +1100133 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000134
Damien Miller95def091999-11-25 00:26:21 +1100135 va_start(args, fmt);
Ben Lindstromdb65e8f2001-01-19 04:26:52 +0000136 do_log(SYSLOG_LEVEL_INFO, fmt, args);
Damien Miller95def091999-11-25 00:26:21 +1100137 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +1100138}
139
140/* More detailed messages (information that does not need to go to the log). */
141
142void
Damien Miller95def091999-11-25 00:26:21 +1100143verbose(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +1100144{
Damien Miller95def091999-11-25 00:26:21 +1100145 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000146
Damien Miller95def091999-11-25 00:26:21 +1100147 va_start(args, fmt);
148 do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
149 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +1100150}
151
152/* Debugging messages that should not be logged during normal operation. */
153
154void
Damien Miller95def091999-11-25 00:26:21 +1100155debug(const char *fmt,...)
Damien Miller5ce662a1999-11-11 17:57:39 +1100156{
Damien Miller95def091999-11-25 00:26:21 +1100157 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000158
Damien Miller95def091999-11-25 00:26:21 +1100159 va_start(args, fmt);
Damien Millere4340be2000-09-16 13:29:08 +1100160 do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
161 va_end(args);
162}
163
164void
165debug2(const char *fmt,...)
166{
167 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000168
Damien Millere4340be2000-09-16 13:29:08 +1100169 va_start(args, fmt);
170 do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
171 va_end(args);
172}
173
174void
175debug3(const char *fmt,...)
176{
177 va_list args;
Ben Lindstrom8e8ef2a2002-07-07 22:14:55 +0000178
Damien Millere4340be2000-09-16 13:29:08 +1100179 va_start(args, fmt);
180 do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
Damien Miller95def091999-11-25 00:26:21 +1100181 va_end(args);
Damien Miller5ce662a1999-11-11 17:57:39 +1100182}
183
184/* Fatal cleanup */
185
Damien Miller95def091999-11-25 00:26:21 +1100186struct fatal_cleanup {
187 struct fatal_cleanup *next;
188 void (*proc) (void *);
189 void *context;
Damien Miller5ce662a1999-11-11 17:57:39 +1100190};
191
192static struct fatal_cleanup *fatal_cleanups = NULL;
193
194/* Registers a cleanup function to be called by fatal() before exiting. */
195
196void
Damien Miller95def091999-11-25 00:26:21 +1100197fatal_add_cleanup(void (*proc) (void *), void *context)
Damien Miller5ce662a1999-11-11 17:57:39 +1100198{
Damien Miller95def091999-11-25 00:26:21 +1100199 struct fatal_cleanup *cu;
Damien Miller5ce662a1999-11-11 17:57:39 +1100200
Damien Miller95def091999-11-25 00:26:21 +1100201 cu = xmalloc(sizeof(*cu));
202 cu->proc = proc;
203 cu->context = context;
204 cu->next = fatal_cleanups;
205 fatal_cleanups = cu;
Damien Miller5ce662a1999-11-11 17:57:39 +1100206}
207
208/* Removes a cleanup frunction to be called at fatal(). */
209
210void
Damien Miller95def091999-11-25 00:26:21 +1100211fatal_remove_cleanup(void (*proc) (void *context), void *context)
Damien Miller5ce662a1999-11-11 17:57:39 +1100212{
Damien Miller95def091999-11-25 00:26:21 +1100213 struct fatal_cleanup **cup, *cu;
214
215 for (cup = &fatal_cleanups; *cup; cup = &cu->next) {
216 cu = *cup;
217 if (cu->proc == proc && cu->context == context) {
218 *cup = cu->next;
219 xfree(cu);
220 return;
221 }
Damien Miller5ce662a1999-11-11 17:57:39 +1100222 }
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000223 fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx",
224 (u_long) proc, (u_long) context);
Damien Miller5ce662a1999-11-11 17:57:39 +1100225}
226
Ben Lindstrom264ee302002-07-23 21:01:56 +0000227/* Remove all cleanups, to be called after fork() */
228void
229fatal_remove_all_cleanups(void)
230{
231 struct fatal_cleanup *cu, *next_cu;
232
233 for (cu = fatal_cleanups; cu; cu = next_cu) {
234 next_cu = cu->next;
235 xfree(cu);
236 }
Damien Miller0946d872003-01-14 22:22:43 +1100237 fatal_cleanups = NULL;
Ben Lindstrom264ee302002-07-23 21:01:56 +0000238}
239
Damien Miller5ce662a1999-11-11 17:57:39 +1100240/* Cleanup and exit */
241void
242fatal_cleanup(void)
243{
Damien Miller95def091999-11-25 00:26:21 +1100244 struct fatal_cleanup *cu, *next_cu;
245 static int called = 0;
Damien Miller5ce662a1999-11-11 17:57:39 +1100246
Damien Miller95def091999-11-25 00:26:21 +1100247 if (called)
248 exit(255);
249 called = 1;
250 /* Call cleanup functions. */
251 for (cu = fatal_cleanups; cu; cu = next_cu) {
252 next_cu = cu->next;
253 debug("Calling cleanup 0x%lx(0x%lx)",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100254 (u_long) cu->proc, (u_long) cu->context);
Damien Miller95def091999-11-25 00:26:21 +1100255 (*cu->proc) (cu->context);
256 }
257 exit(255);
Damien Miller5ce662a1999-11-11 17:57:39 +1100258}
Damien Miller6162d121999-11-21 13:23:52 +1100259
Damien Miller6162d121999-11-21 13:23:52 +1100260
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000261/*
262 * Initialize the log.
263 */
264
265void
266log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
267{
268 argv0 = av0;
269
270 switch (level) {
271 case SYSLOG_LEVEL_QUIET:
272 case SYSLOG_LEVEL_FATAL:
273 case SYSLOG_LEVEL_ERROR:
274 case SYSLOG_LEVEL_INFO:
275 case SYSLOG_LEVEL_VERBOSE:
276 case SYSLOG_LEVEL_DEBUG1:
277 case SYSLOG_LEVEL_DEBUG2:
278 case SYSLOG_LEVEL_DEBUG3:
279 log_level = level;
280 break;
281 default:
Kevin Stevesedcd5762001-04-02 13:45:00 +0000282 fprintf(stderr, "Unrecognized internal syslog level code %d\n",
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000283 (int) level);
284 exit(1);
285 }
286
287 log_on_stderr = on_stderr;
288 if (on_stderr)
289 return;
290
291 switch (facility) {
292 case SYSLOG_FACILITY_DAEMON:
293 log_facility = LOG_DAEMON;
294 break;
295 case SYSLOG_FACILITY_USER:
296 log_facility = LOG_USER;
297 break;
298 case SYSLOG_FACILITY_AUTH:
299 log_facility = LOG_AUTH;
300 break;
Damien Miller30246a82001-03-05 21:23:31 +1100301#ifdef LOG_AUTHPRIV
302 case SYSLOG_FACILITY_AUTHPRIV:
303 log_facility = LOG_AUTHPRIV;
304 break;
Ben Lindstrom53f11c62001-03-05 08:18:17 +0000305#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000306 case SYSLOG_FACILITY_LOCAL0:
307 log_facility = LOG_LOCAL0;
308 break;
309 case SYSLOG_FACILITY_LOCAL1:
310 log_facility = LOG_LOCAL1;
311 break;
312 case SYSLOG_FACILITY_LOCAL2:
313 log_facility = LOG_LOCAL2;
314 break;
315 case SYSLOG_FACILITY_LOCAL3:
316 log_facility = LOG_LOCAL3;
317 break;
318 case SYSLOG_FACILITY_LOCAL4:
319 log_facility = LOG_LOCAL4;
320 break;
321 case SYSLOG_FACILITY_LOCAL5:
322 log_facility = LOG_LOCAL5;
323 break;
324 case SYSLOG_FACILITY_LOCAL6:
325 log_facility = LOG_LOCAL6;
326 break;
327 case SYSLOG_FACILITY_LOCAL7:
328 log_facility = LOG_LOCAL7;
329 break;
330 default:
331 fprintf(stderr,
Kevin Stevesedcd5762001-04-02 13:45:00 +0000332 "Unrecognized internal syslog facility code %d\n",
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000333 (int) facility);
334 exit(1);
335 }
Damien Miller6162d121999-11-21 13:23:52 +1100336}
337
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000338#define MSGBUFSIZ 1024
339
Ben Lindstrom9c8edc92002-02-26 17:52:14 +0000340void
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000341do_log(LogLevel level, const char *fmt, va_list args)
Damien Miller6162d121999-11-21 13:23:52 +1100342{
Damien Miller74a34422003-05-20 09:24:17 +1000343#ifdef OPENLOG_R
344 struct syslog_data sdata = SYSLOG_DATA_INIT;
345#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000346 char msgbuf[MSGBUFSIZ];
347 char fmtbuf[MSGBUFSIZ];
348 char *txt = NULL;
349 int pri = LOG_INFO;
350
351 if (level > log_level)
352 return;
353
354 switch (level) {
355 case SYSLOG_LEVEL_FATAL:
356 if (!log_on_stderr)
357 txt = "fatal";
358 pri = LOG_CRIT;
359 break;
360 case SYSLOG_LEVEL_ERROR:
361 if (!log_on_stderr)
362 txt = "error";
363 pri = LOG_ERR;
364 break;
365 case SYSLOG_LEVEL_INFO:
366 pri = LOG_INFO;
367 break;
368 case SYSLOG_LEVEL_VERBOSE:
369 pri = LOG_INFO;
370 break;
371 case SYSLOG_LEVEL_DEBUG1:
372 txt = "debug1";
373 pri = LOG_DEBUG;
374 break;
375 case SYSLOG_LEVEL_DEBUG2:
376 txt = "debug2";
377 pri = LOG_DEBUG;
378 break;
379 case SYSLOG_LEVEL_DEBUG3:
380 txt = "debug3";
381 pri = LOG_DEBUG;
382 break;
383 default:
384 txt = "internal error";
385 pri = LOG_ERR;
386 break;
387 }
388 if (txt != NULL) {
389 snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt);
390 vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
391 } else {
392 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
393 }
Damien Millerd419bda2003-05-23 18:43:40 +1000394 strnvis(fmtbuf, msgbuf, sizeof(fmtbuf), VIS_SAFE|VIS_OCTAL);
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000395 if (log_on_stderr) {
Damien Millerc11fe252003-05-25 14:38:02 +1000396 snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf);
397 write(STDERR_FILENO, msgbuf, strlen(msgbuf));
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000398 } else {
Damien Miller74a34422003-05-20 09:24:17 +1000399#ifdef OPENLOG_R
400 openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
Damien Millerc11fe252003-05-25 14:38:02 +1000401 syslog_r(pri, &sdata, "%.500s", fmtbuf);
Damien Miller74a34422003-05-20 09:24:17 +1000402 closelog_r(&sdata);
403#else
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000404 openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
Damien Millerb93addb2003-01-07 17:04:18 +1100405 syslog(pri, "%.500s", fmtbuf);
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000406 closelog();
Damien Miller74a34422003-05-20 09:24:17 +1000407#endif
Ben Lindstrom8a432f52001-03-05 07:24:46 +0000408 }
Damien Miller6162d121999-11-21 13:23:52 +1100409}