2015-08-18 14:21:55 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-01-02 12:06:00 +01:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
2015-08-18 14:21:55 +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/configobjectutility.hpp"
|
2015-08-28 17:58:29 +02:00
|
|
|
#include "remote/configpackageutility.hpp"
|
2016-05-11 13:04:39 +02:00
|
|
|
#include "remote/apilistener.hpp"
|
2015-08-20 16:43:03 +02:00
|
|
|
#include "config/configcompiler.hpp"
|
2015-08-18 14:21:55 +02:00
|
|
|
#include "config/configitem.hpp"
|
2015-10-26 11:05:24 +01:00
|
|
|
#include "base/configwriter.hpp"
|
2015-08-18 14:21:55 +02:00
|
|
|
#include "base/exception.hpp"
|
2015-08-25 16:43:48 +02:00
|
|
|
#include "base/dependencygraph.hpp"
|
2015-08-18 14:21:55 +02:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2018-01-04 18:24:45 +01:00
|
|
|
#include <fstream>
|
2015-08-18 14:21:55 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
String ConfigObjectUtility::GetConfigDir()
|
2015-08-18 14:21:55 +02:00
|
|
|
{
|
2015-08-28 17:58:29 +02:00
|
|
|
return ConfigPackageUtility::GetPackageDir() + "/_api/" +
|
2017-12-19 15:50:05 +01:00
|
|
|
ConfigPackageUtility::GetActiveStage("_api");
|
2015-08-18 14:21:55 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
String ConfigObjectUtility::GetObjectConfigPath(const Type::Ptr& type, const String& fullName)
|
|
|
|
{
|
|
|
|
String typeDir = type->GetPluralName();
|
|
|
|
boost::algorithm::to_lower(typeDir);
|
|
|
|
|
|
|
|
return GetConfigDir() + "/conf.d/" + typeDir +
|
2017-12-19 15:50:05 +01:00
|
|
|
"/" + EscapeName(fullName) + ".conf";
|
2015-08-20 16:43:03 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 14:21:55 +02:00
|
|
|
String ConfigObjectUtility::EscapeName(const String& name)
|
|
|
|
{
|
|
|
|
return Utility::EscapeString(name, "<>:\"/\\|?*", true);
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
String ConfigObjectUtility::CreateObjectConfig(const Type::Ptr& type, const String& fullName,
|
2017-12-19 15:50:05 +01:00
|
|
|
bool ignoreOnError, const Array::Ptr& templates, const Dictionary::Ptr& attrs)
|
2015-08-18 14:21:55 +02:00
|
|
|
{
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *nc = dynamic_cast<NameComposer *>(type.get());
|
2015-08-18 14:21:55 +02:00
|
|
|
Dictionary::Ptr nameParts;
|
|
|
|
String name;
|
|
|
|
|
|
|
|
if (nc) {
|
|
|
|
nameParts = nc->ParseName(fullName);
|
|
|
|
name = nameParts->Get("name");
|
|
|
|
} else
|
|
|
|
name = fullName;
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
Dictionary::Ptr allAttrs = new Dictionary();
|
2015-08-18 14:21:55 +02:00
|
|
|
|
2015-11-11 08:29:48 +01:00
|
|
|
if (attrs) {
|
2015-08-20 16:43:03 +02:00
|
|
|
attrs->CopyTo(allAttrs);
|
2015-08-18 14:21:55 +02:00
|
|
|
|
2015-11-11 08:29:48 +01:00
|
|
|
ObjectLock olock(attrs);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : attrs) {
|
2015-11-12 09:31:27 +01:00
|
|
|
int fid = type->GetFieldId(kv.first.SubStr(0, kv.first.FindFirstOf(".")));
|
2015-11-11 08:29:48 +01:00
|
|
|
|
|
|
|
if (fid < 0)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Invalid attribute specified: " + kv.first));
|
|
|
|
|
|
|
|
Field field = type->GetFieldInfo(fid);
|
|
|
|
|
2015-11-12 09:31:27 +01:00
|
|
|
if (!(field.Attributes & FAConfig) || kv.first == "name")
|
2015-11-11 08:29:48 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Attribute is marked for internal use only and may not be set: " + kv.first));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
if (nameParts)
|
|
|
|
nameParts->CopyTo(allAttrs);
|
|
|
|
|
|
|
|
allAttrs->Remove("name");
|
|
|
|
|
2015-09-30 16:58:18 +02:00
|
|
|
/* update the version for config sync */
|
|
|
|
allAttrs->Set("version", Utility::GetTime());
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
std::ostringstream config;
|
2015-08-20 17:18:48 +02:00
|
|
|
ConfigWriter::EmitConfigItem(config, type->GetName(), name, false, ignoreOnError, templates, allAttrs);
|
2015-08-20 16:43:03 +02:00
|
|
|
ConfigWriter::EmitRaw(config, "\n");
|
|
|
|
|
2015-09-22 17:58:12 +02:00
|
|
|
return config.str();
|
2015-08-20 16:43:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& fullName,
|
2018-04-06 12:26:49 +02:00
|
|
|
const String& config, const Array::Ptr& errors, const Array::Ptr& diagnosticInformation)
|
2015-08-20 16:43:03 +02:00
|
|
|
{
|
2017-09-20 16:40:02 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(ConfigPackageUtility::GetStaticMutex());
|
|
|
|
if (!ConfigPackageUtility::PackageExists("_api")) {
|
|
|
|
ConfigPackageUtility::CreatePackage("_api");
|
2015-08-20 16:43:03 +02:00
|
|
|
|
2017-09-20 16:40:02 +02:00
|
|
|
String stage = ConfigPackageUtility::CreateStage("_api");
|
|
|
|
ConfigPackageUtility::ActivateStage("_api", stage);
|
|
|
|
}
|
2015-08-18 14:21:55 +02:00
|
|
|
}
|
2015-08-20 16:43:03 +02:00
|
|
|
|
2018-04-06 12:32:27 +02:00
|
|
|
ConfigItem::Ptr item = ConfigItem::GetByTypeAndName(type, fullName);
|
|
|
|
|
|
|
|
if (item) {
|
|
|
|
errors->Add("Object '" + fullName + "' already exists.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
String path = GetObjectConfigPath(type, fullName);
|
|
|
|
Utility::MkDirP(Utility::DirName(path), 0700);
|
|
|
|
|
2016-05-09 15:08:35 +02:00
|
|
|
if (Utility::PathExists(path)) {
|
2018-04-06 12:26:49 +02:00
|
|
|
errors->Add("Cannot create object '" + fullName + "'. Configuration file '" + path + "' already exists.");
|
2016-05-09 15:08:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-11-11 10:25:18 +01:00
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
std::ofstream fp(path.CStr(), std::ofstream::out | std::ostream::trunc);
|
|
|
|
fp << config;
|
|
|
|
fp.close();
|
|
|
|
|
2017-12-15 05:34:46 +01:00
|
|
|
std::unique_ptr<Expression> expr = ConfigCompiler::CompileFile(path, String(), "_api");
|
2015-08-20 16:43:03 +02:00
|
|
|
|
2015-08-18 14:21:55 +02:00
|
|
|
try {
|
2015-11-19 19:38:20 +01:00
|
|
|
ActivationScope ascope;
|
|
|
|
|
2018-01-03 10:19:24 +01:00
|
|
|
ScriptFrame frame(true);
|
2015-08-20 16:43:03 +02:00
|
|
|
expr->Evaluate(frame);
|
2017-12-15 05:34:46 +01:00
|
|
|
expr.reset();
|
2015-08-18 14:21:55 +02:00
|
|
|
|
|
|
|
WorkQueue upq;
|
2018-06-15 14:25:03 +02:00
|
|
|
upq.SetName("ConfigObjectUtility::CreateObject");
|
|
|
|
|
2015-11-19 19:38:20 +01:00
|
|
|
std::vector<ConfigItem::Ptr> newItems;
|
2015-08-18 14:21:55 +02:00
|
|
|
|
2015-11-19 19:38:20 +01:00
|
|
|
if (!ConfigItem::CommitItems(ascope.GetContext(), upq, newItems) || !ConfigItem::ActivateItems(upq, newItems, true)) {
|
2015-08-18 14:21:55 +02:00
|
|
|
if (errors) {
|
2016-10-05 14:17:26 +02:00
|
|
|
if (unlink(path.CStr()) < 0 && errno != ENOENT) {
|
2015-11-09 13:35:51 +01:00
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
2017-12-19 15:50:05 +01:00
|
|
|
<< boost::errinfo_api_function("unlink")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(path));
|
2015-11-09 13:35:51 +01:00
|
|
|
}
|
2015-11-09 12:33:37 +01:00
|
|
|
|
2016-08-31 13:43:14 +02:00
|
|
|
for (const boost::exception_ptr& ex : upq.GetExceptions()) {
|
2018-04-06 12:26:49 +02:00
|
|
|
errors->Add(DiagnosticInformation(ex, false));
|
|
|
|
|
|
|
|
if (diagnosticInformation)
|
|
|
|
diagnosticInformation->Add(DiagnosticInformation(ex));
|
2015-08-18 14:21:55 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-20 16:43:03 +02:00
|
|
|
|
2015-08-18 14:21:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-11 13:04:39 +02:00
|
|
|
|
|
|
|
ApiListener::UpdateObjectAuthority();
|
2015-08-18 14:21:55 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2016-10-05 14:17:26 +02:00
|
|
|
if (unlink(path.CStr()) < 0 && errno != ENOENT) {
|
2015-11-09 13:35:51 +01:00
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
2017-12-19 15:50:05 +01:00
|
|
|
<< boost::errinfo_api_function("unlink")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(path));
|
2015-11-09 13:35:51 +01:00
|
|
|
}
|
2015-11-09 12:33:37 +01:00
|
|
|
|
2015-08-18 14:21:55 +02:00
|
|
|
if (errors)
|
2018-04-06 12:26:49 +02:00
|
|
|
errors->Add(DiagnosticInformation(ex, false));
|
|
|
|
|
|
|
|
if (diagnosticInformation)
|
|
|
|
diagnosticInformation->Add(DiagnosticInformation(ex));
|
2015-08-20 16:43:03 +02:00
|
|
|
|
2015-08-18 14:21:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-18 20:25:06 +02:00
|
|
|
|
2015-08-18 14:21:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-08-25 16:43:48 +02:00
|
|
|
|
2018-04-06 12:26:49 +02:00
|
|
|
bool ConfigObjectUtility::DeleteObjectHelper(const ConfigObject::Ptr& object, bool cascade,
|
|
|
|
const Array::Ptr& errors, const Array::Ptr& diagnosticInformation)
|
2015-08-18 14:21:55 +02:00
|
|
|
{
|
2015-08-25 16:43:48 +02:00
|
|
|
std::vector<Object::Ptr> parents = DependencyGraph::GetParents(object);
|
|
|
|
|
2017-03-27 10:45:27 +02:00
|
|
|
Type::Ptr type = object->GetReflectionType();
|
|
|
|
|
2018-04-06 12:26:49 +02:00
|
|
|
String name = object->GetName();
|
|
|
|
|
2015-08-25 16:43:48 +02:00
|
|
|
if (!parents.empty() && !cascade) {
|
2018-04-06 12:26:49 +02:00
|
|
|
if (errors) {
|
|
|
|
errors->Add("Object '" + name + "' of type '" + type->GetName() +
|
2017-12-19 15:50:05 +01:00
|
|
|
"' cannot be deleted because other objects depend on it. "
|
|
|
|
"Use cascading delete to delete it anyway.");
|
2018-04-06 12:26:49 +02:00
|
|
|
}
|
2015-08-25 16:43:48 +02:00
|
|
|
|
2015-08-18 14:21:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Object::Ptr& pobj : parents) {
|
2015-08-25 16:43:48 +02:00
|
|
|
ConfigObject::Ptr parentObj = dynamic_pointer_cast<ConfigObject>(pobj);
|
|
|
|
|
|
|
|
if (!parentObj)
|
|
|
|
continue;
|
|
|
|
|
2018-04-06 12:26:49 +02:00
|
|
|
DeleteObjectHelper(parentObj, cascade, errors, diagnosticInformation);
|
2015-08-25 16:43:48 +02:00
|
|
|
}
|
|
|
|
|
2018-04-06 12:26:49 +02:00
|
|
|
ConfigItem::Ptr item = ConfigItem::GetByTypeAndName(type, name);
|
2015-08-18 14:21:55 +02:00
|
|
|
|
|
|
|
try {
|
2015-09-15 17:56:34 +02:00
|
|
|
/* mark this object for cluster delete event */
|
|
|
|
object->SetExtension("ConfigObjectDeleted", true);
|
|
|
|
/* triggers signal for DB IDO and other interfaces */
|
2015-08-20 17:18:48 +02:00
|
|
|
object->Deactivate(true);
|
2015-08-18 14:21:55 +02:00
|
|
|
|
|
|
|
if (item)
|
|
|
|
item->Unregister();
|
|
|
|
else
|
|
|
|
object->Unregister();
|
|
|
|
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
if (errors)
|
2018-04-06 12:26:49 +02:00
|
|
|
errors->Add(DiagnosticInformation(ex, false));
|
|
|
|
|
|
|
|
if (diagnosticInformation)
|
|
|
|
diagnosticInformation->Add(DiagnosticInformation(ex));
|
2015-08-20 16:43:03 +02:00
|
|
|
|
2015-08-18 14:21:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-25 16:43:48 +02:00
|
|
|
|
2018-04-06 12:26:49 +02:00
|
|
|
String path = GetObjectConfigPath(object->GetReflectionType(), name);
|
2015-08-20 16:43:03 +02:00
|
|
|
|
2015-08-18 14:21:55 +02:00
|
|
|
if (Utility::PathExists(path)) {
|
2016-10-05 14:17:26 +02:00
|
|
|
if (unlink(path.CStr()) < 0 && errno != ENOENT) {
|
2015-08-18 14:21:55 +02:00
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
2017-12-19 15:50:05 +01:00
|
|
|
<< boost::errinfo_api_function("unlink")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(path));
|
2015-08-18 14:21:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-25 16:43:48 +02:00
|
|
|
|
2018-04-06 12:26:49 +02:00
|
|
|
bool ConfigObjectUtility::DeleteObject(const ConfigObject::Ptr& object, bool cascade, const Array::Ptr& errors, const Array::Ptr& diagnosticInformation)
|
2015-08-25 16:43:48 +02:00
|
|
|
{
|
2015-08-28 17:58:29 +02:00
|
|
|
if (object->GetPackage() != "_api") {
|
2015-08-25 16:43:48 +02:00
|
|
|
if (errors)
|
|
|
|
errors->Add("Object cannot be deleted because it was not created using the API.");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-06 12:26:49 +02:00
|
|
|
return DeleteObjectHelper(object, cascade, errors, diagnosticInformation);
|
2015-08-25 16:43:48 +02:00
|
|
|
}
|