parse.c 90.5 KB
Newer Older
1 2 3 4 5
/* parse.c

   Common parser code for dhcpd and dhclient. */

/*
Ted Lemon's avatar
Ted Lemon committed
6 7 8 9 10 11
 * Copyright (c) 1996-1999 Internet Software Consortium.
 * Use is subject to license terms which appear in the file named
 * ISC-LICENSE that should have accompanied this file when you
 * received it.   If a file named ISC-LICENSE did not accompany this
 * file, or you are not sure the one you have is correct, you may
 * obtain an applicable copy of the license at:
12
 *
Ted Lemon's avatar
Ted Lemon committed
13
 *             http://www.isc.org/isc-license-1.0.html. 
14
 *
Ted Lemon's avatar
Ted Lemon committed
15 16 17
 * This file is part of the ISC DHCP distribution.   The documentation
 * associated with this file is listed in the file DOCUMENTATION,
 * included in the top-level directory of this release.
18
 *
Ted Lemon's avatar
Ted Lemon committed
19 20
 * Support and other services are available for ISC products - see
 * http://www.isc.org for more information.
21 22 23 24
 */

#ifndef lint
static char copyright[] =
Ted Lemon's avatar
Ted Lemon committed
25
"$Id: parse.c,v 1.65 2000/03/06 23:17:24 mellon Exp $ Copyright (c) 1995, 1996, 1997, 1998, 1999 The Internet Software Consortium.  All rights reserved.\n";
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#endif /* not lint */

#include "dhcpd.h"

/* Skip to the semicolon ending the current statement.   If we encounter
   braces, the matching closing brace terminates the statement.   If we
   encounter a right brace but haven't encountered a left brace, return
   leaving the brace in the token buffer for the caller.   If we see a
   semicolon and haven't seen a left brace, return.   This lets us skip
   over:

   	statement;
	statement foo bar { }
	statement foo bar { statement { } }
	statement}
 
	...et cetera. */

void skip_to_semi (cfile)
45
	struct parse *cfile;
46 47 48 49 50
{
	skip_to_rbrace (cfile, 0);
}

void skip_to_rbrace (cfile, brace_count)
51
	struct parse *cfile;
52
	int brace_count;
53
{
54
	enum dhcp_token token;
55
	const char *val;
56

57 58 59
#if defined (DEBUG_TOKEN)
	log_error ("skip_to_rbrace: %d\n", brace_count);
#endif
60 61 62
	do {
		token = peek_token (&val, cfile);
		if (token == RBRACE) {
63
			token = next_token (&val, cfile);
64 65 66 67 68 69 70 71 72 73
			if (brace_count) {
				if (!--brace_count)
					return;
			} else
				return;
		} else if (token == LBRACE) {
			brace_count++;
		} else if (token == SEMI && !brace_count) {
			token = next_token (&val, cfile);
			return;
74 75 76 77 78 79
		} else if (token == EOL) {
			/* EOL only happens when parsing /etc/resolv.conf,
			   and we treat it like a semicolon because the
			   resolv.conf file is line-oriented. */
			token = next_token (&val, cfile);
			return;
80 81 82 83 84 85
		}
		token = next_token (&val, cfile);
	} while (token != EOF);
}

int parse_semi (cfile)
86
	struct parse *cfile;
87
{
88
	enum dhcp_token token;
89
	const char *val;
90 91 92

	token = next_token (&val, cfile);
	if (token != SEMI) {
93
		parse_warn (cfile, "semicolon expected.");
94 95 96 97 98 99 100 101 102
		skip_to_semi (cfile);
		return 0;
	}
	return 1;
}

/* string-parameter :== STRING SEMI */

char *parse_string (cfile)
103
	struct parse *cfile;
104
{
105
	const char *val;
106
	enum dhcp_token token;
107 108 109 110
	char *s;

	token = next_token (&val, cfile);
	if (token != STRING) {
111
		parse_warn (cfile, "filename must be a string");
112 113 114
		skip_to_semi (cfile);
		return (char *)0;
	}
Ted Lemon's avatar
Ted Lemon committed
115
	s = (char *)dmalloc (strlen (val) + 1, MDL);
116
	if (!s)
117
		log_fatal ("no memory for string %s.", val);
118 119 120 121 122 123 124
	strcpy (s, val);

	if (!parse_semi (cfile))
		return (char *)0;
	return s;
}

