2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-12-11 15:12:34 -05:00
|
|
|
|
2014-12-12 09:33:02 -05:00
|
|
|
#include "base/scriptframe.hpp"
|
2015-03-28 19:03:47 -04:00
|
|
|
#include "base/scriptglobal.hpp"
|
2018-08-07 07:55:41 -04:00
|
|
|
#include "base/namespace.hpp"
|
2015-09-23 03:21:45 -04:00
|
|
|
#include "base/exception.hpp"
|
2018-08-09 09:37:23 -04:00
|
|
|
#include "base/configuration.hpp"
|
2014-12-11 15:12:34 -05:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2015-03-29 16:26:07 -04:00
|
|
|
boost::thread_specific_ptr<std::stack<ScriptFrame *> > ScriptFrame::m_ScriptFrames;
|
2018-08-07 07:55:41 -04:00
|
|
|
|
|
|
|
|
static auto l_InternalNSBehavior = new ConstNamespaceBehavior();
|
2014-12-22 08:14:16 -05:00
|
|
|
|
2018-09-04 09:17:34 -04:00
|
|
|
/* Ensure that this gets called with highest priority
|
|
|
|
|
* and wins against other static initializers in lib/icinga, etc.
|
|
|
|
|
* LTO-enabled builds will cause trouble otherwise, see GH #6575.
|
|
|
|
|
*/
|
2016-08-27 03:35:08 -04:00
|
|
|
INITIALIZE_ONCE_WITH_PRIORITY([]() {
|
2018-08-07 07:55:41 -04:00
|
|
|
Namespace::Ptr globalNS = ScriptGlobal::GetGlobals();
|
|
|
|
|
|
|
|
|
|
auto systemNSBehavior = new ConstNamespaceBehavior();
|
|
|
|
|
systemNSBehavior->Freeze();
|
|
|
|
|
Namespace::Ptr systemNS = new Namespace(systemNSBehavior);
|
2019-07-26 08:45:11 -04:00
|
|
|
globalNS->SetAttribute("System", new ConstEmbeddedNamespaceValue(systemNS));
|
2016-08-12 09:36:47 -04:00
|
|
|
|
2019-07-26 08:45:11 -04:00
|
|
|
systemNS->SetAttribute("Configuration", new EmbeddedNamespaceValue(new Configuration()));
|
2018-08-09 09:37:23 -04:00
|
|
|
|
2018-08-07 07:55:41 -04:00
|
|
|
auto typesNSBehavior = new ConstNamespaceBehavior();
|
|
|
|
|
typesNSBehavior->Freeze();
|
|
|
|
|
Namespace::Ptr typesNS = new Namespace(typesNSBehavior);
|
2019-07-26 08:45:11 -04:00
|
|
|
globalNS->SetAttribute("Types", new ConstEmbeddedNamespaceValue(typesNS));
|
2016-08-12 10:53:44 -04:00
|
|
|
|
2018-08-07 07:55:41 -04:00
|
|
|
auto statsNSBehavior = new ConstNamespaceBehavior();
|
|
|
|
|
statsNSBehavior->Freeze();
|
|
|
|
|
Namespace::Ptr statsNS = new Namespace(statsNSBehavior);
|
2019-07-26 08:45:11 -04:00
|
|
|
globalNS->SetAttribute("StatsFunctions", new ConstEmbeddedNamespaceValue(statsNS));
|
2018-08-07 07:55:41 -04:00
|
|
|
|
|
|
|
|
Namespace::Ptr internalNS = new Namespace(l_InternalNSBehavior);
|
2019-07-26 08:45:11 -04:00
|
|
|
globalNS->SetAttribute("Internal", new ConstEmbeddedNamespaceValue(internalNS));
|
2018-09-04 09:17:34 -04:00
|
|
|
}, 1000);
|
2016-08-12 09:36:47 -04:00
|
|
|
|
2018-08-07 07:55:41 -04:00
|
|
|
INITIALIZE_ONCE_WITH_PRIORITY([]() {
|
|
|
|
|
l_InternalNSBehavior->Freeze();
|
|
|
|
|
}, 0);
|
|
|
|
|
|
2025-09-22 06:31:02 -04:00
|
|
|
/**
|
|
|
|
|
* Construct a @c ScriptFrame that has `Self` assigned to the global namespace.
|
|
|
|
|
*
|
|
|
|
|
* Prefer the other constructor if possible since if misused this may leak global variables
|
|
|
|
|
* without permissions or senstive variables like TicketSalt in a sandboxed context.
|
|
|
|
|
*
|
|
|
|
|
* @todo Remove this constructor and call the other with the global namespace in places where it's actually necessary.
|
|
|
|
|
*/
|
2017-12-18 04:30:20 -05:00
|
|
|
ScriptFrame::ScriptFrame(bool allocLocals)
|
2025-09-22 06:31:02 -04:00
|
|
|
: Locals(allocLocals ? new Dictionary() : nullptr), PermChecker(new ScriptPermissionChecker),
|
|
|
|
|
Self(ScriptGlobal::GetGlobals()), Sandboxed(false), Depth(0), Globals(nullptr)
|
2016-08-12 05:42:59 -04:00
|
|
|
{
|
|
|
|
|
InitializeFrame();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-04 02:54:18 -05:00
|
|
|
ScriptFrame::ScriptFrame(bool allocLocals, Value self)
|
2025-09-22 06:31:02 -04:00
|
|
|
: Locals(allocLocals ? new Dictionary() : nullptr), PermChecker(new ScriptPermissionChecker), Self(std::move(self)),
|
|
|
|
|
Sandboxed(false), Depth(0), Globals(nullptr)
|
2016-08-12 05:42:59 -04:00
|
|
|
{
|
|
|
|
|
InitializeFrame();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void ScriptFrame::InitializeFrame()
|
2014-12-22 08:14:16 -05:00
|
|
|
{
|
2016-07-29 08:11:52 -04:00
|
|
|
std::stack<ScriptFrame *> *frames = m_ScriptFrames.get();
|
|
|
|
|
|
2016-08-12 05:25:36 -04:00
|
|
|
if (frames && !frames->empty()) {
|
|
|
|
|
ScriptFrame *frame = frames->top();
|
|
|
|
|
|
2025-09-22 06:31:02 -04:00
|
|
|
// See the documentation of `ScriptFrame::Globals` for why these two are inherited and Globals isn't.
|
2025-09-22 06:25:26 -04:00
|
|
|
PermChecker = frame->PermChecker;
|
2016-08-12 05:25:36 -04:00
|
|
|
Sandboxed = frame->Sandboxed;
|
|
|
|
|
}
|
2016-07-29 08:11:52 -04:00
|
|
|
|
2015-03-29 16:26:07 -04:00
|
|
|
PushFrame(this);
|
2014-12-22 08:14:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
ScriptFrame::~ScriptFrame()
|
2014-12-22 08:14:16 -05:00
|
|
|
{
|
2015-03-29 16:26:07 -04:00
|
|
|
ScriptFrame *frame = PopFrame();
|
|
|
|
|
ASSERT(frame == this);
|
2019-03-08 08:07:29 -05:00
|
|
|
|
|
|
|
|
#ifndef I2_DEBUG
|
|
|
|
|
(void)frame;
|
|
|
|
|
#endif /* I2_DEBUG */
|
2014-12-22 08:14:16 -05:00
|
|
|
}
|
|
|
|
|
|
2025-09-22 06:31:02 -04:00
|
|
|
/**
|
|
|
|
|
* Returns a sanitized copy of the global variables namespace when sandboxed.
|
|
|
|
|
*
|
|
|
|
|
* This filters out the TicketSalt variable specifically and any variable for which the
|
|
|
|
|
* PermChecker does not return 'true'.
|
|
|
|
|
*
|
|
|
|
|
* However it specifically keeps the Types, System, and Icinga sub-namespaces, because they're
|
|
|
|
|
* accessed through globals in ScopeExpression and the user should have access to all Values
|
|
|
|
|
* contained in these namespaces.
|
|
|
|
|
*
|
|
|
|
|
* @return a sanitized copy of the global namespace if sandboxed, a pointer to the global namespace otherwise.
|
|
|
|
|
*/
|
|
|
|
|
Namespace::Ptr ScriptFrame::GetGlobals()
|
|
|
|
|
{
|
|
|
|
|
if (Sandboxed) {
|
|
|
|
|
if (!Globals) {
|
|
|
|
|
Globals = new Namespace;
|
|
|
|
|
auto globals = ScriptGlobal::GetGlobals();
|
|
|
|
|
ObjectLock lock{globals};
|
|
|
|
|
for (auto& kv : globals) {
|
|
|
|
|
if (kv.first == "TicketSalt") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (kv.first == "Types" || kv.first == "System" || kv.first == "Icinga" || PermChecker->CanAccessGlobalVariable(kv.first)) {
|
|
|
|
|
Globals->Set(kv.first, kv.second->Get());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Globals;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ScriptGlobal::GetGlobals();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void ScriptFrame::IncreaseStackDepth()
|
2016-03-23 03:40:32 -04:00
|
|
|
{
|
|
|
|
|
if (Depth + 1 > 300)
|
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Stack overflow while evaluating expression: Recursion level too deep."));
|
|
|
|
|
|
|
|
|
|
Depth++;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void ScriptFrame::DecreaseStackDepth()
|
2016-03-23 03:40:32 -04:00
|
|
|
{
|
|
|
|
|
Depth--;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
ScriptFrame *ScriptFrame::GetCurrentFrame()
|
2014-12-22 08:14:16 -05:00
|
|
|
{
|
2015-03-29 16:26:07 -04:00
|
|
|
std::stack<ScriptFrame *> *frames = m_ScriptFrames.get();
|
2014-12-22 08:14:16 -05:00
|
|
|
|
2015-03-29 16:26:07 -04:00
|
|
|
ASSERT(!frames->empty());
|
|
|
|
|
return frames->top();
|
2014-12-22 08:14:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
ScriptFrame *ScriptFrame::PopFrame()
|
2014-12-22 08:14:16 -05:00
|
|
|
{
|
2015-03-29 16:26:07 -04:00
|
|
|
std::stack<ScriptFrame *> *frames = m_ScriptFrames.get();
|
|
|
|
|
|
|
|
|
|
ASSERT(!frames->empty());
|
|
|
|
|
|
|
|
|
|
ScriptFrame *frame = frames->top();
|
|
|
|
|
frames->pop();
|
|
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptFrame::PushFrame(ScriptFrame *frame)
|
|
|
|
|
{
|
|
|
|
|
std::stack<ScriptFrame *> *frames = m_ScriptFrames.get();
|
|
|
|
|
|
|
|
|
|
if (!frames) {
|
|
|
|
|
frames = new std::stack<ScriptFrame *>();
|
|
|
|
|
m_ScriptFrames.reset(frames);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-23 03:40:32 -04:00
|
|
|
if (!frames->empty()) {
|
|
|
|
|
ScriptFrame *parent = frames->top();
|
|
|
|
|
frame->Depth += parent->Depth;
|
|
|
|
|
}
|
2015-09-23 03:21:45 -04:00
|
|
|
|
2015-03-29 16:26:07 -04:00
|
|
|
frames->push(frame);
|
2015-03-28 19:03:47 -04:00
|
|
|
}
|