parse.c 64.1 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[] =
25
"$Id: parse.c,v 1.39 1999/09/28 22:54:56 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 45 46
#endif /* not lint */

#include "dhcpd.h"
#include "dhctoken.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)
	FILE *cfile;
47 48 49 50 51 52 53
{
	skip_to_rbrace (cfile, 0);
}

void skip_to_rbrace (cfile, brace_count)
	FILE *cfile;
	int brace_count;
54
{
55
	enum dhcp_token token;
56 57 58 59 60
	char *val;

	do {
		token = peek_token (&val, cfile);
		if (token == RBRACE) {
61
			token = next_token (&val, cfile);
62 63 64 65 66 67 68 69 70 71
			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;
72 73 74 75 76 77
		} 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;
78 79 80 81 82 83 84 85
		}
		token = next_token (&val, cfile);
	} while (token != EOF);
}

int parse_semi (cfile)
	FILE *cfile;
{
86
	enum dhcp_token token;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	char *val;

	token = next_token (&val, cfile);
	if (token != SEMI) {
		parse_warn ("semicolon expected.");
		skip_to_semi (cfile);
		return 0;
	}
	return 1;
}

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

char *parse_string (cfile)
	FILE *cfile;
{
	char *val;
104
	enum dhcp_token token;
105 106 107 108 109 110 111 112 113 114
	char *s;

	token = next_token (&val, cfile);
	if (token != STRING) {
		parse_warn ("filename must be a string");
		skip_to_semi (cfile);
		return (char *)0;
	}
	s = (char *)malloc (strlen (val) + 1);
	if (!s)
115
		log_fatal ("no memory for string %s.", val);
116 117 118 119 120 121 122
	strcpy (s, val);

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

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

char *parse_host_name (cfile)
	FILE *cfile;
{
	char *val;
133
	enum dhcp_token token;
134 135 136 137 138 139 140 141
	int len = 0;
	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
142 143 144
		token = peek_token (&val, cfile);
		if (!is_identifier (token) && token != NUMBER)
			break;
145
		token = next_token (&val, cfile);
Ted Lemon's avatar
Ted Lemon committed
146

147 148
		/* Store this identifier... */
		if (!(s = (char *)malloc (strlen (val) + 1)))
149
			log_fatal ("can't allocate temp space for hostname.");
150 151 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);

	/* Assemble the hostname together into a string. */
	if (!(s = (char *)malloc (len)))
162
		log_fatal ("can't allocate space for hostname.");
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	t = s + len;
	*--t = 0;
	while (c) {
		pair cdr = c -> cdr;
		int l = strlen ((char *)(c -> car));
		t -= l;
		memcpy (t, (char *)(c -> car), l);
		/* Free up temp space. */
		free (c -> car);
		free (c);
		c = cdr;
		if (t != s)
			*--t = '.';
	}
	return s;
}

180 181 182 183 184 185 186
/* 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. */

187 188
int parse_ip_addr_or_hostname (expr, cfile, uniform)
	struct expression **expr;
189 190 191 192
	FILE *cfile;
	int uniform;
{
	char *val;
193
	enum dhcp_token token;
194 195 196
	unsigned char addr [4];
	int len = sizeof addr;
	char *name;
197
	struct expression *x = (struct expression *)0;
198 199 200 201 202

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

227
	return 1;
228 229 230 231 232 233
}	
	
/*
 * ip-address :== NUMBER DOT NUMBER DOT NUMBER DOT NUMBER
 */

234 235 236 237 238
int parse_ip_addr (cfile, addr)
	FILE *cfile;
	struct iaddr *addr;
{
	char *val;
239
	enum dhcp_token token;
240 241 242 243 244 245 246 247

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

248 249 250 251
/*
 * hardware-parameter :== HARDWARE hardware-type colon-seperated-hex-list SEMI
 * hardware-type :== ETHERNET | TOKEN_RING
 */
252 253 254 255 256 257

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

