xref: /openbsd-src/usr.sbin/dhcpd/parse.c (revision 0795b38919d5e1a9797bb5628fbd00cbe8824b7d)
1 /*	$OpenBSD: parse.c,v 1.7 2004/09/16 18:35:43 deraadt Exp $	*/
2 
3 /* Common parser code for dhcpd and dhclient. */
4 
5 /*
6  * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
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.
21  *
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
34  * SUCH DAMAGE.
35  *
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''.
41  */
42 
43 #include "dhcpd.h"
44 #include "dhctoken.h"
45 
46 /*
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
52  * over:
53  *
54  *	statement;
55  *	statement foo bar { }
56  *	statement foo bar { statement { } }
57  *	statement}
58  *
59  *	...et cetera.
60  */
61 void
62 skip_to_semi(FILE *cfile)
63 {
64 	int		 token;
65 	char		*val;
66 	int		 brace_count = 0;
67 
68 	do {
69 		token = peek_token(&val, cfile);
70 		if (token == RBRACE) {
71 			if (brace_count) {
72 				token = next_token(&val, cfile);
73 				if (!--brace_count)
74 					return;
75 			} else
76 				return;
77 		} else if (token == LBRACE) {
78 			brace_count++;
79 		} else if (token == SEMI && !brace_count) {
80 			token = next_token(&val, cfile);
81 			return;
82 		} else if (token == '\n') {
83 			/*
84 			 * EOL only happens when parsing
85 			 * /etc/resolv.conf, and we treat it like a
86 			 * semicolon because the resolv.conf file is
87 			 * line-oriented.
88 			 */
89 			token = next_token(&val, cfile);
90 			return;
91 		}
92 		token = next_token(&val, cfile);
93 	} while (token != EOF);
94 }
95 
96 int
97 parse_semi(FILE *cfile)
98 {
99 	int token;
100 	char *val;
101 
102 	token = next_token(&val, cfile);
103 	if (token != SEMI) {
104 		parse_warn("semicolon expected.");
105 		skip_to_semi(cfile);
106 		return (0);
107 	}
108 	return (1);
109 }
110 
111 /*
112  * string-parameter :== STRING SEMI
113  */
114 char *
115 parse_string(FILE *cfile)
116 {
117 	char *val, *s;
118 	int token;
119 
120 	token = next_token(&val, cfile);
121 	if (token != STRING) {
122 		parse_warn("filename must be a string");
123 		skip_to_semi(cfile);
124 		return (NULL);
125 	}
126 	s = malloc(strlen(val) + 1);
127 	if (!s)
128 		error("no memory for string %s.", val);
129 	strlcpy(s, val, strlen(val) + 1);
130 
131 	if (!parse_semi(cfile))
132 		return (NULL);
133 	return (s);
134 }
135 
136 /*
137  * hostname :== identifier | hostname DOT identifier
138  */
139 char *
140 parse_host_name(FILE *cfile)
141 {
142 	char *val, *s, *t;
143 	int token, len = 0;
144 	pair c = NULL;
145 
146 	/* Read a dotted hostname... */
147 	do {
148 		/* Read a token, which should be an identifier. */
149 		token = next_token(&val, cfile);
150 		if (!is_identifier(token) && token != NUMBER) {
151 			parse_warn("expecting an identifier in hostname");
152 			skip_to_semi(cfile);
153 			return (NULL);
154 		}
155 		/* Store this identifier... */
156 		if (!(s = malloc(strlen(val) + 1)))
157 			error("can't allocate temp space for hostname.");
158 		strlcpy(s, val, strlen(val) + 1);
159 		c = cons((caddr_t) s, c);
160 		len += strlen(s) + 1;
161 		/*
162 		 * Look for a dot; if it's there, keep going, otherwise
163 		 * we're done.
164 		 */
165 		token = peek_token(&val, cfile);
166 		if (token == DOT)
167 			token = next_token(&val, cfile);
168 	} while (token == DOT);
169 
170 	/* Assemble the hostname together into a string. */
171 	if (!(s = malloc(len)))
172 		error("can't allocate space for hostname.");
173 	t = s + len;
174 	*--t = '\0';
175 	while (c) {
176 		pair cdr = c->cdr;
177 		int l = strlen((char *)c->car);
178 
179 		t -= l;
180 		memcpy(t, (char *)c->car, l);
181 		/* Free up temp space. */
182 		free(c->car);
183 		free(c);
184 		c = cdr;
185 		if (t != s)
186 			*--t = '.';
187 	}
188 	return (s);
189 }
190 
191 /*
192  * hardware-parameter :== HARDWARE ETHERNET csns SEMI
193  * csns :== NUMBER | csns COLON NUMBER
194  */
195 void
196 parse_hardware_param(FILE *cfile, struct hardware *hardware)
197 {
198 	char *val;
199 	int token, hlen;
200 	unsigned char *t;
201 
202 	token = next_token(&val, cfile);
203 	switch (token) {
204 	case ETHERNET:
205 		hardware->htype = HTYPE_ETHER;
206 		break;
207 	case TOKEN_RING:
208 		hardware->htype = HTYPE_IEEE802;
209 		break;
210 	case FDDI:
211 		hardware->htype = HTYPE_FDDI;
212 		break;
213 	default:
214 		parse_warn("expecting a network hardware type");
215 		skip_to_semi(cfile);
216 		return;
217 	}
218 
219 	/*
220 	 * Parse the hardware address information.   Technically, it
221 	 * would make a lot of sense to restrict the length of the data
222 	 * we'll accept here to the length of a particular hardware
223 	 * address type.   Unfortunately, there are some broken clients
224 	 * out there that put bogus data in the chaddr buffer, and we
225 	 * accept that data in the lease file rather than simply failing
226 	 * on such clients.   Yuck.
227 	 */
228 	hlen = 0;
229 	t = parse_numeric_aggregate(cfile, NULL, &hlen, COLON, 16, 8);
230 	if (!t)
231 		return;
232 	if (hlen > sizeof(hardware->haddr)) {
233 		free(t);
234 		parse_warn("hardware address too long");
235 	} else {
236 		hardware->hlen = hlen;
237 		memcpy((unsigned char *)&hardware->haddr[0], t, hardware->hlen);
238 		if (hlen < sizeof(hardware->haddr))
239 			memset(&hardware->haddr[hlen], 0,
240 			    sizeof(hardware->haddr) - hlen);
241 		free(t);
242 	}
243 
244 	token = next_token(&val, cfile);
245 	if (token != SEMI) {
246 		parse_warn("expecting semicolon.");
247 		skip_to_semi(cfile);
248 	}
249 }
250 
251 /*
252  * lease-time :== NUMBER SEMI
253  */
254 void
255 parse_lease_time(FILE *cfile, time_t *timep)
256 {
257 	char *val;
258 	int token;
259 
260 	token = next_token(&val, cfile);
261 	if (token != NUMBER) {
262 		parse_warn("Expecting numeric lease time");
263 		skip_to_semi(cfile);
264 		return;
265 	}
266 	convert_num((unsigned char *)timep, val, 10, 32);
267 	/* Unswap the number - convert_num returns stuff in NBO. */
268 	*timep = ntohl(*timep);	/* XXX */
269 
270 	parse_semi(cfile);
271 }
272 
273 /*
274  * No BNF for numeric aggregates - that's defined by the caller.  What
275  * this function does is to parse a sequence of numbers separated by the
276  * token specified in separator.  If max is zero, any number of numbers
277  * will be parsed; otherwise, exactly max numbers are expected.  Base
278  * and size tell us how to internalize the numbers once they've been
279  * tokenized.
280  */
281 unsigned char *
282 parse_numeric_aggregate(FILE *cfile, unsigned char *buf, int *max,
283     int separator, int base, int size)
284 {
285 	char *val, *t;
286 	int token, count = 0;
287 	unsigned char *bufp = buf, *s = NULL;
288 	pair c = NULL;
289 
290 	if (!bufp && *max) {
291 		bufp = malloc(*max * size / 8);
292 		if (!bufp)
293 			error("can't allocate space for numeric aggregate");
294 	} else
295 		s = bufp;
296 
297 	do {
298 		if (count) {
299 			token = peek_token(&val, cfile);
300 			if (token != separator) {
301 				if (!*max)
302 					break;
303 				if (token != RBRACE && token != LBRACE)
304 					token = next_token(&val, cfile);
305 				parse_warn("too few numbers.");
306 				if (token != SEMI)
307 					skip_to_semi(cfile);
308 				return (NULL);
309 			}
310 			token = next_token(&val, cfile);
311 		}
312 		token = next_token(&val, cfile);
313 
314 		if (token == EOF) {
315 			parse_warn("unexpected end of file");
316 			break;
317 		}
318 		/* Allow NUMBER_OR_NAME if base is 16. */
319 		if (token != NUMBER &&
320 		    (base != 16 || token != NUMBER_OR_NAME)) {
321 			parse_warn("expecting numeric value.");
322 			skip_to_semi(cfile);
323 			return (NULL);
324 		}
325 		/*
326 		 * If we can, convert the number now; otherwise, build a
327 		 * linked list of all the numbers.
328 		 */
329 		if (s) {
330 			convert_num(s, val, base, size);
331 			s += size / 8;
332 		} else {
333 			t = malloc(strlen(val) + 1);
334 			if (!t)
335 				error("no temp space for number.");
336 			strlcpy(t, val, strlen(val) + 1);
337 			c = cons(t, c);
338 		}
339 	} while (++count != *max);
340 
341 	/* If we had to cons up a list, convert it now. */
342 	if (c) {
343 		bufp = malloc(count * size / 8);
344 		if (!bufp)
345 			error("can't allocate space for numeric aggregate.");
346 		s = bufp + count - size / 8;
347 		*max = count;
348 	}
349 	while (c) {
350 		pair		cdr = c->cdr;
351 		convert_num(s, (char *)c->car, base, size);
352 		s -= size / 8;
353 		/* Free up temp space. */
354 		free(c->car);
355 		free(c);
356 		c = cdr;
357 	}
358 	return (bufp);
359 }
360 
361 void
362 convert_num(unsigned char *buf, char *str, int base, int size)
363 {
364 	int negative = 0, tval, max;
365 	char *ptr = str;
366 	u_int32_t val = 0;
367 
368 	if (*ptr == '-') {
369 		negative = 1;
370 		ptr++;
371 	}
372 	/* If base wasn't specified, figure it out from the data. */
373 	if (!base) {
374 		if (ptr[0] == '0') {
375 			if (ptr[1] == 'x') {
376 				base = 16;
377 				ptr += 2;
378 			} else if (isascii(ptr[1]) && isdigit(ptr[1])) {
379 				base = 8;
380 				ptr += 1;
381 			} else
382 				base = 10;
383 		} else
384 			base = 10;
385 	}
386 	do {
387 		tval = *ptr++;
388 		/* XXX assumes ASCII... */
389 		if (tval >= 'a')
390 			tval = tval - 'a' + 10;
391 		else if (tval >= 'A')
392 			tval = tval - 'A' + 10;
393 		else if (tval >= '0')
394 			tval -= '0';
395 		else {
396 			warning("Bogus number: %s.", str);
397 			break;
398 		}
399 		if (tval >= base) {
400 			warning("Bogus number: %s: digit %d not in base %d",
401 			    str, tval, base);
402 			break;
403 		}
404 		val = val * base + tval;
405 	} while (*ptr);
406 
407 	if (negative)
408 		max = (1 << (size - 1));
409 	else
410 		max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
411 	if (val > max) {
412 		switch (base) {
413 		case 8:
414 			warning("value %s%o exceeds max (%d) for precision.",
415 			    negative ? "-" : "", val, max);
416 			break;
417 		case 16:
418 			warning("value %s%x exceeds max (%d) for precision.",
419 			    negative ? "-" : "", val, max);
420 			break;
421 		default:
422 			warning("value %s%u exceeds max (%d) for precision.",
423 			    negative ? "-" : "", val, max);
424 			break;
425 		}
426 	}
427 	if (negative) {
428 		switch (size) {
429 		case 8:
430 			*buf = -(unsigned long)val;
431 			break;
432 		case 16:
433 			putShort(buf, -(unsigned long)val);
434 			break;
435 		case 32:
436 			putLong(buf, -(unsigned long)val);
437 			break;
438 		default:
439 			warning("Unexpected integer size: %d", size);
440 			break;
441 		}
442 	} else {
443 		switch (size) {
444 		case 8:
445 			*buf = (u_int8_t)val;
446 			break;
447 		case 16:
448 			putUShort(buf, (u_int16_t)val);
449 			break;
450 		case 32:
451 			putULong(buf, val);
452 			break;
453 		default:
454 			warning("Unexpected integer size: %d", size);
455 			break;
456 		}
457 	}
458 }
459 
460 /*
461  * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER
462  *		NUMBER COLON NUMBER COLON NUMBER SEMI
463  *
464  * Dates are always in GMT; first number is day of week; next is
465  * year/month/day; next is hours:minutes:seconds on a 24-hour
466  * clock.
467  */
468 time_t
469 parse_date(FILE * cfile)
470 {
471 	struct tm tm;
472 	int guess, token;
473 	char *val;
474 	static int months[11] = {31, 59, 90, 120, 151, 181,
475 	    212, 243, 273, 304, 334};
476 
477 	/* Day of week... */
478 	token = next_token(&val, cfile);
479 	if (token != NUMBER) {
480 		parse_warn("numeric day of week expected.");
481 		if (token != SEMI)
482 			skip_to_semi(cfile);
483 		return (NULL);
484 	}
485 	tm.tm_wday = atoi(val);
486 
487 	/* Year... */
488 	token = next_token(&val, cfile);
489 	if (token != NUMBER) {
490 		parse_warn("numeric year expected.");
491 		if (token != SEMI)
492 			skip_to_semi(cfile);
493 		return (NULL);
494 	}
495 	tm.tm_year = atoi(val);
496 	if (tm.tm_year > 1900)
497 		tm.tm_year -= 1900;
498 
499 	/* Slash separating year from month... */
500 	token = next_token(&val, cfile);
501 	if (token != SLASH) {
502 		parse_warn("expected slash separating year from month.");
503 		if (token != SEMI)
504 			skip_to_semi(cfile);
505 		return (NULL);
506 	}
507 	/* Month... */
508 	token = next_token(&val, cfile);
509 	if (token != NUMBER) {
510 		parse_warn("numeric month expected.");
511 		if (token != SEMI)
512 			skip_to_semi(cfile);
513 		return (NULL);
514 	}
515 	tm.tm_mon = atoi(val) - 1;
516 
517 	/* Slash separating month from day... */
518 	token = next_token(&val, cfile);
519 	if (token != SLASH) {
520 		parse_warn("expected slash separating month from day.");
521 		if (token != SEMI)
522 			skip_to_semi(cfile);
523 		return (NULL);
524 	}
525 	/* Month... */
526 	token = next_token(&val, cfile);
527 	if (token != NUMBER) {
528 		parse_warn("numeric day of month expected.");
529 		if (token != SEMI)
530 			skip_to_semi(cfile);
531 		return (NULL);
532 	}
533 	tm.tm_mday = atoi(val);
534 
535 	/* Hour... */
536 	token = next_token(&val, cfile);
537 	if (token != NUMBER) {
538 		parse_warn("numeric hour expected.");
539 		if (token != SEMI)
540 			skip_to_semi(cfile);
541 		return (NULL);
542 	}
543 	tm.tm_hour = atoi(val);
544 
545 	/* Colon separating hour from minute... */
546 	token = next_token(&val, cfile);
547 	if (token != COLON) {
548 		parse_warn("expected colon separating hour from minute.");
549 		if (token != SEMI)
550 			skip_to_semi(cfile);
551 		return (NULL);
552 	}
553 	/* Minute... */
554 	token = next_token(&val, cfile);
555 	if (token != NUMBER) {
556 		parse_warn("numeric minute expected.");
557 		if (token != SEMI)
558 			skip_to_semi(cfile);
559 		return (NULL);
560 	}
561 	tm.tm_min = atoi(val);
562 
563 	/* Colon separating minute from second... */
564 	token = next_token(&val, cfile);
565 	if (token != COLON) {
566 		parse_warn("expected colon separating hour from minute.");
567 		if (token != SEMI)
568 			skip_to_semi(cfile);
569 		return (NULL);
570 	}
571 	/* Minute... */
572 	token = next_token(&val, cfile);
573 	if (token != NUMBER) {
574 		parse_warn("numeric minute expected.");
575 		if (token != SEMI)
576 			skip_to_semi(cfile);
577 		return (NULL);
578 	}
579 	tm.tm_sec = atoi(val);
580 	tm.tm_isdst = 0;
581 
582 	/* XXX: We assume that mktime does not use tm_yday. */
583 	tm.tm_yday = 0;
584 
585 	/* Make sure the date ends in a semicolon... */
586 	token = next_token(&val, cfile);
587 	if (token != SEMI) {
588 		parse_warn("semicolon expected.");
589 		skip_to_semi(cfile);
590 		return (NULL);
591 	}
592 	/* Guess the time value... */
593 	guess = ((((((365 * (tm.tm_year - 70) +	/* Days in years since '70 */
594 	    (tm.tm_year - 69) / 4 +	/* Leap days since '70 */
595 	    (tm.tm_mon			/* Days in months this year */
596 	    ? months[tm.tm_mon - 1] : 0) +
597 	    (tm.tm_mon > 1 &&		/* Leap day this year */
598 	    !((tm.tm_year - 72) & 3)) +
599 	    tm.tm_mday - 1) * 24) +	/* Day of month */
600 	    tm.tm_hour) * 60) + tm.tm_min) * 60) + tm.tm_sec;
601 
602 	/*
603 	 * This guess could be wrong because of leap seconds or other
604 	 * weirdness we don't know about that the system does.   For
605 	 * now, we're just going to accept the guess, but at some point
606 	 * it might be nice to do a successive approximation here to get
607 	 * an exact value.   Even if the error is small, if the server
608 	 * is restarted frequently (and thus the lease database is
609 	 * reread), the error could accumulate into something
610 	 * significant.
611 	 */
612 	return (guess);
613 }
614