diff --git a/src/conf-yaml-loader.c b/src/conf-yaml-loader.c index c97ec0550f..9442caeac6 100644 --- a/src/conf-yaml-loader.c +++ b/src/conf-yaml-loader.c @@ -271,7 +271,7 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq) if (ConfYamlHandleInclude(node, value) != 0) goto fail; } - else if (node->allow_override) { + else if (!node->final) { if (node->val != NULL) SCFree(node->val); node->val = SCStrdup(value); diff --git a/src/conf.c b/src/conf.c index 82e1879f18..61fd294886 100644 --- a/src/conf.c +++ b/src/conf.c @@ -131,8 +131,6 @@ ConfNodeNew(void) if (unlikely(new == NULL)) { return NULL; } - /* By default we allow an override. */ - new->allow_override = 1; TAILQ_INIT(&new->head); return new; @@ -215,16 +213,29 @@ ConfGetRootNode(void) * \retval 1 if the value was set otherwise 0. */ int -ConfSet(char *name, char *val, int allow_override) +ConfSet(char *name, char *val) { ConfNode *node = ConfGetNodeOrCreate(name); - if (node == NULL || !node->allow_override) { + if (node == NULL || node->final) { return 0; } if (node->val != NULL) SCFree(node->val); node->val = SCStrdup(val); - node->allow_override = allow_override; + return 1; +} + +int +ConfSetFinal(char *name, char *val) +{ + ConfNode *node = ConfGetNodeOrCreate(name); + if (node == NULL) { + return 0; + } + if (node->val != NULL) + SCFree(node->val); + node->val = SCStrdup(val); + node->final = 1; return 1; } @@ -786,7 +797,7 @@ ConfTestSetAndGet(void) char value[] = "some-value"; char *value0; - if (ConfSet(name, value, 1) != 1) + if (ConfSet(name, value) != 1) return 0; if (ConfGet(name, &value0) != 1) return 0; @@ -812,9 +823,9 @@ ConfTestOverrideValue1(void) char *val; int rc; - if (ConfSet(name, value0, 1) != 1) + if (ConfSet(name, value0) != 1) return 0; - if (ConfSet(name, value1, 1) != 1) + if (ConfSet(name, value1) != 1) return 0; if (ConfGet(name, &val) != 1) return 0; @@ -828,8 +839,7 @@ ConfTestOverrideValue1(void) } /** - * Test that overriding a value is not allowed provided that - * allow_override is false and make sure the value was not overrided. + * Test that a final value will not be overrided by a ConfSet. */ static int ConfTestOverrideValue2(void) @@ -840,9 +850,9 @@ ConfTestOverrideValue2(void) char *val; int rc; - if (ConfSet(name, value0, 0) != 1) + if (ConfSetFinal(name, value0) != 1) return 0; - if (ConfSet(name, value1, 1) != 0) + if (ConfSet(name, value1) != 0) return 0; if (ConfGet(name, &val) != 1) return 0; @@ -864,7 +874,7 @@ ConfTestGetInt(void) char name[] = "some-int.x"; intmax_t val; - if (ConfSet(name, "0", 1) != 1) + if (ConfSet(name, "0") != 1) return 0; if (ConfGetInt(name, &val) != 1) return 0; @@ -872,21 +882,21 @@ ConfTestGetInt(void) if (val != 0) return 0; - if (ConfSet(name, "-1", 1) != 1) + if (ConfSet(name, "-1") != 1) return 0; if (ConfGetInt(name, &val) != 1) return 0; if (val != -1) return 0; - if (ConfSet(name, "0xffff", 1) != 1) + if (ConfSet(name, "0xffff") != 1) return 0; if (ConfGetInt(name, &val) != 1) return 0; if (val != 0xffff) return 0; - if (ConfSet(name, "not-an-int", 1) != 1) + if (ConfSet(name, "not-an-int") != 1) return 0; if (ConfGetInt(name, &val) != 0) return 0; @@ -918,7 +928,7 @@ ConfTestGetBool(void) size_t u; for (u = 0; u < sizeof(trues) / sizeof(trues[0]); u++) { - if (ConfSet(name, trues[u], 1) != 1) + if (ConfSet(name, trues[u]) != 1) return 0; if (ConfGetBool(name, &val) != 1) return 0; @@ -927,7 +937,7 @@ ConfTestGetBool(void) } for (u = 0; u < sizeof(falses) / sizeof(falses[0]); u++) { - if (ConfSet(name, falses[u], 1) != 1) + if (ConfSet(name, falses[u]) != 1) return 0; if (ConfGetBool(name, &val) != 1) return 0; @@ -1037,9 +1047,9 @@ static int ConfGetChildValueWithDefaultTest(void) int ret = 1; ConfCreateContextBackup(); ConfInit(); - ConfSet("af-packet.0.interface", "eth0", 1); - ConfSet("af-packet.1.interface", "default", 1); - ConfSet("af-packet.1.cluster-type", "cluster_cpu", 1); + ConfSet("af-packet.0.interface", "eth0"); + ConfSet("af-packet.1.interface", "default"); + ConfSet("af-packet.1.cluster-type", "cluster_cpu"); ConfNode *root = ConfGetNode("af-packet.0"); ConfNode *dflt = ConfGetNode("af-packet.1"); @@ -1050,7 +1060,7 @@ static int ConfGetChildValueWithDefaultTest(void) return 0; } - ConfSet("af-packet.0.cluster-type", "cluster_flow", 1); + ConfSet("af-packet.0.cluster-type", "cluster_flow"); ConfGetChildValueWithDefault(root, dflt, "cluster-type", &val); if (strcmp(val, "cluster_flow")) { @@ -1066,9 +1076,9 @@ static int ConfGetChildValueIntWithDefaultTest(void) intmax_t val; ConfCreateContextBackup(); ConfInit(); - ConfSet("af-packet.0.interface", "eth0", 1); - ConfSet("af-packet.1.interface", "default", 1); - ConfSet("af-packet.1.threads", "2", 1); + ConfSet("af-packet.0.interface", "eth0"); + ConfSet("af-packet.1.interface", "default"); + ConfSet("af-packet.1.threads", "2"); ConfNode *root = ConfGetNode("af-packet.0"); ConfNode *dflt = ConfGetNode("af-packet.1"); @@ -1079,7 +1089,7 @@ static int ConfGetChildValueIntWithDefaultTest(void) return 0; } - ConfSet("af-packet.0.threads", "1", 1); + ConfSet("af-packet.0.threads", "1"); ConfGetChildValueIntWithDefault(root, dflt, "threads", &val); ConfDeInit(); @@ -1095,9 +1105,9 @@ static int ConfGetChildValueBoolWithDefaultTest(void) int val; ConfCreateContextBackup(); ConfInit(); - ConfSet("af-packet.0.interface", "eth0", 1); - ConfSet("af-packet.1.interface", "default", 1); - ConfSet("af-packet.1.use-mmap", "yes", 1); + ConfSet("af-packet.0.interface", "eth0"); + ConfSet("af-packet.1.interface", "default"); + ConfSet("af-packet.1.use-mmap", "yes"); ConfNode *root = ConfGetNode("af-packet.0"); ConfNode *dflt = ConfGetNode("af-packet.1"); @@ -1108,7 +1118,7 @@ static int ConfGetChildValueBoolWithDefaultTest(void) return 0; } - ConfSet("af-packet.0.use-mmap", "no", 1); + ConfSet("af-packet.0.use-mmap", "no"); ConfGetChildValueBoolWithDefault(root, dflt, "use-mmap", &val); ConfDeInit(); @@ -1128,7 +1138,7 @@ ConfNodeRemoveTest(void) ConfCreateContextBackup(); ConfInit(); - if (ConfSet("some.nested.parameter", "blah", 1) != 1) + if (ConfSet("some.nested.parameter", "blah") != 1) return 0; ConfNode *node = ConfGetNode("some.nested.parameter"); @@ -1153,7 +1163,7 @@ ConfSetTest(void) ConfInit(); /* Set some value with 2 levels. */ - if (ConfSet("one.two", "three", 1) != 1) + if (ConfSet("one.two", "three") != 1) return 0; ConfNode *n = ConfGetNode("one.two"); if (n == NULL) @@ -1162,7 +1172,7 @@ ConfSetTest(void) /* Set another 2 level parameter with the same first level, this * used to trigger a bug that caused the second level of the name * to become a first level node. */ - if (ConfSet("one.three", "four", 1) != 1) + if (ConfSet("one.three", "four") != 1) return 0; n = ConfGetNode("one.three"); diff --git a/src/conf.h b/src/conf.h index c9a089f771..86caeaa8f4 100644 --- a/src/conf.h +++ b/src/conf.h @@ -34,7 +34,9 @@ typedef struct ConfNode_ { char *val; int is_seq; - int allow_override; + + /**< Flag that sets this nodes value as final. */ + int final; struct ConfNode_ *parent; TAILQ_HEAD(, ConfNode_) head; @@ -59,7 +61,8 @@ int ConfGetInt(char *name, intmax_t *val); int ConfGetBool(char *name, int *val); int ConfGetDouble(char *name, double *val); int ConfGetFloat(char *name, float *val); -int ConfSet(char *name, char *val, int allow_override); +int ConfSet(char *name, char *val); +int ConfSetFinal(char *name, char *val); void ConfDump(void); void ConfNodeDump(ConfNode *node, const char *prefix); ConfNode *ConfNodeNew(void); diff --git a/src/defrag.c b/src/defrag.c index 01d8c6c907..bd2c6e854b 100644 --- a/src/defrag.c +++ b/src/defrag.c @@ -2040,7 +2040,7 @@ DefragTimeoutTest(void) int ret = 0; /* Setup a small numberr of trackers. */ - if (ConfSet("defrag.trackers", "16", 1) != 1) { + if (ConfSet("defrag.trackers", "16") != 1) { printf("ConfSet failed: "); goto end; } diff --git a/src/runmode-unix-socket.c b/src/runmode-unix-socket.c index f1e2b7e825..fd198c84ff 100644 --- a/src/runmode-unix-socket.c +++ b/src/runmode-unix-socket.c @@ -307,13 +307,13 @@ TmEcode UnixSocketPcapFilesCheck(void *data) SCLogInfo("Starting run for '%s'", cfile->filename); unix_manager_file_task_running = 1; this->running = 1; - if (ConfSet("pcap-file.file", cfile->filename, 1) != 1) { + if (ConfSet("pcap-file.file", cfile->filename) != 1) { SCLogInfo("Can not set working file to '%s'", cfile->filename); PcapFilesFree(cfile); return TM_ECODE_FAILED; } if (cfile->output_dir) { - if (ConfSet("default-log-dir", cfile->output_dir, 1) != 1) { + if (ConfSet("default-log-dir", cfile->output_dir) != 1) { SCLogInfo("Can not set output dir to '%s'", cfile->output_dir); PcapFilesFree(cfile); return TM_ECODE_FAILED; diff --git a/src/suricata.c b/src/suricata.c index a569bafa94..32c7157cd9 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -389,7 +389,7 @@ static int SetBpfString(int optind, char *argv[]) { } if(strlen(bpf_filter) > 0) { - if (ConfSet("bpf-filter", bpf_filter, 0) != 1) { + if (ConfSetFinal("bpf-filter", bpf_filter) != 1) { SCLogError(SC_ERR_FATAL, "Failed to set bpf filter."); return TM_ECODE_FAILED; } @@ -459,7 +459,7 @@ static void SetBpfStringFromFile(char *filename) { while((bpf_comment_tmp = strchr(bpf_filter, '\n')) != NULL) { *bpf_comment_tmp = ' '; } - if(ConfSet("bpf-filter", bpf_filter, 0) != 1) { + if(ConfSetFinal("bpf-filter", bpf_filter) != 1) { SCLogError(SC_ERR_FOPEN, "ERROR: Failed to set bpf filter!"); SCFree(bpf_filter); exit(EXIT_FAILURE); @@ -873,7 +873,7 @@ static TmEcode ParseInterfacesList(int run_mode, char *pcap_dev) #ifdef HAVE_MPIPE } else if (run_mode == RUNMODE_TILERA_MPIPE) { if (strlen(pcap_dev)) { - if (ConfSet("mpipe.single_mpipe_dev", pcap_dev, 0) != 1) { + if (ConfSetFinal("mpipe.single_mpipe_dev", pcap_dev) != 1) { fprintf(stderr, "ERROR: Failed to set mpipe.single_mpipe_dev\n"); SCReturnInt(TM_ECODE_FAILED); } @@ -889,7 +889,7 @@ static TmEcode ParseInterfacesList(int run_mode, char *pcap_dev) /* FIXME add backward compat support */ /* iface has been set on command line */ if (strlen(pcap_dev)) { - if (ConfSet("pfring.live-interface", pcap_dev, 0) != 1) { + if (ConfSetFinal("pfring.live-interface", pcap_dev) != 1) { SCLogError(SC_ERR_INITIALIZATION, "Failed to set pfring.live-interface"); SCReturnInt(TM_ECODE_FAILED); } @@ -900,7 +900,7 @@ static TmEcode ParseInterfacesList(int run_mode, char *pcap_dev) } else if (run_mode == RUNMODE_AFP_DEV) { /* iface has been set on command line */ if (strlen(pcap_dev)) { - if (ConfSet("af-packet.live-interface", pcap_dev, 0) != 1) { + if (ConfSetFinal("af-packet.live-interface", pcap_dev) != 1) { SCLogError(SC_ERR_INITIALIZATION, "Failed to set af-packet.live-interface"); SCReturnInt(TM_ECODE_FAILED); } @@ -1073,7 +1073,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri) } else if(strcmp((long_opts[option_index]).name , "pfring-cluster-id") == 0){ #ifdef HAVE_PFRING - if (ConfSet("pfring.cluster-id", optarg, 0) != 1) { + if (ConfSetFinal("pfring.cluster-id", optarg) != 1) { fprintf(stderr, "ERROR: Failed to set pfring.cluster-id.\n"); return TM_ECODE_FAILED; } @@ -1085,7 +1085,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri) } else if(strcmp((long_opts[option_index]).name , "pfring-cluster-type") == 0){ #ifdef HAVE_PFRING - if (ConfSet("pfring.cluster-type", optarg, 0) != 1) { + if (ConfSetFinal("pfring.cluster-type", optarg) != 1) { fprintf(stderr, "ERROR: Failed to set pfring.cluster-type.\n"); return TM_ECODE_FAILED; } @@ -1154,7 +1154,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri) return TM_ECODE_FAILED; } } else if(strcmp((long_opts[option_index]).name, "init-errors-fatal") == 0) { - if (ConfSet("engine.init-failure-fatal", "1", 0) != 1) { + if (ConfSetFinal("engine.init-failure-fatal", "1") != 1) { fprintf(stderr, "ERROR: Failed to set engine init-failure-fatal.\n"); return TM_ECODE_FAILED; } @@ -1163,7 +1163,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri) if (suri->run_mode == RUNMODE_UNKNOWN) { suri->run_mode = RUNMODE_UNIX_SOCKET; if (optarg) { - if (ConfSet("unix-command.filename", optarg, 0) != 1) { + if (ConfSetFinal("unix-command.filename", optarg) != 1) { fprintf(stderr, "ERROR: Failed to set unix-command.filename.\n"); return TM_ECODE_FAILED; } @@ -1226,7 +1226,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri) } else if(strcmp((long_opts[option_index]).name, "fatal-unittests") == 0) { #ifdef UNITTESTS - if (ConfSet("unittests.failure-fatal", "1", 0) != 1) { + if (ConfSetFinal("unittests.failure-fatal", "1") != 1) { fprintf(stderr, "ERROR: Failed to set unittests failure-fatal.\n"); return TM_ECODE_FAILED; } @@ -1257,7 +1257,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri) } else if (strcmp((long_opts[option_index]).name, "erf-in") == 0) { suri->run_mode = RUNMODE_ERF_FILE; - if (ConfSet("erf-file.file", optarg, 0) != 1) { + if (ConfSetFinal("erf-file.file", optarg) != 1) { fprintf(stderr, "ERROR: Failed to set erf-file.file\n"); return TM_ECODE_FAILED; } @@ -1291,7 +1291,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri) } else if(strcmp((long_opts[option_index]).name, "pcap-buffer-size") == 0) { #ifdef HAVE_PCAP_SET_BUFF - if (ConfSet("pcap.buffer-size", optarg, 0) != 1) { + if (ConfSetFinal("pcap.buffer-size", optarg) != 1) { fprintf(stderr, "ERROR: Failed to set pcap-buffer-size.\n"); return TM_ECODE_FAILED; } @@ -1330,7 +1330,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri) case 'T': SCLogInfo("Running suricata under test mode"); conf_test = 1; - if (ConfSet("engine.init-failure-fatal", "1", 0) != 1) { + if (ConfSetFinal("engine.init-failure-fatal", "1") != 1) { fprintf(stderr, "ERROR: Failed to set engine init-failure-fatal.\n"); return TM_ECODE_FAILED; } @@ -1455,7 +1455,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri) usage(argv[0]); return TM_ECODE_FAILED; } - if (ConfSet("pcap-file.file", optarg, 0) != 1) { + if (ConfSetFinal("pcap-file.file", optarg) != 1) { fprintf(stderr, "ERROR: Failed to set pcap-file.file\n"); return TM_ECODE_FAILED; } diff --git a/src/util-conf.c b/src/util-conf.c index e74b1ca18d..e0f25d1d63 100644 --- a/src/util-conf.c +++ b/src/util-conf.c @@ -28,7 +28,7 @@ TmEcode ConfigSetLogDirectory(char *name) { - return ConfSet("default-log-dir", name, 0) ? TM_ECODE_OK : TM_ECODE_FAILED; + return ConfSetFinal("default-log-dir", name) ? TM_ECODE_OK : TM_ECODE_FAILED; } char *ConfigGetLogDirectory()