From bdbc16a566a06c008868cd32547be8afb7a0982c Mon Sep 17 00:00:00 2001 From: Wilco Baan Hofman Date: Sun, 5 Aug 2012 02:41:46 +0200 Subject: [PATCH] Add jsonbot client. Return configuration as const. --- config.c | 28 +++++++++++++++++++- config.h | 7 ++++- database.c | 2 +- includes.h | 1 + jsonbot.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ jsonbot.h | 21 +++++++++++++++ secipd.c | 4 +-- siahsd.c | 21 +++++++-------- siahsd.conf | 7 +++++ status.c | 2 +- wscript | 7 ++--- 11 files changed, 155 insertions(+), 19 deletions(-) create mode 100644 jsonbot.c create mode 100644 jsonbot.h diff --git a/config.c b/config.c index e5eb0aa..ef31489 100644 --- a/config.c +++ b/config.c @@ -23,7 +23,7 @@ const char *process_name = NULL; struct rsa_public_key *public_key = NULL; struct rsa_private_key *private_key = NULL; -configuration *get_conf(void) { +const configuration *get_conf(void) { return conf; } @@ -111,6 +111,31 @@ STATUS read_configuration_file(TALLOC_CTX *mem_ctx) fprintf(stderr, "No pid file supplied in the configuration.\n"); return ST_CONFIGURATION_ERROR; } + conf->jsonbot_address = g_key_file_get_string(keyfile, "jsonbot", "address", &error); + if (error) { + fprintf(stderr, "No jsonbot address supplied in the configuration.\n"); + return ST_CONFIGURATION_ERROR; + } + conf->jsonbot_port = g_key_file_get_integer(keyfile, "jsonbot", "port", &error); + if (error) { + fprintf(stderr, "No jsonbot port supplied in the configuration.\n"); + return ST_CONFIGURATION_ERROR; + } + conf->jsonbot_aeskey = g_key_file_get_string(keyfile, "jsonbot", "aes key", &error); + if (error) { + fprintf(stderr, "No jsonbot aes key supplied in the configuration.\n"); + return ST_CONFIGURATION_ERROR; + } + conf->jsonbot_password = g_key_file_get_string(keyfile, "jsonbot", "password", &error); + if (error) { + fprintf(stderr, "No jsonbot password supplied in the configuration.\n"); + return ST_CONFIGURATION_ERROR; + } + conf->jsonbot_privmsg_to = g_key_file_get_string(keyfile, "jsonbot", "privmsg to", &error); + if (error) { + fprintf(stderr, "No jsonbot privsmg to supplied in the configuration.\n"); + return ST_CONFIGURATION_ERROR; + } conf->foreground = g_key_file_get_boolean(keyfile, "siahsd", "foreground", &error); if (error) { conf->foreground = false; @@ -120,6 +145,7 @@ STATUS read_configuration_file(TALLOC_CTX *mem_ctx) conf->secip_port = g_key_file_get_integer(keyfile, "secip", "port", &error); conf->rsa_key_file = g_key_file_get_string(keyfile, "secip", "rsa key file", &error); + return ST_OK; } diff --git a/config.h b/config.h index 0a6e070..9e3e65b 100644 --- a/config.h +++ b/config.h @@ -31,10 +31,15 @@ typedef struct { char *pid_file; gint secip_port; char *rsa_key_file; + char *jsonbot_address; + gint jsonbot_port; + char *jsonbot_aeskey; + char *jsonbot_password; + char *jsonbot_privmsg_to; } configuration; -configuration *get_conf(void); +const configuration *get_conf(void); STATUS get_rsa_keys(struct rsa_public_key **pub, struct rsa_private_key **priv); STATUS set_rsa_keys(struct rsa_public_key *pub, struct rsa_private_key *priv); diff --git a/database.c b/database.c index 942f994..ca90b92 100644 --- a/database.c +++ b/database.c @@ -75,7 +75,7 @@ STATUS log_event_to_database(TALLOC_CTX *mem_ctx, dbi_conn conn, const char *pro STATUS connect_to_database(dbi_conn *conn) { - configuration *conf = get_conf(); + const configuration *conf = get_conf(); DEBUG(1, "Connecting to %s database %s at %s as user %s", conf->database_driver, conf->database_name, conf->database_host, conf->database_username); diff --git a/includes.h b/includes.h index 305da13..d9ed54f 100644 --- a/includes.h +++ b/includes.h @@ -44,3 +44,4 @@ #include "sia.h" #include "config.h" #include "database.h" +#include "jsonbot.h" diff --git a/jsonbot.c b/jsonbot.c new file mode 100644 index 0000000..1833688 --- /dev/null +++ b/jsonbot.c @@ -0,0 +1,74 @@ +/* + JSONBot event generator + Alarm Monitoring Service + Copyright (C) Wilco Baan Hofman 2012 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "includes.h" +#include + + +STATUS jsonbot_notify(TALLOC_CTX *mem_ctx, dbi_conn conn, const char *prom, const char *code, const char *description) +{ + int sockfd; + struct sockaddr_in servaddr; + const configuration *conf; + char *outtext; + struct aes_ctx aes; + uint8_t *msgbuf, *msgbuf_crypted; + uint16_t msglen; + + conf = get_conf(); + + + aes_set_encrypt_key(&aes, strlen(conf->jsonbot_aeskey), (uint8_t *) conf->jsonbot_aeskey); + + outtext = talloc_asprintf(mem_ctx, "%s %s Event at prom %s: %s: %s: %s -- %s\n", + conf->jsonbot_password, conf->jsonbot_privmsg_to, prom, description, code, + sia_code_str(code), sia_code_desc(code)); + + + msglen = (strlen(outtext) + 1) + (16 - ((strlen(outtext) + 1) % 16)); + + msgbuf = talloc_zero_array(mem_ctx, uint8_t, msglen + 1); + msgbuf_crypted = talloc_array(mem_ctx, uint8_t, msglen + 1); + + memcpy(msgbuf, outtext, strlen(outtext)); + + aes_encrypt(&aes, msglen, msgbuf_crypted, msgbuf); + + /* + * Set up the outgoing UDP socket + */ + sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (sockfd < 0) { + DEBUG(0, "Failed to set up UDP socket for jsonbot"); + return ST_GENERAL_FAILURE; + } + + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(conf->jsonbot_port); + servaddr.sin_addr.s_addr = inet_addr(conf->jsonbot_address); + + if (sendto(sockfd, msgbuf_crypted, msglen, 0, + (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) { + DEBUG(0, "Failed to send UDP packet to %s:%d", conf->jsonbot_address, conf->jsonbot_port); + return ST_GENERAL_FAILURE; + } + + return ST_OK; +} diff --git a/jsonbot.h b/jsonbot.h new file mode 100644 index 0000000..f1df4c0 --- /dev/null +++ b/jsonbot.h @@ -0,0 +1,21 @@ +/* + JSONBot event generator + Alarm Monitoring Service + Copyright (C) Wilco Baan Hofman 2012 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + + +STATUS jsonbot_notify(TALLOC_CTX *mem_ctx, dbi_conn conn, const char *prom, const char *code, const char *description); diff --git a/secipd.c b/secipd.c index 59182c5..236fe1a 100644 --- a/secipd.c +++ b/secipd.c @@ -24,7 +24,7 @@ static int read_rsa_keys(void) { uint8_t buf[1024]; struct rsa_private_key *priv; struct rsa_public_key *pub; - configuration *conf = get_conf(); + const configuration *conf = get_conf(); uint8_t *buffer = NULL; size_t n, size=0; @@ -120,7 +120,7 @@ int main (int argc, char **argv) { STATUS rv; FILE *pidfile; pid_t pid; - configuration *conf; + const configuration *conf; set_process_name(argv[0]); diff --git a/siahsd.c b/siahsd.c index 4e6da14..ec28e90 100644 --- a/siahsd.c +++ b/siahsd.c @@ -58,14 +58,6 @@ STATUS parse_message(TALLOC_CTX *mem_ctx, dbi_conn conn, struct siahs_packet *pk /* The remaining ptr contains the human readable description string */ - - /* Ignore alive! messages */ - if (strcmp(code, "alive!") == 0) { - DEBUG(2, "Got keepalive packet from prom %x", prom); - /* FIXME We must update some keepalive status somewhere to generate offline messages */ - return ST_OK; - } - /* Assert that string prom is identical to hex representation of pkt->prom */ pkt_prom = talloc_asprintf(message, "%04x", pkt->prom); @@ -75,7 +67,16 @@ STATUS parse_message(TALLOC_CTX *mem_ctx, dbi_conn conn, struct siahs_packet *pk return ST_ASSERTION_FAILED; } + + /* Ignore alive! messages */ + if (strcmp(code, "alive!") == 0) { + DEBUG(2, "Got keepalive packet from prom %s", prom); + /* FIXME We must update some keepalive status somewhere to generate offline messages */ + return ST_OK; + } + log_event_to_database(message, conn, prom, code, ptr); + jsonbot_notify(message, conn, prom, code, ptr); talloc_free(message); @@ -159,7 +160,7 @@ int main(int argc, char **argv) { STATUS rv; FILE *pidfile; pid_t pid; - configuration *conf; + const configuration *conf; set_process_name(argv[0]); @@ -236,7 +237,7 @@ int main(int argc, char **argv) { NO_MEM_RETURN(pkt); - n = recvfrom(sock, &buf, 1024, 0, (struct sockaddr *) &from, &fromlen); + n = recvfrom(sock, buf, 1024, 0, (struct sockaddr *) &from, &fromlen); if (n < 0) { DEBUG( 0, "Error when storing packet in buffer!"); talloc_free(pkt); diff --git a/siahsd.conf b/siahsd.conf index afe2ace..73a95f8 100644 --- a/siahsd.conf +++ b/siahsd.conf @@ -14,3 +14,10 @@ password = [siahs] port = 4000 + +[jsonbot] +address = 127.0.0.1 +port = 9001 # OVER 9000! +aeskey = +password = +privmsg to = #bitlair diff --git a/status.c b/status.c index d8ad571..5b3c05f 100644 --- a/status.c +++ b/status.c @@ -26,7 +26,7 @@ STATUS debug(int loglevel, const char *location, const char *function, ...) struct tm *timeinfo; size_t s; FILE *logfile; - configuration *conf = get_conf(); + const configuration *conf = get_conf(); if (loglevel > conf->log_level) { return ST_OK; diff --git a/wscript b/wscript index 6f91dc0..62b66f4 100644 --- a/wscript +++ b/wscript @@ -28,7 +28,7 @@ def process_idl(self, node): def dist(ctx): ctx.base_name = 'siahsd' - ctx.algo = 'bz2' + ctx.algo = 'tar.bz2' ctx.excl = ' **/.waf-1* **/*~ **/*.o **/*.swp **/.lock-w*' ctx.files = ctx.path.ant_glob('**/wscript') @@ -92,16 +92,17 @@ def build(bld): bld.stlib(source="status.c", target="status", use='glib-2.0') bld.stlib(source="config.c", target="config", use='glib-2.0') bld.stlib(source="sia.c", target="sia", use='glib-2.0') + bld.stlib(source="jsonbot.c", target="jsonbot", use='glib-2.0') bld.program( source = 'siahsd.c', target = 'siahsd', - use = [ 'database', 'config', 'status', 'sia', 'dbi', 'talloc','glib-2.0' ]) + use = [ 'database', 'config', 'status', 'sia', 'jsonbot', 'dbi', 'talloc','glib-2.0', 'nettle' ]) bld.program( source = 'secip.idl secipd.c crc16.c', target = 'secipd', - use = [ 'database', 'config', 'status', 'sia', 'dbi', 'talloc','glib-2.0', 'nettle', 'ndr' ]) + use = [ 'database', 'config', 'status', 'sia', 'jsonbot', 'dbi', 'talloc','glib-2.0', 'nettle', 'ndr' ]) pass def clean(ctx):