conf.c revision 1.87
1/* $OpenBSD: conf.c,v 1.87 2006/05/27 17:01:46 hshoexer Exp $ */ 2/* $EOM: conf.c,v 1.48 2000/12/04 02:04:29 angelos Exp $ */ 3 4/* 5 * Copyright (c) 1998, 1999, 2000, 2001 Niklas Hallqvist. All rights reserved. 6 * Copyright (c) 2000, 2001, 2002 H�kan Olsson. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 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 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29/* 30 * This code was written under funding by Ericsson Radio Systems. 31 */ 32 33#include <sys/param.h> 34#include <sys/mman.h> 35#include <sys/queue.h> 36#include <sys/socket.h> 37#include <sys/stat.h> 38#include <netinet/in.h> 39#include <arpa/inet.h> 40#include <ctype.h> 41#include <fcntl.h> 42#include <stdio.h> 43#include <stdlib.h> 44#include <string.h> 45#include <unistd.h> 46#include <errno.h> 47 48#include "app.h" 49#include "conf.h" 50#include "log.h" 51#include "monitor.h" 52#include "util.h" 53 54static char *conf_get_trans_str(int, char *, char *); 55static void conf_load_defaults(int); 56#if 0 57static int conf_find_trans_xf(int, char *); 58#endif 59 60struct conf_trans { 61 TAILQ_ENTRY(conf_trans) link; 62 int trans; 63 enum conf_op { 64 CONF_SET, CONF_REMOVE, CONF_REMOVE_SECTION 65 } op; 66 char *section; 67 char *tag; 68 char *value; 69 int override; 70 int is_default; 71}; 72 73#define CONF_SECT_MAX 256 74 75TAILQ_HEAD(conf_trans_head, conf_trans) conf_trans_queue; 76 77struct conf_binding { 78 LIST_ENTRY(conf_binding) link; 79 char *section; 80 char *tag; 81 char *value; 82 int is_default; 83}; 84 85char *conf_path = CONFIG_FILE; 86LIST_HEAD(conf_bindings, conf_binding) conf_bindings[256]; 87 88static char *conf_addr; 89static __inline__ u_int8_t 90conf_hash(char *s) 91{ 92 u_int8_t hash = 0; 93 94 while (*s) { 95 hash = ((hash << 1) | (hash >> 7)) ^ tolower(*s); 96 s++; 97 } 98 return hash; 99} 100 101/* 102 * Insert a tag-value combination from LINE (the equal sign is at POS) 103 */ 104static int 105conf_remove_now(char *section, char *tag) 106{ 107 struct conf_binding *cb, *next; 108 109 for (cb = LIST_FIRST(&conf_bindings[conf_hash(section)]); cb; 110 cb = next) { 111 next = LIST_NEXT(cb, link); 112 if (strcasecmp(cb->section, section) == 0 && 113 strcasecmp(cb->tag, tag) == 0) { 114 LIST_REMOVE(cb, link); 115 LOG_DBG((LOG_MISC, 95, "[%s]:%s->%s removed", section, 116 tag, cb->value)); 117 free(cb->section); 118 free(cb->tag); 119 free(cb->value); 120 free(cb); 121 return 0; 122 } 123 } 124 return 1; 125} 126 127static int 128conf_remove_section_now(char *section) 129{ 130 struct conf_binding *cb, *next; 131 int unseen = 1; 132 133 for (cb = LIST_FIRST(&conf_bindings[conf_hash(section)]); cb; 134 cb = next) { 135 next = LIST_NEXT(cb, link); 136 if (strcasecmp(cb->section, section) == 0) { 137 unseen = 0; 138 LIST_REMOVE(cb, link); 139 LOG_DBG((LOG_MISC, 95, "[%s]:%s->%s removed", section, 140 cb->tag, cb->value)); 141 free(cb->section); 142 free(cb->tag); 143 free(cb->value); 144 free(cb); 145 } 146 } 147 return unseen; 148} 149 150/* 151 * Insert a tag-value combination from LINE (the equal sign is at POS) 152 * into SECTION of our configuration database. 153 */ 154static int 155conf_set_now(char *section, char *tag, char *value, int override, 156 int is_default) 157{ 158 struct conf_binding *node = 0; 159 160 if (override) 161 conf_remove_now(section, tag); 162 else if (conf_get_str(section, tag)) { 163 if (!is_default) 164 log_print("conf_set_now: duplicate tag [%s]:%s, " 165 "ignoring...\n", section, tag); 166 return 1; 167 } 168 node = calloc(1, sizeof *node); 169 if (!node) { 170 log_error("conf_set_now: calloc (1, %lu) failed", 171 (unsigned long)sizeof *node); 172 return 1; 173 } 174 node->section = node->tag = node->value = NULL; 175 if ((node->section = strdup(section)) == NULL) 176 goto fail; 177 if ((node->tag = strdup(tag)) == NULL) 178 goto fail; 179 if ((node->value = strdup(value)) == NULL) 180 goto fail; 181 node->is_default = is_default; 182 183 LIST_INSERT_HEAD(&conf_bindings[conf_hash(section)], node, link); 184 LOG_DBG((LOG_MISC, 95, "conf_set_now: [%s]:%s->%s", node->section, 185 node->tag, node->value)); 186 return 0; 187fail: 188 if (node->value) { 189 free(node->value); 190 node->value = NULL; 191 } 192 if (node->tag) { 193 free(node->tag); 194 node->tag = NULL; 195 } 196 if (node->section) { 197 free(node->section); 198 node->section = NULL; 199 } 200 return 1; 201} 202 203/* 204 * Parse the line LINE of SZ bytes. Skip Comments, recognize section 205 * headers and feed tag-value pairs into our configuration database. 206 */ 207static void 208conf_parse_line(int trans, char *line, int ln, size_t sz) 209{ 210 char *val; 211 size_t i; 212 int j; 213 static char *section = 0; 214 215 /* Lines starting with '#' or ';' are comments. */ 216 if (*line == '#' || *line == ';') 217 return; 218 219 /* '[section]' parsing... */ 220 if (*line == '[') { 221 for (i = 1; i < sz; i++) 222 if (line[i] == ']') 223 break; 224 if (section) 225 free(section); 226 if (i == sz) { 227 log_print("conf_parse_line: %d:" 228 "unmatched ']', ignoring until next section", ln); 229 section = 0; 230 return; 231 } 232 section = malloc(i); 233 if (!section) { 234 log_print("conf_parse_line: %d: malloc (%lu) failed", 235 ln, (unsigned long)i); 236 return; 237 } 238 strlcpy(section, line + 1, i); 239 return; 240 } 241 /* Deal with assignments. */ 242 for (i = 0; i < sz; i++) 243 if (line[i] == '=') { 244 /* If no section, we are ignoring the lines. */ 245 if (!section) { 246 log_print("conf_parse_line: %d: ignoring line " 247 "due to no section", ln); 248 return; 249 } 250 line[strcspn(line, " \t=")] = '\0'; 251 val = line + i + 1 + strspn(line + i + 1, " \t"); 252 /* Skip trailing whitespace, if any */ 253 for (j = sz - (val - line) - 1; j > 0 && 254 isspace(val[j]); j--) 255 val[j] = '\0'; 256 /* XXX Perhaps should we not ignore errors? */ 257 conf_set(trans, section, line, val, 0, 0); 258 return; 259 } 260 /* Other non-empty lines are weird. */ 261 i = strspn(line, " \t"); 262 if (line[i]) 263 log_print("conf_parse_line: %d: syntax error", ln); 264} 265 266/* Parse the mapped configuration file. */ 267static void 268conf_parse(int trans, char *buf, size_t sz) 269{ 270 char *cp = buf; 271 char *bufend = buf + sz; 272 char *line; 273 int ln = 1; 274 275 line = cp; 276 while (cp < bufend) { 277 if (*cp == '\n') { 278 /* Check for escaped newlines. */ 279 if (cp > buf && *(cp - 1) == '\\') 280 *(cp - 1) = *cp = ' '; 281 else { 282 *cp = '\0'; 283 conf_parse_line(trans, line, ln, cp - line); 284 line = cp + 1; 285 } 286 ln++; 287 } 288 cp++; 289 } 290 if (cp != line) 291 log_print("conf_parse: last line unterminated, ignored."); 292} 293 294/* 295 * Auto-generate default configuration values for the transforms and 296 * suites the user wants. 297 * 298 * Resulting section names can be: 299 * For main mode: 300 * {DES,BLF,3DES,CAST,AES}-{MD5,SHA}[-GRP{1,2,5,14,15}][-{DSS,RSA_SIG}] 301 * For quick mode: 302 * QM-{proto}[-TRP]-{cipher}[-{hash}][-PFS[-{group}]]-SUITE 303 * where 304 * {proto} = ESP, AH 305 * {cipher} = DES, 3DES, CAST, BLF, AES 306 * {hash} = MD5, SHA, RIPEMD, SHA2-{-256,384,512} 307 * {group} = GRP1, GRP2, GRP5, GRP14, GRP15 308 * 309 * DH group defaults to MODP_1024. 310 * 311 * XXX We may want to support USE_TRIPLEDES, etc... 312 * XXX No EC2N DH support here yet. 313 */ 314 315/* Find the value for a section+tag in the transaction list. */ 316static char * 317conf_get_trans_str(int trans, char *section, char *tag) 318{ 319 struct conf_trans *node, *nf = 0; 320 321 for (node = TAILQ_FIRST(&conf_trans_queue); node; 322 node = TAILQ_NEXT(node, link)) 323 if (node->trans == trans && strcasecmp(section, node->section) 324 == 0 && strcasecmp(tag, node->tag) == 0) { 325 if (!nf) 326 nf = node; 327 else if (node->override) 328 nf = node; 329 } 330 return nf ? nf->value : 0; 331} 332 333#if 0 334/* XXX Currently unused. */ 335static int 336conf_find_trans_xf(int phase, char *xf) 337{ 338 struct conf_trans *node; 339 char *p; 340 341 /* Find the relevant transforms and suites, if any. */ 342 for (node = TAILQ_FIRST(&conf_trans_queue); node; 343 node = TAILQ_NEXT(node, link)) 344 if ((phase == 1 && strcmp("Transforms", node->tag) == 0) || 345 (phase == 2 && strcmp("Suites", node->tag) == 0)) { 346 p = node->value; 347 while ((p = strstr(p, xf)) != NULL) 348 if (*(p + strlen(p)) && 349 *(p + strlen(p)) != ',') 350 p += strlen(p); 351 else 352 return 1; 353 } 354 return 0; 355} 356#endif 357 358static void 359conf_load_defaults_mm(int tr, char *mme, char *mmh, char *mma, char *dhg, 360 char *mme_p, char *mma_p, char *dhg_p) 361{ 362 char sect[CONF_SECT_MAX]; 363 364 snprintf(sect, sizeof sect, "%s-%s%s%s", mme_p, mmh, dhg_p, mma_p); 365 366 LOG_DBG((LOG_MISC, 95, "conf_load_defaults_mm: main mode %s", sect)); 367 368 conf_set(tr, sect, "ENCRYPTION_ALGORITHM", mme, 0, 1); 369 if (strcmp(mme, "BLOWFISH_CBC") == 0) 370 conf_set(tr, sect, "KEY_LENGTH", CONF_DFLT_VAL_BLF_KEYLEN, 0, 371 1); 372 else if (strcmp(mme, "AES_CBC") == 0) 373 conf_set(tr, sect, "KEY_LENGTH", CONF_DFLT_VAL_AES_KEYLEN, 0, 374 1); 375 376 conf_set(tr, sect, "HASH_ALGORITHM", mmh, 0, 1); 377 conf_set(tr, sect, "AUTHENTICATION_METHOD", mma, 0, 1); 378 conf_set(tr, sect, "GROUP_DESCRIPTION", dhg, 0, 1); 379 conf_set(tr, sect, "Life", CONF_DFLT_TAG_LIFE_MAIN_MODE, 0, 1); 380} 381 382static void 383conf_load_defaults_qm(int tr, char *qme, char *qmh, char *dhg, char *qme_p, 384 char *qmh_p, char *dhg_p, int proto, int mode, int pfs) 385{ 386 char sect[CONF_SECT_MAX], tmp[CONF_SECT_MAX]; 387 388 /* Helper #defines, incl abbreviations. */ 389#define PROTO(x) ((x) ? "AH" : "ESP") 390#define PFS(x) ((x) ? "-PFS" : "") 391#define MODE(x) ((x) ? "TRANSPORT" : "TUNNEL") 392#define MODE_p(x) ((x) ? "-TRP" : "") 393 394 if (proto == 1 && strcmp(qmh, "NONE") == 0) /* AH */ 395 return; 396 397 snprintf(tmp, sizeof tmp, "QM-%s%s%s%s%s%s", PROTO(proto), 398 MODE_p(mode), qme_p, qmh_p, PFS(pfs), dhg_p); 399 400 strlcpy(sect, tmp, CONF_SECT_MAX); 401 strlcat(sect, "-SUITE", CONF_SECT_MAX); 402 403 LOG_DBG((LOG_MISC, 95, "conf_load_defaults_qm: quick mode %s", sect)); 404 405 conf_set(tr, sect, "Protocols", tmp, 0, 1); 406 snprintf(sect, sizeof sect, "IPSEC_%s", PROTO(proto)); 407 conf_set(tr, tmp, "PROTOCOL_ID", sect, 0, 1); 408 strlcpy(sect, tmp, CONF_SECT_MAX); 409 strlcat(sect, "-XF", CONF_SECT_MAX); 410 conf_set(tr, tmp, "Transforms", sect, 0, 1); 411 412 /* 413 * XXX For now, defaults 414 * contain one xf per protocol. 415 */ 416 conf_set(tr, sect, "TRANSFORM_ID", qme, 0, 1); 417 if (strcmp(qme ,"BLOWFISH") == 0) 418 conf_set(tr, sect, "KEY_LENGTH", CONF_DFLT_VAL_BLF_KEYLEN, 0, 419 1); 420 else if (strcmp(qme ,"AES") == 0) 421 conf_set(tr, sect, "KEY_LENGTH", CONF_DFLT_VAL_AES_KEYLEN, 0, 422 1); 423 conf_set(tr, sect, "ENCAPSULATION_MODE", MODE(mode), 0, 1); 424 if (strcmp(qmh, "NONE")) { 425 conf_set(tr, sect, "AUTHENTICATION_ALGORITHM", qmh, 0, 1); 426 427 /* XXX Another shortcut to keep length down */ 428 if (pfs) 429 conf_set(tr, sect, "GROUP_DESCRIPTION", dhg, 0, 1); 430 } 431 432 /* XXX Lifetimes depending on enc/auth strength? */ 433 conf_set(tr, sect, "Life", CONF_DFLT_TAG_LIFE_QUICK_MODE, 0, 1); 434} 435 436static void 437conf_load_defaults(int tr) 438{ 439 int enc, auth, hash, group, proto, mode, pfs; 440 char *dflt; 441 442 char *mm_auth[] = {"PRE_SHARED", "DSS", "RSA_SIG", 0}; 443 char *mm_auth_p[] = {"", "-DSS", "-RSA_SIG", 0}; 444 char *mm_hash[] = {"MD5", "SHA", 0}; 445 char *mm_enc[] = {"DES_CBC", "BLOWFISH_CBC", "3DES_CBC", "CAST_CBC", 446 "AES_CBC", 0}; 447 char *mm_enc_p[] = {"DES", "BLF", "3DES", "CAST", "AES", 0}; 448 char *dhgroup[] = {"MODP_1024", "MODP_768", "MODP_1024", 449 "MODP_1536", "MODP_2048", "MODP_3072", 0}; 450 char *dhgroup_p[] = {"", "-GRP1", "-GRP2", "-GRP5", "-GRP14", 451 "-GRP15", 0}; 452 char *qm_enc[] = {"DES", "3DES", "CAST", "BLOWFISH", "AES", 0}; 453 char *qm_enc_p[] = {"-DES", "-3DES", "-CAST", "-BLF", "-AES", 0}; 454 char *qm_hash[] = {"HMAC_MD5", "HMAC_SHA", "HMAC_RIPEMD", 455 "HMAC_SHA2_256", "HMAC_SHA2_384", "HMAC_SHA2_512", "NONE", 456 0}; 457 char *qm_hash_p[] = {"-MD5", "-SHA", "-RIPEMD", "-SHA2-256", 458 "-SHA2-384", "-SHA2-512", "", 0}; 459 460 /* General and X509 defaults */ 461 conf_set(tr, "General", "Retransmits", CONF_DFLT_RETRANSMITS, 0, 1); 462 conf_set(tr, "General", "Exchange-max-time", CONF_DFLT_EXCH_MAX_TIME, 463 0, 1); 464 conf_set(tr, "General", "Use-Keynote", CONF_DFLT_USE_KEYNOTE, 0, 1); 465 conf_set(tr, "General", "Policy-file", CONF_DFLT_POLICY_FILE, 0, 1); 466 conf_set(tr, "General", "Pubkey-directory", CONF_DFLT_PUBKEY_DIR, 0, 467 1); 468 469 conf_set(tr, "X509-certificates", "CA-directory", 470 CONF_DFLT_X509_CA_DIR, 0, 1); 471 conf_set(tr, "X509-certificates", "Cert-directory", 472 CONF_DFLT_X509_CERT_DIR, 0, 1); 473 conf_set(tr, "X509-certificates", "Private-key", 474 CONF_DFLT_X509_PRIVATE_KEY, 0, 1); 475 conf_set(tr, "X509-certificates", "CRL-directory", 476 CONF_DFLT_X509_CRL_DIR, 0, 1); 477 478 conf_set(tr, "KeyNote", "Credential-directory", 479 CONF_DFLT_KEYNOTE_CRED_DIR, 0, 1); 480 481 /* Lifetimes. XXX p1/p2 vs main/quick mode may be unclear. */ 482 dflt = conf_get_trans_str(tr, "General", "Default-phase-1-lifetime"); 483 conf_set(tr, CONF_DFLT_TAG_LIFE_MAIN_MODE, "LIFE_TYPE", 484 CONF_DFLT_TYPE_LIFE_MAIN_MODE, 0, 1); 485 conf_set(tr, CONF_DFLT_TAG_LIFE_MAIN_MODE, "LIFE_DURATION", 486 (dflt ? dflt : CONF_DFLT_VAL_LIFE_MAIN_MODE), 0, 1); 487 488 dflt = conf_get_trans_str(tr, "General", "Default-phase-2-lifetime"); 489 conf_set(tr, CONF_DFLT_TAG_LIFE_QUICK_MODE, "LIFE_TYPE", 490 CONF_DFLT_TYPE_LIFE_QUICK_MODE, 0, 1); 491 conf_set(tr, CONF_DFLT_TAG_LIFE_QUICK_MODE, "LIFE_DURATION", 492 (dflt ? dflt : CONF_DFLT_VAL_LIFE_QUICK_MODE), 0, 1); 493 494 /* Default Phase-1 Configuration section */ 495 conf_set(tr, CONF_DFLT_TAG_PHASE1_CONFIG, "EXCHANGE_TYPE", 496 CONF_DFLT_PHASE1_EXCH_TYPE, 0, 1); 497 conf_set(tr, CONF_DFLT_TAG_PHASE1_CONFIG, "Transforms", 498 CONF_DFLT_PHASE1_TRANSFORMS, 0, 1); 499 500 /* Main modes */ 501 for (enc = 0; mm_enc[enc]; enc++) 502 for (hash = 0; mm_hash[hash]; hash++) 503 for (auth = 0; mm_auth[auth]; auth++) 504 for (group = 0; dhgroup_p[group]; group++) 505 conf_load_defaults_mm (tr, mm_enc[enc], 506 mm_hash[hash], mm_auth[auth], 507 dhgroup[group], mm_enc_p[enc], 508 mm_auth_p[auth], dhgroup_p[group]); 509 510 /* Setup a default Phase 1 entry */ 511 conf_set(tr, "Phase 1", "Default", "Default-phase-1", 0, 1); 512 conf_set(tr, "Default-phase-1", "Phase", "1", 0, 1); 513 conf_set(tr, "Default-phase-1", "Configuration", 514 "Default-phase-1-configuration", 0, 1); 515 dflt = conf_get_trans_str(tr, "General", "Default-phase-1-ID"); 516 if (dflt) 517 conf_set(tr, "Default-phase-1", "ID", dflt, 0, 1); 518 519 /* Quick modes */ 520 for (enc = 0; qm_enc[enc]; enc++) 521 for (proto = 0; proto < 2; proto++) 522 for (mode = 0; mode < 2; mode++) 523 for (pfs = 0; pfs < 2; pfs++) 524 for (hash = 0; qm_hash[hash]; hash++) 525 for (group = 0; 526 dhgroup_p[group]; group++) 527 conf_load_defaults_qm( 528 tr, qm_enc[enc], 529 qm_hash[hash], 530 dhgroup[group], 531 qm_enc_p[enc], 532 qm_hash_p[hash], 533 dhgroup_p[group], 534 proto, mode, pfs); 535} 536 537void 538conf_init(void) 539{ 540 unsigned int i; 541 542 for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++) 543 LIST_INIT(&conf_bindings[i]); 544 TAILQ_INIT(&conf_trans_queue); 545 conf_reinit(); 546} 547 548/* Open the config file and map it into our address space, then parse it. */ 549void 550conf_reinit(void) 551{ 552 struct conf_binding *cb = 0; 553 int fd, trans; 554 unsigned int i; 555 size_t sz; 556 char *new_conf_addr = 0; 557 558 fd = monitor_open(conf_path, O_RDONLY, 0); 559 if (fd == -1 || check_file_secrecy_fd(fd, conf_path, &sz) == -1) { 560 if (fd == -1 && errno != ENOENT) 561 log_error("conf_reinit: open(\"%s\", O_RDONLY, 0) " 562 "failed", conf_path); 563 if (fd != -1) 564 close(fd); 565 566 trans = conf_begin(); 567 } else { 568 new_conf_addr = malloc(sz); 569 if (!new_conf_addr) { 570 log_error("conf_reinit: malloc (%lu) failed", 571 (unsigned long)sz); 572 goto fail; 573 } 574 /* XXX I assume short reads won't happen here. */ 575 if (read(fd, new_conf_addr, sz) != (int)sz) { 576 log_error("conf_reinit: read (%d, %p, %lu) failed", 577 fd, new_conf_addr, (unsigned long)sz); 578 goto fail; 579 } 580 close(fd); 581 582 trans = conf_begin(); 583 584 /* XXX Should we not care about errors and rollback? */ 585 conf_parse(trans, new_conf_addr, sz); 586 } 587 588 /* Load default configuration values. */ 589 conf_load_defaults(trans); 590 591 /* Free potential existing configuration. */ 592 if (conf_addr) { 593 for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; 594 i++) 595 for (cb = LIST_FIRST(&conf_bindings[i]); cb; 596 cb = LIST_FIRST(&conf_bindings[i])) 597 conf_remove_now(cb->section, cb->tag); 598 free(conf_addr); 599 } 600 conf_end(trans, 1); 601 conf_addr = new_conf_addr; 602 return; 603 604fail: 605 if (new_conf_addr) 606 free(new_conf_addr); 607 close(fd); 608} 609 610/* 611 * Return the numeric value denoted by TAG in section SECTION or DEF 612 * if that tag does not exist. 613 */ 614int 615conf_get_num(char *section, char *tag, int def) 616{ 617 char *value = conf_get_str(section, tag); 618 619 if (value) 620 return atoi(value); 621 return def; 622} 623 624/* 625 * Return the socket endpoint address denoted by TAG in SECTION as a 626 * struct sockaddr. It is the callers responsibility to deallocate 627 * this structure when it is finished with it. 628 */ 629struct sockaddr * 630conf_get_address(char *section, char *tag) 631{ 632 char *value = conf_get_str(section, tag); 633 struct sockaddr *sa; 634 635 if (!value) 636 return 0; 637 if (text2sockaddr(value, 0, &sa, 0, 0) == -1) 638 return 0; 639 return sa; 640} 641 642/* Validate X according to the range denoted by TAG in section SECTION. */ 643int 644conf_match_num(char *section, char *tag, int x) 645{ 646 char *value = conf_get_str(section, tag); 647 int val, min, max, n; 648 649 if (!value) 650 return 0; 651 n = sscanf(value, "%d,%d:%d", &val, &min, &max); 652 switch (n) { 653 case 1: 654 LOG_DBG((LOG_MISC, 95, "conf_match_num: %s:%s %d==%d?", 655 section, tag, val, x)); 656 return x == val; 657 case 3: 658 LOG_DBG((LOG_MISC, 95, "conf_match_num: %s:%s %d<=%d<=%d?", 659 section, tag, min, x, max)); 660 return min <= x && max >= x; 661 default: 662 log_error("conf_match_num: section %s tag %s: invalid number " 663 "spec %s", section, tag, value); 664 } 665 return 0; 666} 667 668/* Return the string value denoted by TAG in section SECTION. */ 669char * 670conf_get_str(char *section, char *tag) 671{ 672 struct conf_binding *cb; 673 674 for (cb = LIST_FIRST(&conf_bindings[conf_hash(section)]); cb; 675 cb = LIST_NEXT(cb, link)) 676 if (strcasecmp(section, cb->section) == 0 && 677 strcasecmp(tag, cb->tag) == 0) { 678 LOG_DBG((LOG_MISC, 95, "conf_get_str: [%s]:%s->%s", 679 section, tag, cb->value)); 680 return cb->value; 681 } 682 LOG_DBG((LOG_MISC, 95, 683 "conf_get_str: configuration value not found [%s]:%s", section, 684 tag)); 685 return 0; 686} 687 688/* 689 * Build a list of string values out of the comma separated value denoted by 690 * TAG in SECTION. 691 */ 692struct conf_list * 693conf_get_list(char *section, char *tag) 694{ 695 char *liststr = 0, *p, *field, *t; 696 struct conf_list *list = 0; 697 struct conf_list_node *node = 0; 698 699 list = malloc(sizeof *list); 700 if (!list) 701 goto cleanup; 702 TAILQ_INIT(&list->fields); 703 list->cnt = 0; 704 liststr = conf_get_str(section, tag); 705 if (!liststr) 706 goto cleanup; 707 liststr = strdup(liststr); 708 if (!liststr) 709 goto cleanup; 710 p = liststr; 711 while ((field = strsep(&p, ",")) != NULL) { 712 /* Skip leading whitespace */ 713 while (isspace(*field)) 714 field++; 715 /* Skip trailing whitespace */ 716 if (p) 717 for (t = p - 1; t > field && isspace(*t); t--) 718 *t = '\0'; 719 if (*field == '\0') { 720 log_print("conf_get_list: empty field, ignoring..."); 721 continue; 722 } 723 list->cnt++; 724 node = calloc(1, sizeof *node); 725 if (!node) 726 goto cleanup; 727 node->field = strdup(field); 728 if (!node->field) 729 goto cleanup; 730 TAILQ_INSERT_TAIL(&list->fields, node, link); 731 } 732 free(liststr); 733 return list; 734 735cleanup: 736 if (node) 737 free(node); 738 if (list) 739 conf_free_list(list); 740 if (liststr) 741 free(liststr); 742 return 0; 743} 744 745struct conf_list * 746conf_get_tag_list(char *section) 747{ 748 struct conf_list *list = 0; 749 struct conf_list_node *node = 0; 750 struct conf_binding *cb; 751 752 list = malloc(sizeof *list); 753 if (!list) 754 goto cleanup; 755 TAILQ_INIT(&list->fields); 756 list->cnt = 0; 757 for (cb = LIST_FIRST(&conf_bindings[conf_hash(section)]); cb; 758 cb = LIST_NEXT(cb, link)) 759 if (strcasecmp(section, cb->section) == 0) { 760 list->cnt++; 761 node = calloc(1, sizeof *node); 762 if (!node) 763 goto cleanup; 764 node->field = strdup(cb->tag); 765 if (!node->field) 766 goto cleanup; 767 TAILQ_INSERT_TAIL(&list->fields, node, link); 768 } 769 return list; 770 771cleanup: 772 if (node) 773 free(node); 774 if (list) 775 conf_free_list(list); 776 return 0; 777} 778 779void 780conf_free_list(struct conf_list *list) 781{ 782 struct conf_list_node *node = TAILQ_FIRST(&list->fields); 783 784 while (node) { 785 TAILQ_REMOVE(&list->fields, node, link); 786 if (node->field) 787 free(node->field); 788 free(node); 789 node = TAILQ_FIRST(&list->fields); 790 } 791 free(list); 792} 793 794int 795conf_begin(void) 796{ 797 static int seq = 0; 798 799 return ++seq; 800} 801 802static struct conf_trans * 803conf_trans_node(int transaction, enum conf_op op) 804{ 805 struct conf_trans *node; 806 807 node = calloc(1, sizeof *node); 808 if (!node) { 809 log_error("conf_trans_node: calloc (1, %lu) failed", 810 (unsigned long)sizeof *node); 811 return 0; 812 } 813 node->trans = transaction; 814 node->op = op; 815 TAILQ_INSERT_TAIL(&conf_trans_queue, node, link); 816 return node; 817} 818 819/* Queue a set operation. */ 820int 821conf_set(int transaction, char *section, char *tag, char *value, int override, 822 int is_default) 823{ 824 struct conf_trans *node; 825 826 node = conf_trans_node(transaction, CONF_SET); 827 if (!node) 828 return 1; 829 node->section = strdup(section); 830 if (!node->section) { 831 log_error("conf_set: strdup (\"%s\") failed", section); 832 goto fail; 833 } 834 node->tag = strdup(tag); 835 if (!node->tag) { 836 log_error("conf_set: strdup (\"%s\") failed", tag); 837 goto fail; 838 } 839 node->value = strdup(value); 840 if (!node->value) { 841 log_error("conf_set: strdup (\"%s\") failed", value); 842 goto fail; 843 } 844 node->override = override; 845 node->is_default = is_default; 846 return 0; 847 848fail: 849 if (node->tag) 850 free(node->tag); 851 if (node->section) 852 free(node->section); 853 if (node) 854 free(node); 855 return 1; 856} 857 858/* Queue a remove operation. */ 859int 860conf_remove(int transaction, char *section, char *tag) 861{ 862 struct conf_trans *node; 863 864 node = conf_trans_node(transaction, CONF_REMOVE); 865 if (!node) 866 goto fail; 867 node->section = strdup(section); 868 if (!node->section) { 869 log_error("conf_remove: strdup (\"%s\") failed", section); 870 goto fail; 871 } 872 node->tag = strdup(tag); 873 if (!node->tag) { 874 log_error("conf_remove: strdup (\"%s\") failed", tag); 875 goto fail; 876 } 877 return 0; 878 879fail: 880 if (node->section) 881 free(node->section); 882 if (node) 883 free(node); 884 return 1; 885} 886 887/* Queue a remove section operation. */ 888int 889conf_remove_section(int transaction, char *section) 890{ 891 struct conf_trans *node; 892 893 node = conf_trans_node(transaction, CONF_REMOVE_SECTION); 894 if (!node) 895 goto fail; 896 node->section = strdup(section); 897 if (!node->section) { 898 log_error("conf_remove_section: strdup (\"%s\") failed", 899 section); 900 goto fail; 901 } 902 return 0; 903 904fail: 905 if (node) 906 free(node); 907 return 1; 908} 909 910/* Execute all queued operations for this transaction. Cleanup. */ 911int 912conf_end(int transaction, int commit) 913{ 914 struct conf_trans *node, *next; 915 916 for (node = TAILQ_FIRST(&conf_trans_queue); node; node = next) { 917 next = TAILQ_NEXT(node, link); 918 if (node->trans == transaction) { 919 if (commit) 920 switch (node->op) { 921 case CONF_SET: 922 conf_set_now(node->section, node->tag, 923 node->value, node->override, 924 node->is_default); 925 break; 926 case CONF_REMOVE: 927 conf_remove_now(node->section, 928 node->tag); 929 break; 930 case CONF_REMOVE_SECTION: 931 conf_remove_section_now(node->section); 932 break; 933 default: 934 log_print("conf_end: unknown " 935 "operation: %d", node->op); 936 } 937 TAILQ_REMOVE(&conf_trans_queue, node, link); 938 if (node->section) 939 free(node->section); 940 if (node->tag) 941 free(node->tag); 942 if (node->value) 943 free(node->value); 944 free(node); 945 } 946 } 947 return 0; 948} 949 950/* 951 * Dump running configuration upon SIGUSR1. 952 * Configuration is "stored in reverse order", so reverse it again. 953 */ 954struct dumper { 955 char *s, *v; 956 struct dumper *next; 957}; 958 959static void 960conf_report_dump(struct dumper *node) 961{ 962 /* Recursive, cleanup when we're done. */ 963 964 if (node->next) 965 conf_report_dump(node->next); 966 967 if (node->v) 968 LOG_DBG((LOG_REPORT, 0, "%s=\t%s", node->s, node->v)); 969 else if (node->s) { 970 LOG_DBG((LOG_REPORT, 0, "%s", node->s)); 971 if (strlen(node->s) > 0) 972 free(node->s); 973 } 974 free(node); 975} 976 977void 978conf_report(void) 979{ 980 struct conf_binding *cb, *last = 0; 981 unsigned int i; 982 char *current_section = (char *)0; 983 struct dumper *dumper, *dnode; 984 985 dumper = dnode = (struct dumper *)calloc(1, sizeof *dumper); 986 if (!dumper) 987 goto mem_fail; 988 989 LOG_DBG((LOG_REPORT, 0, "conf_report: dumping running configuration")); 990 991 for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++) 992 for (cb = LIST_FIRST(&conf_bindings[i]); cb; 993 cb = LIST_NEXT(cb, link)) { 994 if (!cb->is_default) { 995 /* Dump this entry. */ 996 if (!current_section || strcmp(cb->section, 997 current_section)) { 998 if (current_section) { 999 if (asprintf(&dnode->s, "[%s]", 1000 current_section) == -1) 1001 goto mem_fail; 1002 dnode->next = (struct dumper *) 1003 calloc(1, 1004 sizeof(struct dumper)); 1005 dnode = dnode->next; 1006 if (!dnode) 1007 goto mem_fail; 1008 1009 dnode->s = ""; 1010 dnode->next = (struct dumper *) 1011 calloc(1, 1012 sizeof(struct dumper)); 1013 dnode = dnode->next; 1014 if (!dnode) 1015 goto mem_fail; 1016 } 1017 current_section = cb->section; 1018 } 1019 dnode->s = cb->tag; 1020 dnode->v = cb->value; 1021 dnode->next = (struct dumper *) 1022 calloc(1, sizeof(struct dumper)); 1023 dnode = dnode->next; 1024 if (!dnode) 1025 goto mem_fail; 1026 last = cb; 1027 } 1028 } 1029 1030 if (last) 1031 if (asprintf(&dnode->s, "[%s]", last->section) == -1) 1032 goto mem_fail; 1033 conf_report_dump(dumper); 1034 1035 return; 1036 1037mem_fail: 1038 log_error("conf_report: malloc/calloc failed"); 1039 while ((dnode = dumper) != 0) { 1040 dumper = dumper->next; 1041 if (dnode->s) 1042 free(dnode->s); 1043 free(dnode); 1044 } 1045} 1046