1 /**	$MirOS: src/usr.sbin/dhcpd/parse.c,v 1.3 2006/09/20 20:03:33 tg Exp $ */
2 /*	$OpenBSD: parse.c,v 1.8 2006/04/18 19:18:32 deraadt Exp $	*/
3 
4 /* Common parser code for dhcpd and dhclient. */
5 
6 /*
7  * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of The Internet Software Consortium nor the names
20  *    of its contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
24  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
28  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
31  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * This software has been written for the Internet Software Consortium
38  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
39  * Enterprises.  To learn more about the Internet Software Consortium,
40  * see ``http://www.vix.com/isc''.  To learn more about Vixie
41  * Enterprises, see ``http://www.vix.com''.
42  */
43 
44 #include "dhcpd.h"
45 #include "dhctoken.h"
46 
47 __RCSID("$MirOS: src/usr.sbin/dhcpd/parse.c,v 1.3 2006/09/20 20:03:33 tg Exp $");
48 
49 /*
50  * Skip to the semicolon ending the current statement.   If we encounter
51  * braces, the matching closing brace terminates the statement.   If we
52  * encounter a right brace but haven't encountered a left brace, return
53  * leaving the brace in the token buffer for the caller.   If we see a
54  * semicolon and haven't seen a left brace, return.   This lets us skip
55  * over:
56  *
57  *	statement;
58  *	statement foo bar { }
59  *	statement foo bar { statement { } }
60  *	statement}
61  *
62  *	...et cetera.
63  */
64 void
skip_to_semi(FILE * cfile)65 skip_to_semi(FILE *cfile)
66 {
67 	int		 token;
68 	char		*val;
69 	int		 brace_count = 0;
70 
71 	do {
72 		token = peek_token(&val, cfile);
73 		if (token == '}') {
74 			if (brace_count) {
75 				token = next_token(&val, cfile);
76 				if (!--brace_count)
77 					return;
78 			} else
79 				return;
80 		} else if (token == '{') {
81 			brace_count++;
82 		} else if (token == ';' && !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 != ';') {
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 	int token;
122 
123 	token = next_token(&val, cfile);
124 	if (token != TOK_STRING) {
125 		parse_warn("filename must be a string");
126 		skip_to_semi(cfile);
127 		return 0;
128 	}
129 	s = malloc(strlen(val) + 1);
130 	if (!s)
131 		error("no memory for string %s.", val);
132 	strlcpy(s, val, strlen(val) + 1);
133 
134 	if (!parse_semi(cfile))
135 		return 0;
136 	return (s);
137 }
138 
139 /*
140  * hostname :== identifier | hostname DOT identifier
141  */
142 char *
parse_host_name(FILE * cfile)143 parse_host_name(FILE *cfile)
144 {
145 	char *val, *s, *t;
146 	int token, len = 0;
147 	pair c = NULL;
148 
149 	/* Read a dotted hostname... */
150 	do {
151 		/* Read a token, which should be an identifier. */
152 		token = next_token(&val, cfile);
153 		if (!is_identifier(token) && token != TOK_NUMBER) {
154 			parse_warn("expecting an identifier in hostname");
155 			skip_to_semi(cfile);
156 			return 0;
157 		}
158 		/* Store this identifier... */
159 		if (!(s = malloc(strlen(val) + 1)))
160 			error("can't allocate temp space for hostname.");
161 		strlcpy(s, val, strlen(val) + 1);
162 		c = cons((caddr_t) s, c);
163 		len += strlen(s) + 1;
164 		/*
165 		 * Look for a dot; if it's there, keep going, otherwise
166 		 * we're done.
167 		 */
168 		token = peek_token(&val, cfile);
169 		if (token == '.')
170 			token = next_token(&val, cfile);
171 	} while (token == '.');
172 
173 	/* Assemble the hostname together into a string. */
174 	if (!(s = malloc(len)))
175 		error("can't allocate space for hostname.");
176 	t = s + len;
177 	*--t = '\0';
178 	while (c) {
179 		pair cdr = c->cdr;
180 		int l = strlen((char *)c->car);
181 
182 		t -= l;
183 		memcpy(t, (char *)c->car, l);
184 		/* Free up temp space. */
185 		free(c->car);
186 		free(c);
187 		c = cdr;
188 		if (t != s)
189 			*--t = '.';
190 	}
191 	return (s);
192 }
193 
194 /*
195  * hardware-parameter :== HARDWARE ETHERNET csns SEMI
196  * csns :== NUMBER | csns COLON NUMBER
197  */
198 void
parse_hardware_param(FILE * cfile,struct hardware * hardware)199 parse_hardware_param(FILE *cfile, struct hardware *hardware)
200 {
201 	char *val;
202 	int token, hlen;
203 	unsigned char *t;
204 
205 	token = next_token(&val, cfile);
206 	switch (token) {
207 	case TOK_ETHERNET:
208 		hardware->htype = HTYPE_ETHER;
209 		break;
210 	case TOK_TOKEN_RING:
211 		hardware->htype = HTYPE_IEEE802;
212 		break;
213 	case TOK_FDDI:
214 		hardware->htype = HTYPE_FDDI;
215 		break;
216 	default:
217 		parse_warn("expecting a network hardware type");
218 		skip_to_semi(cfile);
219 		return;
220 	}
221 
222 	/*
223 	 * Parse the hardware address information.   Technically, it
224 	 * would make a lot of sense to restrict the length of the data
225 	 * we'll accept here to the length of a particular hardware
226 	 * address type.   Unfortunately, there are some broken clients
227 	 * out there that put bogus data in the chaddr buffer, and we
228 	 * accept that data in the lease file rather than simply failing
229 	 * on such clients.   Yuck.
230 	 */
231 	hlen = 0;
232 	t = parse_numeric_aggregate(cfile, NULL, &hlen, ':', 16, 8);
233 	if (!t)
234 		return;
235 	if (hlen > sizeof(hardware->haddr)) {
236 		free(t);
237 		parse_warn("hardware address too long");
238 	} else {
239 		hardware->hlen = hlen;
240 		memcpy((unsigned char *)&hardware->haddr[0], t, hardware->hlen);
241 		if (hlen < sizeof(hardware->haddr))
242 			memset(&hardware->haddr[hlen], 0,
243 			    sizeof(hardware->haddr) - hlen);
244 		free(t);
245 	}
246 
247 	token = next_token(&val, cfile);
248 	if (token != ';') {
249 		parse_warn("expecting semicolon.");
250 		skip_to_semi(cfile);
251 	}
252 }
253 
254 /*
255  * lease-time :== NUMBER SEMI
256  */
257 void
parse_lease_time(FILE * cfile,time_t * timep)258 parse_lease_time(FILE *cfile, time_t *timep)
259 {
260 	char *val;
261 	int token;
262 	uint32_t tmp;
263 
264 	token = next_token(&val, cfile);
265 	if (token != TOK_NUMBER) {
266 		parse_warn("Expecting numeric lease time");
267 		skip_to_semi(cfile);
268 		return;
269 	}
270 	convert_num((unsigned char *)&tmp, val, 10, 32);
271 	/* Unswap the number - convert_num returns stuff in NBO. */
272 	*timep = ntohl(tmp);
273 
274 	parse_semi(cfile);
275 }
276 
277 /*
278  * No BNF for numeric aggregates - that's defined by the caller.  What
279  * this function does is to parse a sequence of numbers separated by the
280  * token specified in separator.  If max is zero, any number of numbers
281  * will be parsed; otherwise, exactly max numbers are expected.  Base
282  * and size tell us how to internalize the numbers once they've been
283  * tokenized.
284  */
285 unsigned char *
parse_numeric_aggregate(FILE * cfile,unsigned char * buf,int * max,int separator,int base,int size)286 parse_numeric_aggregate(FILE *cfile, unsigned char *buf, int *max,
287     int separator, int base, int size)
288 {
289 	char *val, *t;
290 	int token, count = 0;
291 	unsigned char *bufp = buf, *s = NULL;
292 	pair c = NULL;
293 
294 	if (!bufp && *max) {
295 		bufp = malloc(*max * size / 8);
296 		if (!bufp)
297 			error("can't allocate space for numeric aggregate");
298 	} else
299 		s = bufp;
300 
301 	do {
302 		if (count) {
303 			token = peek_token(&val, cfile);
304 			if (token != separator) {
305 				if (!*max)
306 					break;
307 				if (token != '{' && token != '}')
308 					token = next_token(&val, cfile);
309 				parse_warn("too few numbers.");
310 				if (token != ';')
311 					skip_to_semi(cfile);
312 				return 0;
313 			}
314 			token = next_token(&val, cfile);
315 		}
316 		token = next_token(&val, cfile);
317 
318 		if (token == EOF) {
319 			parse_warn("unexpected end of file");
320 			break;
321 		}
322 		/* Allow NUMBER_OR_NAME if base is 16. */
323 		if (token != TOK_NUMBER &&
324 		    (base != 16 || token != TOK_NUMBER_OR_NAME)) {
325 			parse_warn("expecting numeric value.");
326 			skip_to_semi(cfile);
327 			return 0;
328 		}
329 		/*
330 		 * If we can, convert the number now; otherwise, build a
331 		 * linked list of all the numbers.
332 		 */
333 		if (s) {
334 			convert_num(s, val, base, size);
335 			s += size / 8;
336 		} else {
337 			t = malloc(strlen(val) + 1);
338 			if (!t)
339 				error("no temp space for number.");
340 			strlcpy(t, val, strlen(val) + 1);
341 			c = cons(t, c);
342 		}
343 	} while (++count != *max);
344 
345 	/* If we had to cons up a list, convert it now. */
346 	if (c) {
347 		bufp = malloc(count * size / 8);
348 		if (!bufp)
349 			error("can't allocate space for numeric aggregate.");
350 		s = bufp + count - size / 8;
351 		*max = count;
352 	}
353 	while (c) {
354 		pair		cdr = c->cdr;
355 		convert_num(s, (char *)c->car, base, size);
356 		s -= size / 8;
357 		/* Free up temp space. */
358 		free(c->car);
359 		free(c);
360 		c = cdr;
361 	}
362 	return (bufp);
363 }
364 
365 void
convert_num(unsigned char * buf,char * str,int base,int size)366 convert_num(unsigned char *buf, char *str, int base, int size)
367 {
368 	int negative = 0, tval, max;
369 	char *ptr = str;
370 	u_int32_t val = 0;
371 
372 	if (*ptr == '-') {
373 		negative = 1;
374 		ptr++;
375 	}
376 	/* If base wasn't specified, figure it out from the data. */
377 	if (!base) {
378 		if (ptr[0] == '0') {
379 			if (ptr[1] == 'x') {
380 				base = 16;
381 				ptr += 2;
382 			} else if (isascii(ptr[1]) && isdigit(ptr[1])) {
383 				base = 8;
384 				ptr += 1;
385 			} else
386 				base = 10;
387 		} else
388 			base = 10;
389 	}
390 	do {
391 		tval = *ptr++;
392 		/* XXX assumes ASCII... */
393 		if (tval >= 'a')
394 			tval = tval - 'a' + 10;
395 		else if (tval >= 'A')
396 			tval = tval - 'A' + 10;
397 		else if (tval >= '0')
398 			tval -= '0';
399 		else {
400 			warning("Bogus number: %s.", str);
401 			break;
402 		}
403 		if (tval >= base) {
404 			warning("Bogus number: %s: digit %d not in base %d",
405 			    str, tval, base);
406 			break;
407 		}
408 		val = val * base + tval;
409 	} while (*ptr);
410 
411 	if (negative)
412 		max = (1 << (size - 1));
413 	else
414 		max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
415 	if (val > max) {
416 		switch (base) {
417 		case 8:
418 			warning("value %s%o exceeds max (%d) for precision.",
419 			    negative ? "-" : "", val, max);
420 			break;
421 		case 16:
422 			warning("value %s%x exceeds max (%d) for precision.",
423 			    negative ? "-" : "", val, max);
424 			break;
425 		default:
426 			warning("value %s%u exceeds max (%d) for precision.",
427 			    negative ? "-" : "", val, max);
428 			break;
429 		}
430 	}
431 	if (negative) {
432 		switch (size) {
433 		case 8:
434 			*buf = -(unsigned long)val;
435 			break;
436 		case 16:
437 			putShort(buf, -(unsigned long)val);
438 			break;
439 		case 32:
440 			putLong(buf, -(unsigned long)val);
441 			break;
442 		default:
443 			warning("Unexpected integer size: %d", size);
444 			break;
445 		}
446 	} else {
447 		switch (size) {
448 		case 8:
449 			*buf = (u_int8_t)val;
450 			break;
451 		case 16:
452 			putUShort(buf, (u_int16_t)val);
453 			break;
454 		case 32:
455 			putULong(buf, val);
456 			break;
457 		default:
458 			warning("Unexpected integer size: %d", size);
459 			break;
460 		}
461 	}
462 }
463 
464 /*
465  * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER
466  *		NUMBER COLON NUMBER COLON NUMBER SEMI
467  *
468  * Dates are always in GMT; first number is day of week; next is
469  * year/month/day; next is hours:minutes:seconds on a 24-hour
470  * clock.
471  */
472 time_t
parse_date(FILE * cfile)473 parse_date(FILE * cfile)
474 {
475 	struct tm tm;
476 	int guess, token;
477 	char *val;
478 	static int months[11] = {31, 59, 90, 120, 151, 181,
479 	    212, 243, 273, 304, 334};
480 
481 	/* Day of week... */
482 	token = next_token(&val, cfile);
483 	if (token != TOK_NUMBER) {
484 		parse_warn("numeric day of week expected.");
485 		if (token != ';')
486 			skip_to_semi(cfile);
487 		return 0;
488 	}
489 	tm.tm_wday = atoi(val);
490 
491 	/* Year... */
492 	token = next_token(&val, cfile);
493 	if (token != TOK_NUMBER) {
494 		parse_warn("numeric year expected.");
495 		if (token != ';')
496 			skip_to_semi(cfile);
497 		return 0;
498 	}
499 	tm.tm_year = atoi(val);
500 	if (tm.tm_year > 1900)
501 		tm.tm_year -= 1900;
502 
503 	/* Slash separating year from month... */
504 	token = next_token(&val, cfile);
505 	if (token != '/') {
506 		parse_warn("expected slash separating year from month.");
507 		if (token != ';')
508 			skip_to_semi(cfile);
509 		return 0;
510 	}
511 	/* Month... */
512 	token = next_token(&val, cfile);
513 	if (token != TOK_NUMBER) {
514 		parse_warn("numeric month expected.");
515 		if (token != ';')
516 			skip_to_semi(cfile);
517 		return 0;
518 	}
519 	tm.tm_mon = atoi(val) - 1;
520 
521 	/* Slash separating month from day... */
522 	token = next_token(&val, cfile);
523 	if (token != '/') {
524 		parse_warn("expected slash separating month from day.");
525 		if (token != ';')
526 			skip_to_semi(cfile);
527 		return 0;
528 	}
529 	/* Month... */
530 	token = next_token(&val, cfile);
531 	if (token != TOK_NUMBER) {
532 		parse_warn("numeric day of month expected.");
533 		if (token != ';')
534 			skip_to_semi(cfile);
535 		return 0;
536 	}
537 	tm.tm_mday = atoi(val);
538 
539 	/* Hour... */
540 	token = next_token(&val, cfile);
541 	if (token != TOK_NUMBER) {
542 		parse_warn("numeric hour expected.");
543 		if (token != ';')
544 			skip_to_semi(cfile);
545 		return 0;
546 	}
547 	tm.tm_hour = atoi(val);
548 
549 	/* Colon separating hour from minute... */
550 	token = next_token(&val, cfile);
551 	if (token != ':') {
552 		parse_warn("expected colon separating hour from minute.");
553 		if (token != ';')
554 			skip_to_semi(cfile);
555 		return 0;
556 	}
557 	/* Minute... */
558 	token = next_token(&val, cfile);
559 	if (token != TOK_NUMBER) {
560 		parse_warn("numeric minute expected.");
561 		if (token != ';')
562 			skip_to_semi(cfile);
563 		return 0;
564 	}
565 	tm.tm_min = atoi(val);
566 
567 	/* Colon separating minute from second... */
568 	token = next_token(&val, cfile);
569 	if (token != ':') {
570 		parse_warn("expected colon separating hour from minute.");
571 		if (token != ';')
572 			skip_to_semi(cfile);
573 		return 0;
574 	}
575 	/* Minute... */
576 	token = next_token(&val, cfile);
577 	if (token != TOK_NUMBER) {
578 		parse_warn("numeric minute expected.");
579 		if (token != ';')
580 			skip_to_semi(cfile);
581 		return 0;
582 	}
583 	tm.tm_sec = atoi(val);
584 	tm.tm_isdst = 0;
585 
586 	/* XXX: We assume that mktime does not use tm_yday. */
587 	tm.tm_yday = 0;
588 
589 	/* Make sure the date ends in a semicolon... */
590 	token = next_token(&val, cfile);
591 	if (token != ';') {
592 		parse_warn("semicolon expected.");
593 		skip_to_semi(cfile);
594 		return 0;
595 	}
596 	/* Guess the time value... */
597 	guess = ((((((365 * (tm.tm_year - 70) +	/* Days in years since '70 */
598 	    (tm.tm_year - 69) / 4 +	/* Leap days since '70 */
599 	    (tm.tm_mon			/* Days in months this year */
600 	    ? months[tm.tm_mon - 1] : 0) +
601 	    (tm.tm_mon > 1 &&		/* Leap day this year */
602 	    !((tm.tm_year - 72) & 3)) +
603 	    tm.tm_mday - 1) * 24) +	/* Day of month */
604 	    tm.tm_hour) * 60) + tm.tm_min) * 60) + tm.tm_sec;
605 
606 	/*
607 	 * This guess could be wrong because of leap seconds or other
608 	 * weirdness we don't know about that the system does.   For
609 	 * now, we're just going to accept the guess, but at some point
610 	 * it might be nice to do a successive approximation here to get
611 	 * an exact value.   Even if the error is small, if the server
612 	 * is restarted frequently (and thus the lease database is
613 	 * reread), the error could accumulate into something
614 	 * significant.
615 	 */
616 	return (guess);
617 }
618