parse.c 105 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
 * Copyright (c) 1995-2000 Internet Software Consortium.
 * All rights reserved.
8
 *
Ted Lemon's avatar
Ted Lemon committed
9 10 11
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
12
 *
Ted Lemon's avatar
Ted Lemon committed
13 14 15 16 17 18 19 20
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of The Internet Software Consortium nor the names
 *    of its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
21
 *
Ted Lemon's avatar
Ted Lemon committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * This software has been written for the Internet Software Consortium
 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
 * To learn more about the Internet Software Consortium, see
 * ``http://www.isc.org/''.  To learn more about Vixie Enterprises,
 * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
 * ``http://www.nominum.com''.
42 43 44 45
 */

#ifndef lint
static char copyright[] =
46
"$Id: parse.c,v 1.89 2000/10/10 22:45:25 mellon Exp $ Copyright (c) 1995-2000 The Internet Software Consortium.  All rights reserved.\n";
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#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)
66
	struct parse *cfile;
67 68 69 70 71
{
	skip_to_rbrace (cfile, 0);
}

void skip_to_rbrace (cfile, brace_count)
72
	struct parse *cfile;
73
	int brace_count;
74
{
75
	enum dhcp_token token;
76
	const char *val;
77

78 79 80
#if defined (DEBUG_TOKEN)
	log_error ("skip_to_rbrace: %d\n", brace_count);
#endif
81 82 83
	do {
		token = peek_token (&val, cfile);
		if (token == RBRACE) {
84
			token = next_token (&val, cfile);
85 86 87 88 89 90 91 92 93 94
			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;
95 96 97 98 99 100
		} 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;
101 102 103 104 105 106
		}
		token = next_token (&val, cfile);
	} while (token != EOF);
}

int parse_semi (cfile)
107
	struct parse *cfile;
108
{
109
	enum dhcp_token token;
110
	const char *val;
111 112 113

	token = next_token (&val, cfile);
	if (token != SEMI) {
114
		parse_warn (cfile, "semicolon expected.");
115 116 117 118 119 120 121 122 123
		skip_to_semi (cfile);
		return 0;
	}
	return 1;
}

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

char *parse_string (cfile)
124
	struct parse *cfile;
125
{
126
	const char *val;
127
	enum dhcp_token token;
128 129 130 131
	char *s;

	token = next_token (&val, cfile);
	if (token != STRING) {
132
		parse_warn (cfile, "filename must be a string");
133 134 135
		skip_to_semi (cfile);
		return (char *)0;
	}
Ted Lemon's avatar
Ted Lemon committed
136
	s = (char *)dmalloc (strlen (val) + 1, MDL);
137
	if (!s)
138
		log_fatal ("no memory for string %s.", val);
139 140 141 142 143 144 145
	strcpy (s, val);

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

146 147 148 149 150
/*
 * hostname :== IDENTIFIER
 *		| IDENTIFIER DOT
 *		| hostname DOT IDENTIFIER
 */
151 152

char *parse_host_name (cfile)
153
	struct parse *cfile;
154
{
155
	const char *val;
156
	enum dhcp_token token;
157
	unsigned len = 0;
158 159 160
	char *s;
	char *t;
	pair c = (pair)0;
161
	int ltid = 0;
162 163 164 165
	
	/* Read a dotted hostname... */
	do {
		/* Read a token, which should be an identifier. */
Ted Lemon's avatar
Ted Lemon committed
166 167 168
		token = peek_token (&val, cfile);
		if (!is_identifier (token) && token != NUMBER)
			break;
169
		token = next_token (&val, cfile);
Ted Lemon's avatar
Ted Lemon committed
170

171
		/* Store this identifier... */
Ted Lemon's avatar
Ted Lemon committed
172
		if (!(s = (char *)dmalloc (strlen (val) + 1, MDL)))
173
			log_fatal ("can't allocate temp space for hostname.");
174 175 176 177 178 179
		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);
180
		if (token == DOT) {
181
			token = next_token (&val, cfile);
182 183 184
			ltid = 1;
		} else
			ltid = 0;
185 186
	} while (token == DOT);

Ted Lemon's avatar
Ted Lemon committed
187 188 189 190
	/* Should be at least one token. */
	if (!len)
		return (char *)0;

191
	/* Assemble the hostname together into a string. */
192
	if (!(s = (char *)dmalloc (len + ltid, MDL)))
193
		log_fatal ("can't allocate space for hostname.");
194
	t = s + len + ltid;
195
	*--t = 0;
196 197
	if (ltid)
		*--t = '.';
198 199
	while (c) {
		pair cdr = c -> cdr;
200
		unsigned l = strlen ((char *)(c -> car));
201 202 203
		t -= l;
		memcpy (t, (char *)(c -> car), l);
		/* Free up temp space. */
Ted Lemon's avatar
Ted Lemon committed
204 205
		dfree (c -> car, MDL);
		dfree (c, MDL);
206 207 208 209 210 211 212
		c = cdr;
		if (t != s)
			*--t = '.';
	}
	return s;
}

