diff options
author | Yves-Alexis Perez <corsac@debian.org> | 2013-02-07 13:27:27 +0100 |
---|---|---|
committer | Yves-Alexis Perez <corsac@debian.org> | 2013-02-07 13:27:27 +0100 |
commit | 7585facf05d927eb6df3929ce09ed5e60d905437 (patch) | |
tree | e4d14b4dc180db20356b6b01ce0112f3a2d7897e /src/libcharon/bus | |
parent | c1343b3278cdf99533b7902744d15969f9d6fdc1 (diff) | |
download | vyos-strongswan-7585facf05d927eb6df3929ce09ed5e60d905437.tar.gz vyos-strongswan-7585facf05d927eb6df3929ce09ed5e60d905437.zip |
Imported Upstream version 5.0.2
Diffstat (limited to 'src/libcharon/bus')
-rw-r--r-- | src/libcharon/bus/bus.c | 5 | ||||
-rw-r--r-- | src/libcharon/bus/bus.h | 42 | ||||
-rw-r--r-- | src/libcharon/bus/listeners/file_logger.c | 106 | ||||
-rw-r--r-- | src/libcharon/bus/listeners/file_logger.h | 29 | ||||
-rw-r--r-- | src/libcharon/bus/listeners/sys_logger.c | 32 | ||||
-rw-r--r-- | src/libcharon/bus/listeners/sys_logger.h | 11 |
6 files changed, 203 insertions, 22 deletions
diff --git a/src/libcharon/bus/bus.c b/src/libcharon/bus/bus.c index 1f9592e6e..b5cdaaa89 100644 --- a/src/libcharon/bus/bus.c +++ b/src/libcharon/bus/bus.c @@ -309,6 +309,7 @@ METHOD(bus_t, vlog, void, va_end(copy); if (len >= sizeof(buf)) { + len++; data.message = malloc(len); len = vsnprintf(data.message, len, format, args); } @@ -719,6 +720,10 @@ METHOD(bus_t, authorize, bool, } enumerator->destroy(enumerator); this->mutex->unlock(this->mutex); + if (!success) + { + alert(this, ALERT_AUTHORIZATION_FAILED); + } return success; } diff --git a/src/libcharon/bus/bus.h b/src/libcharon/bus/bus.h index aba8acdbd..4645bbde6 100644 --- a/src/libcharon/bus/bus.h +++ b/src/libcharon/bus/bus.h @@ -28,7 +28,7 @@ typedef struct bus_t bus_t; #include <stdarg.h> -#include <debug.h> +#include <utils/debug.h> #include <sa/ike_sa.h> #include <sa/child_sa.h> #include <processing/jobs/job.h> @@ -86,10 +86,50 @@ enum alert_t { ALERT_RADIUS_NOT_RESPONDING, /** a shutdown signal has been received, argument is the signal (int) */ ALERT_SHUTDOWN_SIGNAL, + /** creating local authentication data failed, no arguments */ + ALERT_LOCAL_AUTH_FAILED, /** peer authentication failed, no arguments */ ALERT_PEER_AUTH_FAILED, /** failed to resolve peer address, no arguments */ ALERT_PEER_ADDR_FAILED, + /** peer did not respond to initial message, current try (int, 0-based) */ + ALERT_PEER_INIT_UNREACHABLE, + /** received IKE message with invalid SPI, argument is message_t* */ + ALERT_INVALID_IKE_SPI, + /** received IKE message with invalid header, argument is message_t* */ + ALERT_PARSE_ERROR_HEADER, + /** received IKE message with invalid body, argument is message_t*, + * followed by a status_t result returned by message_t.parse_body(). */ + ALERT_PARSE_ERROR_BODY, + /** sending a retransmit for a message, argument is packet_t */ + ALERT_RETRANSMIT_SEND, + /** sending retransmits timed out, argument is packet_t */ + ALERT_RETRANSMIT_SEND_TIMEOUT, + /** received a retransmit for a message, argument is message_t */ + ALERT_RETRANSMIT_RECEIVE, + /** received half-open timeout before IKE_SA established, no argument */ + ALERT_HALF_OPEN_TIMEOUT, + /** IKE proposals do not match, argument is linked_list_t of proposal_t */ + ALERT_PROPOSAL_MISMATCH_IKE, + /** CHILD proposals do not match, argument is linked_list_t of proposal_t */ + ALERT_PROPOSAL_MISMATCH_CHILD, + /** traffic selectors do not match, arguments are two linked_list_t + * containing traffic_selector_t for initiator and for responder */ + ALERT_TS_MISMATCH, + /** Installation of IPsec SAs failed, argument is child_sa_t */ + ALERT_INSTALL_CHILD_SA_FAILED, + /** Installation of IPsec Policy failed, argument is child_sa_t */ + ALERT_INSTALL_CHILD_POLICY_FAILED, + /** IKE_SA deleted because of "replace" unique policy, no argument */ + ALERT_UNIQUE_REPLACE, + /** IKE_SA deleted because of "keep" unique policy, no argument */ + ALERT_UNIQUE_KEEP, + /** IKE_SA kept on failed child SA establishment, no argument */ + ALERT_KEEP_ON_CHILD_SA_FAILURE, + /** allocating virtual IP failed, linked_list_t of host_t requested */ + ALERT_VIP_FAILURE, + /** an authorize() hook failed, no argument */ + ALERT_AUTHORIZATION_FAILED, }; /** diff --git a/src/libcharon/bus/listeners/file_logger.c b/src/libcharon/bus/listeners/file_logger.c index 9c8458eb5..68a386d11 100644 --- a/src/libcharon/bus/listeners/file_logger.c +++ b/src/libcharon/bus/listeners/file_logger.c @@ -17,10 +17,15 @@ #include <stdio.h> #include <string.h> #include <time.h> +#include <errno.h> +#include <unistd.h> +#include <sys/types.h> #include "file_logger.h" +#include <daemon.h> #include <threading/mutex.h> +#include <threading/rwlock.h> typedef struct private_file_logger_t private_file_logger_t; @@ -35,7 +40,12 @@ struct private_file_logger_t { file_logger_t public; /** - * output file + * File name of the target + */ + char *filename; + + /** + * Current output file */ FILE *out; @@ -58,6 +68,11 @@ struct private_file_logger_t { * Mutex to ensure multi-line log messages are not torn apart */ mutex_t *mutex; + + /** + * Lock to read/write options (FD, levels, time_format, etc.) + */ + rwlock_t *lock; }; METHOD(logger_t, log_, void, @@ -69,6 +84,12 @@ METHOD(logger_t, log_, void, struct tm tm; time_t t; + this->lock->read_lock(this->lock); + if (!this->out) + { /* file is not open */ + this->lock->unlock(this->lock); + return; + } if (this->time_format) { t = time(NULL); @@ -117,17 +138,24 @@ METHOD(logger_t, log_, void, current = next + 1; } this->mutex->unlock(this->mutex); + this->lock->unlock(this->lock); } METHOD(logger_t, get_level, level_t, private_file_logger_t *this, debug_t group) { - return this->levels[group]; + level_t level; + + this->lock->read_lock(this->lock); + level = this->levels[group]; + this->lock->unlock(this->lock); + return level; } METHOD(file_logger_t, set_level, void, private_file_logger_t *this, debug_t group, level_t level) { + this->lock->write_lock(this->lock); if (group < DBG_ANY) { this->levels[group] = level; @@ -139,23 +167,81 @@ METHOD(file_logger_t, set_level, void, this->levels[group] = level; } } + this->lock->unlock(this->lock); } -METHOD(file_logger_t, destroy, void, - private_file_logger_t *this) +METHOD(file_logger_t, set_options, void, + private_file_logger_t *this, char *time_format, bool ike_name) { - if (this->out != stdout && this->out != stderr) + this->lock->write_lock(this->lock); + free(this->time_format); + this->time_format = strdupnull(time_format); + this->ike_name = ike_name; + this->lock->unlock(this->lock); +} + +/** + * Close the current file, if any + */ +static void close_file(private_file_logger_t *this) +{ + if (this->out && this->out != stdout && this->out != stderr) { fclose(this->out); + this->out = NULL; + } +} + +METHOD(file_logger_t, open_, void, + private_file_logger_t *this, bool flush_line, bool append) +{ + FILE *file; + + if (streq(this->filename, "stderr")) + { + file = stderr; + } + else if (streq(this->filename, "stdout")) + { + file = stdout; + } + else + { + file = fopen(this->filename, append ? "a" : "w"); + if (file == NULL) + { + DBG1(DBG_DMN, "opening file %s for logging failed: %s", + this->filename, strerror(errno)); + return; + } + if (flush_line) + { + setlinebuf(file); + } } + this->lock->write_lock(this->lock); + close_file(this); + this->out = file; + this->lock->unlock(this->lock); +} + +METHOD(file_logger_t, destroy, void, + private_file_logger_t *this) +{ + this->lock->write_lock(this->lock); + close_file(this); + this->lock->unlock(this->lock); this->mutex->destroy(this->mutex); + this->lock->destroy(this->lock); + free(this->time_format); + free(this->filename); free(this); } /* * Described in header. */ -file_logger_t *file_logger_create(FILE *out, char *time_format, bool ike_name) +file_logger_t *file_logger_create(char *filename) { private_file_logger_t *this; @@ -166,16 +252,16 @@ file_logger_t *file_logger_create(FILE *out, char *time_format, bool ike_name) .get_level = _get_level, }, .set_level = _set_level, + .set_options = _set_options, + .open = _open_, .destroy = _destroy, }, - .out = out, - .time_format = time_format, - .ike_name = ike_name, + .filename = strdup(filename), .mutex = mutex_create(MUTEX_TYPE_DEFAULT), + .lock = rwlock_create(RWLOCK_TYPE_DEFAULT), ); set_level(this, DBG_ANY, LEVEL_SILENT); return &this->public; } - diff --git a/src/libcharon/bus/listeners/file_logger.h b/src/libcharon/bus/listeners/file_logger.h index 85a2690a2..9e5aed50b 100644 --- a/src/libcharon/bus/listeners/file_logger.h +++ b/src/libcharon/bus/listeners/file_logger.h @@ -1,4 +1,5 @@ /* + * Copyright (C) 2012 Tobias Brunner * Copyright (C) 2006 Martin Willi * Hochschule fuer Technik Rapperswil * @@ -44,6 +45,22 @@ struct file_logger_t { void (*set_level) (file_logger_t *this, debug_t group, level_t level); /** + * Set options used by this logger + * + * @param time_format format of timestamp prefix, as in strftime(), cloned + * @param ike_name TRUE to prefix the name of the IKE_SA + */ + void (*set_options) (file_logger_t *this, char *time_format, bool ike_name); + + /** + * Open (or reopen) the log file according to the given parameters + * + * @param flush_line TRUE to flush buffers after every logged line + * @param append FALSE to overwrite an existing file, TRUE to append + */ + void (*open) (file_logger_t *this, bool flush_line, bool append); + + /** * Destroys a file_logger_t object. */ void (*destroy) (file_logger_t *this); @@ -52,11 +69,13 @@ struct file_logger_t { /** * Constructor to create a file_logger_t object. * - * @param out FILE to write to - * @param time_format format of timestamp prefix, as in strftime() - * @param ike_name TRUE to prefix the name of the IKE_SA - * @return file_logger_t object + * The logger has to be opened via file_logger_t.open() before anything is + * logged. + * + * @param filename name of the log file (stderr and stdout are handled + * specially), cloned + * @return file_logger_t object */ -file_logger_t *file_logger_create(FILE *out, char *time_format, bool ike_name); +file_logger_t *file_logger_create(char *filename); #endif /** FILE_LOGGER_H_ @}*/ diff --git a/src/libcharon/bus/listeners/sys_logger.c b/src/libcharon/bus/listeners/sys_logger.c index 53fdefe89..82e2c8e4c 100644 --- a/src/libcharon/bus/listeners/sys_logger.c +++ b/src/libcharon/bus/listeners/sys_logger.c @@ -21,6 +21,7 @@ #include "sys_logger.h" #include <threading/mutex.h> +#include <threading/rwlock.h> typedef struct private_sys_logger_t private_sys_logger_t; @@ -53,6 +54,11 @@ struct private_sys_logger_t { * Mutex to ensure multi-line log messages are not torn apart */ mutex_t *mutex; + + /** + * Lock to read/write options (levels, ike_name) + */ + rwlock_t *lock; }; METHOD(logger_t, log_, void, @@ -65,6 +71,7 @@ METHOD(logger_t, log_, void, /* cache group name and optional name string */ snprintf(groupstr, sizeof(groupstr), "%N", debug_names, group); + this->lock->read_lock(this->lock); if (this->ike_name && ike_sa) { if (ike_sa->get_peer_cfg(ike_sa)) @@ -78,6 +85,7 @@ METHOD(logger_t, log_, void, ike_sa->get_unique_id(ike_sa)); } } + this->lock->unlock(this->lock); /* do a syslog for every line */ this->mutex->lock(this->mutex); @@ -100,12 +108,18 @@ METHOD(logger_t, log_, void, METHOD(logger_t, get_level, level_t, private_sys_logger_t *this, debug_t group) { - return this->levels[group]; + level_t level; + + this->lock->read_lock(this->lock); + level = this->levels[group]; + this->lock->unlock(this->lock); + return level; } METHOD(sys_logger_t, set_level, void, private_sys_logger_t *this, debug_t group, level_t level) { + this->lock->write_lock(this->lock); if (group < DBG_ANY) { this->levels[group] = level; @@ -117,12 +131,21 @@ METHOD(sys_logger_t, set_level, void, this->levels[group] = level; } } + this->lock->unlock(this->lock); +} + +METHOD(sys_logger_t, set_options, void, + private_sys_logger_t *this, bool ike_name) +{ + this->lock->write_lock(this->lock); + this->ike_name = ike_name; + this->lock->unlock(this->lock); } METHOD(sys_logger_t, destroy, void, private_sys_logger_t *this) { - closelog(); + this->lock->destroy(this->lock); this->mutex->destroy(this->mutex); free(this); } @@ -130,7 +153,7 @@ METHOD(sys_logger_t, destroy, void, /* * Described in header. */ -sys_logger_t *sys_logger_create(int facility, bool ike_name) +sys_logger_t *sys_logger_create(int facility) { private_sys_logger_t *this; @@ -141,11 +164,12 @@ sys_logger_t *sys_logger_create(int facility, bool ike_name) .get_level = _get_level, }, .set_level = _set_level, + .set_options = _set_options, .destroy = _destroy, }, .facility = facility, - .ike_name = ike_name, .mutex = mutex_create(MUTEX_TYPE_DEFAULT), + .lock = rwlock_create(RWLOCK_TYPE_DEFAULT), ); set_level(this, DBG_ANY, LEVEL_SILENT); diff --git a/src/libcharon/bus/listeners/sys_logger.h b/src/libcharon/bus/listeners/sys_logger.h index fcb6655ca..9a0fee018 100644 --- a/src/libcharon/bus/listeners/sys_logger.h +++ b/src/libcharon/bus/listeners/sys_logger.h @@ -1,4 +1,5 @@ /* + * Copyright (C) 2012 Tobias Brunner * Copyright (C) 2006 Martin Willi * Hochschule fuer Technik Rapperswil * @@ -44,6 +45,13 @@ struct sys_logger_t { void (*set_level) (sys_logger_t *this, debug_t group, level_t level); /** + * Set options used by this logger. + * + * @param ike_name TRUE to prefix the name of the IKE_SA + */ + void (*set_options) (sys_logger_t *this, bool ike_name); + + /** * Destroys a sys_logger_t object. */ void (*destroy) (sys_logger_t *this); @@ -53,9 +61,8 @@ struct sys_logger_t { * Constructor to create a sys_logger_t object. * * @param facility syslog facility to use - * @param ike_name TRUE to prefix the name of the IKE_SA * @return sys_logger_t object */ -sys_logger_t *sys_logger_create(int facility, bool ike_name); +sys_logger_t *sys_logger_create(int facility); #endif /** SYS_LOGGER_H_ @}*/ |