]> granicus.if.org Git - icinga2/blob - lib/config/vmops.hpp
Verify array bounds
[icinga2] / lib / config / vmops.hpp
1 /******************************************************************************
2 * Icinga 2                                                                   *
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4 *                                                                            *
5 * This program is free software; you can redistribute it and/or              *
6 * modify it under the terms of the GNU General Public License                *
7 * as published by the Free Software Foundation; either version 2             *
8 * of the License, or (at your option) any later version.                     *
9 *                                                                            *
10 * This program is distributed in the hope that it will be useful,            *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13 * GNU General Public License for more details.                               *
14 *                                                                            *
15 * You should have received a copy of the GNU General Public License          *
16 * along with this program; if not, write to the Free Software Foundation     *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18 ******************************************************************************/
19
20 #ifndef VMOPS_H
21 #define VMOPS_H
22
23 #include "config/i2-config.hpp"
24 #include "config/expression.hpp"
25 #include "config/configitembuilder.hpp"
26 #include "config/applyrule.hpp"
27 #include "config/objectrule.hpp"
28 #include "base/debuginfo.hpp"
29 #include "base/array.hpp"
30 #include "base/dictionary.hpp"
31 #include "base/scriptfunction.hpp"
32 #include "base/scriptglobal.hpp"
33 #include "base/exception.hpp"
34 #include "base/convert.hpp"
35 #include "base/objectlock.hpp"
36 #include <boost/foreach.hpp>
37 #include <boost/smart_ptr/make_shared.hpp>
38 #include <map>
39 #include <vector>
40
41 namespace icinga
42 {
43
44 class VMOps
45 {
46 public:
47         static inline Value Variable(ScriptFrame& frame, const String& name, const DebugInfo& debugInfo = DebugInfo())
48         {
49                 if (frame.Locals && frame.Locals->Contains(name))
50                         return frame.Locals->Get(name);
51                 else if (frame.Self.IsObject() && frame.Locals != static_cast<Object::Ptr>(frame.Self) && HasField(frame.Self, name))
52                         return GetField(frame.Self, name, debugInfo);
53                 else
54                         return ScriptGlobal::Get(name);
55         }
56
57         static inline Value FunctionCall(ScriptFrame& frame, const Value& self, const ScriptFunction::Ptr& func, const std::vector<Value>& arguments)
58         {
59                 boost::shared_ptr<ScriptFrame> vframe;
60
61                 if (!self.IsEmpty())
62                         vframe = boost::make_shared<ScriptFrame>(self); /* passes self to the callee using a TLS variable */
63                 else
64                         vframe = boost::make_shared<ScriptFrame>();
65
66                 return func->Invoke(arguments);
67         }
68
69         static inline Value NewFunction(ScriptFrame& frame, const std::vector<String>& args,
70             std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression)
71         {
72                 return new ScriptFunction(boost::bind(&FunctionWrapper, _1, args,
73                     EvaluateClosedVars(frame, closedVars), expression));
74         }
75
76         static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const boost::shared_ptr<Expression>& filter,
77                 const String& fkvar, const String& fvvar, const boost::shared_ptr<Expression>& fterm, std::map<String, Expression *> *closedVars,
78                 const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
79         {
80                 ApplyRule::AddRule(type, target, name, expression, filter, fkvar,
81                     fvvar, fterm, debugInfo, EvaluateClosedVars(frame, closedVars));
82
83                 return Empty;
84         }
85
86         static inline Value NewObject(ScriptFrame& frame, bool abstract, const String& type, const String& name, const boost::shared_ptr<Expression>& filter,
87                 const String& zone, std::map<String, Expression *> *closedVars, const boost::shared_ptr<Expression>& expression, const DebugInfo& debugInfo = DebugInfo())
88         {
89                 ConfigItemBuilder::Ptr item = new ConfigItemBuilder(debugInfo);
90
91                 String checkName = name;
92
93                 if (!abstract) {
94                         Type::Ptr ptype = Type::GetByName(type);
95
96                         NameComposer *nc = dynamic_cast<NameComposer *>(ptype.get());
97
98                         if (nc)
99                                 checkName = nc->MakeName(name, Dictionary::Ptr());
100                 }
101
102                 if (!checkName.IsEmpty()) {
103                         ConfigItem::Ptr oldItem = ConfigItem::GetObject(type, checkName);
104
105                         if (oldItem) {
106                                 std::ostringstream msgbuf;
107                                 msgbuf << "Object '" << name << "' of type '" << type << "' re-defined: " << debugInfo << "; previous definition: " << oldItem->GetDebugInfo();
108                                 BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debugInfo));
109                         }
110                 }
111
112                 item->SetType(type);
113
114                 if (name.FindFirstOf("!") != String::NPos) {
115                         std::ostringstream msgbuf;
116                         msgbuf << "Name for object '" << name << "' of type '" << type << "' is invalid: Object names may not contain '!'";
117                         BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debugInfo));
118                 }
119
120                 item->SetName(name);
121
122                 item->AddExpression(new OwnedExpression(expression));
123                 item->SetAbstract(abstract);
124                 item->SetScope(EvaluateClosedVars(frame, closedVars));
125                 item->SetZone(zone);
126                 item->SetFilter(filter);
127                 item->Compile()->Register();
128
129                 return Empty;
130         }
131
132         static inline Value For(ScriptFrame& frame, const String& fkvar, const String& fvvar, const Value& value, Expression *expression, const DebugInfo& debugInfo = DebugInfo())
133         {
134                 if (value.IsObjectType<Array>()) {
135                         if (!fvvar.IsEmpty())
136                                 BOOST_THROW_EXCEPTION(ScriptError("Cannot use dictionary iterator for array.", debugInfo));
137
138                         Array::Ptr arr = value;
139
140                         ObjectLock olock(arr);
141                         BOOST_FOREACH(const Value& value, arr) {
142                                 frame.Locals->Set(fkvar, value);
143                                 expression->Evaluate(frame);
144                         }
145                 } else if (value.IsString()) {
146                         if (!fvvar.IsEmpty())
147                                 BOOST_THROW_EXCEPTION(ScriptError("Cannot use dictionary iterator for string.", debugInfo));
148
149                         String str = value;
150
151                         BOOST_FOREACH(char ch, str) {
152                                 frame.Locals->Set(fkvar, String(1, ch));
153                                 expression->Evaluate(frame);
154                         }
155                 } else if (value.IsObjectType<Dictionary>()) {
156                         if (fvvar.IsEmpty())
157                                 BOOST_THROW_EXCEPTION(ScriptError("Cannot use array iterator for dictionary.", debugInfo));
158
159                         Dictionary::Ptr dict = value;
160
161                         ObjectLock olock(dict);
162                         BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
163                                 frame.Locals->Set(fkvar, kv.first);
164                                 frame.Locals->Set(fvvar, kv.second);
165                                 expression->Evaluate(frame);
166                         }
167                 } else
168                         BOOST_THROW_EXCEPTION(ScriptError("Invalid type in for expression: " + value.GetTypeName(), debugInfo));
169
170                 return Empty;
171         }
172
173         static inline bool HasField(const Object::Ptr& context, const String& field)
174         {
175                 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
176
177                 if (dict)
178                         return dict->Contains(field);
179                 else {
180                         Type::Ptr type = context->GetReflectionType();
181
182                         if (!type)
183                                 return false;
184
185                         return type->GetFieldId(field) != -1;
186                 }
187         }
188
189         static inline Value GetPrototypeField(const Value& context, const String& field, bool not_found_error = true, const DebugInfo& debugInfo = DebugInfo())
190         {
191                 Type::Ptr type = context.GetReflectionType();
192
193                 do {
194                         Object::Ptr object = type->GetPrototype();
195
196                         if (object && HasField(object, field))
197                                 return GetField(object, field, debugInfo);
198
199                         type = type->GetBaseType();
200                 } while (type);
201
202                 if (not_found_error)
203                         BOOST_THROW_EXCEPTION(ScriptError("Invalid field name: '" + field + "'", debugInfo));
204                 else
205                         return Empty;
206         }
207
208         static inline Value GetField(const Value& context, const String& field, const DebugInfo& debugInfo = DebugInfo())
209         {
210                 if (context.IsEmpty())
211                         return Empty;
212
213                 if (!context.IsObject())
214                         return GetPrototypeField(context, field, true, debugInfo);
215
216                 Object::Ptr object = context;
217
218                 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(object);
219
220                 if (dict) {
221                         if (dict->Contains(field))
222                                 return dict->Get(field);
223                         else
224                                 return GetPrototypeField(context, field, false, debugInfo);
225                 }
226
227                 Array::Ptr arr = dynamic_pointer_cast<Array>(object);
228
229                 if (arr) {
230                         int index;
231
232                         try {
233                                 index = Convert::ToLong(field);
234                         } catch (...) {
235                                 return GetPrototypeField(context, field, true, debugInfo);
236                         }
237
238                         if (index < 0 || index >= arr->GetLength())
239                                 BOOST_THROW_EXCEPTION(ScriptError("Array index '" + Convert::ToString(index) + "' is out of bounds.", debugInfo));
240
241                         return arr->Get(index);
242                 }
243
244                 Type::Ptr type = object->GetReflectionType();
245
246                 if (!type)
247                         return Empty;
248
249                 int fid = type->GetFieldId(field);
250
251                 if (fid == -1)
252                         return GetPrototypeField(context, field, true, debugInfo);
253
254                 return object->GetField(fid);
255         }
256
257         static inline void SetField(const Object::Ptr& context, const String& field, const Value& value, const DebugInfo& debugInfo = DebugInfo())
258         {
259                 Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
260
261                 if (dict) {
262                         dict->Set(field, value);
263                         return;
264                 }
265
266                 Array::Ptr arr = dynamic_pointer_cast<Array>(context);
267
268                 if (arr) {
269                         int index = Convert::ToLong(field);
270                         if (index >= arr->GetLength())
271                                 arr->Resize(index + 1);
272                         arr->Set(index, value);
273                         return;
274                 }
275
276                 Type::Ptr type = context->GetReflectionType();
277
278                 if (!type)
279                         BOOST_THROW_EXCEPTION(ScriptError("Cannot set field on object.", debugInfo));
280
281                 int fid = type->GetFieldId(field);
282
283                 if (fid == -1)
284                         BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' does not exist.", debugInfo));
285
286                 try {
287                         context->SetField(fid, value);
288                 } catch (const boost::bad_lexical_cast&) {
289                         BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'", debugInfo));
290                 } catch (const std::bad_cast&) {
291                         BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'", debugInfo));
292                 }
293         }
294
295 private:
296         static inline Value FunctionWrapper(const std::vector<Value>& arguments,
297             const std::vector<String>& funcargs, const Dictionary::Ptr& closedVars, const boost::shared_ptr<Expression>& expr)
298         {
299                 if (arguments.size() < funcargs.size())
300                         BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
301
302                 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
303                 ScriptFrame frame(vframe->Self);
304
305                 if (closedVars)
306                         closedVars->CopyTo(frame.Locals);
307
308                 for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), funcargs.size()); i++)
309                         frame.Locals->Set(funcargs[i], arguments[i]);
310
311                 Value result;
312                 try {
313                         result = expr->Evaluate(frame);
314                 } catch (const InterruptExecutionError& iee) {
315                         result = iee.GetResult();
316                 }
317
318                 return result;
319         }
320
321         static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, std::map<String, Expression *> *closedVars)
322         {
323                 Dictionary::Ptr locals;
324
325                 if (closedVars) {
326                         locals = new Dictionary();
327
328                         typedef std::pair<String, Expression *> ClosedVar;
329                         BOOST_FOREACH(const ClosedVar& cvar, *closedVars) {
330                                 locals->Set(cvar.first, cvar.second->Evaluate(frame));
331                         }
332                 }
333
334                 return locals;
335         }
336 };
337
338 }
339
340 #endif /* VMOPS_H */