213 214 215 216 217 218 219
/* 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. */

220 221
int parse_ip_addr_or_hostname (expr, cfile, uniform)
	struct expression **expr;
222
	struct parse *cfile;
223 224
	int uniform;
{
225
	const char *val;
226
	enum dhcp_token token;
227
	unsigned char addr [4];
228
	unsigned len = sizeof addr;
229
	char *name;
230
	struct expression *x = (struct expression *)0;
231 232 233 234 235

	token = peek_token (&val, cfile);
	if (is_identifier (token)) {
		name = parse_host_name (cfile);
		if (!name)
236 237 238 239 240 241
			return 0;
		if (!make_host_lookup (expr, name))
			return 0;
		if (!uniform) {
			if (!make_limit (&x, *expr, 4))
				return 0;
242
			expression_dereference (expr, MDL);
243 244
			*expr = x;
		}
245 246
	} else if (token == NUMBER) {
		if (!parse_numeric_aggregate (cfile, addr, &len, DOT, 10, 8))
247 248
			return 0;
		return make_const_data (expr, addr, len, 0, 1);
249 250 251
	} else {
		if (token != RBRACE && token != LBRACE)
			token = next_token (&val, cfile);
252
		parse_warn (cfile, "%s (%d): expecting IP address or hostname",
253 254 255
			    val, token);
		if (token != SEMI)
			skip_to_semi (cfile);
256
		return 0;
257 258
	}

259
	return 1;
260 261 262 263 264 265
}	
	
/*
 * ip-address :== NUMBER DOT NUMBER DOT NUMBER DOT NUMBER
 */

266
int parse_ip_addr (cfile, addr)
267
	struct parse *cfile;
268 269
	struct iaddr *addr;
{
270
	const char *val;
271
	enum dhcp_token token;
272 273 274 275 276 277 278 279

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

280 281 282 283
/*
 * hardware-parameter :== HARDWARE hardware-type colon-seperated-hex-list SEMI
 * hardware-type :== ETHERNET | TOKEN_RING
 */
284 285

void parse_hardware_param (cfile, hardware)
286
	struct parse *cfile;
287 288
	struct hardware *hardware;
{
289
	const char *val;
290
	enum dhcp_token token;
291
	unsigned hlen;
292 293 294 295 296
	unsigned char *t;

	token = next_token (&val, cfile);
	switch (token) {
	      case ETHERNET:
297
		hardware -> hbuf [0] = HTYPE_ETHER;
298 299
		break;
	      case TOKEN_RING:
300
		hardware -> hbuf [0] = HTYPE_IEEE802;
301
		break;
302
	      case FDDI:
303
		hardware -> hbuf [0] = HTYPE_FDDI;
304
		break;
305
	      default:
306
		if (!strncmp (val, "unknown-", 8)) {
307
			hardware -> hbuf [0] = atoi (&val [8]);
308 309 310 311 312 313 314
		} else {
			parse_warn (cfile,
				    "expecting a network hardware type");
			skip_to_semi (cfile);

			return;
		}
315 316 317 318 319 320 321 322 323 324 325 326 327 328
	}

