]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Add parsing for a config file
authordhfelix <daniel.hdz.felix@hotmail.com>
Fri, 1 Feb 2019 22:30:09 +0000 (16:30 -0600)
committerdhfelix <daniel.hdz.felix@hotmail.com>
Fri, 1 Feb 2019 22:30:09 +0000 (16:30 -0600)
First version of it, needs more tweaks, but first I need
to finish an arg parser framework.

configure.ac
src/Makefile.am
src/config.c [new file with mode: 0644]
src/config.h [new file with mode: 0644]
src/main.c

index 1528b7f3515b78243f34b28b0fddf30492feba6a..d723366940d515288904f17c27d1a145e59625e7 100644 (file)
@@ -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])
 
index 1967fb2725371a3151bcbc5fcd5d936ee941d701..d615e8c57fa756907665f6c3aa51c9a024e29e96 100644 (file)
@@ -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 (file)
index 0000000..2b8fcd2
--- /dev/null
@@ -0,0 +1,135 @@
+#include "config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include <toml.h>
+
+#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 (file)
index 0000000..6d760af
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef SRC_CONFIG_H_
+#define SRC_CONFIG_H_
+
+#include <stdbool.h>
+
+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_ */
index 5fddc47e3e2789966eff26ecde6d81ff0ccd39d4..f0f9f793ac2511dd13dca52e2fa73d92a725a399 100644 (file)
@@ -4,6 +4,7 @@
 #include <openssl/objects.h>
 
 #include "common.h"
+#include "config.h"
 #include "debug.h"
 #include "log.h"
 #include "rpp.h"
 #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;