1 /* $OpenBSD: parse.c,v 1.11 2004/05/05 23:07:47 deraadt Exp $ */
3 /* Common parser code for dhcpd and dhclient. */
6 * SPDX-License-Identifier: BSD-3-Clause
8 * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of The Internet Software Consortium nor the names
21 * of its contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
25 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
32 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
35 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * This software has been written for the Internet Software Consortium
39 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
40 * Enterprises. To learn more about the Internet Software Consortium,
41 * see ``http://www.vix.com/isc''. To learn more about Vixie
42 * Enterprises, see ``http://www.vix.com''.
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
53 /* Skip to the semicolon ending the current statement. If we encounter
54 * braces, the matching closing brace terminates the statement. If we
55 * encounter a right brace but haven't encountered a left brace, return
56 * leaving the brace in the token buffer for the caller. If we see a
57 * semicolon and haven't seen a left brace, return. This lets us skip
61 * statement foo bar { }
62 * statement foo bar { statement { } }
68 skip_to_semi(FILE *cfile)
70 int brace_count = 0, token;
74 token = peek_token(&val, cfile);
75 if (token == RBRACE) {
77 token = next_token(&val, cfile);
82 } else if (token == LBRACE) {
84 } else if (token == SEMI && !brace_count) {
85 token = next_token(&val, cfile);
87 } else if (token == '\n') {
89 * EOL only happens when parsing
90 * /etc/resolv.conf, and we treat it like a
91 * semicolon because the resolv.conf file is
94 token = next_token(&val, cfile);
97 token = next_token(&val, cfile);
98 } while (token != EOF);
102 parse_semi(FILE *cfile)
107 token = next_token(&val, cfile);
109 parse_warn("semicolon expected.");
117 * string-parameter :== STRING SEMI
120 parse_string(FILE *cfile)
126 token = next_token(&val, cfile);
127 if (token != STRING) {
128 parse_warn("filename must be a string");
132 valsize = strlen(val) + 1;
135 error("no memory for string %s.", val);
136 memcpy(s, val, valsize);
138 if (!parse_semi(cfile)) {
146 parse_ip_addr(FILE *cfile, struct iaddr *addr)
149 if (parse_numeric_aggregate(cfile, addr->iabuf,
150 &addr->len, DOT, 10, 8))
156 * hardware-parameter :== HARDWARE ETHERNET csns SEMI
157 * csns :== NUMBER | csns COLON NUMBER
160 parse_hardware_param(FILE *cfile, struct hardware *hardware)
167 token = next_token(&val, cfile);
170 hardware->htype = HTYPE_ETHER;
173 hardware->htype = HTYPE_IEEE802;
176 hardware->htype = HTYPE_FDDI;
179 parse_warn("expecting a network hardware type");
185 * Parse the hardware address information. Technically, it
186 * would make a lot of sense to restrict the length of the data
187 * we'll accept here to the length of a particular hardware
188 * address type. Unfortunately, there are some broken clients
189 * out there that put bogus data in the chaddr buffer, and we
190 * accept that data in the lease file rather than simply failing
191 * on such clients. Yuck.
194 t = parse_numeric_aggregate(cfile, NULL, &hlen, COLON, 16, 8);
197 if (hlen > sizeof(hardware->haddr)) {
199 parse_warn("hardware address too long");
201 hardware->hlen = hlen;
202 memcpy((unsigned char *)&hardware->haddr[0], t,
204 if (hlen < sizeof(hardware->haddr))
205 memset(&hardware->haddr[hlen], 0,
206 sizeof(hardware->haddr) - hlen);
210 token = next_token(&val, cfile);
212 parse_warn("expecting semicolon.");
218 * lease-time :== NUMBER SEMI
221 parse_lease_time(FILE *cfile, time_t *timep)
226 token = next_token(&val, cfile);
227 if (token != NUMBER) {
228 parse_warn("Expecting numeric lease time");
232 convert_num((unsigned char *)timep, val, 10, 32);
233 /* Unswap the number - convert_num returns stuff in NBO. */
234 *timep = ntohl(*timep); /* XXX */
240 * No BNF for numeric aggregates - that's defined by the caller. What
241 * this function does is to parse a sequence of numbers separated by the
242 * token specified in separator. If max is zero, any number of numbers
243 * will be parsed; otherwise, exactly max numbers are expected. Base
244 * and size tell us how to internalize the numbers once they've been
248 parse_numeric_aggregate(FILE *cfile, unsigned char *buf, size_t *max,
249 int separator, unsigned base, int size)
251 unsigned char *bufp = buf, *s = NULL;
254 size_t valsize, count = 0;
256 unsigned char *lbufp = NULL;
259 lbufp = bufp = malloc(*max * size / 8);
261 error("can't allocate space for numeric aggregate");
267 token = peek_token(&val, cfile);
268 if (token != separator) {
271 if (token != RBRACE && token != LBRACE)
272 token = next_token(&val, cfile);
273 parse_warn("too few numbers.");
279 token = next_token(&val, cfile);
281 token = next_token(&val, cfile);
284 parse_warn("unexpected end of file");
288 /* Allow NUMBER_OR_NAME if base is 16. */
289 if (token != NUMBER &&
290 (base != 16 || token != NUMBER_OR_NAME)) {
291 parse_warn("expecting numeric value.");
297 * If we can, convert the number now; otherwise, build a
298 * linked list of all the numbers.
301 convert_num(s, val, base, size);
304 valsize = strlen(val) + 1;
307 error("no temp space for number.");
308 memcpy(t, val, valsize);
311 } while (++count != *max);
313 /* If we had to cons up a list, convert it now. */
316 bufp = malloc(count * size / 8);
318 error("can't allocate space for numeric aggregate.");
319 s = bufp + count - size / 8;
324 convert_num(s, (char *)c->car, base, size);
326 /* Free up temp space. */
335 convert_num(unsigned char *buf, char *str, unsigned base, int size)
337 bool negative = false;
347 /* If base wasn't specified, figure it out from the data. */
353 } else if (isascii(ptr[1]) && isdigit(ptr[1])) {
364 /* XXX assumes ASCII... */
366 tval = tval - 'a' + 10;
367 else if (tval >= 'A')
368 tval = tval - 'A' + 10;
369 else if (tval >= '0')
372 warning("Bogus number: %s.", str);
376 warning("Bogus number: %s: digit %d not in base %d",
380 val = val * base + tval;
384 max = (1 << (size - 1));
386 max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
390 warning("value %s%o exceeds max (%d) for precision.",
391 negative ? "-" : "", val, max);
394 warning("value %s%x exceeds max (%d) for precision.",
395 negative ? "-" : "", val, max);
398 warning("value %s%u exceeds max (%d) for precision.",
399 negative ? "-" : "", val, max);
407 *buf = -(unsigned long)val;
410 putShort(buf, -(unsigned long)val);
413 putLong(buf, -(unsigned long)val);
416 warning("Unexpected integer size: %d", size);
422 *buf = (u_int8_t)val;
425 putUShort(buf, (u_int16_t)val);
431 warning("Unexpected integer size: %d", size);
437 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER
438 * NUMBER COLON NUMBER COLON NUMBER SEMI
440 * Dates are always in GMT; first number is day of week; next is
441 * year/month/day; next is hours:minutes:seconds on a 24-hour
445 parse_date(FILE *cfile)
447 static int months[11] = { 31, 59, 90, 120, 151, 181,
448 212, 243, 273, 304, 334 };
454 token = next_token(&val, cfile);
455 if (token != NUMBER) {
456 parse_warn("numeric day of week expected.");
461 tm.tm_wday = atoi(val);
464 token = next_token(&val, cfile);
465 if (token != NUMBER) {
466 parse_warn("numeric year expected.");
471 tm.tm_year = atoi(val);
472 if (tm.tm_year > 1900)
475 /* Slash separating year from month... */
476 token = next_token(&val, cfile);
477 if (token != SLASH) {
478 parse_warn("expected slash separating year from month.");
485 token = next_token(&val, cfile);
486 if (token != NUMBER) {
487 parse_warn("numeric month expected.");
492 tm.tm_mon = atoi(val) - 1;
494 /* Slash separating month from day... */
495 token = next_token(&val, cfile);
496 if (token != SLASH) {
497 parse_warn("expected slash separating month from day.");
504 token = next_token(&val, cfile);
505 if (token != NUMBER) {
506 parse_warn("numeric day of month expected.");
511 tm.tm_mday = atoi(val);
514 token = next_token(&val, cfile);
515 if (token != NUMBER) {
516 parse_warn("numeric hour expected.");
521 tm.tm_hour = atoi(val);
523 /* Colon separating hour from minute... */
524 token = next_token(&val, cfile);
525 if (token != COLON) {
526 parse_warn("expected colon separating hour from minute.");
533 token = next_token(&val, cfile);
534 if (token != NUMBER) {
535 parse_warn("numeric minute expected.");
540 tm.tm_min = atoi(val);
542 /* Colon separating minute from second... */
543 token = next_token(&val, cfile);
544 if (token != COLON) {
545 parse_warn("expected colon separating hour from minute.");
552 token = next_token(&val, cfile);
553 if (token != NUMBER) {
554 parse_warn("numeric minute expected.");
559 tm.tm_sec = atoi(val);
562 /* XXX: We assume that mktime does not use tm_yday. */
565 /* Make sure the date ends in a semicolon... */
566 token = next_token(&val, cfile);
568 parse_warn("semicolon expected.");
573 /* Guess the time value... */
574 guess = ((((((365 * (tm.tm_year - 70) + /* Days in years since '70 */
575 (tm.tm_year - 69) / 4 + /* Leap days since '70 */
576 (tm.tm_mon /* Days in months this year */
577 ? months[tm.tm_mon - 1]
579 (tm.tm_mon > 1 && /* Leap day this year */
580 !((tm.tm_year - 72) & 3)) +
581 tm.tm_mday - 1) * 24) + /* Day of month */
583 tm.tm_min) * 60) + tm.tm_sec;
586 * This guess could be wrong because of leap seconds or other
587 * weirdness we don't know about that the system does. For
588 * now, we're just going to accept the guess, but at some point
589 * it might be nice to do a successive approximation here to get
590 * an exact value. Even if the error is small, if the server
591 * is restarted frequently (and thus the lease database is
592 * reread), the error could accumulate into something