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.
12 #include <sys/types.h>
20 #include <linux/netfilter/xt_time.h>
22 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
24 enum { /* getopt "seen" bits */
25 F_DATE_START = 1 << 0,
27 F_TIME_START = 1 << 2,
34 static const char *const week_days[] = {
35 NULL, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
38 static const struct option time_opts[] = {
39 {"datestart", true, NULL, 'D'},
40 {"datestop", true, NULL, 'E'},
41 {"timestart", true, NULL, 'X'},
42 {"timestop", true, NULL, 'Y'},
43 {"weekdays", true, NULL, 'w'},
44 {"monthdays", true, NULL, 'm'},
45 {"localtz", false, NULL, 'l'},
46 {"utc", false, NULL, 'u'},
50 static void time_help(void)
53 "time match options:\n"
54 " --datestart time Start and stop time, to be given in ISO 8601\n"
55 " --datestop time (YYYY[-MM[-DD[Thh[:mm[:ss]]]]])\n"
56 " --timestart time Start and stop daytime (hh:mm[:ss])\n"
57 " --timestop time (between 00:00:00 and 23:59:59)\n"
58 "[!] --monthdays value List of days on which to match, separated by comma\n"
59 " (Possible days: 1 to 31; defaults to all)\n"
60 "[!] --weekdays value List of weekdays on which to match, sep. by comma\n"
61 " (Possible days: Mon,Tue,Wed,Thu,Fri,Sat,Sun or 1 to 7\n"
62 " Defaults to all weekdays.)\n"
63 " --localtz/--utc Time is interpreted as UTC/local time\n");
66 static void time_init(struct xt_entry_match *m)
68 struct xt_time_info *info = (void *)m->data;
70 /* By default, we match on every day, every daytime */
71 info->monthdays_match = XT_TIME_ALL_MONTHDAYS;
72 info->weekdays_match = XT_TIME_ALL_WEEKDAYS;
73 info->daytime_start = XT_TIME_MIN_DAYTIME;
74 info->daytime_stop = XT_TIME_MAX_DAYTIME;
76 /* ...and have no date-begin or date-end boundary */
78 info->date_stop = INT_MAX;
80 /* local time is default */
81 info->flags |= XT_TIME_LOCAL_TZ;
84 static time_t time_parse_date(const char *s, bool end)
86 unsigned int month = 1, day = 1, hour = 0, minute = 0, second = 0;
87 unsigned int year = end ? 2038 : 1970;
93 year = strtoul(s, &e, 10);
94 if ((*e != '-' && *e != '\0') || year < 1970 || year > 2038)
100 month = strtoul(s, &e, 10);
101 if ((*e != '-' && *e != '\0') || month > 12)
107 day = strtoul(s, &e, 10);
108 if ((*e != 'T' && *e != '\0') || day > 31)
114 hour = strtoul(s, &e, 10);
115 if ((*e != ':' && *e != '\0') || hour > 23)
121 minute = strtoul(s, &e, 10);
122 if ((*e != ':' && *e != '\0') || minute > 59)
128 second = strtoul(s, &e, 10);
129 if (*e != '\0' || second > 59)
133 tm.tm_year = year - 1900;
134 tm.tm_mon = month - 1;
143 exit_error(OTHER_PROBLEM, "mktime returned an error");
146 exit_error(PARAMETER_PROBLEM, "Invalid date \"%s\" specified. Should "
147 "be YYYY[-MM[-DD[Thh[:mm[:ss]]]]]", os);
151 static unsigned int time_parse_minutes(const char *s)
153 unsigned int hour, minute, second = 0;
156 hour = strtoul(s, &e, 10);
157 if (*e != ':' || hour > 23)
161 minute = strtoul(s, &e, 10);
162 if ((*e != ':' && *e != '\0') || minute > 59)
168 second = strtoul(s, &e, 10);
169 if (*e != '\0' || second > 59)
173 return 60 * 60 * hour + 60 * minute + second;
176 exit_error(PARAMETER_PROBLEM, "invalid time \"%s\" specified, "
177 "should be hh:mm[:ss] format and within the boundaries", s);
181 static const char *my_strseg(char *buf, unsigned int buflen,
182 const char **arg, char delim)
186 if (*arg == NULL || **arg == '\0')
188 sep = strchr(*arg, delim);
190 snprintf(buf, buflen, "%s", *arg);
194 snprintf(buf, buflen, "%.*s", (unsigned int)(sep - *arg), *arg);
199 static uint32_t time_parse_monthdays(const char *arg)
201 char day[3], *err = NULL;
205 while (my_strseg(day, sizeof(day), &arg, ',') != NULL) {
206 i = strtoul(day, &err, 0);
207 if ((*err != ',' && *err != '\0') || i > 31)
208 exit_error(PARAMETER_PROBLEM,
209 "%s is not a valid day for --monthdays", day);
216 static unsigned int time_parse_weekdays(const char *arg)
218 char day[4], *err = NULL;
219 unsigned int i, ret = 0;
222 while (my_strseg(day, sizeof(day), &arg, ',') != NULL) {
223 i = strtoul(day, &err, 0);
226 exit_error(PARAMETER_PROBLEM,
227 "No, the week does NOT begin with Sunday.");
233 for (i = 1; i < ARRAY_SIZE(week_days); ++i)
234 if (strncmp(day, week_days[i], 2) == 0) {
240 exit_error(PARAMETER_PROBLEM,
241 "%s is not a valid day specifier", day);
247 static int time_parse(int c, char **argv, int invert, unsigned int *flags,
248 const void *entry, struct xt_entry_match **match)
250 struct xt_time_info *info = (void *)(*match)->data;
253 case 'D': /* --datestart */
254 if (*flags & F_DATE_START)
255 exit_error(PARAMETER_PROBLEM,
256 "Cannot specify --datestart twice");
258 exit_error(PARAMETER_PROBLEM,
259 "Unexpected \"!\" with --datestart");
260 info->date_start = time_parse_date(optarg, false);
261 *flags |= F_DATE_START;
263 case 'E': /* --datestop */
264 if (*flags & F_DATE_STOP)
265 exit_error(PARAMETER_PROBLEM,
266 "Cannot specify --datestop more than once");
268 exit_error(PARAMETER_PROBLEM,
269 "unexpected \"!\" with --datestop");
270 info->date_stop = time_parse_date(optarg, true);
271 *flags |= F_DATE_STOP;
273 case 'X': /* --timestart */
274 if (*flags & F_TIME_START)
275 exit_error(PARAMETER_PROBLEM,
276 "Cannot specify --timestart more than once");
278 exit_error(PARAMETER_PROBLEM,
279 "Unexpected \"!\" with --timestart");
280 info->daytime_start = time_parse_minutes(optarg);
281 *flags |= F_TIME_START;
283 case 'Y': /* --timestop */
284 if (*flags & F_TIME_STOP)
285 exit_error(PARAMETER_PROBLEM,
286 "Cannot specify --timestop more than once");
288 exit_error(PARAMETER_PROBLEM,
289 "Unexpected \"!\" with --timestop");
290 info->daytime_stop = time_parse_minutes(optarg);
291 *flags |= F_TIME_STOP;
293 case 'l': /* --localtz */
294 if (*flags & F_TIMEZONE)
295 exit_error(PARAMETER_PROBLEM,
296 "Can only specify exactly one of --localtz or --utc");
297 info->flags |= XT_TIME_LOCAL_TZ;
298 *flags |= F_TIMEZONE;
300 case 'm': /* --monthdays */
301 if (*flags & F_MONTHDAYS)
302 exit_error(PARAMETER_PROBLEM,
303 "Cannot specify --monthdays more than once");
304 info->monthdays_match = time_parse_monthdays(optarg);
306 info->monthdays_match ^= XT_TIME_ALL_MONTHDAYS;
307 *flags |= F_MONTHDAYS;
309 case 'w': /* --weekdays */
310 if (*flags & F_WEEKDAYS)
311 exit_error(PARAMETER_PROBLEM,
312 "Cannot specify --weekdays more than once");
313 info->weekdays_match = time_parse_weekdays(optarg);
315 info->weekdays_match ^= XT_TIME_ALL_WEEKDAYS;
316 *flags |= F_WEEKDAYS;
318 case 'u': /* --utc */
319 if (*flags & F_TIMEZONE)
320 exit_error(PARAMETER_PROBLEM,
321 "Can only specify exactly one of --localtz or --utc");
322 info->flags &= ~XT_TIME_LOCAL_TZ;
323 *flags |= F_TIMEZONE;
329 static void time_print_date(time_t date, const char *command)
333 /* If it is the default value, do not print it. */
334 if (date == 0 || date == LONG_MAX)
337 t = localtime(&date);
340 * Need a contiguous string (no whitespaces), hence using
341 * the ISO 8601 "T" variant.
343 printf("%s %04u-%02u-%02uT%02u:%02u:%02u ",
344 command, t->tm_year + 1900, t->tm_mon + 1,
345 t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
347 printf("%04u-%02u-%02u %02u:%02u:%02u ",
348 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
349 t->tm_hour, t->tm_min, t->tm_sec);
352 static void time_print_monthdays(uint32_t mask, bool human_readable)
354 unsigned int i, nbdays = 0;
356 for (i = 1; i <= 31; ++i)
357 if (mask & (1 << i)) {
380 static void time_print_weekdays(unsigned int mask)
382 unsigned int i, nbdays = 0;
384 for (i = 1; i <= 7; ++i)
385 if (mask & (1 << i)) {
387 printf(",%s", week_days[i]);
389 printf("%s", week_days[i]);
395 static inline void divide_time(unsigned int fulltime, unsigned int *hours,
396 unsigned int *minutes, unsigned int *seconds)
398 *seconds = fulltime % 60;
400 *minutes = fulltime % 60;
401 *hours = fulltime / 60;
404 static void time_print(const void *ip, const struct xt_entry_match *match,
407 struct xt_time_info *info = (void *)match->data;
408 unsigned int h, m, s;
412 if (info->daytime_start != XT_TIME_MIN_DAYTIME ||
413 info->daytime_stop != XT_TIME_MAX_DAYTIME) {
414 divide_time(info->daytime_start, &h, &m, &s);
415 printf("from %02u:%02u:%02u ", h, m, s);
416 divide_time(info->daytime_stop, &h, &m, &s);
417 printf("to %02u:%02u:%02u ", h, m, s);
419 if (info->weekdays_match != XT_TIME_ALL_WEEKDAYS) {
421 time_print_weekdays(info->weekdays_match);
423 if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) {
425 time_print_monthdays(info->monthdays_match, true);
427 if (info->date_start != 0) {
428 printf("starting from ");
429 time_print_date(info->date_start, NULL);
431 if (info->date_stop != INT_MAX) {
432 printf("until date ");
433 time_print_date(info->date_stop, NULL);
435 if (!(info->flags & XT_TIME_LOCAL_TZ))
439 static void time_save(const void *ip, const struct xt_entry_match *match)
441 const struct xt_time_info *info = (const void *)match->data;
442 unsigned int h, m, s;
444 if (info->daytime_start != XT_TIME_MIN_DAYTIME ||
445 info->daytime_stop != XT_TIME_MAX_DAYTIME) {
446 divide_time(info->daytime_start, &h, &m, &s);
447 printf("--timestart %02u:%02u:%02u ", h, m, s);
448 divide_time(info->daytime_stop, &h, &m, &s);
449 printf("--timestop %02u:%02u:%02u ", h, m, s);
451 if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) {
452 printf("--monthdays ");
453 time_print_monthdays(info->monthdays_match, false);
455 if (info->weekdays_match != XT_TIME_ALL_WEEKDAYS) {
456 printf("--weekdays ");
457 time_print_weekdays(info->weekdays_match);
460 time_print_date(info->date_start, "--datestart");
461 time_print_date(info->date_stop, "--datestop");
462 if (!(info->flags & XT_TIME_LOCAL_TZ))
466 static struct xtables_match time_match = {
469 .version = XTABLES_VERSION,
470 .size = XT_ALIGN(sizeof(struct xt_time_info)),
471 .userspacesize = XT_ALIGN(sizeof(struct xt_time_info)),
477 .extra_opts = time_opts,
482 xtables_register_match(&time_match);