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>
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)) {
144 parse_ip_addr(FILE *cfile, struct iaddr *addr)
147 if (parse_numeric_aggregate(cfile, addr->iabuf,
148 &addr->len, DOT, 10, 8))
154 * hardware-parameter :== HARDWARE ETHERNET csns SEMI
155 * csns :== NUMBER | csns COLON NUMBER
158 parse_hardware_param(FILE *cfile, struct hardware *hardware)
165 token = next_token(&val, cfile);
168 hardware->htype = HTYPE_ETHER;
171 hardware->htype = HTYPE_IEEE802;
174 hardware->htype = HTYPE_FDDI;
177 parse_warn("expecting a network hardware type");
183 * Parse the hardware address information. Technically, it
184 * would make a lot of sense to restrict the length of the data
185 * we'll accept here to the length of a particular hardware
186 * address type. Unfortunately, there are some broken clients
187 * out there that put bogus data in the chaddr buffer, and we
188 * accept that data in the lease file rather than simply failing
189 * on such clients. Yuck.
192 t = parse_numeric_aggregate(cfile, NULL, &hlen, COLON, 16, 8);
195 if (hlen > sizeof(hardware->haddr)) {
197 parse_warn("hardware address too long");
199 hardware->hlen = hlen;
200 memcpy((unsigned char *)&hardware->haddr[0], t,
202 if (hlen < sizeof(hardware->haddr))
203 memset(&hardware->haddr[hlen], 0,
204 sizeof(hardware->haddr) - hlen);
208 token = next_token(&val, cfile);
210 parse_warn("expecting semicolon.");
216 * lease-time :== NUMBER SEMI
219 parse_lease_time(FILE *cfile, time_t *timep)
224 token = next_token(&val, cfile);
225 if (token != NUMBER) {
226 parse_warn("Expecting numeric lease time");
230 convert_num((unsigned char *)timep, val, 10, 32);
231 /* Unswap the number - convert_num returns stuff in NBO. */
232 *timep = ntohl(*timep); /* XXX */
238 * No BNF for numeric aggregates - that's defined by the caller. What
239 * this function does is to parse a sequence of numbers separated by the
240 * token specified in separator. If max is zero, any number of numbers
241 * will be parsed; otherwise, exactly max numbers are expected. Base
242 * and size tell us how to internalize the numbers once they've been
246 parse_numeric_aggregate(FILE *cfile, unsigned char *buf, size_t *max,
247 int separator, unsigned base, int size)
249 unsigned char *bufp = buf, *s = NULL;
252 size_t valsize, count = 0;
254 unsigned char *lbufp = NULL;
257 lbufp = bufp = malloc(*max * size / 8);
259 error("can't allocate space for numeric aggregate");
265 token = peek_token(&val, cfile);
266 if (token != separator) {
269 if (token != RBRACE && token != LBRACE)
270 token = next_token(&val, cfile);
271 parse_warn("too few numbers.");
277 token = next_token(&val, cfile);
279 token = next_token(&val, cfile);
282 parse_warn("unexpected end of file");
286 /* Allow NUMBER_OR_NAME if base is 16. */
287 if (token != NUMBER &&
288 (base != 16 || token != NUMBER_OR_NAME)) {
289 parse_warn("expecting numeric value.");
295 * If we can, convert the number now; otherwise, build a
296 * linked list of all the numbers.
299 convert_num(s, val, base, size);
302 valsize = strlen(val) + 1;
305 error("no temp space for number.");
306 memcpy(t, val, valsize);
309 } while (++count != *max);
311 /* If we had to cons up a list, convert it now. */
314 bufp = malloc(count * size / 8);
316 error("can't allocate space for numeric aggregate.");
317 s = bufp + count - size / 8;
322 convert_num(s, (char *)c->car, base, size);
324 /* Free up temp space. */
333 convert_num(unsigned char *buf, char *str, unsigned base, int size)
335 bool negative = false;
345 /* If base wasn't specified, figure it out from the data. */
351 } else if (isascii(ptr[1]) && isdigit(ptr[1])) {
362 /* XXX assumes ASCII... */
364 tval = tval - 'a' + 10;
365 else if (tval >= 'A')
366 tval = tval - 'A' + 10;
367 else if (tval >= '0')
370 warning("Bogus number: %s.", str);
374 warning("Bogus number: %s: digit %d not in base %d",
378 val = val * base + tval;
382 max = (1 << (size - 1));
384 max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
388 warning("value %s%o exceeds max (%d) for precision.",
389 negative ? "-" : "", val, max);
392 warning("value %s%x exceeds max (%d) for precision.",
393 negative ? "-" : "", val, max);
396 warning("value %s%u exceeds max (%d) for precision.",
397 negative ? "-" : "", val, max);
405 *buf = -(unsigned long)val;
408 putShort(buf, -(unsigned long)val);
411 putLong(buf, -(unsigned long)val);
414 warning("Unexpected integer size: %d", size);
420 *buf = (u_int8_t)val;
423 putUShort(buf, (u_int16_t)val);
429 warning("Unexpected integer size: %d", size);
435 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER
436 * NUMBER COLON NUMBER COLON NUMBER SEMI
438 * Dates are always in GMT; first number is day of week; next is
439 * year/month/day; next is hours:minutes:seconds on a 24-hour
443 parse_date(FILE *cfile)
450 token = next_token(&val, cfile);
451 if (token != NUMBER) {
452 parse_warn("numeric day of week expected.");
457 tm.tm_wday = atoi(val);
460 token = next_token(&val, cfile);
461 if (token != NUMBER) {
462 parse_warn("numeric year expected.");
467 tm.tm_year = atoi(val);
468 if (tm.tm_year > 1900)
471 /* Slash separating year from month... */
472 token = next_token(&val, cfile);
473 if (token != SLASH) {
474 parse_warn("expected slash separating year from month.");
481 token = next_token(&val, cfile);
482 if (token != NUMBER) {
483 parse_warn("numeric month expected.");
488 tm.tm_mon = atoi(val) - 1;
490 /* Slash separating month from day... */
491 token = next_token(&val, cfile);
492 if (token != SLASH) {
493 parse_warn("expected slash separating month from day.");
500 token = next_token(&val, cfile);
501 if (token != NUMBER) {
502 parse_warn("numeric day of month expected.");
507 tm.tm_mday = atoi(val);
510 token = next_token(&val, cfile);
511 if (token != NUMBER) {
512 parse_warn("numeric hour expected.");
517 tm.tm_hour = atoi(val);
519 /* Colon separating hour from minute... */
520 token = next_token(&val, cfile);
521 if (token != COLON) {
522 parse_warn("expected colon separating hour from minute.");
529 token = next_token(&val, cfile);
530 if (token != NUMBER) {
531 parse_warn("numeric minute expected.");
536 tm.tm_min = atoi(val);
538 /* Colon separating minute from second... */
539 token = next_token(&val, cfile);
540 if (token != COLON) {
541 parse_warn("expected colon separating hour from minute.");
548 token = next_token(&val, cfile);
549 if (token != NUMBER) {
550 parse_warn("numeric minute expected.");
555 tm.tm_sec = atoi(val);
558 /* XXX: We assume that mktime does not use tm_yday. */
561 /* Make sure the date ends in a semicolon... */
562 token = next_token(&val, cfile);
564 parse_warn("semicolon expected.");
569 return (timegm(&tm));