libxt_time.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <time.h>
  16. #include <linux/types.h>
  17. #include <linux/netfilter/xt_time.h>
  18. #include <xtables.h>
  19. enum {
  20. O_DATE_START = 0,
  21. O_DATE_STOP,
  22. O_TIME_START,
  23. O_TIME_STOP,
  24. O_TIME_CONTIGUOUS,
  25. O_MONTHDAYS,
  26. O_WEEKDAYS,
  27. O_LOCAL_TZ,
  28. O_UTC,
  29. O_KERNEL_TZ,
  30. F_LOCAL_TZ = 1 << O_LOCAL_TZ,
  31. F_UTC = 1 << O_UTC,
  32. F_KERNEL_TZ = 1 << O_KERNEL_TZ,
  33. F_TIME_CONTIGUOUS = 1 << O_TIME_CONTIGUOUS,
  34. };
  35. static const char *const week_days[] = {
  36. NULL, "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
  37. };
  38. static const struct xt_option_entry time_opts[] = {
  39. {.name = "datestart", .id = O_DATE_START, .type = XTTYPE_STRING},
  40. {.name = "datestop", .id = O_DATE_STOP, .type = XTTYPE_STRING},
  41. {.name = "timestart", .id = O_TIME_START, .type = XTTYPE_STRING},
  42. {.name = "timestop", .id = O_TIME_STOP, .type = XTTYPE_STRING},
  43. {.name = "contiguous", .id = O_TIME_CONTIGUOUS, .type = XTTYPE_NONE},
  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},
  48. {.name = "localtz", .id = O_LOCAL_TZ, .type = XTTYPE_NONE,
  49. .excl = F_UTC},
  50. {.name = "utc", .id = O_UTC, .type = XTTYPE_NONE,
  51. .excl = F_LOCAL_TZ | F_KERNEL_TZ},
  52. {.name = "kerneltz", .id = O_KERNEL_TZ, .type = XTTYPE_NONE,
  53. .excl = F_UTC},
  54. XTOPT_TABLEEND,
  55. };
  56. static void time_help(void)
  57. {
  58. printf(
  59. "time match options:\n"
  60. " --datestart time Start and stop time, to be given in ISO 8601\n"
  61. " --datestop time (YYYY[-MM[-DD[Thh[:mm[:ss]]]]])\n"
  62. " --timestart time Start and stop daytime (hh:mm[:ss])\n"
  63. " --timestop time (between 00:00:00 and 23:59:59)\n"
  64. "[!] --monthdays value List of days on which to match, separated by comma\n"
  65. " (Possible days: 1 to 31; defaults to all)\n"
  66. "[!] --weekdays value List of weekdays on which to match, sep. by comma\n"
  67. " (Possible days: Mon,Tue,Wed,Thu,Fri,Sat,Sun or 1 to 7\n"
  68. " Defaults to all weekdays.)\n"
  69. " --kerneltz Work with the kernel timezone instead of UTC\n");
  70. }
  71. static void time_init(struct xt_entry_match *m)
  72. {
  73. struct xt_time_info *info = (void *)m->data;
  74. /* By default, we match on every day, every daytime */
  75. info->monthdays_match = XT_TIME_ALL_MONTHDAYS;
  76. info->weekdays_match = XT_TIME_ALL_WEEKDAYS;
  77. info->daytime_start = XT_TIME_MIN_DAYTIME;
  78. info->daytime_stop = XT_TIME_MAX_DAYTIME;
  79. /* ...and have no date-begin or date-end boundary */
  80. info->date_start = 0;
  81. info->date_stop = INT_MAX;
  82. }
  83. static time_t time_parse_date(const char *s, bool end)
  84. {
  85. unsigned int month = 1, day = 1, hour = 0, minute = 0, second = 0;
  86. unsigned int year = end ? 2038 : 1970;
  87. const char *os = s;
  88. struct tm tm;
  89. time_t ret;
  90. char *e;
  91. year = strtoul(s, &e, 10);
  92. if ((*e != '-' && *e != '\0') || year < 1970 || year > 2038)
  93. goto out;
  94. if (*e == '\0')
  95. goto eval;
  96. s = e + 1;
  97. month = strtoul(s, &e, 10);
  98. if ((*e != '-' && *e != '\0') || month > 12)
  99. goto out;
  100. if (*e == '\0')
  101. goto eval;
  102. s = e + 1;
  103. day = strtoul(s, &e, 10);
  104. if ((*e != 'T' && *e != '\0') || day > 31)
  105. goto out;
  106. if (*e == '\0')
  107. goto eval;
  108. s = e + 1;
  109. hour = strtoul(s, &e, 10);
  110. if ((*e != ':' && *e != '\0') || hour > 23)
  111. goto out;
  112. if (*e == '\0')
  113. goto eval;
  114. s = e + 1;
  115. minute = strtoul(s, &e, 10);
  116. if ((*e != ':' && *e != '\0') || minute > 59)
  117. goto out;
  118. if (*e == '\0')
  119. goto eval;
  120. s = e + 1;
  121. second = strtoul(s, &e, 10);
  122. if (*e != '\0' || second > 59)
  123. goto out;
  124. eval:
  125. tm.tm_year = year - 1900;
  126. tm.tm_mon = month - 1;
  127. tm.tm_mday = day;
  128. tm.tm_hour = hour;
  129. tm.tm_min = minute;
  130. tm.tm_sec = second;
  131. tm.tm_isdst = 0;
  132. /*
  133. * Offsetting, if any, is done by xt_time.ko,
  134. * so we have to disable it here in userspace.
  135. */
  136. setenv("TZ", "UTC", true);
  137. tzset();
  138. ret = mktime(&tm);
  139. if (ret >= 0)
  140. return ret;
  141. perror("mktime");
  142. xtables_error(OTHER_PROBLEM, "mktime returned an error");
  143. out:
  144. xtables_error(PARAMETER_PROBLEM, "Invalid date \"%s\" specified. Should "
  145. "be YYYY[-MM[-DD[Thh[:mm[:ss]]]]]", os);
  146. return -1;
  147. }
  148. static unsigned int time_parse_minutes(const char *s)
  149. {
  150. unsigned int hour, minute, second = 0;
  151. char *e;
  152. hour = strtoul(s, &e, 10);
  153. if (*e != ':' || hour > 23)
  154. goto out;
  155. s = e + 1;
  156. minute = strtoul(s, &e, 10);
  157. if ((*e != ':' && *e != '\0') || minute > 59)
  158. goto out;
  159. if (*e == '\0')
  160. goto eval;
  161. s = e + 1;
  162. second = strtoul(s, &e, 10);
  163. if (*e != '\0' || second > 59)
  164. goto out;
  165. eval:
  166. return 60 * 60 * hour + 60 * minute + second;
  167. out:
  168. xtables_error(PARAMETER_PROBLEM, "invalid time \"%s\" specified, "
  169. "should be hh:mm[:ss] format and within the boundaries", s);
  170. return -1;
  171. }
  172. static const char *my_strseg(char *buf, unsigned int buflen,
  173. const char **arg, char delim)
  174. {
  175. const char *sep;
  176. if (*arg == NULL || **arg == '\0')
  177. return NULL;
  178. sep = strchr(*arg, delim);
  179. if (sep == NULL) {
  180. snprintf(buf, buflen, "%s", *arg);
  181. *arg = NULL;
  182. return buf;
  183. }
  184. snprintf(buf, buflen, "%.*s", (unsigned int)(sep - *arg), *arg);
  185. *arg = sep + 1;
  186. return buf;
  187. }
  188. static uint32_t time_parse_monthdays(const char *arg)
  189. {
  190. char day[3], *err = NULL;
  191. uint32_t ret = 0;
  192. unsigned int i;
  193. while (my_strseg(day, sizeof(day), &arg, ',') != NULL) {
  194. i = strtoul(day, &err, 0);
  195. if ((*err != ',' && *err != '\0') || i > 31)
  196. xtables_error(PARAMETER_PROBLEM,
  197. "%s is not a valid day for --monthdays", day);
  198. ret |= 1 << i;
  199. }
  200. return ret;
  201. }
  202. static unsigned int time_parse_weekdays(const char *arg)
  203. {
  204. char day[4], *err = NULL;
  205. unsigned int i, ret = 0;
  206. bool valid;
  207. while (my_strseg(day, sizeof(day), &arg, ',') != NULL) {
  208. i = strtoul(day, &err, 0);
  209. if (*err == '\0') {
  210. if (i == 0)
  211. xtables_error(PARAMETER_PROBLEM,
  212. "No, the week does NOT begin with Sunday.");
  213. ret |= 1 << i;
  214. continue;
  215. }
  216. valid = false;
  217. for (i = 1; i < ARRAY_SIZE(week_days); ++i)
  218. if (strncmp(day, week_days[i], 2) == 0) {
  219. ret |= 1 << i;
  220. valid = true;
  221. }
  222. if (!valid)
  223. xtables_error(PARAMETER_PROBLEM,
  224. "%s is not a valid day specifier", day);
  225. }
  226. return ret;
  227. }
  228. static void time_parse(struct xt_option_call *cb)
  229. {
  230. struct xt_time_info *info = cb->data;
  231. xtables_option_parse(cb);
  232. switch (cb->entry->id) {
  233. case O_DATE_START:
  234. info->date_start = time_parse_date(cb->arg, false);
  235. break;
  236. case O_DATE_STOP:
  237. info->date_stop = time_parse_date(cb->arg, true);
  238. break;
  239. case O_TIME_START:
  240. info->daytime_start = time_parse_minutes(cb->arg);
  241. break;
  242. case O_TIME_STOP:
  243. info->daytime_stop = time_parse_minutes(cb->arg);
  244. break;
  245. case O_TIME_CONTIGUOUS:
  246. info->flags |= XT_TIME_CONTIGUOUS;
  247. break;
  248. case O_LOCAL_TZ:
  249. fprintf(stderr, "WARNING: --localtz is being replaced by "
  250. "--kerneltz, since \"local\" is ambiguous. Note the "
  251. "kernel timezone has caveats - "
  252. "see manpage for details.\n");
  253. /* fallthrough */
  254. case O_KERNEL_TZ:
  255. info->flags |= XT_TIME_LOCAL_TZ;
  256. break;
  257. case O_MONTHDAYS:
  258. info->monthdays_match = time_parse_monthdays(cb->arg);
  259. if (cb->invert)
  260. info->monthdays_match ^= XT_TIME_ALL_MONTHDAYS;
  261. break;
  262. case O_WEEKDAYS:
  263. info->weekdays_match = time_parse_weekdays(cb->arg);
  264. if (cb->invert)
  265. info->weekdays_match ^= XT_TIME_ALL_WEEKDAYS;
  266. break;
  267. }
  268. }
  269. static void time_print_date(time_t date, const char *command)
  270. {
  271. struct tm *t;
  272. /* If it is the default value, do not print it. */
  273. if (date == 0 || date == LONG_MAX)
  274. return;
  275. t = gmtime(&date);
  276. if (command != NULL)
  277. /*
  278. * Need a contiguous string (no whitespaces), hence using
  279. * the ISO 8601 "T" variant.
  280. */
  281. printf(" %s %04u-%02u-%02uT%02u:%02u:%02u",
  282. command, t->tm_year + 1900, t->tm_mon + 1,
  283. t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
  284. else
  285. printf(" %04u-%02u-%02u %02u:%02u:%02u",
  286. t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
  287. t->tm_hour, t->tm_min, t->tm_sec);
  288. }
  289. static void time_print_monthdays(uint32_t mask, bool human_readable)
  290. {
  291. unsigned int i, nbdays = 0;
  292. printf(" ");
  293. for (i = 1; i <= 31; ++i)
  294. if (mask & (1 << i)) {
  295. if (nbdays++ > 0)
  296. printf(",");
  297. printf("%u", i);
  298. if (human_readable)
  299. switch (i % 10) {
  300. case 1:
  301. printf("st");
  302. break;
  303. case 2:
  304. printf("nd");
  305. break;
  306. case 3:
  307. printf("rd");
  308. break;
  309. default:
  310. printf("th");
  311. break;
  312. }
  313. }
  314. }
  315. static void time_print_weekdays(unsigned int mask)
  316. {
  317. unsigned int i, nbdays = 0;
  318. printf(" ");
  319. for (i = 1; i <= 7; ++i)
  320. if (mask & (1 << i)) {
  321. if (nbdays > 0)
  322. printf(",%s", week_days[i]);
  323. else
  324. printf("%s", week_days[i]);
  325. ++nbdays;
  326. }
  327. }
  328. static inline void divide_time(unsigned int fulltime, unsigned int *hours,
  329. unsigned int *minutes, unsigned int *seconds)
  330. {
  331. *seconds = fulltime % 60;
  332. fulltime /= 60;
  333. *minutes = fulltime % 60;
  334. *hours = fulltime / 60;
  335. }
  336. static void time_print(const void *ip, const struct xt_entry_match *match,
  337. int numeric)
  338. {
  339. const struct xt_time_info *info = (const void *)match->data;
  340. unsigned int h, m, s;
  341. printf(" TIME");
  342. if (info->daytime_start != XT_TIME_MIN_DAYTIME ||
  343. info->daytime_stop != XT_TIME_MAX_DAYTIME) {
  344. divide_time(info->daytime_start, &h, &m, &s);
  345. printf(" from %02u:%02u:%02u", h, m, s);
  346. divide_time(info->daytime_stop, &h, &m, &s);
  347. printf(" to %02u:%02u:%02u", h, m, s);
  348. }
  349. if (info->weekdays_match != XT_TIME_ALL_WEEKDAYS) {
  350. printf(" on");
  351. time_print_weekdays(info->weekdays_match);
  352. }
  353. if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) {
  354. printf(" on");
  355. time_print_monthdays(info->monthdays_match, true);
  356. }
  357. if (info->date_start != 0) {
  358. printf(" starting from");
  359. time_print_date(info->date_start, NULL);
  360. }
  361. if (info->date_stop != INT_MAX) {
  362. printf(" until date");
  363. time_print_date(info->date_stop, NULL);
  364. }
  365. if (!(info->flags & XT_TIME_LOCAL_TZ))
  366. printf(" UTC");
  367. if (info->flags & XT_TIME_CONTIGUOUS)
  368. printf(" contiguous");
  369. }
  370. static void time_save(const void *ip, const struct xt_entry_match *match)
  371. {
  372. const struct xt_time_info *info = (const void *)match->data;
  373. unsigned int h, m, s;
  374. if (info->daytime_start != XT_TIME_MIN_DAYTIME ||
  375. info->daytime_stop != XT_TIME_MAX_DAYTIME) {
  376. divide_time(info->daytime_start, &h, &m, &s);
  377. printf(" --timestart %02u:%02u:%02u", h, m, s);
  378. divide_time(info->daytime_stop, &h, &m, &s);
  379. printf(" --timestop %02u:%02u:%02u", h, m, s);
  380. }
  381. if (info->monthdays_match != XT_TIME_ALL_MONTHDAYS) {
  382. printf(" --monthdays");
  383. time_print_monthdays(info->monthdays_match, false);
  384. }
  385. if (info->weekdays_match != XT_TIME_ALL_WEEKDAYS) {
  386. printf(" --weekdays");
  387. time_print_weekdays(info->weekdays_match);
  388. }
  389. time_print_date(info->date_start, "--datestart");
  390. time_print_date(info->date_stop, "--datestop");
  391. if (info->flags & XT_TIME_LOCAL_TZ)
  392. printf(" --kerneltz");
  393. if (info->flags & XT_TIME_CONTIGUOUS)
  394. printf(" --contiguous");
  395. }
  396. static void time_check(struct xt_fcheck_call *cb)
  397. {
  398. const struct xt_time_info *info = (const void *) cb->data;
  399. if ((cb->xflags & F_TIME_CONTIGUOUS) &&
  400. info->daytime_start < info->daytime_stop)
  401. xtables_error(PARAMETER_PROBLEM,
  402. "time: --contiguous only makes sense when stoptime is smaller than starttime");
  403. }
  404. static struct xtables_match time_match = {
  405. .name = "time",
  406. .family = NFPROTO_UNSPEC,
  407. .version = XTABLES_VERSION,
  408. .size = XT_ALIGN(sizeof(struct xt_time_info)),
  409. .userspacesize = XT_ALIGN(sizeof(struct xt_time_info)),
  410. .help = time_help,
  411. .init = time_init,
  412. .print = time_print,
  413. .save = time_save,
  414. .x6_parse = time_parse,
  415. .x6_fcheck = time_check,
  416. .x6_options = time_opts,
  417. };
  418. void _init(void)
  419. {
  420. xtables_register_match(&time_match);
  421. }