1 /* $OpenBSD: parse.c,v 1.11 2007/02/26 00:49:53 stevesk Exp $ */ 2 3 /* Common parser code for dhcpd and dhclient. */ 4 5 /* 6 * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of The Internet Software Consortium nor the names 19 * of its contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND 23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * This software has been written for the Internet Software Consortium 37 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie 38 * Enterprises. To learn more about the Internet Software Consortium, 39 * see ``http://www.vix.com/isc''. To learn more about Vixie 40 * Enterprises, see ``http://www.vix.com''. 41 */ 42 43 #include "dhcpd.h" 44 #include "dhctoken.h" 45 46 /* 47 * Skip to the semicolon ending the current statement. If we encounter 48 * braces, the matching closing brace terminates the statement. If we 49 * encounter a right brace but haven't encountered a left brace, return 50 * leaving the brace in the token buffer for the caller. If we see a 51 * semicolon and haven't seen a left brace, return. This lets us skip 52 * over: 53 * 54 * statement; 55 * statement foo bar { } 56 * statement foo bar { statement { } } 57 * statement} 58 * 59 * ...et cetera. 60 */ 61 void 62 skip_to_semi(FILE *cfile) 63 { 64 int token; 65 char *val; 66 int brace_count = 0; 67 68 do { 69 token = peek_token(&val, cfile); 70 if (token == '}') { 71 if (brace_count) { 72 token = next_token(&val, cfile); 73 if (!--brace_count) 74 return; 75 } else 76 return; 77 } else if (token == '{') { 78 brace_count++; 79 } else if (token == ';' && !brace_count) { 80 token = next_token(&val, cfile); 81 return; 82 } else if (token == '\n') { 83 /* 84 * EOL only happens when parsing 85 * /etc/resolv.conf, and we treat it like a 86 * semicolon because the resolv.conf file is 87 * line-oriented. 88 */ 89 token = next_token(&val, cfile); 90 return; 91 } 92 token = next_token(&val, cfile); 93 } while (token != EOF); 94 } 95 96 int 97 parse_semi(FILE *cfile) 98 { 99 int token; 100 char *val; 101 102 token = next_token(&val, cfile); 103 if (token != ';') { 104 parse_warn("semicolon expected."); 105 skip_to_semi(cfile); 106 return (0); 107 } 108 return (1); 109 } 110 111 /* 112 * string-parameter :== STRING SEMI 113 */ 114 char * 115 parse_string(FILE *cfile) 116 { 117 char *val, *s; 118 int token; 119 120 token = next_token(&val, cfile); 121 if (token != TOK_STRING) { 122 parse_warn("filename must be a string"); 123 skip_to_semi(cfile); 124 return (NULL); 125 } 126 s = malloc(strlen(val) + 1); 127 if (!s) 128 error("no memory for string %s.", val); 129 strlcpy(s, val, strlen(val) + 1); 130 131 if (!parse_semi(cfile)) { 132 free(s); 133 return (NULL); 134 } 135 return (s); 136 } 137 138 /* 139 * hostname :== identifier | hostname DOT identifier 140 */ 141 char * 142 parse_host_name(FILE *cfile) 143 { 144 char *val, *s, *t; 145 int token, len = 0; 146 pair c = NULL; 147 148 /* Read a dotted hostname... */ 149 do { 150 /* Read a token, which should be an identifier. */ 151 token = next_token(&val, cfile); 152 if (!is_identifier(token) && token != TOK_NUMBER) { 153 parse_warn("expecting an identifier in hostname"); 154 skip_to_semi(cfile); 155 return (NULL); 156 } 157 /* Store this identifier... */ 158 if (!(s = malloc(strlen(val) + 1))) 159 error("can't allocate temp space for hostname."); 160 strlcpy(s, val, strlen(val) + 1); 161 c = cons((caddr_t) s, c); 162 len += strlen(s) + 1; 163 /* 164 * Look for a dot; if it's there, keep going, otherwise 165 * we're done. 166 */ 167 token = peek_token(&val, cfile); 168 if (token == '.') 169 token = next_token(&val, cfile); 170 } while (token == '.'); 171 172 /* Assemble the hostname together into a string. */ 173 if (!(s = malloc(len))) 174 error("can't allocate space for hostname."); 175 t = s + len; 176 *--t = '\0'; 177 while (c) { 178 pair cdr = c->cdr; 179 int l = strlen((char *)c->car); 180 181 t -= l; 182 memcpy(t, (char *)c->car, l); 183 /* Free up temp space. */ 184 free(c->car); 185 free(c); 186 c = cdr; 187 if (t != s) 188 *--t = '.'; 189 } 190 return (s); 191 } 192 193 /* 194 * hardware-parameter :== HARDWARE ETHERNET csns SEMI 195 * csns :== NUMBER | csns COLON NUMBER 196 */ 197 void 198 parse_hardware_param(FILE *cfile, struct hardware *hardware) 199 { 200 char *val; 201 int token, hlen; 202 unsigned char *t; 203 204 token = next_token(&val, cfile); 205 switch (token) { 206 case TOK_ETHERNET: 207 hardware->htype = HTYPE_ETHER; 208 break; 209 case TOK_TOKEN_RING: 210 hardware->htype = HTYPE_IEEE802; 211 break; 212 case TOK_FDDI: 213 hardware->htype = HTYPE_FDDI; 214 break; 215 default: 216 parse_warn("expecting a network hardware type"); 217 skip_to_semi(cfile); 218 return; 219 } 220 221 /* 222 * Parse the hardware address information. Technically, it 223 * would make a lot of sense to restrict the length of the data 224 * we'll accept here to the length of a particular hardware 225 * address type. Unfortunately, there are some broken clients 226 * out there that put bogus data in the chaddr buffer, and we 227 * accept that data in the lease file rather than simply failing 228 * on such clients. Yuck. 229 */ 230 hlen = 0; 231 t = parse_numeric_aggregate(cfile, NULL, &hlen, ':', 16, 8); 232 if (!t) 233 return; 234 if (hlen > sizeof(hardware->haddr)) { 235 free(t); 236 parse_warn("hardware address too long"); 237 } else { 238 hardware->hlen = hlen; 239 memcpy((unsigned char *)&hardware->haddr[0], t, hardware->hlen); 240 if (hlen < sizeof(hardware->haddr)) 241 memset(&hardware->haddr[hlen], 0, 242 sizeof(hardware->haddr) - hlen); 243 free(t); 244 } 245 246 token = next_token(&val, cfile); 247 if (token != ';') { 248 parse_warn("expecting semicolon."); 249 skip_to_semi(cfile); 250 } 251 } 252 253 /* 254 * lease-time :== NUMBER SEMI 255 */ 256 void 257 parse_lease_time(FILE *cfile, time_t *timep) 258 { 259 char *val; 260 int token; 261 262 token = next_token(&val, cfile); 263 if (token != TOK_NUMBER) { 264 parse_warn("Expecting numeric lease time"); 265 skip_to_semi(cfile); 266 return; 267 } 268 convert_num((unsigned char *)timep, val, 10, 32); 269 /* Unswap the number - convert_num returns stuff in NBO. */ 270 *timep = ntohl(*timep); /* XXX */ 271 272 parse_semi(cfile); 273 } 274 275 /* 276 * No BNF for numeric aggregates - that's defined by the caller. What 277 * this function does is to parse a sequence of numbers separated by the 278 * token specified in separator. If max is zero, any number of numbers 279 * will be parsed; otherwise, exactly max numbers are expected. Base 280 * and size tell us how to internalize the numbers once they've been 281 * tokenized. 282 */ 283 unsigned char * 284 parse_numeric_aggregate(FILE *cfile, unsigned char *buf, int *max, 285 int separator, int base, int size) 286 { 287 char *val, *t; 288 int token, count = 0; 289 unsigned char *bufp = buf, *s = NULL; 290 pair c = NULL; 291 292 if (!bufp && *max) { 293 bufp = malloc(*max * size / 8); 294 if (!bufp) 295 error("can't allocate space for numeric aggregate"); 296 } else 297 s = bufp; 298 299 do { 300 if (count) { 301 token = peek_token(&val, cfile); 302 if (token != separator) { 303 if (!*max) 304 break; 305 if (token != '{' && token != '}') 306 token = next_token(&val, cfile); 307 parse_warn("too few numbers."); 308 if (token != ';') 309 skip_to_semi(cfile); 310 return (NULL); 311 } 312 token = next_token(&val, cfile); 313 } 314 token = next_token(&val, cfile); 315 316 if (token == EOF) { 317 parse_warn("unexpected end of file"); 318 break; 319 } 320 /* Allow NUMBER_OR_NAME if base is 16. */ 321 if (token != TOK_NUMBER && 322 (base != 16 || token != TOK_NUMBER_OR_NAME)) { 323 parse_warn("expecting numeric value."); 324 skip_to_semi(cfile); 325 return (NULL); 326 } 327 /* 328 * If we can, convert the number now; otherwise, build a 329 * linked list of all the numbers. 330 */ 331 if (s) { 332 convert_num(s, val, base, size); 333 s += size / 8; 334 } else { 335 t = malloc(strlen(val) + 1); 336 if (!t) 337 error("no temp space for number."); 338 strlcpy(t, val, strlen(val) + 1); 339 c = cons(t, c); 340 } 341 } while (++count != *max); 342 343 /* If we had to cons up a list, convert it now. */ 344 if (c) { 345 bufp = malloc(count * size / 8); 346 if (!bufp) 347 error("can't allocate space for numeric aggregate."); 348 s = bufp + count - size / 8; 349 *max = count; 350 } 351 while (c) { 352 pair cdr = c->cdr; 353 convert_num(s, (char *)c->car, base, size); 354 s -= size / 8; 355 /* Free up temp space. */ 356 free(c->car); 357 free(c); 358 c = cdr; 359 } 360 return (bufp); 361 } 362 363 void 364 convert_num(unsigned char *buf, char *str, int base, int size) 365 { 366 int negative = 0, tval, max; 367 u_int32_t val = 0; 368 char *ptr = str; 369 370 if (*ptr == '-') { 371 negative = 1; 372 ptr++; 373 } 374 375 /* If base wasn't specified, figure it out from the data. */ 376 if (!base) { 377 if (ptr[0] == '0') { 378 if (ptr[1] == 'x') { 379 base = 16; 380 ptr += 2; 381 } else if (isascii(ptr[1]) && isdigit(ptr[1])) { 382 base = 8; 383 ptr += 1; 384 } else 385 base = 10; 386 } else 387 base = 10; 388 } 389 390 do { 391 tval = *ptr++; 392 /* XXX assumes ASCII... */ 393 if (tval >= 'a') 394 tval = tval - 'a' + 10; 395 else if (tval >= 'A') 396 tval = tval - 'A' + 10; 397 else if (tval >= '0') 398 tval -= '0'; 399 else { 400 warning("Bogus number: %s.", str); 401 break; 402 } 403 if (tval >= base) { 404 warning("Bogus number: %s: digit %d not in base %d", 405 str, tval, base); 406 break; 407 } 408 val = val * base + tval; 409 } while (*ptr); 410 411 if (negative) 412 max = (1 << (size - 1)); 413 else 414 max = (1 << (size - 1)) + ((1 << (size - 1)) - 1); 415 if (val > max) { 416 switch (base) { 417 case 8: 418 warning("value %s%o exceeds max (%d) for precision.", 419 negative ? "-" : "", val, max); 420 break; 421 case 16: 422 warning("value %s%x exceeds max (%d) for precision.", 423 negative ? "-" : "", val, max); 424 break; 425 default: 426 warning("value %s%u exceeds max (%d) for precision.", 427 negative ? "-" : "", val, max); 428 break; 429 } 430 } 431 432 if (negative) { 433 switch (size) { 434 case 8: 435 *buf = -(unsigned long)val; 436 break; 437 case 16: 438 putShort(buf, -(unsigned long)val); 439 break; 440 case 32: 441 putLong(buf, -(unsigned long)val); 442 break; 443 default: 444 warning("Unexpected integer size: %d", size); 445 break; 446 } 447 } else { 448 switch (size) { 449 case 8: 450 *buf = (u_int8_t)val; 451 break; 452 case 16: 453 putUShort(buf, (u_int16_t)val); 454 break; 455 case 32: 456 putULong(buf, val); 457 break; 458 default: 459 warning("Unexpected integer size: %d", size); 460 break; 461 } 462 } 463 } 464 465 /* 466 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER 467 * NUMBER COLON NUMBER COLON NUMBER SEMI 468 * 469 * Dates are always in GMT; first number is day of week; next is 470 * year/month/day; next is hours:minutes:seconds on a 24-hour 471 * clock. 472 */ 473 time_t 474 parse_date(FILE *cfile) 475 { 476 static int months[11] = { 31, 59, 90, 120, 151, 181, 477 212, 243, 273, 304, 334 }; 478 int guess, token; 479 struct tm tm; 480 char *val; 481 482 /* Day of week... */ 483 token = next_token(&val, cfile); 484 if (token != TOK_NUMBER) { 485 parse_warn("numeric day of week expected."); 486 if (token != ';') 487 skip_to_semi(cfile); 488 return (0); 489 } 490 tm.tm_wday = atoi(val); 491 492 /* Year... */ 493 token = next_token(&val, cfile); 494 if (token != TOK_NUMBER) { 495 parse_warn("numeric year expected."); 496 if (token != ';') 497 skip_to_semi(cfile); 498 return (0); 499 } 500 tm.tm_year = atoi(val); 501 if (tm.tm_year > 1900) 502 tm.tm_year -= 1900; 503 504 /* Slash separating year from month... */ 505 token = next_token(&val, cfile); 506 if (token != '/') { 507 parse_warn("expected slash separating year from month."); 508 if (token != ';') 509 skip_to_semi(cfile); 510 return (0); 511 } 512 513 /* Month... */ 514 token = next_token(&val, cfile); 515 if (token != TOK_NUMBER) { 516 parse_warn("numeric month expected."); 517 if (token != ';') 518 skip_to_semi(cfile); 519 return (0); 520 } 521 tm.tm_mon = atoi(val) - 1; 522 523 /* Slash separating month from day... */ 524 token = next_token(&val, cfile); 525 if (token != '/') { 526 parse_warn("expected slash separating month from day."); 527 if (token != ';') 528 skip_to_semi(cfile); 529 return (0); 530 } 531 532 /* Day... */ 533 token = next_token(&val, cfile); 534 if (token != TOK_NUMBER) { 535 parse_warn("numeric day of month expected."); 536 if (token != ';') 537 skip_to_semi(cfile); 538 return (0); 539 } 540 tm.tm_mday = atoi(val); 541 542 /* Hour... */ 543 token = next_token(&val, cfile); 544 if (token != TOK_NUMBER) { 545 parse_warn("numeric hour expected."); 546 if (token != ';') 547 skip_to_semi(cfile); 548 return (0); 549 } 550 tm.tm_hour = atoi(val); 551 552 /* Colon separating hour from minute... */ 553 token = next_token(&val, cfile); 554 if (token != ':') { 555 parse_warn("expected colon separating hour from minute."); 556 if (token != ';') 557 skip_to_semi(cfile); 558 return (0); 559 } 560 561 /* Minute... */ 562 token = next_token(&val, cfile); 563 if (token != TOK_NUMBER) { 564 parse_warn("numeric minute expected."); 565 if (token != ';') 566 skip_to_semi(cfile); 567 return (0); 568 } 569 tm.tm_min = atoi(val); 570 571 /* Colon separating minute from second... */ 572 token = next_token(&val, cfile); 573 if (token != ':') { 574 parse_warn("expected colon separating minute from second."); 575 if (token != ';') 576 skip_to_semi(cfile); 577 return (0); 578 } 579 580 /* Second... */ 581 token = next_token(&val, cfile); 582 if (token != TOK_NUMBER) { 583 parse_warn("numeric second expected."); 584 if (token != ';') 585 skip_to_semi(cfile); 586 return (0); 587 } 588 tm.tm_sec = atoi(val); 589 tm.tm_isdst = 0; 590 591 /* XXX: We assume that mktime does not use tm_yday. */ 592 tm.tm_yday = 0; 593 594 /* Make sure the date ends in a semicolon... */ 595 token = next_token(&val, cfile); 596 if (token != ';') { 597 parse_warn("semicolon expected."); 598 skip_to_semi(cfile); 599 return (0); 600 } 601 602 /* Guess the time value... */ 603 guess = ((((((365 * (tm.tm_year - 70) + /* Days in years since '70 */ 604 (tm.tm_year - 69) / 4 + /* Leap days since '70 */ 605 (tm.tm_mon /* Days in months this year */ 606 ? months[tm.tm_mon - 1] : 0) + 607 (tm.tm_mon > 1 && /* Leap day this year */ 608 !((tm.tm_year - 72) & 3)) + 609 tm.tm_mday - 1) * 24) + /* Day of month */ 610 tm.tm_hour) * 60) + tm.tm_min) * 60) + tm.tm_sec; 611 612 /* 613 * This guess could be wrong because of leap seconds or other 614 * weirdness we don't know about that the system does. For 615 * now, we're just going to accept the guess, but at some point 616 * it might be nice to do a successive approximation here to get 617 * an exact value. Even if the error is small, if the server 618 * is restarted frequently (and thus the lease database is 619 * reread), the error could accumulate into something 620 * significant. 621 */ 622 return (guess); 623 } 624