parse.c 101 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.86 2000/09/27 19:13:57 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
		parse_warn (cfile, "expecting a network hardware type");
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
		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;
323
	if (hlen + 1 > sizeof hardware -> hbuf) {
Ted Lemon's avatar
Ted Lemon committed
324
		dfree (t, MDL);
325
		parse_warn (cfile, "hardware address too long");
326
	} else {
327 328 329 330 331
		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
332
		dfree (t, MDL);
333 334 335 336
	}
	
	token = next_token (&val, cfile);
	if (token != SEMI) {
337
		parse_warn (cfile, "expecting semicolon.");
338 339 340 341 342 343 344
		skip_to_semi (cfile);
	}
}

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

void parse_lease_time (cfile, timep)
345
	struct parse *cfile;
346 347
	TIME *timep;
{
348
	const char *val;
349
	enum dhcp_token token;
350 351 352

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

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

		if (token == EOF) {
412
			parse_warn (cfile, "unexpected end of file");
413 414 415 416 417 418
			break;
		}

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

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

457 458
void convert_num (cfile, buf, str, base, size)
	struct parse *cfile;
459
	unsigned char *buf;
460
	const char *str;
461
	int base;
462
	unsigned size;
463
{
464
	const char *ptr = str;
465 466 467 468 469 470 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
	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 {
502
			parse_warn (cfile, "Bogus number: %s.", str);
503 504 505
			break;
		}
		if (tval >= base) {
506 507
			parse_warn (cfile,
				    "Bogus number %s: digit %d not in base %d",
508
				    str, tval, base);
509 510 511 512 513 514 515 516 517 518 519 520
			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:
521
			parse_warn (cfile,
522
				    "%s%lo exceeds max (%d) for precision.",
Ted Lemon's avatar
Ted Lemon committed
523 524
				    negative ? "-" : "",
				    (unsigned long)val, max);
525 526
			break;
		      case 16:
527
			parse_warn (cfile,
528
				    "%s%lx exceeds max (%d) for precision.",
Ted Lemon's avatar
Ted Lemon committed
529 530
				    negative ? "-" : "",
				    (unsigned long)val, max);
531 532
			break;
		      default:
533
			parse_warn (cfile,
534
				    "%s%lu exceeds max (%d) for precision.",
Ted Lemon's avatar
Ted Lemon committed
535 536
				    negative ? "-" : "",
				    (unsigned long)val, max);
537 538 539 540 541 542 543 544 545 546
			break;
		}
	}

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

576 577
/*
 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER 
578 579 580
 *		NUMBER COLON NUMBER COLON NUMBER SEMI |
 *          NUMBER NUMBER SLASH NUMBER SLASH NUMBER 
 *		NUMBER COLON NUMBER COLON NUMBER NUMBER SEMI |
581
 *	    NEVER
582
 *
583 584 585 586
 * 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.
587
 */
588 589

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

600
	/* Day of week, or "never"... */
601
	token = next_token (&val, cfile);
602 603 604 605 606 607
	if (token == NEVER) {
		if (!parse_semi (cfile))
			return 0;
		return MAX_TIME;
	}

608
	if (token != NUMBER) {
609
		parse_warn (cfile, "numeric day of week expected.");
610 611 612 613
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
614
	wday = atoi (val);
615 616 617 618

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

	/* 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. */
629 630 631
	year = atoi (val);
	if (year > 1900)
		year -= 1900;
632 633 634 635

	/* Slash seperating year from month... */
	token = next_token (&val, cfile);
	if (token != SLASH) {
636 637
		parse_warn (cfile,
			    "expected slash seperating year from month.");
638 639 640 641 642 643 644 645
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Month... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
646
		parse_warn (cfile, "numeric month expected.");
647 648 649 650
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
651
	mon = atoi (val) - 1;
652 653 654 655

	/* Slash seperating month from day... */
	token = next_token (&val, cfile);
	if (token != SLASH) {
656 657
		parse_warn (cfile,
			    "expected slash seperating month from day.");
658 659 660 661 662 663 664 665
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Month... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
666
		parse_warn (cfile, "numeric day of month expected.");
667 668 669 670
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
671
	mday = atoi (val);
672 673 674 675

	/* Hour... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
676
		parse_warn (cfile, "numeric hour expected.");
677 678 679 680
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
681
	hour = atoi (val);
682 683 684 685

	/* Colon seperating hour from minute... */
	token = next_token (&val, cfile);
	if (token != COLON) {
686 687
		parse_warn (cfile,
			    "expected colon seperating hour from minute.");
688 689 690 691 692 693 694 695
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Minute... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
696
		parse_warn (cfile, "numeric minute expected.");
697 698 699 700
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
701
	min = atoi (val);
702 703 704 705

	/* Colon seperating minute from second... */
	token = next_token (&val, cfile);
	if (token != COLON) {
706 707
		parse_warn (cfile,
			    "expected colon seperating hour from minute.");
708 709 710 711 712 713 714 715
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}

	/* Minute... */
	token = next_token (&val, cfile);
	if (token != NUMBER) {
716
		parse_warn (cfile, "numeric minute expected.");
717 718 719 720
		if (token != SEMI)
			skip_to_semi (cfile);
		return (TIME)0;
	}
721
	sec = atoi (val);
722

723 724 725 726 727 728
	token = peek_token (&val, cfile);
	if (token == NUMBER) {
		token = next_token (&val, cfile);
		tzoff = atoi (val);
	} else
		tzoff = 0;
729 730

	/* Make sure the date ends in a semicolon... */
Ted Lemon's avatar
Ted Lemon committed
731
	if (!parse_semi (cfile))
732 733 734
		return 0;

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

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

758 759 760 761 762
/*
 * option-name :== IDENTIFIER |
 		   IDENTIFIER . IDENTIFIER
 */

763
struct option *parse_option_name (cfile, allocate, known)
764
	struct parse *cfile;
765
	int allocate;
766
	int *known;
767
{
768
	const char *val;
769
	enum dhcp_token token;
770
	char *uname;
771 772 773 774 775
	struct universe *universe;
	struct option *option;

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

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

	/* Look up the actual option info... */
817 818
	option = (struct option *)0;
	option_hash_lookup (&option, universe -> hash, val, 0, MDL);
819 820

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

	/* Free the initial identifier token. */
Ted Lemon's avatar
Ted Lemon committed
855
	dfree (uname, MDL);
856 857 858
	return option;
}

859 860 861
/* IDENTIFIER SEMI */

void parse_option_space_decl (cfile)
862
	struct parse *cfile;
863 864
{
	int token;
865
	const char *val;
866
	struct universe **ua, *nu;
867
	char *s;
868 869 870 871 872

	next_token (&val, cfile);	/* Discard the SPACE token, which was
					   checked by the caller. */
	token = next_token (&val, cfile);
	if (!is_identifier (token)) {
873
		parse_warn (cfile, "expecting identifier.");
874 875 876
		skip_to_semi (cfile);
		return;
	}
877
	nu = new_universe (MDL);
878 879 880 881
	if (!nu)
		log_fatal ("No memory for new option space.");

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

917 918 919 920 921 922 923 924 925
/* 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 |
926
	    ARRAY OF ocsd_simple_type_sequence
927 928 929

   ocsd_type_sequence :== LBRACE ocsd_types RBRACE

930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
   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 |
			STRING */
948 949

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

	token = next_token (&val, cfile);
	if (token != EQUAL) {
976
		parse_warn (cfile, "expecting \"=\"");
977 978 979 980 981 982 983 984 985
		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) {
986
			parse_warn (cfile, "expecting \"of\".");
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
			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) {
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
	      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 (&val, cfile);
		if ((recordp) && (token == LBRACE)) {
			parse_warn (cfile,
				    "only uniform array inside record.");
			skip_to_rbrace (cfile, recordp + 1);
			skip_to_semi (cfile);
			return 0;
		}
		goto next_type;
1026 1027 1028 1029 1030 1031 1032 1033
	      case BOOLEAN:
		type = 'f';
		break;
	      case INTEGER:
		is_signed = 1;
	      parse_integer:
		token = next_token (&val, cfile);
		if (token != NUMBER) {
1034
			parse_warn (cfile, "expecting number.");
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
			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: