]> granicus.if.org Git - icinga2/blob - lib/icinga/macroprocessor.cpp
Merge pull request #6103 from Icinga/fix/http-security-fixes
[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/join.hpp>
32
33 using namespace icinga;
34
35 Value MacroProcessor::ResolveMacros(const Value& str, const ResolverList& resolvers,
36         const CheckResult::Ptr& cr, String *missingMacro,
37         const MacroProcessor::EscapeCallback& escapeFn, const Dictionary::Ptr& resolvedMacros,
38         bool useResolvedMacros, int recursionLevel)
39 {
40         if (useResolvedMacros)
41                 REQUIRE_NOT_NULL(resolvedMacros);
42
43         Value result;
44
45         if (str.IsEmpty())
46                 return Empty;
47
48         if (str.IsScalar()) {
49                 result = InternalResolveMacros(str, resolvers, cr, missingMacro, escapeFn,
50                         resolvedMacros, useResolvedMacros, recursionLevel + 1);
51         } else if (str.IsObjectType<Array>()) {
52                 ArrayData resultArr;
53                 Array::Ptr arr = str;
54
55                 ObjectLock olock(arr);
56
57                 for (const Value& arg : arr) {
58                         /* Note: don't escape macros here. */
59                         Value value = InternalResolveMacros(arg, resolvers, cr, missingMacro,
60                                 EscapeCallback(), resolvedMacros, useResolvedMacros, recursionLevel + 1);
61
62                         if (value.IsObjectType<Array>())
63                                 resultArr.push_back(Utility::Join(value, ';'));
64                         else
65                                 resultArr.push_back(value);
66                 }
67
68                 result = new Array(std::move(resultArr));
69         } else if (str.IsObjectType<Dictionary>()) {
70                 Dictionary::Ptr resultDict = new Dictionary();
71                 Dictionary::Ptr dict = str;
72
73                 ObjectLock olock(dict);
74
75                 for (const Dictionary::Pair& kv : dict) {
76                         /* Note: don't escape macros here. */
77                         resultDict->Set(kv.first, InternalResolveMacros(kv.second, resolvers, cr, missingMacro,
78                                 EscapeCallback(), resolvedMacros, useResolvedMacros, recursionLevel + 1));
79                 }
80
81                 result = resultDict;
82         } else if (str.IsObjectType<Function>()) {
83                 result = EvaluateFunction(str, resolvers, cr, escapeFn, resolvedMacros, useResolvedMacros, 0);
84         } else {
85                 BOOST_THROW_EXCEPTION(std::invalid_argument("Macro is not a string or array."));
86         }
87
88         return result;
89 }
90
91 bool MacroProcessor::ResolveMacro(const String& macro, const ResolverList& resolvers,
92         const CheckResult::Ptr& cr, Value *result, bool *recursive_macro)
93 {
94         CONTEXT("Resolving macro '" + macro + "'");
95
96         *recursive_macro = false;
97
98         std::vector<String> tokens = macro.Split(".");
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                 auto *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                                 ArrayData resolved_arr;
279
280                                 ObjectLock olock(arr);
281                                 for (const Value& value : arr) {
282                                         if (value.IsScalar()) {
283                                                 resolved_arr.push_back(InternalResolveMacros(value,
284                                                         resolvers, cr, missingMacro, EscapeCallback(), nullptr,
285                                                         false, recursionLevel + 1));
286                                         } else
287                                                 resolved_arr.push_back(value);
288                                 }
289
290                                 resolved_macro = new Array(std::move(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{0};
426         bool SkipKey{false};
427         bool RepeatKey{true};
428         bool SkipValue{false};
429         String Key;
430         Value AValue;
431
432         bool operator<(const CommandArgument& rhs) const
433         {
434                 return Order < rhs.Order;
435         }
436 };
437
438 Value MacroProcessor::ResolveArguments(const Value& command, const Dictionary::Ptr& arguments,
439         const MacroProcessor::ResolverList& resolvers, const CheckResult::Ptr& cr,
440         const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int recursionLevel)
441 {
442         if (useResolvedMacros)
443                 REQUIRE_NOT_NULL(resolvedMacros);
444
445         Value resolvedCommand;
446         if (!arguments || command.IsObjectType<Array>() || command.IsObjectType<Function>())
447                 resolvedCommand = MacroProcessor::ResolveMacros(command, resolvers, cr, nullptr,
448                         EscapeMacroShellArg, resolvedMacros, useResolvedMacros, recursionLevel + 1);
449         else {
450                 resolvedCommand = new Array({ command });
451         }
452
453         if (arguments) {
454                 std::vector<CommandArgument> args;
455
456                 ObjectLock olock(arguments);
457                 for (const Dictionary::Pair& kv : arguments) {
458                         const Value& arginfo = kv.second;
459
460                         CommandArgument arg;
461                         arg.Key = kv.first;
462
463                         bool required = false;
464                         Value argval;
465
466                         if (arginfo.IsObjectType<Dictionary>()) {
467                                 Dictionary::Ptr argdict = arginfo;
468                                 if (argdict->Contains("key"))
469                                         arg.Key = argdict->Get("key");
470                                 argval = argdict->Get("value");
471                                 if (argdict->Contains("required"))
472                                         required = argdict->Get("required");
473                                 arg.SkipKey = argdict->Get("skip_key");
474                                 if (argdict->Contains("repeat_key"))
475                                         arg.RepeatKey = argdict->Get("repeat_key");
476                                 arg.Order = argdict->Get("order");
477
478                                 Value set_if = argdict->Get("set_if");
479
480                                 if (!set_if.IsEmpty()) {
481                                         String missingMacro;
482                                         Value set_if_resolved = MacroProcessor::ResolveMacros(set_if, resolvers,
483                                                 cr, &missingMacro, MacroProcessor::EscapeCallback(), resolvedMacros,
484                                                 useResolvedMacros, recursionLevel + 1);
485
486                                         if (!missingMacro.IsEmpty())
487                                                 continue;
488
489                                         int value;
490
491                                         if (set_if_resolved == "true")
492                                                 value = 1;
493                                         else if (set_if_resolved == "false")
494                                                 value = 0;
495                                         else {
496                                                 try {
497                                                         value = Convert::ToLong(set_if_resolved);
498                                                 } catch (const std::exception& ex) {
499                                                         /* tried to convert a string */
500                                                         Log(LogWarning, "PluginUtility")
501                                                                 << "Error evaluating set_if value '" << set_if_resolved
502                                                                 << "' used in argument '" << arg.Key << "': " << ex.what();
503                                                         continue;
504                                                 }
505                                         }
506
507                                         if (!value)
508                                                 continue;
509                                 }
510                         }
511                         else
512                                 argval = arginfo;
513
514                         if (argval.IsEmpty())
515                                 arg.SkipValue = true;
516
517                         String missingMacro;
518                         arg.AValue = MacroProcessor::ResolveMacros(argval, resolvers,
519                                 cr, &missingMacro, MacroProcessor::EscapeCallback(), resolvedMacros,
520                                 useResolvedMacros, recursionLevel + 1);
521
522                         if (!missingMacro.IsEmpty()) {
523                                 if (required) {
524                                         BOOST_THROW_EXCEPTION(ScriptError("Non-optional macro '" + missingMacro + "' used in argument '" +
525                                                 arg.Key + "' is missing."));
526                                 }
527
528                                 continue;
529                         }
530
531                         args.emplace_back(std::move(arg));
532                 }
533
534                 std::sort(args.begin(), args.end());
535
536                 Array::Ptr command_arr = resolvedCommand;
537                 for (const CommandArgument& arg : args) {
538
539                         if (arg.AValue.IsObjectType<Dictionary>()) {
540                                 Log(LogWarning, "PluginUtility")
541                                         << "Tried to use dictionary in argument '" << arg.Key << "'.";
542                                 continue;
543                         } else if (arg.AValue.IsObjectType<Array>()) {
544                                 bool first = true;
545                                 Array::Ptr arr = static_cast<Array::Ptr>(arg.AValue);
546
547                                 ObjectLock olock(arr);
548                                 for (const Value& value : arr) {
549                                         bool add_key;
550
551                                         if (first) {
552                                                 first = false;
553                                                 add_key = !arg.SkipKey;
554                                         } else
555                                                 add_key = !arg.SkipKey && arg.RepeatKey;
556
557                                         AddArgumentHelper(command_arr, arg.Key, value, add_key, !arg.SkipValue);
558                                 }
559                         } else
560                                 AddArgumentHelper(command_arr, arg.Key, arg.AValue, !arg.SkipKey, !arg.SkipValue);
561                 }
562         }
563
564         return resolvedCommand;
565 }