parse.c 66.7 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.43 1999/10/06 01:00:00 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 56 57 58 59
	char *val;

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

int parse_semi (cfile)
83
	struct parse *cfile;
84
{
85
	enum dhcp_token token;
86 87 88 89
	char *val;

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

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

char *parse_string (cfile)
100
	struct parse *cfile;
101 102
{
	char *val;
103
	enum dhcp_token token;
104 105 106 107
	char *s;

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

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

122 123 124 125 126
/*
 * hostname :== IDENTIFIER
 *		| IDENTIFIER DOT
 *		| hostname DOT IDENTIFIER
 */
127 128

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

146 147
		/* Store this identifier... */
		if (!(s = (char *)malloc (strlen (val) + 1)))
148
			log_fatal ("can't allocate temp space for hostname.");
149 150 151 152 153 154 155 156 157 158 159 160
		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)))
161
		log_fatal ("can't allocate space for hostname.");
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	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;
}

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

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

	token = peek_token (&val, cfile);
	if (is_identifier (token)) {
		name = parse_host_name (cfile);
		if (!name)
202 203 204 205 206 207 208 209 210 211
			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;
		}
212 213
	} else if (token == NUMBER) {
		if (!parse_numeric_aggregate (cfile, addr, &len, DOT, 10, 8))
214 215
			return 0;
		return make_const_data (expr, addr, len, 0, 1);
216 217 218
	} else {
		if (token != RBRACE && token != LBRACE)
			token = next_token (&val, cfile);
219
		parse_warn (cfile, "%s (%d): expecting IP address or hostname",
220 221 222
			    val, token);
		if (token != SEMI)
			skip_to_semi (cfile);
223
		return 0;
224 225
	}

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

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

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

247 248 249 250
/*
 * hardware-parameter :== HARDWARE hardware-type colon-seperated-hex-list SEMI
 * hardware-type :== ETHERNET | TOKEN_RING
 */
251 252

void parse_hardware_param (cfile, hardware)
253
	struct parse *cfile;
254 255 256
	struct hardware *hardware;
{
	char *val;
257
	enum dhcp_token token;
258 259 260 261 262 263 264 265 266 267 268
	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;
269 270 271
	      case FDDI:
		hardware -> htype = HTYPE_FDDI;
		break;
272
	      default:
273
		parse_warn (cfile, "expecting a network hardware type");
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
		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);
292
		parse_warn (cfile, "hardware address too long");
293 294 295 296
	} else {
		hardware -> hlen = hlen;
		memcpy ((unsigned char *)&hardware -> haddr [0],
			t, hardware -> hlen);
297 298 299
		if (hlen < sizeof hardware -> haddr)
			memset (&hardware -> haddr [hlen], 0,
				(sizeof hardware -> haddr) - hlen);
300 301 302 303 304
		free (t);
	}
	
	token = next_token (&val, cfile);
	if (token != SEMI) {
305
		parse_warn (cfile, "expecting semicolon.");
306 307 308 309 310 311 312
		skip_to_semi (cfile);
	}
}

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

void parse_lease_time (cfile, timep)
313
	struct parse *cfile;
314 315 316
	TIME *timep;
{
	char *val;
317
	enum dhcp_token token;
318 319 320

	token = next_token (&val, cfile);
	if (token != NUMBER) {
321
		parse_warn (cfile, "Expecting numeric lease time");
322 323 324
		skip_to_semi (cfile);
		return;
	}
325
	convert_num (cfile, (unsigned char *)timep, val, 10, 32);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	/* 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)
341
	struct parse *cfile;
342 343 344 345 346 347 348
	unsigned char *buf;
	int *max;
	int seperator;
	int base;
	int size;
{
	char *val;
349
	enum dhcp_token token;
350 351 352 353 354 355 356
	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)
357 358
			log_fatal ("no space for numeric aggregate");
		s = 0;
359 360 361 362 363 364 365 366 367 368 369
	} 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);
370
				parse_warn (cfile, "too few numbers.");
371 372 373 374 375 376 377 378 379
				if (token != SEMI)
					skip_to_semi (cfile);
				return (unsigned char *)0;
			}
			token = next_token (&val, cfile);
		}
		token = next_token (&val, cfile);

		if (token == EOF) {
380
			parse_warn (cfile, "unexpected end of file");
381 382 383 384 385 386
			break;
		}

		/* Allow NUMBER_OR_NAME if base is 16. */
		if (token != NUMBER &&
		    (base != 16 || token != NUMBER_OR_NAME)) {
387
			parse_warn (cfile, "expecting numeric value.");
388 389 390 391 392 393
			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) {
394
			convert_num (cfile, s, val, base, size);
395 396 397 398
			s += size / 8;
		} else {
			t = (unsigned char *)malloc (strlen (val) + 1);
			if (!t)
399
				log_fatal ("no temp space for number.");
400 401
			strcpy ((char *)t, val);
			c = cons ((caddr_t)t, c);
402 403 404 405 406 407 408
		}
	} 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)
