]> granicus.if.org Git - icinga2/commitdiff
Shell-escape macros.
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 22 Mar 2013 09:58:47 +0000 (10:58 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 22 Mar 2013 09:58:47 +0000 (10:58 +0100)
lib/base/qstring.cpp
lib/base/qstring.h
lib/base/utility.cpp
lib/base/utility.h
lib/icinga/macroprocessor.cpp
lib/icinga/macroprocessor.h
lib/icinga/pluginchecktask.cpp
lib/icinga/pluginnotificationtask.cpp
test/Makefile.am
test/base-shellescape.cpp [new file with mode: 0644]

index 8ee5c0e4004c312be4ef1b25343422b26b4aa2c4..1ae6cff0d6e4c44a1d49891cb945cdc3989a7ad9 100644 (file)
@@ -86,6 +86,12 @@ String& String::operator+=(const char *rhs)
        return *this;
 }
 
+String& String::operator+=(char rhs)
+{
+       m_Data += rhs;
+       return *this;
+}
+
 bool String::IsEmpty(void) const
 {
        return m_Data.empty();
@@ -121,6 +127,11 @@ size_t String::FindFirstOf(const char *s, size_t pos) const
        return m_Data.find_first_of(s, pos);
 }
 
+size_t String::FindFirstOf(char ch, size_t pos) const
+{
+       return m_Data.find_first_of(ch, pos);
+}
+
 String String::SubStr(size_t first, size_t len) const
 {
        return m_Data.substr(first, len);
index 38d289dceb0ecf0ba5c40f4a23365005ee224597..5c7628e9dd431c640fbd6f69cc505fd94bc1e55e 100644 (file)
@@ -63,6 +63,7 @@ public:
 
        String& operator+=(const String& rhs);
        String& operator+=(const char *rhs);
+       String& operator+=(char rhs);
 
        bool IsEmpty(void) const;
 
@@ -75,6 +76,7 @@ public:
        size_t GetLength(void) const;
 
        size_t FindFirstOf(const char *s, size_t pos = 0) const;
+       size_t FindFirstOf(char ch, size_t pos = 0) const;
        String SubStr(size_t first, size_t len = NPos) const;
        void Replace(size_t first, size_t second, const String& str);
 
index b12367fbf045f995c7ed75b86b0a28d2f0f711a0..d970380bd8840fff223170dfac29418516278739 100644 (file)
@@ -431,3 +431,49 @@ String Utility::FormatDateTime(const char *format, double ts)
 
        return timestamp;
 }
+
+String Utility::EscapeShellCmd(const String& s)
+{
+       String result;
+       int prev_quote = String::NPos;
+       ssize_t index = -1;
+
+       BOOST_FOREACH(char ch, s) {
+               bool escape = false;
+
+               index++;
+
+#ifdef _WIN32
+               if (ch == '%' || ch == '"' || ch == '\'')
+                       escape = true;
+#else /* _WIN32 */
+               if (ch == '"' || ch == '\'') {
+                       /* Find a matching closing quotation character. */
+                       if (prev_quote == String::NPos && (prev_quote = s.FindFirstOf(ch, index + 1)) != String::NPos)
+                               ; /* Empty statement. */
+                       else if (prev_quote != String::NPos && s[prev_quote] == ch)
+                               prev_quote = String::NPos;
+                       else
+                               escape = true;
+               }
+#endif /* _WIN32 */
+
+               if (ch == '#' || ch == '&' || ch == ';' || ch == '`' || ch == '|' ||
+                   ch == '*' || ch == '?' || ch == '~' || ch == '<' || ch == '>' ||
+                   ch == '^' || ch == '(' || ch == ')' || ch == '[' || ch == ']' ||
+                   ch == '{' || ch == '}' || ch == '$' || ch == '\\' || ch == '\x0A' ||
+                   ch == '\xFF')
+                       escape = true;
+
+               if (escape)
+#ifdef _WIN32
+                       result += '%';
+#else /* _WIN32 */
+                       result += '\\';
+#endif /* _WIN32 */
+
+               result += ch;
+       }
+
+       return result;
+}
index 826863fd6246052fe76c6dc610c7da0b76b319cb..4a5e869387c8b4c4f08f23a5899ab0d78ce07d52 100644 (file)
@@ -75,6 +75,8 @@ public:
 
        static void SetNonBlockingSocket(SOCKET s);
 
