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