]> andersk Git - openssh.git/blob - log.c
- (djm) OpenBSD CVS Sync
[openssh.git] / log.c
1 /*
2  * 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 /*
13  * 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.
34  */
35
36 #include "includes.h"
37 RCSID("$OpenBSD: log.c,v 1.28 2003/05/24 09:02:22 djm Exp $");
38
39 #include "log.h"
40 #include "xmalloc.h"
41
42 #include <syslog.h>
43 #include <vis.h>
44
45 static LogLevel log_level = SYSLOG_LEVEL_INFO;
46 static int log_on_stderr = 1;
47 static int log_facility = LOG_AUTH;
48 static char *argv0;
49
50 extern char *__progname;
51
52 /* textual representation of log-facilities/levels */
53
54 static 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 },
61 #ifdef LOG_AUTHPRIV
62         { "AUTHPRIV",   SYSLOG_FACILITY_AUTHPRIV },
63 #endif
64         { "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 },
72         { NULL,         SYSLOG_FACILITY_NOT_SET }
73 };
74
75 static 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 },
89         { NULL,         SYSLOG_LEVEL_NOT_SET }
90 };
91
92 SyslogFacility
93 log_facility_number(char *name)
94 {
95         int i;
96
97         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;
101         return SYSLOG_FACILITY_NOT_SET;
102 }
103
104 LogLevel
105 log_level_number(char *name)
106 {
107         int i;
108
109         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;
113         return SYSLOG_LEVEL_NOT_SET;
114 }
115
116 /* Error messages that should be logged. */
117
118 void
119 error(const char *fmt,...)
120 {
121         va_list args;
122
123         va_start(args, fmt);
124         do_log(SYSLOG_LEVEL_ERROR, fmt, args);
125         va_end(args);
126 }
127
128 /* Log this message (information that usually should go to the log). */
129
130 void
131 logit(const char *fmt,...)
132 {
133         va_list args;
134
135         va_start(args, fmt);
136         do_log(SYSLOG_LEVEL_INFO, fmt, args);
137         va_end(args);
138 }
139
140 /* More detailed messages (information that does not need to go to the log). */
141
142 void
143 verbose(const char *fmt,...)
144 {
145         va_list args;
146
147         va_start(args, fmt);
148         do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
149         va_end(args);
150 }
151
152 /* Debugging messages that should not be logged during normal operation. */
153
154 void
155 debug(const char *fmt,...)
156 {
157         va_list args;
158
159         va_start(args, fmt);
160         do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
161         va_end(args);
162 }
163
164 void
165 debug2(const char *fmt,...)
166 {
167         va_list args;
168
169         va_start(args, fmt);
170         do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
171         va_end(args);
172 }
173
174 void
175 debug3(const char *fmt,...)
176 {
177         va_list args;
178
179         va_start(args, fmt);
180         do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
181         va_end(args);
182 }
183
184 /* Fatal cleanup */
185
186 struct fatal_cleanup {
187         struct fatal_cleanup *next;
188         void (*proc) (void *);
189         void *context;
190 };
191
192 static struct fatal_cleanup *fatal_cleanups = NULL;
193
194 /* Registers a cleanup function to be called by fatal() before exiting. */
195
196 void
197 fatal_add_cleanup(void (*proc) (void *), void *context)
198 {
199         struct fatal_cleanup *cu;
200
201         cu = xmalloc(sizeof(*cu));
202         cu->proc = proc;
203         cu->context = context;
204         cu->next = fatal_cleanups;
205         fatal_cleanups = cu;
206 }
207
208 /* Removes a cleanup frunction to be called at fatal(). */
209
210 void
211 fatal_remove_cleanup(void (*proc) (void *context), void *context)
212 {
213         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                 }
222         }
223         fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx",
224             (u_long) proc, (u_long) context);
225 }
226
227 /* Remove all cleanups, to be called after fork() */
228 void
229 fatal_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         }
237         fatal_cleanups = NULL;
238 }
239
240 /* Cleanup and exit */
241 void
242 fatal_cleanup(void)
243 {
244         struct fatal_cleanup *cu, *next_cu;
245         static int called = 0;
246
247         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)",
254                     (u_long) cu->proc, (u_long) cu->context);
255                 (*cu->proc) (cu->context);
256         }
257         exit(255);
258 }
259
260
261 /*
262  * Initialize the log.
263  */
264
265 void
266 log_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:
282                 fprintf(stderr, "Unrecognized internal syslog level code %d\n",
283                     (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;
301 #ifdef LOG_AUTHPRIV
302         case SYSLOG_FACILITY_AUTHPRIV:
303                 log_facility = LOG_AUTHPRIV;
304                 break;
305 #endif
306         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,
332                     "Unrecognized internal syslog facility code %d\n",
333                     (int) facility);
334                 exit(1);
335         }
336 }
337
338 #define MSGBUFSIZ 1024
339
340 void
341 do_log(LogLevel level, const char *fmt, va_list args)
342 {
343 #ifdef OPENLOG_R
344         struct syslog_data sdata = SYSLOG_DATA_INIT;
345 #endif
346         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         }
394         strnvis(fmtbuf, msgbuf, sizeof(fmtbuf), VIS_SAFE|VIS_OCTAL);
395         if (log_on_stderr) {
396                 snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf);
397                 write(STDERR_FILENO, msgbuf, strlen(msgbuf));
398         } else {
399 #ifdef OPENLOG_R
400                 openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
401                 syslog_r(pri, &sdata, "%.500s", fmtbuf);
402                 closelog_r(&sdata);
403 #else
404                 openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
405                 syslog(pri, "%.500s", fmtbuf);
406                 closelog();
407 #endif
408         }
409 }
This page took 0.32448 seconds and 5 git commands to generate.