Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libxt_time - iptables part for xt_time |
Jan Engelhardt | 032722b | 2007-10-20 15:17:30 +0000 | [diff] [blame] | 3 | * Copyright © CC Computer Consultants GmbH, 2007 |
| 4 | * Contact: <jengelh@computergmbh.de> |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 5 | * |
| 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 Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | #include <stdlib.h> |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 15 | #include <time.h> |
Jan Engelhardt | b4af04b | 2010-12-03 20:15:35 +0100 | [diff] [blame] | 16 | #include <linux/types.h> |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 17 | #include <linux/netfilter/xt_time.h> |
| 18 | #include <xtables.h> |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 19 | |
Jan Engelhardt | d64d547 | 2011-03-02 23:03:36 +0100 | [diff] [blame] | 20 | enum { |
| 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 Engelhardt | db50b83 | 2011-05-23 18:38:09 +0200 | [diff] [blame] | 29 | O_KERNEL_TZ, |
Jan Engelhardt | 1201871 | 2011-05-23 17:48:20 +0200 | [diff] [blame] | 30 | F_LOCAL_TZ = 1 << O_LOCAL_TZ, |
| 31 | F_UTC = 1 << O_UTC, |
Jan Engelhardt | db50b83 | 2011-05-23 18:38:09 +0200 | [diff] [blame] | 32 | F_KERNEL_TZ = 1 << O_KERNEL_TZ, |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static const char *const week_days[] = { |
| 36 | NULL, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", |
| 37 | }; |
| 38 | |
Jan Engelhardt | d64d547 | 2011-03-02 23:03:36 +0100 | [diff] [blame] | 39 | static 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 Engelhardt | 1201871 | 2011-05-23 17:48:20 +0200 | [diff] [blame] | 48 | {.name = "localtz", .id = O_LOCAL_TZ, .type = XTTYPE_NONE, |
| 49 | .excl = F_UTC}, |
| 50 | {.name = "utc", .id = O_UTC, .type = XTTYPE_NONE, |
Jan Engelhardt | db50b83 | 2011-05-23 18:38:09 +0200 | [diff] [blame] | 51 | .excl = F_LOCAL_TZ | F_KERNEL_TZ}, |
| 52 | {.name = "kerneltz", .id = O_KERNEL_TZ, .type = XTTYPE_NONE, |
| 53 | .excl = F_UTC}, |
Jan Engelhardt | d64d547 | 2011-03-02 23:03:36 +0100 | [diff] [blame] | 54 | XTOPT_TABLEEND, |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | static void time_help(void) |
| 58 | { |
| 59 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 60 | "time match options:\n" |
Jan Engelhardt | 9b488b9 | 2008-06-08 19:11:51 +0200 | [diff] [blame] | 61 | " --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 Engelhardt | db50b83 | 2011-05-23 18:38:09 +0200 | [diff] [blame] | 70 | " --kerneltz Work with the kernel timezone instead of UTC\n"); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static 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 McHardy | fceebd8 | 2007-10-18 12:34:20 +0000 | [diff] [blame] | 85 | info->date_stop = INT_MAX; |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static 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 Westphal | 8ad33a3 | 2011-01-09 22:00:31 +0100 | [diff] [blame] | 143 | tm.tm_isdst = 0; |
Jan Engelhardt | b1319cc | 2011-05-23 17:42:37 +0200 | [diff] [blame] | 144 | /* |
| 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 Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 150 | ret = mktime(&tm); |
| 151 | if (ret >= 0) |
| 152 | return ret; |
| 153 | perror("mktime"); |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 154 | xtables_error(OTHER_PROBLEM, "mktime returned an error"); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 155 | |
| 156 | out: |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 157 | xtables_error(PARAMETER_PROBLEM, "Invalid date \"%s\" specified. Should " |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 158 | "be YYYY[-MM[-DD[Thh[:mm[:ss]]]]]", os); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | static 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 Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 187 | xtables_error(PARAMETER_PROBLEM, "invalid time \"%s\" specified, " |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 188 | "should be hh:mm[:ss] format and within the boundaries", s); |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | static 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 | |
| 210 | static 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 Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 219 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 220 | "%s is not a valid day for --monthdays", day); |
| 221 | ret |= 1 << i; |
| 222 | } |
| 223 | |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | static 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 Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 237 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 238 | "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 Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 251 | xtables_error(PARAMETER_PROBLEM, |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 252 | "%s is not a valid day specifier", day); |
| 253 | } |
| 254 | |
| 255 | return ret; |
| 256 | } |
| 257 | |
Jan Engelhardt | d64d547 | 2011-03-02 23:03:36 +0100 | [diff] [blame] | 258 | static void time_parse(struct xt_option_call *cb) |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 259 | { |
Jan Engelhardt | d64d547 | 2011-03-02 23:03:36 +0100 | [diff] [blame] | 260 | struct xt_time_info *info = cb->data; |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 261 | |
Jan Engelhardt | d64d547 | 2011-03-02 23:03:36 +0100 | [diff] [blame] | 262 | 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 Engelhardt | db50b83 | 2011-05-23 18:38:09 +0200 | [diff] [blame] | 277 | 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 Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 283 | info->flags |= XT_TIME_LOCAL_TZ; |
Jan Engelhardt | d64d547 | 2011-03-02 23:03:36 +0100 | [diff] [blame] | 284 | break; |
| 285 | case O_MONTHDAYS: |
| 286 | info->monthdays_match = time_parse_monthdays(cb->arg); |
| 287 | if (cb->invert) |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 288 | info->monthdays_match ^= XT_TIME_ALL_MONTHDAYS; |
Jan Engelhardt | d64d547 | 2011-03-02 23:03:36 +0100 | [diff] [blame] | 289 | break; |
| 290 | case O_WEEKDAYS: |
| 291 | info->weekdays_match = time_parse_weekdays(cb->arg); |
| 292 | if (cb->invert) |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 293 | info->weekdays_match ^= XT_TIME_ALL_WEEKDAYS; |
Jan Engelhardt | d64d547 | 2011-03-02 23:03:36 +0100 | [diff] [blame] | 294 | break; |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 295 | } |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 298 | static 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 Engelhardt | b1319cc | 2011-05-23 17:42:37 +0200 | [diff] [blame] | 306 | t = gmtime(&date); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 307 | if (command != NULL) |
| 308 | /* |
| 309 | * Need a contiguous string (no whitespaces), hence using |
| 310 | * the ISO 8601 "T" variant. |
| 311 | */ |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 312 | printf(" %s %04u-%02u-%02uT%02u:%02u:%02u", |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 313 | 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 Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 316 | printf(" %04u-%02u-%02u %02u:%02u:%02u", |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 317 | t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, |
| 318 | t->tm_hour, t->tm_min, t->tm_sec); |
| 319 | } |
| 320 | |
| 321 | static void time_print_monthdays(uint32_t mask, bool human_readable) |
| 322 | { |
| 323 | unsigned int i, nbdays = 0; |
| 324 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 325 | printf(" "); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 326 | 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 Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static void time_print_weekdays(unsigned int mask) |
| 350 | { |
| 351 | unsigned int i, nbdays = 0; |
| 352 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 353 | printf(" "); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 354 | 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 Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static 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 | |
| 373 | static void time_print(const void *ip, const struct xt_entry_match *match, |
| 374 | int numeric) |
| 375 | { |
Jan Engelhardt | 69f564e | 2009-05-26 13:14:06 +0200 | [diff] [blame] | 376 | const struct xt_time_info *info = (const void *)match->data; |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 377 | unsigned int h, m, s; |
| 378 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 379 | printf(" TIME"); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 380 | |
| 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 Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 384 | printf(" from %02u:%02u:%02u", h, m, s); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 385 | divide_time(info->daytime_stop, &h, &m, &s); |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 386 | printf(" to %02u:%02u:%02u", h, m, s); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 387 | } |
| 388 | if (info->weekdays_match != XT_TIME_ALL_WEEKDAYS) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 389 | printf(" on"); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 390 | time_print_weekdays(info->weekdays_match); |
| 391 | } |
| 392 | if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 393 | printf(" on"); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 394 | time_print_monthdays(info->monthdays_match, true); |
| 395 | } |
| 396 | if (info->date_start != 0) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 397 | printf(" starting from"); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 398 | time_print_date(info->date_start, NULL); |
| 399 | } |
Patrick McHardy | fceebd8 | 2007-10-18 12:34:20 +0000 | [diff] [blame] | 400 | if (info->date_stop != INT_MAX) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 401 | printf(" until date"); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 402 | time_print_date(info->date_stop, NULL); |
| 403 | } |
| 404 | if (!(info->flags & XT_TIME_LOCAL_TZ)) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 405 | printf(" UTC"); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static 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 Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 416 | printf(" --timestart %02u:%02u:%02u", h, m, s); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 417 | divide_time(info->daytime_stop, &h, &m, &s); |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 418 | printf(" --timestop %02u:%02u:%02u", h, m, s); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 419 | } |
| 420 | if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 421 | printf(" --monthdays"); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 422 | time_print_monthdays(info->monthdays_match, false); |
| 423 | } |
| 424 | if (info->weekdays_match != XT_TIME_ALL_WEEKDAYS) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 425 | printf(" --weekdays"); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 426 | time_print_weekdays(info->weekdays_match); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 427 | } |
| 428 | time_print_date(info->date_start, "--datestart"); |
| 429 | time_print_date(info->date_stop, "--datestop"); |
Jan Engelhardt | db50b83 | 2011-05-23 18:38:09 +0200 | [diff] [blame] | 430 | if (info->flags & XT_TIME_LOCAL_TZ) |
| 431 | printf(" --kerneltz"); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 434 | static struct xtables_match time_match = { |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 435 | .name = "time", |
Jan Engelhardt | 4297936 | 2009-06-01 11:56:23 +0200 | [diff] [blame] | 436 | .family = NFPROTO_UNSPEC, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 437 | .version = XTABLES_VERSION, |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 438 | .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 Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 442 | .print = time_print, |
| 443 | .save = time_save, |
Jan Engelhardt | d64d547 | 2011-03-02 23:03:36 +0100 | [diff] [blame] | 444 | .x6_parse = time_parse, |
| 445 | .x6_options = time_opts, |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 446 | }; |
| 447 | |
| 448 | void _init(void) |
| 449 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 450 | xtables_register_match(&time_match); |
Jan Engelhardt | ad326ef | 2007-09-23 15:17:42 +0000 | [diff] [blame] | 451 | } |