diff options
Diffstat (limited to 'sbin/dhclient')
-rw-r--r-- | sbin/dhclient/dhclient.c | 12 | ||||
-rw-r--r-- | sbin/dhclient/dhcpd.h | 5 | ||||
-rw-r--r-- | sbin/dhclient/parse.c | 185 |
3 files changed, 58 insertions, 144 deletions
diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c index c07e0d7cc64..cb044d9d9cc 100644 --- a/sbin/dhclient/dhclient.c +++ b/sbin/dhclient/dhclient.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dhclient.c,v 1.243 2013/04/25 06:43:20 otto Exp $ */ +/* $OpenBSD: dhclient.c,v 1.244 2013/04/27 17:54:24 krw Exp $ */ /* * Copyright 2004 Henning Brauer <henning@openbsd.org> @@ -1675,18 +1675,20 @@ lease_as_string(char *type, struct client_lease *lease) sz -= rslt; } -#define TIMEFMT "%u %Y/%m/%d %T;\n" - rsltsz = strftime(p, sz, " renew " TIMEFMT, gmtime(&lease->renewal)); + rsltsz = strftime(p, sz, " renew " DB_TIMEFMT ";\n", + gmtime(&lease->renewal)); if (rsltsz == 0) return (NULL); p += rsltsz; sz -= rsltsz; - rsltsz = strftime(p, sz, " rebind " TIMEFMT, gmtime(&lease->rebind)); + rsltsz = strftime(p, sz, " rebind " DB_TIMEFMT ";\n", + gmtime(&lease->rebind)); if (rsltsz == 0) return (NULL); p += rsltsz; sz -= rsltsz; - rsltsz = strftime(p, sz, " expire " TIMEFMT, gmtime(&lease->expiry)); + rsltsz = strftime(p, sz, " expire " DB_TIMEFMT ";\n", + gmtime(&lease->expiry)); if (rsltsz == 0) return (NULL); p += rsltsz; diff --git a/sbin/dhclient/dhcpd.h b/sbin/dhclient/dhcpd.h index 8fab3123b6e..485db698e7c 100644 --- a/sbin/dhclient/dhcpd.h +++ b/sbin/dhclient/dhcpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dhcpd.h,v 1.113 2013/04/05 19:19:05 krw Exp $ */ +/* $OpenBSD: dhcpd.h,v 1.114 2013/04/27 17:54:24 krw Exp $ */ /* * Copyright (c) 2004 Henning Brauer <henning@openbsd.org> @@ -73,6 +73,9 @@ #define LOCAL_PORT 68 #define REMOTE_PORT 67 #define INTERNALSIG INT_MAX +#define DB_TIMEFMT "%w %Y/%m/%d %T UTC" +#define BAD_DB_TIMEFMT "%u %Y/%m/%d %T" +#define OLD_DB_TIMEFMT "%w %Y/%m/%d %T" struct option { char *name; diff --git a/sbin/dhclient/parse.c b/sbin/dhclient/parse.c index 2c71eeaa5e9..f9b4b22f077 100644 --- a/sbin/dhclient/parse.c +++ b/sbin/dhclient/parse.c @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.c,v 1.22 2013/04/02 02:37:41 guenther Exp $ */ +/* $OpenBSD: parse.c,v 1.23 2013/04/27 17:54:24 krw Exp $ */ /* Common parser code for dhcpd and dhclient. */ @@ -339,160 +339,69 @@ convert_num(unsigned char *buf, char *str, int base, int size) /* * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER - * NUMBER COLON NUMBER COLON NUMBER SEMI + * NUMBER COLON NUMBER COLON NUMBER UTC SEMI * - * Dates are always in GMT; first number is day of week; next is + * Dates are always in UTC; first number is day of week; next is * year/month/day; next is hours:minutes:seconds on a 24-hour * clock. */ time_t parse_date(FILE *cfile) { - static int months[11] = { 31, 59, 90, 120, 151, 181, - 212, 243, 273, 304, 334 }; - int guess, token; struct tm tm; - char *val; - - /* Day of week... */ - token = next_token(&val, cfile); - if (token != TOK_NUMBER) { - parse_warn("numeric day of week expected."); - if (token != ';') - skip_to_semi(cfile); - return (0); - } - tm.tm_wday = atoi(val); - - /* Year... */ - token = next_token(&val, cfile); - if (token != TOK_NUMBER) { - parse_warn("numeric year expected."); - if (token != ';') - skip_to_semi(cfile); - return (0); - } - tm.tm_year = atoi(val); - if (tm.tm_year > 1900) - tm.tm_year -= 1900; - - /* Slash separating year from month... */ - token = next_token(&val, cfile); - if (token != '/') { - parse_warn("expected slash separating year from month."); - if (token != ';') - skip_to_semi(cfile); - return (0); - } - - /* Month... */ - token = next_token(&val, cfile); - if (token != TOK_NUMBER) { - parse_warn("numeric month expected."); - if (token != ';') - skip_to_semi(cfile); - return (0); - } - tm.tm_mon = atoi(val) - 1; - - /* Slash separating month from day... */ - token = next_token(&val, cfile); - if (token != '/') { - parse_warn("expected slash separating month from day."); - if (token != ';') - skip_to_semi(cfile); - return (0); - } - - /* Day... */ - token = next_token(&val, cfile); - if (token != TOK_NUMBER) { - parse_warn("numeric day of month expected."); - if (token != ';') - skip_to_semi(cfile); - return (0); - } - tm.tm_mday = atoi(val); - - /* Hour... */ - token = next_token(&val, cfile); - if (token != TOK_NUMBER) { - parse_warn("numeric hour expected."); - if (token != ';') - skip_to_semi(cfile); - return (0); - } - tm.tm_hour = atoi(val); + char timestr[26]; /* "w yyyy/mm/dd hh:mm:ss UTC" */ + char *val, *p; + size_t n; + time_t guess; + int token; - /* Colon separating hour from minute... */ - token = next_token(&val, cfile); - if (token != ':') { - parse_warn("expected colon separating hour from minute."); - if (token != ';') - skip_to_semi(cfile); - return (0); - } + memset(timestr, 0, sizeof(timestr)); - /* Minute... */ - token = next_token(&val, cfile); - if (token != TOK_NUMBER) { - parse_warn("numeric minute expected."); - if (token != ';') + do { + token = peek_token(NULL, cfile); + switch (token) { + case TOK_NAME: + case TOK_NUMBER: + case '/': + case ':': + token = next_token(&val, cfile); + n = strlcat(timestr, val, sizeof(timestr)); + if (n >= sizeof(timestr)) { + /* XXX Will break after year 9999! */ + parse_warn("time string too long"); + skip_to_semi(cfile); + return (0); + } + break; + case';': + break; + default: + parse_warn("invalid time string"); skip_to_semi(cfile); - return (0); - } - tm.tm_min = atoi(val); + return (0); + } + } while (token != ';'); - /* Colon separating minute from second... */ - token = next_token(&val, cfile); - if (token != ':') { - parse_warn("expected colon separating minute from second."); - if (token != ';') - skip_to_semi(cfile); - return (0); - } + parse_semi(cfile); - /* Second... */ - token = next_token(&val, cfile); - if (token != TOK_NUMBER) { - parse_warn("numeric second expected."); - if (token != ';') - skip_to_semi(cfile); - return (0); + memset(&tm, 0, sizeof(tm)); /* 'cuz strptime ignores tm_isdt. */ + p = strptime(timestr, DB_TIMEFMT, &tm); + if (p == NULL || *p != '\0') { + p = strptime(timestr, OLD_DB_TIMEFMT, &tm); + if (p == NULL || *p != '\0') { + p = strptime(timestr, BAD_DB_TIMEFMT, &tm); + if (p == NULL || *p != '\0') { + parse_warn("unparseable time string"); + return (0); + } + } } - tm.tm_sec = atoi(val); - tm.tm_isdst = 0; - - /* XXX: We assume that mktime does not use tm_yday. */ - tm.tm_yday = 0; - /* Make sure the date ends in a semicolon... */ - token = next_token(&val, cfile); - if (token != ';') { - parse_warn("semicolon expected."); - skip_to_semi(cfile); + guess = timegm(&tm); + if (guess == -1) { + parse_warn("time could not be represented"); return (0); } - /* Guess the time value... */ - guess = ((((((365 * (tm.tm_year - 70) + /* Days in years since '70 */ - (tm.tm_year - 69) / 4 + /* Leap days since '70 */ - (tm.tm_mon /* Days in months this year */ - ? months[tm.tm_mon - 1] : 0) + - (tm.tm_mon > 1 && /* Leap day this year */ - !((tm.tm_year - 72) & 3)) + - tm.tm_mday - 1) * 24) + /* Day of month */ - tm.tm_hour) * 60) + tm.tm_min) * 60) + tm.tm_sec; - - /* - * This guess could be wrong because of leap seconds or other - * weirdness we don't know about that the system does. For - * now, we're just going to accept the guess, but at some point - * it might be nice to do a successive approximation here to get - * an exact value. Even if the error is small, if the server - * is restarted frequently (and thus the lease database is - * reread), the error could accumulate into something - * significant. - */ return (guess); } |