409
			log_fatal ("no space for numeric aggregate.");
410 411 412 413 414
		s = bufp + count - size / 8;
		*max = count;
	}
	while (c) {
		pair cdr = c -> cdr;
415
		convert_num (cfile, s, (char *)(c -> car), base, size);
416 417 418 419 420 421 422 423 424
		s -= size / 8;
		/* Free up temp space. */
		free (c -> car);
		free (c);
		c = cdr;
	}
	return bufp;
}

425 426
void convert_num (cfile, buf, str, base, size)
	struct parse *cfile;
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
	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 (cfile, "Bogus number: %s.", str);
471 472 473
			break;
		}
		if (tval >= base) {
474 475
			parse_warn (cfile,
				    "Bogus number %s: digit %d not in base %d",
476
				    str, tval, base);
477 478 479 480 481 482 483 484 485 486 487 488
			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:
489 490 491
			parse_warn (cfile,
				    "%s%o exceeds max (%d) for precision.",
				    negative ? "-" : "", val, max);
492 493
			break;
		      case 16:
494 495 496
			parse_warn (cfile,
				    "%s%x exceeds max (%d) for precision.",
				    negative ? "-" : "", val, max);
497 498
			break;
		      default:
499 500
			parse_warn (cfile,
				    "%s%u exceeds max (%d) for precision.",
501
				    negative ? "-" : "", val, max);
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
			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:
518 519
			parse_warn (cfile,
				    "Unexpected integer size: %d\n", size);
520 521 522 523 524 525 526 527 528 529 530 531 532 533
			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:
534 535
			parse_warn (cfile,
				    "Unexpected integer size: %d\n", size);
536 537 538 539 540
			break;
		}
	}
}

541 542
/*
 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER 
543 544 545
 *		NUMBER COLON NUMBER COLON NUMBER SEMI |
 *          NUMBER NUMBER SLASH NUMBER SLASH NUMBER 
 *		NUMBER COLON NUMBER COLON NUMBER NUMBER SEMI |
546
 *	    NEVER
547
 *
548 549 550 551
 * 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.
552
 */
553 554

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

565
	/* Day of week, or "never"... */
566
	token = next_token (&val, cfile);
567 568 569 570 571 572
	if (token == NEVER) {
		if (!parse_semi (cfile))
			return 0;
		return MAX_TIME;
	}

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

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

	/* 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. */
594 595 596
	year = atoi (val);
	if (year > 1900)
		year -= 1900;
597 598 599 600

	/* Slash seperating year from month... */
	token = next_token (&val, cfile);
	if (token != SLASH) {
601 602
		parse_warn (cfile,
			    "expected slash seperating year from month.");
603 604 605 606 607 608 609 610
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

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

	/* Slash seperating month from day... */
	token = next_token (&val, cfile);
	if (token != SLASH) {
621 622
		parse_warn (cfile,
			    "expected slash seperating month from day.");
623 624 625 626 627 628 629 630
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

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

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

	/* Colon seperating hour from minute... */
	token = next_token (&val, cfile);
	if (token != COLON) {
651 652
		parse_warn (cfile,
			    "expected colon seperating hour from minute.");
653 654 655 656 657 658 659 660
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

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

	/* Colon seperating minute from second... */
	token = next_token (&val, cfile);
	if (token != COLON) {
671 672
		parse_warn (cfile,
			    "expected colon seperating hour from minute.");
673 674 675 676 677 678 679 680
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Minute... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
681
		parse_warn (cfile, "numeric minute expected.");
682 683 684 685
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
686
	sec = atoi (val);
687

688 689 690 691 692 693
	token = peek_token (&val, cfile);
	if (token == NUMBER) {
		token = next_token (&val, cfile);
		tzoff = atoi (val);
	} else
		tzoff = 0;
694 695

	/* Make sure the date ends in a semicolon... */
Ted Lemon's avatar
Ted Lemon committed
696
	if (!parse_semi (cfile))
697 698 699
		return 0;

	/* Guess the time value... */
700 701 702 703
	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]
704
		       : 0) +
705 706 707 708 709
		      (mon > 1 &&		/* Leap day this year */
		       !((year - 72) & 3)) +
		      mday - 1) * 24) +		/* Day of month */
		    hour) * 60) +
		  min) * 60) + sec + tzoff;