125 126 127 128 129
/*
 * hostname :== IDENTIFIER
 *		| IDENTIFIER DOT
 *		| hostname DOT IDENTIFIER
 */
130 131

char *parse_host_name (cfile)
132
	struct parse *cfile;
133
{
134
	const char *val;
135
	enum dhcp_token token;
136
	unsigned len = 0;
137 138 139 140 141 142 143
	char *s;
	char *t;
	pair c = (pair)0;
	
	/* Read a dotted hostname... */
	do {
		/* Read a token, which should be an identifier. */
Ted Lemon's avatar
Ted Lemon committed
144 145 146
		token = peek_token (&val, cfile);
		if (!is_identifier (token) && token != NUMBER)
			break;
147
		token = next_token (&val, cfile);
Ted Lemon's avatar
Ted Lemon committed
148

149
		/* Store this identifier... */
Ted Lemon's avatar
Ted Lemon committed
150
		if (!(s = (char *)dmalloc (strlen (val) + 1, MDL)))
151
			log_fatal ("can't allocate temp space for hostname.");
152 153 154 155 156 157 158 159 160 161
		strcpy (s, val);
		c = cons ((caddr_t)s, c);
		len += strlen (s) + 1;
		/* Look for a dot; if it's there, keep going, otherwise
		   we're done. */
		token = peek_token (&val, cfile);
		if (token == DOT)
			token = next_token (&val, cfile);
	} while (token == DOT);

Ted Lemon's avatar
Ted Lemon committed
162 163 164 165
	/* Should be at least one token. */
	if (!len)
		return (char *)0;

166
	/* Assemble the hostname together into a string. */
Ted Lemon's avatar
Ted Lemon committed
167
	if (!(s = (char *)dmalloc (len, MDL)))
168
		log_fatal ("can't allocate space for hostname.");
169 170 171 172
	t = s + len;
	*--t = 0;
	while (c) {
		pair cdr = c -> cdr;
173
		unsigned l = strlen ((char *)(c -> car));
174 175 176
		t -= l;
		memcpy (t, (char *)(c -> car), l);
		/* Free up temp space. */
Ted Lemon's avatar
Ted Lemon committed
177 178
		dfree (c -> car, MDL);
		dfree (c, MDL);
179 180 181 182 183 184 185
		c = cdr;
		if (t != s)
			*--t = '.';
	}
	return s;
}

186 187 188 189 190 191 192
/* ip-addr-or-hostname :== ip-address | hostname
   ip-address :== NUMBER DOT NUMBER DOT NUMBER DOT NUMBER
   
   Parse an ip address or a hostname.   If uniform is zero, put in
   an expr_substring node to limit hostnames that evaluate to more
   than one IP address. */

193 194
int parse_ip_addr_or_hostname (expr, cfile, uniform)
	struct expression **expr;
195
	struct parse *cfile;
196 197
	int uniform;
{
198
	const char *val;
199
	enum dhcp_token token;
200
	unsigned char addr [4];
201
	unsigned len = sizeof addr;
202
	char *name;
203
	struct expression *x = (struct expression *)0;
204 205 206 207 208

	token = peek_token (&val, cfile);
	if (is_identifier (token)) {
		name = parse_host_name (cfile);
		if (!name)
209 210 211 212 213 214
			return 0;
		if (!make_host_lookup (expr, name))
			return 0;
		if (!uniform) {
			if (!make_limit (&x, *expr, 4))
				return 0;
215
			expression_dereference (expr, MDL);
216 217
			*expr = x;
		}
218 219
	} else if (token == NUMBER) {
		if (!parse_numeric_aggregate (cfile, addr, &len, DOT, 10, 8))
220 221
			return 0;
		return make_const_data (expr, addr, len, 0, 1);
222 223 224
	} else {
		if (token != RBRACE && token != LBRACE)
			token = next_token (&val, cfile);
225
		parse_warn (cfile, "%s (%d): expecting IP address or hostname",
226 227 228
			    val, token);
		if (token != SEMI)
			skip_to_semi (cfile);
229
		return 0;
230 231
	}

232
	return 1;
233 234 235 236 237 238
}	
	
/*
 * ip-address :== NUMBER DOT NUMBER DOT NUMBER DOT NUMBER
 */

239
int parse_ip_addr (cfile, addr)
240
	struct parse *cfile;
