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