From e0a96e28757d100923978e09256ec5a5a0080642 Mon Sep 17 00:00:00 2001 From: Sergey Safarov Date: Sun, 5 Feb 2017 17:44:11 -0500 Subject: [PATCH] FS-10013: Several bugfixes --- .../event_handlers/mod_fail2ban/mod_fail2ban.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/mod/event_handlers/mod_fail2ban/mod_fail2ban.c b/src/mod/event_handlers/mod_fail2ban/mod_fail2ban.c index 4a010a71ff..6ea6deadd0 100644 --- a/src/mod/event_handlers/mod_fail2ban/mod_fail2ban.c +++ b/src/mod/event_handlers/mod_fail2ban/mod_fail2ban.c @@ -39,17 +39,17 @@ static switch_status_t mod_fail2ban_do_config(void) if (strncmp(var,"logfile", 7) == 0) { if (zstr(val)) { - globals.logfile_name = switch_core_strdup(globals.modpool, val); - } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Null or empty Logfile attribute %s: %s\n", var, val); + } else { + globals.logfile_name = switch_core_strdup(globals.modpool, val); } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unknown attribute %s: %s\n", var, val); } } } - if ( ! globals.logfile_name ) { - switch_core_sprintf(globals.modpool, globals.logfile_name, "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, "fail2ban.log"); + if ( zstr(globals.logfile_name) ) { + globals.logfile_name = switch_core_sprintf(globals.modpool, "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, "fail2ban.log"); } if ((status = switch_file_open(&globals.logfile, globals.logfile_name, SWITCH_FOPEN_WRITE|SWITCH_FOPEN_APPEND|SWITCH_FOPEN_CREATE, SWITCH_FPROT_OS_DEFAULT, globals.modpool)) != SWITCH_STATUS_SUCCESS) { @@ -65,18 +65,20 @@ static switch_status_t mod_fail2ban_do_config(void) static int fail2ban_logger(const char *message, char *user, char *ip) { + switch_time_exp_t tm; if (!globals.logfile) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not print to fail2ban log!\n"); return -1; } - struct tm * timeinfo = localtime(switch_epoch_time_now(NULL)); - return switch_file_printf(globals.logfile, "%s user[%s] ip[%s] at[%s]\n", message, user, ip, asctime(timeinfo)); + switch_time_exp_lt(&tm, switch_micro_time_now()); + return switch_file_printf(globals.logfile, "%s user[%s] ip[%s] at[%04u-%02u-%02uT%02u:%02u:%02u.%06u%+03d%02d]\n", message, user, ip, + tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, + tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_gmtoff / 3600, tm.tm_gmtoff % 3600); } static void fail2ban_event_handler(switch_event_t *event) { - struct tm * timeinfo = localtime(switch_epoch_time_now(NULL)); if (event->event_id == SWITCH_EVENT_CUSTOM) { if (strncmp(event->subclass_name, "sofia::register_attempt",23) == 0) { fail2ban_logger("A registration was attempted", switch_event_get_header(event, "to-user"), switch_event_get_header(event, "network-ip"));