]> granicus.if.org Git - icinga2/commitdiff
Fix: Don't shell-escape macros by default.
authorGunnar Beutner <gunnar.beutner@netways.de>
Mon, 9 Sep 2013 11:44:18 +0000 (13:44 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Mon, 9 Sep 2013 11:44:18 +0000 (13:44 +0200)
lib/icinga/command.cpp
lib/icinga/command.h
lib/icinga/icinga-type.conf
lib/icinga/macroprocessor.cpp
lib/icinga/macroprocessor.h
lib/icinga/pluginchecktask.cpp
lib/icinga/plugineventtask.cpp
lib/icinga/pluginnotificationtask.cpp

index 298bfc5b5bad465de1741b6764aaabec7f41ba93..3919e1886dfa71f5a1e8409086d0335e4cbc965e 100644 (file)
@@ -44,6 +44,11 @@ Array::Ptr Command::GetExportMacros(void) const
        return m_ExportMacros;
 }
 
+Array::Ptr Command::GetEscapeMacros(void) const
+{
+       return m_EscapeMacros;
+}
+
 bool Command::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const
 {
        Dictionary::Ptr macros = GetMacros();
@@ -65,6 +70,7 @@ void Command::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes)
                bag->Set("timeout", m_Timeout);
                bag->Set("macros", m_Macros);
                bag->Set("export_macros", m_ExportMacros);
+               bag->Set("escape_macros", m_EscapeMacros);
        }
 }
 
@@ -78,5 +84,6 @@ void Command::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes
                m_Macros = bag->Get("macros");
                m_Macros = bag->Get("macros");
                m_ExportMacros = bag->Get("export_macros");
+               m_EscapeMacros = bag->Get("escape_macros");
        }
 }
index e4fedc260d63aa2457a8f7296264b1270b6bc1b4..ca6e782a64fa2649a3fe9bf31f9480cf48f1117b 100644 (file)
@@ -47,6 +47,7 @@ public:
 
        Dictionary::Ptr GetMacros(void) const;
        Array::Ptr GetExportMacros(void) const;
+       Array::Ptr GetEscapeMacros(void) const;
        virtual bool ResolveMacro(const String& macro, const Dictionary::Ptr& cr, String *result) const;
 
 protected:
@@ -58,6 +59,7 @@ private:
        Value m_Timeout;
        Dictionary::Ptr m_Macros;
        Array::Ptr m_ExportMacros;
+       Array::Ptr m_EscapeMacros;
 };
 
 }
