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