Commit fa13be99 authored by Alexander Færøy's avatar Alexander Færøy
Browse files

Add SyslogFacility option.

This patch adds the SyslogFacility option to torrc. This option allows
you to customize the logging facility passed to openlog(3). The default
value will continue to be the configure time constant LOGFACILITY.

See: https://bugs.torproject.org/25355
parent 54e25ab1
Pipeline #18027858 failed with stage
in 8 minutes and 7 seconds
o Minor features (logging):
- When logging to syslog, it is now possible to change the facility value by
setting the SyslogFacility entry in torrc. Fixes bug 25355.
......@@ -723,6 +723,12 @@ GENERAL OPTIONS
If 1, Tor will overwrite logs at startup and in response to a HUP signal,
instead of appending to them. (Default: 0)
[[SyslogFacility]] **SyslogFacility** __facility__:
When logging to syslog this option sets the syslog facility value. This
value affects all syslog-based Log entries. Consult the syslog(3) man page for
information about possible values available on your platform. Can not be
changed while tor is running. (Default: none)
[[SyslogIdentityTag]] **SyslogIdentityTag** __tag__::
When logging to syslog, adds a tag to the syslog identity such that
log entries are marked with "Tor-__tag__". Can not be changed while tor is
......
......@@ -32,6 +32,9 @@
#endif
#include "compat.h"
#include "util.h"
#ifdef HAVE_SYSLOG_H
#define SYSLOG_NAMES
#endif // HAVE_SYSLOG_H
#define LOG_PRIVATE
#include "torlog.h"
#include "container.h"
......@@ -1162,6 +1165,67 @@ add_file_log(const log_severity_list_t *severity, const char *filename,
}
#ifdef HAVE_SYSLOG_H
/**
* Helper function to convert a syslog facility string into its
* corresponding integer representation. Returns -1 if no valid facility
* value was found, otherwise returns the integer facility value.
*/
static int
syslog_facility_from_string(const char *syslog_facility)
{
int facility = -1;
// Both CODE and facilitynames are defined in syslog.h.
CODE *code = facilitynames;
if (syslog_facility == NULL)
return facility;
while (code->c_name) {
if (!strcasecmp(syslog_facility, code->c_name)) {
facility = code->c_val;
break;
}
++code;
}
return facility;
}
/**
* Helper function to convert <b>syslog_facility</b> into a valid syslog
* facility integer. Returns the compile time defined default value if
* no value was set in torrc or if we were unable to convert the user
* defined string into a valid facility integer value.
*/
static int
syslog_facility_value(const char *syslog_facility)
{
int facility = syslog_facility_from_string(syslog_facility);
if (facility == -1) {
// The user passed a syslog_facility via the SyslogFacility entry in
// their torrc, but we were unable to find the corresponding value
// in syslog.h. Since we are in the initialization process of the
// logging subsystem we cannot use the logging system itself to warn
// the user about this.
facility = LOGFACILITY;
}
return facility;
}
/**
* Check if a given <b>syslog_facility</b> is valid. Returns 1
* iff the value is valid, otherwise returns 0.
*/
int
syslog_facility_validate(const char *syslog_facility)
{
return syslog_facility_from_string(syslog_facility) != -1;
}
/**
* Add a log handler to send messages to they system log facility.
*
......@@ -1170,18 +1234,20 @@ add_file_log(const log_severity_list_t *severity, const char *filename,
*/
int
add_syslog_log(const log_severity_list_t *severity,
const char* syslog_identity_tag)
const char *syslog_identity_tag,
const char *syslog_facility)
{
logfile_t *lf;
if (syslog_count++ == 0) {
/* This is the first syslog. */
static char buf[256];
int facility = syslog_facility_value(syslog_facility);
if (syslog_identity_tag) {
tor_snprintf(buf, sizeof(buf), "Tor-%s", syslog_identity_tag);
} else {
tor_snprintf(buf, sizeof(buf), "Tor");
}
openlog(buf, LOG_PID | LOG_NDELAY, LOGFACILITY);
openlog(buf, LOG_PID | LOG_NDELAY, facility);
}
lf = tor_malloc_zero(sizeof(logfile_t));
......
......@@ -146,8 +146,10 @@ void add_stream_log(const log_severity_list_t *severity, const char *name,
int add_file_log(const log_severity_list_t *severity, const char *filename,
const int truncate);
#ifdef HAVE_SYSLOG_H
int syslog_facility_validate(const char *syslog_facility);
int add_syslog_log(const log_severity_list_t *severity,
const char* syslog_identity_tag);
const char *syslog_identity_tag,
const char *syslog_facility);
#endif // HAVE_SYSLOG_H.
#ifdef HAVE_ANDROID_LOG_H
int add_android_log(const log_severity_list_t *severity,
......
......@@ -427,6 +427,7 @@ static config_var_t option_vars_[] = {
V(LogMessageDomains, BOOL, "0"),
V(LogTimeGranularity, MSEC_INTERVAL, "1 second"),
V(TruncateLogFile, BOOL, "0"),
V(SyslogFacility, STRING, NULL),
V(SyslogIdentityTag, STRING, NULL),
V(AndroidIdentityTag, STRING, NULL),
V(LongLivedPorts, CSV,
......@@ -3389,6 +3390,14 @@ options_validate(or_options_t *old_options, or_options_t *options,
}
/* Validate the tor_log(s) */
if (options->SyslogFacility != NULL) {
if (! syslog_facility_validate(options->SyslogFacility)) {
tor_asprintf(msg, "Invalid SyslogFacility entry: %s",
options->SyslogFacility);
return -1;
}
}
if (options_init_logs(old_options, options, 1)<0)
REJECT("Failed to validate Log options. See logs for details.");
......@@ -4752,6 +4761,7 @@ options_transition_allowed(const or_options_t *old,
NO_CHANGE_STRING(CacheDirectory);
NO_CHANGE_STRING(User);
NO_CHANGE_BOOL(KeepBindCapabilities);
NO_CHANGE_STRING(SyslogFacility);
NO_CHANGE_STRING(SyslogIdentityTag);
NO_CHANGE_STRING(AndroidIdentityTag);
NO_CHANGE_BOOL(HardwareAccel);
......@@ -5749,7 +5759,9 @@ options_init_logs(const or_options_t *old_options, or_options_t *options,
if (!strcasecmp(smartlist_get(elts,0), "syslog")) {
#ifdef HAVE_SYSLOG_H
if (!validate_only) {
add_syslog_log(severity, options->SyslogIdentityTag);
add_syslog_log(severity,
options->SyslogIdentityTag,
options->SyslogFacility);
}
#else
log_warn(LD_CONFIG, "Syslog is not supported on this system. Sorry.");
......
......@@ -3687,6 +3687,7 @@ typedef struct {
* each log message occurs? */
int TruncateLogFile; /**< Boolean: Should we truncate the log file
before we start writing? */
char *SyslogFacility; /**< Syslog facility. */
char *SyslogIdentityTag; /**< Identity tag to add for syslog logging. */
char *AndroidIdentityTag; /**< Identity tag to add for Android logging. */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment