1 /* $OpenBSD: src/sbin/dhclient/parse.c,v 1.19 2010/06/26 21:14:10 krw 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''.
47 * Skip to the semicolon ending the current statement. If we encounter
48 * braces, the matching closing brace terminates the statement. If we
49 * encounter a right brace but haven't encountered a left brace, return
50 * leaving the brace in the token buffer for the caller. If we see a
51 * semicolon and haven't seen a left brace, return. This lets us skip
55 * statement foo bar { }
56 * statement foo bar { statement { } }
62 skip_to_semi(FILE *cfile
)
69 token
= peek_token(&val
, cfile
);
72 token
= next_token(&val
, cfile
);
77 } else if (token
== '{') {
79 } else if (token
== ';' && !brace_count
) {
80 token
= next_token(&val
, cfile
);
82 } else if (token
== '\n') {
84 * EOL only happens when parsing
85 * /etc/resolv.conf, and we treat it like a
86 * semicolon because the resolv.conf file is
89 token
= next_token(&val
, cfile
);
92 token
= next_token(&val
, cfile
);
93 } while (token
!= EOF
);
97 parse_semi(FILE *cfile
)
102 token
= next_token(&val
, cfile
);
104 parse_warn("semicolon expected.");
112 * string-parameter :== STRING SEMI
115 parse_string(FILE *cfile
)
120 token
= next_token(&val
, cfile
);
121 if (token
!= TOK_STRING
) {
122 parse_warn("filename must be a string");
128 error("no memory for string %s.", val
);
130 if (!parse_semi(cfile
)) {
138 parse_ip_addr(FILE *cfile
, struct iaddr
*addr
)
141 return (parse_numeric_aggregate(cfile
, addr
->iabuf
, addr
->len
, '.',
146 * hardware-parameter :== HARDWARE ETHERNET csns SEMI
147 * csns :== NUMBER | csns COLON NUMBER
150 parse_hardware_param(FILE *cfile
, struct hardware
*hardware
)
155 token
= next_token(&val
, cfile
);
158 hardware
->htype
= HTYPE_ETHER
;
162 hardware
->htype
= HTYPE_IEEE802
;
166 hardware
->htype
= HTYPE_FDDI
;
170 parse_warn("expecting a network hardware type");
175 if (parse_numeric_aggregate(cfile
, hardware
->haddr
, hardware
->hlen
,
179 token
= next_token(&val
, cfile
);
181 parse_warn("expecting semicolon.");
187 * lease-time :== NUMBER SEMI
190 parse_lease_time(FILE *cfile
, time_t *timep
)
195 token
= next_token(&val
, cfile
);
196 if (token
!= TOK_NUMBER
) {
197 parse_warn("Expecting numeric lease time");
201 convert_num((unsigned char *)timep
, val
, 10, 32);
202 /* Unswap the number - convert_num returns stuff in NBO. */
203 *timep
= ntohl(*timep
); /* XXX */
209 * Parse a sequence of numbers separated by the token specified in separator.
210 * Exactly max numbers are expected.
213 parse_numeric_aggregate(FILE *cfile
, unsigned char *buf
, int max
, int separator
,
219 if (buf
== NULL
|| max
== 0)
220 error("no space for numeric aggregate");
222 for (count
= 0; count
< max
; count
++, buf
++) {
223 if (count
&& (peek_token(&val
, cfile
) == separator
))
224 token
= next_token(&val
, cfile
);
226 token
= next_token(&val
, cfile
);
228 if (token
== TOK_NUMBER
|| (base
== 16 && token
== TOK_NUMBER_OR_NAME
))
229 /* XXX Need to check if conversion was successful. */
230 convert_num(buf
, val
, base
, 8);
236 parse_warn("numeric aggregate too short.");
244 convert_num(unsigned char *buf
, char *str
, int base
, int size
)
246 int negative
= 0, tval
, max
;
255 /* If base wasn't specified, figure it out from the data. */
261 } else if (isascii(ptr
[1]) && isdigit(ptr
[1])) {
272 /* XXX assumes ASCII... */
274 tval
= tval
- 'a' + 10;
275 else if (tval
>= 'A')
276 tval
= tval
- 'A' + 10;
277 else if (tval
>= '0')
280 warning("Bogus number: %s.", str
);
284 warning("Bogus number: %s: digit %d not in base %d",
288 val
= val
* base
+ tval
;
292 max
= (1 << (size
- 1));
294 max
= (1 << (size
- 1)) + ((1 << (size
- 1)) - 1);
298 warning("value %s%o exceeds max (%d) for precision.",
299 negative
? "-" : "", val
, max
);
302 warning("value %s%x exceeds max (%d) for precision.",
303 negative
? "-" : "", val
, max
);
306 warning("value %s%u exceeds max (%d) for precision.",
307 negative
? "-" : "", val
, max
);
315 *buf
= -(unsigned long)val
;
318 putShort(buf
, -(unsigned long)val
);
321 putLong(buf
, -(unsigned long)val
);
324 warning("Unexpected integer size: %d", size
);
330 *buf
= (u_int8_t
)val
;
333 putUShort(buf
, (u_int16_t
)val
);
339 warning("Unexpected integer size: %d", size
);
345 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER
346 * NUMBER COLON NUMBER COLON NUMBER SEMI
348 * Dates are always in GMT; first number is day of week; next is
349 * year/month/day; next is hours:minutes:seconds on a 24-hour
353 parse_date(FILE *cfile
)
355 static int months
[11] = { 31, 59, 90, 120, 151, 181,
356 212, 243, 273, 304, 334 };
362 token
= next_token(&val
, cfile
);
363 if (token
!= TOK_NUMBER
) {
364 parse_warn("numeric day of week expected.");
369 tm
.tm_wday
= atoi(val
);
372 token
= next_token(&val
, cfile
);
373 if (token
!= TOK_NUMBER
) {
374 parse_warn("numeric year expected.");
379 tm
.tm_year
= atoi(val
);
380 if (tm
.tm_year
> 1900)
383 /* Slash separating year from month... */
384 token
= next_token(&val
, cfile
);
386 parse_warn("expected slash separating year from month.");
393 token
= next_token(&val
, cfile
);
394 if (token
!= TOK_NUMBER
) {
395 parse_warn("numeric month expected.");
400 tm
.tm_mon
= atoi(val
) - 1;
402 /* Slash separating month from day... */
403 token
= next_token(&val
, cfile
);
405 parse_warn("expected slash separating month from day.");
412 token
= next_token(&val
, cfile
);
413 if (token
!= TOK_NUMBER
) {
414 parse_warn("numeric day of month expected.");
419 tm
.tm_mday
= atoi(val
);
422 token
= next_token(&val
, cfile
);
423 if (token
!= TOK_NUMBER
) {
424 parse_warn("numeric hour expected.");
429 tm
.tm_hour
= atoi(val
);
431 /* Colon separating hour from minute... */
432 token
= next_token(&val
, cfile
);
434 parse_warn("expected colon separating hour from minute.");
441 token
= next_token(&val
, cfile
);
442 if (token
!= TOK_NUMBER
) {
443 parse_warn("numeric minute expected.");
448 tm
.tm_min
= atoi(val
);
450 /* Colon separating minute from second... */
451 token
= next_token(&val
, cfile
);
453 parse_warn("expected colon separating minute from second.");
460 token
= next_token(&val
, cfile
);
461 if (token
!= TOK_NUMBER
) {
462 parse_warn("numeric second expected.");
467 tm
.tm_sec
= atoi(val
);
470 /* XXX: We assume that mktime does not use tm_yday. */
473 /* Make sure the date ends in a semicolon... */
474 token
= next_token(&val
, cfile
);
476 parse_warn("semicolon expected.");
481 /* Guess the time value... */
482 guess
= ((((((365 * (tm
.tm_year
- 70) + /* Days in years since '70 */
483 (tm
.tm_year
- 69) / 4 + /* Leap days since '70 */
484 (tm
.tm_mon
/* Days in months this year */
485 ? months
[tm
.tm_mon
- 1] : 0) +
486 (tm
.tm_mon
> 1 && /* Leap day this year */
487 !((tm
.tm_year
- 72) & 3)) +
488 tm
.tm_mday
- 1) * 24) + /* Day of month */
489 tm
.tm_hour
) * 60) + tm
.tm_min
) * 60) + tm
.tm_sec
;
492 * This guess could be wrong because of leap seconds or other
493 * weirdness we don't know about that the system does. For
494 * now, we're just going to accept the guess, but at some point
495 * it might be nice to do a successive approximation here to get
496 * an exact value. Even if the error is small, if the server
497 * is restarted frequently (and thus the lease database is
498 * reread), the error could accumulate into something