1 /* $OpenBSD: parse.c,v 1.11 2004/05/05 23:07:47 deraadt Exp $ */
2
3 /* Common parser code for dhcpd and dhclient. */
4
5 /*-
6 * SPDX-License-Identifier: BSD-3-Clause
7 *
8 * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
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.
23 *
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
36 * SUCH DAMAGE.
37 *
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''.
43 */
44
45 #include <sys/cdefs.h>
46 #include <stdbool.h>
47
48 #include "dhcpd.h"
49 #include "dhctoken.h"
50
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
56 * over:
57 *
58 * statement;
59 * statement foo bar { }
60 * statement foo bar { statement { } }
61 * statement}
62 *
63 * ...et cetera.
64 */
65 void
skip_to_semi(FILE * cfile)66 skip_to_semi(FILE *cfile)
67 {
68 int brace_count = 0, token;
69 char *val;
70
71 do {
72 token = peek_token(&val, cfile);
73 if (token == RBRACE) {
74 if (brace_count) {
75 token = next_token(&val, cfile);
76 if (!--brace_count)
77 return;
78 } else
79 return;
80 } else if (token == LBRACE) {
81 brace_count++;
82 } else if (token == SEMI && !brace_count) {
83 token = next_token(&val, cfile);
84 return;
85 } else if (token == '\n') {
86 /*
87 * EOL only happens when parsing
88 * /etc/resolv.conf, and we treat it like a
89 * semicolon because the resolv.conf file is
90 * line-oriented.
91 */
92 token = next_token(&val, cfile);
93 return;
94 }
95 token = next_token(&val, cfile);
96 } while (token != EOF);
97 }
98
99 int
parse_semi(FILE * cfile)100 parse_semi(FILE *cfile)
101 {
102 int token;
103 char *val;
104
105 token = next_token(&val, cfile);
106 if (token != SEMI) {
107 parse_warn("semicolon expected.");
108 skip_to_semi(cfile);
109 return (0);
110 }
111 return (1);
112 }
113
114 /*
115 * string-parameter :== STRING SEMI
116 */
117 char *
parse_string(FILE * cfile)118 parse_string(FILE *cfile)
119 {
120 char *val, *s;
121 size_t valsize;
122 int token;
123
124 token = next_token(&val, cfile);
125 if (token != STRING) {
126 parse_warn("filename must be a string");
127 skip_to_semi(cfile);
128 return (NULL);
129 }
130 valsize = strlen(val) + 1;
131 s = malloc(valsize);
132 if (!s)
133 error("no memory for string %s.", val);
134 memcpy(s, val, valsize);
135
136 if (!parse_semi(cfile)) {
137 free(s);
138 return (NULL);
139 }
140 return (s);
141 }
142
143 int
parse_ip_addr(FILE * cfile,struct iaddr * addr)144 parse_ip_addr(FILE *cfile, struct iaddr *addr)
145 {
146 addr->len = 4;
147 if (parse_numeric_aggregate(cfile, addr->iabuf,
148 &addr->len, DOT, 10, 8))
149 return (1);
150 return (0);
151 }
152
153 /*
154 * hardware-parameter :== HARDWARE ETHERNET csns SEMI
155 * csns :== NUMBER | csns COLON NUMBER
156 */
157 void
parse_hardware_param(FILE * cfile,struct hardware * hardware)158 parse_hardware_param(FILE *cfile, struct hardware *hardware)
159 {
160 unsigned char *t;
161 int token;
162 size_t hlen;
163 char *val;
164
165 token = next_token(&val, cfile);
166 switch (token) {
167 case ETHERNET:
168 hardware->htype = HTYPE_ETHER;
169 break;
170 case TOKEN_RING:
171 hardware->htype = HTYPE_IEEE802;
172 break;
173 case FDDI:
174 hardware->htype = HTYPE_FDDI;
175 break;
176 default:
177 parse_warn("expecting a network hardware type");
178 skip_to_semi(cfile);
179 return;
180 }
181
182 /*
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.
190 */
191 hlen = 0;
192 t = parse_numeric_aggregate(cfile, NULL, &hlen, COLON, 16, 8);
193 if (!t)
194 return;
195 if (hlen > sizeof(hardware->haddr)) {
196 free(t);
197 parse_warn("hardware address too long");
198 } else {
199 hardware->hlen = hlen;
200 memcpy((unsigned char *)&hardware->haddr[0], t,
201 hardware->hlen);
202 if (hlen < sizeof(hardware->haddr))
203 memset(&hardware->haddr[hlen], 0,
204 sizeof(hardware->haddr) - hlen);
205 free(t);
206 }
207
208 token = next_token(&val, cfile);
209 if (token != SEMI) {
210 parse_warn("expecting semicolon.");
211 skip_to_semi(cfile);
212 }
213 }
214
215 /*
216 * lease-time :== NUMBER SEMI
217 */
218 void
parse_lease_time(FILE * cfile,time_t * timep)219 parse_lease_time(FILE *cfile, time_t *timep)
220 {
221 char *val;
222 int token;
223
224 token = next_token(&val, cfile);
225 if (token != NUMBER) {
226 parse_warn("Expecting numeric lease time");
227 skip_to_semi(cfile);
228 return;
229 }
230 convert_num((unsigned char *)timep, val, 10, 32);
231 /* Unswap the number - convert_num returns stuff in NBO. */
232 *timep = ntohl(*timep); /* XXX */
233
234 parse_semi(cfile);
235 }
236
237 /*
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
243 * tokenized.
244 */
245 unsigned char *
parse_numeric_aggregate(FILE * cfile,unsigned char * buf,size_t * max,int separator,unsigned base,int size)246 parse_numeric_aggregate(FILE *cfile, unsigned char *buf, size_t *max,
247 int separator, unsigned base, int size)
248 {
249 unsigned char *bufp = buf, *s = NULL;
250 int token;
251 char *val, *t;
252 size_t valsize, count = 0;
253 pair c = NULL;
254 unsigned char *lbufp = NULL;
255
256 if (!bufp && *max) {
257 lbufp = bufp = malloc(*max * size / 8);
258 if (!bufp)
259 error("can't allocate space for numeric aggregate");
260 } else
261 s = bufp;
262
263 do {
264 if (count) {
265 token = peek_token(&val, cfile);
266 if (token != separator) {
267 if (!*max)
268 break;
269 if (token != RBRACE && token != LBRACE)
270 token = next_token(&val, cfile);
271 parse_warn("too few numbers.");
272 if (token != SEMI)
273 skip_to_semi(cfile);
274 free(lbufp);
275 return (NULL);
276 }
277 token = next_token(&val, cfile);
278 }
279 token = next_token(&val, cfile);
280
281 if (token == EOF) {
282 parse_warn("unexpected end of file");
283 break;
284 }
285
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.");
290 skip_to_semi(cfile);
291 free(lbufp);
292 return (NULL);
293 }
294 /*
295 * If we can, convert the number now; otherwise, build a
296 * linked list of all the numbers.
297 */
298 if (s) {
299 convert_num(s, val, base, size);
300 s += size / 8;
301 } else {
302 valsize = strlen(val) + 1;
303 t = malloc(valsize);
304 if (!t)
305 error("no temp space for number.");
306 memcpy(t, val, valsize);
307 c = cons(t, c);
308 }
309 } while (++count != *max);
310
311 /* If we had to cons up a list, convert it now. */
312 if (c) {
313 free(lbufp);
314 bufp = malloc(count * size / 8);
315 if (!bufp)
316 error("can't allocate space for numeric aggregate.");
317 s = bufp + count - size / 8;
318 *max = count;
319 }
320 while (c) {
321 pair cdr = c->cdr;
322 convert_num(s, (char *)c->car, base, size);
323 s -= size / 8;
324 /* Free up temp space. */
325 free(c->car);
326 free(c);
327 c = cdr;
328 }
329 return (bufp);
330 }
331
332 void
convert_num(unsigned char * buf,char * str,unsigned base,int size)333 convert_num(unsigned char *buf, char *str, unsigned base, int size)
334 {
335 bool negative = false;
336 unsigned tval, max;
337 u_int32_t val = 0;
338 char *ptr = str;
339
340 if (*ptr == '-') {
341 negative = true;
342 ptr++;
343 }
344
345 /* If base wasn't specified, figure it out from the data. */
346 if (!base) {
347 if (ptr[0] == '0') {
348 if (ptr[1] == 'x') {
349 base = 16;
350 ptr += 2;
351 } else if (isascii(ptr[1]) && isdigit(ptr[1])) {
352 base = 8;
353 ptr += 1;
354 } else
355 base = 10;
356 } else
357 base = 10;
358 }
359
360 do {
361 tval = *ptr++;
362 /* XXX assumes ASCII... */
363 if (tval >= 'a')
364 tval = tval - 'a' + 10;
365 else if (tval >= 'A')
366 tval = tval - 'A' + 10;
367 else if (tval >= '0')
368 tval -= '0';
369 else {
370 warning("Bogus number: %s.", str);
371 break;
372 }
373 if (tval >= base) {
374 warning("Bogus number: %s: digit %d not in base %d",
375 str, tval, base);
376 break;
377 }
378 val = val * base + tval;
379 } while (*ptr);
380
381 if (negative)
382 max = (1 << (size - 1));
383 else
384 max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
385 if (val > max) {
386 switch (base) {
387 case 8:
388 warning("value %s%o exceeds max (%d) for precision.",
389 negative ? "-" : "", val, max);
390 break;
391 case 16:
392 warning("value %s%x exceeds max (%d) for precision.",
393 negative ? "-" : "", val, max);
394 break;
395 default:
396 warning("value %s%u exceeds max (%d) for precision.",
397 negative ? "-" : "", val, max);
398 break;
399 }
400 }
401
402 if (negative)
403 switch (size) {
404 case 8:
405 *buf = -(unsigned long)val;
406 break;
407 case 16:
408 putShort(buf, -(unsigned long)val);
409 break;
410 case 32:
411 putLong(buf, -(unsigned long)val);
412 break;
413 default:
414 warning("Unexpected integer size: %d", size);
415 break;
416 }
417 else
418 switch (size) {
419 case 8:
420 *buf = (u_int8_t)val;
421 break;
422 case 16:
423 putUShort(buf, (u_int16_t)val);
424 break;
425 case 32:
426 putULong(buf, val);
427 break;
428 default:
429 warning("Unexpected integer size: %d", size);
430 break;
431 }
432 }
433
434 /*
435 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER
436 * NUMBER COLON NUMBER COLON NUMBER SEMI
437 *
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
440 * clock.
441 */
442 time_t
parse_date(FILE * cfile)443 parse_date(FILE *cfile)
444 {
445 int token;
446 struct tm tm;
447 char *val;
448
449 /* Day of week... */
450 token = next_token(&val, cfile);
451 if (token != NUMBER) {
452 parse_warn("numeric day of week expected.");
453 if (token != SEMI)
454 skip_to_semi(cfile);
455 return (0);
456 }
457 tm.tm_wday = atoi(val);
458
459 /* Year... */
460 token = next_token(&val, cfile);
461 if (token != NUMBER) {
462 parse_warn("numeric year expected.");
463 if (token != SEMI)
464 skip_to_semi(cfile);
465 return (0);
466 }
467 tm.tm_year = atoi(val);
468 if (tm.tm_year > 1900)
469 tm.tm_year -= 1900;
470
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.");
475 if (token != SEMI)
476 skip_to_semi(cfile);
477 return (0);
478 }
479
480 /* Month... */
481 token = next_token(&val, cfile);
482 if (token != NUMBER) {
483 parse_warn("numeric month expected.");
484 if (token != SEMI)
485 skip_to_semi(cfile);
486 return (0);
487 }
488 tm.tm_mon = atoi(val) - 1;
489
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.");
494 if (token != SEMI)
495 skip_to_semi(cfile);
496 return (0);
497 }
498
499 /* Month... */
500 token = next_token(&val, cfile);
501 if (token != NUMBER) {
502 parse_warn("numeric day of month expected.");
503 if (token != SEMI)
504 skip_to_semi(cfile);
505 return (0);
506 }
507 tm.tm_mday = atoi(val);
508
509 /* Hour... */
510 token = next_token(&val, cfile);
511 if (token != NUMBER) {
512 parse_warn("numeric hour expected.");
513 if (token != SEMI)
514 skip_to_semi(cfile);
515 return (0);
516 }
517 tm.tm_hour = atoi(val);
518
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.");
523 if (token != SEMI)
524 skip_to_semi(cfile);
525 return (0);
526 }
527
528 /* Minute... */
529 token = next_token(&val, cfile);
530 if (token != NUMBER) {
531 parse_warn("numeric minute expected.");
532 if (token != SEMI)
533 skip_to_semi(cfile);
534 return (0);
535 }
536 tm.tm_min = atoi(val);
537
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.");
542 if (token != SEMI)
543 skip_to_semi(cfile);
544 return (0);
545 }
546
547 /* Minute... */
548 token = next_token(&val, cfile);
549 if (token != NUMBER) {
550 parse_warn("numeric minute expected.");
551 if (token != SEMI)
552 skip_to_semi(cfile);
553 return (0);
554 }
555 tm.tm_sec = atoi(val);
556 tm.tm_isdst = 0;
557
558 /* XXX: We assume that mktime does not use tm_yday. */
559 tm.tm_yday = 0;
560
561 /* Make sure the date ends in a semicolon... */
562 token = next_token(&val, cfile);
563 if (token != SEMI) {
564 parse_warn("semicolon expected.");
565 skip_to_semi(cfile);
566 return (0);
567 }
568
569 return (timegm(&tm));
570 }
571