diff --git a/alertik.c b/alertik.c index 4e363a8..6f4571c 100644 --- a/alertik.c +++ b/alertik.c @@ -51,15 +51,15 @@ int main(void) log_init(); - if (!init_static_events() && !init_environment_events()) - panic("No event was configured, please configure at least one\n" - "before proceeding!\n"); - log_msg( "Alertik (" GIT_HASH ") (built at " __DATE__ " " __TIME__ ")\n"); log_msg(" (https://github.com/Theldus/alertik)\n"); log_msg("-------------------------------------------------\n"); + if (!init_static_events() && !init_environment_events()) + panic("No event was configured, please configure at least one\n" + "before proceeding!\n"); + fd = syslog_create_udp_socket(); if (pthread_create(&handler, NULL, handle_messages, NULL)) panic_errno("Unable to create hanler thread!"); diff --git a/env_events.h b/env_events.h index 8477441..8ac8335 100644 --- a/env_events.h +++ b/env_events.h @@ -19,7 +19,6 @@ regex_t regex; /* Compiled regex. */ }; - extern struct env_event env_events[MAX_ENV_EVENTS]; extern int init_environment_events(void); extern int process_environment_event(struct log_event *ev); diff --git a/events.c b/events.c index 2cb5bd1..08528a8 100644 --- a/events.c +++ b/events.c @@ -13,17 +13,20 @@ #include "notifiers.h" #include "log.h" -static void handle_wifi_login_attempts(struct log_event *, int); +/* Misc. */ +#define MAX_MATCHES 32 +static regmatch_t pmatch[MAX_MATCHES]; /* Handlers. */ -struct ev_handler handlers[NUM_EVENTS] = { +static void handle_wifi_login_attempts(struct log_event *, int); +struct static_event static_events[NUM_EVENTS] = { /* Failed login attempts. */ { - .str = "unicast key exchange timeout", - .hnd = handle_wifi_login_attempts, - .evnt_type = EVNT_SUBSTR, - .enabled = 0, - .evnt_notifier_idx = NOTIFY_IDX_TELE + .ev_match_str = "unicast key exchange timeout", + .hnd = handle_wifi_login_attempts, + .ev_match_type = EVNT_SUBSTR, + .enabled = 0, + .ev_notifier_idx = NOTIFY_IDX_TELE }, /* Add new handlers here. */ }; @@ -63,15 +66,27 @@ int process_static_event(struct log_event *ev) { int i; int handled; + struct static_event *sta_ev; for (i = 0, handled = 0; i < NUM_EVENTS; i++) { /* Skip not enabled events. */ - if (!handlers[i].enabled) + if (!static_events[i].enabled) continue; - if (strstr(ev->msg, handlers[i].str)) { - handlers[i].hnd(ev, i); - handled += 1; + sta_ev = &static_events[i]; + + if (static_events[i].ev_match_type == EVNT_SUBSTR) { + if (strstr(ev->msg, static_events[i].ev_match_str)) { + static_events[i].hnd(ev, i); + handled += 1; + } + } + + else { + if (regexec(&sta_ev->regex, ev->msg, MAX_MATCHES, pmatch, 0)) { + static_events[i].hnd(ev, i); + handled += 1; + } } } return handled; @@ -114,9 +129,9 @@ int init_static_events(void) ev, NUM_EVENTS - 1); /* Try to retrieve & initialize notifier for the event. */ - handlers[ev].evnt_notifier_idx = + static_events[ev].ev_notifier_idx = get_event_idx(ev, "NOTIFIER", notifiers_str, NUM_NOTIFIERS); - handlers[ev].enabled = 1; + static_events[ev].enabled = 1; if (*end != ',' && *end != '\0') panic("Wrong event number in STATIC_EVENTS_ENABLED, aborting...\n"); @@ -126,23 +141,38 @@ int init_static_events(void) log_msg("Static events summary:\n"); for (int i = 0; i < NUM_EVENTS; i++) { - if (!handlers[i].enabled) + if (!static_events[i].enabled) continue; printf( "STATIC_EVENT%d : enabled\n" "STATIC_EVENT%d_NOTIFIER: %s\n\n", - i, i, notifiers_str[handlers[i].evnt_notifier_idx] + i, i, notifiers_str[static_events[i].ev_notifier_idx] ); /* Try to setup notifier if not yet. */ - notifiers[handlers[i].evnt_notifier_idx].setup(); + notifiers[static_events[i].ev_notifier_idx].setup(); + + /* If regex, compile it first. */ + if (static_events[i].ev_match_type == EVNT_REGEX) { + if (regcomp( + &static_events[i].regex, + static_events[i].ev_match_str, + REG_EXTENDED)) + { + panic("Unable to compile regex (%s) for EVENT%d!!!", + static_events[i].ev_match_str, i); + } + } } return 1; } +/////////////////////////////////////////////////////////////////////////////// ///////////////////////////// FAILED LOGIN ATTEMPTS /////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + static int parse_login_attempt_msg(const char *msg, char *wifi_iface, char *mac_addr) { @@ -203,7 +233,7 @@ static void handle_wifi_login_attempts(struct log_event *ev, int idx_env) log_msg("> Retrieved info, MAC: (%s), Interface: (%s)\n", mac_addr, wifi_iface); - notif_idx = handlers[idx_env].evnt_notifier_idx; + notif_idx = static_events[idx_env].ev_notifier_idx; if (notifiers[notif_idx].send_notification(notification_message) < 0) { log_msg("unable to send the notification!\n"); return; diff --git a/events.h b/events.h index 7488f74..49b4826 100644 --- a/events.h +++ b/events.h @@ -6,6 +6,7 @@ #ifndef EVENTS_H #define EVENTS_H + #include #include #define MSG_MAX 2048 @@ -20,15 +21,15 @@ time_t timestamp; }; - struct ev_handler { - const char *str; /* Substr or regex to match. */ + struct static_event { void(*hnd)(struct log_event *, int); /* Event handler. */ - int evnt_type; /* Whether substr or regex. */ - int evnt_notifier_idx; /* Telegram, Discord... */ - int enabled; /* Whether if handler enabled or not. */ + const char *ev_match_str; /* Substr or regex to match. */ + int ev_match_type; /* Whether substr or regex. */ + int ev_notifier_idx; /* Telegram, Discord... */ + int enabled; /* Whether if handler enabled or not. */ + regex_t regex; /* Compiled regex. */ }; - extern struct ev_handler handlers[NUM_EVENTS]; extern int process_static_event(struct log_event *ev); extern int init_static_events(void);