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
| 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
|
44#include "dhcpd.h" 45#include "dhctoken.h" 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 */ 61void 62skip_to_semi(FILE *cfile) 63{ 64 int brace_count = 0, token; 65 char *val; 66 67 do { 68 token = peek_token(&val, cfile); 69 if (token == RBRACE) { 70 if (brace_count) { 71 token = next_token(&val, cfile); 72 if (!--brace_count) 73 return; 74 } else 75 return; 76 } else if (token == LBRACE) { 77 brace_count++; 78 } else if (token == SEMI && !brace_count) { 79 token = next_token(&val, cfile); 80 return; 81 } else if (token == '\n') { 82 /* 83 * EOL only happens when parsing 84 * /etc/resolv.conf, and we treat it like a 85 * semicolon because the resolv.conf file is 86 * line-oriented. 87 */ 88 token = next_token(&val, cfile); 89 return; 90 } 91 token = next_token(&val, cfile); 92 } while (token != EOF); 93} 94 95int 96parse_semi(FILE *cfile) 97{ 98 int token; 99 char *val; 100 101 token = next_token(&val, cfile); 102 if (token != SEMI) { 103 parse_warn("semicolon expected."); 104 skip_to_semi(cfile); 105 return (0); 106 } 107 return (1); 108} 109 110/* 111 * string-parameter :== STRING SEMI 112 */ 113char * 114parse_string(FILE *cfile) 115{ 116 char *val, *s; 117 int token; 118 119 token = next_token(&val, cfile); 120 if (token != STRING) { 121 parse_warn("filename must be a string"); 122 skip_to_semi(cfile); 123 return (NULL); 124 } 125 s = malloc(strlen(val) + 1); 126 if (!s) 127 error("no memory for string %s.", val); 128 strlcpy(s, val, strlen(val) + 1); 129 130 if (!parse_semi(cfile)) 131 return (NULL); 132 return (s); 133} 134 135int 136parse_ip_addr(FILE *cfile, struct iaddr *addr) 137{ 138 addr->len = 4; 139 if (parse_numeric_aggregate(cfile, addr->iabuf, 140 &addr->len, DOT, 10, 8)) 141 return (1); 142 return (0); 143} 144 145/* 146 * hardware-parameter :== HARDWARE ETHERNET csns SEMI 147 * csns :== NUMBER | csns COLON NUMBER 148 */ 149void 150parse_hardware_param(FILE *cfile, struct hardware *hardware) 151{ 152 unsigned char *t; 153 int token, hlen; 154 char *val; 155 156 token = next_token(&val, cfile); 157 switch (token) { 158 case ETHERNET: 159 hardware->htype = HTYPE_ETHER; 160 break; 161 case TOKEN_RING: 162 hardware->htype = HTYPE_IEEE802; 163 break; 164 case FDDI: 165 hardware->htype = HTYPE_FDDI; 166 break; 167 default: 168 parse_warn("expecting a network hardware type"); 169 skip_to_semi(cfile); 170 return; 171 } 172 173 /* 174 * Parse the hardware address information. Technically, it 175 * would make a lot of sense to restrict the length of the data 176 * we'll accept here to the length of a particular hardware 177 * address type. Unfortunately, there are some broken clients 178 * out there that put bogus data in the chaddr buffer, and we 179 * accept that data in the lease file rather than simply failing 180 * on such clients. Yuck. 181 */ 182 hlen = 0; 183 t = parse_numeric_aggregate(cfile, NULL, &hlen, COLON, 16, 8); 184 if (!t) 185 return; 186 if (hlen > sizeof(hardware->haddr)) { 187 free(t); 188 parse_warn("hardware address too long"); 189 } else { 190 hardware->hlen = hlen; 191 memcpy((unsigned char *)&hardware->haddr[0], t, 192 hardware->hlen); 193 if (hlen < sizeof(hardware->haddr)) 194 memset(&hardware->haddr[hlen], 0, 195 sizeof(hardware->haddr) - hlen); 196 free(t); 197 } 198 199 token = next_token(&val, cfile); 200 if (token != SEMI) { 201 parse_warn("expecting semicolon."); 202 skip_to_semi(cfile); 203 } 204} 205 206/* 207 * lease-time :== NUMBER SEMI 208 */ 209void 210parse_lease_time(FILE *cfile, time_t *timep) 211{ 212 char *val; 213 int token; 214 215 token = next_token(&val, cfile); 216 if (token != NUMBER) { 217 parse_warn("Expecting numeric lease time"); 218 skip_to_semi(cfile); 219 return; 220 } 221 convert_num((unsigned char *)timep, val, 10, 32); 222 /* Unswap the number - convert_num returns stuff in NBO. */ 223 *timep = ntohl(*timep); /* XXX */ 224 225 parse_semi(cfile); 226} 227 228/* 229 * No BNF for numeric aggregates - that's defined by the caller. What 230 * this function does is to parse a sequence of numbers separated by the 231 * token specified in separator. If max is zero, any number of numbers 232 * will be parsed; otherwise, exactly max numbers are expected. Base 233 * and size tell us how to internalize the numbers once they've been 234 * tokenized. 235 */ 236unsigned char * 237parse_numeric_aggregate(FILE *cfile, unsigned char *buf, int *max, 238 int separator, int base, int size) 239{ 240 unsigned char *bufp = buf, *s = NULL; 241 int token, count = 0; 242 char *val, *t; 243 pair c = NULL; 244 245 if (!bufp && *max) { 246 bufp = malloc(*max * size / 8); 247 if (!bufp) 248 error("can't allocate space for numeric aggregate"); 249 } else 250 s = bufp; 251 252 do { 253 if (count) { 254 token = peek_token(&val, cfile); 255 if (token != separator) { 256 if (!*max) 257 break; 258 if (token != RBRACE && token != LBRACE) 259 token = next_token(&val, cfile); 260 parse_warn("too few numbers."); 261 if (token != SEMI) 262 skip_to_semi(cfile); 263 return (NULL); 264 } 265 token = next_token(&val, cfile); 266 } 267 token = next_token(&val, cfile); 268 269 if (token == EOF) { 270 parse_warn("unexpected end of file"); 271 break; 272 } 273 274 /* Allow NUMBER_OR_NAME if base is 16. */ 275 if (token != NUMBER && 276 (base != 16 || token != NUMBER_OR_NAME)) { 277 parse_warn("expecting numeric value."); 278 skip_to_semi(cfile); 279 return (NULL); 280 } 281 /* 282 * If we can, convert the number now; otherwise, build a 283 * linked list of all the numbers. 284 */ 285 if (s) { 286 convert_num(s, val, base, size); 287 s += size / 8; 288 } else { 289 t = malloc(strlen(val) + 1); 290 if (!t) 291 error("no temp space for number."); 292 strlcpy(t, val, strlen(val) + 1); 293 c = cons(t, c); 294 } 295 } while (++count != *max); 296 297 /* If we had to cons up a list, convert it now. */ 298 if (c) { 299 bufp = malloc(count * size / 8); 300 if (!bufp) 301 error("can't allocate space for numeric aggregate."); 302 s = bufp + count - size / 8; 303 *max = count; 304 } 305 while (c) { 306 pair cdr = c->cdr; 307 convert_num(s, (char *)c->car, base, size); 308 s -= size / 8; 309 /* Free up temp space. */ 310 free(c->car); 311 free(c); 312 c = cdr; 313 } 314 return (bufp); 315} 316 317void 318convert_num(unsigned char *buf, char *str, int base, int size) 319{ 320 int negative = 0, tval, max; 321 u_int32_t val = 0; 322 char *ptr = str; 323 324 if (*ptr == '-') { 325 negative = 1; 326 ptr++; 327 } 328 329 /* If base wasn't specified, figure it out from the data. */ 330 if (!base) { 331 if (ptr[0] == '0') { 332 if (ptr[1] == 'x') { 333 base = 16; 334 ptr += 2; 335 } else if (isascii(ptr[1]) && isdigit(ptr[1])) { 336 base = 8; 337 ptr += 1; 338 } else 339 base = 10; 340 } else 341 base = 10; 342 } 343 344 do { 345 tval = *ptr++; 346 /* XXX assumes ASCII... */ 347 if (tval >= 'a') 348 tval = tval - 'a' + 10; 349 else if (tval >= 'A') 350 tval = tval - 'A' + 10; 351 else if (tval >= '0') 352 tval -= '0'; 353 else { 354 warning("Bogus number: %s.", str); 355 break; 356 } 357 if (tval >= base) { 358 warning("Bogus number: %s: digit %d not in base %d", 359 str, tval, base); 360 break; 361 } 362 val = val * base + tval; 363 } while (*ptr); 364 365 if (negative) 366 max = (1 << (size - 1)); 367 else 368 max = (1 << (size - 1)) + ((1 << (size - 1)) - 1); 369 if (val > max) { 370 switch (base) { 371 case 8: 372 warning("value %s%o exceeds max (%d) for precision.", 373 negative ? "-" : "", val, max); 374 break; 375 case 16: 376 warning("value %s%x exceeds max (%d) for precision.", 377 negative ? "-" : "", val, max); 378 break; 379 default: 380 warning("value %s%u exceeds max (%d) for precision.", 381 negative ? "-" : "", val, max); 382 break; 383 } 384 } 385 386 if (negative) 387 switch (size) { 388 case 8: 389 *buf = -(unsigned long)val; 390 break; 391 case 16: 392 putShort(buf, -(unsigned long)val); 393 break; 394 case 32: 395 putLong(buf, -(unsigned long)val); 396 break; 397 default: 398 warning("Unexpected integer size: %d", size); 399 break; 400 } 401 else 402 switch (size) { 403 case 8: 404 *buf = (u_int8_t)val; 405 break; 406 case 16: 407 putUShort(buf, (u_int16_t)val); 408 break; 409 case 32: 410 putULong(buf, val); 411 break; 412 default: 413 warning("Unexpected integer size: %d", size); 414 break; 415 } 416} 417 418/* 419 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER 420 * NUMBER COLON NUMBER COLON NUMBER SEMI 421 * 422 * Dates are always in GMT; first number is day of week; next is 423 * year/month/day; next is hours:minutes:seconds on a 24-hour 424 * clock. 425 */ 426time_t 427parse_date(FILE *cfile) 428{ 429 static int months[11] = { 31, 59, 90, 120, 151, 181, 430 212, 243, 273, 304, 334 }; 431 int guess, token; 432 struct tm tm; 433 char *val; 434 435 /* Day of week... */ 436 token = next_token(&val, cfile); 437 if (token != NUMBER) { 438 parse_warn("numeric day of week expected."); 439 if (token != SEMI) 440 skip_to_semi(cfile); 441 return (0); 442 } 443 tm.tm_wday = atoi(val); 444 445 /* Year... */ 446 token = next_token(&val, cfile); 447 if (token != NUMBER) { 448 parse_warn("numeric year expected."); 449 if (token != SEMI) 450 skip_to_semi(cfile); 451 return (0); 452 } 453 tm.tm_year = atoi(val); 454 if (tm.tm_year > 1900) 455 tm.tm_year -= 1900; 456 457 /* Slash separating year from month... */ 458 token = next_token(&val, cfile); 459 if (token != SLASH) { 460 parse_warn("expected slash separating year from month."); 461 if (token != SEMI) 462 skip_to_semi(cfile); 463 return (0); 464 } 465 466 /* Month... */ 467 token = next_token(&val, cfile); 468 if (token != NUMBER) { 469 parse_warn("numeric month expected."); 470 if (token != SEMI) 471 skip_to_semi(cfile); 472 return (0); 473 } 474 tm.tm_mon = atoi(val) - 1; 475 476 /* Slash separating month from day... */ 477 token = next_token(&val, cfile); 478 if (token != SLASH) { 479 parse_warn("expected slash separating month from day."); 480 if (token != SEMI) 481 skip_to_semi(cfile); 482 return (0); 483 } 484 485 /* Month... */ 486 token = next_token(&val, cfile); 487 if (token != NUMBER) { 488 parse_warn("numeric day of month expected."); 489 if (token != SEMI) 490 skip_to_semi(cfile); 491 return (0); 492 } 493 tm.tm_mday = atoi(val); 494 495 /* Hour... */ 496 token = next_token(&val, cfile); 497 if (token != NUMBER) { 498 parse_warn("numeric hour expected."); 499 if (token != SEMI) 500 skip_to_semi(cfile); 501 return (0); 502 } 503 tm.tm_hour = atoi(val); 504 505 /* Colon separating hour from minute... */ 506 token = next_token(&val, cfile); 507 if (token != COLON) { 508 parse_warn("expected colon separating hour from minute."); 509 if (token != SEMI) 510 skip_to_semi(cfile); 511 return (0); 512 } 513 514 /* Minute... */ 515 token = next_token(&val, cfile); 516 if (token != NUMBER) { 517 parse_warn("numeric minute expected."); 518 if (token != SEMI) 519 skip_to_semi(cfile); 520 return (0); 521 } 522 tm.tm_min = atoi(val); 523 524 /* Colon separating minute from second... */ 525 token = next_token(&val, cfile); 526 if (token != COLON) { 527 parse_warn("expected colon separating hour from minute."); 528 if (token != SEMI) 529 skip_to_semi(cfile); 530 return (0); 531 } 532 533 /* Minute... */ 534 token = next_token(&val, cfile); 535 if (token != NUMBER) { 536 parse_warn("numeric minute expected."); 537 if (token != SEMI) 538 skip_to_semi(cfile); 539 return (0); 540 } 541 tm.tm_sec = atoi(val); 542 tm.tm_isdst = 0; 543 544 /* XXX: We assume that mktime does not use tm_yday. */ 545 tm.tm_yday = 0; 546 547 /* Make sure the date ends in a semicolon... */ 548 token = next_token(&val, cfile); 549 if (token != SEMI) { 550 parse_warn("semicolon expected."); 551 skip_to_semi(cfile); 552 return (0); 553 } 554 555 /* Guess the time value... */ 556 guess = ((((((365 * (tm.tm_year - 70) + /* Days in years since '70 */ 557 (tm.tm_year - 69) / 4 + /* Leap days since '70 */ 558 (tm.tm_mon /* Days in months this year */ 559 ? months[tm.tm_mon - 1] 560 : 0) + 561 (tm.tm_mon > 1 && /* Leap day this year */ 562 !((tm.tm_year - 72) & 3)) + 563 tm.tm_mday - 1) * 24) + /* Day of month */ 564 tm.tm_hour) * 60) + 565 tm.tm_min) * 60) + tm.tm_sec; 566 567 /* 568 * This guess could be wrong because of leap seconds or other 569 * weirdness we don't know about that the system does. For 570 * now, we're just going to accept the guess, but at some point 571 * it might be nice to do a successive approximation here to get 572 * an exact value. Even if the error is small, if the server 573 * is restarted frequently (and thus the lease database is 574 * reread), the error could accumulate into something 575 * significant. 576 */ 577 return (guess); 578}
| 46#include "dhcpd.h" 47#include "dhctoken.h" 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 */ 63void 64skip_to_semi(FILE *cfile) 65{ 66 int brace_count = 0, token; 67 char *val; 68 69 do { 70 token = peek_token(&val, cfile); 71 if (token == RBRACE) { 72 if (brace_count) { 73 token = next_token(&val, cfile); 74 if (!--brace_count) 75 return; 76 } else 77 return; 78 } else if (token == LBRACE) { 79 brace_count++; 80 } else if (token == SEMI && !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 97int 98parse_semi(FILE *cfile) 99{ 100 int token; 101 char *val; 102 103 token = next_token(&val, cfile); 104 if (token != SEMI) { 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 */ 115char * 116parse_string(FILE *cfile) 117{ 118 char *val, *s; 119 int token; 120 121 token = next_token(&val, cfile); 122 if (token != 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 return (NULL); 134 return (s); 135} 136 137int 138parse_ip_addr(FILE *cfile, struct iaddr *addr) 139{ 140 addr->len = 4; 141 if (parse_numeric_aggregate(cfile, addr->iabuf, 142 &addr->len, DOT, 10, 8)) 143 return (1); 144 return (0); 145} 146 147/* 148 * hardware-parameter :== HARDWARE ETHERNET csns SEMI 149 * csns :== NUMBER | csns COLON NUMBER 150 */ 151void 152parse_hardware_param(FILE *cfile, struct hardware *hardware) 153{ 154 unsigned char *t; 155 int token, hlen; 156 char *val; 157 158 token = next_token(&val, cfile); 159 switch (token) { 160 case ETHERNET: 161 hardware->htype = HTYPE_ETHER; 162 break; 163 case TOKEN_RING: 164 hardware->htype = HTYPE_IEEE802; 165 break; 166 case FDDI: 167 hardware->htype = HTYPE_FDDI; 168 break; 169 default: 170 parse_warn("expecting a network hardware type"); 171 skip_to_semi(cfile); 172 return; 173 } 174 175 /* 176 * Parse the hardware address information. Technically, it 177 * would make a lot of sense to restrict the length of the data 178 * we'll accept here to the length of a particular hardware 179 * address type. Unfortunately, there are some broken clients 180 * out there that put bogus data in the chaddr buffer, and we 181 * accept that data in the lease file rather than simply failing 182 * on such clients. Yuck. 183 */ 184 hlen = 0; 185 t = parse_numeric_aggregate(cfile, NULL, &hlen, COLON, 16, 8); 186 if (!t) 187 return; 188 if (hlen > sizeof(hardware->haddr)) { 189 free(t); 190 parse_warn("hardware address too long"); 191 } else { 192 hardware->hlen = hlen; 193 memcpy((unsigned char *)&hardware->haddr[0], t, 194 hardware->hlen); 195 if (hlen < sizeof(hardware->haddr)) 196 memset(&hardware->haddr[hlen], 0, 197 sizeof(hardware->haddr) - hlen); 198 free(t); 199 } 200 201 token = next_token(&val, cfile); 202 if (token != SEMI) { 203 parse_warn("expecting semicolon."); 204 skip_to_semi(cfile); 205 } 206} 207 208/* 209 * lease-time :== NUMBER SEMI 210 */ 211void 212parse_lease_time(FILE *cfile, time_t *timep) 213{ 214 char *val; 215 int token; 216 217 token = next_token(&val, cfile); 218 if (token != NUMBER) { 219 parse_warn("Expecting numeric lease time"); 220 skip_to_semi(cfile); 221 return; 222 } 223 convert_num((unsigned char *)timep, val, 10, 32); 224 /* Unswap the number - convert_num returns stuff in NBO. */ 225 *timep = ntohl(*timep); /* XXX */ 226 227 parse_semi(cfile); 228} 229 230/* 231 * No BNF for numeric aggregates - that's defined by the caller. What 232 * this function does is to parse a sequence of numbers separated by the 233 * token specified in separator. If max is zero, any number of numbers 234 * will be parsed; otherwise, exactly max numbers are expected. Base 235 * and size tell us how to internalize the numbers once they've been 236 * tokenized. 237 */ 238unsigned char * 239parse_numeric_aggregate(FILE *cfile, unsigned char *buf, int *max, 240 int separator, int base, int size) 241{ 242 unsigned char *bufp = buf, *s = NULL; 243 int token, count = 0; 244 char *val, *t; 245 pair c = NULL; 246 247 if (!bufp && *max) { 248 bufp = malloc(*max * size / 8); 249 if (!bufp) 250 error("can't allocate space for numeric aggregate"); 251 } else 252 s = bufp; 253 254 do { 255 if (count) { 256 token = peek_token(&val, cfile); 257 if (token != separator) { 258 if (!*max) 259 break; 260 if (token != RBRACE && token != LBRACE) 261 token = next_token(&val, cfile); 262 parse_warn("too few numbers."); 263 if (token != SEMI) 264 skip_to_semi(cfile); 265 return (NULL); 266 } 267 token = next_token(&val, cfile); 268 } 269 token = next_token(&val, cfile); 270 271 if (token == EOF) { 272 parse_warn("unexpected end of file"); 273 break; 274 } 275 276 /* Allow NUMBER_OR_NAME if base is 16. */ 277 if (token != NUMBER && 278 (base != 16 || token != NUMBER_OR_NAME)) { 279 parse_warn("expecting numeric value."); 280 skip_to_semi(cfile); 281 return (NULL); 282 } 283 /* 284 * If we can, convert the number now; otherwise, build a 285 * linked list of all the numbers. 286 */ 287 if (s) { 288 convert_num(s, val, base, size); 289 s += size / 8; 290 } else { 291 t = malloc(strlen(val) + 1); 292 if (!t) 293 error("no temp space for number."); 294 strlcpy(t, val, strlen(val) + 1); 295 c = cons(t, c); 296 } 297 } while (++count != *max); 298 299 /* If we had to cons up a list, convert it now. */ 300 if (c) { 301 bufp = malloc(count * size / 8); 302 if (!bufp) 303 error("can't allocate space for numeric aggregate."); 304 s = bufp + count - size / 8; 305 *max = count; 306 } 307 while (c) { 308 pair cdr = c->cdr; 309 convert_num(s, (char *)c->car, base, size); 310 s -= size / 8; 311 /* Free up temp space. */ 312 free(c->car); 313 free(c); 314 c = cdr; 315 } 316 return (bufp); 317} 318 319void 320convert_num(unsigned char *buf, char *str, int base, int size) 321{ 322 int negative = 0, tval, max; 323 u_int32_t val = 0; 324 char *ptr = str; 325 326 if (*ptr == '-') { 327 negative = 1; 328 ptr++; 329 } 330 331 /* If base wasn't specified, figure it out from the data. */ 332 if (!base) { 333 if (ptr[0] == '0') { 334 if (ptr[1] == 'x') { 335 base = 16; 336 ptr += 2; 337 } else if (isascii(ptr[1]) && isdigit(ptr[1])) { 338 base = 8; 339 ptr += 1; 340 } else 341 base = 10; 342 } else 343 base = 10; 344 } 345 346 do { 347 tval = *ptr++; 348 /* XXX assumes ASCII... */ 349 if (tval >= 'a') 350 tval = tval - 'a' + 10; 351 else if (tval >= 'A') 352 tval = tval - 'A' + 10; 353 else if (tval >= '0') 354 tval -= '0'; 355 else { 356 warning("Bogus number: %s.", str); 357 break; 358 } 359 if (tval >= base) { 360 warning("Bogus number: %s: digit %d not in base %d", 361 str, tval, base); 362 break; 363 } 364 val = val * base + tval; 365 } while (*ptr); 366 367 if (negative) 368 max = (1 << (size - 1)); 369 else 370 max = (1 << (size - 1)) + ((1 << (size - 1)) - 1); 371 if (val > max) { 372 switch (base) { 373 case 8: 374 warning("value %s%o exceeds max (%d) for precision.", 375 negative ? "-" : "", val, max); 376 break; 377 case 16: 378 warning("value %s%x exceeds max (%d) for precision.", 379 negative ? "-" : "", val, max); 380 break; 381 default: 382 warning("value %s%u exceeds max (%d) for precision.", 383 negative ? "-" : "", val, max); 384 break; 385 } 386 } 387 388 if (negative) 389 switch (size) { 390 case 8: 391 *buf = -(unsigned long)val; 392 break; 393 case 16: 394 putShort(buf, -(unsigned long)val); 395 break; 396 case 32: 397 putLong(buf, -(unsigned long)val); 398 break; 399 default: 400 warning("Unexpected integer size: %d", size); 401 break; 402 } 403 else 404 switch (size) { 405 case 8: 406 *buf = (u_int8_t)val; 407 break; 408 case 16: 409 putUShort(buf, (u_int16_t)val); 410 break; 411 case 32: 412 putULong(buf, val); 413 break; 414 default: 415 warning("Unexpected integer size: %d", size); 416 break; 417 } 418} 419 420/* 421 * date :== NUMBER NUMBER SLASH NUMBER SLASH NUMBER 422 * NUMBER COLON NUMBER COLON NUMBER SEMI 423 * 424 * Dates are always in GMT; first number is day of week; next is 425 * year/month/day; next is hours:minutes:seconds on a 24-hour 426 * clock. 427 */ 428time_t 429parse_date(FILE *cfile) 430{ 431 static int months[11] = { 31, 59, 90, 120, 151, 181, 432 212, 243, 273, 304, 334 }; 433 int guess, token; 434 struct tm tm; 435 char *val; 436 437 /* Day of week... */ 438 token = next_token(&val, cfile); 439 if (token != NUMBER) { 440 parse_warn("numeric day of week expected."); 441 if (token != SEMI) 442 skip_to_semi(cfile); 443 return (0); 444 } 445 tm.tm_wday = atoi(val); 446 447 /* Year... */ 448 token = next_token(&val, cfile); 449 if (token != NUMBER) { 450 parse_warn("numeric year expected."); 451 if (token != SEMI) 452 skip_to_semi(cfile); 453 return (0); 454 } 455 tm.tm_year = atoi(val); 456 if (tm.tm_year > 1900) 457 tm.tm_year -= 1900; 458 459 /* Slash separating year from month... */ 460 token = next_token(&val, cfile); 461 if (token != SLASH) { 462 parse_warn("expected slash separating year from month."); 463 if (token != SEMI) 464 skip_to_semi(cfile); 465 return (0); 466 } 467 468 /* Month... */ 469 token = next_token(&val, cfile); 470 if (token != NUMBER) { 471 parse_warn("numeric month expected."); 472 if (token != SEMI) 473 skip_to_semi(cfile); 474 return (0); 475 } 476 tm.tm_mon = atoi(val) - 1; 477 478 /* Slash separating month from day... */ 479 token = next_token(&val, cfile); 480 if (token != SLASH) { 481 parse_warn("expected slash separating month from day."); 482 if (token != SEMI) 483 skip_to_semi(cfile); 484 return (0); 485 } 486 487 /* Month... */ 488 token = next_token(&val, cfile); 489 if (token != NUMBER) { 490 parse_warn("numeric day of month expected."); 491 if (token != SEMI) 492 skip_to_semi(cfile); 493 return (0); 494 } 495 tm.tm_mday = atoi(val); 496 497 /* Hour... */ 498 token = next_token(&val, cfile); 499 if (token != NUMBER) { 500 parse_warn("numeric hour expected."); 501 if (token != SEMI) 502 skip_to_semi(cfile); 503 return (0); 504 } 505 tm.tm_hour = atoi(val); 506 507 /* Colon separating hour from minute... */ 508 token = next_token(&val, cfile); 509 if (token != COLON) { 510 parse_warn("expected colon separating hour from minute."); 511 if (token != SEMI) 512 skip_to_semi(cfile); 513 return (0); 514 } 515 516 /* Minute... */ 517 token = next_token(&val, cfile); 518 if (token != NUMBER) { 519 parse_warn("numeric minute expected."); 520 if (token != SEMI) 521 skip_to_semi(cfile); 522 return (0); 523 } 524 tm.tm_min = atoi(val); 525 526 /* Colon separating minute from second... */ 527 token = next_token(&val, cfile); 528 if (token != COLON) { 529 parse_warn("expected colon separating hour from minute."); 530 if (token != SEMI) 531 skip_to_semi(cfile); 532 return (0); 533 } 534 535 /* Minute... */ 536 token = next_token(&val, cfile); 537 if (token != NUMBER) { 538 parse_warn("numeric minute expected."); 539 if (token != SEMI) 540 skip_to_semi(cfile); 541 return (0); 542 } 543 tm.tm_sec = atoi(val); 544 tm.tm_isdst = 0; 545 546 /* XXX: We assume that mktime does not use tm_yday. */ 547 tm.tm_yday = 0; 548 549 /* Make sure the date ends in a semicolon... */ 550 token = next_token(&val, cfile); 551 if (token != SEMI) { 552 parse_warn("semicolon expected."); 553 skip_to_semi(cfile); 554 return (0); 555 } 556 557 /* Guess the time value... */ 558 guess = ((((((365 * (tm.tm_year - 70) + /* Days in years since '70 */ 559 (tm.tm_year - 69) / 4 + /* Leap days since '70 */ 560 (tm.tm_mon /* Days in months this year */ 561 ? months[tm.tm_mon - 1] 562 : 0) + 563 (tm.tm_mon > 1 && /* Leap day this year */ 564 !((tm.tm_year - 72) & 3)) + 565 tm.tm_mday - 1) * 24) + /* Day of month */ 566 tm.tm_hour) * 60) + 567 tm.tm_min) * 60) + tm.tm_sec; 568 569 /* 570 * This guess could be wrong because of leap seconds or other 571 * weirdness we don't know about that the system does. For 572 * now, we're just going to accept the guess, but at some point 573 * it might be nice to do a successive approximation here to get 574 * an exact value. Even if the error is small, if the server 575 * is restarted frequently (and thus the lease database is 576 * reread), the error could accumulate into something 577 * significant. 578 */ 579 return (guess); 580}
|