]> andersk Git - openssh.git/blame - log.c
- stevesk@cvs.openbsd.org 2006/07/10 16:37:36
[openssh.git] / log.c
CommitLineData
4c72fcfd 1/* $OpenBSD: log.c,v 1.33 2006/07/10 16:37:36 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>
20244695 40#include <syslog.h>
0a23d79f 41#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
42# include <vis.h>
43#endif
20244695 44
ca1df159 45#include "log.h"
46#include "xmalloc.h"
47
20244695 48static 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
ac87b3c2 55#define LOG_SYSLOG_VIS (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL)
56#define LOG_STDERR_VIS (VIS_SAFE|VIS_OCTAL)
57
20244695 58/* 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 },
cc3067d6 67#ifdef LOG_AUTHPRIV
68 { "AUTHPRIV", SYSLOG_FACILITY_AUTHPRIV },
69#endif
20244695 70 { "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 },
5eaf8578 78 { NULL, SYSLOG_FACILITY_NOT_SET }
20244695 79};
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 },
5eaf8578 95 { NULL, SYSLOG_LEVEL_NOT_SET }
20244695 96};
97
98SyslogFacility
99log_facility_number(char *name)
100{
101 int i;
4394a17f 102
20244695 103 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;
5eaf8578 107 return SYSLOG_FACILITY_NOT_SET;
20244695 108}
109
110LogLevel
111log_level_number(char *name)
112{
113 int i;
4394a17f 114
20244695 115 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;
5eaf8578 119 return SYSLOG_LEVEL_NOT_SET;
20244695 120}
6a17f9c2 121
122/* Error messages that should be logged. */
123
124void
5260325f 125error(const char *fmt,...)
6a17f9c2 126{
5260325f 127 va_list args;
4394a17f 128
5260325f 129 va_start(args, fmt);
130 do_log(SYSLOG_LEVEL_ERROR, fmt, args);
131 va_end(args);
6a17f9c2 132}
133
134/* Log this message (information that usually should go to the log). */
135
136void
bbe88b6d 137logit(const char *fmt,...)
6a17f9c2 138{
5260325f 139 va_list args;
4394a17f 140
5260325f 141 va_start(args, fmt);
59c97189 142 do_log(SYSLOG_LEVEL_INFO, fmt, args);
5260325f 143 va_end(args);
6a17f9c2 144}
145
146/* More detailed messages (information that does not need to go to the log). */
147
148void
5260325f 149verbose(const char *fmt,...)
6a17f9c2 150{
5260325f 151 va_list args;
4394a17f 152
5260325f 153 va_start(args, fmt);
154 do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
155 va_end(args);
6a17f9c2 156}
157
158/* Debugging messages that should not be logged during normal operation. */
159
160void
5260325f 161debug(const char *fmt,...)
6a17f9c2 162{
5260325f 163 va_list args;
4394a17f 164
5260325f 165 va_start(args, fmt);
bcbf86ec 166 do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
167 va_end(args);
168}
169
170void
171debug2(const char *fmt,...)
172{
173 va_list args;
4394a17f 174
bcbf86ec 175 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;
4394a17f 184
bcbf86ec 185 va_start(args, fmt);
186 do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
5260325f 187 va_end(args);
6a17f9c2 188}
189
20244695 190/*
191 * Initialize the log.
192 */
9d6b7add 193
20244695 194void
195log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
9d6b7add 196{
29c82270 197#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
198 struct syslog_data sdata = SYSLOG_DATA_INIT;
199#endif
9d6b7add 200
b1d73a9a 201 argv0 = av0;
202
20244695 203 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:
0be033ea 215 fprintf(stderr, "Unrecognized internal syslog level code %d\n",
20244695 216 (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;
cc3067d6 234#ifdef LOG_AUTHPRIV
235 case SYSLOG_FACILITY_AUTHPRIV:
236 log_facility = LOG_AUTHPRIV;
237 break;
20244695 238#endif
239 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,
0be033ea 265 "Unrecognized internal syslog facility code %d\n",
20244695 266 (int) facility);
267 exit(1);
268 }
29c82270 269
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
9d6b7add 282}
283
20244695 284#define MSGBUFSIZ 1024
285
2bae80e9 286void
20244695 287do_log(LogLevel level, const char *fmt, va_list args)
9d6b7add 288{
a90ed4b3 289#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
c453493f 290 struct syslog_data sdata = SYSLOG_DATA_INIT;
291#endif
20244695 292 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 }
ac87b3c2 340 strnvis(fmtbuf, msgbuf, sizeof(fmtbuf),
341 log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS);
20244695 342 if (log_on_stderr) {
5b0fe364 343 snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf);
344 write(STDERR_FILENO, msgbuf, strlen(msgbuf));
20244695 345 } else {
a90ed4b3 346#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
c453493f 347 openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata);
5b0fe364 348 syslog_r(pri, &sdata, "%.500s", fmtbuf);
c453493f 349 closelog_r(&sdata);
350#else
20244695 351 openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
63cada0a 352 syslog(pri, "%.500s", fmtbuf);
20244695 353 closelog();
c453493f 354#endif
20244695 355 }
9d6b7add 356}
This page took 0.299305 seconds and 5 git commands to generate.