added plugin load mechanism to pluto and scepclient and improved debug output

This commit is contained in:
Andreas Steffen
2009-04-21 18:48:58 +00:00
parent b757ebc2a7
commit 6e9fe153fc
4 changed files with 140 additions and 77 deletions
+6 -3
View File
@@ -87,10 +87,10 @@ u_int16_t cur_from_port; /* host order */
static void pluto_dbg(int level, char *fmt, ...)
{
int priority = LOG_INFO;
int debug_level;
char buffer[8192];
char *current = buffer, *next;
va_list args;
int debug_level;
if (cur_debugging & DBG_PRIVATE)
{
@@ -115,7 +115,10 @@ static void pluto_dbg(int level, char *fmt, ...)
if (log_to_stderr)
{
fprintf(stderr, "| ");
if (level > 1)
{
fprintf(stderr, "| ");
}
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
}
@@ -132,7 +135,7 @@ static void pluto_dbg(int level, char *fmt, ...)
{
*(next++) = '\0';
}
syslog(priority, "| %s\n", current);
syslog(priority, "%s%s\n", (level > 1)? "| ":"", current);
current = next;
}
}
+26 -2
View File
@@ -40,6 +40,8 @@
#include <freeswan.h>
#include <library.h>
#include <debug.h>
#include <utils/enumerator.h>
#include <pfkeyv2.h>
#include <pfkey.h>
@@ -223,6 +225,24 @@ bool pkcs11_proxy = FALSE;
*/
static const char *pkcs11_init_args = NULL;
/**
* Log loaded plugins
*/
static void print_plugins()
{
char buf[BUF_LEN], *plugin;
int len = 0;
enumerator_t *enumerator;
enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
while (len < BUF_LEN && enumerator->enumerate(enumerator, &plugin))
{
len += snprintf(&buf[len], BUF_LEN-len, "%s ", plugin);
}
enumerator->destroy(enumerator);
DBG1("loaded plugins: %s", buf);
}
int main(int argc, char **argv)
{
bool fork_desired = TRUE;
@@ -609,6 +629,11 @@ int main(int argc, char **argv)
, ipsec_version_code()
, compile_time_interop_options);
/* load plugins, further infrastructure may need it */
lib->plugins->load(lib->plugins, IPSEC_PLUGINDIR,
lib->settings->get_str(lib->settings, "pluto.load", ""));
print_plugins();
init_nat_traversal(nat_traversal, keep_alive, force_keepalive, nat_t_spf);
init_virtual_ip(virtual_private);
scx_init(pkcs11_module_path, pkcs11_init_args); /* load and initialize PKCS #11 module */
@@ -690,8 +715,7 @@ int main(int argc, char **argv)
* 1 general discomfort
* 10 lock file exists
*/
void
exit_pluto(int status)
void exit_pluto(int status)
{
reset_globals(); /* needed because we may be called in odd state */
free_preshared_secrets();
+88 -32
View File
@@ -29,6 +29,7 @@
#include <sys/types.h>
#include <freeswan.h>
#include <debug.h>
#include <constants.h>
#include <defs.h>
@@ -39,24 +40,90 @@ bool
log_to_stderr = FALSE, /* should log go to stderr? */
log_to_syslog = TRUE; /* should log go to syslog? */
void
init_log(const char *program)
/**
* @brief scepclient dbg function
*/
static void scepclient_dbg(int level, char *fmt, ...)
{
if (log_to_stderr)
setbuf(stderr, NULL);
if (log_to_syslog)
openlog(program, LOG_CONS | LOG_NDELAY | LOG_PID, LOG_AUTHPRIV);
int priority = LOG_INFO;
int debug_level;
char buffer[8192];
char *current = buffer, *next;
va_list args;
if (cur_debugging & DBG_PRIVATE)
{
debug_level = 4;
}
else if (cur_debugging & DBG_RAW)
{
debug_level = 3;
}
else if (cur_debugging & DBG_PARSING)
{
debug_level = 2;
}
else
{
debug_level = 1;
}
if (level <= debug_level)
{
va_start(args, fmt);
if (log_to_stderr)
{
if (level > 1)
{
fprintf(stderr, "| ");
}
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
}
if (log_to_syslog)
{
/* write in memory buffer first */
vsnprintf(buffer, sizeof(buffer), fmt, args);
/* do a syslog with every line */
while (current)
{
next = strchr(current, '\n');
if (next)
{
*(next++) = '\0';
}
syslog(priority, "%s%s\n", (level > 1)? "| ":"", current);
current = next;
}
}
va_end(args);
}
}
void
close_log(void)
void init_log(const char *program)
{
/* enable scepclient bugging hook */
dbg = scepclient_dbg;
if (log_to_stderr)
{
setbuf(stderr, NULL);
}
if (log_to_syslog)
{
openlog(program, LOG_CONS | LOG_NDELAY | LOG_PID, LOG_AUTHPRIV);
}
}
void close_log(void)
{
if (log_to_syslog)
closelog();
}
void
plog(const char *message, ...)
void plog(const char *message, ...)
{
va_list args;
char m[LOG_WIDTH]; /* longer messages will be truncated */
@@ -71,8 +138,7 @@ plog(const char *message, ...)
syslog(LOG_WARNING, "%s", m);
}
void
loglog(int mess_no, const char *message, ...)
void loglog(int mess_no, const char *message, ...)
{
va_list args;
char m[LOG_WIDTH]; /* longer messages will be truncated */
@@ -87,8 +153,7 @@ loglog(int mess_no, const char *message, ...)
syslog(LOG_WARNING, "%s", m);
}
void
log_errno_routine(int e, const char *message, ...)
void log_errno_routine(int e, const char *message, ...)
{
va_list args;
char m[LOG_WIDTH]; /* longer messages will be truncated */
@@ -103,8 +168,7 @@ log_errno_routine(int e, const char *message, ...)
syslog(LOG_ERR, "ERROR: %s. Errno %d: %s", m, e, strerror(e));
}
void
exit_log(const char *message, ...)
void exit_log(const char *message, ...)
{
va_list args;
char m[LOG_WIDTH]; /* longer messages will be truncated */
@@ -120,8 +184,7 @@ exit_log(const char *message, ...)
exit(1);
}
void
exit_log_errno_routine(int e, const char *message, ...)
void exit_log_errno_routine(int e, const char *message, ...)
{
va_list args;
char m[LOG_WIDTH]; /* longer messages will be truncated */
@@ -137,8 +200,7 @@ exit_log_errno_routine(int e, const char *message, ...)
exit(1);
}
void
whack_log(int mess_no, const char *message, ...)
void whack_log(int mess_no, const char *message, ...)
{
va_list args;
char m[LOG_WIDTH]; /* longer messages will be truncated */
@@ -162,8 +224,7 @@ whack_log(int mess_no, const char *message, ...)
*/
char diag_space[sizeof(diag_space)];
err_t
builddiag(const char *fmt, ...)
err_t builddiag(const char *fmt, ...)
{
static char diag_space[LOG_WIDTH]; /* longer messages will be truncated */
char t[sizeof(diag_space)]; /* build result here first */
@@ -181,8 +242,7 @@ builddiag(const char *fmt, ...)
#ifdef DEBUG
void
switch_fail(int n, const char *file_str, unsigned long line_no)
void switch_fail(int n, const char *file_str, unsigned long line_no)
{
char buf[30];
@@ -190,8 +250,7 @@ switch_fail(int n, const char *file_str, unsigned long line_no)
passert_fail(buf, file_str, line_no);
}
void
passert_fail(const char *pred_str, const char *file_str, unsigned long line_no)
void passert_fail(const char *pred_str, const char *file_str, unsigned long line_no)
{
/* we will get a possibly unplanned prefix. Hope it works */
loglog(RC_LOG_SERIOUS, "ASSERTION FAILED at %s:%lu: %s", file_str, line_no, pred_str);
@@ -202,8 +261,7 @@ lset_t
base_debugging = DBG_NONE, /* default to reporting nothing */
cur_debugging = DBG_NONE;
void
pexpect_log(const char *pred_str, const char *file_str, unsigned long line_no)
void pexpect_log(const char *pred_str, const char *file_str, unsigned long line_no)
{
/* we will get a possibly unplanned prefix. Hope it works */
loglog(RC_LOG_SERIOUS, "EXPECTATION FAILED at %s:%lu: %s", file_str, line_no, pred_str);
@@ -211,8 +269,7 @@ pexpect_log(const char *pred_str, const char *file_str, unsigned long line_no)
/* log a debugging message (prefixed by "| ") */
void
DBG_log(const char *message, ...)
void DBG_log(const char *message, ...)
{
va_list args;
char m[LOG_WIDTH]; /* longer messages will be truncated */
@@ -229,8 +286,7 @@ DBG_log(const char *message, ...)
/* dump raw bytes in hex to stderr (for lack of any better destination) */
void
DBG_dump(const char *label, const void *p, size_t len)
void DBG_dump(const char *label, const void *p, size_t len)
{
# define DUMP_LABEL_WIDTH 20 /* arbitrary modest boundary */
# define DUMP_WIDTH (4 * (1 + 4 * 3) + 1)
+20 -40
View File
@@ -27,7 +27,6 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <string.h>
#include <getopt.h>
#include <ctype.h>
@@ -41,6 +40,7 @@
#include <asn1/asn1.h>
#include <asn1/oid.h>
#include <utils/optionsfrom.h>
#include <utils/enumerator.h>
#include "../pluto/constants.h"
#include "../pluto/defs.h"
@@ -272,47 +272,24 @@ usage(const char *message)
exit_scepclient(message);
}
static int debug_level = 1;
/**
* @brief scepclient dbg function
* Log loaded plugins
*/
static void scepclient_dbg(int level, char *fmt, ...)
static void print_plugins()
{
int priority = LOG_INFO;
char buffer[8192];
char *current = buffer, *next;
va_list args;
if (level <= debug_level)
char buf[BUF_LEN], *plugin;
int len = 0;
enumerator_t *enumerator;
enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
while (len < BUF_LEN && enumerator->enumerate(enumerator, &plugin))
{
va_start(args, fmt);
if (log_to_stderr)
{
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
}
if (log_to_syslog)
{
/* write in memory buffer first */
vsnprintf(buffer, sizeof(buffer), fmt, args);
/* do a syslog with every line */
while (current)
{
next = strchr(current, '\n');
if (next)
{
*(next++) = '\0';
}
syslog(priority, "%s\n", current);
current = next;
}
}
va_end(args);
len += snprintf(&buf[len], BUF_LEN-len, "%s ", plugin);
}
enumerator->destroy(enumerator);
DBG1(" loaded plugins: %s", buf);
}
/**
* @brief main of scepclient
*
@@ -762,12 +739,15 @@ int main(int argc, char **argv)
/* break from loop */
break;
}
/* enable scepclient bugging hook */
dbg = scepclient_dbg;
cur_debugging = base_debugging;
init_log("scepclient");
cur_debugging = base_debugging;
/* load plugins, further infrastructure may need it */
lib->plugins->load(lib->plugins, IPSEC_PLUGINDIR,
lib->settings->get_str(lib->settings, "scepclient.load", ""));
print_plugins();
init_rnd_pool();
init_fetch();