]> granicus.if.org Git - icinga2/blob - lib/icinga/macroprocessor.cpp
Merge pull request #5928 from Icinga/fix/build-fix-msvc
[icinga2] / lib / icinga / macroprocessor.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 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::EvaluateFunction(const Function::Ptr& func, const ResolverList& resolvers,
184         const CheckResult::Ptr& cr, const MacroProcessor::EscapeCallback& escapeFn,
185         const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int recursionLevel)
186 {
187         Dictionary::Ptr resolvers_this = new Dictionary();
188
189         for (const ResolverSpec& resolver : resolvers) {
190                 resolvers_this->Set(resolver.first, resolver.second);
191         }
192
193         auto internalResolveMacrosShim = [resolvers, cr, resolvedMacros, useResolvedMacros, recursionLevel](const std::vector<Value>& args) {
194                 if (args.size() < 1)
195                         BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
196
197                 String missingMacro;
198
199                 return MacroProcessor::InternalResolveMacros(args[0], resolvers, cr, &missingMacro, MacroProcessor::EscapeCallback(),
200                         resolvedMacros, useResolvedMacros, recursionLevel);
201         };
202
203         resolvers_this->Set("macro", new Function("macro (temporary)", internalResolveMacrosShim, { "str" }));
204
205         auto internalResolveArgumentsShim = [resolvers, cr, resolvedMacros, useResolvedMacros, recursionLevel](const std::vector<Value>& args) {
206                 if (args.size() < 2)
207                         BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
208
209                 return MacroProcessor::ResolveArguments(args[0], args[1], resolvers, cr,
210                         resolvedMacros, useResolvedMacros, recursionLevel + 1);
211         };
212
213         resolvers_this->Set("resolve_arguments", new Function("resolve_arguments (temporary)", internalResolveArgumentsShim, { "command", "args" }));
214
215         return func->InvokeThis(resolvers_this);
216 }
217
218 Value MacroProcessor::InternalResolveMacros(const String& str, const ResolverList& resolvers,
219         const CheckResult::Ptr& cr, String *missingMacro,
220         const MacroProcessor::EscapeCallback& escapeFn, const Dictionary::Ptr& resolvedMacros,
221         bool useResolvedMacros, int recursionLevel)
222 {
223         CONTEXT("Resolving macros for string '" + str + "'");
224
225         if (recursionLevel > 15)
226                 BOOST_THROW_EXCEPTION(std::runtime_error("Infinite recursion detected while resolving macros"));
227
228         size_t offset, pos_first, pos_second;
229         offset = 0;
230
231         Dictionary::Ptr resolvers_this;
232
233         String result = str;
234         while ((pos_first = result.FindFirstOf("$", offset)) != String::NPos) {
235                 pos_second = result.FindFirstOf("$", pos_first + 1);
236
237                 if (pos_second == String::NPos)
238                         BOOST_THROW_EXCEPTION(std::runtime_error("Closing $ not found in macro format string."));
239
240                 String name = result.SubStr(pos_first + 1, pos_second - pos_first - 1);
241
242                 Value resolved_macro;
243                 bool recursive_macro;
244                 bool found;
245
246                 if (useResolvedMacros) {
247                         recursive_macro = false;
248                         found = resolvedMacros->Contains(name);
249
250                         if (found)
251                                 resolved_macro = resolvedMacros->Get(name);
252                 } else
253                         found = ResolveMacro(name, resolvers, cr, &resolved_macro, &recursive_macro);
254
255                 /* $$ is an escape sequence for $. */
256                 if (name.IsEmpty()) {
257                         resolved_macro = "$";
258                         found = true;
259                 }
260
261                 if (resolved_macro.IsObjectType<Function>()) {
262                         resolved_macro = EvaluateFunction(resolved_macro, resolvers, cr, escapeFn,
263                                 resolvedMacros, useResolvedMacros, recursionLevel + 1);
264                 }
265
266                 if (!found) {
267                         if (!missingMacro)
268                                 Log(LogWarning, "MacroProcessor")
269                                         << "Macro '" << name << "' is not defined.";
270                         else
271                                 *missingMacro = name;
272                 }
273
274                 /* recursively resolve macros in the macro if it was a user macro */
275                 if (recursive_macro) {
276                         if (resolved_macro.IsObjectType<Array>()) {
277                                 Array::Ptr arr = resolved_macro;
278                                 Array::Ptr resolved_arr = new Array();
279
280                                 ObjectLock olock(arr);
281                                 for (const Value& value : arr) {
282                                         if (value.IsScalar()) {
283                                                 resolved_arr->Add(InternalResolveMacros(value,
284                                                         resolvers, cr, missingMacro, EscapeCallback(), nullptr,
285                                                         false, recursionLevel + 1));
286                                         } else
287                                                 resolved_arr->Add(value);
288                                 }
289
290                                 resolved_macro = resolved_arr;
291                         } else if (resolved_macro.IsString()) {
292                                 resolved_macro = InternalResolveMacros(resolved_macro,
293                                         resolvers, cr, missingMacro, EscapeCallback(), nullptr,
294                                         false, recursionLevel + 1);
295                         }
296                 }
297
298                 if (!useResolvedMacros && found && resolvedMacros)
299                         resolvedMacros->Set(name, resolved_macro);
300
301                 if (escapeFn)
302                         resolved_macro = escapeFn(resolved_macro);
303
304                 /* we're done if this is the only macro and there are no other non-macro parts in the string */
305                 if (pos_first == 0 && pos_second == str.GetLength() - 1)
306                         return resolved_macro;
307                 else if (resolved_macro.IsObjectType<Array>())
308                                 BOOST_THROW_EXCEPTION(std::invalid_argument("Mixing both strings and non-strings in macros is not allowed."));
309
310                 if (resolved_macro.IsObjectType<Array>()) {
311                         /* don't allow mixing strings and arrays in macro strings */
312                         if (pos_first != 0 || pos_second != str.GetLength() - 1)
313                                 BOOST_THROW_EXCEPTION(std::invalid_argument("Mixing both strings and non-strings in macros is not allowed."));
314
315                         return resolved_macro;
316                 }
317
318                 String resolved_macro_str = resolved_macro;
319
320                 result.Replace(pos_first, pos_second - pos_first + 1, resolved_macro_str);
321                 offset = pos_first + resolved_macro_str.GetLength();
322         }
323
324         return result;
325 }
326
327
328 bool MacroProcessor::ValidateMacroString(const String& macro)
329 {
330         if (macro.IsEmpty())
331                 return true;
332
333         size_t pos_first, pos_second, offset;
334         offset = 0;
335
336         while ((pos_first = macro.FindFirstOf("$", offset)) != String::NPos) {
337                 pos_second = macro.FindFirstOf("$", pos_first + 1);
338
339                 if (pos_second == String::NPos)
340                         return false;
341
342                 offset = pos_second + 1;
343         }
344
345         return true;
346 }
347
348 void MacroProcessor::ValidateCustomVars(const ConfigObject::Ptr& object, const Dictionary::Ptr& value)
349 {
350         if (!value)
351                 return;
352
353         /* string, array, dictionary */
354         ObjectLock olock(value);
355         for (const Dictionary::Pair& kv : value) {
356                 const Value& varval = kv.second;
357
358                 if (varval.IsObjectType<Dictionary>()) {
359                         /* only one dictonary level */
360                         Dictionary::Ptr varval_dict = varval;
361
362                         ObjectLock xlock(varval_dict);
363                         for (const Dictionary::Pair& kv_var : varval_dict) {
364                                 if (!kv_var.second.IsString())
365                                         continue;
366
367                                 if (!ValidateMacroString(kv_var.second))
368                                         BOOST_THROW_EXCEPTION(ValidationError(object.get(), { "vars", kv.first, kv_var.first }, "Closing $ not found in macro format string '" + kv_var.second + "'."));
369                         }
370                 } else if (varval.IsObjectType<Array>()) {
371                         /* check all array entries */
372                         Array::Ptr varval_arr = varval;
373
374                         ObjectLock ylock (varval_arr);
375                         for (const Value& arrval : varval_arr) {
376                                 if (!arrval.IsString())
377                                         continue;
378
379                                 if (!ValidateMacroString(arrval)) {
380                                         BOOST_THROW_EXCEPTION(ValidationError(object.get(), { "vars", kv.first }, "Closing $ not found in macro format string '" + arrval + "'."));
381                                 }
382                         }
383                 } else {
384                         if (!varval.IsString())
385                                 continue;
386
387                         if (!ValidateMacroString(varval))
388                                 BOOST_THROW_EXCEPTION(ValidationError(object.get(), { "vars", kv.first }, "Closing $ not found in macro format string '" + varval + "'."));
389                 }
390         }
391 }
392
393 void MacroProcessor::AddArgumentHelper(const Array::Ptr& args, const String& key, const String& value,
394         bool add_key, bool add_value)
395 {
396         if (add_key)
397                 args->Add(key);
398
399         if (add_value)
400                 args->Add(value);
401 }
402
403 Value MacroProcessor::EscapeMacroShellArg(const Value& value)
404 {
405         String result;
406
407         if (value.IsObjectType<Array>()) {
408                 Array::Ptr arr = value;
409
410                 ObjectLock olock(arr);
411                 for (const Value& arg : arr) {
412                         if (result.GetLength() > 0)
413                                 result += " ";
414
415                         result += Utility::EscapeShellArg(arg);
416                 }
417         } else
418                 result = Utility::EscapeShellArg(value);
419
420         return result;
421 }
422
423 struct CommandArgument
424 {
425         int Order;
426         bool SkipKey;
427         bool RepeatKey;
428         bool SkipValue;
429         String Key;
430         Value AValue;
431
432         CommandArgument(void)
433                 : Order(0), SkipKey(false), RepeatKey(true), SkipValue(false)
434         { }
435
436         bool operator<(const CommandArgument& rhs) const
437         {
438                 return Order < rhs.Order;
439         }
440 };
441
442 Value MacroProcessor::ResolveArguments(const Value& command, const Dictionary::Ptr& arguments,
443         const MacroProcessor::ResolverList& resolvers, const CheckResult::Ptr& cr,
444         const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int recursionLevel)
445 {
446         Value resolvedCommand;
447         if (!arguments || command.IsObjectType<Array>() || command.IsObjectType<Function>())
448                 resolvedCommand = MacroProcessor::ResolveMacros(command, resolvers, cr, nullptr,
449                         EscapeMacroShellArg, resolvedMacros, useResolvedMacros, recursionLevel + 1);
450         else {
451                 Array::Ptr arr = new Array();
452                 arr->Add(command);
453                 resolvedCommand = arr;
454         }
455
456         if (arguments) {
457                 std::vector<CommandArgument> args;
458
459                 ObjectLock olock(arguments);
460                 for (const Dictionary::Pair& kv : arguments) {
461                         const Value& arginfo = kv.second;
462
463                         CommandArgument arg;
464                         arg.Key = kv.first;
465
466                         bool required = false;
467                         Value argval;
468
469                         if (arginfo.IsObjectType<Dictionary>()) {
470                                 Dictionary::Ptr argdict = arginfo;
471                                 if (argdict->Contains("key"))
472                                         arg.Key = argdict->Get("key");
473                                 argval = argdict->Get("value");
474                                 if (argdict->Contains("required"))
475                                         required = argdict->Get("required");
476                                 arg.SkipKey = argdict->Get("skip_key");
477                                 if (argdict->Contains("repeat_key"))
478                                         arg.RepeatKey = argdict->Get("repeat_key");
479                                 arg.Order = argdict->Get("order");
480
481                                 Value set_if = argdict->Get("set_if");
482
483                                 if (!set_if.IsEmpty()) {
484                                         String missingMacro;
485                                         Value set_if_resolved = MacroProcessor::ResolveMacros(set_if, resolvers,
486                                                 cr, &missingMacro, MacroProcessor::EscapeCallback(), resolvedMacros,
487                                                 useResolvedMacros, recursionLevel + 1);
488
489                                         if (!missingMacro.IsEmpty())
490                                                 continue;
491
492                                         int value;
493
494                                         if (set_if_resolved == "true")
495                                                 value = 1;
496                                         else if (set_if_resolved == "false")
497                                                 value = 0;
498                                         else {
499                                                 try {
500                                                         value = Convert::ToLong(set_if_resolved);
501                                                 } catch (const std::exception& ex) {
502                                                         /* tried to convert a string */
503                                                         Log(LogWarning, "PluginUtility")
504                                                                 << "Error evaluating set_if value '" << set_if_resolved
505                                                                 << "' used in argument '" << arg.Key << "': " << ex.what();
506                                                         continue;
507                                                 }
508                                         }
509
510                                         if (!value)
511                                                 continue;
512                                 }
513                         }
514                         else
515                                 argval = arginfo;
516
517                         if (argval.IsEmpty())
518                                 arg.SkipValue = true;
519
520                         String missingMacro;
521                         arg.AValue = MacroProcessor::ResolveMacros(argval, resolvers,
522                                 cr, &missingMacro, MacroProcessor::EscapeCallback(), resolvedMacros,
523                                 useResolvedMacros, recursionLevel + 1);
524
525                         if (!missingMacro.IsEmpty()) {
526                                 if (required) {
527                                         BOOST_THROW_EXCEPTION(ScriptError("Non-optional macro '" + missingMacro + "' used in argument '" +
528                                                 arg.Key + "' is missing."));
529                                 }
530
531                                 continue;
532                         }
533
534                         args.emplace_back(std::move(arg));
535                 }
536
537                 std::sort(args.begin(), args.end());
538
539                 Array::Ptr command_arr = resolvedCommand;
540                 for (const CommandArgument& arg : args) {
541
542                         if (arg.AValue.IsObjectType<Dictionary>()) {
543                                 Log(LogWarning, "PluginUtility")
544                                         << "Tried to use dictionary in argument '" << arg.Key << "'.";
545                                 continue;
546                         } else if (arg.AValue.IsObjectType<Array>()) {
547                                 bool first = true;
548                                 Array::Ptr arr = static_cast<Array::Ptr>(arg.AValue);
549
550                                 ObjectLock olock(arr);
551                                 for (const Value& value : arr) {
552                                         bool add_key;
553
554                                         if (first) {
555                                                 first = false;
556                                                 add_key = !arg.SkipKey;
557                                         } else
558                                                 add_key = !arg.SkipKey && arg.RepeatKey;
559
560                                         AddArgumentHelper(command_arr, arg.Key, value, add_key, !arg.SkipValue);
561                                 }
562                         } else
563                                 AddArgumentHelper(command_arr, arg.Key, arg.AValue, !arg.SkipKey, !arg.SkipValue);
564                 }
565         }
566
567         return resolvedCommand;
568 }
569