1 /* $OpenBSD: log.c,v 1.60 2021/09/16 15:11:19 djm Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
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".
14 * Copyright (c) 2000 Markus Friedl. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
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.
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.
39 #include <sys/types.h>
49 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
56 static LogLevel log_level
= SYSLOG_LEVEL_INFO
;
57 static int log_on_stderr
= 1;
58 static int log_stderr_fd
= STDERR_FILENO
;
59 static int log_facility
= LOG_AUTH
;
60 static const char *argv0
;
61 static log_handler_fn
*log_handler
;
62 static void *log_handler_ctx
;
63 static char **log_verbose
;
64 static size_t nlog_verbose
;
66 extern char *__progname
;
68 #define LOG_SYSLOG_VIS (VIS_CSTYLE|VIS_NL|VIS_TAB|VIS_OCTAL)
69 #define LOG_STDERR_VIS (VIS_SAFE|VIS_OCTAL)
71 /* textual representation of log-facilities/levels */
76 } log_facilities
[] = {
77 { "DAEMON", SYSLOG_FACILITY_DAEMON
},
78 { "USER", SYSLOG_FACILITY_USER
},
79 { "AUTH", SYSLOG_FACILITY_AUTH
},
81 { "AUTHPRIV", SYSLOG_FACILITY_AUTHPRIV
},
83 { "LOCAL0", SYSLOG_FACILITY_LOCAL0
},
84 { "LOCAL1", SYSLOG_FACILITY_LOCAL1
},
85 { "LOCAL2", SYSLOG_FACILITY_LOCAL2
},
86 { "LOCAL3", SYSLOG_FACILITY_LOCAL3
},
87 { "LOCAL4", SYSLOG_FACILITY_LOCAL4
},
88 { "LOCAL5", SYSLOG_FACILITY_LOCAL5
},
89 { "LOCAL6", SYSLOG_FACILITY_LOCAL6
},
90 { "LOCAL7", SYSLOG_FACILITY_LOCAL7
},
91 { NULL
, SYSLOG_FACILITY_NOT_SET
}
99 { "QUIET", SYSLOG_LEVEL_QUIET
},
100 { "FATAL", SYSLOG_LEVEL_FATAL
},
101 { "ERROR", SYSLOG_LEVEL_ERROR
},
102 { "INFO", SYSLOG_LEVEL_INFO
},
103 { "VERBOSE", SYSLOG_LEVEL_VERBOSE
},
104 { "DEBUG", SYSLOG_LEVEL_DEBUG1
},
105 { "DEBUG1", SYSLOG_LEVEL_DEBUG1
},
106 { "DEBUG2", SYSLOG_LEVEL_DEBUG2
},
107 { "DEBUG3", SYSLOG_LEVEL_DEBUG3
},
108 { NULL
, SYSLOG_LEVEL_NOT_SET
}
118 log_facility_number(char *name
)
123 for (i
= 0; log_facilities
[i
].name
; i
++)
124 if (strcasecmp(log_facilities
[i
].name
, name
) == 0)
125 return log_facilities
[i
].val
;
126 return SYSLOG_FACILITY_NOT_SET
;
130 log_facility_name(SyslogFacility facility
)
134 for (i
= 0; log_facilities
[i
].name
; i
++)
135 if (log_facilities
[i
].val
== facility
)
136 return log_facilities
[i
].name
;
141 log_level_number(char *name
)
146 for (i
= 0; log_levels
[i
].name
; i
++)
147 if (strcasecmp(log_levels
[i
].name
, name
) == 0)
148 return log_levels
[i
].val
;
149 return SYSLOG_LEVEL_NOT_SET
;
153 log_level_name(LogLevel level
)
157 for (i
= 0; log_levels
[i
].name
!= NULL
; i
++)
158 if (log_levels
[i
].val
== level
)
159 return log_levels
[i
].name
;
164 log_verbose_add(const char *s
)
168 /* Ignore failures here */
169 if ((tmp
= recallocarray(log_verbose
, nlog_verbose
, nlog_verbose
+ 1,
170 sizeof(*log_verbose
))) != NULL
) {
172 if ((log_verbose
[nlog_verbose
] = strdup(s
)) != NULL
)
178 log_verbose_reset(void)
182 for (i
= 0; i
< nlog_verbose
; i
++)
183 free(log_verbose
[i
]);
190 * Initialize the log.
194 log_init(const char *av0
, LogLevel level
, SyslogFacility facility
,
197 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
198 struct syslog_data sdata
= SYSLOG_DATA_INIT
;
203 if (log_change_level(level
) != 0) {
204 fprintf(stderr
, "Unrecognized internal syslog level code %d\n",
210 log_handler_ctx
= NULL
;
212 log_on_stderr
= on_stderr
;
217 case SYSLOG_FACILITY_DAEMON
:
218 log_facility
= LOG_DAEMON
;
220 case SYSLOG_FACILITY_USER
:
221 log_facility
= LOG_USER
;
223 case SYSLOG_FACILITY_AUTH
:
224 log_facility
= LOG_AUTH
;
227 case SYSLOG_FACILITY_AUTHPRIV
:
228 log_facility
= LOG_AUTHPRIV
;
231 case SYSLOG_FACILITY_LOCAL0
:
232 log_facility
= LOG_LOCAL0
;
234 case SYSLOG_FACILITY_LOCAL1
:
235 log_facility
= LOG_LOCAL1
;
237 case SYSLOG_FACILITY_LOCAL2
:
238 log_facility
= LOG_LOCAL2
;
240 case SYSLOG_FACILITY_LOCAL3
:
241 log_facility
= LOG_LOCAL3
;
243 case SYSLOG_FACILITY_LOCAL4
:
244 log_facility
= LOG_LOCAL4
;
246 case SYSLOG_FACILITY_LOCAL5
:
247 log_facility
= LOG_LOCAL5
;
249 case SYSLOG_FACILITY_LOCAL6
:
250 log_facility
= LOG_LOCAL6
;
252 case SYSLOG_FACILITY_LOCAL7
:
253 log_facility
= LOG_LOCAL7
;
257 "Unrecognized internal syslog facility code %d\n",
263 * If an external library (eg libwrap) attempts to use syslog
264 * immediately after reexec, syslog may be pointing to the wrong
265 * facility, so we force an open/close of syslog here.
267 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
268 openlog_r(argv0
? argv0
: __progname
, LOG_PID
, log_facility
, &sdata
);
271 openlog(argv0
? argv0
: __progname
, LOG_PID
, log_facility
);
277 log_change_level(LogLevel new_log_level
)
279 /* no-op if log_init has not been called */
283 switch (new_log_level
) {
284 case SYSLOG_LEVEL_QUIET
:
285 case SYSLOG_LEVEL_FATAL
:
286 case SYSLOG_LEVEL_ERROR
:
287 case SYSLOG_LEVEL_INFO
:
288 case SYSLOG_LEVEL_VERBOSE
:
289 case SYSLOG_LEVEL_DEBUG1
:
290 case SYSLOG_LEVEL_DEBUG2
:
291 case SYSLOG_LEVEL_DEBUG3
:
292 log_level
= new_log_level
;
300 log_is_on_stderr(void)
302 return log_on_stderr
&& log_stderr_fd
== STDERR_FILENO
;
305 /* redirect what would usually get written to stderr to specified file */
307 log_redirect_stderr_to(const char *logfile
)
311 if (logfile
== NULL
) {
312 if (log_stderr_fd
!= STDERR_FILENO
) {
313 close(log_stderr_fd
);
314 log_stderr_fd
= STDERR_FILENO
;
319 if ((fd
= open(logfile
, O_WRONLY
|O_CREAT
|O_APPEND
, 0600)) == -1) {
320 fprintf(stderr
, "Couldn't open logfile %s: %s\n", logfile
,
327 #define MSGBUFSIZ 1024
330 set_log_handler(log_handler_fn
*handler
, void *ctx
)
332 log_handler
= handler
;
333 log_handler_ctx
= ctx
;
337 do_log(LogLevel level
, int force
, const char *suffix
, const char *fmt
,
340 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
341 struct syslog_data sdata
= SYSLOG_DATA_INIT
;
343 char msgbuf
[MSGBUFSIZ
];
344 char fmtbuf
[MSGBUFSIZ
];
347 int saved_errno
= errno
;
348 log_handler_fn
*tmp_handler
;
349 const char *progname
= argv0
!= NULL
? argv0
: __progname
;
351 if (!force
&& level
> log_level
)
355 case SYSLOG_LEVEL_FATAL
:
360 case SYSLOG_LEVEL_ERROR
:
365 case SYSLOG_LEVEL_INFO
:
368 case SYSLOG_LEVEL_VERBOSE
:
371 case SYSLOG_LEVEL_DEBUG1
:
375 case SYSLOG_LEVEL_DEBUG2
:
379 case SYSLOG_LEVEL_DEBUG3
:
384 txt
= "internal error";
388 if (txt
!= NULL
&& log_handler
== NULL
) {
389 snprintf(fmtbuf
, sizeof(fmtbuf
), "%s: %s", txt
, fmt
);
390 vsnprintf(msgbuf
, sizeof(msgbuf
), fmtbuf
, args
);
392 vsnprintf(msgbuf
, sizeof(msgbuf
), fmt
, args
);
394 if (suffix
!= NULL
) {
395 snprintf(fmtbuf
, sizeof(fmtbuf
), "%s: %s", msgbuf
, suffix
);
396 strlcpy(msgbuf
, fmtbuf
, sizeof(msgbuf
));
398 strnvis(fmtbuf
, msgbuf
, sizeof(fmtbuf
),
399 log_on_stderr
? LOG_STDERR_VIS
: LOG_SYSLOG_VIS
);
400 if (log_handler
!= NULL
) {
401 /* Avoid recursion */
402 tmp_handler
= log_handler
;
404 tmp_handler(level
, force
, fmtbuf
, log_handler_ctx
);
405 log_handler
= tmp_handler
;
406 } else if (log_on_stderr
) {
407 snprintf(msgbuf
, sizeof msgbuf
, "%s%s%.*s\r\n",
408 (log_on_stderr
> 1) ? progname
: "",
409 (log_on_stderr
> 1) ? ": " : "",
410 (int)sizeof msgbuf
- 3, fmtbuf
);
411 (void)write(log_stderr_fd
, msgbuf
, strlen(msgbuf
));
413 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
414 openlog_r(progname
, LOG_PID
, log_facility
, &sdata
);
415 syslog_r(pri
, &sdata
, "%.500s", fmtbuf
);
418 openlog(progname
, LOG_PID
, log_facility
);
419 syslog(pri
, "%.500s", fmtbuf
);
427 sshlog(const char *file
, const char *func
, int line
, int showfunc
,
428 LogLevel level
, const char *suffix
, const char *fmt
, ...)
433 sshlogv(file
, func
, line
, showfunc
, level
, suffix
, fmt
, args
);
438 sshlogdie(const char *file
, const char *func
, int line
, int showfunc
,
439 LogLevel level
, const char *suffix
, const char *fmt
, ...)
444 sshlogv(file
, func
, line
, showfunc
, SYSLOG_LEVEL_INFO
,
451 sshsigdie(const char *file
, const char *func
, int line
, int showfunc
,
452 LogLevel level
, const char *suffix
, const char *fmt
, ...)
457 sshlogv(file
, func
, line
, showfunc
, SYSLOG_LEVEL_FATAL
,
464 sshlogv(const char *file
, const char *func
, int line
, int showfunc
,
465 LogLevel level
, const char *suffix
, const char *fmt
, va_list args
)
467 char tag
[128], fmt2
[MSGBUFSIZ
+ 128];
472 snprintf(tag
, sizeof(tag
), "%.48s:%.48s():%d (pid=%ld)",
473 (cp
= strrchr(file
, '/')) == NULL
? file
: cp
+ 1, func
, line
,
475 for (i
= 0; i
< nlog_verbose
; i
++) {
476 if (match_pattern_list(tag
, log_verbose
[i
], 0) == 1) {
483 snprintf(fmt2
, sizeof(fmt2
), "%s: %s", tag
, fmt
);
485 snprintf(fmt2
, sizeof(fmt2
), "%s: %s", func
, fmt
);
487 strlcpy(fmt2
, fmt
, sizeof(fmt2
));
489 do_log(level
, forced
, suffix
, fmt2
, args
);
493 sshlogdirect(LogLevel level
, int forced
, const char *fmt
, ...)
498 do_log(level
, forced
, NULL
, fmt
, args
);