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$");
49 /* Skip to the semicolon ending the current statement. If we encounter
50 * braces, the matching closing brace terminates the statement. If we
51 * encounter a right brace but haven't encountered a left brace, return
52 * leaving the brace in the token buffer for the caller. If we see a
53 * semicolon and haven't seen a left brace, return. This lets us skip
57 * statement foo bar { }
58 * statement foo bar { statement { } }
64 skip_to_semi(FILE *cfile)
66 int brace_count = 0, token;
70 token = peek_token(&val, cfile);
71 if (token == RBRACE) {
73 token = next_token(&val, cfile);
78 } else if (token == LBRACE) {
80 } else if (token == SEMI && !brace_count) {
81 token = next_token(&val, cfile);
83 } else if (token == '\n') {
85 * EOL only happens when parsing
86 * /etc/resolv.conf, and we treat it like a
87 * semicolon because the resolv.conf file is
90 token = next_token(&val, cfile);
93 token = next_token(&val, cfile);
94 } while (token != EOF);
98 parse_semi(FILE *cfile)
103 token = next_token(&val, cfile);
105 parse_warn("semicolon expected.");
113 * string-parameter :== STRING SEMI
116 parse_string(FILE *cfile)
121 token = next_token(&val, cfile);
122 if (token != STRING) {
123 parse_warn("filename must be a string");
127 s = malloc(strlen(val) + 1);
129 error("no memory for string %s.", val);
130 strlcpy(s, val, strlen(val) + 1);
132 if (!parse_semi(cfile))
138 parse_ip_addr(FILE *cfile, struct iaddr *addr)
141 if (parse_numeric_aggregate(cfile, addr->iabuf,
142 &addr->len, DOT, 10, 8))
148 * hardware-parameter :== HARDWARE ETHERNET csns SEMI
149 * csns :== NUMBER | csns COLON NUMBER
152 parse_hardware_param(FILE *cfile, struct hardware *hardware)
158 token = next_token(&val, cfile);
161 hardware->htype = HTYPE_ETHER;
164 hardware->htype = HTYPE_IEEE802;
167 hardware->htype = HTYPE_FDDI;
170 parse_warn("expecting a network hardware type");
176 * Parse the hardware address information. Technically, it
177 * would make a lot of sense to restrict the length of the data
178 * we'll accept here to the length of a particular hardware
179 * address type. Unfortunately, there are some broken clients
180 * out there that put bogus data in the chaddr buffer, and we
181 * accept that data in the lease file rather than simply failing
182 * on such clients. Yuck.
185 t = parse_numeric_aggregate(cfile, NULL, &hlen, COLON, 16, 8);
188 if (hlen > sizeof(hardware->haddr)) {
190 parse_warn("hardware address too long");
192 hardware->hlen = hlen;
193 memcpy((unsigned char *)&hardware->haddr[0], t,
195 if (hlen < sizeof(hardware->haddr))
196 memset(&hardware->haddr[hlen], 0,
197 sizeof(hardware->haddr) - hlen);
201 token = next_token(&val, cfile);
203 parse_warn("expecting semicolon.");
209 * lease-time :== NUMBER SEMI
212 parse_lease_time(FILE *cfile, time_t *timep)
217 token = next_token(&val, cfile);
218 if (token != NUMBER) {
219 parse_warn("Expecting numeric lease time");
223 convert_num((unsigned char *)timep, val, 10, 32);
224 /* Unswap the number - convert_num returns stuff in NBO. */
225 *timep = ntohl(*timep); /* XXX */
231 * No BNF for numeric aggregates - that's defined by the caller. What
232 * this function does is to parse a sequence of numbers separated by the
233 * token specified in separator. If max is zero, any number of numbers
234 * will be parsed; otherwise, exactly max numbers are expected. Base
235 * and size tell us how to internalize the numbers once they've been
239 parse_numeric_aggregate(FILE *cfile, unsigned char *buf, int *max,
240 int separator, int base, int size)
242 unsigned char *bufp = buf, *s = NULL;
243 int token, count = 0;
248 bufp = malloc(*max * size / 8);
250 error("can't allocate space for numeric aggregate");
256 token = peek_token(&val, cfile);
257 if (token != separator) {
260 if (token != RBRACE && token != LBRACE)
261 token = next_token(&val, cfile);
262 parse_warn("too few numbers.");
267 token = next_token(&val, cfile);
269 token = next_token(&val, cfile);
272 parse_warn("unexpected end of file");
276 /* Allow NUMBER_OR_NAME if base is 16. */
277 if (token != NUMBER &&
278 (base != 16 || token != NUMBER_OR_NAME)) {
279 parse_warn("expecting numeric value.");
284 * If we can, convert the number now; otherwise, build a
285 * linked list of all the numbers.
288 convert_num(s, val, base, size);
291 t = malloc(strlen(val) + 1);
293 error("no temp space for number.");
294 strlcpy(t, val, strlen(val) + 1);
297 } while (++count != *max);
299 /* If we had to cons up a list, convert it now. */
301 bufp = malloc(count * size / 8);
303 error("can't allocate space for numeric aggregate.");
304 s = bufp + count - size / 8;
309 convert_num(s, (char *)c->car, base, size);
311 /* Free up temp space. */
320 convert_num(unsigned char *buf, char *str, int base, int size)
322 int negative = 0, tval, max;
331 /* If base wasn't specified, figure it out from the data. */
337 } else if (isascii(ptr[1]) && isdigit(ptr[1])) {
348 /* XXX assumes ASCII... */
350 tval = tval - 'a' + 10;
351 else if (tval >= 'A')
352 tval = tval - 'A' + 10;
353 else if (tval >= '0')
356 warning("Bogus number: %s.", str);
360 warning("Bogus number: %s: digit %d not in base %d",
364 val = val * base + tval;
368 max = (1 << (size - 1));
370 max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
374 warning("value %s%o exceeds max (%d) for precision.",
375 negative ? "-" : "", val, max);
378 warning("value %s%x exceeds max (%d) for precision.",
379 negative ? "-" : "", val, max);
382 warning("value %s%u exceeds max (%d) for precision.",
383 negative ? "-" : "", val, max);
391 *buf = -(unsigned long)val;
394 putShort(buf, -(unsigned long)val);
397 putLong(buf, -(unsigned long)val);
400 warning("Unexpected integer size: %d", size);
406 *buf = (u_int8_t)val;
409 putUShort(buf, (u_int16_t)val);
415 warning("Unexpected integer size: %d", size);
421 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER
422 * NUMBER COLON NUMBER COLON NUMBER SEMI
424 * Dates are always in GMT; first number is day of week; next is
425 * year/month/day; next is hours:minutes:seconds on a 24-hour
429 parse_date(FILE *cfile)
431 static int months[11] = { 31, 59, 90, 120, 151, 181,
432 212, 243, 273, 304, 334 };
438 token = next_token(&val, cfile);
439 if (token != NUMBER) {
440 parse_warn("numeric day of week expected.");
445 tm.tm_wday = atoi(val);
448 token = next_token(&val, cfile);
449 if (token != NUMBER) {
450 parse_warn("numeric year expected.");
455 tm.tm_year = atoi(val);
456 if (tm.tm_year > 1900)
459 /* Slash separating year from month... */
460 token = next_token(&val, cfile);
461 if (token != SLASH) {
462 parse_warn("expected slash separating year from month.");
469 token = next_token(&val, cfile);
470 if (token != NUMBER) {
471 parse_warn("numeric month expected.");
476 tm.tm_mon = atoi(val) - 1;
478 /* Slash separating month from day... */
479 token = next_token(&val, cfile);
480 if (token != SLASH) {
481 parse_warn("expected slash separating month from day.");
488 token = next_token(&val, cfile);
489 if (token != NUMBER) {
490 parse_warn("numeric day of month expected.");
495 tm.tm_mday = atoi(val);
498 token = next_token(&val, cfile);
499 if (token != NUMBER) {
500 parse_warn("numeric hour expected.");
505 tm.tm_hour = atoi(val);
507 /* Colon separating hour from minute... */
508 token = next_token(&val, cfile);
509 if (token != COLON) {
510 parse_warn("expected colon separating hour from minute.");
517 token = next_token(&val, cfile);
518 if (token != NUMBER) {
519 parse_warn("numeric minute expected.");
524 tm.tm_min = atoi(val);
526 /* Colon separating minute from second... */
527 token = next_token(&val, cfile);
528 if (token != COLON) {
529 parse_warn("expected colon separating hour from minute.");
536 token = next_token(&val, cfile);
537 if (token != NUMBER) {
538 parse_warn("numeric minute expected.");
543 tm.tm_sec = atoi(val);
546 /* XXX: We assume that mktime does not use tm_yday. */
549 /* Make sure the date ends in a semicolon... */
550 token = next_token(&val, cfile);
552 parse_warn("semicolon expected.");
557 /* Guess the time value... */
558 guess = ((((((365 * (tm.tm_year - 70) + /* Days in years since '70 */
559 (tm.tm_year - 69) / 4 + /* Leap days since '70 */
560 (tm.tm_mon /* Days in months this year */
561 ? months[tm.tm_mon - 1]
563 (tm.tm_mon > 1 && /* Leap day this year */
564 !((tm.tm_year - 72) & 3)) +
565 tm.tm_mday - 1) * 24) + /* Day of month */
567 tm.tm_min) * 60) + tm.tm_sec;
570 * This guess could be wrong because of leap seconds or other
571 * weirdness we don't know about that the system does. For
572 * now, we're just going to accept the guess, but at some point
573 * it might be nice to do a successive approximation here to get
574 * an exact value. Even if the error is small, if the server
575 * is restarted frequently (and thus the lease database is
576 * reread), the error could accumulate into something