]>
Commit | Line | Data |
---|---|---|
ffa517a8 | 1 | /* $OpenBSD: log.c,v 1.36 2006/07/26 13:57:17 stevesk Exp $ */ |
bcbf86ec | 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 | */ | |
6a17f9c2 | 13 | /* |
bcbf86ec | 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. | |
aa3378df | 35 | */ |
6a17f9c2 | 36 | |
37 | #include "includes.h" | |
6a17f9c2 | 38 | |
4c72fcfd | 39 | #include <stdarg.h> |
ffa517a8 | 40 | #include <stdlib.h> |
00146caa | 41 | #include <string.h> |
20244695 | 42 | #include <syslog.h> |
5188ba17 | 43 | #include <unistd.h> |
0a23d79f | 44 | #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) |
45 | # include <vis.h> | |
46 | #endif | |
20244695 | 47 | |
ca1df159 | 48 | #include "log.h" |
49 | #include "xmalloc.h" | |
50 | ||
20244695 | 51 | static LogLevel log_level = SYSLOG_LEVEL_INFO; |
52 | static int log_on_stderr = 1; | |
53 | static int log_facility = LOG_AUTH; | |
54 | static char *argv0; | |
55 | ||
56 | extern char *__progname; | |
57 | ||
ac87b3c2 | 58 | #define LOG_SYSLOG_VIS (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL) |
59 | #define LOG_STDERR_VIS (VIS_SAFE|VIS_OCTAL) | |
60 | ||
20244695 | 61 | /* textual representation of log-facilities/levels */ |
62 | ||
63 | static 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 }, | |
cc3067d6 | 70 | #ifdef LOG_AUTHPRIV |
71 | { "AUTHPRIV", SYSLOG_FACILITY_AUTHPRIV }, | |
72 | #endif | |
20244695 | 73 | { "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 }, | |
5eaf8578 | 81 | { NULL, SYSLOG_FACILITY_NOT_SET } |
20244695 | 82 | }; |
83 | ||
84 | static 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 }, | |
5eaf8578 | 98 | { NULL, SYSLOG_LEVEL_NOT_SET } |
20244695 | 99 | }; |
100 | ||
101 | SyslogFacility | |
102 | log_facility_number(char *name) | |
103 | { | |
104 | int i; | |
4394a17f | 105 | |
20244695 | 106 | 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; | |
5eaf8578 | 110 | return SYSLOG_FACILITY_NOT_SET; |
20244695 | 111 | } |
112 | ||
113 | LogLevel | |
114 | log_level_number(char *name) | |
115 | { | |
116 | int i; | |
4394a17f | 117 | |
20244695 | 118 | 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; | |
5eaf8578 | 122 | return SYSLOG_LEVEL_NOT_SET; |
20244695 | 123 | } |
6a17f9c2 | 124 | |
125 | /* Error messages that should be logged. */ | |
126 | ||
127 | void | |
5260325f | 128 | error(const char *fmt,...) |
6a17f9c2 | 129 | { |
5260325f | 130 | va_list args; |
4394a17f | 131 | |
5260325f | 132 | va_start(args, fmt); |
133 | do_log(SYSLOG_LEVEL_ERROR, fmt, args); | |
134 | va_end(args); | |
6a17f9c2 | 135 | } |
136 | ||
137 | /* Log this message (information that usually should go to the log). */ | |
138 | ||
139 | void | |
bbe88b6d | 140 | logit(const char *fmt,...) |
6a17f9c2 | 141 | { |
5260325f | 142 | va_list args; |
4394a17f | 143 | |
5260325f | 144 | va_start(args, fmt); |
59c97189 | 145 | do_log(SYSLOG_LEVEL_INFO, fmt, args); |
5260325f | 146 | va_end(args); |
6a17f9c2 | 147 | } |
148 | ||
149 | /* More detailed messages (information that does not need to go to the log). */ | |
150 | ||
151 | void | |
5260325f | 152 | verbose(const char *fmt,...) |
6a17f9c2 | 153 | { |
5260325f | 154 | va_list args; |
4394a17f | 155 | |
5260325f | 156 | va_start(args, fmt); |
157 | do_log(SYSLOG_LEVEL_VERBOSE, fmt, args); | |
158 | va_end(args); | |
6a17f9c2 | 159 | } |
160 | ||
161 | /* Debugging messages that should not be logged during normal operation. */ | |
162 | ||
163 | void | |
5260325f | 164 | debug(const char *fmt,...) |
6a17f9c2 | 165 | { |
5260325f | 166 | va_list args; |
4394a17f | 167 | |
5260325f | 168 | va_start(args, fmt); |
bcbf86ec | 169 | do_log(SYSLOG_LEVEL_DEBUG1, fmt, args); |
170 | va_end(args); | |
171 | } | |
172 | ||
173 | void | |
174 | debug2(const char *fmt,...) | |
175 | { | |
176 | va_list args; | |
4394a17f | 177 | |
bcbf86ec | 178 | va_start(args, fmt); |
179 | do_log(SYSLOG_LEVEL_DEBUG2, fmt, args); | |
180 | va_end(args); | |
181 | } | |
182 | ||
183 | void | |
184 | debug3(const char *fmt,...) | |
185 | { | |
186 | va_list args; | |
4394a17f | 187 | |
bcbf86ec | 188 | va_start(args, fmt); |
189 | do_log(SYSLOG_LEVEL_DEBUG3, fmt, args); | |
5260325f | 190 | va_end(args); |
6a17f9c2 | 191 | } |
192 | ||
20244695 | 193 | /* |
194 | * Initialize the log. | |
195 | */ | |
9d6b7add | 196 | |
20244695 | 197 | void |
198 | log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr) | |
9d6b7add | 199 | { |
29c82270 | 200 | #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT) |
201 | struct syslog_data sdata = SYSLOG_DATA_INIT; | |
202 | #endif | |
9d6b7add | 203 | |
b1d73a9a | 204 | argv0 = av0; |
205 | ||
20244695 | 206 | 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: | |
0be033ea | 218 | fprintf(stderr, "Unrecognized internal syslog level code %d\n", |
20244695 | 219 | (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; | |
cc3067d6 | 237 | #ifdef LOG_AUTHPRIV |
238 | case SYSLOG_FACILITY_AUTHPRIV: | |
239 | log_facility = LOG_AUTHPRIV; | |
240 | break; | |
20244695 | 241 | #endif |
242 | 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, | |
0be033ea | 268 | "Unrecognized internal syslog facility code %d\n", |
20244695 | 269 | (int) facility); |
270 | exit(1); | |
271 | } | |
29c82270 | 272 | |
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 | |
9d6b7add | 285 | } |
286 | ||
20244695 | 287 | #define MSGBUFSIZ 1024 |
288 | ||
2bae80e9 | 289 | void |
20244695 | 290 | do_log(LogLevel level, const char *fmt, va_list args) |
9d6b7add | 291 | { |
a90ed4b3 | 292 | #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT) |
c453493f | 293 | struct syslog_data sdata = SYSLOG_DATA_INIT; |
294 | #endif | |
20244695 | 295 | 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 | } | |
ac87b3c2 | 343 | strnvis(fmtbuf, msgbuf, sizeof(fmtbuf), |
344 | log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS); | |
20244695 | 345 | if (log_on_stderr) { |
5b0fe364 | 346 | snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf); |
347 | write(STDERR_FILENO, msgbuf, strlen(msgbuf)); | |
20244695 | 348 | } else { |
a90ed4b3 | 349 | #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT) |
c453493f | 350 | openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata); |
5b0fe364 | 351 | syslog_r(pri, &sdata, "%.500s", fmtbuf); |
c453493f | 352 | closelog_r(&sdata); |
353 | #else | |
20244695 | 354 | openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility); |
63cada0a | 355 | syslog(pri, "%.500s", fmtbuf); |
20244695 | 356 | closelog(); |
c453493f | 357 | #endif |
20244695 | 358 | } |
9d6b7add | 359 | } |