	token = next_token (&val, cfile);
	switch (token) {
	      case ETHERNET:
		hardware -> htype = HTYPE_ETHER;
		break;
	      case TOKEN_RING:
		hardware -> htype = HTYPE_IEEE802;
		break;
270 271 272
	      case FDDI:
		hardware -> htype = HTYPE_FDDI;
		break;
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	      default:
		parse_warn ("expecting a network hardware type");
		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;
	if (hlen > sizeof hardware -> haddr) {
		free (t);
		parse_warn ("hardware address too long");
	} else {
		hardware -> hlen = hlen;
		memcpy ((unsigned char *)&hardware -> haddr [0],
			t, hardware -> hlen);
298 299 300
		if (hlen < sizeof hardware -> haddr)
			memset (&hardware -> haddr [hlen], 0,
				(sizeof hardware -> haddr) - hlen);
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
		free (t);
	}
	
	token = next_token (&val, cfile);
	if (token != SEMI) {
		parse_warn ("expecting semicolon.");
		skip_to_semi (cfile);
	}
}

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

void parse_lease_time (cfile, timep)
	FILE *cfile;
	TIME *timep;
{
	char *val;
318
	enum dhcp_token token;
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

	token = next_token (&val, cfile);
	if (token != NUMBER) {
		parse_warn ("Expecting numeric lease time");
		skip_to_semi (cfile);
		return;
	}
	convert_num ((unsigned char *)timep, val, 10, 32);
	/* 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)
	FILE *cfile;
	unsigned char *buf;
	int *max;
	int seperator;
	int base;
	int size;
{
	char *val;
350
	enum dhcp_token token;
351 352 353 354 355 356 357
	unsigned char *bufp = buf, *s, *t;
	int count = 0;
	pair c = (pair)0;

	if (!bufp && *max) {
		bufp = (unsigned char *)malloc (*max * size / 8);
		if (!bufp)
358 359
			log_fatal ("no space for numeric aggregate");
		s = 0;
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
	} 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);
				parse_warn ("too few numbers.");
				if (token != SEMI)
					skip_to_semi (cfile);
				return (unsigned char *)0;
			}
			token = next_token (&val, cfile);
		}
		token = next_token (&val, cfile);

		if (token == EOF) {
			parse_warn ("unexpected end of file");
			break;
		}

		/* Allow NUMBER_OR_NAME if base is 16. */
		if (token != NUMBER &&
		    (base != 16 || token != NUMBER_OR_NAME)) {
			parse_warn ("expecting numeric value.");
			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) {
			convert_num (s, val, base, size);
			s += size / 8;
		} else {
			t = (unsigned char *)malloc (strlen (val) + 1);
			if (!t)
400
				log_fatal ("no temp space for number.");
401 402
			strcpy ((char *)t, val);
			c = cons ((caddr_t)t, c);
403 404 405 406 407 408 409
		}
	} while (++count != *max);

	/* If we had to cons up a list, convert it now. */
	if (c) {
		bufp = (unsigned char *)malloc (count * size / 8);
		if (!bufp)
410
			log_fatal ("no space for numeric aggregate.");
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 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
		s = bufp + count - size / 8;
		*max = count;
	}
	while (c) {
		pair cdr = c -> cdr;
		convert_num (s, (char *)(c -> car), base, size);
		s -= size / 8;
		/* Free up temp space. */
		free (c -> car);
		free (c);
		c = cdr;
	}
	return bufp;
}

void convert_num (buf, str, base, size)
	unsigned char *buf;
	char *str;
	int base;
	int size;
{
	char *ptr = str;
	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 {
470
			parse_warn ("Bogus number: %s.", str);
471 472 473
			break;
		}
		if (tval >= base) {
474 475
			parse_warn ("Bogus number %s: digit %d not in base %d",
				    str, tval, base);
476 477 478 479 480 481 482 483 484 485 486 487
			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:
488
			parse_warn ("%s%o exceeds max (%d) for precision.",
489 490 491
			      negative ? "-" : "", val, max);
			break;
		      case 16:
492
			parse_warn ("%s%x exceeds max (%d) for precision.",
493 494 495
			      negative ? "-" : "", val, max);
			break;
		      default:
496 497
			parse_warn ("%s%u exceeds max (%d) for precision.",
				    negative ? "-" : "", val, max);
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
			break;
		}
	}

	if (negative) {
		switch (size) {
		      case 8:
			*buf = -(unsigned long)val;
			break;
		      case 16:
			putShort (buf, -(unsigned long)val);
			break;
		      case 32:
			putLong (buf, -(unsigned long)val);
			break;
		      default:
514
			parse_warn ("Unexpected integer size: %d\n", size);
515 516 517 518 519 520 521 522 523 524 525 526 527 528
			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:
529
			parse_warn ("Unexpected integer size: %d\n", size);
530 531 532 533 534
			break;
		}
	}
}

