From 489b8b8bcceddb0ce5950ae6ef83fe05f1a39633 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Sat, 11 Feb 2012 15:43:09 +0100 Subject: [PATCH] Allow other yaml files to be included in the main yaml. --- src/conf.c | 36 ++++++++++++++++++++++++++++++++++++ src/conf.h | 1 + src/suricata.c | 15 +++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/src/conf.c b/src/conf.c index beeaa489bb..68223b1a74 100644 --- a/src/conf.c +++ b/src/conf.c @@ -42,6 +42,7 @@ #include "conf.h" #include "util-unittest.h" #include "util-debug.h" +#include "util-path.h" static ConfNode *root = NULL; static ConfNode *root_backup = NULL; @@ -702,6 +703,41 @@ ConfNodeChildValueIsTrue(ConfNode *node, const char *key) return val != NULL ? ConfValIsTrue(val) : 0; } +/** + * \brief Create the path for an include entry + * \param file The name of the file + * \retval str Pointer to the string path + sig_file + */ +char *ConfLoadCompleteIncludePath(char *file) +{ + char *defaultpath = NULL; + char *path = NULL; + + /* Path not specified */ + if (PathIsRelative(file)) { + if (ConfGet("include-path", &defaultpath) == 1) { + SCLogDebug("Default path: %s", defaultpath); + size_t path_len = sizeof(char) * (strlen(defaultpath) + + strlen(file) + 2); + path = SCMalloc(path_len); + if (path == NULL) + return NULL; + strlcpy(path, defaultpath, path_len); + if (path[strlen(path) - 1] != '/') + strlcat(path, "/", path_len); + strlcat(path, file, path_len); + } else { + path = SCStrdup(file); + } + } else { + path = SCStrdup(file); + } + return path; +} + + + + #ifdef UNITTESTS /** diff --git a/src/conf.h b/src/conf.h index 9dad409a09..8aad1e7ff2 100644 --- a/src/conf.h +++ b/src/conf.h @@ -80,5 +80,6 @@ ConfNode *ConfNodeLookupKeyValue(ConfNode *base, const char *key, const char *va int ConfGetChildValue(ConfNode *base, char *name, char **vptr); int ConfGetChildValueInt(ConfNode *base, char *name, intmax_t *val); int ConfGetChildValueBool(ConfNode *base, char *name, int *val); +char *ConfLoadCompleteIncludePath(char *); #endif /* ! __CONF_H__ */ diff --git a/src/suricata.c b/src/suricata.c index ec1c95c8f9..ba7acd8341 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -1153,6 +1153,21 @@ int main(int argc, char **argv) /* Error already displayed. */ exit(EXIT_FAILURE); } + + ConfNode *file; + ConfNode *includes = ConfGetNode("include"); + if (includes != NULL) { + TAILQ_FOREACH(file, &includes->head, next) { + char *ifile = ConfLoadCompleteIncludePath(file->val); + SCLogInfo("Including: %s", ifile); + + if (ConfYamlLoadFile(ifile) != 0) { + /* Error already displayed. */ + exit(EXIT_FAILURE); + } + } + } + } else if (run_mode != RUNMODE_UNITTEST){ SCLogError(SC_ERR_OPENING_FILE, "Configuration file has not been provided"); usage(argv[0]);