1/* $OpenBSD: conf_def.c,v 1.34 2024/04/09 13:56:30 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#define MAX_CONF_VALUE_LENGTH 65536 74 75static char *eat_ws(CONF *conf, char *p); 76static char *eat_alpha_numeric(CONF *conf, char *p); 77static void clear_comments(CONF *conf, char *p); 78static int str_copy(CONF *conf, char *section, char **to, char *from); 79static char *scan_quote(CONF *conf, char *p); 80static char *scan_dquote(CONF *conf, char *p); 81#define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2))) 82 83static CONF *def_create(CONF_METHOD *meth); 84static int def_init_default(CONF *conf); 85static int def_init_WIN32(CONF *conf); 86static int def_destroy(CONF *conf); 87static int def_destroy_data(CONF *conf); 88static int def_load(CONF *conf, const char *name, long *eline); 89static int def_load_bio(CONF *conf, BIO *bp, long *eline); 90static int def_dump(const CONF *conf, BIO *bp); 91static int def_is_number(const CONF *conf, char c); 92static int def_to_int(const CONF *conf, char c); 93 94static CONF_METHOD default_method = { 95 .name = "OpenSSL default", 96 .create = def_create, 97 .init = def_init_default, 98 .destroy = def_destroy, 99 .destroy_data = def_destroy_data, 100 .load_bio = def_load_bio, 101 .dump = def_dump, 102 .is_number = def_is_number, 103 .to_int = def_to_int, 104 .load = def_load 105}; 106 107static CONF_METHOD WIN32_method = { 108 "WIN32", 109 def_create, 110 def_init_WIN32, 111 def_destroy, 112 def_destroy_data, 113 def_load_bio, 114 def_dump, 115 def_is_number, 116 def_to_int, 117 def_load 118}; 119 120CONF_METHOD * 121NCONF_default(void) 122{ 123 return &default_method; 124} 125LCRYPTO_ALIAS(NCONF_default); 126 127CONF_METHOD * 128NCONF_WIN32(void) 129{ 130 return &WIN32_method; 131} 132LCRYPTO_ALIAS(NCONF_WIN32); 133 134static CONF * 135def_create(CONF_METHOD *meth) 136{ 137 CONF *ret; 138 139 ret = malloc(sizeof(CONF) + sizeof(unsigned short *)); 140 if (ret) 141 if (meth->init(ret) == 0) { 142 free(ret); 143 ret = NULL; 144 } 145 return ret; 146} 147 148static int 149def_init_default(CONF *conf) 150{ 151 if (conf == NULL) 152 return 0; 153 154 conf->meth = &default_method; 155 conf->meth_data = CONF_type_default; 156 conf->data = NULL; 157 158 return 1; 159} 160 161static int 162def_init_WIN32(CONF *conf) 163{ 164 if (conf == NULL) 165 return 0; 166 167 conf->meth = &WIN32_method; 168 conf->meth_data = (void *)CONF_type_win32; 169 conf->data = NULL; 170 171 return 1; 172} 173 174static int 175def_destroy(CONF *conf) 176{ 177 if (def_destroy_data(conf)) { 178 free(conf); 179 return 1; 180 } 181 return 0; 182} 183 184static int 185def_destroy_data(CONF *conf) 186{ 187 if (conf == NULL) 188 return 0; 189 _CONF_free_data(conf); 190 return 1; 191} 192 193static int 194def_load(CONF *conf, const char *name, long *line) 195{ 196 int ret; 197 BIO *in = NULL; 198 199 in = BIO_new_file(name, "rb"); 200 if (in == NULL) { 201 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE) 202 CONFerror(CONF_R_NO_SUCH_FILE); 203 else 204 CONFerror(ERR_R_SYS_LIB); 205 return 0; 206 } 207 208 ret = def_load_bio(conf, in, line); 209 BIO_free(in); 210 211 return ret; 212} 213 214static int 215def_load_bio(CONF *conf, BIO *in, long *line) 216{ 217/* The macro BUFSIZE conflicts with a system macro in VxWorks */ 218#define CONFBUFSIZE 512 219 int bufnum = 0, i, ii; 220 BUF_MEM *buff = NULL; 221 char *s, *p, *end; 222 int again; 223 long eline = 0; 224 CONF_VALUE *v = NULL, *tv; 225 CONF_VALUE *sv = NULL; 226 char *section = NULL, *buf; 227 char *start, *psection, *pname; 228 void *h = (void *)(conf->data); 229 230 if ((buff = BUF_MEM_new()) == NULL) { 231 CONFerror(ERR_R_BUF_LIB); 232 goto err; 233 } 234 235 section = strdup("default"); 236 if (section == NULL) { 237 CONFerror(ERR_R_MALLOC_FAILURE); 238 goto err; 239 } 240 241 if (_CONF_new_data(conf) == 0) { 242 CONFerror(ERR_R_MALLOC_FAILURE); 243 goto err; 244 } 245 246 sv = _CONF_new_section(conf, section); 247 if (sv == NULL) { 248 CONFerror(CONF_R_UNABLE_TO_CREATE_NEW_SECTION); 249 goto err; 250 } 251 252 bufnum = 0; 253 again = 0; 254 for (;;) { 255 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) { 256 CONFerror(ERR_R_BUF_LIB); 257 goto err; 258 } 259 p = &(buff->data[bufnum]); 260 *p = '\0'; 261 BIO_gets(in, p, CONFBUFSIZE - 1); 262 p[CONFBUFSIZE - 1] = '\0'; 263 ii = i = strlen(p); 264 if (i == 0 && !again) 265 break; 266 again = 0; 267 while (i > 0) { 268 if ((p[i - 1] != '\r') && (p[i - 1] != '\n')) 269 break; 270 else 271 i--; 272 } 273 /* we removed some trailing stuff so there is a new 274 * line on the end. */ 275 if (ii && i == ii) 276 again = 1; /* long line */ 277 else { 278 p[i] = '\0'; 279 eline++; /* another input line */ 280 } 281 282 /* we now have a line with trailing \r\n removed */ 283 284 /* i is the number of bytes */ 285 bufnum += i; 286 287 v = NULL; 288 /* check for line continuation */ 289 if (bufnum >= 1) { 290 /* If we have bytes and the last char '\\' and 291 * second last char is not '\\' */ 292 p = &(buff->data[bufnum - 1]); 293 if (IS_ESC(conf, p[0]) && 294 ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) { 295 bufnum--; 296 again = 1; 297 } 298 } 299 if (again) 300 continue; 301 bufnum = 0; 302 buf = buff->data; 303 304 clear_comments(conf, buf); 305 s = eat_ws(conf, buf); 306 if (IS_EOF(conf, *s)) 307 continue; /* blank line */ 308 if (*s == '[') { 309 char *ss; 310 311 s++; 312 start = eat_ws(conf, s); 313 ss = start; 314again: 315 end = eat_alpha_numeric(conf, ss); 316 p = eat_ws(conf, end); 317 if (*p != ']') { 318 if (*p != '\0' && ss != p) { 319 ss = p; 320 goto again; 321 } 322 CONFerror(CONF_R_MISSING_CLOSE_SQUARE_BRACKET); 323 goto err; 324 } 325 *end = '\0'; 326 if (!str_copy(conf, NULL, §ion, start)) 327 goto err; 328 if ((sv = _CONF_get_section(conf, section)) == NULL) 329 sv = _CONF_new_section(conf, section); 330 if (sv == NULL) { 331 CONFerror(CONF_R_UNABLE_TO_CREATE_NEW_SECTION); 332 goto err; 333 } 334 continue; 335 } else { 336 pname = s; 337 psection = NULL; 338 end = eat_alpha_numeric(conf, s); 339 if ((end[0] == ':') && (end[1] == ':')) { 340 *end = '\0'; 341 end += 2; 342 psection = pname; 343 pname = end; 344 end = eat_alpha_numeric(conf, end); 345 } 346 p = eat_ws(conf, end); 347 if (*p != '=') { 348 CONFerror(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 CONFerror(ERR_R_MALLOC_FAILURE); 364 goto err; 365 } 366 if (psection == NULL) 367 psection = section; 368 v->name = strdup(pname); 369 v->value = NULL; 370 if (v->name == NULL) { 371 CONFerror(ERR_R_MALLOC_FAILURE); 372 goto err; 373 } 374 if (!str_copy(conf, psection, &(v->value), start)) 375 goto err; 376 377 if (strcmp(psection, section) != 0) { 378 if ((tv = _CONF_get_section(conf, psection)) 379 == NULL) 380 tv = _CONF_new_section(conf, psection); 381 if (tv == NULL) { 382 CONFerror(CONF_R_UNABLE_TO_CREATE_NEW_SECTION); 383 goto err; 384 } 385 } else 386 tv = sv; 387 388 if (_CONF_add_string(conf, tv, v) == 0) { 389 CONFerror(ERR_R_MALLOC_FAILURE); 390 goto err; 391 } 392 v = NULL; 393 } 394 } 395 if (buff != NULL) 396 BUF_MEM_free(buff); 397 free(section); 398 return (1); 399 400err: 401 if (buff != NULL) 402 BUF_MEM_free(buff); 403 free(section); 404 if (line != NULL) 405 *line = eline; 406 ERR_asprintf_error_data("line %ld", eline); 407 if ((h != conf->data) && (conf->data != NULL)) { 408 CONF_free(conf->data); 409 conf->data = NULL; 410 } 411 if (v != NULL) { 412 free(v->name); 413 free(v->value); 414 free(v); 415 } 416 return (0); 417} 418 419static void 420clear_comments(CONF *conf, char *p) 421{ 422 for (;;) { 423 if (IS_FCOMMENT(conf, *p)) { 424 *p = '\0'; 425 return; 426 } 427 if (!IS_WS(conf, *p)) { 428 break; 429 } 430 p++; 431 } 432 433 for (;;) { 434 if (IS_COMMENT(conf, *p)) { 435 *p = '\0'; 436 return; 437 } 438 if (IS_DQUOTE(conf, *p)) { 439 p = scan_dquote(conf, p); 440 continue; 441 } 442 if (IS_QUOTE(conf, *p)) { 443 p = scan_quote(conf, p); 444 continue; 445 } 446 if (IS_ESC(conf, *p)) { 447 p = scan_esc(conf, p); 448 continue; 449 } 450 if (IS_EOF(conf, *p)) 451 return; 452 else 453 p++; 454 } 455} 456 457static int 458str_copy(CONF *conf, char *section, char **pto, char *from) 459{ 460 int q, r,rr = 0, to = 0, len = 0; 461 char *s, *e, *rp, *p, *rrp, *np, *cp, v; 462 size_t newsize; 463 BUF_MEM *buf; 464 465 if ((buf = BUF_MEM_new()) == NULL) 466 return (0); 467 468 len = strlen(from) + 1; 469 if (!BUF_MEM_grow(buf, len)) 470 goto err; 471 472 for (;;) { 473 if (IS_QUOTE(conf, *from)) { 474 q = *from; 475 from++; 476 while (!IS_EOF(conf, *from) && (*from != q)) { 477 if (IS_ESC(conf, *from)) { 478 from++; 479 if (IS_EOF(conf, *from)) 480 break; 481 } 482 buf->data[to++] = *(from++); 483 } 484 if (*from == q) 485 from++; 486 } else if (IS_DQUOTE(conf, *from)) { 487 q = *from; 488 from++; 489 while (!IS_EOF(conf, *from)) { 490 if (*from == q) { 491 if (*(from + 1) == q) { 492 from++; 493 } else { 494 break; 495 } 496 } 497 buf->data[to++] = *(from++); 498 } 499 if (*from == q) 500 from++; 501 } else if (IS_ESC(conf, *from)) { 502 from++; 503 v = *(from++); 504 if (IS_EOF(conf, v)) 505 break; 506 else if (v == 'r') 507 v = '\r'; 508 else if (v == 'n') 509 v = '\n'; 510 else if (v == 'b') 511 v = '\b'; 512 else if (v == 't') 513 v = '\t'; 514 buf->data[to++] = v; 515 } else if (IS_EOF(conf, *from)) 516 break; 517 else if (*from == '$') { 518 /* try to expand it */ 519 rrp = NULL; 520 s = &(from[1]); 521 if (*s == '{') 522 q = '}'; 523 else if (*s == '(') 524 q = ')'; 525 else 526 q = 0; 527 528 if (q) 529 s++; 530 cp = section; 531 e = np = s; 532 while (IS_ALPHA_NUMERIC(conf, *e)) 533 e++; 534 if ((e[0] == ':') && (e[1] == ':')) { 535 cp = np; 536 rrp = e; 537 rr = *e; 538 *rrp = '\0'; 539 e += 2; 540 np = e; 541 while (IS_ALPHA_NUMERIC(conf, *e)) 542 e++; 543 } 544 r = *e; 545 *e = '\0'; 546 rp = e; 547 if (q) { 548 if (r != q) { 549 CONFerror(CONF_R_NO_CLOSE_BRACE); 550 goto err; 551 } 552 e++; 553 } 554 /* So at this point we have 555 * np which is the start of the name string which is 556 * '\0' terminated. 557 * cp which is the start of the section string which is 558 * '\0' terminated. 559 * e is the 'next point after'. 560 * r and rr are the chars replaced by the '\0' 561 * rp and rrp is where 'r' and 'rr' came from. 562 */ 563 p = _CONF_get_string(conf, cp, np); 564 if (rrp != NULL) 565 *rrp = rr; 566 *rp = r; 567 if (p == NULL) { 568 CONFerror(CONF_R_VARIABLE_HAS_NO_VALUE); 569 goto err; 570 } 571 newsize = strlen(p) + buf->length - (e - from); 572 if (newsize > MAX_CONF_VALUE_LENGTH) { 573 CONFerror(CONF_R_VARIABLE_EXPANSION_TOO_LONG); 574 goto err; 575 } 576 if (!BUF_MEM_grow_clean(buf, newsize)) { 577 CONFerror(CONF_R_MODULE_INITIALIZATION_ERROR); 578 goto err; 579 } 580 while (*p) 581 buf->data[to++] = *(p++); 582 583 /* Since we change the pointer 'from', we also have 584 to change the perceived length of the string it 585 points at. /RL */ 586 len -= e - from; 587 from = e; 588 589 /* In case there were no braces or parenthesis around 590 the variable reference, we have to put back the 591 character that was replaced with a '\0'. /RL */ 592 *rp = r; 593 } else 594 buf->data[to++] = *(from++); 595 } 596 buf->data[to]='\0'; 597 free(*pto); 598 *pto = buf->data; 599 free(buf); 600 return (1); 601 602err: 603 if (buf != NULL) 604 BUF_MEM_free(buf); 605 return (0); 606} 607 608static char * 609eat_ws(CONF *conf, char *p) 610{ 611 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p))) 612 p++; 613 return (p); 614} 615 616static char * 617eat_alpha_numeric(CONF *conf, char *p) 618{ 619 for (;;) { 620 if (IS_ESC(conf, *p)) { 621 p = scan_esc(conf, p); 622 continue; 623 } 624 if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p)) 625 return (p); 626 p++; 627 } 628} 629 630static char * 631scan_quote(CONF *conf, char *p) 632{ 633 int q = *p; 634 635 p++; 636 while (!(IS_EOF(conf, *p)) && (*p != q)) { 637 if (IS_ESC(conf, *p)) { 638 p++; 639 if (IS_EOF(conf, *p)) 640 return (p); 641 } 642 p++; 643 } 644 if (*p == q) 645 p++; 646 return (p); 647} 648 649 650static char * 651scan_dquote(CONF *conf, char *p) 652{ 653 int q = *p; 654 655 p++; 656 while (!(IS_EOF(conf, *p))) { 657 if (*p == q) { 658 if (*(p + 1) == q) { 659 p++; 660 } else { 661 break; 662 } 663 } 664 p++; 665 } 666 if (*p == q) 667 p++; 668 return (p); 669} 670 671static void 672dump_value_doall_arg(CONF_VALUE *a, BIO *out) 673{ 674 if (a->name) 675 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value); 676 else 677 BIO_printf(out, "[[%s]]\n", a->section); 678} 679 680static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value, CONF_VALUE, BIO) 681 682static int 683def_dump(const CONF *conf, BIO *out) 684{ 685 lh_CONF_VALUE_doall_arg(conf->data, LHASH_DOALL_ARG_FN(dump_value), 686 BIO, out); 687 return 1; 688} 689 690static int 691def_is_number(const CONF *conf, char c) 692{ 693 return IS_NUMBER(conf, c); 694} 695 696static int 697def_to_int(const CONF *conf, char c) 698{ 699 return c - '0'; 700} 701