535 536
/*
 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER 
537 538 539
 *		NUMBER COLON NUMBER COLON NUMBER SEMI |
 *          NUMBER NUMBER SLASH NUMBER SLASH NUMBER 
 *		NUMBER COLON NUMBER COLON NUMBER NUMBER SEMI |
540
 *	    NEVER
541
 *
542 543 544 545
 * 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.
546
 */
547 548 549 550 551 552

TIME parse_date (cfile)
	FILE *cfile;
{
	struct tm tm;
	int guess;
553
	int tzoff, wday, year, mon, mday, hour, min, sec;
554
	char *val;
555
	enum dhcp_token token;
556 557 558
	static int months [11] = { 31, 59, 90, 120, 151, 181,
					  212, 243, 273, 304, 334 };

559
	/* Day of week, or "never"... */
560
	token = next_token (&val, cfile);
561 562 563 564 565 566
	if (token == NEVER) {
		if (!parse_semi (cfile))
			return 0;
		return MAX_TIME;
	}

567 568 569 570 571 572
	if (token != NUMBER) {
		parse_warn ("numeric day of week expected.");
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
573
	wday = atoi (val);
574 575 576 577 578 579 580 581 582

	/* Year... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
		parse_warn ("numeric year expected.");
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
583 584 585 586 587

	/* 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. */
588 589 590
	year = atoi (val);
	if (year > 1900)
		year -= 1900;
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

	/* Slash seperating year from month... */
	token = next_token (&val, cfile);
	if (token != SLASH) {
		parse_warn ("expected slash seperating year from month.");
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Month... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
		parse_warn ("numeric month expected.");
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
609
	mon = atoi (val) - 1;
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

	/* Slash seperating month from day... */
	token = next_token (&val, cfile);
	if (token != SLASH) {
		parse_warn ("expected slash seperating month from day.");
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Month... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
		parse_warn ("numeric day of month expected.");
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
628
	mday = atoi (val);
629 630 631 632 633 634 635 636 637

	/* Hour... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
		parse_warn ("numeric hour expected.");
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
638
	hour = atoi (val);
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656

	/* Colon seperating hour from minute... */
	token = next_token (&val, cfile);
	if (token != COLON) {
		parse_warn ("expected colon seperating hour from minute.");
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Minute... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
		parse_warn ("numeric minute expected.");
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
657
	min = atoi (val);
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675

	/* Colon seperating minute from second... */
	token = next_token (&val, cfile);
	if (token != COLON) {
		parse_warn ("expected colon seperating hour from minute.");
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Minute... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
		parse_warn ("numeric minute expected.");
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
676
	sec = atoi (val);
677

678 679 680 681 682 683
	token = peek_token (&val, cfile);
	if (token == NUMBER) {
		token = next_token (&val, cfile);
		tzoff = atoi (val);
	} else
		tzoff = 0;
684 685

	/* Make sure the date ends in a semicolon... */
Ted Lemon's avatar
Ted Lemon committed
686
	if (!parse_semi (cfile))
687 688 689
		return 0;

	/* Guess the time value... */
690 691 692 693
	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]
694
		       : 0) +
695 696 697 698 699
		      (mon > 1 &&		/* Leap day this year */
		       !((year - 72) & 3)) +
		      mday - 1) * 24) +		/* Day of month */
		    hour) * 60) +
		  min) * 60) + sec + tzoff;
700 701 702 703 704 705 706 707 708 709 710 711

	/* 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;
}
712

713 714 715 716 717
/*
 * option-name :== IDENTIFIER |
 		   IDENTIFIER . IDENTIFIER
 */

718
struct option *parse_option_name (cfile, allocate)
719
	FILE *cfile;
720
	int allocate;
