mirror of https://github.com/Icinga/icinga2.git
Shell-escape macros.
This commit is contained in:
parent
a71046ff54
commit
c63684a72f
|
@ -86,6 +86,12 @@ String& String::operator+=(const char *rhs)
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String& String::operator+=(char rhs)
|
||||||
|
{
|
||||||
|
m_Data += rhs;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
bool String::IsEmpty(void) const
|
bool String::IsEmpty(void) const
|
||||||
{
|
{
|
||||||
return m_Data.empty();
|
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);
|
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
|
String String::SubStr(size_t first, size_t len) const
|
||||||
{
|
{
|
||||||
return m_Data.substr(first, len);
|
return m_Data.substr(first, len);
|
||||||
|
|
|
@ -63,6 +63,7 @@ public:
|
||||||
|
|
||||||
String& operator+=(const String& rhs);
|
String& operator+=(const String& rhs);
|
||||||
String& operator+=(const char *rhs);
|
String& operator+=(const char *rhs);
|
||||||
|
String& operator+=(char rhs);
|
||||||
|
|
||||||
bool IsEmpty(void) const;
|
bool IsEmpty(void) const;
|
||||||
|
|
||||||
|
@ -75,6 +76,7 @@ public:
|
||||||
size_t GetLength(void) const;
|
size_t GetLength(void) const;
|
||||||
|
|
||||||
size_t FindFirstOf(const char *s, size_t pos = 0) 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;
|
String SubStr(size_t first, size_t len = NPos) const;
|
||||||
void Replace(size_t first, size_t second, const String& str);
|
void Replace(size_t first, size_t second, const String& str);
|
||||||
|
|
||||||
|
|
|
@ -431,3 +431,49 @@ String Utility::FormatDateTime(const char *format, double ts)
|
||||||
|
|
||||||
return timestamp;
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -75,6 +75,8 @@ public:
|
||||||
|
|
||||||
static void SetNonBlockingSocket(SOCKET s);
|
static void SetNonBlockingSocket(SOCKET s);
|
||||||
|
|
||||||
|
static String EscapeShellCmd(const String& s);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Utility(void);
|
Utility(void);
|
||||||
};
|
};
|
||||||
|
|
|
@ -30,14 +30,15 @@ using namespace icinga;
|
||||||
/**
|
/**
|
||||||
* @threadsafety Always.
|
* @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;
|
Value result;
|
||||||
|
|
||||||
ASSERT(macros->IsSealed());
|
ASSERT(macros->IsSealed());
|
||||||
|
|
||||||
if (cmd.IsScalar()) {
|
if (cmd.IsScalar()) {
|
||||||
result = InternalResolveMacros(cmd, macros);
|
result = InternalResolveMacros(cmd, macros, escapeFn);
|
||||||
} else if (cmd.IsObjectType<Array>()) {
|
} else if (cmd.IsObjectType<Array>()) {
|
||||||
Array::Ptr resultArr = boost::make_shared<Array>();
|
Array::Ptr resultArr = boost::make_shared<Array>();
|
||||||
Array::Ptr arr = cmd;
|
Array::Ptr arr = cmd;
|
||||||
|
@ -45,7 +46,8 @@ Value MacroProcessor::ResolveMacros(const Value& cmd, const Dictionary::Ptr& mac
|
||||||
ObjectLock olock(arr);
|
ObjectLock olock(arr);
|
||||||
|
|
||||||
BOOST_FOREACH(const Value& arg, 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;
|
result = resultArr;
|
||||||
|
@ -59,7 +61,8 @@ Value MacroProcessor::ResolveMacros(const Value& cmd, const Dictionary::Ptr& mac
|
||||||
/**
|
/**
|
||||||
* @threadsafety Always.
|
* @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;
|
size_t offset, pos_first, pos_second;
|
||||||
offset = 0;
|
offset = 0;
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
|
|
||||||
#include "icinga/i2-icinga.h"
|
#include "icinga/i2-icinga.h"
|
||||||
#include "base/dictionary.h"
|
#include "base/dictionary.h"
|
||||||
|
#include <boost/function.hpp>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace icinga
|
namespace icinga
|
||||||
|
@ -35,13 +36,17 @@ namespace icinga
|
||||||
class I2_ICINGA_API MacroProcessor
|
class I2_ICINGA_API MacroProcessor
|
||||||
{
|
{
|
||||||
public:
|
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);
|
static Dictionary::Ptr MergeMacroDicts(const std::vector<Dictionary::Ptr>& macroDicts);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MacroProcessor(void);
|
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);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ void PluginCheckTask::ScriptFunc(const ScriptTask::Ptr& task, const std::vector<
|
||||||
Dictionary::Ptr macros = arguments[1];
|
Dictionary::Ptr macros = arguments[1];
|
||||||
|
|
||||||
Value raw_command = service->GetCheckCommand();
|
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);
|
Process::Ptr process = boost::make_shared<Process>(Process::SplitCommand(command), macros);
|
||||||
|
|
||||||
|
|
|
@ -69,7 +69,7 @@ void PluginNotificationTask::ScriptFunc(const ScriptTask::Ptr& task, const std::
|
||||||
|
|
||||||
Dictionary::Ptr allMacros = MacroProcessor::MergeMacroDicts(macroDicts);
|
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);
|
Process::Ptr process = boost::make_shared<Process>(Process::SplitCommand(command), macros);
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,8 @@ check_PROGRAMS = \
|
||||||
|
|
||||||
icinga2_test_SOURCES = \
|
icinga2_test_SOURCES = \
|
||||||
test.cpp \
|
test.cpp \
|
||||||
base-dictionary.cpp
|
base-dictionary.cpp \
|
||||||
|
base-shellescape.cpp
|
||||||
|
|
||||||
icinga2_test_CPPFLAGS = \
|
icinga2_test_CPPFLAGS = \
|
||||||
$(BOOST_CPPFLAGS) \
|
$(BOOST_CPPFLAGS) \
|
||||||
|
|
|
@ -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()
|
Loading…
Reference in New Issue