blob: 44c05b8fe700adb58673103267c5e46725fb2ae1 [file] [log] [blame]
Jan Engelhardtad326ef2007-09-23 15:17:42 +00001/*
2 * libxt_time - iptables part for xt_time
Jan Engelhardt032722b2007-10-20 15:17:30 +00003 * Copyright © CC Computer Consultants GmbH, 2007
4 * Contact: <jengelh@computergmbh.de>
Jan Engelhardtad326ef2007-09-23 15:17:42 +00005 *
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.
9 *
10 * Based on libipt_time.c.
11 */
Jan Engelhardtad326ef2007-09-23 15:17:42 +000012#include <stdio.h>
13#include <string.h>
14#include <stdlib.h>
Jan Engelhardtad326ef2007-09-23 15:17:42 +000015#include <time.h>
Jan Engelhardtb4af04b2010-12-03 20:15:35 +010016#include <linux/types.h>
Jan Engelhardtad326ef2007-09-23 15:17:42 +000017#include <linux/netfilter/xt_time.h>
18#include <xtables.h>
Jan Engelhardtad326ef2007-09-23 15:17:42 +000019
Jan Engelhardtd64d5472011-03-02 23:03:36 +010020enum {
21 O_DATE_START = 0,
22 O_DATE_STOP,
23 O_TIME_START,
24 O_TIME_STOP,
25 O_MONTHDAYS,
26 O_WEEKDAYS,
27 O_LOCAL_TZ,
28 O_UTC,
Jan Engelhardtdb50b832011-05-23 18:38:09 +020029 O_KERNEL_TZ,
Jan Engelhardt12018712011-05-23 17:48:20 +020030 F_LOCAL_TZ = 1 << O_LOCAL_TZ,
31 F_UTC = 1 << O_UTC,
Jan Engelhardtdb50b832011-05-23 18:38:09 +020032 F_KERNEL_TZ = 1 << O_KERNEL_TZ,
Jan Engelhardtad326ef2007-09-23 15:17:42 +000033};
34
35static const char *const week_days[] = {
36 NULL, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
37};
38
Jan Engelhardtd64d5472011-03-02 23:03:36 +010039static const struct xt_option_entry time_opts[] = {
40 {.name = "datestart", .id = O_DATE_START, .type = XTTYPE_STRING},
41 {.name = "datestop", .id = O_DATE_STOP, .type = XTTYPE_STRING},
42 {.name = "timestart", .id = O_TIME_START, .type = XTTYPE_STRING},
43 {.name = "timestop", .id = O_TIME_STOP, .type = XTTYPE_STRING},
44 {.name = "weekdays", .id = O_WEEKDAYS, .type = XTTYPE_STRING,
45 .flags = XTOPT_INVERT},
46 {.name = "monthdays", .id = O_MONTHDAYS, .type = XTTYPE_STRING,
47 .flags = XTOPT_INVERT},
Jan Engelhardt12018712011-05-23 17:48:20 +020048 {.name = "localtz", .id = O_LOCAL_TZ, .type = XTTYPE_NONE,
49 .excl = F_UTC},
50 {.name = "utc", .id = O_UTC, .type = XTTYPE_NONE,
Jan Engelhardtdb50b832011-05-23 18:38:09 +020051 .excl = F_LOCAL_TZ | F_KERNEL_TZ},
52 {.name = "kerneltz", .id = O_KERNEL_TZ, .type = XTTYPE_NONE,
53 .excl = F_UTC},
Jan Engelhardtd64d5472011-03-02 23:03:36 +010054 XTOPT_TABLEEND,
Jan Engelhardtad326ef2007-09-23 15:17:42 +000055};
56
57static void time_help(void)
58{
59 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020060"time match options:\n"
Jan Engelhardt9b488b92008-06-08 19:11:51 +020061" --datestart time Start and stop time, to be given in ISO 8601\n"
62" --datestop time (YYYY[-MM[-DD[Thh[:mm[:ss]]]]])\n"
63" --timestart time Start and stop daytime (hh:mm[:ss])\n"
64" --timestop time (between 00:00:00 and 23:59:59)\n"
65"[!] --monthdays value List of days on which to match, separated by comma\n"
66" (Possible days: 1 to 31; defaults to all)\n"
67"[!] --weekdays value List of weekdays on which to match, sep. by comma\n"
68" (Possible days: Mon,Tue,Wed,Thu,Fri,Sat,Sun or 1 to 7\n"
69" Defaults to all weekdays.)\n"
Jan Engelhardtdb50b832011-05-23 18:38:09 +020070" --kerneltz Work with the kernel timezone instead of UTC\n");
Jan Engelhardtad326ef2007-09-23 15:17:42 +000071}
72
73static void time_init(struct xt_entry_match *m)
74{
75 struct xt_time_info *info = (void *)m->data;
76
77 /* By default, we match on every day, every daytime */
78 info->monthdays_match = XT_TIME_ALL_MONTHDAYS;
79 info->weekdays_match = XT_TIME_ALL_WEEKDAYS;
80 info->daytime_start = XT_TIME_MIN_DAYTIME;
81 info->daytime_stop = XT_TIME_MAX_DAYTIME;
82
83 /* ...and have no date-begin or date-end boundary */
84 info->date_start = 0;
Patrick McHardyfceebd82007-10-18 12:34:20 +000085 info->date_stop = INT_MAX;
Jan Engelhardtad326ef2007-09-23 15:17:42 +000086}
87
88static time_t time_parse_date(const char *s, bool end)
89{
90 unsigned int month = 1, day = 1, hour = 0, minute = 0, second = 0;
91 unsigned int year = end ? 2038 : 1970;
92 const char *os = s;
93 struct tm tm;
94 time_t ret;
95 char *e;
96
97 year = strtoul(s, &e, 10);
98 if ((*e != '-' && *e != '\0') || year < 1970 || year > 2038)
99 goto out;
100 if (*e == '\0')
101 goto eval;
102
103 s = e + 1;
104 month = strtoul(s, &e, 10);
105 if ((*e != '-' && *e != '\0') || month > 12)
106 goto out;
107 if (*e == '\0')
108 goto eval;
109
110 s = e + 1;
111 day = strtoul(s, &e, 10);
112 if ((*e != 'T' && *e != '\0') || day > 31)
113 goto out;
114 if (*e == '\0')
115 goto eval;
116
117 s = e + 1;
118 hour = strtoul(s, &e, 10);
119 if ((*e != ':' && *e != '\0') || hour > 23)
120 goto out;
121 if (*e == '\0')
122 goto eval;
123
124 s = e + 1;
125 minute = strtoul(s, &e, 10);
126 if ((*e != ':' && *e != '\0') || minute > 59)
127 goto out;
128 if (*e == '\0')
129 goto eval;
130
131 s = e + 1;
132 second = strtoul(s, &e, 10);
133 if (*e != '\0' || second > 59)
134 goto out;
135
136 eval:
137 tm.tm_year = year - 1900;
138 tm.tm_mon = month - 1;
139 tm.tm_mday = day;
140 tm.tm_hour = hour;
141 tm.tm_min = minute;
142 tm.tm_sec = second;
Florian Westphal8ad33a32011-01-09 22:00:31 +0100143 tm.tm_isdst = 0;
Jan Engelhardtb1319cc2011-05-23 17:42:37 +0200144 /*
145 * Offsetting, if any, is done by xt_time.ko,
146 * so we have to disable it here in userspace.
147 */
148 setenv("TZ", "UTC", true);
149 tzset();
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000150 ret = mktime(&tm);
151 if (ret >= 0)
152 return ret;
153 perror("mktime");
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100154 xtables_error(OTHER_PROBLEM, "mktime returned an error");
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000155
156 out:
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100157 xtables_error(PARAMETER_PROBLEM, "Invalid date \"%s\" specified. Should "
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000158 "be YYYY[-MM[-DD[Thh[:mm[:ss]]]]]", os);
159 return -1;
160}
161
162static unsigned int time_parse_minutes(const char *s)
163{
164 unsigned int hour, minute, second = 0;
165 char *e;
166
167 hour = strtoul(s, &e, 10);
168 if (*e != ':' || hour > 23)
169 goto out;
170
171 s = e + 1;
172 minute = strtoul(s, &e, 10);
173 if ((*e != ':' && *e != '\0') || minute > 59)
174 goto out;
175 if (*e == '\0')
176 goto eval;
177
178 s = e + 1;
179 second = strtoul(s, &e, 10);
180 if (*e != '\0' || second > 59)
181 goto out;
182
183 eval:
184 return 60 * 60 * hour + 60 * minute + second;
185
186 out:
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100187 xtables_error(PARAMETER_PROBLEM, "invalid time \"%s\" specified, "
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000188 "should be hh:mm[:ss] format and within the boundaries", s);
189 return -1;
190}
191
192static const char *my_strseg(char *buf, unsigned int buflen,
193 const char **arg, char delim)
194{
195 const char *sep;
196
197 if (*arg == NULL || **arg == '\0')
198 return NULL;
199 sep = strchr(*arg, delim);
200 if (sep == NULL) {
201 snprintf(buf, buflen, "%s", *arg);
202 *arg = NULL;
203 return buf;
204 }
205 snprintf(buf, buflen, "%.*s", (unsigned int)(sep - *arg), *arg);
206 *arg = sep + 1;
207 return buf;
208}
209
210static uint32_t time_parse_monthdays(const char *arg)
211{
212 char day[3], *err = NULL;
213 uint32_t ret = 0;
214 unsigned int i;
215
216 while (my_strseg(day, sizeof(day), &arg, ',') != NULL) {
217 i = strtoul(day, &err, 0);
218 if ((*err != ',' && *err != '\0') || i > 31)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100219 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000220 "%s is not a valid day for --monthdays", day);
221 ret |= 1 << i;
222 }
223
224 return ret;
225}
226
227static unsigned int time_parse_weekdays(const char *arg)
228{
229 char day[4], *err = NULL;
230 unsigned int i, ret = 0;
231 bool valid;
232
233 while (my_strseg(day, sizeof(day), &arg, ',') != NULL) {
234 i = strtoul(day, &err, 0);
235 if (*err == '\0') {
236 if (i == 0)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100237 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000238 "No, the week does NOT begin with Sunday.");
239 ret |= 1 << i;
240 continue;
241 }
242
243 valid = false;
244 for (i = 1; i < ARRAY_SIZE(week_days); ++i)
245 if (strncmp(day, week_days[i], 2) == 0) {
246 ret |= 1 << i;
247 valid = true;
248 }
249
250 if (!valid)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100251 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000252 "%s is not a valid day specifier", day);
253 }
254
255 return ret;
256}
257
Jan Engelhardtd64d5472011-03-02 23:03:36 +0100258static void time_parse(struct xt_option_call *cb)
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000259{
Jan Engelhardtd64d5472011-03-02 23:03:36 +0100260 struct xt_time_info *info = cb->data;
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000261
Jan Engelhardtd64d5472011-03-02 23:03:36 +0100262 xtables_option_parse(cb);
263 switch (cb->entry->id) {
264 case O_DATE_START:
265 info->date_start = time_parse_date(cb->arg, false);
266 break;
267 case O_DATE_STOP:
268 info->date_stop = time_parse_date(cb->arg, true);
269 break;
270 case O_TIME_START:
271 info->daytime_start = time_parse_minutes(cb->arg);
272 break;
273 case O_TIME_STOP:
274 info->daytime_stop = time_parse_minutes(cb->arg);
275 break;
276 case O_LOCAL_TZ:
Jan Engelhardtdb50b832011-05-23 18:38:09 +0200277 fprintf(stderr, "WARNING: --localtz is being replaced by "
278 "--kerneltz, since \"local\" is ambiguous. Note the "
279 "kernel timezone has caveats - "
280 "see manpage for details.\n");
281 /* fallthrough */
282 case O_KERNEL_TZ:
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000283 info->flags |= XT_TIME_LOCAL_TZ;
Jan Engelhardtd64d5472011-03-02 23:03:36 +0100284 break;
285 case O_MONTHDAYS:
286 info->monthdays_match = time_parse_monthdays(cb->arg);
287 if (cb->invert)
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000288 info->monthdays_match ^= XT_TIME_ALL_MONTHDAYS;
Jan Engelhardtd64d5472011-03-02 23:03:36 +0100289 break;
290 case O_WEEKDAYS:
291 info->weekdays_match = time_parse_weekdays(cb->arg);
292 if (cb->invert)
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000293 info->weekdays_match ^= XT_TIME_ALL_WEEKDAYS;
Jan Engelhardtd64d5472011-03-02 23:03:36 +0100294 break;
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000295 }
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000296}
297
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000298static void time_print_date(time_t date, const char *command)
299{
300 struct tm *t;
301
302 /* If it is the default value, do not print it. */
303 if (date == 0 || date == LONG_MAX)
304 return;
305
Jan Engelhardtb1319cc2011-05-23 17:42:37 +0200306 t = gmtime(&date);
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000307 if (command != NULL)
308 /*
309 * Need a contiguous string (no whitespaces), hence using
310 * the ISO 8601 "T" variant.
311 */
Jan Engelhardt73866352010-12-18 02:04:59 +0100312 printf(" %s %04u-%02u-%02uT%02u:%02u:%02u",
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000313 command, t->tm_year + 1900, t->tm_mon + 1,
314 t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
315 else
Jan Engelhardt73866352010-12-18 02:04:59 +0100316 printf(" %04u-%02u-%02u %02u:%02u:%02u",
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000317 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
318 t->tm_hour, t->tm_min, t->tm_sec);
319}
320
321static void time_print_monthdays(uint32_t mask, bool human_readable)
322{
323 unsigned int i, nbdays = 0;
324
Jan Engelhardt73866352010-12-18 02:04:59 +0100325 printf(" ");
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000326 for (i = 1; i <= 31; ++i)
327 if (mask & (1 << i)) {
328 if (nbdays++ > 0)
329 printf(",");
330 printf("%u", i);
331 if (human_readable)
332 switch (i % 10) {
333 case 1:
334 printf("st");
335 break;
336 case 2:
337 printf("nd");
338 break;
339 case 3:
340 printf("rd");
341 break;
342 default:
343 printf("th");
344 break;
345 }
346 }
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000347}
348
349static void time_print_weekdays(unsigned int mask)
350{
351 unsigned int i, nbdays = 0;
352
Jan Engelhardt73866352010-12-18 02:04:59 +0100353 printf(" ");
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000354 for (i = 1; i <= 7; ++i)
355 if (mask & (1 << i)) {
356 if (nbdays > 0)
357 printf(",%s", week_days[i]);
358 else
359 printf("%s", week_days[i]);
360 ++nbdays;
361 }
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000362}
363
364static inline void divide_time(unsigned int fulltime, unsigned int *hours,
365 unsigned int *minutes, unsigned int *seconds)
366{
367 *seconds = fulltime % 60;
368 fulltime /= 60;
369 *minutes = fulltime % 60;
370 *hours = fulltime / 60;
371}
372
373static void time_print(const void *ip, const struct xt_entry_match *match,
374 int numeric)
375{
Jan Engelhardt69f564e2009-05-26 13:14:06 +0200376 const struct xt_time_info *info = (const void *)match->data;
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000377 unsigned int h, m, s;
378
Jan Engelhardt73866352010-12-18 02:04:59 +0100379 printf(" TIME");
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000380
381 if (info->daytime_start != XT_TIME_MIN_DAYTIME ||
382 info->daytime_stop != XT_TIME_MAX_DAYTIME) {
383 divide_time(info->daytime_start, &h, &m, &s);
Jan Engelhardt73866352010-12-18 02:04:59 +0100384 printf(" from %02u:%02u:%02u", h, m, s);
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000385 divide_time(info->daytime_stop, &h, &m, &s);
Jan Engelhardt73866352010-12-18 02:04:59 +0100386 printf(" to %02u:%02u:%02u", h, m, s);
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000387 }
388 if (info->weekdays_match != XT_TIME_ALL_WEEKDAYS) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100389 printf(" on");
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000390 time_print_weekdays(info->weekdays_match);
391 }
392 if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100393 printf(" on");
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000394 time_print_monthdays(info->monthdays_match, true);
395 }
396 if (info->date_start != 0) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100397 printf(" starting from");
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000398 time_print_date(info->date_start, NULL);
399 }
Patrick McHardyfceebd82007-10-18 12:34:20 +0000400 if (info->date_stop != INT_MAX) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100401 printf(" until date");
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000402 time_print_date(info->date_stop, NULL);
403 }
404 if (!(info->flags & XT_TIME_LOCAL_TZ))
Jan Engelhardt73866352010-12-18 02:04:59 +0100405 printf(" UTC");
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000406}
407
408static void time_save(const void *ip, const struct xt_entry_match *match)
409{
410 const struct xt_time_info *info = (const void *)match->data;
411 unsigned int h, m, s;
412
413 if (info->daytime_start != XT_TIME_MIN_DAYTIME ||
414 info->daytime_stop != XT_TIME_MAX_DAYTIME) {
415 divide_time(info->daytime_start, &h, &m, &s);
Jan Engelhardt73866352010-12-18 02:04:59 +0100416 printf(" --timestart %02u:%02u:%02u", h, m, s);
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000417 divide_time(info->daytime_stop, &h, &m, &s);
Jan Engelhardt73866352010-12-18 02:04:59 +0100418 printf(" --timestop %02u:%02u:%02u", h, m, s);
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000419 }
420 if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100421 printf(" --monthdays");
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000422 time_print_monthdays(info->monthdays_match, false);
423 }
424 if (info->weekdays_match != XT_TIME_ALL_WEEKDAYS) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100425 printf(" --weekdays");
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000426 time_print_weekdays(info->weekdays_match);
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000427 }
428 time_print_date(info->date_start, "--datestart");
429 time_print_date(info->date_stop, "--datestop");
Jan Engelhardtdb50b832011-05-23 18:38:09 +0200430 if (info->flags & XT_TIME_LOCAL_TZ)
431 printf(" --kerneltz");
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000432}
433
Jan Engelhardt181dead2007-10-04 16:27:07 +0000434static struct xtables_match time_match = {
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000435 .name = "time",
Jan Engelhardt42979362009-06-01 11:56:23 +0200436 .family = NFPROTO_UNSPEC,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200437 .version = XTABLES_VERSION,
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000438 .size = XT_ALIGN(sizeof(struct xt_time_info)),
439 .userspacesize = XT_ALIGN(sizeof(struct xt_time_info)),
440 .help = time_help,
441 .init = time_init,
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000442 .print = time_print,
443 .save = time_save,
Jan Engelhardtd64d5472011-03-02 23:03:36 +0100444 .x6_parse = time_parse,
445 .x6_options = time_opts,
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000446};
447
448void _init(void)
449{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000450 xtables_register_match(&time_match);
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000451}