From 4a830b3d444af976d81ccbba4711a9e8664ec93b Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Tue, 6 Feb 2018 18:07:05 +0100 Subject: [PATCH] TT#32302 Implement config file parsing We need a new configuration file for multiple reasons. For security purposes, to avoid passing passwords as part of the program arguments, which are publicly visible. And to be able to use systemd services files, because there's at least the maintenance configuration that's set conditionally, and that's not easily supported in systemd service files. Refactor the code to make it easier to add a simple config file parser, based on a key or key=value per line, with support for comments starting with # on the first column. In addition this also gives us long command-line options for the existing short options. Change-Id: I611f262847fa2ec4e3c569ca27e01b691e585fcc --- config.c | 388 ++++++++++++++++++++++++++++++++++++++++------------- config.h | 2 +- mediator.c | 2 +- mediator.h | 1 + 4 files changed, 297 insertions(+), 96 deletions(-) diff --git a/config.c b/config.c index 8e6138a..5930a7d 100644 --- a/config.c +++ b/config.c @@ -1,8 +1,10 @@ +#include #include #include "config.h" #include "mediator.h" +char *config_file_path; unsigned int config_interval = MEDIATOR_DEFAULT_INTERVAL; uint8_t config_dumpcdr = MEDIATOR_DEFAULT_DUMPCDR; uint8_t config_daemonize = MEDIATOR_DEFAULT_DAEMONIZE; @@ -36,39 +38,108 @@ med_stats_period_t config_stats_period = MEDIATOR_DEFAULT_STATSPERIOD; int config_maintenance = 0; int strict_leg_tokens = 0; +enum config_option { + OPT_CONFIGFILE = 'c', + OPT_DAEMONIZE = 'd', + OPT_PIDFILE = 'D', + OPT_SYSLOG = 'l', + OPT_INTERVAL = 'i', + OPT_MED_HOST = 'h', + OPT_MED_PORT = 'o', + OPT_MED_USER = 'u', + OPT_MED_PASS = 'p', + OPT_MED_DB = 'b', + OPT_CDR_HOST = 'H', + OPT_CDR_PORT = 'O', + OPT_CDR_USER = 'U', + OPT_CDR_PASS = 'P', + OPT_CDR_DB = 'B', + OPT_PROV_HOST = 'S', + OPT_PROV_PORT = 'T', + OPT_PROV_USER = 'R', + OPT_PROV_PASS = 'A', + OPT_PROV_DB = 'N', + OPT_STATS_HOST = 'Z', + OPT_STATS_PORT = 'z', + OPT_STATS_USER = 'W', + OPT_STATS_PASS = 'w', + OPT_STATS_DB = 'X', + OPT_STATS_PERIOD = 'x', + OPT_MAINTENANCE = 'm', + OPT_LEG_TOKENS = 's', +}; + +static const char options[] = "?c:D:i:dlh:u:p:b:o:H:U:P:B:O:S:T:R:A:N:Z:z:W:w:X:x:ms"; + +struct option long_options[] = { + { "configfile", required_argument, NULL, OPT_CONFIGFILE }, + { "pidfile", required_argument, NULL, OPT_PIDFILE }, + { "daemonize", no_argument, NULL, OPT_DAEMONIZE }, + { "syslog", no_argument, NULL, OPT_SYSLOG }, + { "interval", required_argument, NULL, OPT_INTERVAL }, + { "med-host", required_argument, NULL, OPT_MED_HOST }, + { "med-port", required_argument, NULL, OPT_MED_PORT }, + { "med-user", required_argument, NULL, OPT_MED_USER }, + { "med-pass", required_argument, NULL, OPT_MED_PASS }, + { "med-db", required_argument, NULL, OPT_MED_DB }, + { "cdr-host", required_argument, NULL, OPT_CDR_HOST }, + { "cdr-port", required_argument, NULL, OPT_CDR_PORT }, + { "cdr-user", required_argument, NULL, OPT_CDR_USER }, + { "cdr-pass", required_argument, NULL, OPT_CDR_PASS }, + { "cdr-db", required_argument, NULL, OPT_CDR_DB }, + { "prov-host", required_argument, NULL, OPT_PROV_HOST }, + { "prov-port", required_argument, NULL, OPT_PROV_PORT }, + { "prov-user", required_argument, NULL, OPT_PROV_USER }, + { "prov-pass", required_argument, NULL, OPT_PROV_PASS }, + { "prov-db", required_argument, NULL, OPT_PROV_DB }, + { "stats-host", required_argument, NULL, OPT_STATS_HOST }, + { "stats-port", required_argument, NULL, OPT_STATS_PORT }, + { "stats-user", required_argument, NULL, OPT_STATS_USER }, + { "stats-pass", required_argument, NULL, OPT_STATS_PASS }, + { "stats-db", required_argument, NULL, OPT_STATS_DB }, + { "stats-period", required_argument, NULL, OPT_STATS_PERIOD }, + { "maintenance", no_argument, NULL, OPT_MAINTENANCE }, + { "leg-tokens", no_argument, NULL, OPT_LEG_TOKENS }, + { NULL, 0, NULL, 0 }, +}; + static void config_help(const char *self, int rc) { - printf("mediator %s - Creates call detail records from OpenSER accounting.\n" \ - "Usage: %s [-?] [-d] [-D pidpath]\n" \ - " -D\tThe path of the PID file (default = '%s').\n" \ - " -d\tEnables daemonization of the process (default = disabled).\n" \ - " -l\tEnables dumping of CDRs to syslog (default = disabled).\n" \ - " -i\tThe creation interval (default = %d).\n" \ - " -h\tThe ACC db host (default = '%s').\n" \ - " -o\tThe ACC db port (default = '%d').\n" \ - " -u\tThe ACC db user (default = '%s').\n" \ - " -p\tThe ACC db pass (default = '%s').\n" \ - " -b\tThe ACC db name (default = '%s').\n" \ - " -H\tThe CDR db host (default = '%s').\n" \ - " -O\tThe CDR db port (default = '%d').\n" \ - " -U\tThe CDR db user (default = '%s').\n" \ - " -P\tThe CDR db pass (default = '%s').\n" \ - " -B\tThe CDR db name (default = '%s').\n" \ - " -S\tThe prov db host (default = '%s').\n" \ - " -T\tThe prov db port (default = '%d').\n" \ - " -R\tThe prov db user (default = '%s').\n" \ - " -A\tThe prov db pass (default = '%s').\n" \ - " -N\tThe prov db name (default = '%s').\n" \ - " -Z\tThe stats db host (default = '%s').\n" \ - " -z\tThe stats db port (default = '%d').\n" \ - " -W\tThe stats db user (default = '%s').\n" \ - " -w\tThe stats db pass (default = '%s').\n" \ - " -X\tThe stats db name (default = '%s').\n" \ - " -x\tThe stats db period (default = '%d', 1=hour, 2=day, 3=month).\n" \ - " -m\tMaintenance mode (do nothing, just sleep).\n" \ - " -s\tStrict acc fields (move to trash otherwise).\n" \ - " -?\tDisplays this message.\n", - MEDIATOR_VERSION, self, + printf( +"mediator %s - Creates call detail records from OpenSER accounting.\n" \ +"Usage: %s [