1 /* $OpenBSD: parse.c,v 1.11 2004/05/05 23:07:47 deraadt 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''.
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
51 /* Skip to the semicolon ending the current statement. If we encounter
52 * braces, the matching closing brace terminates the statement. If we
53 * encounter a right brace but haven't encountered a left brace, return
54 * leaving the brace in the token buffer for the caller. If we see a
55 * semicolon and haven't seen a left brace, return. This lets us skip
59 * statement foo bar { }
60 * statement foo bar { statement { } }
66 skip_to_semi(FILE *cfile)
68 int brace_count = 0, token;
72 token = peek_token(&val, cfile);
73 if (token == RBRACE) {
75 token = next_token(&val, cfile);
80 } else if (token == LBRACE) {
82 } else if (token == SEMI && !brace_count) {
83 token = next_token(&val, cfile);
85 } else if (token == '\n') {
87 * EOL only happens when parsing
88 * /etc/resolv.conf, and we treat it like a
89 * semicolon because the resolv.conf file is
92 token = next_token(&val, cfile);
95 token = next_token(&val, cfile);
96 } while (token != EOF);
100 parse_semi(FILE *cfile)
105 token = next_token(&val, cfile);
107 parse_warn("semicolon expected.");
115 * string-parameter :== STRING SEMI
118 parse_string(FILE *cfile)
124 token = next_token(&val, cfile);
125 if (token != STRING) {
126 parse_warn("filename must be a string");
130 valsize = strlen(val) + 1;
133 error("no memory for string %s.", val);
134 memcpy(s, val, valsize);
136 if (!parse_semi(cfile))
142 parse_ip_addr(FILE *cfile, struct iaddr *addr)
145 if (parse_numeric_aggregate(cfile, addr->iabuf,
146 &addr->len, DOT, 10, 8))
152 * hardware-parameter :== HARDWARE ETHERNET csns SEMI
153 * csns :== NUMBER | csns COLON NUMBER
156 parse_hardware_param(FILE *cfile, struct hardware *hardware)
163 token = next_token(&val, cfile);
166 hardware->htype = HTYPE_ETHER;
169 hardware->htype = HTYPE_IEEE802;
172 hardware->htype = HTYPE_FDDI;
175 parse_warn("expecting a network hardware type");
181 * Parse the hardware address information. Technically, it
182 * would make a lot of sense to restrict the length of the data
183 * we'll accept here to the length of a particular hardware
184 * address type. Unfortunately, there are some broken clients
185 * out there that put bogus data in the chaddr buffer, and we
186 * accept that data in the lease file rather than simply failing
187 * on such clients. Yuck.
190 t = parse_numeric_aggregate(cfile, NULL, &hlen, COLON, 16, 8);
193 if (hlen > sizeof(hardware->haddr)) {
195 parse_warn("hardware address too long");
197 hardware->hlen = hlen;
198 memcpy((unsigned char *)&hardware->haddr[0], t,
200 if (hlen < sizeof(hardware->haddr))
201 memset(&hardware->haddr[hlen], 0,
202 sizeof(hardware->haddr) - hlen);
206 token = next_token(&val, cfile);
208 parse_warn("expecting semicolon.");
214 * lease-time :== NUMBER SEMI
217 parse_lease_time(FILE *cfile, time_t *timep)
222 token = next_token(&val, cfile);
223 if (token != NUMBER) {
224 parse_warn("Expecting numeric lease time");
228 convert_num((unsigned char *)timep, val, 10, 32);
229 /* Unswap the number - convert_num returns stuff in NBO. */
230 *timep = ntohl(*timep); /* XXX */
236 * No BNF for numeric aggregates - that's defined by the caller. What
237 * this function does is to parse a sequence of numbers separated by the
238 * token specified in separator. If max is zero, any number of numbers
239 * will be parsed; otherwise, exactly max numbers are expected. Base
240 * and size tell us how to internalize the numbers once they've been
244 parse_numeric_aggregate(FILE *cfile, unsigned char *buf, size_t *max,
245 int separator, unsigned base, int size)
247 unsigned char *bufp = buf, *s = NULL;
250 size_t valsize, count = 0;
254 bufp = malloc(*max * size / 8);
256 error("can't allocate space for numeric aggregate");
262 token = peek_token(&val, cfile);
263 if (token != separator) {
266 if (token != RBRACE && token != LBRACE)
267 token = next_token(&val, cfile);
268 parse_warn("too few numbers.");
273 token = next_token(&val, cfile);
275 token = next_token(&val, cfile);
278 parse_warn("unexpected end of file");
282 /* Allow NUMBER_OR_NAME if base is 16. */
283 if (token != NUMBER &&
284 (base != 16 || token != NUMBER_OR_NAME)) {
285 parse_warn("expecting numeric value.");
290 * If we can, convert the number now; otherwise, build a
291 * linked list of all the numbers.
294 convert_num(s, val, base, size);
297 valsize = strlen(val) + 1;
300 error("no temp space for number.");
301 memcpy(t, val, valsize);
304 } while (++count != *max);
306 /* If we had to cons up a list, convert it now. */
308 bufp = malloc(count * size / 8);
310 error("can't allocate space for numeric aggregate.");
311 s = bufp + count - size / 8;
316 convert_num(s, (char *)c->car, base, size);
318 /* Free up temp space. */
327 convert_num(unsigned char *buf, char *str, unsigned base, int size)
329 bool negative = false;
339 /* If base wasn't specified, figure it out from the data. */
345 } else if (isascii(ptr[1]) && isdigit(ptr[1])) {
356 /* XXX assumes ASCII... */
358 tval = tval - 'a' + 10;
359 else if (tval >= 'A')
360 tval = tval - 'A' + 10;
361 else if (tval >= '0')
364 warning("Bogus number: %s.", str);
368 warning("Bogus number: %s: digit %d not in base %d",
372 val = val * base + tval;
376 max = (1 << (size - 1));
378 max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
382 warning("value %s%o exceeds max (%d) for precision.",
383 negative ? "-" : "", val, max);
386 warning("value %s%x exceeds max (%d) for precision.",
387 negative ? "-" : "", val, max);
390 warning("value %s%u exceeds max (%d) for precision.",
391 negative ? "-" : "", val, max);
399 *buf = -(unsigned long)val;
402 putShort(buf, -(unsigned long)val);
405 putLong(buf, -(unsigned long)val);
408 warning("Unexpected integer size: %d", size);
414 *buf = (u_int8_t)val;
417 putUShort(buf, (u_int16_t)val);
423 warning("Unexpected integer size: %d", size);
429 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER
430 * NUMBER COLON NUMBER COLON NUMBER SEMI
432 * Dates are always in GMT; first number is day of week; next is
433 * year/month/day; next is hours:minutes:seconds on a 24-hour
437 parse_date(FILE *cfile)
439 static int months[11] = { 31, 59, 90, 120, 151, 181,
440 212, 243, 273, 304, 334 };
446 token = next_token(&val, cfile);
447 if (token != NUMBER) {
448 parse_warn("numeric day of week expected.");
453 tm.tm_wday = atoi(val);
456 token = next_token(&val, cfile);
457 if (token != NUMBER) {
458 parse_warn("numeric year expected.");
463 tm.tm_year = atoi(val);
464 if (tm.tm_year > 1900)
467 /* Slash separating year from month... */
468 token = next_token(&val, cfile);
469 if (token != SLASH) {
470 parse_warn("expected slash separating year from month.");
477 token = next_token(&val, cfile);
478 if (token != NUMBER) {
479 parse_warn("numeric month expected.");
484 tm.tm_mon = atoi(val) - 1;
486 /* Slash separating month from day... */
487 token = next_token(&val, cfile);
488 if (token != SLASH) {
489 parse_warn("expected slash separating month from day.");
496 token = next_token(&val, cfile);
497 if (token != NUMBER) {
498 parse_warn("numeric day of month expected.");
503 tm.tm_mday = atoi(val);
506 token = next_token(&val, cfile);
507 if (token != NUMBER) {
508 parse_warn("numeric hour expected.");
513 tm.tm_hour = atoi(val);
515 /* Colon separating hour from minute... */
516 token = next_token(&val, cfile);
517 if (token != COLON) {
518 parse_warn("expected colon separating hour from minute.");
525 token = next_token(&val, cfile);
526 if (token != NUMBER) {
527 parse_warn("numeric minute expected.");
532 tm.tm_min = atoi(val);
534 /* Colon separating minute from second... */
535 token = next_token(&val, cfile);
536 if (token != COLON) {
537 parse_warn("expected colon separating hour from minute.");
544 token = next_token(&val, cfile);
545 if (token != NUMBER) {
546 parse_warn("numeric minute expected.");
551 tm.tm_sec = atoi(val);
554 /* XXX: We assume that mktime does not use tm_yday. */
557 /* Make sure the date ends in a semicolon... */
558 token = next_token(&val, cfile);
560 parse_warn("semicolon expected.");
565 /* Guess the time value... */
566 guess = ((((((365 * (tm.tm_year - 70) + /* Days in years since '70 */
567 (tm.tm_year - 69) / 4 + /* Leap days since '70 */
568 (tm.tm_mon /* Days in months this year */
569 ? months[tm.tm_mon - 1]
571 (tm.tm_mon > 1 && /* Leap day this year */
572 !((tm.tm_year - 72) & 3)) +
573 tm.tm_mday - 1) * 24) + /* Day of month */
575 tm.tm_min) * 60) + tm.tm_sec;
578 * This guess could be wrong because of leap seconds or other
579 * weirdness we don't know about that the system does. For
580 * now, we're just going to accept the guess, but at some point
581 * it might be nice to do a successive approximation here to get
582 * an exact value. Even if the error is small, if the server
583 * is restarted frequently (and thus the lease database is
584 * reread), the error could accumulate into something