diff --git a/libpromises/evalfunction.c b/libpromises/evalfunction.c index 7f693714935..e8a4e76a8ba 100644 --- a/libpromises/evalfunction.c +++ b/libpromises/evalfunction.c @@ -7660,22 +7660,24 @@ static int ParseDate(const char *input_string, time_t *out) strncpy(date_path, GetBinDir(), sizeof(date_path) - 1); JoinPaths(date_path, sizeof(date_path), "date"); - char buffer[CF_BUFSIZE]; - int n = snprintf(buffer, sizeof(buffer), "%s --date '%s' +%%s", date_path, input_string); - - if (n < 0 || (size_t) n >= sizeof(buffer)) { - Log(LOG_LEVEL_ERR, "Truncation error: input string '%.10s...' is too long (%d >= %zu)", - input_string, n, sizeof(buffer)); - return -1; - } - - FILE *fd = cf_popen(buffer, "r", true); + /* The input must be its own argument: inside a command string, a quote in + * it would end the argument and add new ones (see ArgSplitCommand()) */ + Seq *arglist = SeqNew(3, free); + SeqAppend(arglist, xstrdup("--date")); + SeqAppend(arglist, xstrdup(input_string)); + SeqAppend(arglist, xstrdup("+%s")); + + FILE *fd = cf_popensetuid(date_path, arglist, "r", CF_SAME_OWNER, + CF_SAME_GROUP, NULL, NULL, false); + SeqDestroy(arglist); if (fd == NULL) { - Log(LOG_LEVEL_ERR, "Couldn't run command: '%s'", buffer); + Log(LOG_LEVEL_ERR, "Couldn't run '%s' for date '%s'", date_path, + input_string); return -1; } + char buffer[CF_BUFSIZE]; size_t bytes_read = fread(buffer, 1, sizeof(buffer) - 1 , fd); buffer[bytes_read] = '\0'; @@ -7689,10 +7691,10 @@ static int ParseDate(const char *input_string, time_t *out) { Log(LOG_LEVEL_DEBUG, "No output read for '%s'", input_string); } - fclose(fd); + cf_pclose(fd); return -1; } - fclose(fd); + cf_pclose(fd); long time_value; int ret = StringToLong(buffer, &time_value); diff --git a/tests/acceptance/01_vars/02_functions/strtotime.cf b/tests/acceptance/01_vars/02_functions/strtotime.cf index 1b6d5eb98ac..2640539551a 100644 --- a/tests/acceptance/01_vars/02_functions/strtotime.cf +++ b/tests/acceptance/01_vars/02_functions/strtotime.cf @@ -17,8 +17,16 @@ bundle agent test "some_date" int => strtotime("2009-09-21T00:00:00Z"); "epoch" int => int(1253491200); + # Quotes must not split the date into several arguments (ENT-14983). + # As one argument "(...)" is a comment, split into "@100 (" and "@200)" + # date fails. + "quoted_date" int => strtotime("@100 (' '@200)"); + classes: - "ok" expression => strcmp("$(epoch)", "$(some_date)"); + "ok" + expression => and( + strcmp("$(epoch)", "$(some_date)"), strcmp("100", "$(quoted_date)") + ); reports: ok::