upstream: variants of the log methods that append a ssherr.h string

from a supplied error code; ok markus@

OpenBSD-Commit-ID: aed98c4435d48d036ae6740300f6a8357b7cc0bf
This commit is contained in:
djm@openbsd.org 2020-10-18 11:21:59 +00:00 committed by Damien Miller
parent 28cb0a4b03
commit 9e2c4f6422
5 changed files with 80 additions and 39 deletions

View File

@ -1,4 +1,4 @@
/* $OpenBSD: fatal.c,v 1.9 2020/10/17 01:28:20 djm Exp $ */ /* $OpenBSD: fatal.c,v 1.10 2020/10/18 11:21:59 djm Exp $ */
/* /*
* Copyright (c) 2002 Markus Friedl. All rights reserved. * Copyright (c) 2002 Markus Friedl. All rights reserved.
* *
@ -35,12 +35,12 @@
void void
sshfatal(const char *file, const char *func, int line, int showfunc, sshfatal(const char *file, const char *func, int line, int showfunc,
LogLevel level, const char *fmt, ...) LogLevel level, const char *suffix, const char *fmt, ...)
{ {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
ssh_log(file, func, line, showfunc, level, fmt, args); sshlog(file, func, line, showfunc, level, suffix, fmt, args);
va_end(args); va_end(args);
cleanup_exit(255); cleanup_exit(255);
} }

26
log.c
View File

@ -1,4 +1,4 @@
/* $OpenBSD: log.c,v 1.54 2020/10/17 01:28:20 djm Exp $ */ /* $OpenBSD: log.c,v 1.55 2020/10/18 11:21:59 djm Exp $ */
/* /*
* Author: Tatu Ylonen <ylo@cs.hut.fi> * Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@ -334,7 +334,7 @@ set_log_handler(log_handler_fn *handler, void *ctx)
static void static void
do_log(const char *file, const char *func, int line, LogLevel level, do_log(const char *file, const char *func, int line, LogLevel level,
int force, const char *fmt, va_list args) int force, const char *suffix, const char *fmt, va_list args)
{ {
#if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT) #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
struct syslog_data sdata = SYSLOG_DATA_INIT; struct syslog_data sdata = SYSLOG_DATA_INIT;
@ -389,6 +389,10 @@ do_log(const char *file, const char *func, int line, LogLevel level,
} else { } else {
vsnprintf(msgbuf, sizeof(msgbuf), fmt, args); vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
} }
if (suffix != NULL) {
snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", msgbuf, suffix);
strlcpy(msgbuf, fmtbuf, sizeof(msgbuf));
}
strnvis(fmtbuf, msgbuf, sizeof(fmtbuf), strnvis(fmtbuf, msgbuf, sizeof(fmtbuf),
log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS); log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS);
if (log_handler != NULL) { if (log_handler != NULL) {
@ -417,42 +421,44 @@ do_log(const char *file, const char *func, int line, LogLevel level,
void void
sshlog(const char *file, const char *func, int line, int showfunc, sshlog(const char *file, const char *func, int line, int showfunc,
LogLevel level, const char *fmt, ...) LogLevel level, const char *suffix, const char *fmt, ...)
{ {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
sshlogv(file, func, line, showfunc, level, fmt, args); sshlogv(file, func, line, showfunc, level, suffix, fmt, args);
va_end(args); va_end(args);
} }
void void
sshlogdie(const char *file, const char *func, int line, int showfunc, sshlogdie(const char *file, const char *func, int line, int showfunc,
LogLevel level, const char *fmt, ...) LogLevel level, const char *suffix, const char *fmt, ...)
{ {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
sshlogv(file, func, line, showfunc, SYSLOG_LEVEL_INFO, fmt, args); sshlogv(file, func, line, showfunc, SYSLOG_LEVEL_INFO,
suffix, fmt, args);
va_end(args); va_end(args);
cleanup_exit(255); cleanup_exit(255);
} }
void void
sshsigdie(const char *file, const char *func, int line, int showfunc, sshsigdie(const char *file, const char *func, int line, int showfunc,
LogLevel level, const char *fmt, ...) LogLevel level, const char *suffix, const char *fmt, ...)
{ {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
sshlogv(file, func, line, showfunc, SYSLOG_LEVEL_FATAL, fmt, args); sshlogv(file, func, line, showfunc, SYSLOG_LEVEL_FATAL,
suffix, fmt, args);
va_end(args); va_end(args);
_exit(1); _exit(1);
} }
void void
sshlogv(const char *file, const char *func, int line, int showfunc, sshlogv(const char *file, const char *func, int line, int showfunc,
LogLevel level, const char *fmt, va_list args) LogLevel level, const char *suffix, const char *fmt, va_list args)
{ {
char tag[128], fmt2[MSGBUFSIZ + 128]; char tag[128], fmt2[MSGBUFSIZ + 128];
int forced = 0; int forced = 0;
@ -475,5 +481,5 @@ sshlogv(const char *file, const char *func, int line, int showfunc,
else else
strlcpy(fmt2, fmt, sizeof(fmt2)); strlcpy(fmt2, fmt, sizeof(fmt2));
do_log(file, func, line, level, forced, fmt2, args); do_log(file, func, line, level, forced, suffix, fmt2, args);
} }

74
log.h
View File

@ -1,4 +1,4 @@
/* $OpenBSD: log.h,v 1.28 2020/10/18 11:14:27 djm Exp $ */ /* $OpenBSD: log.h,v 1.29 2020/10/18 11:21:59 djm Exp $ */
/* /*
* Author: Tatu Ylonen <ylo@cs.hut.fi> * Author: Tatu Ylonen <ylo@cs.hut.fi>
@ -16,6 +16,7 @@
#define SSH_LOG_H #define SSH_LOG_H
#include <stdarg.h> /* va_list */ #include <stdarg.h> /* va_list */
#include <ssherr.h> /* ssh_err() */
/* Supported syslog facilities and levels. */ /* Supported syslog facilities and levels. */
typedef enum { typedef enum {
@ -68,28 +69,63 @@ void set_log_handler(log_handler_fn *, void *);
void cleanup_exit(int) __attribute__((noreturn)); void cleanup_exit(int) __attribute__((noreturn));
void sshlog(const char *, const char *, int, int, void sshlog(const char *, const char *, int, int,
LogLevel, const char *, ...) __attribute__((format(printf, 6, 7))); LogLevel, const char *, const char *, ...)
__attribute__((format(printf, 7, 8)));
void sshlogv(const char *, const char *, int, int, void sshlogv(const char *, const char *, int, int,
LogLevel, const char *, va_list); LogLevel, const char *, const char *, va_list);
void sshsigdie(const char *, const char *, int, int, void sshsigdie(const char *, const char *, int, int,
LogLevel, const char *, ...) __attribute__((noreturn)) LogLevel, const char *, const char *, ...) __attribute__((noreturn))
__attribute__((format(printf, 6, 7))); __attribute__((format(printf, 7, 8)));
void sshlogdie(const char *, const char *, int, int, void sshlogdie(const char *, const char *, int, int,
LogLevel, const char *, ...) __attribute__((noreturn)) LogLevel, const char *, const char *, ...) __attribute__((noreturn))
__attribute__((format(printf, 6, 7))); __attribute__((format(printf, 7, 8)));
void sshfatal(const char *, const char *, int, int, void sshfatal(const char *, const char *, int, int,
LogLevel, const char *, ...) __attribute__((noreturn)) LogLevel, const char *, const char *, ...) __attribute__((noreturn))
__attribute__((format(printf, 6, 7))); __attribute__((format(printf, 7, 8)));
#define do_log2(level, ...) sshlog(__FILE__, __func__, __LINE__, 0, level, __VA_ARGS__) #define do_log2(level, ...) sshlog(__FILE__, __func__, __LINE__, 0, level, NULL, __VA_ARGS__)
#define debug3(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG3, __VA_ARGS__) #define debug3(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG3, NULL, __VA_ARGS__)
#define debug2(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG2, __VA_ARGS__) #define debug2(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG2, NULL, __VA_ARGS__)
#define debug(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG1, __VA_ARGS__) #define debug(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG1, NULL, __VA_ARGS__)
#define verbose(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_VERBOSE, __VA_ARGS__) #define verbose(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_VERBOSE, NULL, __VA_ARGS__)
#define logit(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_INFO, __VA_ARGS__) #define logit(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_INFO, NULL, __VA_ARGS__)
#define error(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, __VA_ARGS__) #define error(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
#define fatal(...) sshfatal(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_FATAL, __VA_ARGS__) #define fatal(...) sshfatal(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_FATAL, NULL, __VA_ARGS__)
#define logdie(...) sshlogdie(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, __VA_ARGS__) #define logdie(...) sshlogdie(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
#define sigdie(...) sshsigdie(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, __VA_ARGS__) #define sigdie(...) sshsigdie(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
/* Variants that prepend the caller's function */
#define do_log2_f(level, ...) sshlog(__FILE__, __func__, __LINE__, 1, level, NULL, __VA_ARGS__)
#define debug3_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG3, NULL, __VA_ARGS__)
#define debug2_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG2, NULL, __VA_ARGS__)
#define debug_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG1, NULL, __VA_ARGS__)
#define verbose_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_VERBOSE, NULL, __VA_ARGS__)
#define logit_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_INFO, NULL, __VA_ARGS__)
#define error_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
#define fatal_f(...) sshfatal(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_FATAL, NULL, __VA_ARGS__)
#define logdie_f(...) sshlogdie(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
#define sigdie_f(...) sshsigdie(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
/* Variants that appends a ssh_err message */
#define do_log2_r(r, level, ...) sshlog(__FILE__, __func__, __LINE__, 0, level, ssh_err(r), __VA_ARGS__)
#define debug3_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG3, ssh_err(r), __VA_ARGS__)
#define debug2_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG2, ssh_err(r), __VA_ARGS__)
#define debug_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG1, ssh_err(r), __VA_ARGS__)
#define verbose_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_VERBOSE, ssh_err(r), __VA_ARGS__)
#define logit_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_INFO, ssh_err(r), __VA_ARGS__)
#define error_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
#define fatal_r(r, ...) sshfatal(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_FATAL, ssh_err(r), __VA_ARGS__)
#define logdie_r(r, ...) sshlogdie(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
#define sigdie_r(r, ...) sshsigdie(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
#define do_log2_fr(r, level, ...) sshlog(__FILE__, __func__, __LINE__, 1, level, ssh_err(r), __VA_ARGS__)
#define debug3_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG3, ssh_err(r), __VA_ARGS__)
#define debug2_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG2, ssh_err(r), __VA_ARGS__)
#define debug_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG1, ssh_err(r), __VA_ARGS__)
#define verbose_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_VERBOSE, ssh_err(r), __VA_ARGS__)
#define logit_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_INFO, ssh_err(r), __VA_ARGS__)
#define error_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
#define fatal_fr(r, ...) sshfatal(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_FATAL, ssh_err(r), __VA_ARGS__)
#define logdie_fr(r, ...) sshlogdie(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
#define sigdie_fr(r, ...) sshsigdie(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
#endif #endif

View File

@ -1,4 +1,4 @@
/* $OpenBSD: monitor.c,v 1.215 2020/10/16 13:24:45 djm Exp $ */ /* $OpenBSD: monitor.c,v 1.216 2020/10/18 11:21:59 djm Exp $ */
/* /*
* Copyright 2002 Niels Provos <provos@citi.umich.edu> * Copyright 2002 Niels Provos <provos@citi.umich.edu>
* Copyright 2002 Markus Friedl <markus@openbsd.org> * Copyright 2002 Markus Friedl <markus@openbsd.org>
@ -461,9 +461,8 @@ monitor_read_log(struct monitor *pmonitor)
/* Log it */ /* Log it */
if (log_level_name(level) == NULL) if (log_level_name(level) == NULL)
fatal("%s: invalid log level %u (corrupted message?)", fatal_f("invalid log level %u (corrupted message?)", level);
__func__, level); sshlog(file, func, line, 0, level, NULL, "%s [preauth]", msg);
sshlog(file, func, line, 0, level, "%s [preauth]", msg);
sshbuf_free(logmsg); sshbuf_free(logmsg);
free(file); free(file);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: ssh-keyscan.c,v 1.134 2020/10/17 01:28:20 djm Exp $ */ /* $OpenBSD: ssh-keyscan.c,v 1.135 2020/10/18 11:21:59 djm Exp $ */
/* /*
* Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>. * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
* *
@ -636,12 +636,12 @@ do_host(char *host)
void void
sshfatal(const char *file, const char *func, int line, int showfunc, sshfatal(const char *file, const char *func, int line, int showfunc,
LogLevel level, const char *fmt, ...) LogLevel level, const char *suffix, const char *fmt, ...)
{ {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
ssh_log(file, func, line, showfunc, level, fmt, args); sshlog(file, func, line, showfunc, level, suffix, fmt, args);
va_end(args); va_end(args);
cleanup_exit(255); cleanup_exit(255);
} }