lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * libxt_time - iptables part for xt_time |
| 3 | * Copyright © CC Computer Consultants GmbH, 2007 |
| 4 | * Contact: <jengelh@computergmbh.de> |
| 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 | */ |
| 12 | #include <sys/types.h> |
| 13 | #include <getopt.h> |
| 14 | #include <stdbool.h> |
| 15 | #include <stdint.h> |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <stddef.h> |
| 20 | #include <time.h> |
| 21 | #include <limits.h> |
| 22 | #include <ctype.h> |
| 23 | |
| 24 | #include <linux/netfilter/xt_time.h> |
| 25 | #include <xtables.h> |
| 26 | |
| 27 | enum { /* getopt "seen" bits */ |
| 28 | F_DATE_START = 1 << 0, |
| 29 | F_DATE_STOP = 1 << 1, |
| 30 | F_TIME_START = 1 << 2, |
| 31 | F_TIME_STOP = 1 << 3, |
| 32 | F_MONTHDAYS = 1 << 4, |
| 33 | F_WEEKDAYS = 1 << 5, |
| 34 | F_TIMEZONE = 1 << 6, |
| 35 | }; |
| 36 | |
| 37 | static const char *const week_days[] = { |
| 38 | NULL, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", |
| 39 | }; |
| 40 | |
| 41 | static const struct option time_opts_v0[] = { |
| 42 | {"datestart", true, NULL, 'D'}, |
| 43 | {"datestop", true, NULL, 'E'}, |
| 44 | {"timestart", true, NULL, 'X'}, |
| 45 | {"timestop", true, NULL, 'Y'}, |
| 46 | {"weekdays", true, NULL, 'w'}, |
| 47 | {"monthdays", true, NULL, 'm'}, |
| 48 | {"localtz", false, NULL, 'l'}, |
| 49 | {"utc", false, NULL, 'u'}, |
| 50 | { .name = NULL } |
| 51 | }; |
| 52 | |
| 53 | static const struct option time_opts[] = { |
| 54 | {"datestart", true, NULL, 'D'}, |
| 55 | {"datestop", true, NULL, 'E'}, |
| 56 | {"timestart", true, NULL, 'X'}, |
| 57 | {"timestop", true, NULL, 'Y'}, |
| 58 | {"weekdays", true, NULL, 'w'}, |
| 59 | {"monthdays", true, NULL, 'm'}, |
| 60 | {"localtz", false, NULL, 'l'}, |
| 61 | {"utc", false, NULL, 'u'}, |
| 62 | {"tz", true, NULL, 'z'}, |
| 63 | { .name = NULL } |
| 64 | }; |
| 65 | |
| 66 | static void time_help(void) |
| 67 | { |
| 68 | printf( |
| 69 | "time match options:\n" |
| 70 | " --datestart time Start and stop time, to be given in ISO 8601\n" |
| 71 | " --datestop time (YYYY[-MM[-DD[Thh[:mm[:ss]]]]])\n" |
| 72 | " --timestart time Start and stop daytime (hh:mm[:ss])\n" |
| 73 | " --timestop time (between 00:00:00 and 23:59:59)\n" |
| 74 | "[!] --monthdays value List of days on which to match, separated by comma\n" |
| 75 | " (Possible days: 1 to 31; defaults to all)\n" |
| 76 | "[!] --weekdays value List of weekdays on which to match, sep. by comma\n" |
| 77 | " (Possible days: Mon,Tue,Wed,Thu,Fri,Sat,Sun or 1 to 7\n" |
| 78 | " Defaults to all weekdays.)\n" |
| 79 | " --localtz/--utc Time is interpreted as UTC/local time\n" |
| 80 | " --tz tzspec Time is interpreted using timezone spec\n"); |
| 81 | } |
| 82 | |
| 83 | static void time_init(struct xt_entry_match *m) |
| 84 | { |
| 85 | struct xt_time_info1 *info = (void *)m->data; |
| 86 | |
| 87 | /* By default, we match on every day, every daytime */ |
| 88 | info->monthdays_match = XT_TIME_ALL_MONTHDAYS; |
| 89 | info->weekdays_match = XT_TIME_ALL_WEEKDAYS; |
| 90 | info->daytime_start = XT_TIME_MIN_DAYTIME; |
| 91 | info->daytime_stop = XT_TIME_MAX_DAYTIME; |
| 92 | |
| 93 | /* ...and have no date-begin or date-end boundary */ |
| 94 | info->date_start = 0; |
| 95 | info->date_stop = INT_MAX; |
| 96 | |
| 97 | /* local time is default */ |
| 98 | info->flags |= XT_TIME_LOCAL_TZ; |
| 99 | } |
| 100 | |
| 101 | static time_t time_parse_date(const char *s, bool end) |
| 102 | { |
| 103 | unsigned int month = 1, day = 1, hour = 0, minute = 0, second = 0; |
| 104 | unsigned int year = end ? 2038 : 1970; |
| 105 | const char *os = s; |
| 106 | struct tm tm; |
| 107 | time_t ret; |
| 108 | char *e; |
| 109 | |
| 110 | year = strtoul(s, &e, 10); |
| 111 | if ((*e != '-' && *e != '\0') || year < 1970 || year > 2038) |
| 112 | goto out; |
| 113 | if (*e == '\0') |
| 114 | goto eval; |
| 115 | |
| 116 | s = e + 1; |
| 117 | month = strtoul(s, &e, 10); |
| 118 | if ((*e != '-' && *e != '\0') || month > 12) |
| 119 | goto out; |
| 120 | if (*e == '\0') |
| 121 | goto eval; |
| 122 | |
| 123 | s = e + 1; |
| 124 | day = strtoul(s, &e, 10); |
| 125 | if ((*e != 'T' && *e != '\0') || day > 31) |
| 126 | goto out; |
| 127 | if (*e == '\0') |
| 128 | goto eval; |
| 129 | |
| 130 | s = e + 1; |
| 131 | hour = strtoul(s, &e, 10); |
| 132 | if ((*e != ':' && *e != '\0') || hour > 23) |
| 133 | goto out; |
| 134 | if (*e == '\0') |
| 135 | goto eval; |
| 136 | |
| 137 | s = e + 1; |
| 138 | minute = strtoul(s, &e, 10); |
| 139 | if ((*e != ':' && *e != '\0') || minute > 59) |
| 140 | goto out; |
| 141 | if (*e == '\0') |
| 142 | goto eval; |
| 143 | |
| 144 | s = e + 1; |
| 145 | second = strtoul(s, &e, 10); |
| 146 | if (*e != '\0' || second > 59) |
| 147 | goto out; |
| 148 | |
| 149 | eval: |
| 150 | tm.tm_year = year - 1900; |
| 151 | tm.tm_mon = month - 1; |
| 152 | tm.tm_mday = day; |
| 153 | tm.tm_hour = hour; |
| 154 | tm.tm_min = minute; |
| 155 | tm.tm_sec = second; |
| 156 | ret = mktime(&tm); |
| 157 | if (ret >= 0) |
| 158 | return ret; |
| 159 | perror("mktime"); |
| 160 | xtables_error(OTHER_PROBLEM, "mktime returned an error"); |
| 161 | |
| 162 | out: |
| 163 | xtables_error(PARAMETER_PROBLEM, "Invalid date \"%s\" specified. Should " |
| 164 | "be YYYY[-MM[-DD[Thh[:mm[:ss]]]]]", os); |
| 165 | return -1; |
| 166 | } |
| 167 | |
| 168 | static unsigned int time_parse_minutes(const char *s) |
| 169 | { |
| 170 | unsigned int hour, minute, second = 0; |
| 171 | char *e; |
| 172 | |
| 173 | hour = strtoul(s, &e, 10); |
| 174 | if (*e != ':' || hour > 23) |
| 175 | goto out; |
| 176 | |
| 177 | s = e + 1; |
| 178 | minute = strtoul(s, &e, 10); |
| 179 | if ((*e != ':' && *e != '\0') || minute > 59) |
| 180 | goto out; |
| 181 | if (*e == '\0') |
| 182 | goto eval; |
| 183 | |
| 184 | s = e + 1; |
| 185 | second = strtoul(s, &e, 10); |
| 186 | if (*e != '\0' || second > 59) |
| 187 | goto out; |
| 188 | |
| 189 | eval: |
| 190 | return 60 * 60 * hour + 60 * minute + second; |
| 191 | |
| 192 | out: |
| 193 | xtables_error(PARAMETER_PROBLEM, "invalid time \"%s\" specified, " |
| 194 | "should be hh:mm[:ss] format and within the boundaries", s); |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | static const char *my_strseg(char *buf, unsigned int buflen, |
| 199 | const char **arg, char delim) |
| 200 | { |
| 201 | const char *sep; |
| 202 | |
| 203 | if (*arg == NULL || **arg == '\0') |
| 204 | return NULL; |
| 205 | sep = strchr(*arg, delim); |
| 206 | if (sep == NULL) { |
| 207 | snprintf(buf, buflen, "%s", *arg); |
| 208 | *arg = NULL; |
| 209 | return buf; |
| 210 | } |
| 211 | snprintf(buf, buflen, "%.*s", (unsigned int)(sep - *arg), *arg); |
| 212 | *arg = sep + 1; |
| 213 | return buf; |
| 214 | } |
| 215 | |
| 216 | static uint32_t time_parse_monthdays(const char *arg) |
| 217 | { |
| 218 | char day[3], *err = NULL; |
| 219 | uint32_t ret = 0; |
| 220 | unsigned int i; |
| 221 | |
| 222 | while (my_strseg(day, sizeof(day), &arg, ',') != NULL) { |
| 223 | i = strtoul(day, &err, 0); |
| 224 | if ((*err != ',' && *err != '\0') || i > 31) |
| 225 | xtables_error(PARAMETER_PROBLEM, |
| 226 | "%s is not a valid day for --monthdays", day); |
| 227 | ret |= 1 << i; |
| 228 | } |
| 229 | |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | static unsigned int time_parse_weekdays(const char *arg) |
| 234 | { |
| 235 | char day[4], *err = NULL; |
| 236 | unsigned int i, ret = 0; |
| 237 | bool valid; |
| 238 | |
| 239 | while (my_strseg(day, sizeof(day), &arg, ',') != NULL) { |
| 240 | i = strtoul(day, &err, 0); |
| 241 | if (*err == '\0') { |
| 242 | if (i == 0) |
| 243 | xtables_error(PARAMETER_PROBLEM, |
| 244 | "No, the week does NOT begin with Sunday."); |
| 245 | ret |= 1 << i; |
| 246 | continue; |
| 247 | } |
| 248 | |
| 249 | valid = false; |
| 250 | for (i = 1; i < ARRAY_SIZE(week_days); ++i) |
| 251 | if (strncmp(day, week_days[i], 2) == 0) { |
| 252 | ret |= 1 << i; |
| 253 | valid = true; |
| 254 | } |
| 255 | |
| 256 | if (!valid) |
| 257 | xtables_error(PARAMETER_PROBLEM, |
| 258 | "%s is not a valid day specifier", day); |
| 259 | } |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | static const char *time_parse_tz_time(const char *s, u_int32_t *t) |
| 265 | { |
| 266 | unsigned int hour, minute = 0, second = 0; |
| 267 | const char *os = s; |
| 268 | char *e; |
| 269 | |
| 270 | if (!isdigit(*s)) |
| 271 | goto out; |
| 272 | hour = strtoul(s, &e, 10); |
| 273 | if (hour > 24) |
| 274 | goto out; |
| 275 | if (*e != ':') |
| 276 | goto eval; |
| 277 | |
| 278 | s = e + 1; |
| 279 | if (!isdigit(*s)) |
| 280 | goto out; |
| 281 | minute = strtoul(s, &e, 10); |
| 282 | if (minute > 59) |
| 283 | goto out; |
| 284 | if (*e != ':') |
| 285 | goto eval; |
| 286 | |
| 287 | s = e + 1; |
| 288 | if (!isdigit(*s)) |
| 289 | goto out; |
| 290 | second = strtoul(s, &e, 10); |
| 291 | if (second > 59) |
| 292 | goto out; |
| 293 | |
| 294 | eval: |
| 295 | *t = 60 * 60 * hour + 60 * minute + second; |
| 296 | return e; |
| 297 | |
| 298 | out: |
| 299 | xtables_error(PARAMETER_PROBLEM, "invalid time \"%s\" specified, " |
| 300 | "should be hh[:mm[:ss]] format and within the boundaries", |
| 301 | os); |
| 302 | return NULL; |
| 303 | } |
| 304 | |
| 305 | static const char *time_parse_tz_offset(const char *s, int32_t *offset) |
| 306 | { |
| 307 | int neg = 0; |
| 308 | u_int32_t t; |
| 309 | |
| 310 | if (*s == '+') |
| 311 | s++; |
| 312 | else if (*s =='-') { |
| 313 | neg = 1; |
| 314 | s++; |
| 315 | } |
| 316 | |
| 317 | s = time_parse_tz_time(s, &t); |
| 318 | *offset = neg ? -t : t; |
| 319 | return s; |
| 320 | } |
| 321 | |
| 322 | static const char *time_parse_tz_rule(const char *s, |
| 323 | struct xt_time_info1 *info, int rule) |
| 324 | { |
| 325 | unsigned long l; |
| 326 | const char *os = s; |
| 327 | char *e; |
| 328 | |
| 329 | if (isdigit(*s)) { |
| 330 | info->tz[rule].type = XT_TIME_TZ_TYPE_J0; |
| 331 | l = strtoul(s, &e, 10); |
| 332 | if (l > 365) |
| 333 | goto out; |
| 334 | info->tz[rule].day = l; |
| 335 | s = e; |
| 336 | } else if (*s == 'J') { |
| 337 | info->tz[rule].type = XT_TIME_TZ_TYPE_J1; |
| 338 | s++; |
| 339 | |
| 340 | if (!isdigit(*s)) |
| 341 | goto out; |
| 342 | l = strtoul(s, &e, 10); |
| 343 | if (l < 1 || l > 365) |
| 344 | goto out; |
| 345 | info->tz[rule].day = l; |
| 346 | s = e; |
| 347 | } else if (*s == 'M') { |
| 348 | info->tz[rule].type = XT_TIME_TZ_TYPE_M; |
| 349 | s++; |
| 350 | |
| 351 | l = strtoul(s, &e, 10); |
| 352 | if (l < 1 || l > 12) |
| 353 | goto out; |
| 354 | info->tz[rule].month = l; |
| 355 | if (*e != '.') |
| 356 | goto out; |
| 357 | s = e + 1; |
| 358 | |
| 359 | l = strtoul(s, &e, 10); |
| 360 | if (l < 1 || l > 5) |
| 361 | goto out; |
| 362 | info->tz[rule].week = l; |
| 363 | if (*e != '.') |
| 364 | goto out; |
| 365 | s = e + 1; |
| 366 | |
| 367 | l = strtoul(s, &e, 10); |
| 368 | if (l > 6) |
| 369 | goto out; |
| 370 | info->tz[rule].day = l; |
| 371 | s = e; |
| 372 | } else |
| 373 | goto out; |
| 374 | |
| 375 | if (*s == '/') |
| 376 | s = time_parse_tz_time(s + 1, &info->tz[rule].secs); |
| 377 | else |
| 378 | info->tz[rule].secs = 2 * 60 * 60; /* 2:00:00 */ |
| 379 | |
| 380 | return s; |
| 381 | |
| 382 | out: |
| 383 | xtables_error(PARAMETER_PROBLEM, |
| 384 | "invalid tz rule \"%s\" specified", os); |
| 385 | return NULL; |
| 386 | } |
| 387 | |
| 388 | static void time_parse_tz(struct xt_time_info1 *info, const char *arg) |
| 389 | { |
| 390 | const char *p; |
| 391 | size_t l; |
| 392 | |
| 393 | /* Parse STD name and offset */ |
| 394 | p = arg; |
| 395 | l = strcspn(p, "+-0123456789,"); |
| 396 | if (l < 0 || l > 6) |
| 397 | xtables_error(PARAMETER_PROBLEM, |
| 398 | "invalid or missing std name in %s", arg); |
| 399 | memcpy(info->tz[0].name, p, l); |
| 400 | p += l; |
| 401 | |
| 402 | if (!*p || *p == ',') |
| 403 | xtables_error(PARAMETER_PROBLEM, |
| 404 | "missing std offset in %s", arg); |
| 405 | |
| 406 | p = time_parse_tz_offset(p, &info->tz[0].offset); |
| 407 | |
| 408 | if (!*p) { |
| 409 | /* No DST */ |
| 410 | info->tz[1].offset = info->tz[0].offset; |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | /* Parse DST name and optional offset */ |
| 415 | l = strcspn(p, "+-0123456789,"); |
| 416 | if (l < 0 || l > 6) |
| 417 | xtables_error(PARAMETER_PROBLEM, |
| 418 | "invalid or missing dst name in %s", arg); |
| 419 | memcpy(info->tz[1].name, p, l); |
| 420 | p += l; |
| 421 | |
| 422 | if (!*p) |
| 423 | xtables_error(PARAMETER_PROBLEM, |
| 424 | "missing dst offset or rule in %s", arg); |
| 425 | |
| 426 | if (*p == ',') { |
| 427 | info->tz[1].offset = info->tz[0].offset + (60 * 60); |
| 428 | } else { |
| 429 | p = time_parse_tz_offset(p, &info->tz[1].offset); |
| 430 | } |
| 431 | |
| 432 | /* Parse start rule */ |
| 433 | if (*p == ',') |
| 434 | p++; |
| 435 | if (!*p) |
| 436 | xtables_error(PARAMETER_PROBLEM, |
| 437 | "missing dst start rule in %s", arg); |
| 438 | p = time_parse_tz_rule(p, info, 0); |
| 439 | |
| 440 | /* Parse end rule */ |
| 441 | if (*p == ',') |
| 442 | p++; |
| 443 | if (!*p) |
| 444 | xtables_error(PARAMETER_PROBLEM, |
| 445 | "missing dst end rule in %s", arg); |
| 446 | p = time_parse_tz_rule(p, info, 1); |
| 447 | |
| 448 | if (*p) |
| 449 | xtables_error(PARAMETER_PROBLEM, |
| 450 | "invalid tz %s", arg); |
| 451 | } |
| 452 | |
| 453 | static int time_parse(int c, char **argv, int invert, unsigned int *flags, |
| 454 | const void *entry, struct xt_entry_match **match) |
| 455 | { |
| 456 | struct xt_time_info1 *info = (void *)(*match)->data; |
| 457 | |
| 458 | switch (c) { |
| 459 | case 'D': /* --datestart */ |
| 460 | if (*flags & F_DATE_START) |
| 461 | xtables_error(PARAMETER_PROBLEM, |
| 462 | "Cannot specify --datestart twice"); |
| 463 | if (invert) |
| 464 | xtables_error(PARAMETER_PROBLEM, |
| 465 | "Unexpected \"!\" with --datestart"); |
| 466 | info->date_start = time_parse_date(optarg, false); |
| 467 | *flags |= F_DATE_START; |
| 468 | return 1; |
| 469 | case 'E': /* --datestop */ |
| 470 | if (*flags & F_DATE_STOP) |
| 471 | xtables_error(PARAMETER_PROBLEM, |
| 472 | "Cannot specify --datestop more than once"); |
| 473 | if (invert) |
| 474 | xtables_error(PARAMETER_PROBLEM, |
| 475 | "unexpected \"!\" with --datestop"); |
| 476 | info->date_stop = time_parse_date(optarg, true); |
| 477 | *flags |= F_DATE_STOP; |
| 478 | return 1; |
| 479 | case 'X': /* --timestart */ |
| 480 | if (*flags & F_TIME_START) |
| 481 | xtables_error(PARAMETER_PROBLEM, |
| 482 | "Cannot specify --timestart more than once"); |
| 483 | if (invert) |
| 484 | xtables_error(PARAMETER_PROBLEM, |
| 485 | "Unexpected \"!\" with --timestart"); |
| 486 | info->daytime_start = time_parse_minutes(optarg); |
| 487 | *flags |= F_TIME_START; |
| 488 | return 1; |
| 489 | case 'Y': /* --timestop */ |
| 490 | if (*flags & F_TIME_STOP) |
| 491 | xtables_error(PARAMETER_PROBLEM, |
| 492 | "Cannot specify --timestop more than once"); |
| 493 | if (invert) |
| 494 | xtables_error(PARAMETER_PROBLEM, |
| 495 | "Unexpected \"!\" with --timestop"); |
| 496 | info->daytime_stop = time_parse_minutes(optarg); |
| 497 | *flags |= F_TIME_STOP; |
| 498 | return 1; |
| 499 | case 'l': /* --localtz */ |
| 500 | if (*flags & F_TIMEZONE) |
| 501 | xtables_error(PARAMETER_PROBLEM, |
| 502 | "Can only specify exactly one of --tz, --localtz or --utc"); |
| 503 | info->flags |= XT_TIME_LOCAL_TZ; |
| 504 | *flags |= F_TIMEZONE; |
| 505 | return 1; |
| 506 | case 'm': /* --monthdays */ |
| 507 | if (*flags & F_MONTHDAYS) |
| 508 | xtables_error(PARAMETER_PROBLEM, |
| 509 | "Cannot specify --monthdays more than once"); |
| 510 | info->monthdays_match = time_parse_monthdays(optarg); |
| 511 | if (invert) |
| 512 | info->monthdays_match ^= XT_TIME_ALL_MONTHDAYS; |
| 513 | *flags |= F_MONTHDAYS; |
| 514 | return 1; |
| 515 | case 'w': /* --weekdays */ |
| 516 | if (*flags & F_WEEKDAYS) |
| 517 | xtables_error(PARAMETER_PROBLEM, |
| 518 | "Cannot specify --weekdays more than once"); |
| 519 | info->weekdays_match = time_parse_weekdays(optarg); |
| 520 | if (invert) |
| 521 | info->weekdays_match ^= XT_TIME_ALL_WEEKDAYS; |
| 522 | *flags |= F_WEEKDAYS; |
| 523 | return 1; |
| 524 | case 'u': /* --utc */ |
| 525 | if (*flags & F_TIMEZONE) |
| 526 | xtables_error(PARAMETER_PROBLEM, |
| 527 | "Can only specify exactly one of --tz, --localtz or --utc"); |
| 528 | info->flags &= ~XT_TIME_LOCAL_TZ; |
| 529 | *flags |= F_TIMEZONE; |
| 530 | return 1; |
| 531 | case 'z': /* --tz */ |
| 532 | if (*flags & F_TIMEZONE) |
| 533 | xtables_error(PARAMETER_PROBLEM, |
| 534 | "Can only specify exactly one of --tz, --localtz or --utc"); |
| 535 | info->flags &= ~XT_TIME_LOCAL_TZ; |
| 536 | info->flags |= XT_TIME_TZ; |
| 537 | time_parse_tz(info, optarg); |
| 538 | *flags |= F_TIMEZONE; |
| 539 | return 1; |
| 540 | } |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | static void time_print_date(time_t date, const char *command) |
| 545 | { |
| 546 | struct tm *t; |
| 547 | |
| 548 | /* If it is the default value, do not print it. */ |
| 549 | if (date == 0 || date == LONG_MAX) |
| 550 | return; |
| 551 | |
| 552 | t = localtime(&date); |
| 553 | if (command != NULL) |
| 554 | /* |
| 555 | * Need a contiguous string (no whitespaces), hence using |
| 556 | * the ISO 8601 "T" variant. |
| 557 | */ |
| 558 | printf("%s %04u-%02u-%02uT%02u:%02u:%02u ", |
| 559 | command, t->tm_year + 1900, t->tm_mon + 1, |
| 560 | t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); |
| 561 | else |
| 562 | printf("%04u-%02u-%02u %02u:%02u:%02u ", |
| 563 | t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, |
| 564 | t->tm_hour, t->tm_min, t->tm_sec); |
| 565 | } |
| 566 | |
| 567 | static void time_print_monthdays(uint32_t mask, bool human_readable) |
| 568 | { |
| 569 | unsigned int i, nbdays = 0; |
| 570 | |
| 571 | for (i = 1; i <= 31; ++i) |
| 572 | if (mask & (1 << i)) { |
| 573 | if (nbdays++ > 0) |
| 574 | printf(","); |
| 575 | printf("%u", i); |
| 576 | if (human_readable) |
| 577 | switch (i % 10) { |
| 578 | case 1: |
| 579 | printf("st"); |
| 580 | break; |
| 581 | case 2: |
| 582 | printf("nd"); |
| 583 | break; |
| 584 | case 3: |
| 585 | printf("rd"); |
| 586 | break; |
| 587 | default: |
| 588 | printf("th"); |
| 589 | break; |
| 590 | } |
| 591 | } |
| 592 | printf(" "); |
| 593 | } |
| 594 | |
| 595 | static void time_print_weekdays(unsigned int mask) |
| 596 | { |
| 597 | unsigned int i, nbdays = 0; |
| 598 | |
| 599 | for (i = 1; i <= 7; ++i) |
| 600 | if (mask & (1 << i)) { |
| 601 | if (nbdays > 0) |
| 602 | printf(",%s", week_days[i]); |
| 603 | else |
| 604 | printf("%s", week_days[i]); |
| 605 | ++nbdays; |
| 606 | } |
| 607 | printf(" "); |
| 608 | } |
| 609 | |
| 610 | static inline void divide_time(unsigned int fulltime, unsigned int *hours, |
| 611 | unsigned int *minutes, unsigned int *seconds) |
| 612 | { |
| 613 | *seconds = fulltime % 60; |
| 614 | fulltime /= 60; |
| 615 | *minutes = fulltime % 60; |
| 616 | *hours = fulltime / 60; |
| 617 | } |
| 618 | |
| 619 | static void time_print_tz_offset(const struct xt_time_info1 *info, int rule) |
| 620 | { |
| 621 | unsigned int h, m, s; |
| 622 | |
| 623 | printf("%s", info->tz[rule].name); |
| 624 | if (info->tz[rule].offset < 0) { |
| 625 | printf("-"); |
| 626 | divide_time(-info->tz[rule].offset, &h, &m, &s); |
| 627 | } else |
| 628 | divide_time(info->tz[rule].offset, &h, &m, &s); |
| 629 | if (s) |
| 630 | printf("%u:%02u:%02u", h, m, s); |
| 631 | else if (m) |
| 632 | printf("%u:%02u", h, m); |
| 633 | else |
| 634 | printf("%u", h); |
| 635 | |
| 636 | } |
| 637 | |
| 638 | static void time_print_tz_rule(const struct xt_time_info1 *info, int rule) |
| 639 | { |
| 640 | unsigned int h, m, s; |
| 641 | |
| 642 | printf(","); |
| 643 | switch (info->tz[rule].type) { |
| 644 | case XT_TIME_TZ_TYPE_J0: |
| 645 | printf("%u", info->tz[rule].day); |
| 646 | break; |
| 647 | case XT_TIME_TZ_TYPE_J1: |
| 648 | printf("J%u", info->tz[rule].day); |
| 649 | break; |
| 650 | case XT_TIME_TZ_TYPE_M: |
| 651 | printf("M%u.%u.%u", info->tz[rule].month, |
| 652 | info->tz[rule].week, info->tz[rule].day); |
| 653 | } |
| 654 | printf("/"); |
| 655 | divide_time(info->tz[rule].secs, &h, &m, &s); |
| 656 | if (s) |
| 657 | printf("%u:%02u:%02u", h, m, s); |
| 658 | else if (m) |
| 659 | printf("%u:%02u", h, m); |
| 660 | else |
| 661 | printf("%u", h); |
| 662 | } |
| 663 | |
| 664 | static void time_print_tz(const struct xt_time_info1 *info) |
| 665 | { |
| 666 | time_print_tz_offset(info, 0); |
| 667 | if (info->tz[0].offset == info->tz[1].offset) |
| 668 | return; |
| 669 | time_print_tz_offset(info, 1); |
| 670 | time_print_tz_rule(info, 0); |
| 671 | time_print_tz_rule(info, 1); |
| 672 | } |
| 673 | |
| 674 | static void time_print(const void *ip, const struct xt_entry_match *match, |
| 675 | int numeric) |
| 676 | { |
| 677 | struct xt_time_info1 *info = (void *)match->data; |
| 678 | unsigned int h, m, s; |
| 679 | |
| 680 | printf("TIME "); |
| 681 | |
| 682 | if (info->daytime_start != XT_TIME_MIN_DAYTIME || |
| 683 | info->daytime_stop != XT_TIME_MAX_DAYTIME) { |
| 684 | divide_time(info->daytime_start, &h, &m, &s); |
| 685 | printf("from %02u:%02u:%02u ", h, m, s); |
| 686 | divide_time(info->daytime_stop, &h, &m, &s); |
| 687 | printf("to %02u:%02u:%02u ", h, m, s); |
| 688 | } |
| 689 | if (info->weekdays_match != XT_TIME_ALL_WEEKDAYS) { |
| 690 | printf("on "); |
| 691 | time_print_weekdays(info->weekdays_match); |
| 692 | } |
| 693 | if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) { |
| 694 | printf("on "); |
| 695 | time_print_monthdays(info->monthdays_match, true); |
| 696 | } |
| 697 | if (info->date_start != 0) { |
| 698 | printf("starting from "); |
| 699 | time_print_date(info->date_start, NULL); |
| 700 | } |
| 701 | if (info->date_stop != INT_MAX) { |
| 702 | printf("until date "); |
| 703 | time_print_date(info->date_stop, NULL); |
| 704 | } |
| 705 | if (info->flags & XT_TIME_TZ) |
| 706 | time_print_tz(info); |
| 707 | else if (!(info->flags & XT_TIME_LOCAL_TZ)) |
| 708 | printf("UTC "); |
| 709 | } |
| 710 | |
| 711 | static void time_save(const void *ip, const struct xt_entry_match *match) |
| 712 | { |
| 713 | const struct xt_time_info1 *info = (const void *)match->data; |
| 714 | unsigned int h, m, s; |
| 715 | |
| 716 | if (info->daytime_start != XT_TIME_MIN_DAYTIME || |
| 717 | info->daytime_stop != XT_TIME_MAX_DAYTIME) { |
| 718 | divide_time(info->daytime_start, &h, &m, &s); |
| 719 | printf("--timestart %02u:%02u:%02u ", h, m, s); |
| 720 | divide_time(info->daytime_stop, &h, &m, &s); |
| 721 | printf("--timestop %02u:%02u:%02u ", h, m, s); |
| 722 | } |
| 723 | if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) { |
| 724 | printf("--monthdays "); |
| 725 | time_print_monthdays(info->monthdays_match, false); |
| 726 | } |
| 727 | if (info->weekdays_match != XT_TIME_ALL_WEEKDAYS) { |
| 728 | printf("--weekdays "); |
| 729 | time_print_weekdays(info->weekdays_match); |
| 730 | printf(" "); |
| 731 | } |
| 732 | time_print_date(info->date_start, "--datestart"); |
| 733 | time_print_date(info->date_stop, "--datestop"); |
| 734 | if (info->flags & XT_TIME_TZ) { |
| 735 | printf("--tz "); |
| 736 | time_print_tz(info); |
| 737 | printf(" "); |
| 738 | } else if (!(info->flags & XT_TIME_LOCAL_TZ)) |
| 739 | printf("--utc "); |
| 740 | } |
| 741 | |
| 742 | static struct xtables_match time_match_v0 = { |
| 743 | .name = "time", |
| 744 | .revision = 0, |
| 745 | .family = AF_UNSPEC, |
| 746 | .version = XTABLES_VERSION, |
| 747 | .size = XT_ALIGN(sizeof(struct xt_time_info)), |
| 748 | .userspacesize = XT_ALIGN(sizeof(struct xt_time_info)), |
| 749 | .help = time_help, |
| 750 | .init = time_init, |
| 751 | .parse = time_parse, |
| 752 | .print = time_print, |
| 753 | .save = time_save, |
| 754 | .extra_opts = time_opts_v0, |
| 755 | }; |
| 756 | |
| 757 | static struct xtables_match time_match = { |
| 758 | .name = "time", |
| 759 | .revision = 1, |
| 760 | .family = AF_UNSPEC, |
| 761 | .version = XTABLES_VERSION, |
| 762 | .size = XT_ALIGN(sizeof(struct xt_time_info1)), |
| 763 | .userspacesize = XT_ALIGN(sizeof(struct xt_time_info1)), |
| 764 | .help = time_help, |
| 765 | .init = time_init, |
| 766 | .parse = time_parse, |
| 767 | .print = time_print, |
| 768 | .save = time_save, |
| 769 | .extra_opts = time_opts, |
| 770 | }; |
| 771 | |
| 772 | void _init(void) |
| 773 | { |
| 774 | xtables_register_match(&time_match_v0); |
| 775 | xtables_register_match(&time_match); |
| 776 | } |