2 * libxt_time - iptables part for xt_time
3 * Copyright © CC Computer Consultants GmbH, 2007
4 * Contact: <jengelh@computergmbh.de>
6 * libxt_time.c is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 or 3 of the License.
10 * Based on libipt_time.c.
16 #include <linux/types.h>
17 #include <linux/netfilter/xt_time.h>
31 F_LOCAL_TZ = 1 << O_LOCAL_TZ,
33 F_KERNEL_TZ = 1 << O_KERNEL_TZ,
34 F_TIME_CONTIGUOUS = 1 << O_TIME_CONTIGUOUS,
37 static const char *const week_days[] = {
38 NULL, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
41 static const struct xt_option_entry time_opts[] = {
42 {.name = "datestart", .id = O_DATE_START, .type = XTTYPE_STRING},
43 {.name = "datestop", .id = O_DATE_STOP, .type = XTTYPE_STRING},
44 {.name = "timestart", .id = O_TIME_START, .type = XTTYPE_STRING},
45 {.name = "timestop", .id = O_TIME_STOP, .type = XTTYPE_STRING},
46 {.name = "contiguous", .id = O_TIME_CONTIGUOUS, .type = XTTYPE_NONE},
47 {.name = "weekdays", .id = O_WEEKDAYS, .type = XTTYPE_STRING,
48 .flags = XTOPT_INVERT},
49 {.name = "monthdays", .id = O_MONTHDAYS, .type = XTTYPE_STRING,
50 .flags = XTOPT_INVERT},
51 {.name = "localtz", .id = O_LOCAL_TZ, .type = XTTYPE_NONE,
53 {.name = "utc", .id = O_UTC, .type = XTTYPE_NONE,
54 .excl = F_LOCAL_TZ | F_KERNEL_TZ},
55 {.name = "kerneltz", .id = O_KERNEL_TZ, .type = XTTYPE_NONE,
60 static void time_help(void)
63 "time match options:\n"
64 " --datestart time Start and stop time, to be given in ISO 8601\n"
65 " --datestop time (YYYY[-MM[-DD[Thh[:mm[:ss]]]]])\n"
66 " --timestart time Start and stop daytime (hh:mm[:ss])\n"
67 " --timestop time (between 00:00:00 and 23:59:59)\n"
68 "[!] --monthdays value List of days on which to match, separated by comma\n"
69 " (Possible days: 1 to 31; defaults to all)\n"
70 "[!] --weekdays value List of weekdays on which to match, sep. by comma\n"
71 " (Possible days: Mon,Tue,Wed,Thu,Fri,Sat,Sun or 1 to 7\n"
72 " Defaults to all weekdays.)\n"
73 " --kerneltz Work with the kernel timezone instead of UTC\n");
76 static void time_init(struct xt_entry_match *m)
78 struct xt_time_info *info = (void *)m->data;
80 /* By default, we match on every day, every daytime */
81 info->monthdays_match = XT_TIME_ALL_MONTHDAYS;
82 info->weekdays_match = XT_TIME_ALL_WEEKDAYS;
83 info->daytime_start = XT_TIME_MIN_DAYTIME;
84 info->daytime_stop = XT_TIME_MAX_DAYTIME;
86 /* ...and have no date-begin or date-end boundary */
88 info->date_stop = INT_MAX;
91 static time_t time_parse_date(const char *s, bool end)
93 unsigned int month = 1, day = 1, hour = 0, minute = 0, second = 0;
94 unsigned int year = end ? 2038 : 1970;
100 year = strtoul(s, &e, 10);
101 if ((*e != '-' && *e != '\0') || year < 1970 || year > 2038)
107 month = strtoul(s, &e, 10);
108 if ((*e != '-' && *e != '\0') || month > 12)
114 day = strtoul(s, &e, 10);
115 if ((*e != 'T' && *e != '\0') || day > 31)
121 hour = strtoul(s, &e, 10);
122 if ((*e != ':' && *e != '\0') || hour > 23)
128 minute = strtoul(s, &e, 10);
129 if ((*e != ':' && *e != '\0') || minute > 59)
135 second = strtoul(s, &e, 10);
136 if (*e != '\0' || second > 59)
140 tm.tm_year = year - 1900;
141 tm.tm_mon = month - 1;
148 * Offsetting, if any, is done by xt_time.ko,
149 * so we have to disable it here in userspace.
151 setenv("TZ", "UTC", true);
157 xtables_error(OTHER_PROBLEM, "mktime returned an error");
160 xtables_error(PARAMETER_PROBLEM, "Invalid date \"%s\" specified. Should "
161 "be YYYY[-MM[-DD[Thh[:mm[:ss]]]]]", os);
165 static unsigned int time_parse_minutes(const char *s)
167 unsigned int hour, minute, second = 0;
170 hour = strtoul(s, &e, 10);
171 if (*e != ':' || hour > 23)
175 minute = strtoul(s, &e, 10);
176 if ((*e != ':' && *e != '\0') || minute > 59)
182 second = strtoul(s, &e, 10);
183 if (*e != '\0' || second > 59)
187 return 60 * 60 * hour + 60 * minute + second;
190 xtables_error(PARAMETER_PROBLEM, "invalid time \"%s\" specified, "
191 "should be hh:mm[:ss] format and within the boundaries", s);
195 static const char *my_strseg(char *buf, unsigned int buflen,
196 const char **arg, char delim)
200 if (*arg == NULL || **arg == '\0')
202 sep = strchr(*arg, delim);
204 snprintf(buf, buflen, "%s", *arg);
208 snprintf(buf, buflen, "%.*s", (unsigned int)(sep - *arg), *arg);
213 static uint32_t time_parse_monthdays(const char *arg)
215 char day[3], *err = NULL;
219 while (my_strseg(day, sizeof(day), &arg, ',') != NULL) {
220 i = strtoul(day, &err, 0);
221 if ((*err != ',' && *err != '\0') || i > 31)
222 xtables_error(PARAMETER_PROBLEM,
223 "%s is not a valid day for --monthdays", day);
230 static unsigned int time_parse_weekdays(const char *arg)
232 char day[4], *err = NULL;
233 unsigned int i, ret = 0;
236 while (my_strseg(day, sizeof(day), &arg, ',') != NULL) {
237 i = strtoul(day, &err, 0);
240 xtables_error(PARAMETER_PROBLEM,
241 "No, the week does NOT begin with Sunday.");
247 for (i = 1; i < ARRAY_SIZE(week_days); ++i)
248 if (strncmp(day, week_days[i], 2) == 0) {
254 xtables_error(PARAMETER_PROBLEM,
255 "%s is not a valid day specifier", day);
261 static void time_parse(struct xt_option_call *cb)
263 struct xt_time_info *info = cb->data;
265 xtables_option_parse(cb);
266 switch (cb->entry->id) {
268 info->date_start = time_parse_date(cb->arg, false);
271 info->date_stop = time_parse_date(cb->arg, true);
274 info->daytime_start = time_parse_minutes(cb->arg);
277 info->daytime_stop = time_parse_minutes(cb->arg);
279 case O_TIME_CONTIGUOUS:
280 info->flags |= XT_TIME_CONTIGUOUS;
283 fprintf(stderr, "WARNING: --localtz is being replaced by "
284 "--kerneltz, since \"local\" is ambiguous. Note the "
285 "kernel timezone has caveats - "
286 "see manpage for details.\n");
289 info->flags |= XT_TIME_LOCAL_TZ;
292 info->monthdays_match = time_parse_monthdays(cb->arg);
294 info->monthdays_match ^= XT_TIME_ALL_MONTHDAYS;
297 info->weekdays_match = time_parse_weekdays(cb->arg);
299 info->weekdays_match ^= XT_TIME_ALL_WEEKDAYS;
304 static void time_print_date(time_t date, const char *command)
308 /* If it is the default value, do not print it. */
309 if (date == 0 || date == LONG_MAX)
315 * Need a contiguous string (no whitespaces), hence using
316 * the ISO 8601 "T" variant.
318 printf(" %s %04u-%02u-%02uT%02u:%02u:%02u",
319 command, t->tm_year + 1900, t->tm_mon + 1,
320 t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
322 printf(" %04u-%02u-%02u %02u:%02u:%02u",
323 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
324 t->tm_hour, t->tm_min, t->tm_sec);
327 static void time_print_monthdays(uint32_t mask, bool human_readable)
329 unsigned int i, nbdays = 0;
332 for (i = 1; i <= 31; ++i)
333 if (mask & (1 << i)) {
355 static void time_print_weekdays(unsigned int mask)
357 unsigned int i, nbdays = 0;
360 for (i = 1; i <= 7; ++i)
361 if (mask & (1 << i)) {
363 printf(",%s", week_days[i]);
365 printf("%s", week_days[i]);
370 static inline void divide_time(unsigned int fulltime, unsigned int *hours,
371 unsigned int *minutes, unsigned int *seconds)
373 *seconds = fulltime % 60;
375 *minutes = fulltime % 60;
376 *hours = fulltime / 60;
379 static void time_print(const void *ip, const struct xt_entry_match *match,
382 const struct xt_time_info *info = (const void *)match->data;
383 unsigned int h, m, s;
387 if (info->daytime_start != XT_TIME_MIN_DAYTIME ||
388 info->daytime_stop != XT_TIME_MAX_DAYTIME) {
389 divide_time(info->daytime_start, &h, &m, &s);
390 printf(" from %02u:%02u:%02u", h, m, s);
391 divide_time(info->daytime_stop, &h, &m, &s);
392 printf(" to %02u:%02u:%02u", h, m, s);
394 if (info->weekdays_match != XT_TIME_ALL_WEEKDAYS) {
396 time_print_weekdays(info->weekdays_match);
398 if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) {
400 time_print_monthdays(info->monthdays_match, true);
402 if (info->date_start != 0) {
403 printf(" starting from");
404 time_print_date(info->date_start, NULL);
406 if (info->date_stop != INT_MAX) {
407 printf(" until date");
408 time_print_date(info->date_stop, NULL);
410 if (!(info->flags & XT_TIME_LOCAL_TZ))
412 if (info->flags & XT_TIME_CONTIGUOUS)
413 printf(" contiguous");
416 static void time_save(const void *ip, const struct xt_entry_match *match)
418 const struct xt_time_info *info = (const void *)match->data;
419 unsigned int h, m, s;
421 if (info->daytime_start != XT_TIME_MIN_DAYTIME ||
422 info->daytime_stop != XT_TIME_MAX_DAYTIME) {
423 divide_time(info->daytime_start, &h, &m, &s);
424 printf(" --timestart %02u:%02u:%02u", h, m, s);
425 divide_time(info->daytime_stop, &h, &m, &s);
426 printf(" --timestop %02u:%02u:%02u", h, m, s);
428 if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) {
429 printf(" --monthdays");
430 time_print_monthdays(info->monthdays_match, false);
432 if (info->weekdays_match != XT_TIME_ALL_WEEKDAYS) {
433 printf(" --weekdays");
434 time_print_weekdays(info->weekdays_match);
436 time_print_date(info->date_start, "--datestart");
437 time_print_date(info->date_stop, "--datestop");
438 if (info->flags & XT_TIME_LOCAL_TZ)
439 printf(" --kerneltz");
440 if (info->flags & XT_TIME_CONTIGUOUS)
441 printf(" --contiguous");
444 static void time_check(struct xt_fcheck_call *cb)
446 const struct xt_time_info *info = (const void *) cb->data;
447 if ((cb->xflags & F_TIME_CONTIGUOUS) &&
448 info->daytime_start < info->daytime_stop)
449 xtables_error(PARAMETER_PROBLEM,
450 "time: --contiguous only makes sense when stoptime is smaller than starttime");
453 static struct xtables_match time_match = {
455 .family = NFPROTO_UNSPEC,
456 .version = XTABLES_VERSION,
457 .size = XT_ALIGN(sizeof(struct xt_time_info)),
458 .userspacesize = XT_ALIGN(sizeof(struct xt_time_info)),
463 .x6_parse = time_parse,
464 .x6_fcheck = time_check,
465 .x6_options = time_opts,
470 xtables_register_match(&time_match);