From: Jason Ish Date: Fri, 24 Mar 2023 05:49:00 +0000 (-0600) Subject: conf: allow "include" to be a list of files X-Git-Tag: suricata-7.0.0-rc2~485 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=67ce33a97ef4db5bf1bc1ecda2b940145451e3a5;p=thirdparty%2Fsuricata.git conf: allow "include" to be a list of files In preparation for deprecating multiple "include" fields at the same level, allow "include" to be a list of filenames. Ticket: #5939 --- diff --git a/src/conf-yaml-loader.c b/src/conf-yaml-loader.c index e1dad6ed4d..bbc3c9f889 100644 --- a/src/conf-yaml-loader.c +++ b/src/conf-yaml-loader.c @@ -48,7 +48,7 @@ static int mangle_errors = 0; static char *conf_dirname = NULL; -static int ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq, int rlevel); +static int ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq, int rlevel, int state); /* Configuration processing states. */ enum conf_state { @@ -142,7 +142,7 @@ int ConfYamlHandleInclude(ConfNode *parent, const char *filename) yaml_parser_set_input_file(&parser, file); - if (ConfYamlParse(&parser, parent, 0, 0) != 0) { + if (ConfYamlParse(&parser, parent, 0, 0, 0) != 0) { SCLogError("Failed to include configuration file %s", filename); goto done; } @@ -166,14 +166,12 @@ done: * * \retval 0 on success, -1 on failure. */ -static int -ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq, int rlevel) +static int ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq, int rlevel, int state) { ConfNode *node = parent; yaml_event_t event; memset(&event, 0, sizeof(event)); int done = 0; - int state = 0; int seq_idx = 0; int retval = 0; int was_empty = -1; @@ -235,6 +233,15 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq, int rlevel) } if (inseq) { + if (state == CONF_INCLUDE) { + if (value != NULL) { + SCLogInfo("Including configuration file %s.", value); + if (ConfYamlHandleInclude(parent, value) != 0) { + goto fail; + } + } + goto next; + } char sequence_node_name[DEFAULT_NAME_LEN]; snprintf(sequence_node_name, DEFAULT_NAME_LEN, "%d", seq_idx++); ConfNode *seq_node = NULL; @@ -360,7 +367,8 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq, int rlevel) } else if (event.type == YAML_SEQUENCE_START_EVENT) { SCLogDebug("event.type=YAML_SEQUENCE_START_EVENT; state=%d", state); - if (ConfYamlParse(parser, node, 1, rlevel) != 0) + if (ConfYamlParse(parser, node, 1, rlevel, state == CONF_INCLUDE ? CONF_INCLUDE : 0) != + 0) goto fail; node->is_seq = 1; state = CONF_KEY; @@ -396,11 +404,11 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq, int rlevel) } seq_node->is_seq = 1; TAILQ_INSERT_TAIL(&node->head, seq_node, next); - if (ConfYamlParse(parser, seq_node, 0, rlevel) != 0) + if (ConfYamlParse(parser, seq_node, 0, rlevel, 0) != 0) goto fail; } else { - if (ConfYamlParse(parser, node, inseq, rlevel) != 0) + if (ConfYamlParse(parser, node, inseq, rlevel, 0) != 0) goto fail; } state = CONF_KEY; @@ -477,7 +485,7 @@ ConfYamlLoadFile(const char *filename) } yaml_parser_set_input_file(&parser, infile); - ret = ConfYamlParse(&parser, root, 0, 0); + ret = ConfYamlParse(&parser, root, 0, 0, 0); yaml_parser_delete(&parser); fclose(infile); @@ -499,7 +507,7 @@ ConfYamlLoadString(const char *string, size_t len) exit(EXIT_FAILURE); } yaml_parser_set_input_string(&parser, (const unsigned char *)string, len); - ret = ConfYamlParse(&parser, root, 0, 0); + ret = ConfYamlParse(&parser, root, 0, 0, 0); yaml_parser_delete(&parser); return ret; @@ -565,7 +573,7 @@ ConfYamlLoadFileWithPrefix(const char *filename, const char *prefix) } } yaml_parser_set_input_file(&parser, infile); - ret = ConfYamlParse(&parser, root, 0, 0); + ret = ConfYamlParse(&parser, root, 0, 0, 0); yaml_parser_delete(&parser); fclose(infile);