1 /* $OpenBSD: conf_def.c,v 1.31 2015/07/18 22:42:09 beck Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 /* Part of the code in here was originally in conf.c, which is now removed */ 60 61 #include <stdio.h> 62 #include <string.h> 63 64 #include <openssl/buffer.h> 65 #include <openssl/conf.h> 66 #include <openssl/conf_api.h> 67 #include <openssl/err.h> 68 #include <openssl/lhash.h> 69 #include <openssl/stack.h> 70 71 #include "conf_def.h" 72 73 static char *eat_ws(CONF *conf, char *p); 74 static char *eat_alpha_numeric(CONF *conf, char *p); 75 static void clear_comments(CONF *conf, char *p); 76 static int str_copy(CONF *conf, char *section, char **to, char *from); 77 static char *scan_quote(CONF *conf, char *p); 78 static char *scan_dquote(CONF *conf, char *p); 79 #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2))) 80 81 static CONF *def_create(CONF_METHOD *meth); 82 static int def_init_default(CONF *conf); 83 static int def_init_WIN32(CONF *conf); 84 static int def_destroy(CONF *conf); 85 static int def_destroy_data(CONF *conf); 86 static int def_load(CONF *conf, const char *name, long *eline); 87 static int def_load_bio(CONF *conf, BIO *bp, long *eline); 88 static int def_dump(const CONF *conf, BIO *bp); 89 static int def_is_number(const CONF *conf, char c); 90 static int def_to_int(const CONF *conf, char c); 91 92 static CONF_METHOD default_method = { 93 .name = "OpenSSL default", 94 .create = def_create, 95 .init = def_init_default, 96 .destroy = def_destroy, 97 .destroy_data = def_destroy_data, 98 .load_bio = def_load_bio, 99 .dump = def_dump, 100 .is_number = def_is_number, 101 .to_int = def_to_int, 102 .load = def_load 103 }; 104 105 static CONF_METHOD WIN32_method = { 106 "WIN32", 107 def_create, 108 def_init_WIN32, 109 def_destroy, 110 def_destroy_data, 111 def_load_bio, 112 def_dump, 113 def_is_number, 114 def_to_int, 115 def_load 116 }; 117 118 CONF_METHOD * 119 NCONF_default(void) 120 { 121 return &default_method; 122 } 123 124 CONF_METHOD * 125 NCONF_WIN32(void) 126 { 127 return &WIN32_method; 128 } 129 130 static CONF * 131 def_create(CONF_METHOD *meth) 132 { 133 CONF *ret; 134 135 ret = malloc(sizeof(CONF) + sizeof(unsigned short *)); 136 if (ret) 137 if (meth->init(ret) == 0) { 138 free(ret); 139 ret = NULL; 140 } 141 return ret; 142 } 143 144 static int 145 def_init_default(CONF *conf) 146 { 147 if (conf == NULL) 148 return 0; 149 150 conf->meth = &default_method; 151 conf->meth_data = CONF_type_default; 152 conf->data = NULL; 153 154 return 1; 155 } 156 157 static int 158 def_init_WIN32(CONF *conf) 159 { 160 if (conf == NULL) 161 return 0; 162 163 conf->meth = &WIN32_method; 164 conf->meth_data = (void *)CONF_type_win32; 165 conf->data = NULL; 166 167 return 1; 168 } 169 170 static int 171 def_destroy(CONF *conf) 172 { 173 if (def_destroy_data(conf)) { 174 free(conf); 175 return 1; 176 } 177 return 0; 178 } 179 180 static int 181 def_destroy_data(CONF *conf) 182 { 183 if (conf == NULL) 184 return 0; 185 _CONF_free_data(conf); 186 return 1; 187 } 188 189 static int 190 def_load(CONF *conf, const char *name, long *line) 191 { 192 int ret; 193 BIO *in = NULL; 194 195 in = BIO_new_file(name, "rb"); 196 if (in == NULL) { 197 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE) 198 CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE); 199 else 200 CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB); 201 return 0; 202 } 203 204 ret = def_load_bio(conf, in, line); 205 BIO_free(in); 206 207 return ret; 208 } 209 210 static int 211 def_load_bio(CONF *conf, BIO *in, long *line) 212 { 213 /* The macro BUFSIZE conflicts with a system macro in VxWorks */ 214 #define CONFBUFSIZE 512 215 int bufnum = 0, i, ii; 216 BUF_MEM *buff = NULL; 217 char *s, *p, *end; 218 int again; 219 long eline = 0; 220 CONF_VALUE *v = NULL, *tv; 221 CONF_VALUE *sv = NULL; 222 char *section = NULL, *buf; 223 char *start, *psection, *pname; 224 void *h = (void *)(conf->data); 225 226 if ((buff = BUF_MEM_new()) == NULL) { 227 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB); 228 goto err; 229 } 230 231 section = strdup("default"); 232 if (section == NULL) { 233 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE); 234 goto err; 235 } 236 237 if (_CONF_new_data(conf) == 0) { 238 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE); 239 goto err; 240 } 241 242 sv = _CONF_new_section(conf, section); 243 if (sv == NULL) { 244 CONFerr(CONF_F_DEF_LOAD_BIO, 245 CONF_R_UNABLE_TO_CREATE_NEW_SECTION); 246 goto err; 247 } 248 249 bufnum = 0; 250 again = 0; 251 for (;;) { 252 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) { 253 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB); 254 goto err; 255 } 256 p = &(buff->data[bufnum]); 257 *p = '\0'; 258 BIO_gets(in, p, CONFBUFSIZE - 1); 259 p[CONFBUFSIZE - 1] = '\0'; 260 ii = i = strlen(p); 261 if (i == 0 && !again) 262 break; 263 again = 0; 264 while (i > 0) { 265 if ((p[i - 1] != '\r') && (p[i - 1] != '\n')) 266 break; 267 else 268 i--; 269 } 270 /* we removed some trailing stuff so there is a new 271 * line on the end. */ 272 if (ii && i == ii) 273 again = 1; /* long line */ 274 else { 275 p[i] = '\0'; 276 eline++; /* another input line */ 277 } 278 279 /* we now have a line with trailing \r\n removed */ 280 281 /* i is the number of bytes */ 282 bufnum += i; 283 284 v = NULL; 285 /* check for line continuation */ 286 if (bufnum >= 1) { 287 /* If we have bytes and the last char '\\' and 288 * second last char is not '\\' */ 289 p = &(buff->data[bufnum - 1]); 290 if (IS_ESC(conf, p[0]) && 291 ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) { 292 bufnum--; 293 again = 1; 294 } 295 } 296 if (again) 297 continue; 298 bufnum = 0; 299 buf = buff->data; 300 301 clear_comments(conf, buf); 302 s = eat_ws(conf, buf); 303 if (IS_EOF(conf, *s)) 304 continue; /* blank line */ 305 if (*s == '[') { 306 char *ss; 307 308 s++; 309 start = eat_ws(conf, s); 310 ss = start; 311 again: 312 end = eat_alpha_numeric(conf, ss); 313 p = eat_ws(conf, end); 314 if (*p != ']') { 315 if (*p != '\0' && ss != p) { 316 ss = p; 317 goto again; 318 } 319 CONFerr(CONF_F_DEF_LOAD_BIO, 320 CONF_R_MISSING_CLOSE_SQUARE_BRACKET); 321 goto err; 322 } 323 *end = '\0'; 324 if (!str_copy(conf, NULL, §ion, start)) 325 goto err; 326 if ((sv = _CONF_get_section(conf, section)) == NULL) 327 sv = _CONF_new_section(conf, section); 328 if (sv == NULL) { 329 CONFerr(CONF_F_DEF_LOAD_BIO, 330 CONF_R_UNABLE_TO_CREATE_NEW_SECTION); 331 goto err; 332 } 333 continue; 334 } else { 335 pname = s; 336 psection = NULL; 337 end = eat_alpha_numeric(conf, s); 338 if ((end[0] == ':') && (end[1] == ':')) { 339 *end = '\0'; 340 end += 2; 341 psection = pname; 342 pname = end; 343 end = eat_alpha_numeric(conf, end); 344 } 345 p = eat_ws(conf, end); 346 if (*p != '=') { 347 CONFerr(CONF_F_DEF_LOAD_BIO, 348 CONF_R_MISSING_EQUAL_SIGN); 349 goto err; 350 } 351 *end = '\0'; 352 p++; 353 start = eat_ws(conf, p); 354 while (!IS_EOF(conf, *p)) 355 p++; 356 p--; 357 while ((p != start) && (IS_WS(conf, *p))) 358 p--; 359 p++; 360 *p = '\0'; 361 362 if (!(v = malloc(sizeof(CONF_VALUE)))) { 363 CONFerr(CONF_F_DEF_LOAD_BIO, 364 ERR_R_MALLOC_FAILURE); 365 goto err; 366 } 367 if (psection == NULL) 368 psection = section; 369 v->name = strdup(pname); 370 v->value = NULL; 371 if (v->name == NULL) { 372 CONFerr(CONF_F_DEF_LOAD_BIO, 373 ERR_R_MALLOC_FAILURE); 374 goto err; 375 } 376 if (!str_copy(conf, psection, &(v->value), start)) 377 goto err; 378 379 if (strcmp(psection, section) != 0) { 380 if ((tv = _CONF_get_section(conf, psection)) 381 == NULL) 382 tv = _CONF_new_section(conf, psection); 383 if (tv == NULL) { 384 CONFerr(CONF_F_DEF_LOAD_BIO, 385 CONF_R_UNABLE_TO_CREATE_NEW_SECTION); 386 goto err; 387 } 388 } else 389 tv = sv; 390 391 if (_CONF_add_string(conf, tv, v) == 0) { 392 CONFerr(CONF_F_DEF_LOAD_BIO, 393 ERR_R_MALLOC_FAILURE); 394 goto err; 395 } 396 v = NULL; 397 } 398 } 399 if (buff != NULL) 400 BUF_MEM_free(buff); 401 free(section); 402 return (1); 403 404 err: 405 if (buff != NULL) 406 BUF_MEM_free(buff); 407 free(section); 408 if (line != NULL) 409 *line = eline; 410 ERR_asprintf_error_data("line %ld", eline); 411 if ((h != conf->data) && (conf->data != NULL)) { 412 CONF_free(conf->data); 413 conf->data = NULL; 414 } 415 if (v != NULL) { 416 free(v->name); 417 free(v->value); 418 free(v); 419 } 420 return (0); 421 } 422 423 static void 424 clear_comments(CONF *conf, char *p) 425 { 426 for (;;) { 427 if (IS_FCOMMENT(conf, *p)) { 428 *p = '\0'; 429 return; 430 } 431 if (!IS_WS(conf, *p)) { 432 break; 433 } 434 p++; 435 } 436 437 for (;;) { 438 if (IS_COMMENT(conf, *p)) { 439 *p = '\0'; 440 return; 441 } 442 if (IS_DQUOTE(conf, *p)) { 443 p = scan_dquote(conf, p); 444 continue; 445 } 446 if (IS_QUOTE(conf, *p)) { 447 p = scan_quote(conf, p); 448 continue; 449 } 450 if (IS_ESC(conf, *p)) { 451 p = scan_esc(conf, p); 452 continue; 453 } 454 if (IS_EOF(conf, *p)) 455 return; 456 else 457 p++; 458 } 459 } 460 461 static int 462 str_copy(CONF *conf, char *section, char **pto, char *from) 463 { 464 int q, r,rr = 0, to = 0, len = 0; 465 char *s, *e, *rp, *p, *rrp, *np, *cp, v; 466 BUF_MEM *buf; 467 468 if ((buf = BUF_MEM_new()) == NULL) 469 return (0); 470 471 len = strlen(from) + 1; 472 if (!BUF_MEM_grow(buf, len)) 473 goto err; 474 475 for (;;) { 476 if (IS_QUOTE(conf, *from)) { 477 q = *from; 478 from++; 479 while (!IS_EOF(conf, *from) && (*from != q)) { 480 if (IS_ESC(conf, *from)) { 481 from++; 482 if (IS_EOF(conf, *from)) 483 break; 484 } 485 buf->data[to++] = *(from++); 486 } 487 if (*from == q) 488 from++; 489 } else if (IS_DQUOTE(conf, *from)) { 490 q = *from; 491 from++; 492 while (!IS_EOF(conf, *from)) { 493 if (*from == q) { 494 if (*(from + 1) == q) { 495 from++; 496 } else { 497 break; 498 } 499 } 500 buf->data[to++] = *(from++); 501 } 502 if (*from == q) 503 from++; 504 } else if (IS_ESC(conf, *from)) { 505 from++; 506 v = *(from++); 507 if (IS_EOF(conf, v)) 508 break; 509 else if (v == 'r') 510 v = '\r'; 511 else if (v == 'n') 512 v = '\n'; 513 else if (v == 'b') 514 v = '\b'; 515 else if (v == 't') 516 v = '\t'; 517 buf->data[to++] = v; 518 } else if (IS_EOF(conf, *from)) 519 break; 520 else if (*from == '$') { 521 /* try to expand it */ 522 rrp = NULL; 523 s = &(from[1]); 524 if (*s == '{') 525 q = '}'; 526 else if (*s == '(') 527 q = ')'; 528 else 529 q = 0; 530 531 if (q) 532 s++; 533 cp = section; 534 e = np = s; 535 while (IS_ALPHA_NUMERIC(conf, *e)) 536 e++; 537 if ((e[0] == ':') && (e[1] == ':')) { 538 cp = np; 539 rrp = e; 540 rr = *e; 541 *rrp = '\0'; 542 e += 2; 543 np = e; 544 while (IS_ALPHA_NUMERIC(conf, *e)) 545 e++; 546 } 547 r = *e; 548 *e = '\0'; 549 rp = e; 550 if (q) { 551 if (r != q) { 552 CONFerr(CONF_F_STR_COPY, 553 CONF_R_NO_CLOSE_BRACE); 554 goto err; 555 } 556 e++; 557 } 558 /* So at this point we have 559 * np which is the start of the name string which is 560 * '\0' terminated. 561 * cp which is the start of the section string which is 562 * '\0' terminated. 563 * e is the 'next point after'. 564 * r and rr are the chars replaced by the '\0' 565 * rp and rrp is where 'r' and 'rr' came from. 566 */ 567 p = _CONF_get_string(conf, cp, np); 568 if (rrp != NULL) 569 *rrp = rr; 570 *rp = r; 571 if (p == NULL) { 572 CONFerr(CONF_F_STR_COPY, 573 CONF_R_VARIABLE_HAS_NO_VALUE); 574 goto err; 575 } 576 if (!BUF_MEM_grow_clean(buf, 577 (strlen(p) + buf->length - (e - from)))) { 578 CONFerr(CONF_F_STR_COPY, 579 CONF_R_MODULE_INITIALIZATION_ERROR); 580 goto err; 581 } 582 while (*p) 583 buf->data[to++] = *(p++); 584 585 /* Since we change the pointer 'from', we also have 586 to change the perceived length of the string it 587 points at. /RL */ 588 len -= e - from; 589 from = e; 590 591 /* In case there were no braces or parenthesis around 592 the variable reference, we have to put back the 593 character that was replaced with a '\0'. /RL */ 594 *rp = r; 595 } else 596 buf->data[to++] = *(from++); 597 } 598 buf->data[to]='\0'; 599 free(*pto); 600 *pto = buf->data; 601 free(buf); 602 return (1); 603 604 err: 605 if (buf != NULL) 606 BUF_MEM_free(buf); 607 return (0); 608 } 609 610 static char * 611 eat_ws(CONF *conf, char *p) 612 { 613 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p))) 614 p++; 615 return (p); 616 } 617 618 static char * 619 eat_alpha_numeric(CONF *conf, char *p) 620 { 621 for (;;) { 622 if (IS_ESC(conf, *p)) { 623 p = scan_esc(conf, p); 624 continue; 625 } 626 if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p)) 627 return (p); 628 p++; 629 } 630 } 631 632 static char * 633 scan_quote(CONF *conf, char *p) 634 { 635 int q = *p; 636 637 p++; 638 while (!(IS_EOF(conf, *p)) && (*p != q)) { 639 if (IS_ESC(conf, *p)) { 640 p++; 641 if (IS_EOF(conf, *p)) 642 return (p); 643 } 644 p++; 645 } 646 if (*p == q) 647 p++; 648 return (p); 649 } 650 651 652 static char * 653 scan_dquote(CONF *conf, char *p) 654 { 655 int q = *p; 656 657 p++; 658 while (!(IS_EOF(conf, *p))) { 659 if (*p == q) { 660 if (*(p + 1) == q) { 661 p++; 662 } else { 663 break; 664 } 665 } 666 p++; 667 } 668 if (*p == q) 669 p++; 670 return (p); 671 } 672 673 static void 674 dump_value_doall_arg(CONF_VALUE *a, BIO *out) 675 { 676 if (a->name) 677 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value); 678 else 679 BIO_printf(out, "[[%s]]\n", a->section); 680 } 681 682 static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value, CONF_VALUE, BIO) 683 684 static int 685 def_dump(const CONF *conf, BIO *out) 686 { 687 lh_CONF_VALUE_doall_arg(conf->data, LHASH_DOALL_ARG_FN(dump_value), 688 BIO, out); 689 return 1; 690 } 691 692 static int 693 def_is_number(const CONF *conf, char c) 694 { 695 return IS_NUMBER(conf, c); 696 } 697 698 static int 699 def_to_int(const CONF *conf, char c) 700 { 701 return c - '0'; 702 } 703