From: Jason Ish Date: Thu, 21 Nov 2013 20:52:06 +0000 (-0600) Subject: Subsequent configuration keys now override previous ones X-Git-Tag: suricata-2.0beta2~81 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b033acfb0cba7c0356572a09fcfcb01b62fe33fd;p=thirdparty%2Fsuricata.git Subsequent configuration keys now override previous ones instead of merging. The exception is final values, for example, values like default-log-dir that may be set on the command line. --- diff --git a/src/conf-yaml-loader.c b/src/conf-yaml-loader.c index 9442caeac6..a5e0d27e66 100644 --- a/src/conf-yaml-loader.c +++ b/src/conf-yaml-loader.c @@ -237,9 +237,14 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq) Mangle(parent->val); } } - ConfNode *n0 = ConfNodeLookupChild(parent, value); - if (n0 != NULL) { - node = n0; + ConfNode *existing = ConfNodeLookupChild(parent, value); + if (existing != NULL) { + if (!existing->final) { + SCLogInfo("Configuration node '%s' redefined.", + existing->name); + ConfNodePrune(existing); + } + node = existing; } else { node = ConfNodeNew(); @@ -733,6 +738,88 @@ cleanup: return ret; } +/** + * Test that a configuration section is overridden but subsequent + * occurrences. + */ +static int +ConfYamlOverrideTest(void) +{ + char config[] = + "%YAML 1.1\n" + "---\n" + "some-log-dir: /var/log\n" + "some-log-dir: /tmp\n" + "\n" + "parent:\n" + " child0:\n" + " key: value\n" + "parent:\n" + " child1:\n" + " key: value\n" + ; + char *value; + + ConfCreateContextBackup(); + ConfInit(); + + if (ConfYamlLoadString(config, strlen(config)) != 0) + return 0; + if (!ConfGet("some-log-dir", &value)) + return 0; + if (strcmp(value, "/tmp") != 0) + return 0; + + /* Test that parent.child0 does not exist, but child1 does. */ + if (ConfGetNode("parent.child0") != NULL) + return 0; + if (!ConfGet("parent.child1.key", &value)) + return 0; + if (strcmp(value, "value") != 0) + return 0; + + ConfDeInit(); + ConfRestoreContextBackup(); + + return 1; +} + +/** + * Test that a configuration parameter loaded from YAML doesn't + * override a 'final' value that may be set on the command line. + */ +static int +ConfYamlOverrideFinalTest(void) +{ + ConfCreateContextBackup(); + ConfInit(); + + char config[] = + "%YAML 1.1\n" + "---\n" + "default-log-dir: /var/log\n"; + + /* Set the log directory as if it was set on the command line. */ + if (!ConfSetFinal("default-log-dir", "/tmp")) + return 0; + if (ConfYamlLoadString(config, strlen(config)) != 0) + return 0; + + char *default_log_dir; + + if (!ConfGet("default-log-dir", &default_log_dir)) + return 0; + if (strcmp(default_log_dir, "/tmp") != 0) { + fprintf(stderr, "final value was reassigned\n"); + return 0; + } + + ConfDeInit(); + ConfRestoreContextBackup(); + + return 1; +} + #endif /* UNITTESTS */ void @@ -746,5 +833,7 @@ ConfYamlRegisterTests(void) UtRegisterTest("ConfYamlSecondLevelSequenceTest", ConfYamlSecondLevelSequenceTest, 1); UtRegisterTest("ConfYamlFileIncludeTest", ConfYamlFileIncludeTest, 1); + UtRegisterTest("ConfYamlOverrideTest", ConfYamlOverrideTest, 1); + UtRegisterTest("ConfYamlOverrideFinalTest", ConfYamlOverrideFinalTest, 1); #endif /* UNITTESTS */ }