721 722
{
	char *val;
723
	enum dhcp_token token;
724
	char *uname;
725 726 727 728 729 730 731 732 733 734
	struct universe *universe;
	struct option *option;

	token = next_token (&val, cfile);
	if (!is_identifier (token)) {
		parse_warn ("expecting identifier after option keyword.");
		if (token != SEMI)
			skip_to_semi (cfile);
		return (struct option *)0;
	}
735 736 737 738
	uname = malloc (strlen (val) + 1);
	if (!uname)
		log_fatal ("no memory for uname information.");
	strcpy (uname, val);
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
	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)) {
			parse_warn ("expecting identifier after '.'");
			if (token != SEMI)
				skip_to_semi (cfile);
			return (struct option *)0;
		}

		/* Look up the option name hash table for the specified
754
		   uname. */
755 756
		universe = ((struct universe *)
			    hash_lookup (&universe_hash,
757
					 (unsigned char *)uname, 0));
758 759 760
		/* If it's not there, we can't parse the rest of the
		   declaration. */
		if (!universe) {
761
			parse_warn ("no option space named %s.", uname);
762 763 764 765 766 767
			skip_to_semi (cfile);
			return (struct option *)0;
		}
	} else {
		/* Use the default hash table, which contains all the
		   standard dhcp option names. */
768
		val = uname;
769 770 771 772 773 774 775 776 777
		universe = &dhcp_universe;
	}

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

	/* If we didn't get an option structure, it's an undefined option. */
	if (!option) {
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
		/* 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) {
			option = new_option ("parse_option_name");
			if (val == uname)
				option -> name = val;
			else {
				free (uname);
				option -> name = dmalloc (strlen (val) + 1,
							  "parse_option_name");
				if (!option -> name)
					log_fatal ("no memory for option %s.%s",
					           universe -> name, val);
				strcpy (option -> name, val);
			}
			option -> universe = universe;
			option -> code = -1;
			return option;
		}
		if (val == uname)
799 800
			parse_warn ("no option named %s", val);
		else
801 802
			parse_warn ("no option named %s in space %s",
				    val, uname);
803 804 805 806 807
		skip_to_semi (cfile);
		return (struct option *)0;
	}

	/* Free the initial identifier token. */
808
	free (uname);
809 810 811
	return option;
}

812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
/* IDENTIFIER SEMI */

void parse_option_space_decl (cfile)
	FILE *cfile;
{
	int token;
	char *val;
	struct universe **ua, *nu;

	next_token (&val, cfile);	/* Discard the SPACE token, which was
					   checked by the caller. */
	token = next_token (&val, cfile);
	if (!is_identifier (token)) {
		parse_warn ("expecting identifier.");
		skip_to_semi (cfile);
		return;
	}
	nu = new_universe ("parse_option_space_decl");
	if (!nu)
		log_fatal ("No memory for new option space.");

	/* Set up the server option universe... */
	nu -> name = dmalloc (strlen (val) + 1, "parse_option_space_decl");
	if (!nu -> name)
		log_fatal ("No memory for new option space name.");
	strcpy (nu -> name, val);
	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) {
		ua = dmalloc (universe_max * 2 * sizeof *ua,
			      "parse_option_space_decl");
		if (!ua)
			log_fatal ("No memory to expand option space array.");
		memcpy (ua, universes, universe_max * sizeof *ua);
		universe_max *= 2;
		dfree (universes, "parse_option_space_decl");
		universes = ua;
	}
	universes [nu -> index] = nu;
	nu -> hash = new_hash ();
	if (!nu -> hash)
		log_fatal ("Can't allocate %s option hash table.", nu -> name);
	add_hash (&universe_hash,
		  (unsigned char *)nu -> name, 0, (unsigned char *)nu);
	parse_semi (cfile);
}

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 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 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
/* 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)
	FILE *cfile;
	struct option *option;
{
	char *val;
	enum dhcp_token token;
	int arrayp = 0;
	int recordp = 0;
	int no_more_in_record = 0;
	char tokbuf [128];
	int tokix = 0;
	char type;
	int code;
	int is_signed;
	
	/* Parse the option code. */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
		parse_warn ("expecting option code number.");
		skip_to_semi (cfile);
		return 0;
	}
	option -> code = atoi (val);

	token = next_token (&val, cfile);
	if (token != EQUAL) {
		parse_warn ("expecting \"=\"");
		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) {
			parse_warn ("expecting \"of\".");
			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) {
			parse_warn ("expecting number.");
			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:
			parse_warn ("%s bit precision is not supported.", val);
			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) {
			parse_warn ("expecting \"integer\" keyword.");
			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) {
			parse_warn ("arrays of text strings not %s",
				    "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:
		parse_warn ("unknown data type %s", val);
		skip_to_rbrace (cfile, recordp);
		if (recordp)
			skip_to_semi (cfile);
		return 0;
	}

	if (tokix == sizeof tokbuf) {
		parse_warn ("too many types in record.");
		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) {
				parse_warn ("%s must be at end of record.",
					    type == 't' ? "text" : "string");
				skip_to_rbrace (cfile, 1);
				if (recordp)
					skip_to_semi (cfile);
				return 0;
			}
			token = next_token (&val, cfile);
			goto next_type;
		}
		if (token != RBRACE) {
			parse_warn ("expecting right brace.");
			skip_to_rbrace (cfile, 1);
			if (recordp)
				skip_to_semi (cfile);
			return 0;
		}
	}
	if (!parse_semi (cfile)) {
		parse_warn ("semicolon expected.");
		skip_to_semi (cfile);
		if (recordp)
			skip_to_semi (cfile);
		return 0;
	}
	option -> format = dmalloc (tokix + arrayp + 1,
				    "parse_option_code_definition");
	if (!option -> format)
		log_fatal ("no memory for option format.");
	memcpy (option -> format, tokbuf, tokix);
	if (arrayp)
		option -> format [tokix++] = 'A';
	option -> format [tokix] = 0;
	if (option -> universe -> options [option -> code]) {
		/* XXX Free the option, but we can't do that now because they
		   XXX may start out static. */
	}
	option -> universe -> options [option -> code] = option;
	add_hash (option -> universe -> hash,
		  (unsigned char *)option -> name, 0, (unsigned char *)option);
	return 1;
}