241 242
	struct iaddr *addr;
{
243
	const char *val;
244
	enum dhcp_token token;
245 246 247 248 249 250 251 252

	addr -> len = 4;
	if (parse_numeric_aggregate (cfile, addr -> iabuf,
				     &addr -> len, DOT, 10, 8))
		return 1;
	return 0;
}	

253 254 255 256
/*
 * hardware-parameter :== HARDWARE hardware-type colon-seperated-hex-list SEMI
 * hardware-type :== ETHERNET | TOKEN_RING
 */
257 258

void parse_hardware_param (cfile, hardware)
259
	struct parse *cfile;
260 261
	struct hardware *hardware;
{
262
	const char *val;
263
	enum dhcp_token token;
264
	unsigned hlen;
265 266 267 268 269
	unsigned char *t;

	token = next_token (&val, cfile);
	switch (token) {
	      case ETHERNET:
270
		hardware -> hbuf [0] = HTYPE_ETHER;
271 272
		break;
	      case TOKEN_RING:
273
		hardware -> hbuf [0] = HTYPE_IEEE802;
274
		break;
275
	      case FDDI:
276
		hardware -> hbuf [0] = HTYPE_FDDI;
277
		break;
278
	      default:
279
		parse_warn (cfile, "expecting a network hardware type");
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
		skip_to_semi (cfile);
		return;
	}

	/* Parse the hardware address information.   Technically,
	   it would make a lot of sense to restrict the length of the
	   data we'll accept here to the length of a particular hardware
	   address type.   Unfortunately, there are some broken clients
	   out there that put bogus data in the chaddr buffer, and we accept
	   that data in the lease file rather than simply failing on such
	   clients.   Yuck. */
	hlen = 0;
	t = parse_numeric_aggregate (cfile, (unsigned char *)0, &hlen,
				     COLON, 16, 8);
	if (!t)
		return;
296
	if (hlen + 1 > sizeof hardware -> hbuf) {
Ted Lemon's avatar
Ted Lemon committed
297
		dfree (t, MDL);
298
		parse_warn (cfile, "hardware address too long");
299
	} else {
300 301 302 303 304
		hardware -> hlen = hlen + 1;
		memcpy ((unsigned char *)&hardware -> hbuf [1], t, hlen);
		if (hlen + 1 < sizeof hardware -> hbuf)
			memset (&hardware -> hbuf [hlen + 1], 0,
				(sizeof hardware -> hbuf) - hlen - 1);
Ted Lemon's avatar
Ted Lemon committed
305
		dfree (t, MDL);
306 307 308 309
	}
	
	token = next_token (&val, cfile);
	if (token != SEMI) {
310
		parse_warn (cfile, "expecting semicolon.");
311 312 313 314 315 316 317
		skip_to_semi (cfile);
	}
}

/* lease-time :== NUMBER SEMI */

void parse_lease_time (cfile, timep)
318
	struct parse *cfile;
319 320
	TIME *timep;
{
321
	const char *val;
322
	enum dhcp_token token;
323 324 325

	token = next_token (&val, cfile);
	if (token != NUMBER) {
326
		parse_warn (cfile, "Expecting numeric lease time");
327 328 329
		skip_to_semi (cfile);
		return;
	}
330
	convert_num (cfile, (unsigned char *)timep, val, 10, 32);
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	/* Unswap the number - convert_num returns stuff in NBO. */
	*timep = ntohl (*timep); /* XXX */

	parse_semi (cfile);
}

/* No BNF for numeric aggregates - that's defined by the caller.  What
   this function does is to parse a sequence of numbers seperated by
   the token specified in seperator.  If max is zero, any number of
   numbers will be parsed; otherwise, exactly max numbers are
   expected.  Base and size tell us how to internalize the numbers
   once they've been tokenized. */

unsigned char *parse_numeric_aggregate (cfile, buf,
					max, seperator, base, size)
346
	struct parse *cfile;
347
	unsigned char *buf;
348
	unsigned *max;
349 350
	int seperator;
	int base;
351
	unsigned size;
