]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/calendarspec.c
network: warn if Address= is specified without prefixlen
[thirdparty/systemd.git] / src / shared / calendarspec.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
36697dc0 2
11c3a366 3#include <alloca.h>
9dfa81a0 4#include <ctype.h>
11c3a366 5#include <errno.h>
c0aebb4b 6#include <limits.h>
11c3a366
TA
7#include <stddef.h>
8#include <stdio.h>
0d536673 9#include <stdio_ext.h>
36697dc0
LP
10#include <stdlib.h>
11#include <string.h>
48d26c01 12#include <sys/mman.h>
11c3a366 13#include <time.h>
36697dc0 14
b5efdb8a 15#include "alloc-util.h"
36697dc0 16#include "calendarspec.h"
0d39fa9c 17#include "fileio.h"
11c3a366 18#include "macro.h"
93cc7779 19#include "parse-util.h"
dccca82b 20#include "process-util.h"
cf0fbc49 21#include "string-util.h"
48d26c01 22#include "time-util.h"
36697dc0 23
a2eb5ea7
DC
24#define BITS_WEEKDAYS 127
25#define MIN_YEAR 1970
26#define MAX_YEAR 2199
489464d0 27
74353158
ZJS
28/* An arbitrary limit on the length of the chains of components. We don't want to
29 * build a very long linked list, which would be slow to iterate over and might cause
30 * our stack to overflow. It's unlikely that legitimate uses require more than a few
31 * linked compenents anyway. */
32#define CALENDARSPEC_COMPONENTS_MAX 240
33
36697dc0
LP
34static void free_chain(CalendarComponent *c) {
35 CalendarComponent *n;
36
37 while (c) {
38 n = c->next;
39 free(c);
40 c = n;
41 }
42}
43
7c123d49 44CalendarSpec* calendar_spec_free(CalendarSpec *c) {
0b76b4d8
LP
45
46 if (!c)
7c123d49 47 return NULL;
36697dc0
LP
48
49 free_chain(c->year);
50 free_chain(c->month);
51 free_chain(c->day);
52 free_chain(c->hour);
53 free_chain(c->minute);
436dd70f 54 free_chain(c->microsecond);
48d26c01 55 free(c->timezone);
36697dc0 56
7c123d49 57 return mfree(c);
36697dc0
LP
58}
59
93bab288
YW
60static int component_compare(CalendarComponent * const *a, CalendarComponent * const *b) {
61 int r;
36697dc0 62
93bab288
YW
63 r = CMP((*a)->start, (*b)->start);
64 if (r != 0)
65 return r;
36697dc0 66
93bab288
YW
67 r = CMP((*a)->stop, (*b)->stop);
68 if (r != 0)
69 return r;
a2eb5ea7 70
93bab288 71 return CMP((*a)->repeat, (*b)->repeat);
36697dc0
LP
72}
73
a2eb5ea7 74static void normalize_chain(CalendarComponent **c) {
36697dc0 75 CalendarComponent **b, *i, **j, *next;
da6053d0 76 size_t n = 0, k;
36697dc0
LP
77
78 assert(c);
79
a2eb5ea7 80 for (i = *c; i; i = i->next) {
36697dc0
LP
81 n++;
82
a2eb5ea7 83 /*
e78fce48 84 * While we're counting the chain, also normalize `stop`
a2eb5ea7
DC
85 * so the length of the range is a multiple of `repeat`
86 */
c0aebb4b 87 if (i->stop > i->start && i->repeat > 0)
e78fce48 88 i->stop -= (i->stop - i->start) % i->repeat;
a2eb5ea7
DC
89
90 }
91
36697dc0
LP
92 if (n <= 1)
93 return;
94
cf409d15 95 j = b = newa(CalendarComponent*, n);
36697dc0
LP
96 for (i = *c; i; i = i->next)
97 *(j++) = i;
98
93bab288 99 typesafe_qsort(b, n, component_compare);
36697dc0
LP
100
101 b[n-1]->next = NULL;
102 next = b[n-1];
103
104 /* Drop non-unique entries */
105 for (k = n-1; k > 0; k--) {
963e3d83 106 if (component_compare(&b[k-1], &next) == 0) {
36697dc0
LP
107 free(b[k-1]);
108 continue;
109 }
110
111 b[k-1]->next = next;
112 next = b[k-1];
113 }
114
115 *c = next;
116}
117
118static void fix_year(CalendarComponent *c) {
119 /* Turns 12 → 2012, 89 → 1989 */
120
9ed794a3 121 while (c) {
e78fce48
DC
122 if (c->start >= 0 && c->start < 70)
123 c->start += 2000;
36697dc0 124
e78fce48
DC
125 if (c->stop >= 0 && c->stop < 70)
126 c->stop += 2000;
a2eb5ea7 127
e78fce48
DC
128 if (c->start >= 70 && c->start < 100)
129 c->start += 1900;
36697dc0 130
e78fce48
DC
131 if (c->stop >= 70 && c->stop < 100)
132 c->stop += 1900;
a2eb5ea7 133
482f3b54 134 c = c->next;
36697dc0
LP
135 }
136}
137
138int calendar_spec_normalize(CalendarSpec *c) {
139 assert(c);
140
10e859a2
LP
141 if (streq_ptr(c->timezone, "UTC")) {
142 c->utc = true;
143 c->timezone = mfree(c->timezone);
144 }
145
489464d0 146 if (c->weekdays_bits <= 0 || c->weekdays_bits >= BITS_WEEKDAYS)
36697dc0
LP
147 c->weekdays_bits = -1;
148
8ea80351
DC
149 if (c->end_of_month && !c->day)
150 c->end_of_month = false;
151
36697dc0
LP
152 fix_year(c->year);
153
a2eb5ea7
DC
154 normalize_chain(&c->year);
155 normalize_chain(&c->month);
156 normalize_chain(&c->day);
157 normalize_chain(&c->hour);
158 normalize_chain(&c->minute);
159 normalize_chain(&c->microsecond);
36697dc0
LP
160
161 return 0;
162}
163
c58b1b3a 164_pure_ static bool chain_valid(CalendarComponent *c, int from, int to, bool end_of_month) {
e127f26b
ZJS
165 assert(to >= from);
166
36697dc0
LP
167 if (!c)
168 return true;
169
a2eb5ea7
DC
170 /* Forbid dates more than 28 days from the end of the month */
171 if (end_of_month)
172 to -= 3;
173
e78fce48 174 if (c->start < from || c->start > to)
36697dc0
LP
175 return false;
176
e127f26b
ZJS
177 /* Avoid overly large values that could cause overflow */
178 if (c->repeat > to - from)
179 return false;
180
8ea80351
DC
181 /*
182 * c->repeat must be short enough so at least one repetition may
183 * occur before the end of the interval. For dates scheduled
e78fce48 184 * relative to the end of the month, c->start and c->stop
a2eb5ea7 185 * correspond to the Nth last day of the month.
8ea80351 186 */
e78fce48
DC
187 if (c->stop >= 0) {
188 if (c->stop < from || c ->stop > to)
a2eb5ea7 189 return false;
8ea80351 190
e78fce48 191 if (c->start + c->repeat > c->stop)
a2eb5ea7
DC
192 return false;
193 } else {
e78fce48 194 if (end_of_month && c->start - c->repeat < from)
a2eb5ea7
DC
195 return false;
196
e78fce48 197 if (!end_of_month && c->start + c->repeat > to)
a2eb5ea7
DC
198 return false;
199 }
36697dc0
LP
200
201 if (c->next)
c58b1b3a 202 return chain_valid(c->next, from, to, end_of_month);
36697dc0
LP
203
204 return true;
205}
206
44a6b1b6 207_pure_ bool calendar_spec_valid(CalendarSpec *c) {
36697dc0
LP
208 assert(c);
209
489464d0 210 if (c->weekdays_bits > BITS_WEEKDAYS)
36697dc0
LP
211 return false;
212
8ea80351 213 if (!chain_valid(c->year, MIN_YEAR, MAX_YEAR, false))
36697dc0
LP
214 return false;
215
8ea80351 216 if (!chain_valid(c->month, 1, 12, false))
36697dc0
LP
217 return false;
218
8ea80351 219 if (!chain_valid(c->day, 1, 31, c->end_of_month))
36697dc0
LP
220 return false;
221
8ea80351 222 if (!chain_valid(c->hour, 0, 23, false))
36697dc0
LP
223 return false;
224
8ea80351 225 if (!chain_valid(c->minute, 0, 59, false))
36697dc0
LP
226 return false;
227
8ea80351 228 if (!chain_valid(c->microsecond, 0, 60*USEC_PER_SEC-1, false))
36697dc0
LP
229 return false;
230
231 return true;
232}
233
234static void format_weekdays(FILE *f, const CalendarSpec *c) {
235 static const char *const days[] = {
236 "Mon",
237 "Tue",
238 "Wed",
239 "Thu",
240 "Fri",
241 "Sat",
242 "Sun"
243 };
244
245 int l, x;
e638d050 246 bool need_comma = false;
36697dc0
LP
247
248 assert(f);
249 assert(c);
489464d0 250 assert(c->weekdays_bits > 0 && c->weekdays_bits <= BITS_WEEKDAYS);
36697dc0
LP
251
252 for (x = 0, l = -1; x < (int) ELEMENTSOF(days); x++) {
253
254 if (c->weekdays_bits & (1 << x)) {
255
256 if (l < 0) {
e638d050 257 if (need_comma)
0d536673 258 fputc(',', f);
36697dc0 259 else
e638d050 260 need_comma = true;
36697dc0 261
0d536673 262 fputs(days[x], f);
36697dc0
LP
263 l = x;
264 }
265
266 } else if (l >= 0) {
267
268 if (x > l + 1) {
0d536673
LP
269 fputs(x > l + 2 ? ".." : ",", f);
270 fputs(days[x-1], f);
36697dc0
LP
271 }
272
273 l = -1;
274 }
275 }
276
277 if (l >= 0 && x > l + 1) {
0d536673
LP
278 fputs(x > l + 2 ? ".." : ",", f);
279 fputs(days[x-1], f);
36697dc0
LP
280 }
281}
282
436dd70f 283static void format_chain(FILE *f, int space, const CalendarComponent *c, bool usec) {
7c250321 284 int d = usec ? (int) USEC_PER_SEC : 1;
9904dc00 285
36697dc0
LP
286 assert(f);
287
288 if (!c) {
0d536673 289 fputc('*', f);
36697dc0
LP
290 return;
291 }
292
482f3b54 293 if (usec && c->start == 0 && c->repeat == USEC_PER_SEC && !c->next) {
0d536673 294 fputc('*', f);
482f3b54
DC
295 return;
296 }
297
e78fce48 298 assert(c->start >= 0);
7c250321 299
e78fce48
DC
300 fprintf(f, "%0*i", space, c->start / d);
301 if (c->start % d > 0)
302 fprintf(f, ".%06i", c->start % d);
7c250321 303
e78fce48
DC
304 if (c->stop > 0)
305 fprintf(f, "..%0*i", space, c->stop / d);
306 if (c->stop % d > 0)
307 fprintf(f, ".%06i", c->stop % d);
a2eb5ea7 308
e78fce48 309 if (c->repeat > 0 && !(c->stop > 0 && c->repeat == d))
7c250321 310 fprintf(f, "/%i", c->repeat / d);
a2eb5ea7 311 if (c->repeat % d > 0)
7c250321 312 fprintf(f, ".%06i", c->repeat % d);
36697dc0 313
a2eb5ea7 314 if (c->next) {
0d536673 315 fputc(',', f);
436dd70f 316 format_chain(f, space, c->next, usec);
36697dc0
LP
317 }
318}
319
320int calendar_spec_to_string(const CalendarSpec *c, char **p) {
321 char *buf = NULL;
322 size_t sz = 0;
323 FILE *f;
dacd6cee 324 int r;
36697dc0
LP
325
326 assert(c);
327 assert(p);
328
329 f = open_memstream(&buf, &sz);
330 if (!f)
331 return -ENOMEM;
332
0d536673
LP
333 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
334
489464d0 335 if (c->weekdays_bits > 0 && c->weekdays_bits <= BITS_WEEKDAYS) {
36697dc0 336 format_weekdays(f, c);
0d536673 337 fputc(' ', f);
36697dc0
LP
338 }
339
436dd70f 340 format_chain(f, 4, c->year, false);
0d536673 341 fputc('-', f);
436dd70f 342 format_chain(f, 2, c->month, false);
0d536673 343 fputc(c->end_of_month ? '~' : '-', f);
436dd70f 344 format_chain(f, 2, c->day, false);
0d536673 345 fputc(' ', f);
436dd70f 346 format_chain(f, 2, c->hour, false);
0d536673 347 fputc(':', f);
436dd70f 348 format_chain(f, 2, c->minute, false);
0d536673 349 fputc(':', f);
482f3b54 350 format_chain(f, 2, c->microsecond, true);
36697dc0 351
51ffa239 352 if (c->utc)
0d536673 353 fputs(" UTC", f);
48d26c01 354 else if (c->timezone != NULL) {
0d536673
LP
355 fputc(' ', f);
356 fputs(c->timezone, f);
48d26c01 357 } else if (IN_SET(c->dst, 0, 1)) {
21b3a0fc
LP
358
359 /* If daylight saving is explicitly on or off, let's show the used timezone. */
360
361 tzset();
362
363 if (!isempty(tzname[c->dst])) {
0d536673
LP
364 fputc(' ', f);
365 fputs(tzname[c->dst], f);
21b3a0fc
LP
366 }
367 }
51ffa239 368
dacd6cee
LP
369 r = fflush_and_check(f);
370 if (r < 0) {
36697dc0
LP
371 free(buf);
372 fclose(f);
dacd6cee 373 return r;
36697dc0
LP
374 }
375
376 fclose(f);
377
378 *p = buf;
379 return 0;
380}
381
382static int parse_weekdays(const char **p, CalendarSpec *c) {
383 static const struct {
384 const char *name;
385 const int nr;
386 } day_nr[] = {
387 { "Monday", 0 },
388 { "Mon", 0 },
389 { "Tuesday", 1 },
390 { "Tue", 1 },
391 { "Wednesday", 2 },
392 { "Wed", 2 },
393 { "Thursday", 3 },
394 { "Thu", 3 },
395 { "Friday", 4 },
396 { "Fri", 4 },
397 { "Saturday", 5 },
398 { "Sat", 5 },
399 { "Sunday", 6 },
400 { "Sun", 6 }
401 };
402
403 int l = -1;
404 bool first = true;
405
406 assert(p);
407 assert(*p);
408 assert(c);
409
410 for (;;) {
da6053d0 411 size_t i;
36697dc0 412
36697dc0
LP
413 for (i = 0; i < ELEMENTSOF(day_nr); i++) {
414 size_t skip;
415
416 if (!startswith_no_case(*p, day_nr[i].name))
417 continue;
418
419 skip = strlen(day_nr[i].name);
420
4c701096 421 if (!IN_SET((*p)[skip], 0, '-', '.', ',', ' '))
36697dc0
LP
422 return -EINVAL;
423
424 c->weekdays_bits |= 1 << day_nr[i].nr;
425
426 if (l >= 0) {
427 int j;
428
429 if (l > day_nr[i].nr)
430 return -EINVAL;
431
432 for (j = l + 1; j < day_nr[i].nr; j++)
433 c->weekdays_bits |= 1 << j;
434 }
435
436 *p += skip;
437 break;
438 }
439
440 /* Couldn't find this prefix, so let's assume the
441 weekday was not specified and let's continue with
442 the date */
443 if (i >= ELEMENTSOF(day_nr))
444 return first ? 0 : -EINVAL;
445
446 /* We reached the end of the string */
447 if (**p == 0)
448 return 0;
449
450 /* We reached the end of the weekday spec part */
451 if (**p == ' ') {
452 *p += strspn(*p, " ");
453 return 0;
454 }
455
e638d050
DC
456 if (**p == '.') {
457 if (l >= 0)
458 return -EINVAL;
459
460 if ((*p)[1] != '.')
461 return -EINVAL;
462
463 l = day_nr[i].nr;
6bae2fd4 464 *p += 2;
e638d050
DC
465
466 /* Support ranges with "-" for backwards compatibility */
467 } else if (**p == '-') {
36697dc0
LP
468 if (l >= 0)
469 return -EINVAL;
470
471 l = day_nr[i].nr;
6bae2fd4
DC
472 *p += 1;
473
474 } else if (**p == ',') {
36697dc0 475 l = -1;
6bae2fd4
DC
476 *p += 1;
477 }
478
482f3b54 479 /* Allow a trailing comma but not an open range */
4c701096 480 if (IN_SET(**p, 0, ' ')) {
6bae2fd4
DC
481 *p += strspn(*p, " ");
482 return l < 0 ? 0 : -EINVAL;
483 }
36697dc0 484
36697dc0
LP
485 first = false;
486 }
487}
488
d80e5b74
ZJS
489static int parse_one_number(const char *p, const char **e, unsigned long *ret) {
490 char *ee = NULL;
491 unsigned long value;
492
493 errno = 0;
494 value = strtoul(p, &ee, 10);
495 if (errno > 0)
496 return -errno;
497 if (ee == p)
498 return -EINVAL;
499
500 *ret = value;
501 *e = ee;
502 return 0;
503}
504
c0aebb4b 505static int parse_component_decimal(const char **p, bool usec, int *res) {
436dd70f
HV
506 unsigned long value;
507 const char *e = NULL;
436dd70f 508 int r;
36697dc0 509
9dfa81a0
DC
510 if (!isdigit(**p))
511 return -EINVAL;
512
d80e5b74
ZJS
513 r = parse_one_number(*p, &e, &value);
514 if (r < 0)
515 return r;
36697dc0 516
436dd70f
HV
517 if (usec) {
518 if (value * USEC_PER_SEC / USEC_PER_SEC != value)
36697dc0
LP
519 return -ERANGE;
520
436dd70f 521 value *= USEC_PER_SEC;
436dd70f 522
60bf5836
DC
523 /* One "." is a decimal point, but ".." is a range separator */
524 if (e[0] == '.' && e[1] != '.') {
525 unsigned add;
36ff0c97 526
436dd70f
HV
527 e++;
528 r = parse_fractional_part_u(&e, 6, &add);
529 if (r < 0)
530 return r;
531
532 if (add + value < value)
533 return -ERANGE;
534 value += add;
535 }
536 }
537
c0aebb4b
DC
538 if (value > INT_MAX)
539 return -ERANGE;
540
436dd70f
HV
541 *p = e;
542 *res = value;
543
544 return 0;
545}
546
32b52369
DC
547static int const_chain(int value, CalendarComponent **c) {
548 CalendarComponent *cc = NULL;
549
550 assert(c);
551
552 cc = new0(CalendarComponent, 1);
553 if (!cc)
554 return -ENOMEM;
555
e78fce48
DC
556 cc->start = value;
557 cc->stop = -1;
32b52369
DC
558 cc->repeat = 0;
559 cc->next = *c;
560
561 *c = cc;
562
563 return 0;
564}
565
d80e5b74
ZJS
566static int calendarspec_from_time_t(CalendarSpec *c, time_t time) {
567 struct tm tm;
568 CalendarComponent *year = NULL, *month = NULL, *day = NULL, *hour = NULL, *minute = NULL, *us = NULL;
569 int r;
570
55a30fd4
ZJS
571 if (!gmtime_r(&time, &tm))
572 return -ERANGE;
d80e5b74
ZJS
573
574 r = const_chain(tm.tm_year + 1900, &year);
575 if (r < 0)
576 return r;
577
578 r = const_chain(tm.tm_mon + 1, &month);
579 if (r < 0)
580 return r;
581
582 r = const_chain(tm.tm_mday, &day);
583 if (r < 0)
584 return r;
585
586 r = const_chain(tm.tm_hour, &hour);
587 if (r < 0)
588 return r;
589
590 r = const_chain(tm.tm_min, &minute);
591 if (r < 0)
592 return r;
593
594 r = const_chain(tm.tm_sec * USEC_PER_SEC, &us);
595 if (r < 0)
596 return r;
597
598 c->utc = true;
599 c->year = year;
600 c->month = month;
601 c->day = day;
602 c->hour = hour;
603 c->minute = minute;
604 c->microsecond = us;
605 return 0;
606}
607
74353158 608static int prepend_component(const char **p, bool usec, unsigned nesting, CalendarComponent **c) {
c0aebb4b 609 int r, start, stop = -1, repeat = 0;
436dd70f 610 CalendarComponent *cc;
74353158 611 const char *e = *p;
436dd70f
HV
612
613 assert(p);
614 assert(c);
615
74353158
ZJS
616 if (nesting > CALENDARSPEC_COMPONENTS_MAX)
617 return -ENOBUFS;
436dd70f 618
e78fce48 619 r = parse_component_decimal(&e, usec, &start);
436dd70f
HV
620 if (r < 0)
621 return r;
622
a2eb5ea7
DC
623 if (e[0] == '.' && e[1] == '.') {
624 e += 2;
e78fce48 625 r = parse_component_decimal(&e, usec, &stop);
a2eb5ea7
DC
626 if (r < 0)
627 return r;
628
629 repeat = usec ? USEC_PER_SEC : 1;
630 }
631
436dd70f
HV
632 if (*e == '/') {
633 e++;
634 r = parse_component_decimal(&e, usec, &repeat);
635 if (r < 0)
636 return r;
637
638 if (repeat == 0)
639 return -ERANGE;
36697dc0
LP
640 }
641
4c701096 642 if (!IN_SET(*e, 0, ' ', ',', '-', '~', ':'))
36697dc0
LP
643 return -EINVAL;
644
645 cc = new0(CalendarComponent, 1);
646 if (!cc)
647 return -ENOMEM;
648
e78fce48
DC
649 cc->start = start;
650 cc->stop = stop;
36697dc0
LP
651 cc->repeat = repeat;
652 cc->next = *c;
653
654 *p = e;
655 *c = cc;
656
657 if (*e ==',') {
658 *p += 1;
74353158 659 return prepend_component(p, usec, nesting + 1, c);
36697dc0
LP
660 }
661
662 return 0;
663}
664
436dd70f 665static int parse_chain(const char **p, bool usec, CalendarComponent **c) {
36697dc0
LP
666 const char *t;
667 CalendarComponent *cc = NULL;
668 int r;
669
670 assert(p);
671 assert(c);
672
673 t = *p;
674
675 if (t[0] == '*') {
436dd70f
HV
676 if (usec) {
677 r = const_chain(0, c);
678 if (r < 0)
679 return r;
680 (*c)->repeat = USEC_PER_SEC;
681 } else
682 *c = NULL;
683
36697dc0 684 *p = t + 1;
36697dc0
LP
685 return 0;
686 }
687
74353158 688 r = prepend_component(&t, usec, 0, &cc);
36697dc0
LP
689 if (r < 0) {
690 free_chain(cc);
691 return r;
692 }
693
694 *p = t;
695 *c = cc;
696 return 0;
697}
698
36697dc0
LP
699static int parse_date(const char **p, CalendarSpec *c) {
700 const char *t;
701 int r;
702 CalendarComponent *first, *second, *third;
703
704 assert(p);
705 assert(*p);
706 assert(c);
707
708 t = *p;
709
710 if (*t == 0)
711 return 0;
712
d80e5b74
ZJS
713 /* @TIMESTAMP — UNIX time in seconds since the epoch */
714 if (*t == '@') {
715 unsigned long value;
716 time_t time;
717
718 r = parse_one_number(t + 1, &t, &value);
719 if (r < 0)
720 return r;
721
722 time = value;
723 if ((unsigned long) time != value)
724 return -ERANGE;
725
726 r = calendarspec_from_time_t(c, time);
727 if (r < 0)
728 return r;
729
730 *p = t;
731 return 1; /* finito, don't parse H:M:S after that */
732 }
733
436dd70f 734 r = parse_chain(&t, false, &first);
36697dc0
LP
735 if (r < 0)
736 return r;
737
738 /* Already the end? A ':' as separator? In that case this was a time, not a date */
4c701096 739 if (IN_SET(*t, 0, ':')) {
36697dc0
LP
740 free_chain(first);
741 return 0;
742 }
743
8ea80351
DC
744 if (*t == '~')
745 c->end_of_month = true;
746 else if (*t != '-') {
36697dc0
LP
747 free_chain(first);
748 return -EINVAL;
749 }
750
751 t++;
436dd70f 752 r = parse_chain(&t, false, &second);
36697dc0
LP
753 if (r < 0) {
754 free_chain(first);
755 return r;
756 }
757
758 /* Got two parts, hence it's month and day */
4c701096 759 if (IN_SET(*t, 0, ' ')) {
36697dc0
LP
760 *p = t + strspn(t, " ");
761 c->month = first;
762 c->day = second;
763 return 0;
fc2371c7
DC
764 } else if (c->end_of_month) {
765 free_chain(first);
766 free_chain(second);
8ea80351 767 return -EINVAL;
fc2371c7 768 }
36697dc0 769
8ea80351
DC
770 if (*t == '~')
771 c->end_of_month = true;
772 else if (*t != '-') {
36697dc0
LP
773 free_chain(first);
774 free_chain(second);
775 return -EINVAL;
776 }
777
778 t++;
436dd70f 779 r = parse_chain(&t, false, &third);
36697dc0
LP
780 if (r < 0) {
781 free_chain(first);
782 free_chain(second);
783 return r;
784 }
785
8ea80351 786 /* Got three parts, hence it is year, month and day */
4c701096 787 if (IN_SET(*t, 0, ' ')) {
36697dc0
LP
788 *p = t + strspn(t, " ");
789 c->year = first;
790 c->month = second;
791 c->day = third;
792 return 0;
793 }
794
795 free_chain(first);
796 free_chain(second);
797 free_chain(third);
798 return -EINVAL;
799}
800
519cffec 801static int parse_calendar_time(const char **p, CalendarSpec *c) {
36697dc0
LP
802 CalendarComponent *h = NULL, *m = NULL, *s = NULL;
803 const char *t;
804 int r;
805
806 assert(p);
807 assert(*p);
808 assert(c);
809
810 t = *p;
811
408a51e1
DC
812 /* If no time is specified at all, then this means 00:00:00 */
813 if (*t == 0)
814 goto null_hour;
36697dc0 815
436dd70f 816 r = parse_chain(&t, false, &h);
36697dc0
LP
817 if (r < 0)
818 goto fail;
819
820 if (*t != ':') {
821 r = -EINVAL;
822 goto fail;
823 }
824
825 t++;
436dd70f 826 r = parse_chain(&t, false, &m);
36697dc0
LP
827 if (r < 0)
828 goto fail;
829
830 /* Already at the end? Then it's hours and minutes, and seconds are 0 */
c0df71fa
DC
831 if (*t == 0)
832 goto null_second;
36697dc0
LP
833
834 if (*t != ':') {
835 r = -EINVAL;
836 goto fail;
837 }
838
839 t++;
436dd70f 840 r = parse_chain(&t, true, &s);
36697dc0
LP
841 if (r < 0)
842 goto fail;
843
844 /* At the end? Then it's hours, minutes and seconds */
845 if (*t == 0)
846 goto finish;
847
848 r = -EINVAL;
849 goto fail;
850
851null_hour:
852 r = const_chain(0, &h);
853 if (r < 0)
854 goto fail;
855
856 r = const_chain(0, &m);
857 if (r < 0)
858 goto fail;
859
860null_second:
861 r = const_chain(0, &s);
862 if (r < 0)
863 goto fail;
864
865finish:
866 *p = t;
867 c->hour = h;
868 c->minute = m;
436dd70f
HV
869 c->microsecond = s;
870
36697dc0
LP
871 return 0;
872
873fail:
874 free_chain(h);
875 free_chain(m);
876 free_chain(s);
877 return r;
878}
879
880int calendar_spec_from_string(const char *p, CalendarSpec **spec) {
21b3a0fc 881 const char *utc;
921b5987 882 _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
36697dc0
LP
883 int r;
884
885 assert(p);
886 assert(spec);
887
36697dc0
LP
888 c = new0(CalendarSpec, 1);
889 if (!c)
890 return -ENOMEM;
21b3a0fc 891 c->dst = -1;
48d26c01 892 c->timezone = NULL;
36697dc0 893
078efddd
HV
894 utc = endswith_no_case(p, " UTC");
895 if (utc) {
896 c->utc = true;
897 p = strndupa(p, utc - p);
21b3a0fc
LP
898 } else {
899 const char *e = NULL;
900 int j;
901
902 tzset();
903
904 /* Check if the local timezone was specified? */
905 for (j = 0; j <= 1; j++) {
906 if (isempty(tzname[j]))
907 continue;
908
909 e = endswith_no_case(p, tzname[j]);
d80e5b74 910 if (!e)
21b3a0fc
LP
911 continue;
912 if (e == p)
913 continue;
914 if (e[-1] != ' ')
915 continue;
916
917 break;
918 }
919
920 /* Found one of the two timezones specified? */
921 if (IN_SET(j, 0, 1)) {
922 p = strndupa(p, e - p - 1);
923 c->dst = j;
48d26c01
IK
924 } else {
925 const char *last_space;
48d26c01 926
a2932a0d 927 last_space = strrchr(p, ' ');
089fb865 928 if (last_space != NULL && timezone_is_valid(last_space + 1, LOG_DEBUG)) {
a2932a0d 929 c->timezone = strdup(last_space + 1);
921b5987
DT
930 if (!c->timezone)
931 return -ENOMEM;
a2932a0d
ZJS
932
933 p = strndupa(p, last_space - p);
48d26c01 934 }
21b3a0fc 935 }
078efddd 936 }
51ffa239 937
921b5987
DT
938 if (isempty(p))
939 return -EINVAL;
04773cb5 940
272ac205 941 if (strcaseeq(p, "minutely")) {
436dd70f 942 r = const_chain(0, &c->microsecond);
272ac205 943 if (r < 0)
921b5987 944 return r;
272ac205
DM
945
946 } else if (strcaseeq(p, "hourly")) {
36697dc0
LP
947 r = const_chain(0, &c->minute);
948 if (r < 0)
921b5987 949 return r;
436dd70f 950 r = const_chain(0, &c->microsecond);
36697dc0 951 if (r < 0)
921b5987 952 return r;
36697dc0 953
b43d1d01 954 } else if (strcaseeq(p, "daily")) {
36697dc0
LP
955 r = const_chain(0, &c->hour);
956 if (r < 0)
921b5987 957 return r;
36697dc0
LP
958 r = const_chain(0, &c->minute);
959 if (r < 0)
921b5987 960 return r;
436dd70f 961 r = const_chain(0, &c->microsecond);
36697dc0 962 if (r < 0)
921b5987 963 return r;
36697dc0 964
b43d1d01 965 } else if (strcaseeq(p, "monthly")) {
36697dc0
LP
966 r = const_chain(1, &c->day);
967 if (r < 0)
921b5987 968 return r;
36697dc0
LP
969 r = const_chain(0, &c->hour);
970 if (r < 0)
921b5987 971 return r;
36697dc0
LP
972 r = const_chain(0, &c->minute);
973 if (r < 0)
921b5987 974 return r;
436dd70f 975 r = const_chain(0, &c->microsecond);
36697dc0 976 if (r < 0)
921b5987 977 return r;
36697dc0 978
dbfd41e2
LP
979 } else if (strcaseeq(p, "annually") ||
980 strcaseeq(p, "yearly") ||
981 strcaseeq(p, "anually") /* backwards compatibility */ ) {
982
13516818
LP
983 r = const_chain(1, &c->month);
984 if (r < 0)
921b5987 985 return r;
13516818
LP
986 r = const_chain(1, &c->day);
987 if (r < 0)
921b5987 988 return r;
13516818
LP
989 r = const_chain(0, &c->hour);
990 if (r < 0)
921b5987 991 return r;
13516818
LP
992 r = const_chain(0, &c->minute);
993 if (r < 0)
921b5987 994 return r;
436dd70f 995 r = const_chain(0, &c->microsecond);
13516818 996 if (r < 0)
921b5987 997 return r;
13516818 998
b43d1d01 999 } else if (strcaseeq(p, "weekly")) {
36697dc0
LP
1000
1001 c->weekdays_bits = 1;
1002
1003 r = const_chain(0, &c->hour);
1004 if (r < 0)
921b5987 1005 return r;
36697dc0
LP
1006 r = const_chain(0, &c->minute);
1007 if (r < 0)
921b5987 1008 return r;
436dd70f 1009 r = const_chain(0, &c->microsecond);
dbfd41e2 1010 if (r < 0)
921b5987 1011 return r;
dbfd41e2
LP
1012
1013 } else if (strcaseeq(p, "quarterly")) {
1014
1015 r = const_chain(1, &c->month);
1016 if (r < 0)
921b5987 1017 return r;
dbfd41e2
LP
1018 r = const_chain(4, &c->month);
1019 if (r < 0)
921b5987 1020 return r;
dbfd41e2
LP
1021 r = const_chain(7, &c->month);
1022 if (r < 0)
921b5987 1023 return r;
dbfd41e2
LP
1024 r = const_chain(10, &c->month);
1025 if (r < 0)
921b5987 1026 return r;
dbfd41e2
LP
1027 r = const_chain(1, &c->day);
1028 if (r < 0)
921b5987 1029 return r;
dbfd41e2
LP
1030 r = const_chain(0, &c->hour);
1031 if (r < 0)
921b5987 1032 return r;
dbfd41e2
LP
1033 r = const_chain(0, &c->minute);
1034 if (r < 0)
921b5987 1035 return r;
436dd70f 1036 r = const_chain(0, &c->microsecond);
dbfd41e2 1037 if (r < 0)
921b5987 1038 return r;
dbfd41e2
LP
1039
1040 } else if (strcaseeq(p, "biannually") ||
1041 strcaseeq(p, "bi-annually") ||
1042 strcaseeq(p, "semiannually") ||
1043 strcaseeq(p, "semi-annually")) {
1044
1045 r = const_chain(1, &c->month);
1046 if (r < 0)
921b5987 1047 return r;
dbfd41e2
LP
1048 r = const_chain(7, &c->month);
1049 if (r < 0)
921b5987 1050 return r;
dbfd41e2
LP
1051 r = const_chain(1, &c->day);
1052 if (r < 0)
921b5987 1053 return r;
dbfd41e2
LP
1054 r = const_chain(0, &c->hour);
1055 if (r < 0)
921b5987 1056 return r;
dbfd41e2
LP
1057 r = const_chain(0, &c->minute);
1058 if (r < 0)
921b5987 1059 return r;
436dd70f 1060 r = const_chain(0, &c->microsecond);
36697dc0 1061 if (r < 0)
921b5987 1062 return r;
36697dc0
LP
1063
1064 } else {
1065 r = parse_weekdays(&p, c);
1066 if (r < 0)
921b5987 1067 return r;
36697dc0
LP
1068
1069 r = parse_date(&p, c);
1070 if (r < 0)
921b5987 1071 return r;
36697dc0 1072
d80e5b74
ZJS
1073 if (r == 0) {
1074 r = parse_calendar_time(&p, c);
1075 if (r < 0)
921b5987 1076 return r;
d80e5b74 1077 }
36697dc0 1078
921b5987
DT
1079 if (*p != 0)
1080 return -EINVAL;
36697dc0
LP
1081 }
1082
1083 r = calendar_spec_normalize(c);
1084 if (r < 0)
921b5987 1085 return r;
36697dc0 1086
921b5987
DT
1087 if (!calendar_spec_valid(c))
1088 return -EINVAL;
36697dc0 1089
921b5987 1090 *spec = TAKE_PTR(c);
36697dc0 1091 return 0;
36697dc0
LP
1092}
1093
60bf5836 1094static int find_end_of_month(struct tm *tm, bool utc, int day) {
a2eb5ea7
DC
1095 struct tm t = *tm;
1096
1097 t.tm_mon++;
1098 t.tm_mday = 1 - day;
1099
c477ff14 1100 if (mktime_or_timegm(&t, utc) < 0 ||
a2eb5ea7
DC
1101 t.tm_mon != tm->tm_mon)
1102 return -1;
1103
1104 return t.tm_mday;
1105}
1106
8ea80351
DC
1107static int find_matching_component(const CalendarSpec *spec, const CalendarComponent *c,
1108 struct tm *tm, int *val) {
482f3b54 1109 const CalendarComponent *p = c;
e78fce48 1110 int start, stop, d = -1;
36697dc0
LP
1111 bool d_set = false;
1112 int r;
1113
1114 assert(val);
1115
1116 if (!c)
1117 return 0;
1118
1119 while (c) {
e78fce48
DC
1120 start = c->start;
1121 stop = c->stop;
a2eb5ea7 1122
8ea80351 1123 if (spec->end_of_month && p == spec->day) {
e78fce48
DC
1124 start = find_end_of_month(tm, spec->utc, start);
1125 stop = find_end_of_month(tm, spec->utc, stop);
a2eb5ea7 1126
e78fce48
DC
1127 if (stop > 0)
1128 SWAP_TWO(start, stop);
a2eb5ea7 1129 }
8ea80351 1130
e78fce48 1131 if (start >= *val) {
36697dc0 1132
e78fce48
DC
1133 if (!d_set || start < d) {
1134 d = start;
36697dc0
LP
1135 d_set = true;
1136 }
1137
1138 } else if (c->repeat > 0) {
1139 int k;
1140
be6b0c21 1141 k = start + c->repeat * DIV_ROUND_UP(*val - start, c->repeat);
36697dc0 1142
e78fce48 1143 if ((!d_set || k < d) && (stop < 0 || k <= stop)) {
36697dc0
LP
1144 d = k;
1145 d_set = true;
1146 }
1147 }
1148
482f3b54 1149 c = c->next;
36697dc0
LP
1150 }
1151
1152 if (!d_set)
1153 return -ENOENT;
1154
1155 r = *val != d;
1156 *val = d;
1157 return r;
1158}
1159
51ffa239 1160static bool tm_out_of_bounds(const struct tm *tm, bool utc) {
36697dc0
LP
1161 struct tm t;
1162 assert(tm);
1163
1164 t = *tm;
1165
c477ff14 1166 if (mktime_or_timegm(&t, utc) < 0)
36697dc0
LP
1167 return true;
1168
f6e7d66b
DC
1169 /*
1170 * Set an upper bound on the year so impossible dates like "*-02-31"
1171 * don't cause find_next() to loop forever. tm_year contains years
1172 * since 1900, so adjust it accordingly.
1173 */
1174 if (tm->tm_year + 1900 > MAX_YEAR)
1175 return true;
1176
36697dc0
LP
1177 /* Did any normalization take place? If so, it was out of bounds before */
1178 return
1179 t.tm_year != tm->tm_year ||
1180 t.tm_mon != tm->tm_mon ||
1181 t.tm_mday != tm->tm_mday ||
1182 t.tm_hour != tm->tm_hour ||
1183 t.tm_min != tm->tm_min ||
1184 t.tm_sec != tm->tm_sec;
1185}
1186
51ffa239 1187static bool matches_weekday(int weekdays_bits, const struct tm *tm, bool utc) {
36697dc0
LP
1188 struct tm t;
1189 int k;
1190
489464d0 1191 if (weekdays_bits < 0 || weekdays_bits >= BITS_WEEKDAYS)
36697dc0
LP
1192 return true;
1193
1194 t = *tm;
c477ff14 1195 if (mktime_or_timegm(&t, utc) < 0)
36697dc0
LP
1196 return false;
1197
1198 k = t.tm_wday == 0 ? 6 : t.tm_wday - 1;
1199 return (weekdays_bits & (1 << k));
1200}
1201
436dd70f 1202static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
36697dc0 1203 struct tm c;
436dd70f 1204 int tm_usec;
36697dc0
LP
1205 int r;
1206
1207 assert(spec);
1208 assert(tm);
1209
1210 c = *tm;
436dd70f 1211 tm_usec = *usec;
36697dc0
LP
1212
1213 for (;;) {
1214 /* Normalize the current date */
ea3894c1 1215 (void) mktime_or_timegm(&c, spec->utc);
21b3a0fc 1216 c.tm_isdst = spec->dst;
36697dc0
LP
1217
1218 c.tm_year += 1900;
8ea80351 1219 r = find_matching_component(spec, spec->year, &c, &c.tm_year);
36697dc0
LP
1220 c.tm_year -= 1900;
1221
1222 if (r > 0) {
1223 c.tm_mon = 0;
1224 c.tm_mday = 1;
436dd70f 1225 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0 1226 }
e308ddca 1227 if (r < 0)
36697dc0 1228 return r;
e308ddca
LP
1229 if (tm_out_of_bounds(&c, spec->utc))
1230 return -ENOENT;
36697dc0
LP
1231
1232 c.tm_mon += 1;
8ea80351 1233 r = find_matching_component(spec, spec->month, &c, &c.tm_mon);
36697dc0
LP
1234 c.tm_mon -= 1;
1235
1236 if (r > 0) {
1237 c.tm_mday = 1;
436dd70f 1238 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0 1239 }
51ffa239 1240 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1241 c.tm_year++;
36697dc0
LP
1242 c.tm_mon = 0;
1243 c.tm_mday = 1;
436dd70f 1244 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1245 continue;
1246 }
1247
8ea80351 1248 r = find_matching_component(spec, spec->day, &c, &c.tm_mday);
36697dc0 1249 if (r > 0)
436dd70f 1250 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
51ffa239 1251 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1252 c.tm_mon++;
36697dc0 1253 c.tm_mday = 1;
436dd70f 1254 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1255 continue;
1256 }
1257
51ffa239 1258 if (!matches_weekday(spec->weekdays_bits, &c, spec->utc)) {
36697dc0 1259 c.tm_mday++;
436dd70f 1260 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1261 continue;
1262 }
1263
8ea80351 1264 r = find_matching_component(spec, spec->hour, &c, &c.tm_hour);
36697dc0 1265 if (r > 0)
a022d76e 1266 c.tm_min = c.tm_sec = tm_usec = 0;
51ffa239 1267 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1268 c.tm_mday++;
436dd70f 1269 c.tm_hour = c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1270 continue;
1271 }
1272
8ea80351 1273 r = find_matching_component(spec, spec->minute, &c, &c.tm_min);
36697dc0 1274 if (r > 0)
a022d76e 1275 c.tm_sec = tm_usec = 0;
51ffa239 1276 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1277 c.tm_hour++;
436dd70f 1278 c.tm_min = c.tm_sec = tm_usec = 0;
36697dc0
LP
1279 continue;
1280 }
1281
436dd70f 1282 c.tm_sec = c.tm_sec * USEC_PER_SEC + tm_usec;
8ea80351 1283 r = find_matching_component(spec, spec->microsecond, &c, &c.tm_sec);
436dd70f
HV
1284 tm_usec = c.tm_sec % USEC_PER_SEC;
1285 c.tm_sec /= USEC_PER_SEC;
1286
51ffa239 1287 if (r < 0 || tm_out_of_bounds(&c, spec->utc)) {
313cefa1 1288 c.tm_min++;
436dd70f 1289 c.tm_sec = tm_usec = 0;
36697dc0
LP
1290 continue;
1291 }
1292
36697dc0 1293 *tm = c;
436dd70f 1294 *usec = tm_usec;
36697dc0
LP
1295 return 0;
1296 }
1297}
1298
48d26c01 1299static int calendar_spec_next_usec_impl(const CalendarSpec *spec, usec_t usec, usec_t *next) {
36697dc0
LP
1300 struct tm tm;
1301 time_t t;
1302 int r;
436dd70f 1303 usec_t tm_usec;
36697dc0
LP
1304
1305 assert(spec);
1306 assert(next);
1307
1bb4b028
LP
1308 if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX)
1309 return -EINVAL;
1310
436dd70f
HV
1311 usec++;
1312 t = (time_t) (usec / USEC_PER_SEC);
51ffa239 1313 assert_se(localtime_or_gmtime_r(&t, &tm, spec->utc));
436dd70f 1314 tm_usec = usec % USEC_PER_SEC;
36697dc0 1315
436dd70f 1316 r = find_next(spec, &tm, &tm_usec);
36697dc0
LP
1317 if (r < 0)
1318 return r;
1319
51ffa239 1320 t = mktime_or_timegm(&tm, spec->utc);
c477ff14 1321 if (t < 0)
36697dc0
LP
1322 return -EINVAL;
1323
436dd70f 1324 *next = (usec_t) t * USEC_PER_SEC + tm_usec;
36697dc0
LP
1325 return 0;
1326}
48d26c01
IK
1327
1328typedef struct SpecNextResult {
1329 usec_t next;
1330 int return_value;
1331} SpecNextResult;
1332
1333int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next) {
4c253ed1 1334 SpecNextResult *shared, tmp;
48d26c01
IK
1335 int r;
1336
1337 if (isempty(spec->timezone))
1338 return calendar_spec_next_usec_impl(spec, usec, next);
1339
1340 shared = mmap(NULL, sizeof *shared, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
1341 if (shared == MAP_FAILED)
1342 return negative_errno();
1343
1f5d1e02 1344 r = safe_fork("(sd-calendar)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_WAIT, NULL);
4c253ed1 1345 if (r < 0) {
48d26c01 1346 (void) munmap(shared, sizeof *shared);
4c253ed1 1347 return r;
48d26c01 1348 }
4c253ed1 1349 if (r == 0) {
48d26c01
IK
1350 if (setenv("TZ", spec->timezone, 1) != 0) {
1351 shared->return_value = negative_errno();
1352 _exit(EXIT_FAILURE);
1353 }
1354
1355 tzset();
1356
1357 shared->return_value = calendar_spec_next_usec_impl(spec, usec, &shared->next);
1358
1359 _exit(EXIT_SUCCESS);
1360 }
1361
48d26c01 1362 tmp = *shared;
1f5d1e02 1363 if (munmap(shared, sizeof *shared) < 0)
48d26c01
IK
1364 return negative_errno();
1365
1366 if (tmp.return_value == 0)
1367 *next = tmp.next;
1368
1369 return tmp.return_value;
1370}