1079 1080 1081 1082 1083
/*
 * colon-seperated-hex-list :== NUMBER |
 *				NUMBER COLON colon-seperated-hex-list
 */

1084 1085
int parse_cshl (data, cfile)
	struct data_string *data;
1086 1087
	FILE *cfile;
{
Ted Lemon's avatar
Ted Lemon committed
1088
	u_int8_t ibuf [128];
1089 1090 1091 1092
	int ilen = 0;
	int tlen = 0;
	struct option_tag *sl = (struct option_tag *)0;
	struct option_tag *next, **last = &sl;
1093
	enum dhcp_token token;
1094
	char *val;
1095
	unsigned char *rvp;
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105

	do {
		token = next_token (&val, cfile);
		if (token != NUMBER && token != NUMBER_OR_NAME) {
			parse_warn ("expecting hexadecimal number.");
			skip_to_semi (cfile);
			for (; sl; sl = next) {
				next = sl -> next;
				dfree (sl, "parse_cshl");
			}
1106
			return 0;
1107 1108 1109 1110 1111 1112 1113
		}
		if (ilen == sizeof ibuf) {
			next = (struct option_tag *)
				dmalloc (ilen - 1 +
					 sizeof (struct option_tag),
					 "parse_cshl");
			if (!next)
1114
				log_fatal ("no memory for string list.");
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
			memcpy (next -> data, ibuf, ilen);
			*last = next;
			last = &next -> next;
			tlen += ilen;
			ilen = 0;
		}
		convert_num (&ibuf [ilen++], val, 16, 8);

		token = peek_token (&val, cfile);
		if (token != COLON)
			break;
		token = next_token (&val, cfile);
	} while (1);

1129
	if (!buffer_allocate (&data -> buffer, tlen + ilen, "parse_cshl"))
1130
		log_fatal ("no memory to store octet data.");
1131 1132 1133 1134 1135
	data -> data = &data -> buffer -> data [0];
	data -> len = tlen + ilen;
	data -> terminated = 0;

	rvp = &data -> data [0];
1136 1137 1138 1139 1140 1141 1142 1143 1144
	while (sl) {
		next = sl -> next;
		memcpy (rvp, sl -> data, sizeof ibuf);
		rvp += sizeof ibuf;
		dfree (sl, "parse_cshl");
		sl = next;
	}
	
	memcpy (rvp, ibuf, ilen);
1145
	return 1;
1146
}
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161

