From f104e9cecc9a554d039695757004ffb27e7a82bb Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Mon, 4 Mar 2024 17:08:08 -0600 Subject: [PATCH] suricata: expose and break out configuration loading Expose LoadYamlConfig as SCLoadYamlConfig and remove it from SuricataInit. This is required to allow the library user the ability customize the loading of the configuration, for example doing some programmatic configuration then loading a configuration file. --- examples/lib/simple/main.c | 7 +++++++ src/main.c | 8 +++++++- src/suricata.c | 9 +++------ src/suricata.h | 1 + 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/examples/lib/simple/main.c b/examples/lib/simple/main.c index 0e728e9732..f9c09fb0f5 100644 --- a/examples/lib/simple/main.c +++ b/examples/lib/simple/main.c @@ -40,6 +40,13 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } + /* Load configuration file, could be done earlier but must be done + * before SuricataInit, but even then its still optional as you + * may be programmatically configuration Suricata. */ + if (SCLoadYamlConfig() != TM_ECODE_OK) { + exit(EXIT_FAILURE); + } + SuricataInit(); SuricataPostInit(); diff --git a/src/main.c b/src/main.c index 714efda118..f9fcbf5e2c 100644 --- a/src/main.c +++ b/src/main.c @@ -44,7 +44,13 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } - /* Initialization tasks: Loading config, setup logging */ + /* Load yaml configuration file if provided. */ + if (SCLoadYamlConfig() != TM_ECODE_OK) { + exit(EXIT_FAILURE); + } + + /* Initialization tasks: apply configuration, drop privileges, + * etc. */ SuricataInit(); /* Post-initialization tasks: wait on thread start/running and get ready for the main loop. */ diff --git a/src/suricata.c b/src/suricata.c index 3bc86f446a..d59e0b8601 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -955,10 +955,12 @@ void RegisterAllModules(void) TmModuleDecodeDPDKRegister(); } -static TmEcode LoadYamlConfig(SCInstance *suri) +TmEcode SCLoadYamlConfig(void) { SCEnter(); + SCInstance *suri = &suricata; + if (suri->conf_filename == NULL) suri->conf_filename = DEFAULT_CONF_FILE; @@ -2904,11 +2906,6 @@ void SuricataInit(void) /* Initializations for global vars, queues, etc (memsets, mutex init..) */ GlobalsInitPreConfig(); - /* Load yaml configuration file if provided. */ - if (LoadYamlConfig(&suricata) != TM_ECODE_OK) { - exit(EXIT_FAILURE); - } - if (suricata.run_mode == RUNMODE_DUMP_CONFIG) { ConfDump(); exit(EXIT_SUCCESS); diff --git a/src/suricata.h b/src/suricata.h index 9ec80ddc83..f2ca5e574e 100644 --- a/src/suricata.h +++ b/src/suricata.h @@ -203,6 +203,7 @@ void PostConfLoadedDetectSetup(SCInstance *suri); int SCFinalizeRunMode(void); TmEcode SCParseCommandLine(int argc, char **argv); int SCStartInternalRunMode(int argc, char **argv); +TmEcode SCLoadYamlConfig(void); void PreRunInit(const int runmode); void PreRunPostPrivsDropInit(const int runmode);