summaryrefslogtreecommitdiffstats
path: root/sources
diff options
context:
space:
mode:
authorRoberto Hueso Gómez <rhueso@soleta.eu>2020-06-25 10:46:46 +0200
committerOpenGnSys Support Team <soporte-og@soleta.eu>2020-06-26 14:55:23 +0200
commit866b6c5e3766f65719f730ac4dac11b221a6692a (patch)
tree156e77509f6edaf2178e31e01987f367cc6d9c7f /sources
parentc0f5d2c2068968822c2d1d397bf84447ae2e5642 (diff)
#988 add json configuration file
New configuration file at /opt/opengnsys/cfg/ogserver.json
Diffstat (limited to 'sources')
-rw-r--r--sources/cfg.c165
-rw-r--r--sources/cfg.h33
-rw-r--r--sources/main.c12
-rw-r--r--sources/ogAdmServer.c10
-rw-r--r--sources/rest.c1
-rw-r--r--sources/rest.h2
6 files changed, 214 insertions, 9 deletions
diff --git a/sources/cfg.c b/sources/cfg.c
new file mode 100644
index 0000000..05ba80a
--- /dev/null
+++ b/sources/cfg.c
@@ -0,0 +1,165 @@
+/*
+ * Copyright (C) 2020 Soleta Networks <info@soleta.eu>
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU Affero General Public License as published by the
+ * Free Software Foundation, version 3.
+ */
+
+#include "json.h"
+#include "cfg.h"
+#include "ogAdmServer.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <syslog.h>
+
+static int parse_json_rest(struct og_server_cfg *cfg, json_t *element)
+{
+ const char *key;
+ json_t *value;
+
+ json_object_foreach(element, key, value) {
+ if (!strcmp(key, "ip")) {
+ if (og_json_parse_string(value, &cfg->rest.ip) < 0)
+ return -1;
+ } else if (!strcmp(key, "port")) {
+ if (og_json_parse_string(value, &cfg->rest.port) < 0)
+ return -1;
+ } else if (!strcmp(key, "api_token")) {
+ if (og_json_parse_string(value, &cfg->rest.api_token) < 0)
+ return -1;
+ } else {
+ syslog(LOG_ERR, "unknown key `%s' in rest\n", key);
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+static int parse_json_db(struct og_server_cfg *cfg, json_t *element)
+{
+ const char *key;
+ json_t *value;
+
+ json_object_foreach(element, key, value) {
+ if (!strcmp(key, "ip")) {
+ if (og_json_parse_string(value, &cfg->db.ip) < 0)
+ return -1;
+ } else if (!strcmp(key, "user")) {
+ if (og_json_parse_string(value, &cfg->db.user) < 0)
+ return -1;
+ } else if (!strcmp(key, "pass")) {
+ if (og_json_parse_string(value, &cfg->db.pass) < 0)
+ return -1;
+ } else if (!strcmp(key, "name")) {
+ if (og_json_parse_string(value, &cfg->db.name) < 0)
+ return -1;
+ } else {
+ syslog(LOG_ERR, "unknown key `%s' in db\n", key);
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+static int parse_json_wol(struct og_server_cfg *cfg, json_t *element)
+{
+ const char *key;
+ json_t *value;
+
+ json_object_foreach(element, key, value) {
+ if (!strcmp(key, "interface")) {
+ if (og_json_parse_string(value, &cfg->wol.interface) < 0)
+ return -1;
+ } else {
+ syslog(LOG_ERR, "unknown key `%s' in wol\n", key);
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+#define OG_SERVER_CFG_REST (1 << 0)
+#define OG_SERVER_CFG_DB (1 << 1)
+#define OG_SERVER_CFG_WOL (1 << 2)
+
+int parse_json_config(const char *filename, struct og_server_cfg *cfg)
+{
+ json_t *root, *value;
+ uint32_t flags = 0;
+ json_error_t err;
+ const char *key;
+ char buf[4096];
+ int fd, ret;
+
+ fd = open(filename, O_RDONLY);
+ if (!fd) {
+ syslog(LOG_ERR, "Cannot open %s", filename);
+ return -1;
+ }
+
+ ret = read(fd, buf, sizeof(buf));
+ if (ret < 0 || ret == sizeof(buf)) {
+ syslog(LOG_ERR, "Cannot read from %s", filename);
+ return -1;
+ }
+
+ root = json_loads(buf, 0, &err);
+ if (!root) {
+ syslog(LOG_ERR, "Cannot parse malformed json file");
+ return -1;
+ }
+
+ json_object_foreach(root, key, value) {
+ if (!strcmp(key, "rest")) {
+ if (parse_json_rest(cfg, value) < 0)
+ return -1;
+
+ flags |= OG_SERVER_CFG_REST;
+ } else if (!strcmp(key, "wol")) {
+ if (parse_json_wol(cfg, value) < 0)
+ return -1;
+
+ flags |= OG_SERVER_CFG_WOL;
+ } else if (!strcmp(key, "database")) {
+ if (parse_json_db(cfg, value) < 0)
+ return -1;
+
+ flags |= OG_SERVER_CFG_DB;
+ } else {
+ syslog(LOG_ERR, "unknown key `%s' in %s\n",
+ key, filename);
+ ret = -1;
+ }
+ }
+
+ if ((flags & OG_SERVER_CFG_REST) &&
+ (flags & OG_SERVER_CFG_DB) &&
+ (flags & OG_SERVER_CFG_WOL)) {
+ ret = 0;
+ } else {
+ syslog(LOG_ERR, "Missing attributes in json file");
+ ret = -1;
+ }
+
+ json_decref(root);
+
+ return ret;
+}
+
+void from_json_to_legacy(struct og_server_cfg *cfg)
+{
+ snprintf(servidoradm, sizeof(servidoradm), cfg->rest.ip);
+ snprintf(puerto, sizeof(puerto), cfg->rest.port);
+ snprintf(usuario, sizeof(usuario), cfg->db.user);
+ snprintf(pasguor, sizeof(pasguor), cfg->db.pass);
+ snprintf(datasource, sizeof(datasource), cfg->db.ip);
+ snprintf(catalog, sizeof(catalog), cfg->db.name);
+ snprintf(interface, sizeof(interface), cfg->wol.interface);
+ snprintf(auth_token, sizeof(auth_token), cfg->rest.api_token);
+}
diff --git a/sources/cfg.h b/sources/cfg.h
new file mode 100644
index 0000000..cfb37bd
--- /dev/null
+++ b/sources/cfg.h
@@ -0,0 +1,33 @@
+#ifndef _OG_SERVER_CFG_H
+#define _OG_SERVER_CFG_H
+
+struct og_server_cfg {
+ struct {
+ const char *user;
+ const char *pass;
+ const char *ip;
+ const char *name;
+ } db;
+ struct {
+ const char *ip;
+ const char *port;
+ const char *api_token;
+ } rest;
+ struct {
+ const char *interface;
+ } wol;
+};
+
+int parse_json_config(const char *filename, struct og_server_cfg *cfg);
+
+extern char auth_token[4096];
+extern char usuario[4096];
+extern char pasguor[4096];
+extern char catalog[4096];
+extern char datasource[4096];
+extern char interface[4096];
+extern char api_token[4096];
+
+void from_json_to_legacy(struct og_server_cfg *cfg);
+
+#endif
diff --git a/sources/main.c b/sources/main.c
index d0add72..3588e4c 100644
--- a/sources/main.c
+++ b/sources/main.c
@@ -15,11 +15,15 @@
#include "json.h"
#include "schedule.h"
#include "core.h"
+#include "cfg.h"
#include <syslog.h>
+#define OG_SERVER_CFG_DATABASE "/opt/opengnsys/cfg/ogserver.json"
+
int main(int argc, char *argv[])
{
struct ev_io ev_io_server_rest, ev_io_agent_rest;
+ struct og_server_cfg cfg = {};
int i;
og_loop = ev_default_loop(0);
@@ -32,8 +36,12 @@ int main(int argc, char *argv[])
if (!validacionParametros(argc, argv, 1)) // Valida parámetros de ejecución
exit(EXIT_FAILURE);
- if (!tomaConfiguracion(szPathFileCfg)) { // Toma parametros de configuracion
- exit(EXIT_FAILURE);
+ if (!tomaConfiguracion(szPathFileCfg)) {
+ syslog(LOG_INFO, "falling back to %s\n", OG_SERVER_CFG_DATABASE);
+ if (parse_json_config(OG_SERVER_CFG_DATABASE, &cfg) < 0)
+ exit(EXIT_FAILURE);
+
+ from_json_to_legacy(&cfg);
}
for (i = 0; i < MAXIMOS_CLIENTES; i++) {
diff --git a/sources/ogAdmServer.c b/sources/ogAdmServer.c
index c9b0b70..436c3b1 100644
--- a/sources/ogAdmServer.c
+++ b/sources/ogAdmServer.c
@@ -23,11 +23,11 @@
#include <jansson.h>
#include <time.h>
-static char usuario[LONPRM]; // Usuario de acceso a la base de datos
-static char pasguor[LONPRM]; // Password del usuario
-static char datasource[LONPRM]; // Dirección IP del gestor de base de datos
-static char catalog[LONPRM]; // Nombre de la base de datos
-static char interface[LONPRM]; // Interface name
+char usuario[LONPRM]; // Usuario de acceso a la base de datos
+char pasguor[LONPRM]; // Password del usuario
+char datasource[LONPRM]; // Dirección IP del gestor de base de datos
+char catalog[LONPRM]; // Nombre de la base de datos
+char interface[LONPRM]; // Interface name
char auth_token[LONPRM]; // API token
struct og_dbi_config dbi_config = {
diff --git a/sources/rest.c b/sources/rest.c
index b39e35a..097cbf8 100644
--- a/sources/rest.c
+++ b/sources/rest.c
@@ -11,6 +11,7 @@
#include "utils.h"
#include "list.h"
#include "rest.h"
+#include "cfg.h"
#include "schedule.h"
#include <ev.h>
#include <syslog.h>
diff --git a/sources/rest.h b/sources/rest.h
index c0c26d5..4f2347f 100644
--- a/sources/rest.h
+++ b/sources/rest.h
@@ -94,6 +94,4 @@ struct og_cmd {
const struct og_cmd *og_cmd_find(const char *client_ip);
void og_cmd_free(const struct og_cmd *cmd);
-extern char auth_token[LONPRM];
-
#endif