/*
 * executable-statements :== executable-statement executable-statements |
 *			     executable-statement
 *
 * executable-statement :==
 *	IF if-statement |
 * 	ADD class-name SEMI |
 *	BREAK SEMI |
 *	OPTION option-parameter SEMI |
 *	SUPERSEDE option-parameter SEMI |
 *	PREPEND option-parameter SEMI |
 *	APPEND option-parameter SEMI
 */

1162 1163
int parse_executable_statements (statements, cfile, lose)
	struct executable_statement **statements;
1164 1165 1166
	FILE *cfile;
	int *lose;
{
1167
	struct executable_statement **next;
1168

1169 1170
	next = statements;
	while (parse_executable_statement (next, cfile, lose))
1171
		next = &((*next) -> next);
1172
	if (!*lose)
1173 1174
		return 1;
	return 0;
1175 1176
}

1177 1178
int parse_executable_statement (result, cfile, lose)
	struct executable_statement **result;
1179 1180 1181
	FILE *cfile;
	int *lose;
{
1182
	enum dhcp_token token;
1183
	char *val;
1184
	struct executable_statement base;
1185 1186
	struct class *cta;
	struct option *option;
1187
	struct option_cache *cache;
1188

1189 1190
	token = peek_token (&val, cfile);
	switch (token) {
1191
	      case IF:
1192
		next_token (&val, cfile);
1193 1194
		return parse_if_statement (result, cfile, lose);

Ted Lemon's avatar
Ted Lemon committed
1195
	      case TOKEN_ADD:
1196
		token = next_token (&val, cfile);
1197 1198 1199 1200 1201
		token = next_token (&val, cfile);
		if (token != STRING) {
			parse_warn ("expecting class name.");
			skip_to_semi (cfile);
			*lose = 1;
1202
			return 0;
1203 1204 1205 1206 1207 1208
		}
		cta = find_class (val);
		if (!cta) {
			parse_warn ("unknown class %s.", val);
			skip_to_semi (cfile);
			*lose = 1;
1209
			return 0;
1210 1211 1212
		}
		if (!parse_semi (cfile)) {
			*lose = 1;
1213
			return 0;
1214
		}
1215 1216 1217 1218 1219
		if (!executable_statement_allocate
		    (result, "parse_executable_statement"))
			log_fatal ("no memory for new statement.");
		(*result) -> op = add_statement;
		(*result) -> data.add = cta;
1220 1221 1222 1223 1224 1225
		break;

	      case BREAK:
		token = next_token (&val, cfile);
		if (!parse_semi (cfile)) {
			*lose = 1;
1226
			return 0;
1227
		}
1228 1229 1230 1231
		if (!executable_statement_allocate
		    (result, "parse_executable_statement"))
			log_fatal ("no memory for new statement.");
		(*result) -> op = break_statement;
1232 1233
		break;

Ted Lemon's avatar
Ted Lemon committed
1234 1235 1236 1237
	      case SEND:
		*lose = 1;
		parse_warn ("send not appropriate here.");
		skip_to_semi (cfile);
1238
		return 0;
Ted Lemon's avatar
Ted Lemon committed
1239 1240

	      case SUPERSEDE:
1241 1242
	      case OPTION:
		token = next_token (&val, cfile);
1243
		option = parse_option_name (cfile, 0);
1244 1245
		if (!option) {
			*lose = 1;
1246
			return 0;
1247
		}
1248
		return parse_option_statement (result, cfile, 1, option,
1249 1250
					       supersede_option_statement);

1251 1252 1253 1254 1255 1256
	      case ALLOW:
	      case DENY:
		token = next_token (&val, cfile);
		cache = (struct option_cache *)0;
		if (!parse_allow_deny (&cache, cfile,
				       token == ALLOW ? 1 : 0))
1257 1258 1259 1260 1261 1262
			return 0;
		if (!executable_statement_allocate
		    (result, "parse_executable_statement"))
			log_fatal ("no memory for new statement.");
		(*result) -> op = supersede_option_statement;
		(*result) -> data.option = cache;
1263 1264
		break;

1265 1266
	      case DEFAULT:
		token = next_token (&val, cfile);
1267
		option = parse_option_name (cfile, 0);
1268 1269
		if (!option) {
			*lose = 1;
1270
			return 0;
1271
		}
1272
		return parse_option_statement (result, cfile, 1, option,
1273 1274 1275 1276
					       default_option_statement);

	      case PREPEND:
		token = next_token (&val, cfile);
1277
		option = parse_option_name (cfile, 0);
1278 1279
		if (!option) {
			*lose = 1;
1280
			return 0;
1281
		}
1282
		return parse_option_statement (result, cfile, 1, option,
1283 1284 1285 1286
					       prepend_option_statement);

	      case APPEND:
		token = next_token (&val, cfile);
1287
		option = parse_option_name (cfile, 0);
1288 1289
		if (!option) {
			*lose = 1;
1290
			return 0;
1291
		}
1292
		return parse_option_statement (result, cfile, 1, option,
1293 1294
					       append_option_statement);

1295 1296 1297 1298 1299
	      case ON:
		token = next_token (&val, cfile);

		return parse_on_statement (result, cfile, lose);
			
1300 1301
	      default:
		*lose = 0;
1302
		return 0;
1303 1304
	}

1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
	return 1;
}

