1 /* 2 * Copyright (c) 1997, 1998, 1999, 2000 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include "krb5_locl.h" 35 RCSID("$Id: config_file.c,v 1.41 2000/08/16 07:40:36 assar Exp $"); 36 37 #ifndef HAVE_NETINFO 38 39 static int parse_section(char *p, krb5_config_section **s, 40 krb5_config_section **res, 41 char **error_message); 42 static int parse_binding(FILE *f, unsigned *lineno, char *p, 43 krb5_config_binding **b, 44 krb5_config_binding **parent, 45 char **error_message); 46 static int parse_list(FILE *f, unsigned *lineno, krb5_config_binding **parent, 47 char **error_message); 48 49 /* 50 * Parse a section: 51 * 52 * [section] 53 * foo = bar 54 * b = { 55 * a 56 * } 57 * ... 58 * 59 * starting at the line in `p', storing the resulting structure in 60 * `s' and hooking it into `parent'. 61 * Store the error message in `error_message'. 62 */ 63 64 static int 65 parse_section(char *p, krb5_config_section **s, krb5_config_section **parent, 66 char **error_message) 67 { 68 char *p1; 69 krb5_config_section *tmp; 70 71 p1 = strchr (p + 1, ']'); 72 if (p1 == NULL) { 73 *error_message = "missing ]"; 74 return -1; 75 } 76 *p1 = '\0'; 77 tmp = malloc(sizeof(*tmp)); 78 if (tmp == NULL) { 79 *error_message = "out of memory"; 80 return -1; 81 } 82 tmp->name = strdup(p+1); 83 if (tmp->name == NULL) { 84 *error_message = "out of memory"; 85 return -1; 86 } 87 tmp->type = krb5_config_list; 88 tmp->u.list = NULL; 89 tmp->next = NULL; 90 if (*s) 91 (*s)->next = tmp; 92 else 93 *parent = tmp; 94 *s = tmp; 95 return 0; 96 } 97 98 /* 99 * Parse a brace-enclosed list from `f', hooking in the structure at 100 * `parent'. 101 * Store the error message in `error_message'. 102 */ 103 104 static int 105 parse_list(FILE *f, unsigned *lineno, krb5_config_binding **parent, 106 char **error_message) 107 { 108 char buf[BUFSIZ]; 109 int ret; 110 krb5_config_binding *b = NULL; 111 unsigned beg_lineno = *lineno; 112 113 while(fgets(buf, sizeof(buf), f) != NULL) { 114 char *p; 115 116 ++*lineno; 117 if (buf[strlen(buf) - 1] == '\n') 118 buf[strlen(buf) - 1] = '\0'; 119 p = buf; 120 while(isspace((unsigned char)*p)) 121 ++p; 122 if (*p == '#' || *p == ';' || *p == '\0') 123 continue; 124 while(isspace((unsigned char)*p)) 125 ++p; 126 if (*p == '}') 127 return 0; 128 if (*p == '\0') 129 continue; 130 ret = parse_binding (f, lineno, p, &b, parent, error_message); 131 if (ret) 132 return ret; 133 } 134 *lineno = beg_lineno; 135 *error_message = "unclosed {"; 136 return -1; 137 } 138 139 /* 140 * 141 */ 142 143 static int 144 parse_binding(FILE *f, unsigned *lineno, char *p, 145 krb5_config_binding **b, krb5_config_binding **parent, 146 char **error_message) 147 { 148 krb5_config_binding *tmp; 149 char *p1, *p2; 150 int ret = 0; 151 152 p1 = p; 153 while (*p && *p != '=' && !isspace((unsigned char)*p)) 154 ++p; 155 if (*p == '\0') { 156 *error_message = "no ="; 157 return -1; 158 } 159 p2 = p; 160 while (isspace((unsigned char)*p)) 161 ++p; 162 if (*p != '=') { 163 *error_message = "no ="; 164 return -1; 165 } 166 ++p; 167 while(isspace((unsigned char)*p)) 168 ++p; 169 tmp = malloc(sizeof(*tmp)); 170 if (tmp == NULL) { 171 *error_message = "out of memory"; 172 return -1; 173 } 174 *p2 = '\0'; 175 tmp->name = strdup(p1); 176 tmp->next = NULL; 177 if (*p == '{') { 178 tmp->type = krb5_config_list; 179 tmp->u.list = NULL; 180 ret = parse_list (f, lineno, &tmp->u.list, error_message); 181 } else { 182 p1 = p; 183 p = p1 + strlen(p1); 184 while(p > p1 && isspace((unsigned char)*(p-1))) 185 --p; 186 *p = '\0'; 187 tmp->type = krb5_config_string; 188 tmp->u.string = strdup(p1); 189 } 190 if (*b) 191 (*b)->next = tmp; 192 else 193 *parent = tmp; 194 *b = tmp; 195 return ret; 196 } 197 198 /* 199 * Parse the config file `fname', generating the structures into `res' 200 * returning error messages in `error_message' 201 */ 202 203 krb5_error_code 204 krb5_config_parse_file_debug (const char *fname, 205 krb5_config_section **res, 206 unsigned *lineno, 207 char **error_message) 208 { 209 FILE *f; 210 krb5_config_section *s; 211 krb5_config_binding *b; 212 char buf[BUFSIZ]; 213 int ret = 0; 214 215 s = NULL; 216 b = NULL; 217 *lineno = 0; 218 f = fopen (fname, "r"); 219 if (f == NULL) { 220 *error_message = "cannot open file"; 221 return ENOENT; 222 } 223 *res = NULL; 224 while (fgets(buf, sizeof(buf), f) != NULL) { 225 char *p; 226 227 ++*lineno; 228 if(buf[strlen(buf) - 1] == '\n') 229 buf[strlen(buf) - 1] = '\0'; 230 p = buf; 231 while(isspace((unsigned char)*p)) 232 ++p; 233 if (*p == '#' || *p == ';') 234 continue; 235 if (*p == '[') { 236 ret = parse_section(p, &s, res, error_message); 237 if (ret) { 238 goto out; 239 } 240 b = NULL; 241 } else if (*p == '}') { 242 *error_message = "unmatched }"; 243 ret = -1; 244 goto out; 245 } else if(*p != '\0') { 246 ret = parse_binding(f, lineno, p, &b, &s->u.list, error_message); 247 if (ret) 248 goto out; 249 } 250 } 251 out: 252 fclose (f); 253 return ret; 254 } 255 256 krb5_error_code 257 krb5_config_parse_file (const char *fname, krb5_config_section **res) 258 { 259 char *foo; 260 unsigned lineno; 261 262 return krb5_config_parse_file_debug (fname, res, &lineno, &foo); 263 } 264 265 #endif /* !HAVE_NETINFO */ 266 267 static void 268 free_binding (krb5_context context, krb5_config_binding *b) 269 { 270 krb5_config_binding *next_b; 271 272 while (b) { 273 free (b->name); 274 if (b->type == krb5_config_string) 275 free (b->u.string); 276 else if (b->type == krb5_config_list) 277 free_binding (context, b->u.list); 278 else 279 krb5_abortx(context, "unknown binding type (%d) in free_binding", 280 b->type); 281 next_b = b->next; 282 free (b); 283 b = next_b; 284 } 285 } 286 287 krb5_error_code 288 krb5_config_file_free (krb5_context context, krb5_config_section *s) 289 { 290 free_binding (context, s); 291 return 0; 292 } 293 294 const void * 295 krb5_config_get_next (krb5_context context, 296 krb5_config_section *c, 297 krb5_config_binding **pointer, 298 int type, 299 ...) 300 { 301 const char *ret; 302 va_list args; 303 304 va_start(args, type); 305 ret = krb5_config_vget_next (context, c, pointer, type, args); 306 va_end(args); 307 return ret; 308 } 309 310 const void * 311 krb5_config_vget_next (krb5_context context, 312 krb5_config_section *c, 313 krb5_config_binding **pointer, 314 int type, 315 va_list args) 316 { 317 krb5_config_binding *b; 318 const char *p; 319 320 if(c == NULL) 321 c = context->cf; 322 323 if (c == NULL) 324 return NULL; 325 326 if (*pointer == NULL) { 327 b = (c != NULL) ? c : context->cf; 328 p = va_arg(args, const char *); 329 if (p == NULL) 330 return NULL; 331 } else { 332 b = *pointer; 333 p = b->name; 334 b = b->next; 335 } 336 337 while (b) { 338 if (strcmp (b->name, p) == 0) { 339 if (*pointer == NULL) 340 p = va_arg(args, const char *); 341 else 342 p = NULL; 343 if (type == b->type && p == NULL) { 344 *pointer = b; 345 return b->u.generic; 346 } else if(b->type == krb5_config_list && p != NULL) { 347 b = b->u.list; 348 } else { 349 return NULL; 350 } 351 } else { 352 b = b->next; 353 } 354 } 355 return NULL; 356 } 357 358 const void * 359 krb5_config_get (krb5_context context, 360 krb5_config_section *c, 361 int type, 362 ...) 363 { 364 const void *ret; 365 va_list args; 366 367 va_start(args, type); 368 ret = krb5_config_vget (context, c, type, args); 369 va_end(args); 370 return ret; 371 } 372 373 const void * 374 krb5_config_vget (krb5_context context, 375 krb5_config_section *c, 376 int type, 377 va_list args) 378 { 379 krb5_config_binding *foo = NULL; 380 381 return krb5_config_vget_next (context, c, &foo, type, args); 382 } 383 384 const krb5_config_binding * 385 krb5_config_get_list (krb5_context context, 386 krb5_config_section *c, 387 ...) 388 { 389 const krb5_config_binding *ret; 390 va_list args; 391 392 va_start(args, c); 393 ret = krb5_config_vget_list (context, c, args); 394 va_end(args); 395 return ret; 396 } 397 398 const krb5_config_binding * 399 krb5_config_vget_list (krb5_context context, 400 krb5_config_section *c, 401 va_list args) 402 { 403 return krb5_config_vget (context, c, krb5_config_list, args); 404 } 405 406 const char * 407 krb5_config_get_string (krb5_context context, 408 krb5_config_section *c, 409 ...) 410 { 411 const char *ret; 412 va_list args; 413 414 va_start(args, c); 415 ret = krb5_config_vget_string (context, c, args); 416 va_end(args); 417 return ret; 418 } 419 420 const char * 421 krb5_config_vget_string (krb5_context context, 422 krb5_config_section *c, 423 va_list args) 424 { 425 return krb5_config_vget (context, c, krb5_config_string, args); 426 } 427 428 const char * 429 krb5_config_vget_string_default (krb5_context context, 430 krb5_config_section *c, 431 const char *def_value, 432 va_list args) 433 { 434 const char *ret; 435 436 ret = krb5_config_vget_string (context, c, args); 437 if (ret == NULL) 438 ret = def_value; 439 return ret; 440 } 441 442 const char * 443 krb5_config_get_string_default (krb5_context context, 444 krb5_config_section *c, 445 const char *def_value, 446 ...) 447 { 448 const char *ret; 449 va_list args; 450 451 va_start(args, def_value); 452 ret = krb5_config_vget_string_default (context, c, def_value, args); 453 va_end(args); 454 return ret; 455 } 456 457 char ** 458 krb5_config_vget_strings(krb5_context context, 459 krb5_config_section *c, 460 va_list args) 461 { 462 char **strings = NULL; 463 int nstr = 0; 464 krb5_config_binding *b = NULL; 465 const char *p; 466 467 while((p = krb5_config_vget_next(context, c, &b, 468 krb5_config_string, args))) { 469 char *tmp = strdup(p); 470 char *pos = NULL; 471 char *s; 472 if(tmp == NULL) 473 goto cleanup; 474 s = strtok_r(tmp, " \t", &pos); 475 while(s){ 476 char **tmp = realloc(strings, (nstr + 1) * sizeof(*strings)); 477 if(tmp == NULL) 478 goto cleanup; 479 strings = tmp; 480 strings[nstr] = strdup(s); 481 nstr++; 482 if(strings[nstr-1] == NULL) 483 goto cleanup; 484 s = strtok_r(NULL, " \t", &pos); 485 } 486 free(tmp); 487 } 488 if(nstr){ 489 char **tmp = realloc(strings, (nstr + 1) * sizeof(*strings)); 490 if(strings == NULL) 491 goto cleanup; 492 strings = tmp; 493 strings[nstr] = NULL; 494 } 495 return strings; 496 cleanup: 497 while(nstr--) 498 free(strings[nstr]); 499 free(strings); 500 return NULL; 501 502 } 503 504 char** 505 krb5_config_get_strings(krb5_context context, 506 krb5_config_section *c, 507 ...) 508 { 509 va_list ap; 510 char **ret; 511 va_start(ap, c); 512 ret = krb5_config_vget_strings(context, c, ap); 513 va_end(ap); 514 return ret; 515 } 516 517 void 518 krb5_config_free_strings(char **strings) 519 { 520 char **s = strings; 521 while(s && *s){ 522 free(*s); 523 s++; 524 } 525 free(strings); 526 } 527 528 krb5_boolean 529 krb5_config_vget_bool_default (krb5_context context, 530 krb5_config_section *c, 531 krb5_boolean def_value, 532 va_list args) 533 { 534 const char *str; 535 str = krb5_config_vget_string (context, c, args); 536 if(str == NULL) 537 return def_value; 538 if(strcasecmp(str, "yes") == 0 || 539 strcasecmp(str, "true") == 0 || 540 atoi(str)) return TRUE; 541 return FALSE; 542 } 543 544 krb5_boolean 545 krb5_config_vget_bool (krb5_context context, 546 krb5_config_section *c, 547 va_list args) 548 { 549 return krb5_config_vget_bool_default (context, c, FALSE, args); 550 } 551 552 krb5_boolean 553 krb5_config_get_bool_default (krb5_context context, 554 krb5_config_section *c, 555 krb5_boolean def_value, 556 ...) 557 { 558 va_list ap; 559 krb5_boolean ret; 560 va_start(ap, def_value); 561 ret = krb5_config_vget_bool_default(context, c, def_value, ap); 562 va_end(ap); 563 return ret; 564 } 565 566 krb5_boolean 567 krb5_config_get_bool (krb5_context context, 568 krb5_config_section *c, 569 ...) 570 { 571 va_list ap; 572 krb5_boolean ret; 573 va_start(ap, c); 574 ret = krb5_config_vget_bool (context, c, ap); 575 va_end(ap); 576 return ret; 577 } 578 579 int 580 krb5_config_vget_time_default (krb5_context context, 581 krb5_config_section *c, 582 int def_value, 583 va_list args) 584 { 585 const char *str; 586 str = krb5_config_vget_string (context, c, args); 587 if(str == NULL) 588 return def_value; 589 return parse_time (str, NULL); 590 } 591 592 int 593 krb5_config_vget_time (krb5_context context, 594 krb5_config_section *c, 595 va_list args) 596 { 597 return krb5_config_vget_time_default (context, c, -1, args); 598 } 599 600 int 601 krb5_config_get_time_default (krb5_context context, 602 krb5_config_section *c, 603 int def_value, 604 ...) 605 { 606 va_list ap; 607 int ret; 608 va_start(ap, def_value); 609 ret = krb5_config_vget_time_default(context, c, def_value, ap); 610 va_end(ap); 611 return ret; 612 } 613 614 int 615 krb5_config_get_time (krb5_context context, 616 krb5_config_section *c, 617 ...) 618 { 619 va_list ap; 620 int ret; 621 va_start(ap, c); 622 ret = krb5_config_vget_time (context, c, ap); 623 va_end(ap); 624 return ret; 625 } 626 627 628 int 629 krb5_config_vget_int_default (krb5_context context, 630 krb5_config_section *c, 631 int def_value, 632 va_list args) 633 { 634 const char *str; 635 str = krb5_config_vget_string (context, c, args); 636 if(str == NULL) 637 return def_value; 638 else { 639 char *endptr; 640 long l; 641 l = strtol(str, &endptr, 0); 642 if (endptr == str) 643 return def_value; 644 else 645 return l; 646 } 647 } 648 649 int 650 krb5_config_vget_int (krb5_context context, 651 krb5_config_section *c, 652 va_list args) 653 { 654 return krb5_config_vget_int_default (context, c, -1, args); 655 } 656 657 int 658 krb5_config_get_int_default (krb5_context context, 659 krb5_config_section *c, 660 int def_value, 661 ...) 662 { 663 va_list ap; 664 int ret; 665 va_start(ap, def_value); 666 ret = krb5_config_vget_int_default(context, c, def_value, ap); 667 va_end(ap); 668 return ret; 669 } 670 671 int 672 krb5_config_get_int (krb5_context context, 673 krb5_config_section *c, 674 ...) 675 { 676 va_list ap; 677 int ret; 678 va_start(ap, c); 679 ret = krb5_config_vget_int (context, c, ap); 680 va_end(ap); 681 return ret; 682 } 683 684 #ifdef TEST 685 686 static int print_list (krb5_context context, FILE *f, 687 krb5_config_binding *l, unsigned level); 688 static int print_binding (krb5_context context, FILE *f, 689 krb5_config_binding *b, unsigned level); 690 static int print_section (krb5_context context, FILE *f, 691 krb5_config_section *s, unsigned level); 692 static int print_config (krb5_context context, FILE *f, 693 krb5_config_section *c); 694 695 static void 696 tab (FILE *f, unsigned count) 697 { 698 while(count--) 699 fprintf (f, "\t"); 700 } 701 702 static int 703 print_list (krb5_context context, 704 FILE *f, 705 krb5_config_binding *l, 706 unsigned level) 707 { 708 while(l) { 709 print_binding (context, f, l, level); 710 l = l->next; 711 } 712 return 0; 713 } 714 715 static int 716 print_binding (krb5_context context, 717 FILE *f, 718 krb5_config_binding *b, 719 unsigned level) 720 { 721 tab (f, level); 722 fprintf (f, "%s = ", b->name); 723 if (b->type == krb5_config_string) 724 fprintf (f, "%s\n", b->u.string); 725 else if (b->type == krb5_config_list) { 726 fprintf (f, "{\n"); 727 print_list (f, b->u.list, level + 1); 728 tab (f, level); 729 fprintf (f, "}\n"); 730 } else 731 krb5_abortx(context, "unknown binding type (%d) in print_binding", 732 b->type); 733 return 0; 734 } 735 736 static int 737 print_section (FILE *f, krb5_config_section *s, unsigned level) 738 { 739 fprintf (f, "[%s]\n", s->name); 740 print_list (f, s->u.list, level + 1); 741 return 0; 742 } 743 744 static int 745 print_config (FILE *f, krb5_config_section *c) 746 { 747 while (c) { 748 print_section (f, c, 0); 749 c = c->next; 750 } 751 return 0; 752 } 753 754 755 int 756 main(void) 757 { 758 krb5_config_section *c; 759 760 printf ("%d\n", krb5_config_parse_file ("/etc/krb5.conf", &c)); 761 print_config (stdout, c); 762 printf ("[libdefaults]ticket_lifetime = %s\n", 763 krb5_config_get_string (context, c, 764 "libdefaults", 765 "ticket_lifetime", 766 NULL)); 767 printf ("[realms]foo = %s\n", 768 krb5_config_get_string (context, c, 769 "realms", 770 "foo", 771 NULL)); 772 printf ("[realms]ATHENA.MIT.EDU/v4_instance_convert/lithium = %s\n", 773 krb5_config_get_string (context, c, 774 "realms", 775 "ATHENA.MIT.EDU", 776 "v4_instance_convert", 777 "lithium", 778 NULL)); 779 return 0; 780 } 781 782 #endif /* TEST */ 783