2010-06-22 14:04:33 +00:00
|
|
|
/**
|
2011-08-16 10:59:17 +00:00
|
|
|
* A logging plugin for uhub.
|
|
|
|
* Logs to either file or syslog.
|
2010-06-22 14:04:33 +00:00
|
|
|
*/
|
2010-08-04 15:03:37 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "adc/adcconst.h"
|
|
|
|
#include "adc/sid.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "util/ipcalc.h"
|
2010-06-22 14:04:33 +00:00
|
|
|
#include "plugin_api/handle.h"
|
|
|
|
|
2011-08-16 10:59:17 +00:00
|
|
|
#include "util/misc.h"
|
|
|
|
#include "util/config_token.h"
|
|
|
|
#include <syslog.h>
|
|
|
|
|
2010-06-22 14:04:33 +00:00
|
|
|
struct ip_addr_encap;
|
|
|
|
|
2010-08-04 15:03:37 +00:00
|
|
|
struct log_data
|
2010-06-22 14:04:33 +00:00
|
|
|
{
|
2011-08-16 10:59:17 +00:00
|
|
|
enum {
|
|
|
|
mode_file,
|
|
|
|
mode_syslog
|
|
|
|
} logmode;
|
2010-08-04 15:03:37 +00:00
|
|
|
char* logfile;
|
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
2011-08-16 10:59:17 +00:00
|
|
|
static void reset(struct log_data* data)
|
|
|
|
{
|
|
|
|
/* set defaults */
|
|
|
|
data->logmode = mode_file;
|
|
|
|
data->logfile = NULL;
|
|
|
|
data->fd = -1;
|
|
|
|
}
|
2010-08-04 15:03:37 +00:00
|
|
|
|
|
|
|
static void set_error_message(struct plugin_handle* plugin, const char* msg)
|
|
|
|
{
|
|
|
|
plugin->error_msg = msg;
|
2010-06-22 14:04:33 +00:00
|
|
|
}
|
|
|
|
|
2011-08-16 10:59:17 +00:00
|
|
|
static int log_open_file(struct plugin_handle* plugin, struct log_data* data)
|
|
|
|
{
|
2011-11-30 11:19:37 +00:00
|
|
|
int flags = O_CREAT | O_APPEND | O_WRONLY;
|
|
|
|
data->fd = open(data->logfile, flags, 0664);
|
2011-08-16 10:59:17 +00:00
|
|
|
return (data->fd != -1);
|
|
|
|
}
|
2010-08-04 15:03:37 +00:00
|
|
|
|
2011-08-16 10:59:17 +00:00
|
|
|
static int log_open_syslog(struct plugin_handle* plugin)
|
|
|
|
{
|
|
|
|
openlog("uhub", 0, LOG_USER);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct log_data* parse_config(const char* line, struct plugin_handle* plugin)
|
2010-06-22 14:04:33 +00:00
|
|
|
{
|
2010-08-04 15:03:37 +00:00
|
|
|
struct log_data* data = (struct log_data*) hub_malloc(sizeof(struct log_data));
|
2011-08-16 10:59:17 +00:00
|
|
|
struct cfg_tokens* tokens = cfg_tokenize(line);
|
|
|
|
char* token = cfg_token_get_first(tokens);
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
reset(data);
|
|
|
|
|
|
|
|
while (token)
|
2010-08-04 15:03:37 +00:00
|
|
|
{
|
2011-08-16 10:59:17 +00:00
|
|
|
struct cfg_settings* setting = cfg_settings_split(token);
|
|
|
|
|
|
|
|
if (!setting)
|
|
|
|
{
|
|
|
|
set_error_message(plugin, "Unable to parse startup parameters");
|
|
|
|
cfg_tokens_free(tokens);
|
|
|
|
hub_free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(cfg_settings_get_key(setting), "file") == 0)
|
|
|
|
{
|
|
|
|
data->logfile = strdup(cfg_settings_get_value(setting));
|
|
|
|
data->logmode = mode_file;
|
|
|
|
}
|
|
|
|
else if (strcmp(cfg_settings_get_key(setting), "syslog") == 0)
|
|
|
|
{
|
|
|
|
int use_syslog = 0;
|
|
|
|
if (!string_to_boolean(cfg_settings_get_value(setting), &use_syslog))
|
|
|
|
{
|
|
|
|
data->logmode = (use_syslog) ? mode_syslog : mode_file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_error_message(plugin, "Unknown startup parameters given");
|
|
|
|
cfg_tokens_free(tokens);
|
|
|
|
cfg_settings_free(setting);
|
|
|
|
hub_free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg_settings_free(setting);
|
|
|
|
token = cfg_token_get_next(tokens);
|
2010-08-04 15:03:37 +00:00
|
|
|
}
|
2011-08-16 10:59:17 +00:00
|
|
|
|
2011-12-01 12:37:07 +00:00
|
|
|
cfg_tokens_free(tokens);
|
|
|
|
|
2011-08-16 10:59:17 +00:00
|
|
|
if (data->logmode == mode_file)
|
|
|
|
{
|
|
|
|
if ((data->logmode == mode_file && !data->logfile))
|
|
|
|
{
|
|
|
|
set_error_message(plugin, "No log file is given, use file=<path>");
|
|
|
|
hub_free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!log_open_file(plugin, data))
|
|
|
|
{
|
|
|
|
hub_free(data->logfile);
|
|
|
|
hub_free(data);
|
|
|
|
set_error_message(plugin, "Unable to open log file");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!log_open_syslog(plugin))
|
|
|
|
{
|
|
|
|
hub_free(data->logfile);
|
|
|
|
hub_free(data);
|
|
|
|
set_error_message(plugin, "Unable to open syslog");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-04 15:03:37 +00:00
|
|
|
return data;
|
2010-06-22 14:04:33 +00:00
|
|
|
}
|
|
|
|
|
2010-08-04 15:03:37 +00:00
|
|
|
static void log_close(struct log_data* data)
|
2010-06-22 14:04:33 +00:00
|
|
|
{
|
2011-08-16 10:59:17 +00:00
|
|
|
if (data->logmode == mode_file)
|
|
|
|
{
|
|
|
|
hub_free(data->logfile);
|
|
|
|
close(data->fd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
closelog();
|
|
|
|
}
|
2010-08-04 15:03:37 +00:00
|
|
|
hub_free(data);
|
2010-06-22 14:04:33 +00:00
|
|
|
}
|
|
|
|
|
2010-08-04 15:03:37 +00:00
|
|
|
static void log_message(struct log_data* data, const char *format, ...)
|
2010-06-22 14:04:33 +00:00
|
|
|
{
|
2010-08-04 15:03:37 +00:00
|
|
|
static char logmsg[1024];
|
|
|
|
struct tm *tmp;
|
|
|
|
time_t t;
|
|
|
|
va_list args;
|
|
|
|
ssize_t size = 0;
|
|
|
|
|
2011-08-16 10:59:17 +00:00
|
|
|
if (data->logmode == mode_file)
|
|
|
|
{
|
|
|
|
t = time(NULL);
|
|
|
|
tmp = localtime(&t);
|
|
|
|
strftime(logmsg, 32, "%Y-%m-%d %H:%M:%S ", tmp);
|
2010-08-04 15:03:37 +00:00
|
|
|
|
2011-08-16 10:59:17 +00:00
|
|
|
va_start(args, format);
|
|
|
|
size = vsnprintf(logmsg + 20, 1004, format, args);
|
|
|
|
va_end(args);
|
2010-08-04 15:03:37 +00:00
|
|
|
|
2011-12-18 23:40:02 +00:00
|
|
|
if (write(data->fd, logmsg, size + 20) < (size+20))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to write full log. Error=%d: %s\n", errno, strerror(errno));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-30 11:19:37 +00:00
|
|
|
#ifdef _POSIX_SYNCHRONIZED_IO
|
2011-12-18 23:40:02 +00:00
|
|
|
fdatasync(data->fd);
|
2011-11-30 11:19:37 +00:00
|
|
|
#else
|
2011-12-18 23:40:02 +00:00
|
|
|
fsync(data->fd);
|
2011-11-30 11:19:37 +00:00
|
|
|
#endif
|
2011-12-18 23:40:02 +00:00
|
|
|
}
|
2011-08-16 10:59:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
va_start(args, format);
|
|
|
|
vsyslog(LOG_INFO, format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
2010-06-22 14:04:33 +00:00
|
|
|
}
|
|
|
|
|
2010-08-04 15:03:37 +00:00
|
|
|
static void log_user_login(struct plugin_handle* plugin, struct plugin_user* user)
|
|
|
|
{
|
|
|
|
const char* cred = auth_cred_to_string(user->credentials);
|
|
|
|
const char* addr = ip_convert_to_string(&user->addr);
|
|
|
|
|
|
|
|
log_message(plugin->ptr, "LoginOK %s/%s %s \"%s\" (%s) \"%s\"\n", sid_to_string(user->sid), user->cid, addr, user->nick, cred, user->user_agent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void log_user_login_error(struct plugin_handle* plugin, struct plugin_user* user, const char* reason)
|
|
|
|
{
|
|
|
|
const char* addr = ip_convert_to_string(&user->addr);
|
|
|
|
log_message(plugin->ptr, "LoginError %s/%s %s \"%s\" (%s) \"%s\"\n", sid_to_string(user->sid), user->cid, addr, user->nick, reason, user->user_agent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void log_user_logout(struct plugin_handle* plugin, struct plugin_user* user, const char* reason)
|
|
|
|
{
|
|
|
|
const char* addr = ip_convert_to_string(&user->addr);
|
|
|
|
log_message(plugin->ptr, "Logout %s/%s %s \"%s\" (%s) \"%s\"\n", sid_to_string(user->sid), user->cid, addr, user->nick, reason, user->user_agent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void log_change_nick(struct plugin_handle* plugin, struct plugin_user* user, const char* new_nick)
|
|
|
|
{
|
|
|
|
const char* addr = ip_convert_to_string(&user->addr);
|
|
|
|
log_message(plugin->ptr, "NickChange %s/%s %s \"%s\" -> \"%s\"\n", sid_to_string(user->sid), user->cid, addr, user->nick, new_nick);
|
|
|
|
}
|
2010-06-22 14:04:33 +00:00
|
|
|
|
2010-07-29 06:43:22 +00:00
|
|
|
int plugin_register(struct plugin_handle* plugin, const char* config)
|
2010-06-22 14:04:33 +00:00
|
|
|
{
|
2010-08-30 09:54:34 +00:00
|
|
|
PLUGIN_INITIALIZE(plugin, "Logging plugin", "1.0", "Logs users entering and leaving the hub.");
|
2010-08-04 15:03:37 +00:00
|
|
|
|
2010-06-22 14:04:33 +00:00
|
|
|
plugin->funcs.on_user_login = log_user_login;
|
2010-08-04 15:03:37 +00:00
|
|
|
plugin->funcs.on_user_login_error = log_user_login_error;
|
2010-06-22 14:04:33 +00:00
|
|
|
plugin->funcs.on_user_logout = log_user_logout;
|
2010-08-04 15:03:37 +00:00
|
|
|
plugin->funcs.on_user_nick_change = log_change_nick;
|
|
|
|
|
2011-08-16 10:59:17 +00:00
|
|
|
plugin->ptr = parse_config(config, plugin);
|
2010-08-04 15:03:37 +00:00
|
|
|
if (!plugin->ptr)
|
|
|
|
return -1;
|
2010-06-22 14:04:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-29 06:43:22 +00:00
|
|
|
int plugin_unregister(struct plugin_handle* plugin)
|
2010-06-22 14:04:33 +00:00
|
|
|
{
|
|
|
|
/* No need to do anything! */
|
2010-08-04 15:03:37 +00:00
|
|
|
log_close(plugin->ptr);
|
2010-06-22 14:04:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|