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