	/* 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;
329
	if (hlen + 1 > sizeof hardware -> hbuf) {
Ted Lemon's avatar
Ted Lemon committed
330
		dfree (t, MDL);
331
		parse_warn (cfile, "hardware address too long");
332
	} else {
333 334 335 336 337
		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
338
		dfree (t, MDL);
339 340 341 342
	}
	
	token = next_token (&val, cfile);
	if (token != SEMI) {
343
		parse_warn (cfile, "expecting semicolon.");
344 345 346 347 348 349 350
		skip_to_semi (cfile);
	}
}

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

void parse_lease_time (cfile, timep)
351
	struct parse *cfile;
352 353
	TIME *timep;
{
354
	const char *val;
355
	enum dhcp_token token;
356 357 358

	token = next_token (&val, cfile);
	if (token != NUMBER) {
359
		parse_warn (cfile, "Expecting numeric lease time");
360 361 362
		skip_to_semi (cfile);
		return;
	}
363
	convert_num (cfile, (unsigned char *)timep, val, 10, 32);
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
	/* 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)
379
	struct parse *cfile;
380
	unsigned char *buf;
381
	unsigned *max;
382 383
	int seperator;
	int base;
384
	unsigned size;
385
{
386
	const char *val;
387
	enum dhcp_token token;
388
	unsigned char *bufp = buf, *s, *t;
389
	unsigned count = 0;
390 391 392
	pair c = (pair)0;

	if (!bufp && *max) {
Ted Lemon's avatar
Ted Lemon committed
393
		bufp = (unsigned char *)dmalloc (*max * size / 8, MDL);
394
		if (!bufp)
395 396
			log_fatal ("no space for numeric aggregate");
		s = 0;
397 398 399 400 401 402 403 404 405 406 407
	} 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);
408
				parse_warn (cfile, "too few numbers.");
409 410 411 412 413 414 415 416 417
				if (token != SEMI)
					skip_to_semi (cfile);
				return (unsigned char *)0;
			}
			token = next_token (&val, cfile);
		}
		token = next_token (&val, cfile);

		if (token == EOF) {
418
			parse_warn (cfile, "unexpected end of file");
419 420 421 422 423 424
			break;
		}

		/* Allow NUMBER_OR_NAME if base is 16. */
		if (token != NUMBER &&
		    (base != 16 || token != NUMBER_OR_NAME)) {
425
			parse_warn (cfile, "expecting numeric value.");
426 427 428 429 430 431
			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) {
432
			convert_num (cfile, s, val, base, size);
433 434
			s += size / 8;
		} else {
Ted Lemon's avatar
Ted Lemon committed
435
			t = (unsigned char *)dmalloc (strlen (val) + 1, MDL);
436
			if (!t)
437
				log_fatal ("no temp space for number.");
438 439
			strcpy ((char *)t, val);
			c = cons ((caddr_t)t, c);
440 441 442 443 444
		}
	} while (++count != *max);

	/* If we had to cons up a list, convert it now. */
	if (c) {
Ted Lemon's avatar
Ted Lemon committed
445
		bufp = (unsigned char *)dmalloc (count * size / 8, MDL);
446
		if (!bufp)
447
			log_fatal ("no space for numeric aggregate.");
448 449 450 451 452
		s = bufp + count - size / 8;
		*max = count;
	}
	while (c) {
		pair cdr = c -> cdr;
453
		convert_num (cfile, s, (char *)(c -> car), base, size);
454 455
		s -= size / 8;
		/* Free up temp space. */
Ted Lemon's avatar
Ted Lemon committed
456 457
		dfree (c -> car, MDL);
		dfree (c, MDL);
458 459 460 461 462
		c = cdr;
	}
	return bufp;
}

463 464
void convert_num (cfile, buf, str, base, size)
	struct parse *cfile;
465
	unsigned char *buf;
466
	const char *str;
467
	int base;
468
	unsigned size;
