diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 3 | ||||
-rw-r--r-- | src/log_syslog.c | 171 | ||||
-rw-r--r-- | src/log_syslog.h | 55 | ||||
-rw-r--r-- | src/main.c | 6 | ||||
-rw-r--r-- | src/options.c | 7 | ||||
-rw-r--r-- | src/options.h | 2 |
6 files changed, 242 insertions, 2 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index c2fe07c..d309e02 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,3 +3,6 @@ potd_SOURCES = compat.c utils.c options.c log.c log_colored.c log_file.c socket. if HAVE_SECCOMP potd_SOURCES += pseccomp.c endif +if HAVE_SYSLOG +potd_SOURCES += log_syslog.c +endif diff --git a/src/log_syslog.c b/src/log_syslog.c new file mode 100644 index 0000000..3999d9b --- /dev/null +++ b/src/log_syslog.c @@ -0,0 +1,171 @@ +/* + * log_syslog.c + * potd is licensed under the BSD license: + * + * Copyright (c) 2018 Toni Uhlig <matzeton@googlemail.com> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - The names of its contributors may not be used to endorse or promote + * products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#else +#define PACKAGE "unknown" +#endif +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <syslog.h> +#include <assert.h> + +#include "log_syslog.h" + +#define LOG(facility, fmt, arglist) \ + { vsyslog(facility, fmt, arglist); } while(0) +#define LOGEX(facility, src, line, out) \ + { syslog(facility, "%s.%zu: %s", src, line, out); } while(0) +#define LOGEXERR(facility, src, line, out, serrno) \ + { \ + if (serrno) { \ + syslog(facility, "%s.%zu: %s failed: %s", \ + src, line, out, strerror(serrno)); \ + } else { \ + syslog(facility, "%s.%zu: %s failed", \ + src, line, out); \ + } \ + } while(0) + + +int log_open_syslog(void) +{ + openlog(PACKAGE, 0, LOG_DAEMON); + + return 0; +} + +void log_close_syslog(void) +{ + closelog(); +} + +void log_fmt_syslog(log_priority prio, const char *fmt, ...) +{ + va_list arglist; + + if (prio < log_prio) + return; + assert(fmt); + va_start(arglist, fmt); + switch (prio) { + case DEBUG: + LOG(LOG_DEBUG, fmt, arglist); + break; + case NOTICE: + LOG(LOG_NOTICE, fmt, arglist); + break; + case WARNING: + LOG(LOG_WARNING, fmt, arglist); + break; + case ERROR: + LOG(LOG_ERR, fmt, arglist); + break; + case CMD: + LOG(LOG_INFO, fmt, arglist); + break; + case PROTOCOL: + LOG(LOG_INFO, fmt, arglist); + break; + } + va_end(arglist); +} + +void log_fmtex_syslog(log_priority prio, const char *srcfile, + size_t line, const char *fmt, ...) +{ + char out[LOGMSG_MAXLEN+1] = {0}; + va_list arglist; + + if (prio < log_prio) + return; + assert(fmt); + va_start(arglist, fmt); + /* Flawfinder: ignore */ + assert( vsnprintf(&out[0], LOGMSG_MAXLEN, fmt, arglist) >= 0 ); + va_end(arglist); + + switch (prio) { + case DEBUG: + LOGEX(LOG_DEBUG, srcfile, line, out); + break; + case NOTICE: + LOGEX(LOG_NOTICE, srcfile, line, out); + break; + case WARNING: + LOGEX(LOG_WARNING, srcfile, line, out); + break; + case ERROR: + LOGEX(LOG_ERR, srcfile, line, out); + break; + case CMD: + case PROTOCOL: + break; + } +} + +void log_fmtexerr_syslog(log_priority prio, const char *srcfile, + size_t line, const char *fmt, ...) +{ + int saved_errno = errno; + char out[LOGMSG_MAXLEN+1] = {0}; + va_list arglist; + + if (prio < log_prio) + return; + assert(fmt); + va_start(arglist, fmt); + /* Flawfinder: ignore */ + assert( vsnprintf(&out[0], LOGMSG_MAXLEN, fmt, arglist) >= 0 ); + va_end(arglist); + + switch (prio) { + case DEBUG: + LOGEXERR(LOG_DEBUG, srcfile, line, out, saved_errno); + break; + case NOTICE: + LOGEXERR(LOG_NOTICE, srcfile, line, out, saved_errno); + break; + case WARNING: + LOGEXERR(LOG_WARNING, srcfile, line, out, saved_errno); + break; + case ERROR: + LOGEXERR(LOG_WARNING, srcfile, line, out, saved_errno); + break; + case CMD: + case PROTOCOL: + break; + } +} diff --git a/src/log_syslog.h b/src/log_syslog.h new file mode 100644 index 0000000..34924df --- /dev/null +++ b/src/log_syslog.h @@ -0,0 +1,55 @@ +/* + * log_syslog.h + * potd is licensed under the BSD license: + * + * Copyright (c) 2018 Toni Uhlig <matzeton@googlemail.com> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * - The names of its contributors may not be used to endorse or promote + * products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef POTD_LOG_SYSLOG_H +#define POTD_LOG_SYSLOG_H 1 + +#include "log.h" + +#define LOG_SYSLOG_FUNCS log_open_syslog, log_close_syslog, \ + log_fmt_syslog, log_fmtex_syslog, log_fmtexerr_syslog + + +int log_open_syslog(void); + +void log_close_syslog(void); + +void log_fmt_syslog(log_priority prio, const char *fmt, ...); + +void log_fmtex_syslog(log_priority prio, const char *srcfile, + size_t line, const char *fmt, ...); + +void log_fmtexerr_syslog(log_priority prio, const char *srcfile, + size_t line, const char *fmt, ...); + +#endif @@ -47,6 +47,7 @@ #include "log.h" #include "log_colored.h" #include "log_file.h" +#include "log_syslog.h" #include "options.h" #include "utils.h" #include "redirector.h" @@ -338,7 +339,10 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - if (getopt_used(OPT_LOGTOFILE) || getopt_used(OPT_LOGFILE)) { + if (getopt_used(OPT_LOGTOSYSLOG)) { + LOG_SET_FUNCS_VA(LOG_SYSLOG_FUNCS); + fprintf(stderr, "%s\n", "Loging to syslog"); + } else if (getopt_used(OPT_LOGTOFILE) || getopt_used(OPT_LOGFILE)) { log_file = getopt_str(OPT_LOGFILE); LOG_SET_FUNCS_VA(LOG_FILE_FUNCS); fprintf(stderr, "Logfile: '%s'\n", log_file); diff --git a/src/options.c b/src/options.c index 5815bbf..7b17b23 100644 --- a/src/options.c +++ b/src/options.c @@ -35,6 +35,12 @@ #include "config.h" #else #define POTD_LOGFILE "/tmp/potd.log" +#define POTD_DEFROOT "/tmp/potd-root" +#define POTD_RODIR "/tmp/potd-rodir" +#define POTD_ROFILE "/tmp/potd-rofile" +#define POTD_NETNS_RUN_DIR "/tmp/potd-netns" +#define POTD_SSH_RUN_DIR "/tmp/potd-ssh" +#define POTD_DEFUSER "nobody" #endif #include <stdio.h> @@ -88,6 +94,7 @@ struct opt { #define OPT_NOARG(arg, short_help, help) \ OPT(OT_NOARG, .ll = 0, arg, short_help, help) static struct opt options[OPT_MAX+1] = { + OPT_NOARG("log-to-syslog", "log to the syslog interface\n", NULL), OPT_NOARG("log-to-file", "log to the default logfile path\n", NULL), OPT(OT_PATH, .str = POTD_LOGFILE, "log-file", "specify a logfile path\n", NULL), diff --git a/src/options.h b/src/options.h index 96bd025..92c7866 100644 --- a/src/options.h +++ b/src/options.h @@ -37,7 +37,7 @@ struct opt_list; typedef enum opt_name { - OPT_LOGTOFILE = 0, OPT_LOGFILE, OPT_LOGLEVEL, + OPT_LOGTOSYSLOG = 0, OPT_LOGTOFILE, OPT_LOGFILE, OPT_LOGLEVEL, OPT_DAEMON, OPT_REDIRECT, OPT_PROTOCOL, |