710 711 712 713 714 715 716 717 718 719 720 721

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

723 724 725 726 727
/*
 * option-name :== IDENTIFIER |
 		   IDENTIFIER . IDENTIFIER
 */

728
struct option *parse_option_name (cfile, allocate)
729
	struct parse *cfile;
730
	int allocate;
731 732
{
	char *val;
733
	enum dhcp_token token;
734
	char *uname;
735 736 737 738 739
	struct universe *universe;
	struct option *option;

	token = next_token (&val, cfile);
	if (!is_identifier (token)) {
740 741
		parse_warn (cfile,
			    "expecting identifier after option keyword.");
742 743 744 745
		if (token != SEMI)
			skip_to_semi (cfile);
		return (struct option *)0;
	}
746 747 748 749
	uname = malloc (strlen (val) + 1);
	if (!uname)
		log_fatal ("no memory for uname information.");
	strcpy (uname, val);
750 751 752 753 754 755 756 757
	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)) {
758
			parse_warn (cfile, "expecting identifier after '.'");
759 760 761 762 763 764
			if (token != SEMI)
				skip_to_semi (cfile);
			return (struct option *)0;
		}

		/* Look up the option name hash table for the specified
765
		   uname. */
766 767
		universe = ((struct universe *)
			    hash_lookup (&universe_hash,
768
					 (unsigned char *)uname, 0));
769 770 771
		/* If it's not there, we can't parse the rest of the
		   declaration. */
		if (!universe) {
772
			parse_warn (cfile, "no option space named %s.", uname);
773 774 775 776 777 778
			skip_to_semi (cfile);
			return (struct option *)0;
		}
	} else {
		/* Use the default hash table, which contains all the
		   standard dhcp option names. */
779
		val = uname;
780 781 782 783 784 785 786 787 788
		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) {
789 790 791 792 793 794 795 796 797 798 799 800
		/* 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)
801 802
				    log_fatal ("no memory for option %s.%s",
					       universe -> name, val);
803 804 805 806 807 808 809
				strcpy (option -> name, val);
			}
			option -> universe = universe;
			option -> code = -1;
			return option;
		}
		if (val == uname)
810
			parse_warn (cfile, "no option named %s", val);
811
		else
812
			parse_warn (cfile, "no option named %s in space %s",
813
				    val, uname);
814 815 816 817 818
		skip_to_semi (cfile);
		return (struct option *)0;
	}

	/* Free the initial identifier token. */
819
	free (uname);
820 821 822
	return option;
}

823 824 825
/* IDENTIFIER SEMI */

void parse_option_space_decl (cfile)
826
	struct parse *cfile;
827 828 829 830 831 832 833 834 835
{
	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)) {
836
		parse_warn (cfile, "expecting identifier.");
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 870 871 872 873 874 875 876 877 878 879 880
		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);
}

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
/* 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)
907
	struct parse *cfile;
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
	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) {
924
		parse_warn (cfile, "expecting option code number.");
925 926 927 928 929 930 931
		skip_to_semi (cfile);
		return 0;
	}
	option -> code = atoi (val);

	token = next_token (&val, cfile);
	if (token != EQUAL) {
932
		parse_warn (cfile, "expecting \"=\"");
933 934 935 936 937 938 939 940 941
		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) {
942
			parse_warn (cfile, "expecting \"of\".");
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
			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) {
966
			parse_warn (cfile, "expecting number.");
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
			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:
983 984
			parse_warn (cfile,
				    "%s bit precision is not supported.", val);
985 986 987 988 989 990 991 992 993 994 995
			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) {
996
			parse_warn (cfile, "expecting \"integer\" keyword.");
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
			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) {
1014
			parse_warn (cfile, "arrays of text strings not %s",
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
				    "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:
1028
		parse_warn (cfile, "unknown data type %s", val);
1029 1030 1031 1032 1033 1034 1035
		skip_to_rbrace (cfile, recordp);
		if (recordp)
			skip_to_semi (cfile);
		return 0;
	}

	if (tokix == sizeof tokbuf) {
1036
		parse_warn (cfile, "too many types in record.");
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
		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) {
1048 1049
				parse_warn (cfile,
					    "%s must be at end of record.",
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
					    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) {
1060
			parse_warn (cfile, "expecting right brace.");
1061 1062 1063 1064 1065 1066 1067
			skip_to_rbrace (cfile, 1);
			if (recordp)
				skip_to_semi (cfile);
			return 0;
		}
	}
	if (!parse_semi (cfile)) {
1068
		parse_warn (cfile, "semicolon expected.");
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
		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;
}

1092 1093 1094 1095 1096
/*
 * colon-seperated-hex-list :== NUMBER |
 *				NUMBER COLON colon-seperated-hex-list
 */