352
{
353
	const char *val;
354
	enum dhcp_token token;
355
	unsigned char *bufp = buf, *s, *t;
356
	unsigned count = 0;
357 358 359
	pair c = (pair)0;

	if (!bufp && *max) {
Ted Lemon's avatar
Ted Lemon committed
360
		bufp = (unsigned char *)dmalloc (*max * size / 8, MDL);
361
		if (!bufp)
362 363
			log_fatal ("no space for numeric aggregate");
		s = 0;
364 365 366 367 368 369 370 371 372 373 374
	} else
		s = bufp;

	do {
		if (count) {
			token = peek_token (&val, cfile);
			if (token != seperator) {
				if (!*max)
					break;
				if (token != RBRACE && token != LBRACE)
					token = next_token (&val, cfile);
375
				parse_warn (cfile, "too few numbers.");
376 377 378 379 380 381 382 383 384
				if (token != SEMI)
					skip_to_semi (cfile);
				return (unsigned char *)0;
			}
			token = next_token (&val, cfile);
		}
		token = next_token (&val, cfile);

		if (token == EOF) {
385
			parse_warn (cfile, "unexpected end of file");
386 387 388 389 390 391
			break;
		}

		/* Allow NUMBER_OR_NAME if base is 16. */
		if (token != NUMBER &&
		    (base != 16 || token != NUMBER_OR_NAME)) {
392
			parse_warn (cfile, "expecting numeric value.");
393 394 395 396 397 398
			skip_to_semi (cfile);
			return (unsigned char *)0;
		}
		/* If we can, convert the number now; otherwise, build
		   a linked list of all the numbers. */
		if (s) {
399
			convert_num (cfile, s, val, base, size);
400 401
			s += size / 8;
		} else {
Ted Lemon's avatar
Ted Lemon committed
402
			t = (unsigned char *)dmalloc (strlen (val) + 1, MDL);
403
			if (!t)
404
				log_fatal ("no temp space for number.");
405 406
			strcpy ((char *)t, val);
			c = cons ((caddr_t)t, c);
407 408 409 410 411
		}
	} while (++count != *max);

	/* If we had to cons up a list, convert it now. */
	if (c) {
Ted Lemon's avatar
Ted Lemon committed
412
		bufp = (unsigned char *)dmalloc (count * size / 8, MDL);
413
		if (!bufp)
414
			log_fatal ("no space for numeric aggregate.");
415 416 417 418 419
		s = bufp + count - size / 8;
		*max = count;
	}
	while (c) {
		pair cdr = c -> cdr;
420
		convert_num (cfile, s, (char *)(c -> car), base, size);
421 422
		s -= size / 8;
		/* Free up temp space. */
Ted Lemon's avatar
Ted Lemon committed
423 424
		dfree (c -> car, MDL);
		dfree (c, MDL);
425 426 427 428 429
		c = cdr;
	}
	return bufp;
}

430 431
void convert_num (cfile, buf, str, base, size)
	struct parse *cfile;
432
	unsigned char *buf;
433
	const char *str;
434
	int base;
435
	unsigned size;
436
{
437
	const char *ptr = str;
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
	int negative = 0;
	u_int32_t val = 0;
	int tval;
	int max;

	if (*ptr == '-') {
		negative = 1;
		++ptr;
	}

	/* If base wasn't specified, figure it out from the data. */
	if (!base) {
		if (ptr [0] == '0') {
			if (ptr [1] == 'x') {
				base = 16;
				ptr += 2;
			} else if (isascii (ptr [1]) && isdigit (ptr [1])) {
				base = 8;
				ptr += 1;
			} else {
				base = 10;
			}
		} else {
			base = 10;
		}
	}

	do {
		tval = *ptr++;
		/* XXX assumes ASCII... */
		if (tval >= 'a')
			tval = tval - 'a' + 10;
		else if (tval >= 'A')
			tval = tval - 'A' + 10;
		else if (tval >= '0')
			tval -= '0';
		else {
475
			parse_warn (cfile, "Bogus number: %s.", str);
476 477 478
			break;
		}
		if (tval >= base) {
479 480
			parse_warn (cfile,
				    "Bogus number %s: digit %d not in base %d",
481
				    str, tval, base);
482 483 484 485 486 487 488 489 490 491 492 493
			break;
		}
		val = val * base + tval;
	} while (*ptr);

	if (negative)
		max = (1 << (size - 1));
	else
		max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
	if (val > max) {
		switch (base) {
		      case 8:
494
			parse_warn (cfile,
495
				    "%s%lo exceeds max (%d) for precision.",
Ted Lemon's avatar
Ted Lemon committed
496 497
				    negative ? "-" : "",
				    (unsigned long)val, max);
498 499
			break;
		      case 16:
500
			parse_warn (cfile,
501
				    "%s%lx exceeds max (%d) for precision.",
Ted Lemon's avatar
Ted Lemon committed
502 503
				    negative ? "-" : "",
				    (unsigned long)val, max);
504 505
			break;
		      default:
506
			parse_warn (cfile,
507
				    "%s%lu exceeds max (%d) for precision.",
Ted Lemon's avatar
Ted Lemon committed
508 509
				    negative ? "-" : "",
				    (unsigned long)val, max);
510 511 512 513 514 515 516 517 518 519
			break;
		}
	}

	if (negative) {
		switch (size) {
		      case 8:
			*buf = -(unsigned long)val;
			break;
		      case 16:
520
			putShort (buf, -(long)val);
521 522
			break;
		      case 32:
523
			putLong (buf, -(long)val);
524 525
			break;
		      default:
526 527
			parse_warn (cfile,
				    "Unexpected integer size: %d\n", size);
528 529 530 531 532 533 534 535 536 537 538 539 540 541
			break;
		}
	} else {
		switch (size) {
		      case 8:
			*buf = (u_int8_t)val;
			break;
		      case 16:
			putUShort (buf, (u_int16_t)val);
			break;
		      case 32:
			putULong (buf, val);
			break;
		      default:
542 543
			parse_warn (cfile,
				    "Unexpected integer size: %d\n", size);
544 545 546 547 548
			break;
		}
	}
}

