2015-07-28 13:57:59 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2016-01-12 08:29:59 +01:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2015-07-28 13:57:59 +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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "remote/filterutility.hpp"
|
2015-07-29 13:39:58 +02:00
|
|
|
#include "remote/httputility.hpp"
|
2015-07-28 13:57:59 +02:00
|
|
|
#include "config/configcompiler.hpp"
|
|
|
|
#include "config/expression.hpp"
|
|
|
|
#include "base/json.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2015-07-29 13:39:58 +02:00
|
|
|
#include "base/logger.hpp"
|
2015-07-28 13:57:59 +02:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
Type::Ptr FilterUtility::TypeFromPluralName(const String& pluralName)
|
|
|
|
{
|
|
|
|
String uname = pluralName;
|
|
|
|
boost::algorithm::to_lower(uname);
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Type::Ptr&type : Type::GetAllTypes()) {
|
2016-08-12 16:53:44 +02:00
|
|
|
String pname = type->GetPluralName();
|
|
|
|
boost::algorithm::to_lower(pname);
|
2015-07-28 13:57:59 +02:00
|
|
|
|
2016-08-12 16:53:44 +02:00
|
|
|
if (uname == pname)
|
|
|
|
return type;
|
2015-07-28 13:57:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Type::Ptr();
|
|
|
|
}
|
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
void ConfigObjectTargetProvider::FindTargets(const String& type, const boost::function<void (const Value&)>& addTarget) const
|
2015-07-28 13:57:59 +02:00
|
|
|
{
|
2016-08-16 11:02:10 +02:00
|
|
|
Type::Ptr ptype = Type::GetByName(type);
|
|
|
|
ConfigType *ctype = dynamic_cast<ConfigType *>(ptype.get());
|
2015-07-28 13:57:59 +02:00
|
|
|
|
2016-08-16 11:02:10 +02:00
|
|
|
if (ctype) {
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const ConfigObject::Ptr& object : ctype->GetObjects()) {
|
2015-09-30 13:26:19 +02:00
|
|
|
addTarget(object);
|
|
|
|
}
|
2015-08-26 10:58:59 +02:00
|
|
|
}
|
2015-07-28 13:57:59 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
Value ConfigObjectTargetProvider::GetTargetByName(const String& type, const String& name) const
|
2015-07-28 13:57:59 +02:00
|
|
|
{
|
2015-08-26 10:58:59 +02:00
|
|
|
ConfigObject::Ptr obj = ConfigObject::GetObject(type, name);
|
|
|
|
|
|
|
|
if (!obj)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Object does not exist."));
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConfigObjectTargetProvider::IsValidType(const String& type) const
|
|
|
|
{
|
2015-09-30 13:26:19 +02:00
|
|
|
Type::Ptr ptype = Type::GetByName(type);
|
|
|
|
|
|
|
|
if (!ptype)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ConfigObject::TypeInstance->IsAssignableFrom(ptype);
|
2015-08-26 10:58:59 +02:00
|
|
|
}
|
2015-07-28 13:57:59 +02:00
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
String ConfigObjectTargetProvider::GetPluralName(const String& type) const
|
|
|
|
{
|
|
|
|
return Type::GetByName(type)->GetPluralName();
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:31:18 +02:00
|
|
|
bool FilterUtility::EvaluateFilter(ScriptFrame& frame, Expression *filter,
|
|
|
|
const Object::Ptr& target, const String& variableName)
|
2015-08-26 10:58:59 +02:00
|
|
|
{
|
2015-09-28 08:57:25 +02:00
|
|
|
if (!filter)
|
|
|
|
return true;
|
|
|
|
|
2015-09-22 09:42:30 +02:00
|
|
|
Type::Ptr type = target->GetReflectionType();
|
2015-10-19 17:31:18 +02:00
|
|
|
String varName;
|
|
|
|
|
|
|
|
if (variableName.IsEmpty())
|
|
|
|
varName = type->GetName().ToLower();
|
|
|
|
else
|
|
|
|
varName = variableName;
|
2015-09-28 08:57:25 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr vars;
|
|
|
|
|
|
|
|
if (frame.Self.IsEmpty()) {
|
|
|
|
vars = new Dictionary();
|
|
|
|
frame.Self = vars;
|
|
|
|
} else
|
|
|
|
vars = frame.Self;
|
2015-09-22 09:42:30 +02:00
|
|
|
|
2015-11-07 10:01:07 +01:00
|
|
|
vars->Set("obj", target);
|
2015-09-28 08:57:25 +02:00
|
|
|
vars->Set(varName, target);
|
2015-08-26 10:58:59 +02:00
|
|
|
|
2015-09-22 09:42:30 +02:00
|
|
|
for (int fid = 0; fid < type->GetFieldCount(); fid++) {
|
|
|
|
Field field = type->GetFieldInfo(fid);
|
|
|
|
|
|
|
|
if ((field.Attributes & FANavigation) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Object::Ptr joinedObj = target->NavigateField(fid);
|
|
|
|
|
2016-05-12 10:24:15 +02:00
|
|
|
if (field.NavigationName)
|
|
|
|
vars->Set(field.NavigationName, joinedObj);
|
|
|
|
else
|
|
|
|
vars->Set(field.Name, joinedObj);
|
2015-09-22 09:42:30 +02:00
|
|
|
}
|
|
|
|
|
2015-09-28 08:57:25 +02:00
|
|
|
return Convert::ToBool(filter->Evaluate(frame));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FilteredAddTarget(ScriptFrame& permissionFrame, Expression *permissionFilter,
|
2016-06-16 08:28:57 +02:00
|
|
|
ScriptFrame& frame, Expression *ufilter, std::vector<Value>& result, const String& variableName, const Object::Ptr& target)
|
2015-09-28 08:57:25 +02:00
|
|
|
{
|
2016-06-16 08:28:57 +02:00
|
|
|
if (FilterUtility::EvaluateFilter(permissionFrame, permissionFilter, target, variableName) && FilterUtility::EvaluateFilter(frame, ufilter, target, variableName))
|
2015-08-26 10:58:59 +02:00
|
|
|
result.push_back(target);
|
|
|
|
}
|
|
|
|
|
2015-09-28 08:57:25 +02:00
|
|
|
void FilterUtility::CheckPermission(const ApiUser::Ptr& user, const String& permission, Expression **permissionFilter)
|
|
|
|
{
|
|
|
|
if (permissionFilter)
|
|
|
|
*permissionFilter = NULL;
|
|
|
|
|
|
|
|
if (permission.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool foundPermission = false;
|
|
|
|
String requiredPermission = permission.ToLower();
|
|
|
|
|
|
|
|
Array::Ptr permissions = user->GetPermissions();
|
|
|
|
if (permissions) {
|
|
|
|
ObjectLock olock(permissions);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& item : permissions) {
|
2015-09-28 08:57:25 +02:00
|
|
|
String permission;
|
|
|
|
Function::Ptr filter;
|
|
|
|
if (item.IsObjectType<Dictionary>()) {
|
|
|
|
Dictionary::Ptr dict = item;
|
|
|
|
permission = dict->Get("permission");
|
|
|
|
filter = dict->Get("filter");
|
|
|
|
} else
|
|
|
|
permission = item;
|
|
|
|
|
|
|
|
permission = permission.ToLower();
|
|
|
|
|
|
|
|
if (!Utility::Match(permission, requiredPermission))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
foundPermission = true;
|
|
|
|
|
|
|
|
if (filter && permissionFilter) {
|
|
|
|
std::vector<Expression *> args;
|
|
|
|
args.push_back(new GetScopeExpression(ScopeLocal));
|
|
|
|
FunctionCallExpression *fexpr = new FunctionCallExpression(new IndexerExpression(MakeLiteral(filter), MakeLiteral("call")), args);
|
|
|
|
|
|
|
|
if (!*permissionFilter)
|
|
|
|
*permissionFilter = fexpr;
|
|
|
|
else
|
|
|
|
*permissionFilter = new LogicalOrExpression(*permissionFilter, fexpr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundPermission)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Missing permission: " + requiredPermission));
|
|
|
|
}
|
|
|
|
|
2016-06-16 08:28:57 +02:00
|
|
|
std::vector<Value> FilterUtility::GetFilterTargets(const QueryDescription& qd, const Dictionary::Ptr& query, const ApiUser::Ptr& user, const String& variableName)
|
2015-08-26 10:58:59 +02:00
|
|
|
{
|
|
|
|
std::vector<Value> result;
|
|
|
|
|
|
|
|
TargetProvider::Ptr provider;
|
|
|
|
|
|
|
|
if (qd.Provider)
|
|
|
|
provider = qd.Provider;
|
|
|
|
else
|
|
|
|
provider = new ConfigObjectTargetProvider();
|
|
|
|
|
2015-09-28 08:57:25 +02:00
|
|
|
Expression *permissionFilter;
|
|
|
|
CheckPermission(user, qd.Permission, &permissionFilter);
|
|
|
|
|
|
|
|
ScriptFrame permissionFrame;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& type : qd.Types) {
|
2015-08-26 10:58:59 +02:00
|
|
|
String attr = type;
|
2015-07-28 13:57:59 +02:00
|
|
|
boost::algorithm::to_lower(attr);
|
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
if (attr == "type")
|
|
|
|
attr = "name";
|
|
|
|
|
2015-09-28 08:57:25 +02:00
|
|
|
if (query->Contains(attr)) {
|
2016-06-13 08:52:03 +02:00
|
|
|
String name = HttpUtility::GetLastParameter(query, attr);
|
|
|
|
Object::Ptr target = provider->GetTargetByName(type, name);
|
2015-09-28 08:57:25 +02:00
|
|
|
|
2016-06-16 08:28:57 +02:00
|
|
|
if (!FilterUtility::EvaluateFilter(permissionFrame, permissionFilter, target, variableName))
|
2016-06-13 08:52:03 +02:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Access denied to object '" + name + "' of type '" + type + "'"));
|
|
|
|
|
|
|
|
result.push_back(target);
|
2015-09-28 08:57:25 +02:00
|
|
|
}
|
2015-07-28 13:57:59 +02:00
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
attr = provider->GetPluralName(type);
|
2015-07-28 13:57:59 +02:00
|
|
|
boost::algorithm::to_lower(attr);
|
|
|
|
|
|
|
|
if (query->Contains(attr)) {
|
|
|
|
Array::Ptr names = query->Get(attr);
|
2015-07-30 08:30:17 +02:00
|
|
|
if (names) {
|
|
|
|
ObjectLock olock(names);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& name : names) {
|
2015-09-28 08:57:25 +02:00
|
|
|
Object::Ptr target = provider->GetTargetByName(type, name);
|
|
|
|
|
2016-06-16 08:28:57 +02:00
|
|
|
if (!FilterUtility::EvaluateFilter(permissionFrame, permissionFilter, target, variableName))
|
2016-06-13 08:52:03 +02:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Access denied to object '" + name + "' of type '" + type + "'"));
|
|
|
|
|
|
|
|
result.push_back(target);
|
2015-07-30 08:30:17 +02:00
|
|
|
}
|
2015-07-28 13:57:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 08:30:17 +02:00
|
|
|
if (query->Contains("filter") || result.empty()) {
|
2015-07-28 13:57:59 +02:00
|
|
|
if (!query->Contains("type"))
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Type must be specified when using a filter."));
|
|
|
|
|
2015-07-29 13:39:58 +02:00
|
|
|
String type = HttpUtility::GetLastParameter(query, "type");
|
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
if (!provider->IsValidType(type))
|
2015-07-28 13:57:59 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid type specified."));
|
|
|
|
|
2015-08-26 10:58:59 +02:00
|
|
|
if (qd.Types.find(type) == qd.Types.end())
|
2015-07-28 13:57:59 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid type specified for this query."));
|
|
|
|
|
|
|
|
ScriptFrame frame;
|
|
|
|
frame.Sandboxed = true;
|
2015-09-28 08:57:25 +02:00
|
|
|
Dictionary::Ptr uvars = new Dictionary();
|
|
|
|
|
2015-10-14 16:52:01 +02:00
|
|
|
Expression *ufilter = NULL;
|
|
|
|
|
|
|
|
if (query->Contains("filter")) {
|
|
|
|
String filter = HttpUtility::GetLastParameter(query, "filter");
|
|
|
|
ufilter = ConfigCompiler::CompileText("<API query>", filter);
|
|
|
|
}
|
2015-07-28 13:57:59 +02:00
|
|
|
|
2015-08-18 06:47:07 +02:00
|
|
|
Dictionary::Ptr filter_vars = query->Get("filter_vars");
|
|
|
|
if (filter_vars) {
|
|
|
|
ObjectLock olock(filter_vars);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : filter_vars) {
|
2015-09-28 08:57:25 +02:00
|
|
|
uvars->Set(kv.first, kv.second);
|
2015-08-18 06:47:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-28 08:57:25 +02:00
|
|
|
frame.Self = uvars;
|
|
|
|
|
2015-07-28 13:57:59 +02:00
|
|
|
try {
|
2015-09-28 08:57:25 +02:00
|
|
|
provider->FindTargets(type, boost::bind(&FilteredAddTarget,
|
|
|
|
boost::ref(permissionFrame), permissionFilter,
|
2016-06-16 08:28:57 +02:00
|
|
|
boost::ref(frame), ufilter, boost::ref(result), variableName, _1));
|
2015-07-28 13:57:59 +02:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
delete ufilter;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete ufilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2015-09-22 17:58:12 +02:00
|
|
|
|