From: dhfelix Date: Fri, 1 Feb 2019 22:30:09 +0000 (-0600) Subject: Add parsing for a config file X-Git-Tag: v0.0.2~87^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cd10edde353e245b6feba2901724fd9a6df85a06;p=thirdparty%2FFORT-validator.git Add parsing for a config file First version of it, needs more tweaks, but first I need to finish an arg parser framework. --- diff --git a/configure.ac b/configure.ac index 1528b7f3..d7233669 100644 --- a/configure.ac +++ b/configure.ac @@ -33,6 +33,10 @@ AC_SEARCH_LIBS([d2i_X509_bio], [crypto], [], [AC_MSG_ERROR([unable to find the d2i_X509_bio() function])] ) +AC_SEARCH_LIBS([toml_parse], [toml], [], + [AC_MSG_ERROR([unable to find the toml_parse() function])] +) + # Check dependencies. PKG_CHECK_MODULES([CHECK], [check]) diff --git a/src/Makefile.am b/src/Makefile.am index 1967fb27..d615e8c5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,6 +21,7 @@ rpki_validator_SOURCES += sorted_array.h sorted_array.c rpki_validator_SOURCES += state.h state.c rpki_validator_SOURCES += thread_var.h thread_var.c rpki_validator_SOURCES += uri.h uri.c +rpki_validator_SOURCES += config.h config.c rpki_validator_SOURCES += rsync/rsync.h rsync/rsync.c diff --git a/src/config.c b/src/config.c new file mode 100644 index 00000000..2b8fcd21 --- /dev/null +++ b/src/config.c @@ -0,0 +1,135 @@ +#include "config.h" + +#include +#include +#include +#include + +#include + +#include "file.h" +#include "log.h" +#include "thread_var.h" +#include "uri.h" + + +static void +print_config(struct rpki_config *config) +{ + pr_debug("Program configuration"); + pr_debug_add("{"); + pr_debug("%s: %s", "local_repository", + config->local_repository); + pr_debug("%s: %s", "tal.file", config->tal); + pr_debug("%s: %s", "enable_rsync", + config->enable_rsync ? "true" : "false"); + pr_debug("%s: %s", "tal.shuffle_uris", + config->shuffle_uris ? "true" : "false"); + pr_debug_rm("}"); +} + +static int +handle_tal_table(struct toml_table_t *tal, struct rpki_config *config) +{ + const char *result; + int error, bool_result; + + result = toml_raw_in(tal, "file"); + if (result != 0) { + pr_debug("tal.file raw string %s", result); + error = toml_rtos(result, &config->tal); + if (error) + return pr_err("Bad value in '%s'", "file"); + } + + result = toml_raw_in(tal, "shuffle_uris"); + if (result != 0) { + pr_debug("Boolean %s", result); + + error = toml_rtob(result, &bool_result); + if (error) + return pr_err("Bad value in '%s'", "shuffle_uris"); + config->shuffle_uris = bool_result; + } + + return 0; +} + +static int +toml_to_config(struct toml_table_t *root, struct rpki_config *config) +{ + struct toml_table_t *tal; + const char *result; + int error, bool_result; + + result = toml_raw_in(root, "local_repository"); + if (result != 0) { + error = toml_rtos(result, &config->local_repository); + if (error) + return pr_err("Bad value in '%s'", "local_repository"); + } + + result = toml_raw_in(root, "enable_rsync"); + if (result != 0) { + error = toml_rtob(result, &bool_result); + if (error) + return pr_err("Bad value in '%s'", "enable_rsync"); + config->enable_rsync = bool_result; + } + + tal = toml_table_in(root, "tal"); + if (tal != 0) + error = handle_tal_table(tal, config); + else + return pr_err("Required table '%s' is missing.", "tal"); + + return error; +} + +int +set_config_from_file(char *config_file, struct rpki_config *config) +{ + struct file_contents fc; + struct toml_table_t *root; + struct rpki_uri uri; + char errbuf[200]; + int error; + bool is_config_file; + + /* I think I'm not using this struct correctly but I need it to call + * some functions, so be careful using the struct rpki_uri here. + * Also no needs to be freed. */ + uri.global = config_file; + uri.global_len = strlen(config_file); + uri.local = config_file; + + is_config_file = uri_has_extension(&uri, ".ini"); + is_config_file |= uri_has_extension(&uri, ".toml"); + if (!is_config_file) { + error = pr_err("Invalid Config file extension for file '%s'", + uri.local); + goto end; + } + + error = file_load(&uri, &fc); + if (error) + goto end; /* Error msg already printed. */ + + root = toml_parse((char *) fc.buffer, errbuf, sizeof(errbuf)); + file_free(&fc); + + if (root == NULL) { + error = pr_err("Error while parsing configuration file: %s", + errbuf); + goto end; + } + + error = toml_to_config(root, config); + + toml_free(root); + + print_config(config); + +end: + return error; +} diff --git a/src/config.h b/src/config.h new file mode 100644 index 00000000..6d760af0 --- /dev/null +++ b/src/config.h @@ -0,0 +1,21 @@ +#ifndef SRC_CONFIG_H_ +#define SRC_CONFIG_H_ + +#include + +struct rpki_config { + /* tal file path*/ + char *tal; + /* Local repository path */ + char *local_repository; + /* Disable rsync downloads */ + bool enable_rsync; + /* Shuffle uris in tal */ + bool shuffle_uris; + /* Configuration file path */ + bool flag_config; +}; + +int set_config_from_file(char *, struct rpki_config *); + +#endif /* SRC_CONFIG_H_ */ diff --git a/src/main.c b/src/main.c index 5fddc47e..f0f9f793 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include #include "common.h" +#include "config.h" #include "debug.h" #include "log.h" #include "rpp.h" @@ -13,17 +14,6 @@ #include "object/tal.h" #include "rsync/rsync.h" -struct rpki_config { - /* tal file path*/ - char *tal; - /* Local repository path */ - char *local_repository; - /* Disable rsync downloads */ - bool disable_rsync; - /* Shuffle uris in tal */ - bool shuffle_uris; -}; - /** * Registers the RPKI-specific OIDs in the SSL library. * LibreSSL needs it; not sure about OpenSSL. @@ -109,24 +99,38 @@ end: return error; } +static void +set_default_configuration(struct rpki_config *config) +{ + config->enable_rsync = true; + config->shuffle_uris = false; + config->local_repository = NULL; + config->tal = NULL; +} + static int -handle_args(int argc, char **argv, struct rpki_config *config) +handle_file_config(char *config_file, struct rpki_config *config) +{ + config->flag_config = false; + + return set_config_from_file(config_file, config); +} + +static int +handle_flags_config(int argc, char **argv, struct rpki_config *config) { int opt, error = 0; + config->flag_config = true; + static struct option long_options[] = { - {"tal", no_argument, NULL, 't'}, + {"tal", required_argument, NULL, 't'}, {"local_repository", required_argument, NULL, 'l'}, {"disable_rsync", no_argument, 0, 'r'}, {"shuffle_uris", no_argument, 0, 's'}, {0,0,0,} }; - config->disable_rsync = false; - config->shuffle_uris = false; - config->local_repository = NULL; - config->tal = NULL; - while ((opt = getopt_long(argc, argv, "t:l:rs", long_options, NULL)) != -1) { switch (opt) { @@ -137,7 +141,7 @@ handle_args(int argc, char **argv, struct rpki_config *config) config->local_repository = optarg; break; case 'r': - config->disable_rsync = true; + config->enable_rsync = false; break; case 's': config->shuffle_uris = true; @@ -158,7 +162,7 @@ handle_args(int argc, char **argv, struct rpki_config *config) pr_debug("TAL file : %s", config->tal); pr_debug("Local repository : %s", config->local_repository); - pr_debug("Disable rsync : %s", config->disable_rsync + pr_debug("Enable rsync : %s", config->enable_rsync ? "true" : "false"); pr_debug("shuffle uris : %s", config->shuffle_uris ? "true" : "false"); @@ -166,6 +170,29 @@ handle_args(int argc, char **argv, struct rpki_config *config) return error; } +static int +handle_args(int argc, char **argv, struct rpki_config *config) +{ + char *config_file; + + if (argc == 1) { + return pr_err("Show usage"); /*TODO*/ + } + if (strcasecmp(argv[1], "--configuration_file") == 0) { + if (argc == 2) { + return pr_err("--configuration_file requires a string " + "as argument."); + } + config_file = argv[2]; + argc -= 2; + argv += 2; + return handle_file_config(config_file, config); + } + + return handle_flags_config(argc, argv, config); +} + + int main(int argc, char **argv) { @@ -173,12 +200,13 @@ main(int argc, char **argv) struct tal *tal; int error; + set_default_configuration(&config); error = handle_args(argc, argv, &config); if (error) return error; print_stack_trace_on_segfault(); - error = rsync_init(!config.disable_rsync); + error = rsync_init(config.enable_rsync); if (error) return error;