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)
122 token = next_token(&val, cfile);
123 if (token != STRING) {
124 parse_warn("filename must be a string");
128 valsize = strlen(val) + 1;
131 error("no memory for string %s.", val);
132 memcpy(s, val, valsize);
134 if (!parse_semi(cfile))
140 parse_ip_addr(FILE *cfile, struct iaddr *addr)
143 if (parse_numeric_aggregate(cfile, addr->iabuf,
144 &addr->len, DOT, 10, 8))
150 * hardware-parameter :== HARDWARE ETHERNET csns SEMI
151 * csns :== NUMBER | csns COLON NUMBER
154 parse_hardware_param(FILE *cfile, struct hardware *hardware)
160 token = next_token(&val, cfile);
163 hardware->htype = HTYPE_ETHER;
166 hardware->htype = HTYPE_IEEE802;
169 hardware->htype = HTYPE_FDDI;
172 parse_warn("expecting a network hardware type");
178 * Parse the hardware address information. Technically, it
179 * would make a lot of sense to restrict the length of the data
180 * we'll accept here to the length of a particular hardware
181 * address type. Unfortunately, there are some broken clients
182 * out there that put bogus data in the chaddr buffer, and we
183 * accept that data in the lease file rather than simply failing
184 * on such clients. Yuck.
187 t = parse_numeric_aggregate(cfile, NULL, &hlen, COLON, 16, 8);
190 if (hlen > sizeof(hardware->haddr)) {
192 parse_warn("hardware address too long");
194 hardware->hlen = hlen;
195 memcpy((unsigned char *)&hardware->haddr[0], t,
197 if (hlen < sizeof(hardware->haddr))
198 memset(&hardware->haddr[hlen], 0,
199 sizeof(hardware->haddr) - hlen);
203 token = next_token(&val, cfile);
205 parse_warn("expecting semicolon.");
211 * lease-time :== NUMBER SEMI
214 parse_lease_time(FILE *cfile, time_t *timep)
219 token = next_token(&val, cfile);
220 if (token != NUMBER) {
221 parse_warn("Expecting numeric lease time");
225 convert_num((unsigned char *)timep, val, 10, 32);
226 /* Unswap the number - convert_num returns stuff in NBO. */
227 *timep = ntohl(*timep); /* XXX */
233 * No BNF for numeric aggregates - that's defined by the caller. What
234 * this function does is to parse a sequence of numbers separated by the
235 * token specified in separator. If max is zero, any number of numbers
236 * will be parsed; otherwise, exactly max numbers are expected. Base
237 * and size tell us how to internalize the numbers once they've been
241 parse_numeric_aggregate(FILE *cfile, unsigned char *buf, int *max,
242 int separator, int base, int size)
244 unsigned char *bufp = buf, *s = NULL;
245 int token, count = 0;
251 bufp = malloc(*max * size / 8);
253 error("can't allocate space for numeric aggregate");
259 token = peek_token(&val, cfile);
260 if (token != separator) {
263 if (token != RBRACE && token != LBRACE)
264 token = next_token(&val, cfile);
265 parse_warn("too few numbers.");
270 token = next_token(&val, cfile);
272 token = next_token(&val, cfile);
275 parse_warn("unexpected end of file");
279 /* Allow NUMBER_OR_NAME if base is 16. */
280 if (token != NUMBER &&
281 (base != 16 || token != NUMBER_OR_NAME)) {
282 parse_warn("expecting numeric value.");
287 * If we can, convert the number now; otherwise, build a
288 * linked list of all the numbers.
291 convert_num(s, val, base, size);
294 valsize = strlen(val) + 1;
297 error("no temp space for number.");
298 memcpy(t, val, valsize);
301 } while (++count != *max);
303 /* If we had to cons up a list, convert it now. */
305 bufp = malloc(count * size / 8);
307 error("can't allocate space for numeric aggregate.");
308 s = bufp + count - size / 8;
313 convert_num(s, (char *)c->car, base, size);
315 /* Free up temp space. */
324 convert_num(unsigned char *buf, char *str, int base, int size)
326 int negative = 0, tval, max;
335 /* If base wasn't specified, figure it out from the data. */
341 } else if (isascii(ptr[1]) && isdigit(ptr[1])) {
352 /* XXX assumes ASCII... */
354 tval = tval - 'a' + 10;
355 else if (tval >= 'A')
356 tval = tval - 'A' + 10;
357 else if (tval >= '0')
360 warning("Bogus number: %s.", str);
364 warning("Bogus number: %s: digit %d not in base %d",
368 val = val * base + tval;
372 max = (1 << (size - 1));
374 max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
378 warning("value %s%o exceeds max (%d) for precision.",
379 negative ? "-" : "", val, max);
382 warning("value %s%x exceeds max (%d) for precision.",
383 negative ? "-" : "", val, max);
386 warning("value %s%u exceeds max (%d) for precision.",
387 negative ? "-" : "", val, max);
395 *buf = -(unsigned long)val;
398 putShort(buf, -(unsigned long)val);
401 putLong(buf, -(unsigned long)val);
404 warning("Unexpected integer size: %d", size);
410 *buf = (u_int8_t)val;
413 putUShort(buf, (u_int16_t)val);
419 warning("Unexpected integer size: %d", size);
425 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER
426 * NUMBER COLON NUMBER COLON NUMBER SEMI
428 * Dates are always in GMT; first number is day of week; next is
429 * year/month/day; next is hours:minutes:seconds on a 24-hour
433 parse_date(FILE *cfile)
435 static int months[11] = { 31, 59, 90, 120, 151, 181,
436 212, 243, 273, 304, 334 };
442 token = next_token(&val, cfile);
443 if (token != NUMBER) {
444 parse_warn("numeric day of week expected.");
449 tm.tm_wday = atoi(val);
452 token = next_token(&val, cfile);
453 if (token != NUMBER) {
454 parse_warn("numeric year expected.");
459 tm.tm_year = atoi(val);
460 if (tm.tm_year > 1900)
463 /* Slash separating year from month... */
464 token = next_token(&val, cfile);
465 if (token != SLASH) {
466 parse_warn("expected slash separating year from month.");
473 token = next_token(&val, cfile);
474 if (token != NUMBER) {
475 parse_warn("numeric month expected.");
480 tm.tm_mon = atoi(val) - 1;
482 /* Slash separating month from day... */
483 token = next_token(&val, cfile);
484 if (token != SLASH) {
485 parse_warn("expected slash separating month from day.");
492 token = next_token(&val, cfile);
493 if (token != NUMBER) {
494 parse_warn("numeric day of month expected.");
499 tm.tm_mday = atoi(val);
502 token = next_token(&val, cfile);
503 if (token != NUMBER) {
504 parse_warn("numeric hour expected.");
509 tm.tm_hour = atoi(val);
511 /* Colon separating hour from minute... */
512 token = next_token(&val, cfile);
513 if (token != COLON) {
514 parse_warn("expected colon separating hour from minute.");
521 token = next_token(&val, cfile);
522 if (token != NUMBER) {
523 parse_warn("numeric minute expected.");
528 tm.tm_min = atoi(val);
530 /* Colon separating minute from second... */
531 token = next_token(&val, cfile);
532 if (token != COLON) {
533 parse_warn("expected colon separating hour from minute.");
540 token = next_token(&val, cfile);
541 if (token != NUMBER) {
542 parse_warn("numeric minute expected.");
547 tm.tm_sec = atoi(val);
550 /* XXX: We assume that mktime does not use tm_yday. */
553 /* Make sure the date ends in a semicolon... */
554 token = next_token(&val, cfile);
556 parse_warn("semicolon expected.");
561 /* Guess the time value... */
562 guess = ((((((365 * (tm.tm_year - 70) + /* Days in years since '70 */
563 (tm.tm_year - 69) / 4 + /* Leap days since '70 */
564 (tm.tm_mon /* Days in months this year */
565 ? months[tm.tm_mon - 1]
567 (tm.tm_mon > 1 && /* Leap day this year */
568 !((tm.tm_year - 72) & 3)) +
569 tm.tm_mday - 1) * 24) + /* Day of month */
571 tm.tm_min) * 60) + tm.tm_sec;
574 * This guess could be wrong because of leap seconds or other
575 * weirdness we don't know about that the system does. For
576 * now, we're just going to accept the guess, but at some point
577 * it might be nice to do a successive approximation here to get
578 * an exact value. Even if the error is small, if the server
579 * is restarted frequently (and thus the lease database is
580 * reread), the error could accumulate into something