+       static String EscapeShellCmd(const String& s);
+
 private:
        Utility(void);
 };
index 43458036640f2b4299f1cd7de832dd9637948a54..53b310effc23d8d65be59f1f8be0da0b558bb075 100644 (file)
@@ -30,14 +30,15 @@ using namespace icinga;
 /**
  * @threadsafety Always.
  */
-Value MacroProcessor::ResolveMacros(const Value& cmd, const Dictionary::Ptr& macros)
+Value MacroProcessor::ResolveMacros(const Value& cmd, const Dictionary::Ptr& macros,
+    const MacroProcessor::EscapeCallback& escapeFn)
 {
        Value result;
 
        ASSERT(macros->IsSealed());
 
        if (cmd.IsScalar()) {
-               result = InternalResolveMacros(cmd, macros);
+               result = InternalResolveMacros(cmd, macros, escapeFn);
        } else if (cmd.IsObjectType<Array>()) {
                Array::Ptr resultArr = boost::make_shared<Array>();
                Array::Ptr arr = cmd;
@@ -45,7 +46,8 @@ Value MacroProcessor::ResolveMacros(const Value& cmd, const Dictionary::Ptr& mac
                ObjectLock olock(arr);
 
                BOOST_FOREACH(const Value& arg, arr) {
-                       resultArr->Add(InternalResolveMacros(arg, macros));
+                       /* Note: don't escape macros here. */
+                       resultArr->Add(InternalResolveMacros(arg, macros, EscapeCallback()));
                }
 
                result = resultArr;
@@ -59,7 +61,8 @@ Value MacroProcessor::ResolveMacros(const Value& cmd, const Dictionary::Ptr& mac
 /**
  * @threadsafety Always.
  */
-String MacroProcessor::InternalResolveMacros(const String& str, const Dictionary::Ptr& macros)
+String MacroProcessor::InternalResolveMacros(const String& str, const Dictionary::Ptr& macros,
+    const MacroProcessor::EscapeCallback& escapeFn)
 {
        size_t offset, pos_first, pos_second;
        offset = 0;
index acbd278fc8ce3ef295d7dc86918e88f5a6d07713..7860c8f936132b151826750555db7473e37f382e 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "icinga/i2-icinga.h"
 #include "base/dictionary.h"
+#include <boost/function.hpp>
 #include <vector>
 
 namespace icinga
@@ -35,13 +36,17 @@ namespace icinga
 class I2_ICINGA_API MacroProcessor
 {
 public:
-       static Value ResolveMacros(const Value& str, const Dictionary::Ptr& macros);
+       typedef boost::function<String (const String&)> EscapeCallback;
+
+       static Value ResolveMacros(const Value& str, const Dictionary::Ptr& macros,
+           const EscapeCallback& escapeFn = EscapeCallback());
        static Dictionary::Ptr MergeMacroDicts(const std::vector<Dictionary::Ptr>& macroDicts);
 
 private:
        MacroProcessor(void);
 
-       static String InternalResolveMacros(const String& str, const Dictionary::Ptr& macros);
+       static String InternalResolveMacros(const String& str,
+           const Dictionary::Ptr& macros, const EscapeCallback& escapeFn);
 };
 
 }
index 6d1458f39bc958b43de243377e1d015707ea074b..89405d1d98a82b57ff2b5d8fe1c3d3bfbacbc038 100644 (file)
@@ -48,7 +48,7 @@ void PluginCheckTask::ScriptFunc(const ScriptTask::Ptr& task, const std::vector<
        Dictionary::Ptr macros = arguments[1];
 
        Value raw_command = service->GetCheckCommand();
-       Value command = MacroProcessor::ResolveMacros(raw_command, macros);
+       Value command = MacroProcessor::ResolveMacros(raw_command, macros, Utility::EscapeShellCmd);
 
        Process::Ptr process = boost::make_shared<Process>(Process::SplitCommand(command), macros);
 
index 217df959a0ff8f061f1d57f413573204dac8845b..c15328eb210351759cb7b269c28fd23ea2ee51b9 100644 (file)
@@ -69,7 +69,7 @@ void PluginNotificationTask::ScriptFunc(const ScriptTask::Ptr& task, const std::
 
        Dictionary::Ptr allMacros = MacroProcessor::MergeMacroDicts(macroDicts);
 
-       Value command = MacroProcessor::ResolveMacros(raw_command, allMacros);
+       Value command = MacroProcessor::ResolveMacros(raw_command, allMacros, Utility::EscapeShellCmd);
 
        Process::Ptr process = boost::make_shared<Process>(Process::SplitCommand(command), macros);
 
index 6e3a78f0f29131a72446e6768d3f9715caa9986e..48e8a8eebc9c0917e977adca0e8c05b7e18afeaa 100644 (file)
@@ -8,7 +8,8 @@ check_PROGRAMS = \
 
 icinga2_test_SOURCES = \
        test.cpp \
-       base-dictionary.cpp
+       base-dictionary.cpp \
+       base-shellescape.cpp
 
 icinga2_test_CPPFLAGS = \
        $(BOOST_CPPFLAGS) \
diff --git a/test/base-shellescape.cpp b/test/base-shellescape.cpp
new file mode 100644 (file)
index 0000000..ee147f2
--- /dev/null
@@ -0,0 +1,51 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
+ *                                                                            *
+ * This program is free software; you can redistribute it and/or              *
+ * modify it under the terms of the GNU General Public License                *
+ * as published by the Free Software Foundation; either version 2             *
+ * of the License, or (at your option) any later version.                     *
+ *                                                                            *
+ * This program is distributed in the hope that it will be useful,            *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
+ * GNU General Public License for more details.                               *
+ *                                                                            *
+ * You should have received a copy of the GNU General Public License          *
+ * along with this program; if not, write to the Free Software Foundation     *
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+ ******************************************************************************/
+
+#include "base/utility.h"
+#include <boost/test/unit_test.hpp>
+#include <boost/smart_ptr/make_shared.hpp>
+#include <iostream>
+
+using namespace icinga;
+
+BOOST_AUTO_TEST_SUITE(base_shellescape)
+
+BOOST_AUTO_TEST_CASE(escape_basic)
+{
+#ifdef _WIN32
+       BOOST_CHECK(Utility::EscapeShellCmd("%PATH%") == "^%PATH^%");
+#endif /* _WIN32 */
+
+       BOOST_CHECK(Utility::EscapeShellCmd("$PATH") == "\\$PATH");
+       BOOST_CHECK(Utility::EscapeShellCmd("\\$PATH") == "\\\\\\$PATH");
+
+}
+
+BOOST_AUTO_TEST_CASE(escape_quoted)
+{
+#ifdef _WIN32
+       BOOST_CHECK(Utility::EscapeShellCmd("'hello'") == "\\'hello\\'");
+       BOOST_CHECK(Utility::EscapeShellCmd("\"hello\"") == "\\\"hello\\\"");
+#else /* _WIN32 */
+       BOOST_CHECK(Utility::EscapeShellCmd("'hello'") == "'hello'");
+       BOOST_CHECK(Utility::EscapeShellCmd("'hello") == "\\'hello");
+#endif /* _WIN32 */
+}
+
+BOOST_AUTO_TEST_SUITE_END()