]> andersk Git - openssh.git/blob - log.c
6eac73743327ebda60baf9495ca06c95f4ed9681
[openssh.git] / log.c
1 /* $OpenBSD: log.c,v 1.38 2006/08/03 03:34:42 deraadt Exp $ */
2 /*
3  * 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 /*
14  * 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.
35  */
36
37 #include "includes.h"
38
39 #include <sys/types.h>
40
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <syslog.h>
46 #include <unistd.h>
47 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
48 # include <vis.h>
49 #endif
50
51 #include "xmalloc.h"
52 #include "log.h"
53
54 static LogLevel log_level = SYSLOG_LEVEL_INFO;
55 static int log_on_stderr = 1;
56 static int log_facility = LOG_AUTH;
57 static char *argv0;
58
59 extern char *__progname;
60
61 #define LOG_SYSLOG_VIS  (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL)
62 #define LOG_STDERR_VIS  (VIS_SAFE|VIS_OCTAL)
63
64 /* textual representation of log-facilities/levels */
65
66 static struct {
67         const char *name;
68         SyslogFacility val;
69 } log_facilities[] = {
70         { "DAEMON",     SYSLOG_FACILITY_DAEMON },
71         { "USER",       SYSLOG_FACILITY_USER },
72         { "AUTH",       SYSLOG_FACILITY_AUTH },
73 #ifdef LOG_AUTHPRIV
74         { "AUTHPRIV",   SYSLOG_FACILITY_AUTHPRIV },
75 #endif
76         { "LOCAL0",     SYSLOG_FACILITY_LOCAL0 },
77         { "LOCAL1",     SYSLOG_FACILITY_LOCAL1 },
78         { "LOCAL2",     SYSLOG_FACILITY_LOCAL2 },
79         { "LOCAL3",     SYSLOG_FACILITY_LOCAL3 },
80         { "LOCAL4",     SYSLOG_FACILITY_LOCAL4 },
81         { "LOCAL5",     SYSLOG_FACILITY_LOCAL5 },
82         { "LOCAL6",     SYSLOG_FACILITY_LOCAL6 },
83         { "LOCAL7",     SYSLOG_FACILITY_LOCAL7 },
84         { NULL,         SYSLOG_FACILITY_NOT_SET }
85 };
86
87 static struct {
88         const char *name;
89         LogLevel val;
90 } log_levels[] =
91 {
92         { "QUIET",      SYSLOG_LEVEL_QUIET },
93         { "FATAL",      SYSLOG_LEVEL_FATAL },
94         { "ERROR",      SYSLOG_LEVEL_ERROR },
95         { "INFO",       SYSLOG_LEVEL_INFO },
96         { "VERBOSE",    SYSLOG_LEVEL_VERBOSE },
97         { "DEBUG",      SYSLOG_LEVEL_DEBUG1 },
98         { "DEBUG1",     SYSLOG_LEVEL_DEBUG1 },
99         { "DEBUG2",     SYSLOG_LEVEL_DEBUG2 },
100         { "DEBUG3",     SYSLOG_LEVEL_DEBUG3 },
101         { NULL,         SYSLOG_LEVEL_NOT_SET }
102 };
103
104 SyslogFacility
105 log_facility_number(char *name)
106 {
107         int i;
108
109         if (name != NULL)
110                 for (i = 0; log_facilities[i].name; i++)
111                         if (strcasecmp(log_facilities[i].name, name) == 0)
112                                 return log_facilities[i].val;
113         return SYSLOG_FACILITY_NOT_SET;
114 }
115
116 LogLevel
117 log_level_number(char *name)
118 {
119         int i;
120
121         if (name != NULL)
122                 for (i = 0; log_levels[i].name; i++)
123                         if (strcasecmp(log_levels[i].name, name) == 0)
124                                 return log_levels[i].val;
125         return SYSLOG_LEVEL_NOT_SET;
126 }
127
128 /* Error messages that should be logged. */
129
130 void
131 error(const char *fmt,...)
132 {
133         va_list args;
134
135         va_start(args, fmt);
136         do_log(SYSLOG_LEVEL_ERROR, fmt, args);
137         va_end(args);
138 }
139
140 /* Log this message (information that usually should go to the log). */
141
142 void
143 logit(const char *fmt,...)
144 {
145         va_list args;
146
147         va_start(args, fmt);
148         do_log(SYSLOG_LEVEL_INFO, fmt, args);
149         va_end(args);
150 }
151
152 /* More detailed messages (information that does not need to go to the log). */
153
154 void
155 verbose(const char *fmt,...)
156 {
157         va_list args;
158
159         va_start(args, fmt);
160         do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
161         va_end(args);
162 }
163
164 /* Debugging messages that should not be logged during normal operation. */
165
166 void
167 debug(const char *fmt,...)
168 {
169         va_list args;
170
171         va_start(args, fmt);
172         do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
173         va_end(args);
174 }
175
176 void
177 debug2(const char *fmt,...)
178 {
179         va_list args;
180
181         va_start(args, fmt);
182         do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
183         va_end(args);
184 }
185
186 void
187 debug3(const char *fmt,...)
188 {
189         va_list args;
190
191         va_start(args, fmt);
192         do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
193         va_end(args);
194 }
195
196 /*
197  * Initialize the log.
198  */
199
200 void
201 log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
202 {
203 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
204         struct syslog_data sdata = SYSLOG_DATA_INIT;
205 #endif
206
207         argv0 = av0;
208
209         switch (level) {
210         case SYSLOG_LEVEL_QUIET:
211         case SYSLOG_LEVEL_FATAL:
212         case SYSLOG_LEVEL_ERROR:
213         case SYSLOG_LEVEL_INFO:
214         case SYSLOG_LEVEL_VERBOSE:
215         case SYSLOG_LEVEL_DEBUG1:
216         case SYSLOG_LEVEL_DEBUG2:
217         case SYSLOG_LEVEL_DEBUG3:
218                 log_level = level;
219                 break;
220         default:
221                 fprintf(stderr, "Unrecognized internal syslog level code %d\n",
222                     (int) level);
223                 exit(1);
224         }
225
226         log_on_stderr = on_stderr;
227         if (on_stderr)
228                 return;
229
230         switch (facility) {
231         case SYSLOG_FACILITY_DAEMON:
232                 log_facility = LOG_DAEMON;
233                 break;
234         case SYSLOG_FACILITY_USER:
235                 log_facility = LOG_USER;
236                 break;
237         case SYSLOG_FACILITY_AUTH:
238                 log_facility = LOG_AUTH;
239                 break;
240 #ifdef LOG_AUTHPRIV
241         case SYSLOG_FACILITY_AUTHPRIV:
242                 log_facility = LOG_AUTHPRIV;
243                 break;
244 #endif
245         case SYSLOG_FACILITY_LOCAL0:
246                 log_facility = LOG_LOCAL0;
247                 break;
248         case SYSLOG_FACILITY_LOCAL1:
249                 log_facility = LOG_LOCAL1;
250                 break;
251         case SYSLOG_FACILITY_LOCAL2:
252                 log_facility = LOG_LOCAL2;
253                 break;
254         case SYSLOG_FACILITY_LOCAL3:
255                 log_facility = LOG_LOCAL3;
256                 break;
257         case SYSLOG_FACILITY_LOCAL4:
258                 log_facility = LOG_LOCAL4;
259                 break;
260         case SYSLOG_FACILITY_LOCAL5:
261                 log_facility = LOG_LOCAL5;
262                 break;
263         case SYSLOG_FACILITY_LOCAL6:
264                 log_facility = LOG_LOCAL6;
265                 break;
266         case SYSLOG_FACILITY_LOCAL7:
267                 log_facility = LOG_LOCAL7;
268                 break;
269         default:
270                 fprintf(stderr,
271                     "Unrecognized internal syslog facility code %d\n",
272                     (int) facility);
273                 exit(1);
274         }
275
276         /*
277          * If an external library (eg libwrap) attempts to use syslog
278          * immediately after reexec, syslog may be pointing to the wrong
279          * facility, so we force an open/close of syslog here.
280          */
281 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
282         openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
283         closelog_r(&sdata);
284 #else
285         openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
286         closelog();
287 #endif
288 }
289
290 #define MSGBUFSIZ 1024
291
292 void
293 do_log(LogLevel level, const char *fmt, va_list args)
294 {
295 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
296         struct syslog_data sdata = SYSLOG_DATA_INIT;
297 #endif
298         char msgbuf[MSGBUFSIZ];
299         char fmtbuf[MSGBUFSIZ];
300         char *txt = NULL;
301         int pri = LOG_INFO;
302
303         if (level > log_level)
304                 return;
305
306         switch (level) {
307         case SYSLOG_LEVEL_FATAL:
308                 if (!log_on_stderr)
309                         txt = "fatal";
310                 pri = LOG_CRIT;
311                 break;
312         case SYSLOG_LEVEL_ERROR:
313                 if (!log_on_stderr)
314                         txt = "error";
315                 pri = LOG_ERR;
316                 break;
317         case SYSLOG_LEVEL_INFO:
318                 pri = LOG_INFO;
319                 break;
320         case SYSLOG_LEVEL_VERBOSE:
321                 pri = LOG_INFO;
322                 break;
323         case SYSLOG_LEVEL_DEBUG1:
324                 txt = "debug1";
325                 pri = LOG_DEBUG;
326                 break;
327         case SYSLOG_LEVEL_DEBUG2:
328                 txt = "debug2";
329                 pri = LOG_DEBUG;
330                 break;
331         case SYSLOG_LEVEL_DEBUG3:
332                 txt = "debug3";
333                 pri = LOG_DEBUG;
334                 break;
335         default:
336                 txt = "internal error";
337                 pri = LOG_ERR;
338                 break;
339         }
340         if (txt != NULL) {
341                 snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt);
342                 vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
343         } else {
344                 vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
345         }
346         strnvis(fmtbuf, msgbuf, sizeof(fmtbuf),
347             log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS);
348         if (log_on_stderr) {
349                 snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf);
350                 write(STDERR_FILENO, msgbuf, strlen(msgbuf));
351         } else {
352 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
353                 openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
354                 syslog_r(pri, &sdata, "%.500s", fmtbuf);
355                 closelog_r(&sdata);
356 #else
357                 openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
358                 syslog(pri, "%.500s", fmtbuf);
359                 closelog();
360 #endif
361         }
362 }
This page took 0.050855 seconds and 3 git commands to generate.