1 /* $OpenBSD: parse.c,v 1.18 2007/01/08 13:34:38 krw Exp $ */
2 /* $DragonFly: src/sbin/dhclient/parse.c,v 1.1 2008/08/30 16:07:58 hasso Exp $ */
4 /* Common parser code for dhcpd and dhclient. */
7 * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of The Internet Software Consortium nor the names
20 * of its contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
24 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
31 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * This software has been written for the Internet Software Consortium
38 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
39 * Enterprises. To learn more about the Internet Software Consortium,
40 * see ``http://www.vix.com/isc''. To learn more about Vixie
41 * Enterprises, see ``http://www.vix.com''.
48 * Skip to the semicolon ending the current statement. If we encounter
49 * braces, the matching closing brace terminates the statement. If we
50 * encounter a right brace but haven't encountered a left brace, return
51 * leaving the brace in the token buffer for the caller. If we see a
52 * semicolon and haven't seen a left brace, return. This lets us skip
56 * statement foo bar { }
57 * statement foo bar { statement { } }
63 skip_to_semi(FILE *cfile)
70 token = peek_token(&val, cfile);
73 token = next_token(&val, cfile);
78 } else if (token == '{') {
80 } else if (token == ';' && !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 != TOK_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)) {
140 parse_ip_addr(FILE *cfile, struct iaddr *addr)
143 return (parse_numeric_aggregate(cfile, addr->iabuf, addr->len, '.',
148 * hardware-parameter :== HARDWARE ETHERNET csns SEMI
149 * csns :== NUMBER | csns COLON NUMBER
152 parse_hardware_param(FILE *cfile, struct hardware *hardware)
157 token = next_token(&val, cfile);
160 hardware->htype = HTYPE_ETHER;
164 hardware->htype = HTYPE_IEEE802;
168 hardware->htype = HTYPE_FDDI;
172 parse_warn("expecting a network hardware type");
177 if (parse_numeric_aggregate(cfile, hardware->haddr, hardware->hlen,
181 token = next_token(&val, cfile);
183 parse_warn("expecting semicolon.");
189 * lease-time :== NUMBER SEMI
192 parse_lease_time(FILE *cfile, time_t *timep)
197 token = next_token(&val, cfile);
198 if (token != TOK_NUMBER) {
199 parse_warn("Expecting numeric lease time");
203 convert_num((unsigned char *)timep, val, 10, 32);
204 /* Unswap the number - convert_num returns stuff in NBO. */
205 *timep = ntohl(*timep); /* XXX */
211 * Parse a sequence of numbers separated by the token specified in separator.
212 * Exactly max numbers are expected.
215 parse_numeric_aggregate(FILE *cfile, unsigned char *buf, int max, int separator,
221 if (buf == NULL || max == 0)
222 error("no space for numeric aggregate");
224 for (count = 0; count < max; count++, buf++) {
225 if (count && (peek_token(&val, cfile) == separator))
226 token = next_token(&val, cfile);
228 token = next_token(&val, cfile);
230 if (token == TOK_NUMBER || (base == 16 && token == TOK_NUMBER_OR_NAME))
231 /* XXX Need to check if conversion was successful. */
232 convert_num(buf, val, base, 8);
238 parse_warn("numeric aggregate too short.");
246 convert_num(unsigned char *buf, char *str, int base, int size)
248 int negative = 0, tval, max;
257 /* If base wasn't specified, figure it out from the data. */
263 } else if (isascii(ptr[1]) && isdigit(ptr[1])) {
274 /* XXX assumes ASCII... */
276 tval = tval - 'a' + 10;
277 else if (tval >= 'A')
278 tval = tval - 'A' + 10;
279 else if (tval >= '0')
282 warning("Bogus number: %s.", str);
286 warning("Bogus number: %s: digit %d not in base %d",
290 val = val * base + tval;
294 max = (1 << (size - 1));
296 max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
300 warning("value %s%o exceeds max (%d) for precision.",
301 negative ? "-" : "", val, max);
304 warning("value %s%x exceeds max (%d) for precision.",
305 negative ? "-" : "", val, max);
308 warning("value %s%u exceeds max (%d) for precision.",
309 negative ? "-" : "", val, max);
317 *buf = -(unsigned long)val;
320 putShort(buf, -(unsigned long)val);
323 putLong(buf, -(unsigned long)val);
326 warning("Unexpected integer size: %d", size);
332 *buf = (u_int8_t)val;
335 putUShort(buf, (u_int16_t)val);
341 warning("Unexpected integer size: %d", size);
347 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER
348 * NUMBER COLON NUMBER COLON NUMBER SEMI
350 * Dates are always in GMT; first number is day of week; next is
351 * year/month/day; next is hours:minutes:seconds on a 24-hour
355 parse_date(FILE *cfile)
357 static int months[11] = { 31, 59, 90, 120, 151, 181,
358 212, 243, 273, 304, 334 };
364 token = next_token(&val, cfile);
365 if (token != TOK_NUMBER) {
366 parse_warn("numeric day of week expected.");
371 tm.tm_wday = atoi(val);
374 token = next_token(&val, cfile);
375 if (token != TOK_NUMBER) {
376 parse_warn("numeric year expected.");
381 tm.tm_year = atoi(val);
382 if (tm.tm_year > 1900)
385 /* Slash separating year from month... */
386 token = next_token(&val, cfile);
388 parse_warn("expected slash separating year from month.");
395 token = next_token(&val, cfile);
396 if (token != TOK_NUMBER) {
397 parse_warn("numeric month expected.");
402 tm.tm_mon = atoi(val) - 1;
404 /* Slash separating month from day... */
405 token = next_token(&val, cfile);
407 parse_warn("expected slash separating month from day.");
414 token = next_token(&val, cfile);
415 if (token != TOK_NUMBER) {
416 parse_warn("numeric day of month expected.");
421 tm.tm_mday = atoi(val);
424 token = next_token(&val, cfile);
425 if (token != TOK_NUMBER) {
426 parse_warn("numeric hour expected.");
431 tm.tm_hour = atoi(val);
433 /* Colon separating hour from minute... */
434 token = next_token(&val, cfile);
436 parse_warn("expected colon separating hour from minute.");
443 token = next_token(&val, cfile);
444 if (token != TOK_NUMBER) {
445 parse_warn("numeric minute expected.");
450 tm.tm_min = atoi(val);
452 /* Colon separating minute from second... */
453 token = next_token(&val, cfile);
455 parse_warn("expected colon separating minute from second.");
462 token = next_token(&val, cfile);
463 if (token != TOK_NUMBER) {
464 parse_warn("numeric second expected.");
469 tm.tm_sec = atoi(val);
472 /* XXX: We assume that mktime does not use tm_yday. */
475 /* Make sure the date ends in a semicolon... */
476 token = next_token(&val, cfile);
478 parse_warn("semicolon expected.");
483 /* Guess the time value... */
484 guess = ((((((365 * (tm.tm_year - 70) + /* Days in years since '70 */
485 (tm.tm_year - 69) / 4 + /* Leap days since '70 */
486 (tm.tm_mon /* Days in months this year */
487 ? months[tm.tm_mon - 1] : 0) +
488 (tm.tm_mon > 1 && /* Leap day this year */
489 !((tm.tm_year - 72) & 3)) +
490 tm.tm_mday - 1) * 24) + /* Day of month */
491 tm.tm_hour) * 60) + tm.tm_min) * 60) + tm.tm_sec;
494 * This guess could be wrong because of leap seconds or other
495 * weirdness we don't know about that the system does. For
496 * now, we're just going to accept the guess, but at some point
497 * it might be nice to do a successive approximation here to get
498 * an exact value. Even if the error is small, if the server
499 * is restarted frequently (and thus the lease database is
500 * reread), the error could accumulate into something