diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2018-05-30 15:43:08 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2018-05-30 15:43:08 +0200 |
commit | 1c9022f075387e86269a79ac71f61dd4ac32c421 (patch) | |
tree | 535773b5db20511503bde730cf78339e18b894fd | |
parent | 0dc05d5eaf474c30bcb71f77a2c34f521452c0b5 (diff) |
POTD skeleton #83.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/log_colored.h | 4 | ||||
-rw-r--r-- | src/log_file.c | 171 | ||||
-rw-r--r-- | src/log_file.h | 24 | ||||
-rw-r--r-- | src/main.c | 8 | ||||
-rw-r--r-- | src/utils.c | 3 |
6 files changed, 208 insertions, 4 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 22c879f..7718218 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,4 +2,4 @@ AM_CFLAGS = -pedantic -Wall -std=gnu99 -fstrict-aliasing -D_GNU_SOURCE=1 $(libss AM_LDFLAGS = $(libssh_LIBS) $(libseccomp_LIBS) $(valgrind_LIBS) sbin_PROGRAMS = potd -potd_SOURCES = utils.c log.c log_colored.c socket.c pevent.c capabilities.c pseccomp.c jail.c forward.c redirector.c protocol.c protocol_ssh.c main.c +potd_SOURCES = utils.c log.c log_colored.c log_file.c socket.c pevent.c capabilities.c pseccomp.c jail.c forward.c redirector.c protocol.c protocol_ssh.c main.c diff --git a/src/log_colored.h b/src/log_colored.h index b06a2f5..b372c9b 100644 --- a/src/log_colored.h +++ b/src/log_colored.h @@ -1,5 +1,5 @@ -#ifndef POTD_LOG_COLORED -#define POTD_LOG_COLORED 1 +#ifndef POTD_LOG_COLORED_H +#define POTD_LOG_COLORED_H 1 #include "log.h" diff --git a/src/log_file.c b/src/log_file.c new file mode 100644 index 0000000..f0a78f2 --- /dev/null +++ b/src/log_file.c @@ -0,0 +1,171 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <stdarg.h> +#include <fcntl.h> +#include <assert.h> + +#include "log_file.h" + +char *log_file = NULL; +static FILE *flog = NULL; + + +int log_open_file(void) +{ + if (!log_file) { + fprintf(stderr, "%s\n", "The path to the logfile was not set."); + return 1; + } + + flog = fopen(log_file, "a+"); + if (!flog) { + fprintf(stderr, "Could not open '%s' for writing: %s\n", + log_file, strerror(errno)); + return 1; + } + + if (setvbuf(flog, NULL, _IOLBF, BUFSIZ)) { + log_close_file(); + return 1; + } + + return 0; +} + +void log_close_file(void) +{ + fclose(flog); + flog = NULL; +} + +void log_fmt_file(log_priority prio, const char *fmt, ...) +{ + pid_t my_pid; + char out[LOGMSG_MAXLEN+1] = {0}; + va_list arglist; + + if (prio < log_prio) + return; + assert(fmt); + va_start(arglist, fmt); + assert( vsnprintf(&out[0], LOGMSG_MAXLEN, fmt, arglist) >= 0 ); + va_end(arglist); + + my_pid = getpid(); + switch (prio) { + case DEBUG: + fprintf(flog, "[DEBUG] [%d] %s\n", my_pid, out); + break; + case NOTICE: + fprintf(flog, "[NOTICE] [%d] %s\n", my_pid, out); + break; + case WARNING: + fprintf(flog, "[WARNING][%d] %s\n", my_pid, out); + break; + case ERROR: + fprintf(flog, "[ERROR] [%d] %s\n", my_pid, out); + break; + case CMD: + fprintf(flog, "[CMD] [%d] %s\n", my_pid, out); + break; + } +} + +void log_fmtex_file(log_priority prio, const char *srcfile, + size_t line, const char *fmt, ...) +{ + pid_t my_pid; + char out[LOGMSG_MAXLEN+1] = {0}; + va_list arglist; + + if (prio < log_prio) + return; + assert(fmt); + va_start(arglist, fmt); + assert( vsnprintf(&out[0], LOGMSG_MAXLEN, fmt, arglist) >= 0 ); + va_end(arglist); + + my_pid = getpid(); + switch (prio) { + case DEBUG: + fprintf(flog, "[DEBUG] [%d] %s.%lu: %s\n", my_pid, srcfile, + line, out); + break; + case NOTICE: + fprintf(flog, "[NOTICE] [%d] %s.%lu: %s\n", + my_pid, srcfile, line, out); + break; + case WARNING: + fprintf(flog, "[WARNING][%d] %s.%lu: %s\n", + my_pid, srcfile, line, out); + break; + case ERROR: + fprintf(flog, "[ERROR] [%d] %s.%lu: %s\n", + my_pid, srcfile, line, out); + break; + case CMD: + break; + } +} + +void log_fmtexerr_file(log_priority prio, const char *srcfile, + size_t line, const char *fmt, ...) +{ + pid_t my_pid; + int saved_errno = errno; + char out[LOGMSG_MAXLEN+1] = {0}; + va_list arglist; + + if (prio < log_prio) + return; + assert(fmt); + va_start(arglist, fmt); + assert( vsnprintf(&out[0], LOGMSG_MAXLEN, fmt, arglist) >= 0 ); + va_end(arglist); + + my_pid = getpid(); + switch (prio) { + case DEBUG: + if (saved_errno) + fprintf(flog, "[DEBUG] [%d] %s.%lu: %s failed: %s\n", + my_pid, srcfile, line, out, + strerror(saved_errno)); + else + fprintf(flog, "[DEBUG] [%d] %s.%lu: %s failed\n", + my_pid, srcfile, line, out); + break; + case NOTICE: + if (saved_errno) + fprintf(flog, "[NOTICE] [%d] %s.%lu: %s failed: %s\n", + my_pid, srcfile, + line, out, strerror(saved_errno)); + else + fprintf(flog, "[NOTICE] [%d] %s.%lu: %s failed\n", + my_pid, srcfile, + line, out); + break; + case WARNING: + if (saved_errno) + fprintf(flog, "[WARNING][%d] %s.%lu: %s failed: %s\n", + my_pid, srcfile, + line, out, strerror(saved_errno)); + else + fprintf(flog, "[WARNING][%d] %s.%lu: %s failed\n", + my_pid, srcfile, + line, out); + break; + case ERROR: + if (saved_errno) + fprintf(flog, "[ERROR] [%d] %s.%lu: %s failed: %s\n", + my_pid, srcfile, + line, out, strerror(saved_errno)); + else + fprintf(flog, "[ERROR] [%d] %s.%lu: %s failed\n", + my_pid, srcfile, + line, out); + break; + case CMD: + break; + } +} diff --git a/src/log_file.h b/src/log_file.h new file mode 100644 index 0000000..e119f5d --- /dev/null +++ b/src/log_file.h @@ -0,0 +1,24 @@ +#ifndef POTD_LOG_FILE_H +#define POTD_LOG_FILE_H 1 + +#include "log.h" + +#define LOG_FILE_FUNCS log_open_file, log_close_file, \ + log_fmt_file, log_fmtex_file, log_fmtexerr_file + +extern char *log_file; + + +int log_open_file(void); + +void log_close_file(void); + +void log_fmt_file(log_priority prio, const char *fmt, ...); + +void log_fmtex_file(log_priority prio, const char *srcfile, + size_t line, const char *fmt, ...); + +void log_fmtexerr_file(log_priority prio, const char *srcfile, + size_t line, const char *fmt, ...); + +#endif @@ -11,6 +11,7 @@ #include "capabilities.h" #include "log.h" #include "log_colored.h" +#include "log_file.h" #include "utils.h" #include "redirector.h" #include "protocol_ssh.h" @@ -128,8 +129,12 @@ int main(int argc, char *argv[]) (void) argv; arg0 = argv[0]; - LOG_SET_FUNCS_VA(LOG_COLORED_FUNCS); + //LOG_SET_FUNCS_VA(LOG_COLORED_FUNCS); + log_file = strdup("./potd.log"); + LOG_SET_FUNCS_VA(LOG_FILE_FUNCS); //log_prio = DEBUG; + if (log_open()) + exit(EXIT_FAILURE); #ifdef HAVE_CONFIG_H N("%s (C) 2018 Toni Uhlig (%s)", PACKAGE_STRING, PACKAGE_BUGREPORT); #endif @@ -189,5 +194,6 @@ int main(int argc, char *argv[]) } else W2("Process with pid %d terminated", child_pid); } + log_close(); return 0; } diff --git a/src/utils.c b/src/utils.c index 96ea0cd..b070be2 100644 --- a/src/utils.c +++ b/src/utils.c @@ -173,6 +173,9 @@ pid_t daemonize(int stay_foreground) assert( close_fds_except(0, 1, 2, -1) == 0 ); } + if (log_open()) + return -1; + return pid; } |