From ae23144b67e62636b8955d28cc0b8f9c761a3334 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Wed, 20 May 2015 15:14:59 -0600 Subject: [PATCH] --set - handle spaces on either side of '=' Discard spaces when provided as part of --set around the '='. For example, "val=key", "val = key", "val= key" and "val =key" are all equivalent now. --- src/conf.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++ src/conf.h | 1 + src/suricata.c | 4 +- 3 files changed, 113 insertions(+), 2 deletions(-) diff --git a/src/conf.c b/src/conf.c index a5c7741b1e..88b9389c58 100644 --- a/src/conf.c +++ b/src/conf.c @@ -232,6 +232,56 @@ int ConfSet(const char *name, char *val) return 1; } +/** + * \brief Set a configuration parameter from a string. + * + * Where the input string is something like: + * stream.midstream=true + * + * \param input the input string to be parsed. + * + * \retval 1 if the value of set, otherwise 0. + */ +int ConfSetFromString(const char *input, int final) +{ + int retval = 0; + char *name = SCStrdup(input), *val = NULL; + if (unlikely(name == NULL)) { + goto done; + } + val = strchr(name, '='); + if (val == NULL) { + goto done; + } + *val++ = '\0'; + + while (isspace((int)name[strlen(name) - 1])) { + name[strlen(name) - 1] = '\0'; + } + + while (isspace((int)*val)) { + val++; + } + + if (final) { + if (!ConfSetFinal(name, val)) { + goto done; + } + } + else { + if (!ConfSet(name, val)) { + goto done; + } + } + + retval = 1; +done: + if (name != NULL) { + SCFree(name); + } + return retval; +} + /** * \brief Set a final configuration value. * @@ -1399,6 +1449,65 @@ end: return retval; } +static int ConfSetFromStringTest(void) +{ + int retval = 0; + ConfNode *n; + + ConfCreateContextBackup(); + ConfInit(); + + if (!ConfSetFromString("stream.midstream=true", 0)) { + goto end; + } + n = ConfGetNode("stream.midstream"); + if (n == NULL) { + goto end; + } + if (n->val == NULL || strcmp("true", n->val)) { + goto end; + } + + if (!ConfSetFromString("stream.midstream =false", 0)) { + goto end; + } + n = ConfGetNode("stream.midstream"); + if (n == NULL) { + goto end; + } + if (n->val == NULL || strcmp("false", n->val)) { + goto end; + } + + if (!ConfSetFromString("stream.midstream= true", 0)) { + goto end; + } + n = ConfGetNode("stream.midstream"); + if (n == NULL) { + goto end; + } + if (n->val == NULL || strcmp("true", n->val)) { + goto end; + } + + if (!ConfSetFromString("stream.midstream = false", 0)) { + goto end; + } + n = ConfGetNode("stream.midstream"); + if (n == NULL) { + goto end; + } + if (n->val == NULL || strcmp("false", n->val)) { + goto end; + } + + retval = 1; +end: + ConfDeInit(); + ConfRestoreContextBackup(); + return retval; +} + void ConfRegisterTests(void) { UtRegisterTest("ConfTestGetNonExistant", ConfTestGetNonExistant, 1); @@ -1417,6 +1526,7 @@ void ConfRegisterTests(void) UtRegisterTest("ConfGetNodeOrCreateTest", ConfGetNodeOrCreateTest, 1); UtRegisterTest("ConfNodePruneTest", ConfNodePruneTest, 1); UtRegisterTest("ConfNodeIsSequenceTest", ConfNodeIsSequenceTest, 1); + UtRegisterTest("ConfSetFromStringTest", ConfSetFromStringTest, 1); } #endif /* UNITTESTS */ diff --git a/src/conf.h b/src/conf.h index 2c01a43ee1..2318580a65 100644 --- a/src/conf.h +++ b/src/conf.h @@ -62,6 +62,7 @@ int ConfGetBool(const char *name, int *val); int ConfGetDouble(const char *name, double *val); int ConfGetFloat(const char *name, float *val); int ConfSet(const char *name, char *val); +int ConfSetFromString(const char *input, int final); int ConfSetFinal(const char *name, char *val); void ConfDump(void); void ConfNodeDump(const ConfNode *node, const char *prefix); diff --git a/src/suricata.c b/src/suricata.c index 4b88b7b12a..b26924f307 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -1479,14 +1479,14 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri) #endif else if (strcmp((long_opts[option_index]).name, "set") == 0) { if (optarg != NULL) { + /* Quick validation. */ char *val = strchr(optarg, '='); if (val == NULL) { SCLogError(SC_ERR_CMD_LINE, "Invalid argument for --set, must be key=val."); exit(EXIT_FAILURE); } - *val++ = '\0'; - if (ConfSetFinal(optarg, val) != 1) { + if (!ConfSetFromString(optarg, 1)) { fprintf(stderr, "Failed to set configuration value %s.", optarg); exit(EXIT_FAILURE);