]> granicus.if.org Git - icinga2/blob - lib/icinga/legacytimeperiod.cpp
Merge pull request #5861 from Icinga/fix/invalid-memory-access
[icinga2] / lib / icinga / legacytimeperiod.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/legacytimeperiod.hpp"
21 #include "base/function.hpp"
22 #include "base/convert.hpp"
23 #include "base/exception.hpp"
24 #include "base/objectlock.hpp"
25 #include "base/logger.hpp"
26 #include "base/debug.hpp"
27 #include "base/utility.hpp"
28 #include <boost/algorithm/string/split.hpp>
29 #include <boost/algorithm/string/classification.hpp>
30
31 using namespace icinga;
32
33 REGISTER_SCRIPTFUNCTION_NS(Internal, LegacyTimePeriod, &LegacyTimePeriod::ScriptFunc, "tp:begin:end");
34
35 bool LegacyTimePeriod::IsInTimeRange(tm *begin, tm *end, int stride, tm *reference)
36 {
37         time_t tsbegin, tsend, tsref;
38         tsbegin = mktime(begin);
39         tsend = mktime(end);
40         tsref = mktime(reference);
41
42         if (tsref < tsbegin || tsref > tsend)
43                 return false;
44
45         int daynumber = (tsref - tsbegin) / (24 * 60 * 60);
46
47         if (stride > 1 && daynumber % stride == 0)
48                 return false;
49
50         return true;
51 }
52
53 void LegacyTimePeriod::FindNthWeekday(int wday, int n, tm *reference)
54 {
55         int dir, seen = 0;
56
57         if (n > 0) {
58                 dir = 1;
59         } else {
60                 n *= -1;
61                 dir = -1;
62
63                 /* Negative days are relative to the next month. */
64                 reference->tm_mon++;
65         }
66
67         ASSERT(n > 0);
68
69         reference->tm_mday = 1;
70
71         for (;;) {
72                 mktime(reference);
73
74                 if (reference->tm_wday == wday) {
75                         seen++;
76
77                         if (seen == n)
78                                 return;
79                 }
80
81                 reference->tm_mday += dir;
82         }
83 }
84
85 int LegacyTimePeriod::WeekdayFromString(const String& daydef)
86 {
87         if (daydef == "sunday")
88                 return 0;
89         else if (daydef == "monday")
90                 return 1;
91         else if (daydef == "tuesday")
92                 return 2;
93         else if (daydef == "wednesday")
94                 return 3;
95         else if (daydef == "thursday")
96                 return 4;
97         else if (daydef == "friday")
98                 return 5;
99         else if (daydef == "saturday")
100                 return 6;
101         else
102                 return -1;
103 }
104
105 int LegacyTimePeriod::MonthFromString(const String& monthdef)
106 {
107         if (monthdef == "january")
108                 return 0;
109         else if (monthdef == "february")
110                 return 1;
111         else if (monthdef == "march")
112                 return 2;
113         else if (monthdef == "april")
114                 return 3;
115         else if (monthdef == "may")
116                 return 4;
117         else if (monthdef == "june")
118                 return 5;
119         else if (monthdef == "july")
120                 return 6;
121         else if (monthdef == "august")
122                 return 7;
123         else if (monthdef == "september")
124                 return 8;
125         else if (monthdef == "october")
126                 return 9;
127         else if (monthdef == "november")
128                 return 10;
129         else if (monthdef == "december")
130                 return 11;
131         else
132                 return -1;
133 }
134
135 void LegacyTimePeriod::ParseTimeSpec(const String& timespec, tm *begin, tm *end, tm *reference)
136 {
137         /* Let mktime() figure out whether we're in DST or not. */
138         reference->tm_isdst = -1;
139
140         /* YYYY-MM-DD */
141         if (timespec.GetLength() == 10 && timespec[4] == '-' && timespec[7] == '-') {
142                 int year = Convert::ToLong(timespec.SubStr(0, 4));
143                 int month = Convert::ToLong(timespec.SubStr(5, 2));
144                 int day = Convert::ToLong(timespec.SubStr(8, 2));
145
146                 if (month < 1 || month > 12)
147                         BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid month in time specification: " + timespec));
148                 if (day < 1 || day > 31)
149                         BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid day in time specification: " + timespec));
150
151                 if (begin) {
152                         *begin = *reference;
153                         begin->tm_year = year - 1900;
154                         begin->tm_mon = month - 1;
155                         begin->tm_mday = day;
156                         begin->tm_hour = 0;
157                         begin->tm_min = 0;
158                         begin->tm_sec = 0;
159                 }
160
161                 if (end) {
162                         *end = *reference;
163                         end->tm_year = year - 1900;
164                         end->tm_mon = month - 1;
165                         end->tm_mday = day;
166                         end->tm_hour = 24;
167                         end->tm_min = 0;
168                         end->tm_sec = 0;
169                 }
170
171                 return;
172         }
173
174         std::vector<String> tokens;
175         boost::algorithm::split(tokens, timespec, boost::is_any_of(" "));
176
177         int mon = -1;
178
179         if (tokens.size() > 1 && (tokens[0] == "day" || (mon = MonthFromString(tokens[0])) != -1)) {
180                 if (mon == -1)
181                         mon = reference->tm_mon;
182
183                 int mday = Convert::ToLong(tokens[1]);
184
185                 if (begin) {
186                         *begin = *reference;
187                         begin->tm_mon = mon;
188                         begin->tm_mday = mday;
189                         begin->tm_hour = 0;
190                         begin->tm_min = 0;
191                         begin->tm_sec = 0;
192
193                         /* Negative days are relative to the next month. */
194                         if (mday < 0) {
195                                 begin->tm_mday = mday * -1 - 1;
196                                 begin->tm_mon++;
197                         }
198                 }
199
200                 if (end) {
201                         *end = *reference;
202                         end->tm_mon = mon;
203                         end->tm_mday = mday;
204                         end->tm_hour = 24;
205                         end->tm_min = 0;
206                         end->tm_sec = 0;
207
208                         /* Negative days are relative to the next month. */
209                         if (mday < 0) {
210                                 end->tm_mday = mday * -1 - 1;
211                                 end->tm_mon++;
212                         }
213                 }
214
215                 return;
216         }
217
218         int wday;
219
220         if (tokens.size() >= 1 && (wday = WeekdayFromString(tokens[0])) != -1) {
221                 tm myref = *reference;
222
223                 if (tokens.size() > 2) {
224                         mon = MonthFromString(tokens[2]);
225
226                         if (mon == -1)
227                                 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid month in time specification: " + timespec));
228
229                         myref.tm_mon = mon;
230                 }
231
232                 int n = 0;
233
234                 if (tokens.size() > 1)
235                         n = Convert::ToLong(tokens[1]);
236
237                 if (begin) {
238                         *begin = myref;
239
240                         if (tokens.size() > 1)
241                                 FindNthWeekday(wday, n, begin);
242                         else
243                                 begin->tm_mday += (7 - begin->tm_wday + wday) % 7;
244
245                         begin->tm_hour = 0;
246                         begin->tm_min = 0;
247                         begin->tm_sec = 0;
248                 }
249
250                 if (end) {
251                         *end = myref;
252
253                         if (tokens.size() > 1)
254                                 FindNthWeekday(wday, n, end);
255                         else
256                                 end->tm_mday += (7 - end->tm_wday + wday) % 7;
257
258                         end->tm_hour = 0;
259                         end->tm_min = 0;
260                         end->tm_sec = 0;
261                         end->tm_mday++;
262                 }
263
264                 return;
265         }
266
267         BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid time specification: " + timespec));
268 }
269
270 void LegacyTimePeriod::ParseTimeRange(const String& timerange, tm *begin, tm *end, int *stride, tm *reference)
271 {
272         String def = timerange;
273
274         /* Figure out the stride. */
275         size_t pos = def.FindFirstOf('/');
276
277         if (pos != String::NPos) {
278                 String strStride = def.SubStr(pos + 1).Trim();
279                 *stride = Convert::ToLong(strStride);
280
281                 /* Remove the stride parameter from the definition. */
282                 def = def.SubStr(0, pos);
283         } else {
284                 *stride = 1; /* User didn't specify anything, assume default. */
285         }
286
287         /* Figure out whether the user has specified two dates. */
288         pos = def.Find("- ");
289
290         if (pos != String::NPos) {
291                 String first = def.SubStr(0, pos).Trim();
292
293                 String second = def.SubStr(pos + 1).Trim();
294
295                 ParseTimeSpec(first, begin, nullptr, reference);
296
297                 /* If the second definition starts with a number we need
298                  * to add the first word from the first definition, e.g.:
299                  * day 1 - 15 --> "day 15" */
300                 bool is_number = true;
301                 size_t xpos = second.FindFirstOf(' ');
302                 String fword = second.SubStr(0, xpos);
303
304                 try {
305                         Convert::ToLong(fword);
306                 } catch (...) {
307                         is_number = false;
308                 }
309
310                 if (is_number) {
311                         xpos = first.FindFirstOf(' ');
312                         ASSERT(xpos != String::NPos);
313                         second = first.SubStr(0, xpos + 1) + second;
314                 }
315
316                 ParseTimeSpec(second, nullptr, end, reference);
317         } else {
318                 ParseTimeSpec(def, begin, end, reference);
319         }
320 }
321
322 bool LegacyTimePeriod::IsInDayDefinition(const String& daydef, tm *reference)
323 {
324         tm begin, end;
325         int stride;
326
327         ParseTimeRange(daydef, &begin, &end, &stride, reference);
328
329         Log(LogDebug, "LegacyTimePeriod")
330             << "ParseTimeRange: '" << daydef << "' => " << mktime(&begin)
331             << " -> " << mktime(&end) << ", stride: " << stride;
332
333         return IsInTimeRange(&begin, &end, stride, reference);
334 }
335
336 void LegacyTimePeriod::ProcessTimeRangeRaw(const String& timerange, tm *reference, tm *begin, tm *end)
337 {
338         std::vector<String> times;
339
340         boost::algorithm::split(times, timerange, boost::is_any_of("-"));
341
342         if (times.size() != 2)
343                 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid timerange: " + timerange));
344
345         std::vector<String> hd1, hd2;
346         boost::algorithm::split(hd1, times[0], boost::is_any_of(":"));
347
348         if (hd1.size() != 2)
349                 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid time specification: " + times[0]));
350
351         boost::algorithm::split(hd2, times[1], boost::is_any_of(":"));
352
353         if (hd2.size() != 2)
354                 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid time specification: " + times[1]));
355
356         *begin = *reference;
357         begin->tm_sec = 0;
358         begin->tm_min = Convert::ToLong(hd1[1]);
359         begin->tm_hour = Convert::ToLong(hd1[0]);
360
361         *end = *reference;
362         end->tm_sec = 0;
363         end->tm_min = Convert::ToLong(hd2[1]);
364         end->tm_hour = Convert::ToLong(hd2[0]);
365
366         if (begin->tm_hour * 3600 + begin->tm_min * 60 + begin->tm_sec >=
367             end->tm_hour * 3600 + end->tm_min * 60 + end->tm_sec)
368                 BOOST_THROW_EXCEPTION(std::invalid_argument("Time period segment ends before it begins"));
369 }
370
371 Dictionary::Ptr LegacyTimePeriod::ProcessTimeRange(const String& timestamp, tm *reference)
372 {
373         tm begin, end;
374
375         ProcessTimeRangeRaw(timestamp, reference, &begin, &end);
376
377         Dictionary::Ptr segment = new Dictionary();
378         segment->Set("begin", (long)mktime(&begin));
379         segment->Set("end", (long)mktime(&end));
380         return segment;
381 }
382
383 void LegacyTimePeriod::ProcessTimeRanges(const String& timeranges, tm *reference, const Array::Ptr& result)
384 {
385         std::vector<String> ranges;
386
387         boost::algorithm::split(ranges, timeranges, boost::is_any_of(","));
388
389         for (const String& range : ranges) {
390                 Dictionary::Ptr segment = ProcessTimeRange(range, reference);
391
392                 if (segment->Get("begin") >= segment->Get("end"))
393                         continue;
394
395                 result->Add(segment);
396         }
397 }
398
399 Dictionary::Ptr LegacyTimePeriod::FindNextSegment(const String& daydef, const String& timeranges, tm *reference)
400 {
401         tm begin, end, iter, ref;
402         time_t tsend, tsiter, tsref;
403         int stride;
404
405         for (int pass = 1; pass <= 2; pass++) {
406                 if (pass == 1) {
407                         ref = *reference;
408                 } else {
409                         ref = end;
410                         ref.tm_mday++;
411                 }
412
413                 tsref = mktime(&ref);
414
415                 ParseTimeRange(daydef, &begin, &end, &stride, &ref);
416
417                 iter = begin;
418
419                 tsend = mktime(&end);
420
421                 do {
422                         if (IsInTimeRange(&begin, &end, stride, &iter)) {
423                                 Array::Ptr segments = new Array();
424                                 ProcessTimeRanges(timeranges, &iter, segments);
425
426                                 Dictionary::Ptr bestSegment;
427                                 double bestBegin;
428
429                                 ObjectLock olock(segments);
430                                 for (const Dictionary::Ptr& segment : segments) {
431                                         double begin = segment->Get("begin");
432
433                                         if (begin < tsref)
434                                                 continue;
435
436                                         if (!bestSegment || begin < bestBegin) {
437                                                 bestSegment = segment;
438                                                 bestBegin = begin;
439                                         }
440                                 }
441
442                                 if (bestSegment)
443                                         return bestSegment;
444                         }
445
446                         iter.tm_mday++;
447                         iter.tm_hour = 0;
448                         iter.tm_min = 0;
449                         iter.tm_sec = 0;
450                         tsiter = mktime(&iter);
451                 } while (tsiter < tsend);
452         }
453
454         return nullptr;
455 }
456
457 Array::Ptr LegacyTimePeriod::ScriptFunc(const TimePeriod::Ptr& tp, double begin, double end)
458 {
459         Array::Ptr segments = new Array();
460
461         Dictionary::Ptr ranges = tp->GetRanges();
462
463         if (ranges) {
464                 for (int i = 0; i <= (end - begin) / (24 * 60 * 60); i++) {
465                         time_t refts = begin + i * 24 * 60 * 60;
466                         tm reference = Utility::LocalTime(refts);
467
468 #ifdef I2_DEBUG
469                         Log(LogDebug, "LegacyTimePeriod")
470                             << "Checking reference time " << refts;
471 #endif /* I2_DEBUG */
472
473                         ObjectLock olock(ranges);
474                         for (const Dictionary::Pair& kv : ranges) {
475                                 if (!IsInDayDefinition(kv.first, &reference)) {
476 #ifdef I2_DEBUG
477                                         Log(LogDebug, "LegacyTimePeriod")
478                                             << "Not in day definition '" << kv.first << "'.";
479 #endif /* I2_DEBUG */
480                                         continue;
481                                 }
482
483 #ifdef I2_DEBUG
484                                 Log(LogDebug, "LegacyTimePeriod")
485                                     << "In day definition '" << kv.first << "'.";
486 #endif /* I2_DEBUG */
487
488                                 ProcessTimeRanges(kv.second, &reference, segments);
489                         }
490                 }
491         }
492
493         Log(LogDebug, "LegacyTimePeriod")
494             << "Legacy timeperiod update returned " << segments->GetLength() << " segments.";
495
496         return segments;
497 }