/*
 * on-statement :== event-type LBRACE executable-statements RBRACE
 *
 * event-type :== EXPIRY | COMMIT | RELEASE
 */

int parse_on_statement (result, cfile, lose)
	struct executable_statement **result;
	FILE *cfile;
	int *lose;
{
	enum dhcp_token token;
	char *val;

	if (!executable_statement_allocate (result,
					    "parse_executable_statement"))
1324
		log_fatal ("no memory for new statement.");
1325
	(*result) -> op = on_statement;
1326 1327 1328 1329 1330 1331 1332 1333

	token = next_token (&val, cfile);
	switch (token) {
	      case EXPIRY:
		(*result) -> data.on.evtype = expiry;
		break;
		
	      case COMMIT:
1334
		(*result) -> data.on.evtype = commit;
1335 1336 1337
		break;
		
	      case RELEASE:
1338
		(*result) -> data.on.evtype = release;
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
		break;
		
	      default:
		parse_warn ("expecting a lease event type");
		skip_to_semi (cfile);
		*lose = 1;
		executable_statement_dereference
			(result, "parse_on_statement");
		break;
	}
		
	token = next_token (&val, cfile);
	if (token != LBRACE) {
		parse_warn ("left brace expected.");
		skip_to_semi (cfile);
		*lose = 1;
		executable_statement_dereference (result,
						  "parse_on_statement");
		return 0;
	}
	if (!parse_executable_statements (&(*result) -> data.on.statements,
					  cfile, lose)) {
		if (*lose) {
			/* Try to even things up. */
			do {
				token = next_token (&val, cfile);
			} while (token != EOF && token != RBRACE);
			executable_statement_dereference
				(result, "parse_on_statement");
			return 0;
		}
	}
	token = next_token (&val, cfile);
	if (token != RBRACE) {
		parse_warn ("right brace expected.");
		skip_to_semi (cfile);
		*lose = 1;
		executable_statement_dereference
			(result, "parse_on_statement");
		return 0;
	}
	return 1;
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
}

/*
 * if-statement :== boolean-expression LBRACE executable-statements RBRACE
 *						else-statement
 *
 * else-statement :== <null> |
 *		      ELSE LBRACE executable-statements RBRACE |
 *		      ELSE IF if-statement |
 *		      ELSIF if-statement
 */

1393 1394
int parse_if_statement (result, cfile, lose)
	struct executable_statement **result;
1395 1396 1397
	FILE *cfile;
	int *lose;
{
1398
	enum dhcp_token token;
1399 1400
	char *val;

1401 1402 1403 1404 1405 1406 1407
	if (!executable_statement_allocate (result, "parse_if_statement"))
		log_fatal ("no memory for if statement.");

	(*result) -> op = if_statement;

	if (!parse_boolean_expression (&(*result) -> data.ie.expr,
				       cfile, lose)) {
1408 1409
		if (!*lose)
			parse_warn ("boolean expression expected.");
1410 1411 1412
		executable_statement_dereference (result,
						  "parse_if_statement");
		return 0;
1413
	}
1414 1415 1416
#if defined (DEBUG_EXPRESSION_PARSE)
	print_expression ("if condition", if_condition);
#endif
1417 1418 1419 1420 1421
	token = next_token (&val, cfile);
	if (token != LBRACE) {
		parse_warn ("left brace expected.");
		skip_to_semi (cfile);
		*lose = 1;
Ted Lemon's avatar