2013-06-13 11:33:00 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2013-06-13 11:33:00 +02:00
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/command.hpp"
|
2015-01-21 08:47:45 +01:00
|
|
|
#include "base/function.hpp"
|
2014-12-18 15:43:01 +01:00
|
|
|
#include "base/exception.hpp"
|
2015-02-11 13:12:08 +01:00
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include <boost/foreach.hpp>
|
2013-06-13 11:33:00 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-11-08 16:07:21 +01:00
|
|
|
REGISTER_TYPE(Command);
|
2014-07-16 12:58:18 +02:00
|
|
|
REGISTER_SCRIPTFUNCTION(ValidateCommandAttributes, &Command::ValidateAttributes);
|
2015-02-11 13:12:08 +01:00
|
|
|
REGISTER_SCRIPTFUNCTION(ValidateCommandArguments, &Command::ValidateArguments);
|
|
|
|
REGISTER_SCRIPTFUNCTION(ValidateEnvironmentVariables, &Command::ValidateEnvironmentVariables);
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2014-04-17 15:20:28 +02:00
|
|
|
int Command::GetModifiedAttributes(void) const
|
|
|
|
{
|
|
|
|
int attrs = 0;
|
|
|
|
|
2014-11-07 21:40:47 +01:00
|
|
|
if (GetOverrideVars())
|
2014-04-17 15:20:28 +02:00
|
|
|
attrs |= ModAttrCustomVariable;
|
|
|
|
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
2014-05-10 17:24:39 +02:00
|
|
|
void Command::SetModifiedAttributes(int flags, const MessageOrigin& origin)
|
2014-04-17 15:20:28 +02:00
|
|
|
{
|
|
|
|
if ((flags & ModAttrCustomVariable) == 0) {
|
|
|
|
SetOverrideVars(Empty);
|
2014-11-08 21:17:16 +01:00
|
|
|
OnVarsChanged(this, GetVars(), origin);
|
2014-04-17 15:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-16 12:58:18 +02:00
|
|
|
|
2014-11-20 13:28:21 +01:00
|
|
|
void Command::ValidateAttributes(const String& location, const Command::Ptr& object)
|
2014-07-16 12:58:18 +02:00
|
|
|
{
|
2014-11-20 13:28:21 +01:00
|
|
|
if (object->GetArguments() != Empty && !object->GetCommandLine().IsObjectType<Array>()) {
|
2014-12-18 15:11:57 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
|
2014-12-18 15:43:01 +01:00
|
|
|
location + ": Attribute 'command' must be an array if the 'arguments' attribute is set.", object->GetDebugInfo()));
|
2014-07-16 12:58:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 13:12:08 +01:00
|
|
|
void Command::ValidateArguments(const String& location, const Command::Ptr& object)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr arguments = object->GetArguments();
|
|
|
|
|
|
|
|
if (!arguments)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ObjectLock olock(arguments);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, arguments) {
|
|
|
|
const Value& arginfo = kv.second;
|
|
|
|
Value argval;
|
|
|
|
|
|
|
|
if (arginfo.IsObjectType<Dictionary>()) {
|
|
|
|
Dictionary::Ptr argdict = arginfo;
|
|
|
|
|
|
|
|
if (argdict->Contains("value"))
|
|
|
|
argval = argdict->Get("value");
|
|
|
|
} else
|
|
|
|
argval = arginfo;
|
|
|
|
|
|
|
|
if (argval.IsEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
String argstr = argval;
|
|
|
|
|
2015-02-11 15:47:45 +01:00
|
|
|
if (!Utility::ValidateMacroString(argstr)) {
|
2015-02-11 13:12:08 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
|
|
|
|
location + ": Closing $ not found in macro format string '" + argstr + "'.", object->GetDebugInfo()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Command::ValidateEnvironmentVariables(const String& location, const Command::Ptr& object)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr env = object->GetEnv();
|
|
|
|
|
|
|
|
if (!env)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ObjectLock olock(env);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, env) {
|
|
|
|
const Value& envval = kv.second;
|
|
|
|
|
|
|
|
if (!envval.IsString() || envval.IsEmpty())
|
|
|
|
continue;
|
|
|
|
|
2015-02-11 15:47:45 +01:00
|
|
|
if (!Utility::ValidateMacroString(envval)) {
|
2015-02-11 13:12:08 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
|
|
|
|
location + ": Closing $ not found in macro format string '" + envval + "'.", object->GetDebugInfo()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|