]> granicus.if.org Git - icinga2/blob - lib/icinga/macroprocessor.cpp
Merge pull request #5827 from Icinga/feature/replace-statsfunction-with-function
[icinga2] / lib / icinga / macroprocessor.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
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 #include "icinga/macroprocessor.hpp"
21 #include "icinga/macroresolver.hpp"
22 #include "icinga/customvarobject.hpp"
23 #include "base/array.hpp"
24 #include "base/objectlock.hpp"
25 #include "base/logger.hpp"
26 #include "base/context.hpp"
27 #include "base/configobject.hpp"
28 #include "base/scriptframe.hpp"
29 #include "base/convert.hpp"
30 #include "base/exception.hpp"
31 #include <boost/algorithm/string/split.hpp>
32 #include <boost/algorithm/string/join.hpp>
33 #include <boost/algorithm/string/classification.hpp>
34
35 using namespace icinga;
36
37 Value MacroProcessor::ResolveMacros(const Value& str, const ResolverList& resolvers,
38     const CheckResult::Ptr& cr, String *missingMacro,
39     const MacroProcessor::EscapeCallback& escapeFn, const Dictionary::Ptr& resolvedMacros,
40     bool useResolvedMacros, int recursionLevel)
41 {
42         Value result;
43
44         if (str.IsEmpty())
45                 return Empty;
46
47         if (str.IsScalar()) {
48                 result = InternalResolveMacros(str, resolvers, cr, missingMacro, escapeFn,
49                     resolvedMacros, useResolvedMacros, recursionLevel + 1);
50         } else if (str.IsObjectType<Array>()) {
51                 Array::Ptr resultArr = new Array();
52                 Array::Ptr arr = str;
53
54                 ObjectLock olock(arr);
55
56                 for (const Value& arg : arr) {
57                         /* Note: don't escape macros here. */
58                         Value value = InternalResolveMacros(arg, resolvers, cr, missingMacro,
59                             EscapeCallback(), resolvedMacros, useResolvedMacros, recursionLevel + 1);
60
61                         if (value.IsObjectType<Array>())
62                                 resultArr->Add(Utility::Join(value, ';'));
63                         else
64                                 resultArr->Add(value);
65                 }
66
67                 result = resultArr;
68         } else if (str.IsObjectType<Dictionary>()) {
69                 Dictionary::Ptr resultDict = new Dictionary();
70                 Dictionary::Ptr dict = str;
71
72                 ObjectLock olock(dict);
73
74                 for (const Dictionary::Pair& kv : dict) {
75                         /* Note: don't escape macros here. */
76                         resultDict->Set(kv.first, InternalResolveMacros(kv.second, resolvers, cr, missingMacro,
77                             EscapeCallback(), resolvedMacros, useResolvedMacros, recursionLevel + 1));
78                 }
79
80                 result = resultDict;
81         } else if (str.IsObjectType<Function>()) {
82                 result = EvaluateFunction(str, resolvers, cr, escapeFn, resolvedMacros, useResolvedMacros, 0);
83         } else {
84                 BOOST_THROW_EXCEPTION(std::invalid_argument("Macro is not a string or array."));
85         }
86
87         return result;
88 }
89
90 bool MacroProcessor::ResolveMacro(const String& macro, const ResolverList& resolvers,
91     const CheckResult::Ptr& cr, Value *result, bool *recursive_macro)
92 {
93         CONTEXT("Resolving macro '" + macro + "'");
94
95         *recursive_macro = false;
96
97         std::vector<String> tokens;
98         boost::algorithm::split(tokens, macro, boost::is_any_of("."));
99
100         String objName;
101         if (tokens.size() > 1) {
102                 objName = tokens[0];
103                 tokens.erase(tokens.begin());
104         }
105
106         for (const ResolverSpec& resolver : resolvers) {
107                 if (!objName.IsEmpty() && objName != resolver.first)
108                         continue;
109
110                 if (objName.IsEmpty()) {
111                         CustomVarObject::Ptr dobj = dynamic_pointer_cast<CustomVarObject>(resolver.second);
112
113                         if (dobj) {
114                                 Dictionary::Ptr vars = dobj->GetVars();
115
116                                 if (vars && vars->Contains(macro)) {
117                                         *result = vars->Get(macro);
118                                         *recursive_macro = true;
119                                         return true;
120                                 }
121                         }
122                 }
123
124                 MacroResolver *mresolver = dynamic_cast<MacroResolver *>(resolver.second.get());
125
126                 if (mresolver && mresolver->ResolveMacro(boost::algorithm::join(tokens, "."), cr, result))
127                         return true;
128
129                 Value ref = resolver.second;
130                 bool valid = true;
131
132                 for (const String& token : tokens) {
133                         if (ref.IsObjectType<Dictionary>()) {
134                                 Dictionary::Ptr dict = ref;
135                                 if (dict->Contains(token)) {
136                                         ref = dict->Get(token);
137                                         continue;
138                                 } else {
139                                         valid = false;
140                                         break;
141                                 }
142                         } else if (ref.IsObject()) {
143                                 Object::Ptr object = ref;
144
145                                 Type::Ptr type = object->GetReflectionType();
146
147                                 if (!type) {
148                                         valid = false;
149                                         break;
150                                 }
151
152                                 int field = type->GetFieldId(token);
153
154                                 if (field == -1) {
155                                         valid = false;
156                                         break;
157                                 }
158
159                                 ref = object->GetField(field);
160
161                                 Field fieldInfo = type->GetFieldInfo(field);
162
163                                 if (strcmp(fieldInfo.TypeName, "Timestamp") == 0)
164                                         ref = static_cast<long>(ref);
165                         }
166                 }
167
168                 if (valid) {
169                         if (tokens[0] == "vars" ||
170                             tokens[0] == "action_url" ||
171                             tokens[0] == "notes_url" ||
172                             tokens[0] == "notes")
173                                 *recursive_macro = true;
174
175                         *result = ref;
176                         return true;
177                 }
178         }
179
180         return false;
181 }
182
183 Value MacroProcessor::InternalResolveMacrosShim(const std::vector<Value>& args, const ResolverList& resolvers,
184     const CheckResult::Ptr& cr, const MacroProcessor::EscapeCallback& escapeFn, const Dictionary::Ptr& resolvedMacros,
185     bool useResolvedMacros, int recursionLevel)
186 {
187         if (args.size() < 1)
188                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
189
190         String missingMacro;
191
192         return MacroProcessor::InternalResolveMacros(args[0], resolvers, cr, &missingMacro, escapeFn,
193             resolvedMacros, useResolvedMacros, recursionLevel);
194 }
195
196 Value MacroProcessor::InternalResolveArgumentsShim(const std::vector<Value>& args, const ResolverList& resolvers,
197     const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros,
198     bool useResolvedMacros, int recursionLevel)
199 {
200         if (args.size() < 2)
201                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
202
203         return MacroProcessor::ResolveArguments(args[0], args[1], resolvers, cr,
204             resolvedMacros, useResolvedMacros, recursionLevel);
205 }
206
207 Value MacroProcessor::EvaluateFunction(const Function::Ptr& func, const ResolverList& resolvers,
208     const CheckResult::Ptr& cr, const MacroProcessor::EscapeCallback& escapeFn,
209     const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int recursionLevel)
210 {
211         Dictionary::Ptr resolvers_this = new Dictionary();
212
213         for (const ResolverSpec& resolver : resolvers) {
214                 resolvers_this->Set(resolver.first, resolver.second);
215         }
216
217         resolvers_this->Set("macro", new Function("macro (temporary)", std::bind(&MacroProcessor::InternalResolveMacrosShim,
218             _1, std::cref(resolvers), cr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros,
219             recursionLevel + 1), { "str" }));
220         resolvers_this->Set("resolve_arguments", new Function("resolve_arguments (temporary)", std::bind(&MacroProcessor::InternalResolveArgumentsShim,
221             _1, std::cref(resolvers), cr, resolvedMacros, useResolvedMacros,
222             recursionLevel + 1)));
223
224         return func->InvokeThis(resolvers_this);
225 }
226
227 Value MacroProcessor::InternalResolveMacros(const String& str, const ResolverList& resolvers,
228     const CheckResult::Ptr& cr, String *missingMacro,
229     const MacroProcessor::EscapeCallback& escapeFn, const Dictionary::Ptr& resolvedMacros,
230     bool useResolvedMacros, int recursionLevel)
231 {
232         CONTEXT("Resolving macros for string '" + str + "'");
233
234         if (recursionLevel > 15)
235                 BOOST_THROW_EXCEPTION(std::runtime_error("Infinite recursion detected while resolving macros"));
236
237         size_t offset, pos_first, pos_second;
238         offset = 0;
239
240         Dictionary::Ptr resolvers_this;
241
242         String result = str;
243         while ((pos_first = result.FindFirstOf("$", offset)) != String::NPos) {
244                 pos_second = result.FindFirstOf("$", pos_first + 1);
245
246                 if (pos_second == String::NPos)
247                         BOOST_THROW_EXCEPTION(std::runtime_error("Closing $ not found in macro format string."));
248
249                 String name = result.SubStr(pos_first + 1, pos_second - pos_first - 1);
250
251                 Value resolved_macro;
252                 bool recursive_macro;
253                 bool found;
254
255                 if (useResolvedMacros) {
256                         recursive_macro = false;
257                         found = resolvedMacros->Contains(name);
258
259                         if (found)
260                                 resolved_macro = resolvedMacros->Get(name);
261                 } else
262                         found = ResolveMacro(name, resolvers, cr, &resolved_macro, &recursive_macro);
263
264                 /* $$ is an escape sequence for $. */
265                 if (name.IsEmpty()) {
266                         resolved_macro = "$";
267                         found = true;
268                 }
269
270                 if (resolved_macro.IsObjectType<Function>()) {
271                         resolved_macro = EvaluateFunction(resolved_macro, resolvers, cr, escapeFn,
272                             resolvedMacros, useResolvedMacros, recursionLevel + 1);
273                 }
274
275                 if (!found) {
276                         if (!missingMacro)
277                                 Log(LogWarning, "MacroProcessor")
278                                     << "Macro '" << name << "' is not defined.";
279                         else
280                                 *missingMacro = name;
281                 }
282
283                 /* recursively resolve macros in the macro if it was a user macro */
284                 if (recursive_macro) {
285                         if (resolved_macro.IsObjectType<Array>()) {
286                                 Array::Ptr arr = resolved_macro;
287                                 Array::Ptr resolved_arr = new Array();
288
289                                 ObjectLock olock(arr);
290                                 for (const Value& value : arr) {
291                                         if (value.IsScalar()) {
292                                                 resolved_arr->Add(InternalResolveMacros(value,
293                                                         resolvers, cr, missingMacro, EscapeCallback(), nullptr,
294                                                         false, recursionLevel + 1));
295                                         } else
296                                                 resolved_arr->Add(value);
297                                 }
298
299                                 resolved_macro = resolved_arr;
300                         } else if (resolved_macro.IsString()) {
301                                 resolved_macro = InternalResolveMacros(resolved_macro,
302                                         resolvers, cr, missingMacro, EscapeCallback(), nullptr,
303                                         false, recursionLevel + 1);
304                         }
305                 }
306
307                 if (!useResolvedMacros && found && resolvedMacros)
308                         resolvedMacros->Set(name, resolved_macro);
309
310                 if (escapeFn)
311                         resolved_macro = escapeFn(resolved_macro);
312
313                 /* we're done if this is the only macro and there are no other non-macro parts in the string */
314                 if (pos_first == 0 && pos_second == str.GetLength() - 1)
315                         return resolved_macro;
316                 else if (resolved_macro.IsObjectType<Array>())
317                                 BOOST_THROW_EXCEPTION(std::invalid_argument("Mixing both strings and non-strings in macros is not allowed."));
318
319                 if (resolved_macro.IsObjectType<Array>()) {
320                         /* don't allow mixing strings and arrays in macro strings */
321                         if (pos_first != 0 || pos_second != str.GetLength() - 1)
322                                 BOOST_THROW_EXCEPTION(std::invalid_argument("Mixing both strings and non-strings in macros is not allowed."));
323
324                         return resolved_macro;
325                 }
326
327                 String resolved_macro_str = resolved_macro;
328
329                 result.Replace(pos_first, pos_second - pos_first + 1, resolved_macro_str);
330                 offset = pos_first + resolved_macro_str.GetLength();
331         }
332
333         return result;
334 }
335
336
337 bool MacroProcessor::ValidateMacroString(const String& macro)
338 {
339         if (macro.IsEmpty())
340                 return true;
341
342         size_t pos_first, pos_second, offset;
343         offset = 0;
344
345         while ((pos_first = macro.FindFirstOf("$", offset)) != String::NPos) {
346                 pos_second = macro.FindFirstOf("$", pos_first + 1);
347
348                 if (pos_second == String::NPos)
349                         return false;
350
351                 offset = pos_second + 1;
352         }
353
354         return true;
355 }
356
357 void MacroProcessor::ValidateCustomVars(const ConfigObject::Ptr& object, const Dictionary::Ptr& value)
358 {
359         if (!value)
360                 return;
361
362         /* string, array, dictionary */
363         ObjectLock olock(value);
364         for (const Dictionary::Pair& kv : value) {
365                 const Value& varval = kv.second;
366
367                 if (varval.IsObjectType<Dictionary>()) {
368                         /* only one dictonary level */
369                         Dictionary::Ptr varval_dict = varval;
370
371                         ObjectLock xlock(varval_dict);
372                         for (const Dictionary::Pair& kv_var : varval_dict) {
373                                 if (!kv_var.second.IsString())
374                                         continue;
375
376                                 if (!ValidateMacroString(kv_var.second))
377                                         BOOST_THROW_EXCEPTION(ValidationError(object.get(), { "vars", kv.first, kv_var.first }, "Closing $ not found in macro format string '" + kv_var.second + "'."));
378                         }
379                 } else if (varval.IsObjectType<Array>()) {
380                         /* check all array entries */
381                         Array::Ptr varval_arr = varval;
382
383                         ObjectLock ylock (varval_arr);
384                         for (const Value& arrval : varval_arr) {
385                                 if (!arrval.IsString())
386                                         continue;
387
388                                 if (!ValidateMacroString(arrval)) {
389                                         BOOST_THROW_EXCEPTION(ValidationError(object.get(), { "vars", kv.first }, "Closing $ not found in macro format string '" + arrval + "'."));
390                                 }
391                         }
392                 } else {
393                         if (!varval.IsString())
394                                 continue;
395
396                         if (!ValidateMacroString(varval))
397                                 BOOST_THROW_EXCEPTION(ValidationError(object.get(), { "vars", kv.first }, "Closing $ not found in macro format string '" + varval + "'."));
398                 }
399         }
400 }
401
402 void MacroProcessor::AddArgumentHelper(const Array::Ptr& args, const String& key, const String& value,
403     bool add_key, bool add_value)
404 {
405         if (add_key)
406                 args->Add(key);
407
408         if (add_value)
409                 args->Add(value);
410 }
411
412 Value MacroProcessor::EscapeMacroShellArg(const Value& value)
413 {
414         String result;
415
416         if (value.IsObjectType<Array>()) {
417                 Array::Ptr arr = value;
418
419                 ObjectLock olock(arr);
420                 for (const Value& arg : arr) {
421                         if (result.GetLength() > 0)
422                                 result += " ";
423
424                         result += Utility::EscapeShellArg(arg);
425                 }
426         } else
427                 result = Utility::EscapeShellArg(value);
428
429         return result;
430 }
431
432 struct CommandArgument
433 {
434         int Order;
435         bool SkipKey;
436         bool RepeatKey;
437         bool SkipValue;
438         String Key;
439         Value AValue;
440
441         CommandArgument(void)
442                 : Order(0), SkipKey(false), RepeatKey(true), SkipValue(false)
443         { }
444
445         bool operator<(const CommandArgument& rhs) const
446         {
447                 return Order < rhs.Order;
448         }
449 };
450
451 Value MacroProcessor::ResolveArguments(const Value& command, const Dictionary::Ptr& arguments,
452     const MacroProcessor::ResolverList& resolvers, const CheckResult::Ptr& cr,
453     const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int recursionLevel)
454 {
455         Value resolvedCommand;
456         if (!arguments || command.IsObjectType<Array>() || command.IsObjectType<Function>())
457                 resolvedCommand = MacroProcessor::ResolveMacros(command, resolvers, cr, NULL,
458                     EscapeMacroShellArg, resolvedMacros, useResolvedMacros, recursionLevel + 1);
459         else {
460                 Array::Ptr arr = new Array();
461                 arr->Add(command);
462                 resolvedCommand = arr;
463         }
464
465         if (arguments) {
466                 std::vector<CommandArgument> args;
467
468                 ObjectLock olock(arguments);
469                 for (const Dictionary::Pair& kv : arguments) {
470                         const Value& arginfo = kv.second;
471
472                         CommandArgument arg;
473                         arg.Key = kv.first;
474
475                         bool required = false;
476                         Value argval;
477
478                         if (arginfo.IsObjectType<Dictionary>()) {
479                                 Dictionary::Ptr argdict = arginfo;
480                                 if (argdict->Contains("key"))
481                                         arg.Key = argdict->Get("key");
482                                 argval = argdict->Get("value");
483                                 if (argdict->Contains("required"))
484                                         required = argdict->Get("required");
485                                 arg.SkipKey = argdict->Get("skip_key");
486                                 if (argdict->Contains("repeat_key"))
487                                         arg.RepeatKey = argdict->Get("repeat_key");
488                                 arg.Order = argdict->Get("order");
489
490                                 Value set_if = argdict->Get("set_if");
491
492                                 if (!set_if.IsEmpty()) {
493                                         String missingMacro;
494                                         Value set_if_resolved = MacroProcessor::ResolveMacros(set_if, resolvers,
495                                             cr, &missingMacro, MacroProcessor::EscapeCallback(), resolvedMacros,
496                                             useResolvedMacros, recursionLevel + 1);
497
498                                         if (!missingMacro.IsEmpty())
499                                                 continue;
500
501                                         int value;
502
503                                         if (set_if_resolved == "true")
504                                                 value = 1;
505                                         else if (set_if_resolved == "false")
506                                                 value = 0;
507                                         else {
508                                                 try {
509                                                         value = Convert::ToLong(set_if_resolved);
510                                                 } catch (const std::exception& ex) {
511                                                         /* tried to convert a string */
512                                                         Log(LogWarning, "PluginUtility")
513                                                             << "Error evaluating set_if value '" << set_if_resolved
514                                                             << "' used in argument '" << arg.Key << "': " << ex.what();
515                                                         continue;
516                                                 }
517                                         }
518
519                                         if (!value)
520                                                 continue;
521                                 }
522                         }
523                         else
524                                 argval = arginfo;
525
526                         if (argval.IsEmpty())
527                                 arg.SkipValue = true;
528
529                         String missingMacro;
530                         arg.AValue = MacroProcessor::ResolveMacros(argval, resolvers,
531                             cr, &missingMacro, MacroProcessor::EscapeCallback(), resolvedMacros,
532                             useResolvedMacros, recursionLevel + 1);
533
534                         if (!missingMacro.IsEmpty()) {
535                                 if (required) {
536                                         BOOST_THROW_EXCEPTION(ScriptError("Non-optional macro '" + missingMacro + "' used in argument '" +
537                                             arg.Key + "' is missing."));
538                                 }
539
540                                 continue;
541                         }
542
543                         args.emplace_back(std::move(arg));
544                 }
545
546                 std::sort(args.begin(), args.end());
547
548                 Array::Ptr command_arr = resolvedCommand;
549                 for (const CommandArgument& arg : args) {
550
551                         if (arg.AValue.IsObjectType<Dictionary>()) {
552                                 Log(LogWarning, "PluginUtility")
553                                     << "Tried to use dictionary in argument '" << arg.Key << "'.";
554                                 continue;
555                         } else if (arg.AValue.IsObjectType<Array>()) {
556                                 bool first = true;
557                                 Array::Ptr arr = static_cast<Array::Ptr>(arg.AValue);
558
559                                 ObjectLock olock(arr);
560                                 for (const Value& value : arr) {
561                                         bool add_key;
562
563                                         if (first) {
564                                                 first = false;
565                                                 add_key = !arg.SkipKey;
566                                         } else
567                                                 add_key = !arg.SkipKey && arg.RepeatKey;
568
569                                         AddArgumentHelper(command_arr, arg.Key, value, add_key, !arg.SkipValue);
570                                 }
571                         } else
572                                 AddArgumentHelper(command_arr, arg.Key, arg.AValue, !arg.SkipKey, !arg.SkipValue);
573                 }
574         }
575
576         return resolvedCommand;
577 }
578