index 3962611f470c5dd5450342e2e6da2a958a09ea95..5685df4b7e65e08b521666a00f6b92dfb83f5f0c 100644 (file)
@@ -368,6 +368,9 @@ type Command {
        %attribute array "export_macros" {
                %attribute string "*"
        },
+       %attribute array "escape_macros" {
+               %attribute string "*"
+       },
        %attribute dictionary "macros" {
            %attribute string "*"
        },
index 26dbd41f78ff4efe3bcf656810b5151ad2079568..dc7409a4f5b8ec7b87d7b1d41c71be13d9b115f6 100644 (file)
 using namespace icinga;
 
 Value MacroProcessor::ResolveMacros(const Value& str, const std::vector<MacroResolver::Ptr>& resolvers,
-    const Dictionary::Ptr& cr, const MacroProcessor::EscapeCallback& escapeFn)
+    const Dictionary::Ptr& cr, const MacroProcessor::EscapeCallback& escapeFn, const Array::Ptr& escapeMacros)
 {
        Value result;
 
        if (str.IsScalar()) {
-               result = InternalResolveMacros(str, resolvers, cr, escapeFn);
+               result = InternalResolveMacros(str, resolvers, cr, escapeFn, escapeMacros);
        } else if (str.IsObjectType<Array>()) {
                Array::Ptr resultArr = boost::make_shared<Array>();
                Array::Ptr arr = str;
@@ -44,7 +44,7 @@ Value MacroProcessor::ResolveMacros(const Value& str, const std::vector<MacroRes
 
                BOOST_FOREACH(const Value& arg, arr) {
                        /* Note: don't escape macros here. */
-                       resultArr->Add(InternalResolveMacros(arg, resolvers, cr, EscapeCallback()));
+                       resultArr->Add(InternalResolveMacros(arg, resolvers, cr, EscapeCallback(), Array::Ptr()));
                }
 
                result = resultArr;
@@ -68,7 +68,7 @@ bool MacroProcessor::ResolveMacro(const String& macro, const std::vector<MacroRe
 
 
 String MacroProcessor::InternalResolveMacros(const String& str, const std::vector<MacroResolver::Ptr>& resolvers,
-    const Dictionary::Ptr& cr, const MacroProcessor::EscapeCallback& escapeFn)
+    const Dictionary::Ptr& cr, const MacroProcessor::EscapeCallback& escapeFn, const Array::Ptr& escapeMacros)
 {
        size_t offset, pos_first, pos_second;
        offset = 0;
@@ -94,8 +94,20 @@ String MacroProcessor::InternalResolveMacros(const String& str, const std::vecto
                if (!found)
                        Log(LogWarning, "icinga", "Macro '" + name + "' is not defined.");
 
-               if (escapeFn)
-                       resolved_macro = escapeFn(resolved_macro);
+               if (escapeFn && escapeMacros) {
+                       bool escape = false;
+
+                       ObjectLock olock(escapeMacros);
+                       BOOST_FOREACH(const String& escapeMacro, escapeMacros) {
+                               if (escapeMacro == name) {
+                                       escape = true;
+                                       break;
+                               }
+                       }
+
+                       if (escape)
+                               resolved_macro = escapeFn(resolved_macro);
+               }
 
                result.Replace(pos_first, pos_second - pos_first + 1, resolved_macro);
                offset = pos_first + resolved_macro.GetLength();
index f6cea05fdc435a061ca3075804c365f71ee255c0..8985fef141937c7edd89162fcf65ab4943a7a5e7 100644 (file)
@@ -23,6 +23,7 @@
 #include "icinga/i2-icinga.h"
 #include "icinga/macroresolver.h"
 #include "base/dictionary.h"
+#include "base/array.h"
 #include <boost/function.hpp>
 #include <vector>
 
@@ -40,7 +41,7 @@ public:
        typedef boost::function<String (const String&)> EscapeCallback;
 
        static Value ResolveMacros(const Value& str, const std::vector<MacroResolver::Ptr>& resolvers,
-           const Dictionary::Ptr& cr, const EscapeCallback& escapeFn = EscapeCallback());
+           const Dictionary::Ptr& cr, const EscapeCallback& escapeFn = EscapeCallback(), const Array::Ptr& escapeMacros = Array::Ptr());
        static bool ResolveMacro(const String& macro, const std::vector<MacroResolver::Ptr>& resolvers,
            const Dictionary::Ptr& cr, String *result);
 
@@ -49,7 +50,7 @@ private:
 
        static String InternalResolveMacros(const String& str,
            const std::vector<MacroResolver::Ptr>& resolvers, const Dictionary::Ptr& cr,
-           const EscapeCallback& escapeFn);
+           const EscapeCallback& escapeFn, const Array::Ptr& escapeMacros);
 };
 
 }
index bc9fe9b91d97be0c3ed23762800df2878e7ec49a..c3fee3a99711df5f269a33d2af562e61d7fb131f 100644 (file)
@@ -46,7 +46,7 @@ Dictionary::Ptr PluginCheckTask::ScriptFunc(const Service::Ptr& service)
        resolvers.push_back(service->GetHost());
        resolvers.push_back(IcingaApplication::GetInstance());
 
-       Value command = MacroProcessor::ResolveMacros(raw_command, resolvers, Dictionary::Ptr(), Utility::EscapeShellCmd);
+       Value command = MacroProcessor::ResolveMacros(raw_command, resolvers, Dictionary::Ptr(), Utility::EscapeShellCmd, commandObj->GetEscapeMacros());
 
        Dictionary::Ptr envMacros = boost::make_shared<Dictionary>();
 
index 0cfc2343ebc15f4e1acb4056b0a630a57ada7ce9..78afaa78d3da694090197b4d68ea84b70a9dcf0f 100644 (file)
@@ -44,7 +44,7 @@ void PluginEventTask::ScriptFunc(const Service::Ptr& service)
        resolvers.push_back(service->GetHost());
        resolvers.push_back(IcingaApplication::GetInstance());
 
-       Value command = MacroProcessor::ResolveMacros(raw_command, resolvers, Dictionary::Ptr(), Utility::EscapeShellCmd);
+       Value command = MacroProcessor::ResolveMacros(raw_command, resolvers, Dictionary::Ptr(), Utility::EscapeShellCmd, commandObj->GetEscapeMacros());
 
        Dictionary::Ptr envMacros = boost::make_shared<Dictionary>();
 
index 6981c8c07f46013b12ab1b64c595ff978e32393e..aec950e487191205204a15ae2f0f6e8776a0b64f 100644 (file)
@@ -56,7 +56,7 @@ void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification, c
        resolvers.push_back(service->GetHost());
        resolvers.push_back(IcingaApplication::GetInstance());
 
-       Value command = MacroProcessor::ResolveMacros(raw_command, resolvers, cr, Utility::EscapeShellCmd);
+       Value command = MacroProcessor::ResolveMacros(raw_command, resolvers, cr, Utility::EscapeShellCmd, commandObj->GetEscapeMacros());
 
        Dictionary::Ptr envMacros = boost::make_shared<Dictionary>();