1 /* $OpenBSD: parse.c,v 1.21 2016/08/27 01:26:22 guenther 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 <sys/types.h> 44 #include <sys/socket.h> 45 46 #include <net/if.h> 47 48 #include <netinet/in.h> 49 50 #include <ctype.h> 51 #include <stdint.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <time.h> 56 57 #include "dhcp.h" 58 #include "tree.h" 59 #include "dhcpd.h" 60 #include "dhctoken.h" 61 62 /* 63 * Skip to the semicolon ending the current statement. If we encounter 64 * braces, the matching closing brace terminates the statement. If we 65 * encounter a right brace but haven't encountered a left brace, return 66 * leaving the brace in the token buffer for the caller. If we see a 67 * semicolon and haven't seen a left brace, return. This lets us skip 68 * over: 69 * 70 * statement; 71 * statement foo bar { } 72 * statement foo bar { statement { } } 73 * statement} 74 * 75 * ...et cetera. 76 */ 77 void 78 skip_to_semi(FILE *cfile) 79 { 80 int token; 81 char *val; 82 int brace_count = 0; 83 84 do { 85 token = peek_token(&val, cfile); 86 if (token == '}') { 87 if (brace_count) { 88 token = next_token(&val, cfile); 89 if (!--brace_count) 90 return; 91 } else 92 return; 93 } else if (token == '{') { 94 brace_count++; 95 } else if (token == ';' && !brace_count) { 96 token = next_token(&val, cfile); 97 return; 98 } else if (token == '\n') { 99 /* 100 * EOL only happens when parsing 101 * /etc/resolv.conf, and we treat it like a 102 * semicolon because the resolv.conf file is 103 * line-oriented. 104 */ 105 token = next_token(&val, cfile); 106 return; 107 } 108 token = next_token(&val, cfile); 109 } while (token != EOF); 110 } 111 112 int 113 parse_semi(FILE *cfile) 114 { 115 int token; 116 char *val; 117 118 token = next_token(&val, cfile); 119 if (token != ';') { 120 parse_warn("semicolon expected."); 121 skip_to_semi(cfile); 122 return (0); 123 } 124 return (1); 125 } 126 127 /* 128 * string-parameter :== STRING SEMI 129 */ 130 char * 131 parse_string(FILE *cfile) 132 { 133 char *val, *s; 134 int token; 135 136 token = next_token(&val, cfile); 137 if (token != TOK_STRING) { 138 parse_warn("filename must be a string"); 139 skip_to_semi(cfile); 140 return (NULL); 141 } 142 s = strdup(val); 143 if (s == NULL) 144 error("no memory for string %s.", val); 145 146 if (!parse_semi(cfile)) { 147 free(s); 148 return (NULL); 149 } 150 return (s); 151 } 152 153 /* 154 * hostname :== identifier | hostname DOT identifier 155 */ 156 char * 157 parse_host_name(FILE *cfile) 158 { 159 char *val, *s, *t; 160 int token, len = 0; 161 pair c = NULL; 162 163 /* Read a dotted hostname... */ 164 do { 165 /* Read a token, which should be an identifier. */ 166 token = next_token(&val, cfile); 167 if (!is_identifier(token)) { 168 parse_warn("expecting an identifier in hostname"); 169 skip_to_semi(cfile); 170 return (NULL); 171 } 172 /* Store this identifier... */ 173 s = strdup(val); 174 if (s == NULL) 175 error("can't allocate temp space for hostname."); 176 c = cons((caddr_t) s, c); 177 len += strlen(s) + 1; 178 /* 179 * Look for a dot; if it's there, keep going, otherwise 180 * we're done. 181 */ 182 token = peek_token(&val, cfile); 183 if (token == '.') 184 token = next_token(&val, cfile); 185 } while (token == '.'); 186 187 /* Assemble the hostname together into a string. */ 188 if (!(s = malloc(len))) 189 error("can't allocate space for hostname."); 190 t = s + len; 191 *--t = '\0'; 192 while (c) { 193 pair cdr = c->cdr; 194 int l = strlen((char *)c->car); 195 196 t -= l; 197 memcpy(t, (char *)c->car, l); 198 /* Free up temp space. */ 199 free(c->car); 200 free(c); 201 c = cdr; 202 if (t != s) 203 *--t = '.'; 204 } 205 return (s); 206 } 207 208 /* 209 * hardware-parameter :== HARDWARE ETHERNET csns SEMI 210 * csns :== NUMBER | csns COLON NUMBER 211 */ 212 void 213 parse_hardware_param(FILE *cfile, struct hardware *hardware) 214 { 215 char *val; 216 int token, hlen; 217 unsigned char *t; 218 219 token = next_token(&val, cfile); 220 switch (token) { 221 case TOK_ETHERNET: 222 hardware->htype = HTYPE_ETHER; 223 break; 224 case TOK_IPSEC_TUNNEL: 225 hardware->htype = HTYPE_IPSEC_TUNNEL; 226 break; 227 default: 228 parse_warn("expecting a network hardware type"); 229 skip_to_semi(cfile); 230 return; 231 } 232 233 /* 234 * Parse the hardware address information. Technically, it 235 * would make a lot of sense to restrict the length of the data 236 * we'll accept here to the length of a particular hardware 237 * address type. Unfortunately, there are some broken clients 238 * out there that put bogus data in the chaddr buffer, and we 239 * accept that data in the lease file rather than simply failing 240 * on such clients. Yuck. 241 */ 242 hlen = 0; 243 t = parse_numeric_aggregate(cfile, NULL, &hlen, ':', 16, 8); 244 if (!t) 245 return; 246 if (hlen > sizeof(hardware->haddr)) { 247 free(t); 248 parse_warn("hardware address too long"); 249 } else { 250 hardware->hlen = hlen; 251 memcpy((unsigned char *)&hardware->haddr[0], t, hardware->hlen); 252 if (hlen < sizeof(hardware->haddr)) 253 memset(&hardware->haddr[hlen], 0, 254 sizeof(hardware->haddr) - hlen); 255 free(t); 256 } 257 258 token = next_token(&val, cfile); 259 if (token != ';') { 260 parse_warn("expecting semicolon."); 261 skip_to_semi(cfile); 262 } 263 } 264 265 /* 266 * lease-time :== NUMBER SEMI 267 */ 268 void 269 parse_lease_time(FILE *cfile, time_t *timep) 270 { 271 const char *errstr; 272 char *val; 273 uint32_t value; 274 int token; 275 276 token = next_token(&val, cfile); 277 278 value = strtonum(val, 0, UINT32_MAX, &errstr); 279 if (errstr) { 280 parse_warn("lease time is %s: %s", errstr, val); 281 skip_to_semi(cfile); 282 return; 283 } 284 285 *timep = value; 286 287 parse_semi(cfile); 288 } 289 290 /* 291 * No BNF for numeric aggregates - that's defined by the caller. What 292 * this function does is to parse a sequence of numbers separated by the 293 * token specified in separator. If max is zero, any number of numbers 294 * will be parsed; otherwise, exactly max numbers are expected. Base 295 * and size tell us how to internalize the numbers once they've been 296 * tokenized. 297 */ 298 unsigned char * 299 parse_numeric_aggregate(FILE *cfile, unsigned char *buf, int *max, 300 int separator, int base, int size) 301 { 302 char *val, *t; 303 int token, count = 0; 304 unsigned char *bufp = buf, *s = NULL; 305 pair c = NULL; 306 307 if (!bufp && *max) { 308 bufp = malloc(*max * size / 8); 309 if (!bufp) 310 error("can't allocate space for numeric aggregate"); 311 } else 312 s = bufp; 313 314 do { 315 if (count) { 316 token = peek_token(&val, cfile); 317 if (token != separator) { 318 if (!*max) 319 break; 320 if (token != '{' && token != '}') 321 token = next_token(&val, cfile); 322 parse_warn("too few numbers."); 323 if (token != ';') 324 skip_to_semi(cfile); 325 return (NULL); 326 } 327 token = next_token(&val, cfile); 328 } 329 token = next_token(&val, cfile); 330 331 if (token == EOF) { 332 parse_warn("unexpected end of file"); 333 break; 334 } 335 if (token != TOK_NUMBER && token != TOK_NUMBER_OR_NAME) { 336 parse_warn("expecting numeric value."); 337 skip_to_semi(cfile); 338 return (NULL); 339 } 340 /* 341 * If we can, convert the number now; otherwise, build a 342 * linked list of all the numbers. 343 */ 344 if (s) { 345 convert_num(s, val, base, size); 346 s += size / 8; 347 } else { 348 t = strdup(val); 349 if (t == NULL) 350 error("no temp space for number."); 351 c = cons(t, c); 352 } 353 } while (++count != *max); 354 355 /* If we had to cons up a list, convert it now. */ 356 if (c) { 357 bufp = malloc(count * size / 8); 358 if (!bufp) 359 error("can't allocate space for numeric aggregate."); 360 s = bufp + count - size / 8; 361 *max = count; 362 } 363 while (c) { 364 pair cdr = c->cdr; 365 convert_num(s, (char *)c->car, base, size); 366 s -= size / 8; 367 /* Free up temp space. */ 368 free(c->car); 369 free(c); 370 c = cdr; 371 } 372 return (bufp); 373 } 374 375 void 376 convert_num(unsigned char *buf, char *str, int base, int size) 377 { 378 int negative = 0, tval, max; 379 u_int32_t val = 0; 380 char *ptr = str; 381 382 if (*ptr == '-') { 383 negative = 1; 384 ptr++; 385 } 386 387 /* If base wasn't specified, figure it out from the data. */ 388 if (!base) { 389 if (ptr[0] == '0') { 390 if (ptr[1] == 'x') { 391 base = 16; 392 ptr += 2; 393 } else if (isascii((unsigned char)ptr[1]) && 394 isdigit((unsigned char)ptr[1])) { 395 base = 8; 396 ptr += 1; 397 } else 398 base = 10; 399 } else 400 base = 10; 401 } 402 403 do { 404 tval = *ptr++; 405 /* XXX assumes ASCII... */ 406 if (tval >= 'a') 407 tval = tval - 'a' + 10; 408 else if (tval >= 'A') 409 tval = tval - 'A' + 10; 410 else if (tval >= '0') 411 tval -= '0'; 412 else { 413 warning("Bogus number: %s.", str); 414 break; 415 } 416 if (tval >= base) { 417 warning("Bogus number: %s: digit %d not in base %d", 418 str, tval, base); 419 break; 420 } 421 val = val * base + tval; 422 } while (*ptr); 423 424 if (negative) 425 max = (1 << (size - 1)); 426 else 427 max = (1 << (size - 1)) + ((1 << (size - 1)) - 1); 428 if (val > max) { 429 switch (base) { 430 case 8: 431 warning("value %s%o exceeds max (%d) for precision.", 432 negative ? "-" : "", val, max); 433 break; 434 case 16: 435 warning("value %s%x exceeds max (%d) for precision.", 436 negative ? "-" : "", val, max); 437 break; 438 default: 439 warning("value %s%u exceeds max (%d) for precision.", 440 negative ? "-" : "", val, max); 441 break; 442 } 443 } 444 445 if (negative) { 446 switch (size) { 447 case 8: 448 *buf = -(unsigned long)val; 449 break; 450 case 16: 451 putShort(buf, -(unsigned long)val); 452 break; 453 case 32: 454 putLong(buf, -(unsigned long)val); 455 break; 456 default: 457 warning("Unexpected integer size: %d", size); 458 break; 459 } 460 } else { 461 switch (size) { 462 case 8: 463 *buf = (u_int8_t)val; 464 break; 465 case 16: 466 putUShort(buf, (u_int16_t)val); 467 break; 468 case 32: 469 putULong(buf, val); 470 break; 471 default: 472 warning("Unexpected integer size: %d", size); 473 break; 474 } 475 } 476 } 477 478 /* 479 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER 480 * NUMBER COLON NUMBER COLON NUMBER UTC SEMI 481 * 482 * Dates are always in UTC; first number is day of week; next is 483 * year/month/day; next is hours:minutes:seconds on a 24-hour 484 * clock. 485 */ 486 time_t 487 parse_date(FILE *cfile) 488 { 489 struct tm tm; 490 char timestr[26]; /* "w yyyy/mm/dd hh:mm:ss UTC" */ 491 char *val, *p; 492 size_t n; 493 time_t guess; 494 int token; 495 496 memset(timestr, 0, sizeof(timestr)); 497 498 do { 499 token = peek_token(NULL, cfile); 500 switch (token) { 501 case TOK_NAME: 502 case TOK_NUMBER: 503 case TOK_NUMBER_OR_NAME: 504 case '/': 505 case ':': 506 token = next_token(&val, cfile); 507 n = strlcat(timestr, val, sizeof(timestr)); 508 if (n >= sizeof(timestr)) { 509 /* XXX Will break after year 9999! */ 510 parse_warn("time string too long"); 511 skip_to_semi(cfile); 512 return (0); 513 } 514 break; 515 case';': 516 break; 517 default: 518 parse_warn("invalid time string"); 519 skip_to_semi(cfile); 520 return (0); 521 } 522 } while (token != ';'); 523 524 parse_semi(cfile); 525 526 memset(&tm, 0, sizeof(tm)); /* 'cuz strptime ignores tm_isdt. */ 527 p = strptime(timestr, DB_TIMEFMT, &tm); 528 if (p == NULL || *p != '\0') { 529 p = strptime(timestr, OLD_DB_TIMEFMT, &tm); 530 if (p == NULL || *p != '\0') { 531 parse_warn("unparseable time string"); 532 return (0); 533 } 534 } 535 536 guess = timegm(&tm); 537 if (guess == -1) { 538 parse_warn("time could not be represented"); 539 return (0); 540 } 541 542 return (guess); 543 } 544