1 /* $OpenBSD: src/sbin/dhclient/parse.c,v 1.20 2011/12/10 17:15:27 krw Exp $ */
3 /* Common parser code for dhcpd and dhclient. */
6 * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The Internet Software Consortium nor the names
19 * of its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This software has been written for the Internet Software Consortium
37 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38 * Enterprises. To learn more about the Internet Software Consortium,
39 * see ``http://www.vix.com/isc''. To learn more about Vixie
40 * Enterprises, see ``http://www.vix.com''.
47 * Skip to the semicolon ending the current statement. If we encounter
48 * braces, the matching closing brace terminates the statement. If we
49 * encounter a right brace but haven't encountered a left brace, return
50 * leaving the brace in the token buffer for the caller. If we see a
51 * semicolon and haven't seen a left brace, return. This lets us skip
55 * statement foo bar { }
56 * statement foo bar { statement { } }
62 skip_to_semi(FILE *cfile)
68 token = peek_token(NULL, cfile);
71 token = next_token(NULL, cfile);
76 } else if (token == '{') {
78 } else if (token == ';' && !brace_count) {
79 token = next_token(NULL, cfile);
81 } else if (token == '\n') {
83 * EOL only happens when parsing
84 * /etc/resolv.conf, and we treat it like a
85 * semicolon because the resolv.conf file is
88 token = next_token(NULL, cfile);
91 token = next_token(NULL, cfile);
92 } while (token != EOF);
96 parse_semi(FILE *cfile)
100 token = next_token(NULL, cfile);
102 parse_warn("semicolon expected.");
110 * string-parameter :== STRING SEMI
113 parse_string(FILE *cfile)
118 token = next_token(&val, cfile);
119 if (token != TOK_STRING) {
120 parse_warn("filename must be a string");
126 error("no memory for string %s.", val);
128 if (!parse_semi(cfile)) {
136 parse_ip_addr(FILE *cfile, struct iaddr *addr)
139 return (parse_numeric_aggregate(cfile, addr->iabuf, addr->len, '.',
144 * hardware-parameter :== HARDWARE ETHERNET csns SEMI
145 * csns :== NUMBER | csns COLON NUMBER
148 parse_hardware_param(FILE *cfile, struct hardware *hardware)
152 token = next_token(NULL, cfile);
155 hardware->htype = HTYPE_ETHER;
159 hardware->htype = HTYPE_IEEE802;
163 hardware->htype = HTYPE_FDDI;
167 parse_warn("expecting a network hardware type");
172 if (parse_numeric_aggregate(cfile, hardware->haddr, hardware->hlen,
176 token = next_token(NULL, cfile);
178 parse_warn("expecting semicolon.");
184 * lease-time :== NUMBER SEMI
187 parse_lease_time(FILE *cfile, time_t *timep)
192 token = next_token(&val, cfile);
193 if (token != TOK_NUMBER) {
194 parse_warn("Expecting numeric lease time");
198 convert_num((unsigned char *)timep, val, 10, 32);
199 /* Unswap the number - convert_num returns stuff in NBO. */
200 *timep = ntohl(*timep); /* XXX */
206 * Parse a sequence of numbers separated by the token specified in separator.
207 * Exactly max numbers are expected.
210 parse_numeric_aggregate(FILE *cfile, unsigned char *buf, int max, int separator,
216 if (buf == NULL || max == 0)
217 error("no space for numeric aggregate");
219 for (count = 0; count < max; count++, buf++) {
220 if (count && (peek_token(&val, cfile) == separator))
221 token = next_token(&val, cfile);
223 token = next_token(&val, cfile);
225 if (token == TOK_NUMBER || (base == 16 && token == TOK_NUMBER_OR_NAME))
226 /* XXX Need to check if conversion was successful. */
227 convert_num(buf, val, base, 8);
233 parse_warn("numeric aggregate too short.");
241 convert_num(unsigned char *buf, char *str, int base, int size)
243 int negative = 0, tval, max;
252 /* If base wasn't specified, figure it out from the data. */
258 } else if (isascii(ptr[1]) && isdigit(ptr[1])) {
269 /* XXX assumes ASCII... */
271 tval = tval - 'a' + 10;
272 else if (tval >= 'A')
273 tval = tval - 'A' + 10;
274 else if (tval >= '0')
277 warning("Bogus number: %s.", str);
281 warning("Bogus number: %s: digit %d not in base %d",
285 val = val * base + tval;
289 max = (1 << (size - 1));
291 max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
295 warning("value %s%o exceeds max (%d) for precision.",
296 negative ? "-" : "", val, max);
299 warning("value %s%x exceeds max (%d) for precision.",
300 negative ? "-" : "", val, max);
303 warning("value %s%u exceeds max (%d) for precision.",
304 negative ? "-" : "", val, max);
312 *buf = -(unsigned long)val;
315 putShort(buf, -(unsigned long)val);
318 putLong(buf, -(unsigned long)val);
321 warning("Unexpected integer size: %d", size);
327 *buf = (u_int8_t)val;
330 putUShort(buf, (u_int16_t)val);
336 warning("Unexpected integer size: %d", size);
342 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER
343 * NUMBER COLON NUMBER COLON NUMBER SEMI
345 * Dates are always in GMT; first number is day of week; next is
346 * year/month/day; next is hours:minutes:seconds on a 24-hour
350 parse_date(FILE *cfile)
352 static int months[11] = { 31, 59, 90, 120, 151, 181,
353 212, 243, 273, 304, 334 };
359 token = next_token(&val, cfile);
360 if (token != TOK_NUMBER) {
361 parse_warn("numeric day of week expected.");
366 tm.tm_wday = atoi(val);
369 token = next_token(&val, cfile);
370 if (token != TOK_NUMBER) {
371 parse_warn("numeric year expected.");
376 tm.tm_year = atoi(val);
377 if (tm.tm_year > 1900)
380 /* Slash separating year from month... */
381 token = next_token(&val, cfile);
383 parse_warn("expected slash separating year from month.");
390 token = next_token(&val, cfile);
391 if (token != TOK_NUMBER) {
392 parse_warn("numeric month expected.");
397 tm.tm_mon = atoi(val) - 1;
399 /* Slash separating month from day... */
400 token = next_token(&val, cfile);
402 parse_warn("expected slash separating month from day.");
409 token = next_token(&val, cfile);
410 if (token != TOK_NUMBER) {
411 parse_warn("numeric day of month expected.");
416 tm.tm_mday = atoi(val);
419 token = next_token(&val, cfile);
420 if (token != TOK_NUMBER) {
421 parse_warn("numeric hour expected.");
426 tm.tm_hour = atoi(val);
428 /* Colon separating hour from minute... */
429 token = next_token(&val, cfile);
431 parse_warn("expected colon separating hour from minute.");
438 token = next_token(&val, cfile);
439 if (token != TOK_NUMBER) {
440 parse_warn("numeric minute expected.");
445 tm.tm_min = atoi(val);
447 /* Colon separating minute from second... */
448 token = next_token(&val, cfile);
450 parse_warn("expected colon separating minute from second.");
457 token = next_token(&val, cfile);
458 if (token != TOK_NUMBER) {
459 parse_warn("numeric second expected.");
464 tm.tm_sec = atoi(val);
467 /* XXX: We assume that mktime does not use tm_yday. */
470 /* Make sure the date ends in a semicolon... */
471 token = next_token(&val, cfile);
473 parse_warn("semicolon expected.");
478 /* Guess the time value... */
479 guess = ((((((365 * (tm.tm_year - 70) + /* Days in years since '70 */
480 (tm.tm_year - 69) / 4 + /* Leap days since '70 */
481 (tm.tm_mon /* Days in months this year */
482 ? months[tm.tm_mon - 1] : 0) +
483 (tm.tm_mon > 1 && /* Leap day this year */
484 !((tm.tm_year - 72) & 3)) +
485 tm.tm_mday - 1) * 24) + /* Day of month */
486 tm.tm_hour) * 60) + tm.tm_min) * 60) + tm.tm_sec;
489 * This guess could be wrong because of leap seconds or other
490 * weirdness we don't know about that the system does. For
491 * now, we're just going to accept the guess, but at some point
492 * it might be nice to do a successive approximation here to get
493 * an exact value. Even if the error is small, if the server
494 * is restarted frequently (and thus the lease database is
495 * reread), the error could accumulate into something