return *this;
}
+String& String::operator+=(char rhs)
+{
+ m_Data += rhs;
+ return *this;
+}
+
bool String::IsEmpty(void) const
{
return m_Data.empty();
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);
String& operator+=(const String& rhs);
String& operator+=(const char *rhs);
+ String& operator+=(char rhs);
bool IsEmpty(void) const;
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);
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;
+}
static void SetNonBlockingSocket(SOCKET s);
+ static String EscapeShellCmd(const String& s);
+
private:
Utility(void);
};
/**
* @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;
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;
/**
* @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;
#include "icinga/i2-icinga.h"
#include "base/dictionary.h"
+#include <boost/function.hpp>
#include <vector>
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);
};
}
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);
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);
icinga2_test_SOURCES = \
test.cpp \
- base-dictionary.cpp
+ base-dictionary.cpp \
+ base-shellescape.cpp
icinga2_test_CPPFLAGS = \
$(BOOST_CPPFLAGS) \
--- /dev/null
+/******************************************************************************
+ * 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()