469
{
470
	const char *ptr = str;
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
	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 {
508
			parse_warn (cfile, "Bogus number: %s.", str);
509 510 511
			break;
		}
		if (tval >= base) {
512 513
			parse_warn (cfile,
				    "Bogus number %s: digit %d not in base %d",
514
				    str, tval, base);
515 516 517 518 519 520 521 522 523 524 525 526
			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:
527
			parse_warn (cfile,
528
				    "%s%lo exceeds max (%d) for precision.",
Ted Lemon's avatar
Ted Lemon committed
529 530
				    negative ? "-" : "",
				    (unsigned long)val, max);
531 532
			break;
		      case 16:
533
			parse_warn (cfile,
534
				    "%s%lx exceeds max (%d) for precision.",
Ted Lemon's avatar
Ted Lemon committed
535 536
				    negative ? "-" : "",
				    (unsigned long)val, max);
537 538
			break;
		      default:
539
			parse_warn (cfile,
540
				    "%s%lu exceeds max (%d) for precision.",
Ted Lemon's avatar
Ted Lemon committed
541 542
				    negative ? "-" : "",
				    (unsigned long)val, max);
543 544 545 546 547 548 549 550 551 552
			break;
		}
	}

	if (negative) {
		switch (size) {
		      case 8:
			*buf = -(unsigned long)val;
			break;
		      case 16:
553
			putShort (buf, -(long)val);
554 555
			break;
		      case 32:
556
			putLong (buf, -(long)val);
557 558
			break;
		      default:
559 560
			parse_warn (cfile,
				    "Unexpected integer size: %d\n", size);
561 562 563 564 565 566 567 568 569 570 571 572 573 574
			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:
575 576
			parse_warn (cfile,
				    "Unexpected integer size: %d\n", size);
577 578 579 580 581
			break;
		}
	}
}

582 583
/*
 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER 
584 585 586
 *		NUMBER COLON NUMBER COLON NUMBER SEMI |
 *          NUMBER NUMBER SLASH NUMBER SLASH NUMBER 
 *		NUMBER COLON NUMBER COLON NUMBER NUMBER SEMI |
587
 *	    NEVER
588
 *
589 590 591 592
 * 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.
593
 */
594 595

TIME parse_date (cfile)
596
	struct parse *cfile;
597 598 599
{
	struct tm tm;
	int guess;
600
	int tzoff, wday, year, mon, mday, hour, min, sec;
601
	const char *val;
602
	enum dhcp_token token;
603 604 605
	static int months [11] = { 31, 59, 90, 120, 151, 181,
					  212, 243, 273, 304, 334 };

606
	/* Day of week, or "never"... */
607
	token = next_token (&val, cfile);
608 609 610 611 612 613
	if (token == NEVER) {
		if (!parse_semi (cfile))
			return 0;
		return MAX_TIME;
	}

614
	if (token != NUMBER) {
615
		parse_warn (cfile, "numeric day of week expected.");
616 617 618 619
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
620
	wday = atoi (val);
621 622 623 624

	/* Year... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
625
		parse_warn (cfile, "numeric year expected.");
626 627 628 629
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
630 631 632 633 634

	/* 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. */
635 636 637
	year = atoi (val);
	if (year > 1900)
		year -= 1900;
638 639 640 641

	/* Slash seperating year from month... */
	token = next_token (&val, cfile);
	if (token != SLASH) {
642 643
		parse_warn (cfile,
			    "expected slash seperating year from month.");
644 645 646 647 648 649 650 651
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Month... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
652
		parse_warn (cfile, "numeric month expected.");
653 654 655 656
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
657
	mon = atoi (val) - 1;
658 659 660 661

	/* Slash seperating month from day... */
	token = next_token (&val, cfile);
	if (token != SLASH) {
662 663
		parse_warn (cfile,
			    "expected slash seperating month from day.");
664 665 666 667 668 669 670 671
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Month... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
672
		parse_warn (cfile, "numeric day of month expected.");
673 674 675 676
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
677
	mday = atoi (val);
678 679 680 681

	/* Hour... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
682
		parse_warn (cfile, "numeric hour expected.");
683 684 685 686
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
687
	hour = atoi (val);
688 689 690 691

	/* Colon seperating hour from minute... */
	token = next_token (&val, cfile);
	if (token != COLON) {
692 693
		parse_warn (cfile,
			    "expected colon seperating hour from minute.");
694 695 696 697 698 699 700 701
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Minute... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
702
		parse_warn (cfile, "numeric minute expected.");
703 704 705 706
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
707
	min = atoi (val);
708 709 710 711

	/* Colon seperating minute from second... */
	token = next_token (&val, cfile);
	if (token != COLON) {
712 713
		parse_warn (cfile,
			    "expected colon seperating hour from minute.");
714 715 716 717 718 719 720 721
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Minute... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
722
		parse_warn (cfile, "numeric minute expected.");
723 724 725 726
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
727
	sec = atoi (val);
728

729 730 731 732 733 734
	token = peek_token (&val, cfile);
	if (token == NUMBER) {
		token = next_token (&val, cfile);
		tzoff = atoi (val);
	} else
		tzoff = 0;
735 736

	/* Make sure the date ends in a semicolon... */
Ted Lemon's avatar
Ted Lemon committed
737
	if (!parse_semi (cfile))
738 739 740
		return 0;

	/* Guess the time value... */
741 742 743 744
	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]
745
		       : 0) +
746 747 748 749 750
		      (mon > 1 &&		/* Leap day this year */
		       !((year - 72) & 3)) +
		      mday - 1) * 24) +		/* Day of month */
		    hour) * 60) +
		  min) * 60) + sec + tzoff;