549 550
/*
 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER 
551 552 553
 *		NUMBER COLON NUMBER COLON NUMBER SEMI |
 *          NUMBER NUMBER SLASH NUMBER SLASH NUMBER 
 *		NUMBER COLON NUMBER COLON NUMBER NUMBER SEMI |
554
 *	    NEVER
555
 *
556 557 558 559
 * Dates are stored in GMT or with a timezone offset; first number is day
 * of week; next is year/month/day; next is hours:minutes:seconds on a
 * 24-hour clock, followed by the timezone offset in seconds, which is
 * optional.
560
 */
561 562

TIME parse_date (cfile)
563
	struct parse *cfile;
564 565 566
{
	struct tm tm;
	int guess;
567
	int tzoff, wday, year, mon, mday, hour, min, sec;
568
	const char *val;
569
	enum dhcp_token token;
570 571 572
	static int months [11] = { 31, 59, 90, 120, 151, 181,
					  212, 243, 273, 304, 334 };

573
	/* Day of week, or "never"... */
574
	token = next_token (&val, cfile);
575 576 577 578 579 580
	if (token == NEVER) {
		if (!parse_semi (cfile))
			return 0;
		return MAX_TIME;
	}

581
	if (token != NUMBER) {
582
		parse_warn (cfile, "numeric day of week expected.");
583 584 585 586
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
587
	wday = atoi (val);
588 589 590 591

	/* Year... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
592
		parse_warn (cfile, "numeric year expected.");
593 594 595 596
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
597 598 599 600 601

	/* Note: the following is not a Y2K bug - it's a Y1.9K bug.   Until
	   somebody invents a time machine, I think we can safely disregard
	   it.   This actually works around a stupid Y2K bug that was present
	   in a very early beta release of dhcpd. */
602 603 604
	year = atoi (val);
	if (year > 1900)
		year -= 1900;
605 606 607 608

	/* Slash seperating year from month... */
	token = next_token (&val, cfile);
	if (token != SLASH) {
609 610
		parse_warn (cfile,
			    "expected slash seperating year from month.");
611 612 613 614 615 616 617 618
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Month... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
619
		parse_warn (cfile, "numeric month expected.");
620 621 622 623
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
624
	mon = atoi (val) - 1;
625 626 627 628

	/* Slash seperating month from day... */
	token = next_token (&val, cfile);
	if (token != SLASH) {
629 630
		parse_warn (cfile,
			    "expected slash seperating month from day.");
631 632 633 634 635 636 637 638
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Month... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
639
		parse_warn (cfile, "numeric day of month expected.");
640 641 642 643
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
644
	mday = atoi (val);
645 646 647 648

	/* Hour... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
649
		parse_warn (cfile, "numeric hour expected.");
650 651 652 653
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
654
	hour = atoi (val);
655 656 657 658

	/* Colon seperating hour from minute... */
	token = next_token (&val, cfile);
	if (token != COLON) {
659 660
		parse_warn (cfile,
			    "expected colon seperating hour from minute.");
661 662 663 664 665 666 667 668
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Minute... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
669
		parse_warn (cfile, "numeric minute expected.");
670 671 672 673
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
674
	min = atoi (val);
675 676 677 678

	/* Colon seperating minute from second... */
	token = next_token (&val, cfile);
	if (token != COLON) {
679 680
		parse_warn (cfile,
			    "expected colon seperating hour from minute.");
681 682 683 684 685 686 687 688
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Minute... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
689
		parse_warn (cfile, "numeric minute expected.");
690 691 692 693
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
694
	sec = atoi (val);
695

696 697 698 699 700 701
	token = peek_token (&val, cfile);
	if (token == NUMBER) {
		token = next_token (&val, cfile);
		tzoff = atoi (val);
	} else
		tzoff = 0;
702 703

	/* Make sure the date ends in a semicolon... */
Ted Lemon's avatar
Ted Lemon committed
704
	if (!parse_semi (cfile))
705 706 707
		return 0;

	/* Guess the time value... */
708 709 710 711
	guess = ((((((365 * (year - 70) +	/* Days in years since '70 */
		      (year - 69) / 4 +		/* Leap days since '70 */
		      (mon			/* Days in months this year */
		       ? months [mon - 1]
712
		       : 0) +
713 714 715 716 717
		      (mon > 1 &&		/* Leap day this year */
		       !((year - 72) & 3)) +
		      mday - 1) * 24) +		/* Day of month */
		    hour) * 60) +
		  min) * 60) + sec + tzoff;
718 719 720 721 722 723 724 725 726 727 728 729

	/* This guess could be wrong because of leap seconds or other
	   weirdness we don't know about that the system does.   For
	   now, we're just going to accept the guess, but at some point
	   it might be nice to do a successive approximation here to
	   get an exact value.   Even if the error is small, if the
	   server is restarted frequently (and thus the lease database
	   is reread), the error could accumulate into something
	   significant. */

	return guess;
}
730

731 732 733 734 735
/*
 * option-name :== IDENTIFIER |
 		   IDENTIFIER . IDENTIFIER
 */

736
struct option *parse_option_name (cfile, allocate, known)
737
	struct parse *cfile;
738
	int allocate;
739
	int *known;
740
{
741
	const char *val;
742
	enum dhcp_token token;
743
	char *uname;
744 745 746 747 748
	struct universe *universe;
	struct option *option;

	token = next_token (&val, cfile);
	if (!is_identifier (token)) {
749 750
		parse_warn (cfile,
			    "expecting identifier after option keyword.");
751 752 753 754
		if (token != SEMI)
			skip_to_semi (cfile);
		return (struct option *)0;
	}
Ted Lemon's avatar
Ted Lemon committed
755
	uname = dmalloc (strlen (val) + 1, MDL);
756 757 758
	if (!uname)
		log_fatal ("no memory for uname information.");
	strcpy (uname, val);
759 760 761 762 763 764 765 766
	token = peek_token (&val, cfile);
	if (token == DOT) {
		/* Go ahead and take the DOT token... */
		token = next_token (&val, cfile);

		/* The next token should be an identifier... */
		token = next_token (&val, cfile);
		if (!is_identifier (token)) {
767
			parse_warn (cfile, "expecting identifier after '.'");
768 769 770 771 772 773
			if (token != SEMI)
				skip_to_semi (cfile);
			return (struct option *)0;
		}

		/* Look up the option name hash table for the specified
774
		   uname. */
775 776
		universe = ((struct universe *)
			    hash_lookup (&universe_hash,
777
					 (unsigned char *)uname, 0));
778 779 780
		/* If it's not there, we can't parse the rest of the
		   declaration. */
		if (!universe) {
781
			parse_warn (cfile, "no option space named %s.", uname);
782 783 784 785 786 787
			skip_to_semi (cfile);
			return (struct option *)0;
		}
	} else {
		/* Use the default hash table, which contains all the
		   standard dhcp option names. */
788
		val = uname;
789 790 791 792 793
		universe = &dhcp_universe;
	}

	/* Look up the actual option info... */
	option = (struct option *)hash_lookup (universe -> hash,
794
					       (const unsigned char *)val, 0);
795 796

	/* If we didn't get an option structure, it's an undefined option. */
797 798 799
	if (option) {
		*known = 1;
	} else {
800 801 802 803
		/* If we've been told to allocate, that means that this
		   (might) be an option code definition, so we'll create
		   an option structure just in case. */
		if (allocate) {
804
			option = new_option (MDL);
805 806 807
			if (val == uname)
				option -> name = val;
			else {
808
				char *s;
Ted Lemon's avatar
Ted Lemon committed
809 810
				dfree (uname, MDL);
				s = dmalloc (strlen (val) + 1, MDL);
811
				if (!s)
812 813
				    log_fatal ("no memory for option %s.%s",
					       universe -> name, val);
814 815
				strcpy (s, val);
				option -> name = s;
816 817
			}
			option -> universe = universe;
818
			option -> code = 0;
819 820 821
			return option;
		}
		if (val == uname)
822
			parse_warn (cfile, "no option named %s", val);
823
		else
824
			parse_warn (cfile, "no option named %s in space %s",
825
				    val, uname);
826 827 828 829 830
		skip_to_semi (cfile);
		return (struct option *)0;
	}

	/* Free the initial identifier token. */
Ted Lemon's avatar
Ted Lemon committed
831
	dfree (uname, MDL);
832 833 834
	return option;
}

835 836 837
/* IDENTIFIER SEMI */

void parse_option_space_decl (cfile)
838
	struct parse *cfile;
839 840
{
	int token;
841
	const char *val;
842
	struct universe **ua, *nu;
843
	char *s;
844 845 846 847 848

	next_token (&val, cfile);	/* Discard the SPACE token, which was
					   checked by the caller. */
	token = next_token (&val, cfile);
	if (!is_identifier (token)) {
849
		parse_warn (cfile, "expecting identifier.");
850 851 852
		skip_to_semi (cfile);
		return;
	}
853
	nu = new_universe (MDL);
854 855 856 857
	if (!nu)
		log_fatal ("No memory for new option space.");

	/* Set up the server option universe... */
Ted Lemon's avatar
Ted Lemon committed
858
	s = dmalloc (strlen (val) + 1, MDL);
859
	if (!s)
860
		log_fatal ("No memory for new option space name.");
861 862
	strcpy (s, val);
	nu -> name = s;
863 864 865 866 867 868 869 870 871 872 873 874 875 876
	nu -> lookup_func = lookup_hashed_option;
	nu -> option_state_dereference =
		hashed_option_state_dereference;
	nu -> get_func = hashed_option_get;
	nu -> set_func = hashed_option_set;
	nu -> save_func = save_hashed_option;
	nu -> delete_func = delete_hashed_option;
	nu -> encapsulate = hashed_option_space_encapsulate;
	nu -> length_size = 1;
	nu -> tag_size = 1;
	nu -> store_tag = putUChar;
	nu -> store_length = putUChar;
	nu -> index = universe_count++;
	if (nu -> index >= universe_max) {
Ted Lemon's avatar
Ted Lemon committed
877
		ua = dmalloc (universe_max * 2 * sizeof *ua, MDL);
878 879 880 881
		if (!ua)
			log_fatal ("No memory to expand option space array.");
		memcpy (ua, universes, universe_max * sizeof *ua);
		universe_max *= 2;
Ted Lemon's avatar
Ted Lemon committed
882
		dfree (universes, MDL);
883 884 885
		universes = ua;
	}
	universes [nu -> index] = nu;
Ted Lemon's avatar
Ted Lemon committed
886
	nu -> hash = new_hash (0, 0);
887 888 889
	if (!nu -> hash)
		log_fatal ("Can't allocate %s option hash table.", nu -> name);
	add_hash (&universe_hash,
890
		  (const unsigned char *)nu -> name, 0, (unsigned char *)nu);
891 892 893
	parse_semi (cfile);
}

894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
/* This is faked up to look good right now.   Ideally, this should do a
   recursive parse and allow arbitrary data structure definitions, but for
   now it just allows you to specify a single type, an array of single types,
   a sequence of types, or an array of sequences of types.

   ocd :== NUMBER EQUALS ocsd SEMI

   ocsd :== ocsd_type |
	    ocsd_type_sequence |
	    ARRAY OF ocsd_type |
	    ARRAY OF ocsd_type_sequence

   ocsd_type :== BOOLEAN |
		 INTEGER NUMBER |
		 SIGNED INTEGER NUMBER |
		 UNSIGNED INTEGER NUMBER |
		 IP-ADDRESS |
		 TEXT |
		 STRING

   ocsd_type_sequence :== LBRACE ocsd_types RBRACE

   ocsd_type :== ocsd_type |
		 ocsd_types ocsd_type */

int parse_option_code_definition (cfile, option)
920
	struct parse *cfile;
921 922
	struct option *option;
{
923
	const char *val;
924
	enum dhcp_token token;
925
	unsigned arrayp = 0;
926 927 928
	int recordp = 0;
	int no_more_in_record = 0;
	char tokbuf [128];
929
	unsigned tokix = 0;
930 931 932
	char type;
	int code;
	int is_signed;
933
	char *s;
934 935 936 937
	
	/* Parse the option code. */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
938
		parse_warn (cfile, "expecting option code number.");
939 940 941 942 943 944 945
		skip_to_semi (cfile);
		return 0;
	}
	option -> code = atoi (val);

	token = next_token (&val, cfile);
	if (token != EQUAL) {
946
		parse_warn (cfile, "expecting \"=\"");
947 948 949 950 951 952 953 954 955
		skip_to_semi (cfile);
		return 0;
	}

	/* See if this is an array. */
	token = next_token (&val, cfile);
	if (token == ARRAY) {
		token = next_token (&val, cfile);
		if (token != OF) {
956
			parse_warn (cfile, "expecting \"of\".");
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
			skip_to_semi (cfile);
			return 0;
		}
		arrayp = 1;
		token = next_token (&val, cfile);
	}

	if (token == LBRACE) {
		recordp = 1;
		token = next_token (&val, cfile);
	}

	/* At this point we're expecting a data type. */
      next_type:
	switch (token) {
	      case BOOLEAN:
		type = 'f';
		break;
	      case INTEGER:
		is_signed = 1;
	      parse_integer:
		token = next_token (&val, cfile);
		if (token != NUMBER) {
980
			parse_warn (cfile, "expecting number.");
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
			skip_to_rbrace (cfile, recordp);
			if (recordp)
				skip_to_semi (cfile);
			return 0;
		}
		switch (atoi (val)) {
		      case 8:
			type = is_signed ? 'b' : 'B';
			break;
		      case 16:
			type = is_signed ? 's' : 'S';
			break;
		      case 32:
			type = is_signed ? 'l' : 'L';
			break;
		      default:
997 998
			parse_warn (cfile,
				    "%s bit precision is not supported.", val);
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
			skip_to_rbrace (cfile, recordp);
			if (recordp)
				skip_to_semi (cfile);
			return 0;
		}
		break;
	      case SIGNED:
		is_signed = 1;
	      parse_signed:
		token = next_token (&val, cfile);
		if (token != INTEGER) {
1010
			parse_warn (cfile, "expecting \"integer\" keyword.");
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
			skip_to_rbrace (cfile, recordp);
			if (recordp)
				skip_to_semi (cfile);
			return 0;
		}
		goto parse_integer;
	      case UNSIGNED:
		is_signed = 0;
		goto parse_signed;

	      case IP_ADDRESS:
		type = 'I';
		break;
	      case TEXT:
		type = 't';
	      no_arrays:
		if (arrayp) {
1028
			parse_warn (cfile, "arrays of text strings not %s",
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
				    "yet supported.");
			skip_to_rbrace (cfile, recordp);
			if (recordp)
				skip_to_semi (cfile);
			return 0;
		}
		no_more_in_record = 1;
		break;
	      case STRING:
		type = 'X';
		goto no_arrays;

	      default:
1042
		parse_warn (cfile, "unknown data type %s", val);
1043 1044 1045 1046 1047 1048 1049
		skip_to_rbrace (cfile, recordp);
		if (recordp)
			skip_to_semi (cfile);
		return 0;
	}

	if (tokix == sizeof tokbuf) {
1050
		parse_warn (cfile, "too many types in record.");
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
		skip_to_rbrace (cfile, recordp);
		if (recordp)
			skip_to_semi (cfile);
		return 0;
	}
	tokbuf [tokix++] = type;

	if (recordp) {
		token = next_token (&val, cfile);
		if (token == COMMA) {
			if (no_more_in_record) {