alertik/notifiers.c

390 lines
10 KiB
C
Raw Normal View History

2024-07-16 03:16:27 +02:00
/*
* Alertik: a tiny 'syslog' server & notification tool for Mikrotik routers.
* This is free and unencumbered software released into the public domain.
*/
#include <stdio.h>
2024-07-17 03:53:12 +02:00
#include <stdlib.h>
#include <time.h>
2024-07-16 03:16:27 +02:00
#include <curl/curl.h>
2024-07-17 03:53:12 +02:00
#include "log.h"
2024-07-16 03:16:27 +02:00
#include "notifiers.h"
#include "str.h"
2024-07-16 03:16:27 +02:00
/*
* Notification handling/notifiers
*/
2024-07-28 23:00:36 +02:00
struct webhook_data {
char *webhook_url;
const char *env_var;
};
/* EPOCH in secs of last sent notification. */
static time_t time_last_sent_notify;
2024-07-16 03:16:27 +02:00
/* Just to omit the print to stdout. */
size_t libcurl_noop_cb(void *ptr, size_t size, size_t nmemb, void *data) {
((void)ptr);
((void)data);
return size * nmemb;
}
2024-07-26 02:28:18 +02:00
/**
* @brief Just updates the time (Epoch) of the last sent
* notify.
*/
void update_notify_last_sent(void) {
time_last_sent_notify = time(NULL);
}
2024-07-26 02:28:18 +02:00
/**
* @brief Checks if the current time is within or not
* the minimal threshold to send a nofication.
*
* @return Returns 1 if within the range (can send nofications),
* 0 otherwise.
*/
int is_within_notify_threshold(void) {
return (time(NULL) - time_last_sent_notify) > LAST_SENT_THRESHOLD_SECS;
}
2024-07-26 03:40:19 +02:00
/**
* @brief Initializes and configures the CURL handle for sending a request.
*
* @param hnd CURL handle.
* @param url Request URL.
2024-07-27 07:49:08 +02:00
* @return Returns 0.
2024-07-26 03:40:19 +02:00
*/
2024-07-27 07:49:08 +02:00
static int setopts_get_curl(CURL *hnd, const char *url)
2024-07-26 03:40:19 +02:00
{
curl_easy_setopt(hnd, CURLOPT_URL, url);
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, CURL_USER_AGENT);
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 3L);
curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, libcurl_noop_cb);
#ifdef CURL_VERBOSE
curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
#endif
#ifndef VALIDATE_CERTS
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
2024-07-27 07:49:08 +02:00
return 0;
2024-07-26 03:40:19 +02:00
}
/**
* @brief Cleanup all resources used by libcurl, including the handler,
* curl_slist and escape'd chars.
2024-07-26 03:40:19 +02:00
*
* @param hnd curl handler
* @param escape Escape string if any (leave NULL if there's none).
* @param slist String list if any (leave NULL if there's none).
2024-07-26 03:40:19 +02:00
*/
2024-07-27 07:49:08 +02:00
static void do_curl_cleanup(CURL *hnd, char *escape, struct curl_slist *slist)
2024-07-26 03:40:19 +02:00
{
curl_free(escape);
2024-07-27 07:49:08 +02:00
curl_slist_free_all(slist);
2024-07-26 03:40:19 +02:00
if (hnd)
curl_easy_cleanup(hnd);
}
/**
* @brief Finally sends a curl request, check its return code
* and then cleanup the resources allocated.
2024-07-26 03:40:19 +02:00
*
* @param hnd curl handler
* @param escape Escape string if any (leave NULL if there's none).
* @param slist String list if any (leave NULL if there's none).
*
* @return Returns CURLE_OK if success, !CURLE_OK if error.
2024-07-26 03:40:19 +02:00
*/
2024-07-27 07:49:08 +02:00
static CURLcode do_curl(CURL *hnd, char *escape, struct curl_slist *slist)
2024-07-26 03:40:19 +02:00
{
long response_code = 0;
CURLcode ret_curl = !CURLE_OK;
2024-07-26 03:40:19 +02:00
#ifndef DISABLE_NOTIFICATIONS
ret_curl = curl_easy_perform(hnd);
if (ret_curl != CURLE_OK) {
log_msg("> Unable to send request!\n");
goto error;
}
else {
curl_easy_getinfo(hnd, CURLINFO_RESPONSE_CODE, &response_code);
log_msg("> Done!\n", response_code);
if (response_code != 200) {
log_msg("(Info: Response code != 200 (%ld), your message might "
"not be correctly sent!)\n", response_code);
}
2024-07-26 03:40:19 +02:00
}
#endif
ret_curl = CURLE_OK;
error:
2024-07-27 07:49:08 +02:00
do_curl_cleanup(hnd, escape, slist);
2024-07-26 03:40:19 +02:00
return ret_curl;
}
/**
* @brief Initializes and configures the CURL handle for sending a POST
* request with a JSON payload.
*
2024-07-27 07:49:08 +02:00
* @param hnd CURL handle.
* @param url Request URL.
* @param json_payload Payload data in JSON format.
2024-07-26 03:40:19 +02:00
*
2024-07-27 07:49:08 +02:00
* @return Returns 0 if successful, 1 otherwise.
2024-07-26 03:40:19 +02:00
*/
2024-07-27 07:49:08 +02:00
static int setopts_post_json_curl(CURL *hnd, const char *url,
const char *json_payload, struct curl_slist **slist)
2024-07-26 03:40:19 +02:00
{
2024-07-27 07:49:08 +02:00
struct curl_slist *s = *slist;
s = NULL;
s = curl_slist_append(s, "Content-Type: application/json");
s = curl_slist_append(s, "Accept: application/json");
if (!s) {
*slist = s;
return 1;
}
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, s);
curl_easy_setopt(hnd, CURLOPT_URL, url);
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, CURL_USER_AGENT);
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 3L);
curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, libcurl_noop_cb);
#ifdef CURL_VERBOSE
curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
#endif
#ifndef VALIDATE_CERTS
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, json_payload);
*slist = s;
return 0;
2024-07-26 03:40:19 +02:00
}
2024-07-27 07:49:08 +02:00
/**
* @brief Sends a generic webhook POST request with JSON payload in the
* format {"text": "text here"}.
*
* @param url Target webhook URL.
* @param text Text to be sent in the json payload.
2024-07-27 07:49:08 +02:00
*
* @return Returns CURLE_OK if success, 1 if error.
2024-07-27 07:49:08 +02:00
*/
static int send_generic_webhook(const char *url, const char *text)
{
CURL *hnd = NULL;
struct curl_slist *s = NULL;
struct str_ab payload_data;
const char *t;
2024-07-27 07:49:08 +02:00
if (!(hnd = curl_easy_init())) {
log_msg("Failed to initialize libcurl!\n");
return 1;
}
ab_init(&payload_data);
ab_append_str(&payload_data, "{\"text\":\"", 9);
/* Append the payload data text while escaping double
* quotes.
*/
for (t = text; *t != '\0'; t++) {
if (*t != '"') {
if (ab_append_chr(&payload_data, *t) < 0)
return 1;
}
else {
if (ab_append_str(&payload_data, "\\\"", 2) < 0)
return 1;
}
}
2024-07-27 07:49:08 +02:00
/* End the string. */
if (ab_append_str(&payload_data, "\"}", 2) < 0)
return 1;
2024-07-27 07:49:08 +02:00
if (setopts_post_json_curl(hnd, url, payload_data.buff, &s))
2024-07-27 07:49:08 +02:00
return 1;
log_msg("> Sending notification!\n");
return do_curl(hnd, NULL, s);
}
2024-07-28 19:39:02 +02:00
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////// TELEGRAM /////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
2024-07-26 02:28:18 +02:00
2024-07-16 03:16:27 +02:00
/* Telegram & request settings. */
static char *telegram_bot_token;
static char *telegram_chat_id;
2024-07-28 23:00:36 +02:00
void setup_telegram(struct notifier *self)
2024-07-16 03:16:27 +02:00
{
static int setup = 0;
if (setup)
return;
2024-07-28 23:00:36 +02:00
((void)self);
2024-07-16 03:16:27 +02:00
telegram_bot_token = getenv("TELEGRAM_BOT_TOKEN");
telegram_chat_id = getenv("TELEGRAM_CHAT_ID");
if (!telegram_bot_token || !telegram_chat_id) {
panic(
"Unable to find env vars, please check if you have all of the "
"following set:\n"
"- TELEGRAM_BOT_TOKEN\n"
"- TELEGRAM_CHAT_ID\n"
);
}
setup = 1;
2024-07-16 03:16:27 +02:00
}
2024-07-28 23:00:36 +02:00
static int send_telegram_notification(const struct notifier *self, const char *msg)
2024-07-16 03:16:27 +02:00
{
struct str_ab full_request_url;
2024-07-16 03:16:27 +02:00
char *escaped_msg = NULL;
2024-07-26 03:40:19 +02:00
CURL *hnd = NULL;
int ret;
2024-07-16 03:16:27 +02:00
2024-07-28 23:00:36 +02:00
((void)self);
2024-07-26 03:40:19 +02:00
if (!(hnd = curl_easy_init())) {
log_msg("Failed to initialize libcurl!\n");
return -1;
2024-07-16 03:16:27 +02:00
}
escaped_msg = curl_easy_escape(hnd, msg, 0);
if (!escaped_msg) {
log_msg("> Unable to escape notification message...\n");
2024-07-27 07:49:08 +02:00
do_curl_cleanup(hnd, escaped_msg, NULL);
2024-07-16 03:16:27 +02:00
}
ab_init(&full_request_url);
ret = ab_append_fmt(&full_request_url,
2024-07-16 03:16:27 +02:00
"https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s",
telegram_bot_token, telegram_chat_id, escaped_msg);
if (ret)
return -1;
setopts_get_curl(hnd, full_request_url.buff);
2024-07-26 03:40:19 +02:00
log_msg("> Sending notification!\n");
2024-07-27 07:49:08 +02:00
return do_curl(hnd, escaped_msg, NULL);
}
2024-07-28 19:39:02 +02:00
///////////////////////////////////////////////////////////////////////////////
2024-07-28 23:00:36 +02:00
///////////////////////////////// GENERIC /////////////////////////////////////
2024-07-28 19:39:02 +02:00
///////////////////////////////////////////////////////////////////////////////
2024-07-27 07:49:08 +02:00
2024-07-28 23:00:36 +02:00
void setup_generic_webhook(struct notifier *self)
2024-07-27 07:49:08 +02:00
{
2024-07-28 23:00:36 +02:00
struct webhook_data *data = self->data;
2024-07-28 19:39:02 +02:00
2024-07-28 23:00:36 +02:00
if (data->webhook_url)
2024-07-28 19:39:02 +02:00
return;
2024-07-28 23:00:36 +02:00
data->webhook_url = getenv(data->env_var);
if (!data->webhook_url) {
panic("Unable to find env vars, please check if you have set the %s!!\n",
data->env_var);
2024-07-28 19:39:02 +02:00
}
}
2024-07-28 23:00:36 +02:00
static int send_generic_webhook_notification(
const struct notifier *self, const char *msg)
{
struct webhook_data *data = self->data;
return send_generic_webhook(data->webhook_url, msg);
2024-07-28 19:39:02 +02:00
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////// DISCORD /////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/* Discord in Slack-compatible mode. */
2024-07-28 23:00:36 +02:00
static int send_discord_notification(
const struct notifier *self, const char *msg)
{
struct webhook_data *data = self->data;
2024-07-28 19:39:02 +02:00
struct str_ab url;
ab_init(&url);
2024-07-28 23:00:36 +02:00
if (ab_append_fmt(&url, "%s/slack", data->webhook_url) < 0)
2024-07-28 19:39:02 +02:00
return 1;
return send_generic_webhook(url.buff, msg);
}
2024-07-27 07:49:08 +02:00
2024-07-16 03:16:27 +02:00
////////////////////////////////// END ////////////////////////////////////////
2024-07-26 03:40:19 +02:00
///////////////////////////////////////////////////////////////////////////////
2024-07-16 03:16:27 +02:00
const char *const notifiers_str[] = {
2024-07-28 19:39:02 +02:00
"Telegram", "Slack", "Teams", "Discord",
2024-07-26 03:40:19 +02:00
"Generic1", "Generic2", "Generic3", "Generic4"
2024-07-16 03:16:27 +02:00
};
struct notifier notifiers[] = {
/* Telegram. */
{
2024-07-28 23:00:36 +02:00
.setup = setup_telegram,
.send_notification = send_telegram_notification,
2024-07-27 07:49:08 +02:00
},
/* Slack. */
{
2024-07-28 23:00:36 +02:00
.setup = setup_generic_webhook,
.send_notification = send_generic_webhook_notification,
.data = &(struct webhook_data)
{.env_var = "SLACK_WEBHOOK_URL"},
2024-07-28 19:39:02 +02:00
},
/* Teams. */
{
2024-07-28 23:00:36 +02:00
.setup = setup_generic_webhook,
.send_notification = send_generic_webhook_notification,
.data = &(struct webhook_data)
{.env_var = "TEAMS_WEBHOOK_URL"},
2024-07-28 19:39:02 +02:00
},
2024-07-28 23:00:36 +02:00
/*
* Discord:
* Since Discord doesn't follow like the others, we need
* to slightly change the URL before proceeding, so this
* is why its function is not generic!.
*/
2024-07-28 19:39:02 +02:00
{
2024-07-28 23:00:36 +02:00
.setup = setup_generic_webhook,
.send_notification = send_discord_notification,
.data = &(struct webhook_data)
{.env_var = "DISCORD_WEBHOOK_URL"},
2024-07-28 23:14:17 +02:00
},
/* Generic webhook events: 1--4 */
{
.setup = setup_generic_webhook,
.send_notification = send_generic_webhook_notification,
.data = &(struct webhook_data)
{.env_var = "GENERIC1_WEBHOOK_URL"},
},
{
.setup = setup_generic_webhook,
.send_notification = send_generic_webhook_notification,
.data = &(struct webhook_data)
{.env_var = "GENERIC2_WEBHOOK_URL"},
},
{
.setup = setup_generic_webhook,
.send_notification = send_generic_webhook_notification,
.data = &(struct webhook_data)
{.env_var = "GENERIC3_WEBHOOK_URL"},
},
{
.setup = setup_generic_webhook,
.send_notification = send_generic_webhook_notification,
.data = &(struct webhook_data)
{.env_var = "GENERIC4_WEBHOOK_URL"},
},
2024-07-16 03:16:27 +02:00
};