751 752 753 754 755 756 757 758 759 760 761 762

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

764 765 766 767 768
/*
 * option-name :== IDENTIFIER |
 		   IDENTIFIER . IDENTIFIER
 */

769
struct option *parse_option_name (cfile, allocate, known)
770
	struct parse *cfile;
771
	int allocate;
772
	int *known;
773
{
774
	const char *val;
775
	enum dhcp_token token;
776
	char *uname;
777 778 779 780 781
	struct universe *universe;
	struct option *option;

	token = next_token (&val, cfile);
	if (!is_identifier (token)) {
782 783
		parse_warn (cfile,
			    "expecting identifier after option keyword.");
784 785 786 787
		if (token != SEMI)
			skip_to_semi (cfile);
		return (struct option *)0;
	}
Ted Lemon's avatar
Ted Lemon committed
788
	uname = dmalloc (strlen (val) + 1, MDL);
789 790 791
	if (!uname)
		log_fatal ("no memory for uname information.");
	strcpy (uname, val);
792 793 794 795 796 797 798 799
	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)) {
800
			parse_warn (cfile, "expecting identifier after '.'");
801 802 803 804 805 806
			if (token != SEMI)
				skip_to_semi (cfile);
			return (struct option *)0;
		}

		/* Look up the option name hash table for the specified
807
		   uname. */
808 809 810
		universe = (struct universe *)0;
		if (!universe_hash_lookup (&universe, universe_hash,
					   uname, 0, MDL)) {
811
			parse_warn (cfile, "no option space named %s.", uname);
812 813 814 815 816 817
			skip_to_semi (cfile);
			return (struct option *)0;
		}
	} else {
		/* Use the default hash table, which contains all the
		   standard dhcp option names. */
818
		val = uname;
819 820 821 822
		universe = &dhcp_universe;
	}

	/* Look up the actual option info... */
823 824
	option = (struct option *)0;
	option_hash_lookup (&option, universe -> hash, val, 0, MDL);
825 826

	/* If we didn't get an option structure, it's an undefined option. */
827 828 829
	if (option) {
		*known = 1;
	} else {
830 831 832 833
		/* 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) {
834
			option = new_option (MDL);
835 836 837
			if (val == uname)
				option -> name = val;
			else {
838
				char *s;
Ted Lemon's avatar
Ted Lemon committed
839 840
				dfree (uname, MDL);
				s = dmalloc (strlen (val) + 1, MDL);
841
				if (!s)
842 843
				    log_fatal ("no memory for option %s.%s",
					       universe -> name, val);
844 845
				strcpy (s, val);
				option -> name = s;
846 847
			}
			option -> universe = universe;
848
			option -> code = 0;
849 850 851
			return option;
		}
		if (val == uname)
852
			parse_warn (cfile, "no option named %s", val);
853
		else
854
			parse_warn (cfile, "no option named %s in space %s",
855
				    val, uname);
856 857 858 859 860
		skip_to_semi (cfile);
		return (struct option *)0;
	}

	/* Free the initial identifier token. */
Ted Lemon's avatar
Ted Lemon committed
861
	dfree (uname, MDL);
862 863 864
	return option;
}

865 866 867
/* IDENTIFIER SEMI */

void parse_option_space_decl (cfile)
868
	struct parse *cfile;
