Add jsonbot client. Return configuration as const.

This commit is contained in:
Wilco Baan Hofman 2012-08-05 02:41:46 +02:00
parent 172d47b6da
commit bdbc16a566
11 changed files with 155 additions and 19 deletions

View file

@ -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;
}

View file

@ -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);

View file

@ -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);

View file

@ -44,3 +44,4 @@
#include "sia.h"
#include "config.h"
#include "database.h"
#include "jsonbot.h"

74
jsonbot.c Normal file
View file

@ -0,0 +1,74 @@
/*
JSONBot event generator
Alarm Monitoring Service
Copyright (C) Wilco Baan Hofman <wilco@baanhofman.nl> 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 <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include <nettle/aes.h>
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;
}

21
jsonbot.h Normal file
View file

@ -0,0 +1,21 @@
/*
JSONBot event generator
Alarm Monitoring Service
Copyright (C) Wilco Baan Hofman <wilco@baanhofman.nl> 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 <http://www.gnu.org/licenses/>.
*/
STATUS jsonbot_notify(TALLOC_CTX *mem_ctx, dbi_conn conn, const char *prom, const char *code, const char *description);

View file

@ -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]);

View file

@ -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);

View file

@ -14,3 +14,10 @@ password =
[siahs]
port = 4000
[jsonbot]
address = 127.0.0.1
port = 9001 # OVER 9000!
aeskey =
password =
privmsg to = #bitlair

View file

@ -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;

View file

@ -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):