xref: /freebsd/sbin/dhclient/parse.c (revision 8a0a413e)
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 "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
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
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 *
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
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
158 parse_hardware_param(FILE *cfile, struct hardware *hardware)
159 {
160 	unsigned char *t;
161 	int token, hlen;
162 	char *val;
163 
164 	token = next_token(&val, cfile);
165 	switch (token) {
166 	case ETHERNET:
167 		hardware->htype = HTYPE_ETHER;
168 		break;
169 	case TOKEN_RING:
170 		hardware->htype = HTYPE_IEEE802;
171 		break;
172 	case FDDI:
173 		hardware->htype = HTYPE_FDDI;
174 		break;
175 	default:
176 		parse_warn("expecting a network hardware type");
177 		skip_to_semi(cfile);
178 		return;
179 	}
180 
181 	/*
182 	 * Parse the hardware address information.   Technically, it
183 	 * would make a lot of sense to restrict the length of the data
184 	 * we'll accept here to the length of a particular hardware
185 	 * address type.   Unfortunately, there are some broken clients
186 	 * out there that put bogus data in the chaddr buffer, and we
187 	 * accept that data in the lease file rather than simply failing
188 	 * on such clients.   Yuck.
189 	 */
190 	hlen = 0;
191 	t = parse_numeric_aggregate(cfile, NULL, &hlen, COLON, 16, 8);
192 	if (!t)
193 		return;
194 	if (hlen > sizeof(hardware->haddr)) {
195 		free(t);
196 		parse_warn("hardware address too long");
197 	} else {
198 		hardware->hlen = hlen;
199 		memcpy((unsigned char *)&hardware->haddr[0], t,
200 		    hardware->hlen);
201 		if (hlen < sizeof(hardware->haddr))
202 			memset(&hardware->haddr[hlen], 0,
203 			    sizeof(hardware->haddr) - hlen);
204 		free(t);
205 	}
206 
207 	token = next_token(&val, cfile);
208 	if (token != SEMI) {
209 		parse_warn("expecting semicolon.");
210 		skip_to_semi(cfile);
211 	}
212 }
213 
214 /*
215  * lease-time :== NUMBER SEMI
216  */
217 void
218 parse_lease_time(FILE *cfile, time_t *timep)
219 {
220 	char *val;
221 	int token;
222 
223 	token = next_token(&val, cfile);
224 	if (token != NUMBER) {
225 		parse_warn("Expecting numeric lease time");
226 		skip_to_semi(cfile);
227 		return;
228 	}
229 	convert_num((unsigned char *)timep, val, 10, 32);
230 	/* Unswap the number - convert_num returns stuff in NBO. */
231 	*timep = ntohl(*timep); /* XXX */
232 
233 	parse_semi(cfile);
234 }
235 
236 /*
237  * No BNF for numeric aggregates - that's defined by the caller.  What
238  * this function does is to parse a sequence of numbers separated by the
239  * token specified in separator.  If max is zero, any number of numbers
240  * will be parsed; otherwise, exactly max numbers are expected.  Base
241  * and size tell us how to internalize the numbers once they've been
242  * tokenized.
243  */
244 unsigned char *
245 parse_numeric_aggregate(FILE *cfile, unsigned char *buf, int *max,
246     int separator, int base, int size)
247 {
248 	unsigned char *bufp = buf, *s = NULL;
249 	int token, count = 0;
250 	char *val, *t;
251 	size_t valsize;
252 	pair c = NULL;
253 	unsigned char *lbufp = NULL;
254 
255 	if (!bufp && *max) {
256 		lbufp = bufp = malloc(*max * size / 8);
257 		if (!bufp)
258 			error("can't allocate space for numeric aggregate");
259 	} else
260 		s = bufp;
261 
262 	do {
263 		if (count) {
264 			token = peek_token(&val, cfile);
265 			if (token != separator) {
266 				if (!*max)
267 					break;
268 				if (token != RBRACE && token != LBRACE)
269 					token = next_token(&val, cfile);
270 				parse_warn("too few numbers.");
271 				if (token != SEMI)
272 					skip_to_semi(cfile);
273 				free(lbufp);
274 				return (NULL);
275 			}
276 			token = next_token(&val, cfile);
277 		}
278 		token = next_token(&val, cfile);
279 
280 		if (token == EOF) {
281 			parse_warn("unexpected end of file");
282 			break;
283 		}
284 
285 		/* Allow NUMBER_OR_NAME if base is 16. */
286 		if (token != NUMBER &&
287 		    (base != 16 || token != NUMBER_OR_NAME)) {
288 			parse_warn("expecting numeric value.");
289 			skip_to_semi(cfile);
290 			free(lbufp);
291 			return (NULL);
292 		}
293 		/*
294 		 * If we can, convert the number now; otherwise, build a
295 		 * linked list of all the numbers.
296 		 */
297 		if (s) {
298 			convert_num(s, val, base, size);
299 			s += size / 8;
300 		} else {
301 			valsize = strlen(val) + 1;
302 			t = malloc(valsize);
303 			if (!t)
304 				error("no temp space for number.");
305 			memcpy(t, val, valsize);
306 			c = cons(t, c);
307 		}
308 	} while (++count != *max);
309 
310 	/* If we had to cons up a list, convert it now. */
311 	if (c) {
312 		free(lbufp);
313 		bufp = malloc(count * size / 8);
314 		if (!bufp)
315 			error("can't allocate space for numeric aggregate.");
316 		s = bufp + count - size / 8;
317 		*max = count;
318 	}
319 	while (c) {
320 		pair cdr = c->cdr;
321 		convert_num(s, (char *)c->car, base, size);
322 		s -= size / 8;
323 		/* Free up temp space. */
324 		free(c->car);
325 		free(c);
326 		c = cdr;
327 	}
328 	return (bufp);
329 }
330 
331 void
332 convert_num(unsigned char *buf, char *str, int base, int size)
333 {
334 	int negative = 0, tval, max;
335 	u_int32_t val = 0;
336 	char *ptr = str;
337 
338 	if (*ptr == '-') {
339 		negative = 1;
340 		ptr++;
341 	}
342 
343 	/* If base wasn't specified, figure it out from the data. */
344 	if (!base) {
345 		if (ptr[0] == '0') {
346 			if (ptr[1] == 'x') {
347 				base = 16;
348 				ptr += 2;
349 			} else if (isascii(ptr[1]) && isdigit(ptr[1])) {
350 				base = 8;
351 				ptr += 1;
352 			} else
353 				base = 10;
354 		} else
355 			base = 10;
356 	}
357 
358 	do {
359 		tval = *ptr++;
360 		/* XXX assumes ASCII... */
361 		if (tval >= 'a')
362 			tval = tval - 'a' + 10;
363 		else if (tval >= 'A')
364 			tval = tval - 'A' + 10;
365 		else if (tval >= '0')
366 			tval -= '0';
367 		else {
368 			warning("Bogus number: %s.", str);
369 			break;
370 		}
371 		if (tval >= base) {
372 			warning("Bogus number: %s: digit %d not in base %d",
373 			    str, tval, base);
374 			break;
375 		}
376 		val = val * base + tval;
377 	} while (*ptr);
378 
379 	if (negative)
380 		max = (1 << (size - 1));
381 	else
382 		max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
383 	if (val > max) {
384 		switch (base) {
385 		case 8:
386 			warning("value %s%o exceeds max (%d) for precision.",
387 			    negative ? "-" : "", val, max);
388 			break;
389 		case 16:
390 			warning("value %s%x exceeds max (%d) for precision.",
391 			    negative ? "-" : "", val, max);
392 			break;
393 		default:
394 			warning("value %s%u exceeds max (%d) for precision.",
395 			    negative ? "-" : "", val, max);
396 			break;
397 		}
398 	}
399 
400 	if (negative)
401 		switch (size) {
402 		case 8:
403 			*buf = -(unsigned long)val;
404 			break;
405 		case 16:
406 			putShort(buf, -(unsigned long)val);
407 			break;
408 		case 32:
409 			putLong(buf, -(unsigned long)val);
410 			break;
411 		default:
412 			warning("Unexpected integer size: %d", size);
413 			break;
414 		}
415 	else
416 		switch (size) {
417 		case 8:
418 			*buf = (u_int8_t)val;
419 			break;
420 		case 16:
421 			putUShort(buf, (u_int16_t)val);
422 			break;
423 		case 32:
424 			putULong(buf, val);
425 			break;
426 		default:
427 			warning("Unexpected integer size: %d", size);
428 			break;
429 		}
430 }
431 
432 /*
433  * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER
434  *		NUMBER COLON NUMBER COLON NUMBER SEMI
435  *
436  * Dates are always in GMT; first number is day of week; next is
437  * year/month/day; next is hours:minutes:seconds on a 24-hour
438  * clock.
439  */
440 time_t
441 parse_date(FILE *cfile)
442 {
443 	static int months[11] = { 31, 59, 90, 120, 151, 181,
444 	    212, 243, 273, 304, 334 };
445 	int guess, 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 	/* Guess the time value... */
570 	guess = ((((((365 * (tm.tm_year - 70) +	/* Days in years since '70 */
571 		    (tm.tm_year - 69) / 4 +	/* Leap days since '70 */
572 		    (tm.tm_mon			/* Days in months this year */
573 		    ? months[tm.tm_mon - 1]
574 		    : 0) +
575 		    (tm.tm_mon > 1 &&		/* Leap day this year */
576 		    !((tm.tm_year - 72) & 3)) +
577 		    tm.tm_mday - 1) * 24) +	/* Day of month */
578 		    tm.tm_hour) * 60) +
579 		    tm.tm_min) * 60) + tm.tm_sec;
580 
581 	/*
582 	 * This guess could be wrong because of leap seconds or other
583 	 * weirdness we don't know about that the system does.   For
584 	 * now, we're just going to accept the guess, but at some point
585 	 * it might be nice to do a successive approximation here to get
586 	 * an exact value.   Even if the error is small, if the server
587 	 * is restarted frequently (and thus the lease database is
588 	 * reread), the error could accumulate into something
589 	 * significant.
590 	 */
591 	return (guess);
592 }
593