869 870
{
	int token;
871
	const char *val;
872
	struct universe **ua, *nu;
873
	char *s;
874 875 876 877 878

	next_token (&val, cfile);	/* Discard the SPACE token, which was
					   checked by the caller. */
	token = next_token (&val, cfile);
	if (!is_identifier (token)) {
879
		parse_warn (cfile, "expecting identifier.");
880 881 882
		skip_to_semi (cfile);
		return;
	}
883
	nu = new_universe (MDL);
884 885 886 887
	if (!nu)
		log_fatal ("No memory for new option space.");

	/* Set up the server option universe... */
Ted Lemon's avatar
Ted Lemon committed
888
	s = dmalloc (strlen (val) + 1, MDL);
889
	if (!s)
890
		log_fatal ("No memory for new option space name.");
891 892
	strcpy (s, val);
	nu -> name = s;
893 894 895 896 897 898
	nu -> lookup_func = lookup_hashed_option;
	nu -> option_state_dereference =
		hashed_option_state_dereference;
	nu -> save_func = save_hashed_option;
	nu -> delete_func = delete_hashed_option;
	nu -> encapsulate = hashed_option_space_encapsulate;
899
	nu -> decode = parse_option_buffer;
900 901 902 903 904 905
	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
906
		ua = dmalloc (universe_max * 2 * sizeof *ua, MDL);
907 908 909 910
		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
911
		dfree (universes, MDL);
912 913 914
		universes = ua;
	}
	universes [nu -> index] = nu;
915
	nu -> hash = new_hash (0, 0, 1);
916 917
	if (!nu -> hash)
		log_fatal ("Can't allocate %s option hash table.", nu -> name);
918
	universe_hash_add (universe_hash, nu -> name, 0, nu, MDL);
919 920 921
	parse_semi (cfile);
}

922 923 924 925 926 927 928 929 930
/* 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 |
931
	    ARRAY OF ocsd_simple_type_sequence
932 933 934

   ocsd_type_sequence :== LBRACE ocsd_types RBRACE

935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
   ocsd_simple_type_sequence :== LBRACE ocsd_simple_types RBRACE

   ocsd_types :== ocsd_type |
		  ocsd_types ocsd_type

   ocsd_type :== ocsd_simple_type |
		 ARRAY OF ocsd_simple_type

   ocsd_simple_types :== ocsd_simple_type |
			 ocsd_simple_types ocsd_simple_type

   ocsd_simple_type :== BOOLEAN |
			INTEGER NUMBER |
			SIGNED INTEGER NUMBER |
			UNSIGNED INTEGER NUMBER |
			IP-ADDRESS |
			TEXT |
952 953
			STRING |
			ENCAPSULATE identifier */
954 955

int parse_option_code_definition (cfile, option)
956
	struct parse *cfile;
957 958
	struct option *option;
{
959
	const char *val;
960
	enum dhcp_token token;
961
	unsigned arrayp = 0;
962 963 964
	int recordp = 0;
	int no_more_in_record = 0;
	char tokbuf [128];
965
	unsigned tokix = 0;
966 967 968
	char type;
	int code;
	int is_signed;
969
	char *s;
970
	int has_encapsulation = 0;
971 972 973 974
	
	/* Parse the option code. */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
975
		parse_warn (cfile, "expecting option code number.");
976 977 978 979 980 981 982
		skip_to_semi (cfile);
		return 0;
	}
	option -> code = atoi (val);

	token = next_token (&val, cfile);
	if (token != EQUAL) {
983
		parse_warn (cfile, "expecting \"=\"");
984 985 986 987 988 989 990 991 992
		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) {
993
			parse_warn (cfile, "expecting \"of\".");
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
			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:
1008 1009 1010 1011 1012 1013 1014
	if (has_encapsulation) {
		parse_warn (cfile,
			    "encapsulate must always be the last item.");
		skip_to_semi (cfile);
		return 0;
	}

1015
	switch (token) {
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
	      case ARRAY:
		if (arrayp) {
			parse_warn (cfile, "no nested arrays.");
			skip_to_rbrace (cfile, recordp);
			if (recordp)
				skip_to_semi (cfile);
			return 0;
		}
		token = next_token (&val, cfile);
		if (token != OF) {
			parse_warn (cfile, "expecting \"of\".");
			skip_to_semi (cfile);
			return 0;
		}
		arrayp = recordp + 1;
		token = next_token (