1097 1098
int parse_cshl (data, cfile)
	struct data_string *data;
1099
	struct parse *cfile;
1100
{
Ted Lemon's avatar
Ted Lemon committed
1101
	u_int8_t ibuf [128];
1102 1103 1104 1105
	int ilen = 0;
	int tlen = 0;
	struct option_tag *sl = (struct option_tag *)0;
	struct option_tag *next, **last = &sl;
1106
	enum dhcp_token token;
1107
	char *val;
1108
	unsigned char *rvp;
1109 1110 1111 1112

	do {
		token = next_token (&val, cfile);
		if (token != NUMBER && token != NUMBER_OR_NAME) {
1113
			parse_warn (cfile, "expecting hexadecimal number.");
1114 1115 1116 1117 1118
			skip_to_semi (cfile);
			for (; sl; sl = next) {
				next = sl -> next;
				dfree (sl, "parse_cshl");
			}
1119
			return 0;
1120 1121 1122 1123 1124 1125 1126
		}
		if (ilen == sizeof ibuf) {
			next = (struct option_tag *)
				dmalloc (ilen - 1 +
					 sizeof (struct option_tag),
					 "parse_cshl");
			if (!next)
1127
				log_fatal ("no memory for string list.");
1128 1129 1130 1131 1132 1133
			memcpy (next -> data, ibuf, ilen);
			*last = next;
			last = &next -> next;
			tlen += ilen;
			ilen = 0;
		}
1134
		convert_num (cfile, &ibuf [ilen++], val, 16, 8);
1135 1136 1137 1138 1139 1140 1141

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

1142
	if (!buffer_allocate (&data -> buffer, tlen + ilen, "parse_cshl"))
1143
		log_fatal ("no memory to store octet data.");
1144 1145 1146 1147 1148
	data -> data = &data -> buffer -> data [0];
	data -> len = tlen + ilen;
	data -> terminated = 0;

	rvp = &data -> data [0];
1149 1150 1151 1152 1153 1154 1155 1156 1157
	while (sl) {
		next = sl -> next;
		memcpy (rvp, sl -> data, sizeof ibuf);
		rvp += sizeof ibuf;
		dfree (sl, "parse_cshl");
		sl = next;
	}
	
	memcpy (rvp, ibuf, ilen);
1158
	return 1;
1159
}
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174

/*
 * 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
 */

1175 1176
int parse_executable_statements (statements, cfile, lose)
	struct executable_statement **statements;
1177
	struct parse *cfile;
1178 1179
	int *lose;
{
1180
	struct executable_statement **next;
1181

1182 1183
	next = statements;
	while (parse_executable_statement (next, cfile, lose))
1184
		next = &((*next) -> next);
1185
	if (!*lose)
1186 1187
		return 1;
	return 0;
1188 1189
}

1190 1191
int parse_executable_statement (result, cfile, lose)
	struct executable_statement **result;
1192
	struct parse *cfile;
1193 1194
	int *lose;
{
1195
	enum dhcp_token token;
1196
	char *val;
1197
	struct executable_statement base;
1198 1199
	struct class *cta;
	struct option *option;
1200
	struct option_cache *cache;
1201

1202 1203
	token = peek_token (&val, cfile);
	switch (token) {
1204
	      case IF:
1205
		next_token (&val, cfile);
1206 1207
		return parse_if_statement (result, cfile, lose);

Ted Lemon's avatar
Ted Lemon committed
1208
	      case TOKEN_ADD:
1209
		token = next_token (&val, cfile);
1210 1211
		token = next_token (&val, cfile);
		if (token != STRING) {
1212
			parse_warn (cfile, "expecting class name.");
1213 1214
			skip_to_semi (cfile);
			*lose = 1;
1215
			return 0;
1216 1217 1218
		}
		cta = find_class (val);
		if (!cta) {