From 340fe83d834865542a30d3507bc59a480e1054c5 Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Wed, 5 Jun 2013 21:12:01 +0200 Subject: common: Prepare for OpenVPN 2.3's new logging feature in plug-in API v3 In OpenVPN v2.3 there's a new plug-in API with a more integrated log features. This patch prepares the logging infrastructure for this API. Signed-off-by: David Sommerseth --- common/eurephia_log.c | 36 +++++++++++++++++++++++++++++++++--- common/eurephia_log.h | 5 +++-- common/eurephia_log_struct.h | 8 ++++++-- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/common/eurephia_log.c b/common/eurephia_log.c index 8063321..a25964d 100644 --- a/common/eurephia_log.c +++ b/common/eurephia_log.c @@ -1,6 +1,6 @@ /* eurephia_log.c -- eurephia logging * - * GPLv2 only - Copyright (C) 2008 - 2012 + * GPLv2 only - Copyright (C) 2008 - 2013 * David Sommerseth * * This program is free software; you can redistribute it and/or @@ -85,6 +85,20 @@ static const int syslog_priority[] = { }; +/** + * Mapping table for eurephia log types to OpenVPN's plug-in v3 log API + */ +static const int openvpnlog_flags[] = { + -1, + PLOG_NOTE, /**< LOG_INFO */ + PLOG_DEBUG, /**< LOG_DEBUG */ + PLOG_WARN, /**< LOG_WARNING */ + PLOG_ERR, /**< LOG_ERROR */ + PLOG_ERR | PLOG_NOMUTE, /**< LOG_CRITICAL */ + PLOG_ERR | PLOG_NOMUTE, /**< LOG_FATAL */ + PLOG_ERR | PLOG_NOMUTE /**< LOG_PANIC */ +}; + /** * Converts eurephiaLOGTYPE value to a string * @@ -98,6 +112,8 @@ static inline const char *logtype_str(eurephiaLOGTYPE lt) { return "file\0"; case logSYSLOG: return "syslog\0"; + case logOPENVPN: + return "openvpn::plugin_vlog\0"; } return NULL; } @@ -229,6 +245,9 @@ void _veurephia_log_func(eurephiaCTX *ctx, int logdst, int loglvl, const char *f case logSYSLOG: vsyslog(syslog_priority[logdst], fmt, ap); break; + case logOPENVPN: + ctx->log->ovpn_log(openvpnlog_flags[logdst], "eurephia", fmt, ap); + break; } } } @@ -260,6 +279,9 @@ void eurephia_log_close(eurephiaCTX *ctx) { case logSYSLOG: closelog(); break; + case logOPENVPN: + // The OpenVPN plug-in v3 log API does not need to close the log + break; } ctx->log->opened = 0; } @@ -281,7 +303,8 @@ void eurephia_log_close(eurephiaCTX *ctx) { * * @return Returns 1 on success, otherwise 0; */ -int eurephia_log_init(eurephiaCTX *ctx, const char *ident, const char *dest, int loglevel) { +int eurephia_log_init(eurephiaCTX *ctx, const char *ident, const char *dest, + int loglevel, plugin_vlog_t plglog_fnc) { assert( (ctx != NULL) && (dest != NULL) ); @@ -294,11 +317,14 @@ int eurephia_log_init(eurephiaCTX *ctx, const char *ident, const char *dest, int if( strncmp(dest, "syslog:", 7) == 0 ) { ctx->log->logtype = logSYSLOG; ctx->log->destination = strdup(dest+7); + } else if( strcmp(dest, "openvpn:") == 0 ) { + ctx->log->logtype = logOPENVPN; + ctx->log->ovpn_log = plglog_fnc; } else { ctx->log->logtype = logFILE; ctx->log->destination = strdup(dest); } - if( ctx->log->destination == NULL) { + if( (ctx->log->destination == NULL) && (ctx->log->logtype != logOPENVPN) ) { free_nullsafe(ctx, ctx->log); return 0; } @@ -326,6 +352,10 @@ int eurephia_log_init(eurephiaCTX *ctx, const char *ident, const char *dest, int case logSYSLOG: // Open syslog openlog(ident, LOG_PID, syslog_logdest(ctx->log->destination)); break; + + case logOPENVPN: + // Logging via OpenVPN's log function do not require any init + break; } ctx->log->opened = 1; eurephia_log(ctx, LOG_INFO, 1, "Logging to %s (%s) started", diff --git a/common/eurephia_log.h b/common/eurephia_log.h index c29aa05..be63787 100644 --- a/common/eurephia_log.h +++ b/common/eurephia_log.h @@ -1,6 +1,6 @@ /* eurephia_log.h -- eurephia logging module * - * GPLv2 only - Copyright (C) 2008 - 2012 + * GPLv2 only - Copyright (C) 2008 - 2013 * David Sommerseth * * This program is free software; you can redistribute it and/or @@ -32,6 +32,7 @@ #define EUREPHIA_LOG_H_ #include +#include "openvpn-plugin.h" #include #include @@ -63,7 +64,7 @@ #warning ########################################################################################## #endif -int eurephia_log_init(eurephiaCTX *ctx, const char *ident, const char *dest, int loglvl); +int eurephia_log_init(eurephiaCTX *ctx, const char *ident, const char *dest, int loglvl, plugin_vlog_t plglog_fnc); void eurephia_log_close(eurephiaCTX *ctx); /** diff --git a/common/eurephia_log_struct.h b/common/eurephia_log_struct.h index 615016e..10951ab 100644 --- a/common/eurephia_log_struct.h +++ b/common/eurephia_log_struct.h @@ -1,6 +1,6 @@ /* eurephia_log.h -- eurephia log struct definition * - * GPLv2 only - Copyright (C) 2009 - 2012 + * GPLv2 only - Copyright (C) 2009 - 2013 * David Sommerseth * * This program is free software; you can redistribute it and/or @@ -32,6 +32,7 @@ #define EUREPHIA_LOG_STRUCT_H #include +#include "openvpn-plugin.h" /* * A lot of these definitions are here to make sure LOG_* definitions will not @@ -76,7 +77,8 @@ * Defines the different valid log types / logging backends */ typedef enum { logFILE, /**< Log to file */ - logSYSLOG /**< Log via syslog */ + logSYSLOG, /**< Log via syslog */ + logOPENVPN, /**< Log via OpenVPN's plugin v3 API */ } eurephiaLOGTYPE; /** @@ -86,6 +88,8 @@ typedef struct { eurephiaLOGTYPE logtype; /**< Defines which log backend to use */ unsigned int opened; /**< Boolean flag, if the logging is openend and enabled */ char *destination; /**< String containing log destination info (filename, syslog facility) */ + plugin_vlog_t ovpn_log; /**< OpenVPN plug-in v3 log API, used when logtype == logOPENVPN */ + FILE *logfile; /**< File pointer to the log file if logtype == logFILE */ int loglevel; /**< Defines the log verbosity, higher number increases log verbosity */ } eurephiaLOG; -- cgit