blob: 97bb0d31e6863ae5593fa59ee36ed2c7ee02b841 [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 */
12#include <sys/types.h>
13#include <getopt.h>
14#include <stdbool.h>
15#include <stdio.h>
16#include <string.h>
17#include <stdlib.h>
18#include <stddef.h>
19#include <time.h>
20#include <linux/netfilter/xt_time.h>
21#include <xtables.h>
22#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
23
24enum { /* getopt "seen" bits */
25 F_DATE_START = 1 << 0,
26 F_DATE_STOP = 1 << 1,
27 F_TIME_START = 1 << 2,
28 F_TIME_STOP = 1 << 3,
29 F_MONTHDAYS = 1 << 4,
30 F_WEEKDAYS = 1 << 5,
31 F_TIMEZONE = 1 << 6,
32};
33
34static const char *const week_days[] = {
35 NULL, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
36};
37
38static 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'},
Max Kellermann9ee386a2008-01-29 13:48:05 +000047 { .name = NULL }
Jan Engelhardtad326ef2007-09-23 15:17:42 +000048};
49
50static void time_help(void)
51{
52 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020053"time match options:\n"
Jan Engelhardt9b488b92008-06-08 19:11:51 +020054" --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");
Jan Engelhardtad326ef2007-09-23 15:17:42 +000064}
65
66static void time_init(struct xt_entry_match *m)
67{
68 struct xt_time_info *info = (void *)m->data;
69
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;
75
76 /* ...and have no date-begin or date-end boundary */
77 info->date_start = 0;
Patrick McHardyfceebd82007-10-18 12:34:20 +000078 info->date_stop = INT_MAX;
Jan Engelhardtad326ef2007-09-23 15:17:42 +000079
80 /* local time is default */
81 info->flags |= XT_TIME_LOCAL_TZ;
82}
83
84static time_t time_parse_date(const char *s, bool end)
85{
86 unsigned int month = 1, day = 1, hour = 0, minute = 0, second = 0;
87 unsigned int year = end ? 2038 : 1970;
88 const char *os = s;
89 struct tm tm;
90 time_t ret;
91 char *e;
92
93 year = strtoul(s, &e, 10);
94 if ((*e != '-' && *e != '\0') || year < 1970 || year > 2038)
95 goto out;
96 if (*e == '\0')
97 goto eval;
98
99 s = e + 1;
100 month = strtoul(s, &e, 10);
101 if ((*e != '-' && *e != '\0') || month > 12)
102 goto out;
103 if (*e == '\0')
104 goto eval;
105
106 s = e + 1;
107 day = strtoul(s, &e, 10);
108 if ((*e != 'T' && *e != '\0') || day > 31)
109 goto out;
110 if (*e == '\0')
111 goto eval;
112
113 s = e + 1;
114 hour = strtoul(s, &e, 10);
115 if ((*e != ':' && *e != '\0') || hour > 23)
116 goto out;
117 if (*e == '\0')
118 goto eval;
119
120 s = e + 1;
121 minute = strtoul(s, &e, 10);
122 if ((*e != ':' && *e != '\0') || minute > 59)
123 goto out;
124 if (*e == '\0')
125 goto eval;
126
127 s = e + 1;
128 second = strtoul(s, &e, 10);
129 if (*e != '\0' || second > 59)
130 goto out;
131
132 eval:
133 tm.tm_year = year - 1900;
134 tm.tm_mon = month - 1;
135 tm.tm_mday = day;
136 tm.tm_hour = hour;
137 tm.tm_min = minute;
138 tm.tm_sec = second;
139 ret = mktime(&tm);
140 if (ret >= 0)
141 return ret;
142 perror("mktime");
143 exit_error(OTHER_PROBLEM, "mktime returned an error");
144
145 out:
146 exit_error(PARAMETER_PROBLEM, "Invalid date \"%s\" specified. Should "
147 "be YYYY[-MM[-DD[Thh[:mm[:ss]]]]]", os);
148 return -1;
149}
150
151static unsigned int time_parse_minutes(const char *s)
152{
153 unsigned int hour, minute, second = 0;
154 char *e;
155
156 hour = strtoul(s, &e, 10);
157 if (*e != ':' || hour > 23)
158 goto out;
159
160 s = e + 1;
161 minute = strtoul(s, &e, 10);
162 if ((*e != ':' && *e != '\0') || minute > 59)
163 goto out;
164 if (*e == '\0')
165 goto eval;
166
167 s = e + 1;
168 second = strtoul(s, &e, 10);
169 if (*e != '\0' || second > 59)
170 goto out;
171
172 eval:
173 return 60 * 60 * hour + 60 * minute + second;
174
175 out:
176 exit_error(PARAMETER_PROBLEM, "invalid time \"%s\" specified, "
177 "should be hh:mm[:ss] format and within the boundaries", s);
178 return -1;
179}
180
181static const char *my_strseg(char *buf, unsigned int buflen,
182 const char **arg, char delim)
183{
184 const char *sep;
185
186 if (*arg == NULL || **arg == '\0')
187 return NULL;
188 sep = strchr(*arg, delim);
189 if (sep == NULL) {
190 snprintf(buf, buflen, "%s", *arg);
191 *arg = NULL;
192 return buf;
193 }
194 snprintf(buf, buflen, "%.*s", (unsigned int)(sep - *arg), *arg);
195 *arg = sep + 1;
196 return buf;
197}
198
199static uint32_t time_parse_monthdays(const char *arg)
200{
201 char day[3], *err = NULL;
202 uint32_t ret = 0;
203 unsigned int i;
204
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);
210 ret |= 1 << i;
211 }
212
213 return ret;
214}
215
216static unsigned int time_parse_weekdays(const char *arg)
217{
218 char day[4], *err = NULL;
219 unsigned int i, ret = 0;
220 bool valid;
221
222 while (my_strseg(day, sizeof(day), &arg, ',') != NULL) {
223 i = strtoul(day, &err, 0);
224 if (*err == '\0') {
225 if (i == 0)
226 exit_error(PARAMETER_PROBLEM,
227 "No, the week does NOT begin with Sunday.");
228 ret |= 1 << i;
229 continue;
230 }
231
232 valid = false;
233 for (i = 1; i < ARRAY_SIZE(week_days); ++i)
234 if (strncmp(day, week_days[i], 2) == 0) {
235 ret |= 1 << i;
236 valid = true;
237 }
238
239 if (!valid)
240 exit_error(PARAMETER_PROBLEM,
241 "%s is not a valid day specifier", day);
242 }
243
244 return ret;
245}
246
247static int time_parse(int c, char **argv, int invert, unsigned int *flags,
248 const void *entry, struct xt_entry_match **match)
249{
250 struct xt_time_info *info = (void *)(*match)->data;
251
252 switch (c) {
253 case 'D': /* --datestart */
254 if (*flags & F_DATE_START)
255 exit_error(PARAMETER_PROBLEM,
256 "Cannot specify --datestart twice");
257 if (invert)
258 exit_error(PARAMETER_PROBLEM,
259 "Unexpected \"!\" with --datestart");
260 info->date_start = time_parse_date(optarg, false);
261 *flags |= F_DATE_START;
262 return 1;
263 case 'E': /* --datestop */
264 if (*flags & F_DATE_STOP)
265 exit_error(PARAMETER_PROBLEM,
266 "Cannot specify --datestop more than once");
267 if (invert)
268 exit_error(PARAMETER_PROBLEM,
269 "unexpected \"!\" with --datestop");
270 info->date_stop = time_parse_date(optarg, true);
271 *flags |= F_DATE_STOP;
272 return 1;
273 case 'X': /* --timestart */
274 if (*flags & F_TIME_START)
275 exit_error(PARAMETER_PROBLEM,
276 "Cannot specify --timestart more than once");
277 if (invert)
278 exit_error(PARAMETER_PROBLEM,
279 "Unexpected \"!\" with --timestart");
280 info->daytime_start = time_parse_minutes(optarg);
281 *flags |= F_TIME_START;
282 return 1;
283 case 'Y': /* --timestop */
284 if (*flags & F_TIME_STOP)
285 exit_error(PARAMETER_PROBLEM,
286 "Cannot specify --timestop more than once");
287 if (invert)
288 exit_error(PARAMETER_PROBLEM,
289 "Unexpected \"!\" with --timestop");
290 info->daytime_stop = time_parse_minutes(optarg);
291 *flags |= F_TIME_STOP;
292 return 1;
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;
299 return 1;
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);
305 if (invert)
306 info->monthdays_match ^= XT_TIME_ALL_MONTHDAYS;
307 *flags |= F_MONTHDAYS;
308 return 1;
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);
314 if (invert)
315 info->weekdays_match ^= XT_TIME_ALL_WEEKDAYS;
316 *flags |= F_WEEKDAYS;
317 return 1;
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;
324 return 1;
325 }
326 return 0;
327}
328
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000329static void time_print_date(time_t date, const char *command)
330{
331 struct tm *t;
332
333 /* If it is the default value, do not print it. */
334 if (date == 0 || date == LONG_MAX)
335 return;
336
337 t = localtime(&date);
338 if (command != NULL)
339 /*
340 * Need a contiguous string (no whitespaces), hence using
341 * the ISO 8601 "T" variant.
342 */
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);
346 else
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);
350}
351
352static void time_print_monthdays(uint32_t mask, bool human_readable)
353{
354 unsigned int i, nbdays = 0;
355
356 for (i = 1; i <= 31; ++i)
357 if (mask & (1 << i)) {
358 if (nbdays++ > 0)
359 printf(",");
360 printf("%u", i);
361 if (human_readable)
362 switch (i % 10) {
363 case 1:
364 printf("st");
365 break;
366 case 2:
367 printf("nd");
368 break;
369 case 3:
370 printf("rd");
371 break;
372 default:
373 printf("th");
374 break;
375 }
376 }
377 printf(" ");
378}
379
380static void time_print_weekdays(unsigned int mask)
381{
382 unsigned int i, nbdays = 0;
383
384 for (i = 1; i <= 7; ++i)
385 if (mask & (1 << i)) {
386 if (nbdays > 0)
387 printf(",%s", week_days[i]);
388 else
389 printf("%s", week_days[i]);
390 ++nbdays;
391 }
392 printf(" ");
393}
394
395static inline void divide_time(unsigned int fulltime, unsigned int *hours,
396 unsigned int *minutes, unsigned int *seconds)
397{
398 *seconds = fulltime % 60;
399 fulltime /= 60;
400 *minutes = fulltime % 60;
401 *hours = fulltime / 60;
402}
403
404static void time_print(const void *ip, const struct xt_entry_match *match,
405 int numeric)
406{
407 struct xt_time_info *info = (void *)match->data;
408 unsigned int h, m, s;
409
410 printf("TIME ");
411
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);
418 }
419 if (info->weekdays_match != XT_TIME_ALL_WEEKDAYS) {
420 printf("on ");
421 time_print_weekdays(info->weekdays_match);
422 }
423 if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) {
424 printf("on ");
425 time_print_monthdays(info->monthdays_match, true);
426 }
427 if (info->date_start != 0) {
428 printf("starting from ");
429 time_print_date(info->date_start, NULL);
430 }
Patrick McHardyfceebd82007-10-18 12:34:20 +0000431 if (info->date_stop != INT_MAX) {
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000432 printf("until date ");
433 time_print_date(info->date_stop, NULL);
434 }
435 if (!(info->flags & XT_TIME_LOCAL_TZ))
436 printf("UTC ");
437}
438
439static void time_save(const void *ip, const struct xt_entry_match *match)
440{
441 const struct xt_time_info *info = (const void *)match->data;
442 unsigned int h, m, s;
443
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);
450 }
451 if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) {
452 printf("--monthdays ");
453 time_print_monthdays(info->monthdays_match, false);
454 }
455 if (info->weekdays_match != XT_TIME_ALL_WEEKDAYS) {
456 printf("--weekdays ");
457 time_print_weekdays(info->weekdays_match);
458 printf(" ");
459 }
460 time_print_date(info->date_start, "--datestart");
461 time_print_date(info->date_stop, "--datestop");
462 if (!(info->flags & XT_TIME_LOCAL_TZ))
463 printf("--utc ");
464}
465
Jan Engelhardt181dead2007-10-04 16:27:07 +0000466static struct xtables_match time_match = {
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000467 .name = "time",
Jan Engelhardt23545c22008-02-14 04:23:04 +0100468 .family = AF_UNSPEC,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200469 .version = XTABLES_VERSION,
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000470 .size = XT_ALIGN(sizeof(struct xt_time_info)),
471 .userspacesize = XT_ALIGN(sizeof(struct xt_time_info)),
472 .help = time_help,
473 .init = time_init,
474 .parse = time_parse,
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000475 .print = time_print,
476 .save = time_save,
477 .extra_opts = time_opts,
478};
479
480void _init(void)
481{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000482 xtables_register_match(&time_match);
Jan Engelhardtad326ef2007-09-23 15:17:42 +0000483}