From 3af1c558df7b33a2c6eeff9ee2cc2324cd8ad4b3 Mon Sep 17 00:00:00 2001 From: Tilghman Lesher Date: Fri, 20 Feb 2009 17:29:51 +0000 Subject: [PATCH] Allow semicolons to be escaped, when passing arguments to the System command. (closes issue #14231) Reported by: jcovert Patches: 20090113__bug14231__2.diff.txt uploaded by Corydon76 (license 14) corrected_20090113__bug14231__2.diff.txt uploaded by jcovert (license 551) Tested by: jcovert git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@177664 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- apps/app_system.c | 9 ++++++++- include/asterisk/app.h | 3 +++ main/app.c | 27 +++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/apps/app_system.c b/apps/app_system.c index 688a649da6..e06a9cb5d8 100644 --- a/apps/app_system.c +++ b/apps/app_system.c @@ -33,6 +33,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/module.h" #include "asterisk/app.h" #include "asterisk/channel.h" /* autoservice */ +#include "asterisk/strings.h" +#include "asterisk/threadstorage.h" /*** DOCUMENTATION @@ -90,6 +92,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") ***/ +AST_THREADSTORAGE(buf_buf); + static char *app = "System"; static char *app2 = "TrySystem"; @@ -99,6 +103,7 @@ static char *chanvar = "SYSTEMSTATUS"; static int system_exec_helper(struct ast_channel *chan, void *data, int failmode) { int res = 0; + struct ast_str *buf = ast_str_thread_get(&buf_buf, 16); if (ast_strlen_zero(data)) { ast_log(LOG_WARNING, "System requires an argument(command)\n"); @@ -109,7 +114,9 @@ static int system_exec_helper(struct ast_channel *chan, void *data, int failmode ast_autoservice_start(chan); /* Do our thing here */ - res = ast_safe_system((char *)data); + ast_str_get_encoded_str(&buf, 0, (char *) data); + res = ast_safe_system(ast_str_buffer(buf)); + if ((res < 0) && (errno != ECHILD)) { ast_log(LOG_WARNING, "Unable to execute '%s'\n", (char *)data); pbx_builtin_setvar_helper(chan, chanvar, "FAILURE"); diff --git a/include/asterisk/app.h b/include/asterisk/app.h index 5bc48515e4..0e8e0c9107 100644 --- a/include/asterisk/app.h +++ b/include/asterisk/app.h @@ -498,6 +498,9 @@ int ast_get_encoded_char(const char *stream, char *result, size_t *consumed); /*! \brief Decode a stream of encoded control or extended ASCII characters */ char *ast_get_encoded_str(const char *stream, char *result, size_t result_len); +/*! \brief Decode a stream of encoded control or extended ASCII characters */ +int ast_str_get_encoded_str(struct ast_str **str, int maxlen, const char *stream); + /*! \brief Common routine for child processes, to close all fds prior to exec(2) */ void ast_close_fds_above_n(int n); diff --git a/main/app.c b/main/app.c index 83939c55b1..cf48b2f1fd 100644 --- a/main/app.c +++ b/main/app.c @@ -1846,6 +1846,33 @@ char *ast_get_encoded_str(const char *stream, char *result, size_t result_size) return result; } +int ast_str_get_encoded_str(struct ast_str **str, int maxlen, const char *stream) +{ + char next, *buf; + size_t offset = 0; + size_t consumed; + + if (strchr(stream, '\\')) { + while (!ast_get_encoded_char(stream, &next, &consumed)) { + if (offset + 2 > ast_str_size(*str) && maxlen > -1) { + ast_str_make_space(str, maxlen > 0 ? maxlen : (ast_str_size(*str) + 48) * 2 - 48); + } + if (offset + 2 > ast_str_size(*str)) { + break; + } + buf = ast_str_buffer(*str); + buf[offset++] = next; + stream += consumed; + } + buf = ast_str_buffer(*str); + buf[offset++] = '\0'; + ast_str_update(*str); + } else { + ast_str_set(str, maxlen, "%s", stream); + } + return 0; +} + void ast_close_fds_above_n(int n) { #ifdef HAVE_CLOSEFROM