1 /* $OpenBSD: conf.c,v 1.99 2010/09/22 13:45:15 mikeb 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 /* For GCM no additional authentication must be specified */ 406 if (proto == 0 && strcmp(qmh, "NONE") != 0 && 407 (strcmp(qme, "AES_GCM_16") == 0 || strcmp(qme, "AES_GMAC") == 0)) 408 return; 409 410 snprintf(tmp, sizeof tmp, "QM-%s%s%s%s%s%s", PROTO(proto), 411 MODE_p(mode), qme_p, qmh_p, PFS(pfs), dhg_p); 412 413 strlcpy(sect, tmp, CONF_SECT_MAX); 414 strlcat(sect, "-SUITE", CONF_SECT_MAX); 415 416 LOG_DBG((LOG_MISC, 95, "conf_load_defaults_qm: quick mode %s", sect)); 417 418 conf_set(tr, sect, "Protocols", tmp, 0, 1); 419 snprintf(sect, sizeof sect, "IPSEC_%s", PROTO(proto)); 420 conf_set(tr, tmp, "PROTOCOL_ID", sect, 0, 1); 421 strlcpy(sect, tmp, CONF_SECT_MAX); 422 strlcat(sect, "-XF", CONF_SECT_MAX); 423 conf_set(tr, tmp, "Transforms", sect, 0, 1); 424 425 /* 426 * XXX For now, defaults 427 * contain one xf per protocol. 428 */ 429 if (proto == 0) 430 conf_set(tr, sect, "TRANSFORM_ID", qme, 0, 1); 431 else 432 conf_set(tr, sect, "TRANSFORM_ID", qm_ah_id, 0, 1); 433 if (strcmp(qme ,"BLOWFISH") == 0) 434 conf_set(tr, sect, "KEY_LENGTH", CONF_DFLT_VAL_BLF_KEYLEN, 0, 435 1); 436 else if (strcmp(qme_p, "-AES-128") == 0 || 437 strcmp(qme_p, "-AESGCM-128") == 0 || 438 strcmp(qme_p, "-AESGMAC-128") == 0) 439 conf_set(tr, sect, "KEY_LENGTH", "128,128:128", 0, 1); 440 else if (strcmp(qme_p, "-AES-192") == 0 || 441 strcmp(qme_p, "-AESGCM-192") == 0 || 442 strcmp(qme_p, "-AESGMAC-192") == 0) 443 conf_set(tr, sect, "KEY_LENGTH", "192,192:192", 0, 1); 444 else if (strcmp(qme_p, "-AES-256") == 0 || 445 strcmp(qme_p, "-AESGCM-256") == 0 || 446 strcmp(qme_p, "-AESGMAC-256") == 0) 447 conf_set(tr, sect, "KEY_LENGTH", "256,256:256", 0, 1); 448 else if (strcmp(qme, "AES") == 0) 449 conf_set(tr, sect, "KEY_LENGTH", CONF_DFLT_VAL_AES_KEYLEN, 0, 450 1); 451 452 conf_set(tr, sect, "ENCAPSULATION_MODE", MODE(mode), 0, 1); 453 if (strcmp(qmh, "NONE")) { 454 conf_set(tr, sect, "AUTHENTICATION_ALGORITHM", qmh, 0, 1); 455 456 /* XXX Another shortcut to keep length down */ 457 if (pfs) 458 conf_set(tr, sect, "GROUP_DESCRIPTION", dhg, 0, 1); 459 } 460 461 /* XXX Lifetimes depending on enc/auth strength? */ 462 conf_set(tr, sect, "Life", CONF_DFLT_TAG_LIFE_QUICK_MODE, 0, 1); 463 } 464 465 static void 466 conf_load_defaults(int tr) 467 { 468 int enc, auth, hash, group, proto, mode, pfs; 469 char *dflt; 470 471 char *mm_auth[] = {"PRE_SHARED", "DSS", "RSA_SIG", 0}; 472 char *mm_auth_p[] = {"", "-DSS", "-RSA_SIG", 0}; 473 char *mm_hash[] = {"MD5", "SHA", "SHA2_256", "SHA2_384", "SHA2_512", 474 0}; 475 char *mm_hash_p[] = {"-MD5", "-SHA", "-SHA2-256", "-SHA2-384", 476 "-SHA2-512", "", 0 }; 477 char *mm_enc[] = {"DES_CBC", "BLOWFISH_CBC", "3DES_CBC", "CAST_CBC", 478 "AES_CBC", "AES_CBC", "AES_CBC", "AES_CBC", 0}; 479 char *mm_enc_p[] = {"DES", "BLF", "3DES", "CAST", "AES", "AES-128", 480 "AES-192", "AES-256", 0}; 481 char *dhgroup[] = {"MODP_1024", "MODP_768", "MODP_1024", 482 "MODP_1536", "MODP_2048", "MODP_3072", 0}; 483 char *dhgroup_p[] = {"", "-GRP1", "-GRP2", "-GRP5", "-GRP14", 484 "-GRP15", 0}; 485 char *qm_enc[] = {"DES", "3DES", "CAST", "BLOWFISH", "AES", 486 "AES", "AES", "AES", "AES_128_CTR", "AES_GCM_16", 487 "AES_GCM_16", "AES_GCM_16", "AES_GMAC", "AES_GMAC", 488 "AES_GMAC", "NULL", "NONE", 0}; 489 char *qm_enc_p[] = {"-DES", "-3DES", "-CAST", "-BLF", "-AES", 490 "-AES-128", "-AES-192", "-AES-256", "-AESCTR", 491 "-AESGCM-128", "-AESGCM-192", "-AESGCM-256", 492 "-AESGMAC-128", "-AESGMAC-192", "-AESGMAC-256", "-NULL", 493 "", 0}; 494 char *qm_hash[] = {"HMAC_MD5", "HMAC_SHA", "HMAC_RIPEMD", 495 "HMAC_SHA2_256", "HMAC_SHA2_384", "HMAC_SHA2_512", "NONE", 496 0}; 497 char *qm_hash_p[] = {"-MD5", "-SHA", "-RIPEMD", "-SHA2-256", 498 "-SHA2-384", "-SHA2-512", "", 0}; 499 char *qm_ah_id[] = {"MD5", "SHA", "RIPEMD", "SHA2_256", "SHA2_384", 500 "SHA2_512", "", 0}; 501 502 /* General and X509 defaults */ 503 conf_set(tr, "General", "Retransmits", CONF_DFLT_RETRANSMITS, 0, 1); 504 conf_set(tr, "General", "Exchange-max-time", CONF_DFLT_EXCH_MAX_TIME, 505 0, 1); 506 conf_set(tr, "General", "Use-Keynote", CONF_DFLT_USE_KEYNOTE, 0, 1); 507 conf_set(tr, "General", "Policy-file", CONF_DFLT_POLICY_FILE, 0, 1); 508 conf_set(tr, "General", "Pubkey-directory", CONF_DFLT_PUBKEY_DIR, 0, 509 1); 510 511 conf_set(tr, "X509-certificates", "CA-directory", 512 CONF_DFLT_X509_CA_DIR, 0, 1); 513 conf_set(tr, "X509-certificates", "Cert-directory", 514 CONF_DFLT_X509_CERT_DIR, 0, 1); 515 conf_set(tr, "X509-certificates", "Private-key", 516 CONF_DFLT_X509_PRIVATE_KEY, 0, 1); 517 conf_set(tr, "X509-certificates", "Private-key-directory", 518 CONF_DFLT_X509_PRIVATE_KEY_DIR, 0, 1); 519 conf_set(tr, "X509-certificates", "CRL-directory", 520 CONF_DFLT_X509_CRL_DIR, 0, 1); 521 522 conf_set(tr, "KeyNote", "Credential-directory", 523 CONF_DFLT_KEYNOTE_CRED_DIR, 0, 1); 524 525 conf_set(tr, "General", "Delete-SAs", CONF_DFLT_DELETE_SAS, 0, 1); 526 527 /* Lifetimes. XXX p1/p2 vs main/quick mode may be unclear. */ 528 dflt = conf_get_trans_str(tr, "General", "Default-phase-1-lifetime"); 529 conf_set(tr, CONF_DFLT_TAG_LIFE_MAIN_MODE, "LIFE_TYPE", 530 CONF_DFLT_TYPE_LIFE_MAIN_MODE, 0, 1); 531 conf_set(tr, CONF_DFLT_TAG_LIFE_MAIN_MODE, "LIFE_DURATION", 532 (dflt ? dflt : CONF_DFLT_VAL_LIFE_MAIN_MODE), 0, 1); 533 534 dflt = conf_get_trans_str(tr, "General", "Default-phase-2-lifetime"); 535 conf_set(tr, CONF_DFLT_TAG_LIFE_QUICK_MODE, "LIFE_TYPE", 536 CONF_DFLT_TYPE_LIFE_QUICK_MODE, 0, 1); 537 conf_set(tr, CONF_DFLT_TAG_LIFE_QUICK_MODE, "LIFE_DURATION", 538 (dflt ? dflt : CONF_DFLT_VAL_LIFE_QUICK_MODE), 0, 1); 539 540 /* Default Phase-1 Configuration section */ 541 conf_set(tr, CONF_DFLT_TAG_PHASE1_CONFIG, "EXCHANGE_TYPE", 542 CONF_DFLT_PHASE1_EXCH_TYPE, 0, 1); 543 conf_set(tr, CONF_DFLT_TAG_PHASE1_CONFIG, "Transforms", 544 CONF_DFLT_PHASE1_TRANSFORMS, 0, 1); 545 546 /* Main modes */ 547 for (enc = 0; mm_enc[enc]; enc++) 548 for (hash = 0; mm_hash[hash]; hash++) 549 for (auth = 0; mm_auth[auth]; auth++) 550 for (group = 0; dhgroup_p[group]; group++) 551 conf_load_defaults_mm (tr, mm_enc[enc], 552 mm_hash[hash], mm_auth[auth], 553 dhgroup[group], mm_enc_p[enc], 554 mm_auth_p[auth], dhgroup_p[group], 555 mm_hash_p[hash]); 556 557 /* Setup a default Phase 1 entry */ 558 conf_set(tr, "Phase 1", "Default", "Default-phase-1", 0, 1); 559 conf_set(tr, "Default-phase-1", "Phase", "1", 0, 1); 560 conf_set(tr, "Default-phase-1", "Configuration", 561 "Default-phase-1-configuration", 0, 1); 562 dflt = conf_get_trans_str(tr, "General", "Default-phase-1-ID"); 563 if (dflt) 564 conf_set(tr, "Default-phase-1", "ID", dflt, 0, 1); 565 566 /* Quick modes */ 567 for (enc = 0; qm_enc[enc]; enc++) 568 for (proto = 0; proto < 2; proto++) 569 for (mode = 0; mode < 2; mode++) 570 for (pfs = 0; pfs < 2; pfs++) 571 for (hash = 0; qm_hash[hash]; hash++) 572 for (group = 0; 573 dhgroup_p[group]; group++) 574 conf_load_defaults_qm( 575 tr, qm_enc[enc], 576 qm_hash[hash], 577 dhgroup[group], 578 qm_enc_p[enc], 579 qm_hash_p[hash], 580 qm_ah_id[hash], 581 dhgroup_p[group], 582 proto, mode, pfs); 583 } 584 585 void 586 conf_init(void) 587 { 588 unsigned int i; 589 590 for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++) 591 LIST_INIT(&conf_bindings[i]); 592 TAILQ_INIT(&conf_trans_queue); 593 conf_reinit(); 594 } 595 596 /* Open the config file and map it into our address space, then parse it. */ 597 void 598 conf_reinit(void) 599 { 600 struct conf_binding *cb = 0; 601 int fd, trans; 602 unsigned int i; 603 size_t sz; 604 char *new_conf_addr = 0; 605 606 fd = monitor_open(conf_path, O_RDONLY, 0); 607 if (fd == -1 || check_file_secrecy_fd(fd, conf_path, &sz) == -1) { 608 if (fd == -1 && errno != ENOENT) 609 log_error("conf_reinit: open(\"%s\", O_RDONLY, 0) " 610 "failed", conf_path); 611 if (fd != -1) 612 close(fd); 613 614 trans = conf_begin(); 615 } else { 616 new_conf_addr = malloc(sz); 617 if (!new_conf_addr) { 618 log_error("conf_reinit: malloc (%lu) failed", 619 (unsigned long)sz); 620 goto fail; 621 } 622 /* XXX I assume short reads won't happen here. */ 623 if (read(fd, new_conf_addr, sz) != (int)sz) { 624 log_error("conf_reinit: read (%d, %p, %lu) failed", 625 fd, new_conf_addr, (unsigned long)sz); 626 goto fail; 627 } 628 close(fd); 629 630 trans = conf_begin(); 631 632 /* XXX Should we not care about errors and rollback? */ 633 conf_parse(trans, new_conf_addr, sz); 634 } 635 636 /* Load default configuration values. */ 637 conf_load_defaults(trans); 638 639 /* Free potential existing configuration. */ 640 if (conf_addr) { 641 for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; 642 i++) 643 for (cb = LIST_FIRST(&conf_bindings[i]); cb; 644 cb = LIST_FIRST(&conf_bindings[i])) 645 conf_remove_now(cb->section, cb->tag); 646 free(conf_addr); 647 } 648 conf_end(trans, 1); 649 conf_addr = new_conf_addr; 650 return; 651 652 fail: 653 free(new_conf_addr); 654 close(fd); 655 } 656 657 /* 658 * Return the numeric value denoted by TAG in section SECTION or DEF 659 * if that tag does not exist. 660 */ 661 int 662 conf_get_num(char *section, char *tag, int def) 663 { 664 char *value = conf_get_str(section, tag); 665 666 if (value) 667 return atoi(value); 668 return def; 669 } 670 671 /* 672 * Return the socket endpoint address denoted by TAG in SECTION as a 673 * struct sockaddr. It is the callers responsibility to deallocate 674 * this structure when it is finished with it. 675 */ 676 struct sockaddr * 677 conf_get_address(char *section, char *tag) 678 { 679 char *value = conf_get_str(section, tag); 680 struct sockaddr *sa; 681 682 if (!value) 683 return 0; 684 if (text2sockaddr(value, 0, &sa, 0, 0) == -1) 685 return 0; 686 return sa; 687 } 688 689 /* Validate X according to the range denoted by TAG in section SECTION. */ 690 int 691 conf_match_num(char *section, char *tag, int x) 692 { 693 char *value = conf_get_str(section, tag); 694 int val, min, max, n; 695 696 if (!value) 697 return 0; 698 n = sscanf(value, "%d,%d:%d", &val, &min, &max); 699 switch (n) { 700 case 1: 701 LOG_DBG((LOG_MISC, 95, "conf_match_num: %s:%s %d==%d?", 702 section, tag, val, x)); 703 return x == val; 704 case 3: 705 LOG_DBG((LOG_MISC, 95, "conf_match_num: %s:%s %d<=%d<=%d?", 706 section, tag, min, x, max)); 707 return min <= x && max >= x; 708 default: 709 log_error("conf_match_num: section %s tag %s: invalid number " 710 "spec %s", section, tag, value); 711 } 712 return 0; 713 } 714 715 /* Return the string value denoted by TAG in section SECTION. */ 716 char * 717 conf_get_str(char *section, char *tag) 718 { 719 struct conf_binding *cb; 720 721 for (cb = LIST_FIRST(&conf_bindings[conf_hash(section)]); cb; 722 cb = LIST_NEXT(cb, link)) 723 if (strcasecmp(section, cb->section) == 0 && 724 strcasecmp(tag, cb->tag) == 0) { 725 LOG_DBG((LOG_MISC, 95, "conf_get_str: [%s]:%s->%s", 726 section, tag, cb->value)); 727 return cb->value; 728 } 729 LOG_DBG((LOG_MISC, 95, 730 "conf_get_str: configuration value not found [%s]:%s", section, 731 tag)); 732 return 0; 733 } 734 735 /* 736 * Build a list of string values out of the comma separated value denoted by 737 * TAG in SECTION. 738 */ 739 struct conf_list * 740 conf_get_list(char *section, char *tag) 741 { 742 char *liststr = 0, *p, *field, *t; 743 struct conf_list *list = 0; 744 struct conf_list_node *node = 0; 745 746 list = malloc(sizeof *list); 747 if (!list) 748 goto cleanup; 749 TAILQ_INIT(&list->fields); 750 list->cnt = 0; 751 liststr = conf_get_str(section, tag); 752 if (!liststr) 753 goto cleanup; 754 liststr = strdup(liststr); 755 if (!liststr) 756 goto cleanup; 757 p = liststr; 758 while ((field = strsep(&p, ",")) != NULL) { 759 /* Skip leading whitespace */ 760 while (isspace(*field)) 761 field++; 762 /* Skip trailing whitespace */ 763 if (p) 764 for (t = p - 1; t > field && isspace(*t); t--) 765 *t = '\0'; 766 if (*field == '\0') { 767 log_print("conf_get_list: empty field, ignoring..."); 768 continue; 769 } 770 list->cnt++; 771 node = calloc(1, sizeof *node); 772 if (!node) 773 goto cleanup; 774 node->field = strdup(field); 775 if (!node->field) 776 goto cleanup; 777 TAILQ_INSERT_TAIL(&list->fields, node, link); 778 } 779 free(liststr); 780 return list; 781 782 cleanup: 783 free(node); 784 if (list) 785 conf_free_list(list); 786 free(liststr); 787 return 0; 788 } 789 790 struct conf_list * 791 conf_get_tag_list(char *section) 792 { 793 struct conf_list *list = 0; 794 struct conf_list_node *node = 0; 795 struct conf_binding *cb; 796 797 list = malloc(sizeof *list); 798 if (!list) 799 goto cleanup; 800 TAILQ_INIT(&list->fields); 801 list->cnt = 0; 802 for (cb = LIST_FIRST(&conf_bindings[conf_hash(section)]); cb; 803 cb = LIST_NEXT(cb, link)) 804 if (strcasecmp(section, cb->section) == 0) { 805 list->cnt++; 806 node = calloc(1, sizeof *node); 807 if (!node) 808 goto cleanup; 809 node->field = strdup(cb->tag); 810 if (!node->field) 811 goto cleanup; 812 TAILQ_INSERT_TAIL(&list->fields, node, link); 813 } 814 return list; 815 816 cleanup: 817 free(node); 818 if (list) 819 conf_free_list(list); 820 return 0; 821 } 822 823 void 824 conf_free_list(struct conf_list *list) 825 { 826 struct conf_list_node *node = TAILQ_FIRST(&list->fields); 827 828 while (node) { 829 TAILQ_REMOVE(&list->fields, node, link); 830 free(node->field); 831 free(node); 832 node = TAILQ_FIRST(&list->fields); 833 } 834 free(list); 835 } 836 837 int 838 conf_begin(void) 839 { 840 static int seq = 0; 841 842 return ++seq; 843 } 844 845 static int 846 conf_trans_node(int transaction, enum conf_op op, char *section, char *tag, 847 char *value, int override, int is_default) 848 { 849 struct conf_trans *node; 850 851 node = calloc(1, sizeof *node); 852 if (!node) { 853 log_error("conf_trans_node: calloc (1, %lu) failed", 854 (unsigned long)sizeof *node); 855 return 1; 856 } 857 node->trans = transaction; 858 node->op = op; 859 node->override = override; 860 node->is_default = is_default; 861 if (section && (node->section = strdup(section)) == NULL) 862 goto fail; 863 if (tag && (node->tag = strdup(tag)) == NULL) 864 goto fail; 865 if (value && (node->value = strdup(value)) == NULL) 866 goto fail; 867 TAILQ_INSERT_TAIL(&conf_trans_queue, node, link); 868 return 0; 869 870 fail: 871 free(node->section); 872 free(node->tag); 873 free(node->value); 874 free(node); 875 return 1; 876 } 877 878 /* Queue a set operation. */ 879 int 880 conf_set(int transaction, char *section, char *tag, char *value, int override, 881 int is_default) 882 { 883 return conf_trans_node(transaction, CONF_SET, section, tag, value, 884 override, is_default); 885 } 886 887 /* Queue a remove operation. */ 888 int 889 conf_remove(int transaction, char *section, char *tag) 890 { 891 return conf_trans_node(transaction, CONF_REMOVE, section, tag, NULL, 892 0, 0); 893 } 894 895 /* Queue a remove section operation. */ 896 int 897 conf_remove_section(int transaction, char *section) 898 { 899 return conf_trans_node(transaction, CONF_REMOVE_SECTION, section, NULL, 900 NULL, 0, 0); 901 } 902 903 /* Execute all queued operations for this transaction. Cleanup. */ 904 int 905 conf_end(int transaction, int commit) 906 { 907 struct conf_trans *node, *next; 908 909 for (node = TAILQ_FIRST(&conf_trans_queue); node; node = next) { 910 next = TAILQ_NEXT(node, link); 911 if (node->trans == transaction) { 912 if (commit) 913 switch (node->op) { 914 case CONF_SET: 915 conf_set_now(node->section, node->tag, 916 node->value, node->override, 917 node->is_default); 918 break; 919 case CONF_REMOVE: 920 conf_remove_now(node->section, 921 node->tag); 922 break; 923 case CONF_REMOVE_SECTION: 924 conf_remove_section_now(node->section); 925 break; 926 default: 927 log_print("conf_end: unknown " 928 "operation: %d", node->op); 929 } 930 TAILQ_REMOVE(&conf_trans_queue, node, link); 931 free(node->section); 932 free(node->tag); 933 free(node->value); 934 free(node); 935 } 936 } 937 return 0; 938 } 939 940 /* 941 * Dump running configuration upon SIGUSR1. 942 * Configuration is "stored in reverse order", so reverse it again. 943 */ 944 struct dumper { 945 char *s, *v; 946 struct dumper *next; 947 }; 948 949 static void 950 conf_report_dump(struct dumper *node) 951 { 952 /* Recursive, cleanup when we're done. */ 953 954 if (node->next) 955 conf_report_dump(node->next); 956 957 if (node->v) 958 LOG_DBG((LOG_REPORT, 0, "%s=\t%s", node->s, node->v)); 959 else if (node->s) { 960 LOG_DBG((LOG_REPORT, 0, "%s", node->s)); 961 if (strlen(node->s) > 0) 962 free(node->s); 963 } 964 free(node); 965 } 966 967 void 968 conf_report(void) 969 { 970 struct conf_binding *cb, *last = 0; 971 unsigned int i; 972 char *current_section = (char *)0; 973 struct dumper *dumper, *dnode; 974 975 dumper = dnode = (struct dumper *)calloc(1, sizeof *dumper); 976 if (!dumper) 977 goto mem_fail; 978 979 LOG_DBG((LOG_REPORT, 0, "conf_report: dumping running configuration")); 980 981 for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++) 982 for (cb = LIST_FIRST(&conf_bindings[i]); cb; 983 cb = LIST_NEXT(cb, link)) { 984 if (!cb->is_default) { 985 /* Dump this entry. */ 986 if (!current_section || strcmp(cb->section, 987 current_section)) { 988 if (current_section) { 989 if (asprintf(&dnode->s, "[%s]", 990 current_section) == -1) 991 goto mem_fail; 992 dnode->next = (struct dumper *) 993 calloc(1, 994 sizeof(struct dumper)); 995 dnode = dnode->next; 996 if (!dnode) 997 goto mem_fail; 998 999 dnode->s = ""; 1000 dnode->next = (struct dumper *) 1001 calloc(1, 1002 sizeof(struct dumper)); 1003 dnode = dnode->next; 1004 if (!dnode) 1005 goto mem_fail; 1006 } 1007 current_section = cb->section; 1008 } 1009 dnode->s = cb->tag; 1010 dnode->v = cb->value; 1011 dnode->next = (struct dumper *) 1012 calloc(1, sizeof(struct dumper)); 1013 dnode = dnode->next; 1014 if (!dnode) 1015 goto mem_fail; 1016 last = cb; 1017 } 1018 } 1019 1020 if (last) 1021 if (asprintf(&dnode->s, "[%s]", last->section) == -1) 1022 goto mem_fail; 1023 conf_report_dump(dumper); 1024 1025 return; 1026 1027 mem_fail: 1028 log_error("conf_report: malloc/calloc failed"); 1029 while ((dnode = dumper) != 0) { 1030 dumper = dumper->next; 1031 free(dnode->s); 1032 free(dnode); 1033 } 1034 } 1035