xref: /openbsd/sbin/isakmpd/conf.c (revision 7ebc7616)
1*7ebc7616Smikeb /* $OpenBSD: conf.c,v 1.99 2010/09/22 13:45:15 mikeb Exp $	 */
2cc475db6Sniklas /* $EOM: conf.c,v 1.48 2000/12/04 02:04:29 angelos Exp $	 */
32040585eSniklas 
42040585eSniklas /*
542af7185Sniklas  * Copyright (c) 1998, 1999, 2000, 2001 Niklas Hallqvist.  All rights reserved.
690d8b2b0Sho  * Copyright (c) 2000, 2001, 2002 H�kan Olsson.  All rights reserved.
72040585eSniklas  *
82040585eSniklas  * Redistribution and use in source and binary forms, with or without
92040585eSniklas  * modification, are permitted provided that the following conditions
102040585eSniklas  * are met:
112040585eSniklas  * 1. Redistributions of source code must retain the above copyright
122040585eSniklas  *    notice, this list of conditions and the following disclaimer.
132040585eSniklas  * 2. Redistributions in binary form must reproduce the above copyright
142040585eSniklas  *    notice, this list of conditions and the following disclaimer in the
152040585eSniklas  *    documentation and/or other materials provided with the distribution.
162040585eSniklas  *
172040585eSniklas  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
182040585eSniklas  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
192040585eSniklas  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
202040585eSniklas  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
212040585eSniklas  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
222040585eSniklas  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232040585eSniklas  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242040585eSniklas  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252040585eSniklas  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
262040585eSniklas  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272040585eSniklas  */
282040585eSniklas 
292040585eSniklas /*
302040585eSniklas  * This code was written under funding by Ericsson Radio Systems.
312040585eSniklas  */
322040585eSniklas 
332040585eSniklas #include <sys/param.h>
342040585eSniklas #include <sys/mman.h>
352040585eSniklas #include <sys/queue.h>
3681c21331Sniklas #include <sys/socket.h>
372040585eSniklas #include <sys/stat.h>
3881c21331Sniklas #include <netinet/in.h>
3981c21331Sniklas #include <arpa/inet.h>
402040585eSniklas #include <ctype.h>
412040585eSniklas #include <fcntl.h>
422040585eSniklas #include <stdio.h>
432040585eSniklas #include <stdlib.h>
442040585eSniklas #include <string.h>
452040585eSniklas #include <unistd.h>
463eed80ffSniklas #include <errno.h>
472040585eSniklas 
48a2d30fd1Sniklas #include "app.h"
492040585eSniklas #include "conf.h"
502040585eSniklas #include "log.h"
51da35d433Sho #include "monitor.h"
52bda02003Sniklas #include "util.h"
532040585eSniklas 
544c8c122bSho static char    *conf_get_trans_str(int, char *, char *);
554c8c122bSho static void     conf_load_defaults(int);
564c8c122bSho #if 0
574c8c122bSho static int      conf_find_trans_xf(int, char *);
584c8c122bSho #endif
594c8c122bSho 
60f8f1e192Sniklas struct conf_trans {
61f8f1e192Sniklas 	TAILQ_ENTRY(conf_trans) link;
62f8f1e192Sniklas 	int	 trans;
63fb9475d6Sderaadt 	enum conf_op {
64fb9475d6Sderaadt 		CONF_SET, CONF_REMOVE, CONF_REMOVE_SECTION
65fb9475d6Sderaadt 	}	 op;
66f8f1e192Sniklas 	char	*section;
67f8f1e192Sniklas 	char	*tag;
68f8f1e192Sniklas 	char	*value;
69f8f1e192Sniklas 	int	 override;
70510d8b0cSniklas 	int	 is_default;
71f8f1e192Sniklas };
72f8f1e192Sniklas 
73d865f642Sho #define CONF_SECT_MAX 256
74d865f642Sho 
75f8f1e192Sniklas TAILQ_HEAD(conf_trans_head, conf_trans) conf_trans_queue;
76f8f1e192Sniklas 
772040585eSniklas struct conf_binding {
782040585eSniklas 	LIST_ENTRY(conf_binding) link;
792040585eSniklas 	char	*section;
802040585eSniklas 	char	*tag;
812040585eSniklas 	char	*value;
82510d8b0cSniklas 	int	 is_default;
832040585eSniklas };
842040585eSniklas 
852040585eSniklas char	*conf_path = CONFIG_FILE;
86f8f1e192Sniklas LIST_HEAD(conf_bindings, conf_binding) conf_bindings[256];
872040585eSniklas 
882040585eSniklas static char	*conf_addr;
89f8f1e192Sniklas static __inline__ u_int8_t
90f8f1e192Sniklas conf_hash(char *s)
91f8f1e192Sniklas {
92f8f1e192Sniklas 	u_int8_t hash = 0;
93f8f1e192Sniklas 
94fb9475d6Sderaadt 	while (*s) {
95f8f1e192Sniklas 		hash = ((hash << 1) | (hash >> 7)) ^ tolower(*s);
96f8f1e192Sniklas 		s++;
97f8f1e192Sniklas 	}
98f8f1e192Sniklas 	return hash;
99f8f1e192Sniklas }
100f8f1e192Sniklas 
101f8f1e192Sniklas /*
102f8f1e192Sniklas  * Insert a tag-value combination from LINE (the equal sign is at POS)
103f8f1e192Sniklas  */
104f8f1e192Sniklas static int
105f8f1e192Sniklas conf_remove_now(char *section, char *tag)
106f8f1e192Sniklas {
107f8f1e192Sniklas 	struct conf_binding *cb, *next;
108f8f1e192Sniklas 
109df915834Shshoexer 	for (cb = LIST_FIRST(&conf_bindings[conf_hash(section)]); cb;
110df915834Shshoexer 	    cb = next) {
111f8f1e192Sniklas 		next = LIST_NEXT(cb, link);
1129d6bd3cfSderaadt 		if (strcasecmp(cb->section, section) == 0 &&
1139d6bd3cfSderaadt 		    strcasecmp(cb->tag, tag) == 0) {
114f8f1e192Sniklas 			LIST_REMOVE(cb, link);
115df915834Shshoexer 			LOG_DBG((LOG_MISC, 95, "[%s]:%s->%s removed", section,
116df915834Shshoexer 			    tag, cb->value));
117f8f1e192Sniklas 			free(cb->section);
118f8f1e192Sniklas 			free(cb->tag);
119f8f1e192Sniklas 			free(cb->value);
120f8f1e192Sniklas 			free(cb);
121f8f1e192Sniklas 			return 0;
122f8f1e192Sniklas 		}
123f8f1e192Sniklas 	}
124f8f1e192Sniklas 	return 1;
125f8f1e192Sniklas }
126f8f1e192Sniklas 
127f8f1e192Sniklas static int
128f8f1e192Sniklas conf_remove_section_now(char *section)
129f8f1e192Sniklas {
130f8f1e192Sniklas 	struct conf_binding *cb, *next;
131f8f1e192Sniklas 	int	unseen = 1;
132f8f1e192Sniklas 
133df915834Shshoexer 	for (cb = LIST_FIRST(&conf_bindings[conf_hash(section)]); cb;
134df915834Shshoexer 	    cb = next) {
135f8f1e192Sniklas 		next = LIST_NEXT(cb, link);
136fb9475d6Sderaadt 		if (strcasecmp(cb->section, section) == 0) {
137f8f1e192Sniklas 			unseen = 0;
138f8f1e192Sniklas 			LIST_REMOVE(cb, link);
139df915834Shshoexer 			LOG_DBG((LOG_MISC, 95, "[%s]:%s->%s removed", section,
140df915834Shshoexer 			    cb->tag, cb->value));
141f8f1e192Sniklas 			free(cb->section);
142f8f1e192Sniklas 			free(cb->tag);
143f8f1e192Sniklas 			free(cb->value);
144f8f1e192Sniklas 			free(cb);
145f8f1e192Sniklas 		}
146f8f1e192Sniklas 	}
147f8f1e192Sniklas 	return unseen;
148f8f1e192Sniklas }
149f8f1e192Sniklas 
1502040585eSniklas /*
1512040585eSniklas  * Insert a tag-value combination from LINE (the equal sign is at POS)
1522040585eSniklas  * into SECTION of our configuration database.
1532040585eSniklas  */
154f8f1e192Sniklas static int
155510d8b0cSniklas conf_set_now(char *section, char *tag, char *value, int override,
156510d8b0cSniklas     int is_default)
1572040585eSniklas {
158f8f1e192Sniklas 	struct conf_binding *node = 0;
1592040585eSniklas 
160f8f1e192Sniklas 	if (override)
161f8f1e192Sniklas 		conf_remove_now(section, tag);
162fb9475d6Sderaadt 	else if (conf_get_str(section, tag)) {
16313e19299Sniklas 		if (!is_default)
164df915834Shshoexer 			log_print("conf_set_now: duplicate tag [%s]:%s, "
165df915834Shshoexer 			    "ignoring...\n", section, tag);
166f8f1e192Sniklas 		return 1;
1672040585eSniklas 	}
168f8f1e192Sniklas 	node = calloc(1, sizeof *node);
169fb9475d6Sderaadt 	if (!node) {
170df915834Shshoexer 		log_error("conf_set_now: calloc (1, %lu) failed",
171df915834Shshoexer 		    (unsigned long)sizeof *node);
172f8f1e192Sniklas 		return 1;
173f8f1e192Sniklas 	}
174cd2f144aScloder 	node->section = node->tag = node->value = NULL;
175cd2f144aScloder 	if ((node->section = strdup(section)) == NULL)
176cd2f144aScloder 		goto fail;
177cd2f144aScloder 	if ((node->tag = strdup(tag)) == NULL)
178cd2f144aScloder 		goto fail;
179cd2f144aScloder 	if ((node->value = strdup(value)) == NULL)
180cd2f144aScloder 		goto fail;
181510d8b0cSniklas 	node->is_default = is_default;
182f8f1e192Sniklas 
183f8f1e192Sniklas 	LIST_INSERT_HEAD(&conf_bindings[conf_hash(section)], node, link);
184df915834Shshoexer 	LOG_DBG((LOG_MISC, 95, "conf_set_now: [%s]:%s->%s", node->section,
185df915834Shshoexer 	    node->tag, node->value));
186f8f1e192Sniklas 	return 0;
187cd2f144aScloder fail:
188cd2f144aScloder 	free(node->value);
189cd2f144aScloder 	free(node->tag);
190cd2f144aScloder 	free(node->section);
191028e4eafSmoritz 	free(node);
192cd2f144aScloder 	return 1;
1932040585eSniklas }
1942040585eSniklas 
1952040585eSniklas /*
1962040585eSniklas  * Parse the line LINE of SZ bytes.  Skip Comments, recognize section
1972040585eSniklas  * headers and feed tag-value pairs into our configuration database.
1982040585eSniklas  */
1992040585eSniklas static void
200e157c6afSmoritz conf_parse_line(int trans, char *line, int ln, size_t sz)
2012040585eSniklas {
20290d8b2b0Sho 	char	*val;
203cde22268Sho 	size_t	 i;
204cde22268Sho 	int	 j;
2052040585eSniklas 	static char *section = 0;
2062040585eSniklas 
2072040585eSniklas 	/* Lines starting with '#' or ';' are comments.  */
2082040585eSniklas 	if (*line == '#' || *line == ';')
2092040585eSniklas 		return;
2102040585eSniklas 
2112040585eSniklas 	/* '[section]' parsing...  */
212fb9475d6Sderaadt 	if (*line == '[') {
2132040585eSniklas 		for (i = 1; i < sz; i++)
2142040585eSniklas 			if (line[i] == ']')
2152040585eSniklas 				break;
21690d8b2b0Sho 		free(section);
217fb9475d6Sderaadt 		if (i == sz) {
2182040585eSniklas 			log_print("conf_parse_line: %d:"
21950eea14cSho 			    "unmatched ']', ignoring until next section", ln);
2202040585eSniklas 			section = 0;
2212040585eSniklas 			return;
2222040585eSniklas 		}
2232040585eSniklas 		section = malloc(i);
224fb9475d6Sderaadt 		if (!section) {
225df915834Shshoexer 			log_print("conf_parse_line: %d: malloc (%lu) failed",
226df915834Shshoexer 			    ln, (unsigned long)i);
22790d8b2b0Sho 			return;
22890d8b2b0Sho 		}
229b8380d91Sho 		strlcpy(section, line + 1, i);
2302040585eSniklas 		return;
2312040585eSniklas 	}
2322040585eSniklas 	/* Deal with assignments.  */
2332040585eSniklas 	for (i = 0; i < sz; i++)
234fb9475d6Sderaadt 		if (line[i] == '=') {
2352040585eSniklas 			/* If no section, we are ignoring the lines.  */
236fb9475d6Sderaadt 			if (!section) {
23780cd8be9Sderaadt 				log_print("conf_parse_line: %d: ignoring line "
23880cd8be9Sderaadt 				    "due to no section", ln);
2392040585eSniklas 				return;
2402040585eSniklas 			}
241f8f1e192Sniklas 			line[strcspn(line, " \t=")] = '\0';
24290d8b2b0Sho 			val = line + i + 1 + strspn(line + i + 1, " \t");
24390d8b2b0Sho 			/* Skip trailing whitespace, if any */
244df915834Shshoexer 			for (j = sz - (val - line) - 1; j > 0 &&
245df915834Shshoexer 			    isspace(val[j]); j--)
246cde22268Sho 				val[j] = '\0';
247f8f1e192Sniklas 			/* XXX Perhaps should we not ignore errors?  */
24890d8b2b0Sho 			conf_set(trans, section, line, val, 0, 0);
2492040585eSniklas 			return;
2502040585eSniklas 		}
251d6fd0492Spvalchev 	/* Other non-empty lines are weird.  */
2522040585eSniklas 	i = strspn(line, " \t");
2532040585eSniklas 	if (line[i])
2542040585eSniklas 		log_print("conf_parse_line: %d: syntax error", ln);
2552040585eSniklas }
2562040585eSniklas 
2572040585eSniklas /* Parse the mapped configuration file.  */
2582040585eSniklas static void
259f8f1e192Sniklas conf_parse(int trans, char *buf, size_t sz)
2602040585eSniklas {
261f8f1e192Sniklas 	char	*cp = buf;
262f8f1e192Sniklas 	char	*bufend = buf + sz;
2632040585eSniklas 	char	*line;
264e157c6afSmoritz 	int	ln = 1;
2652040585eSniklas 
2662040585eSniklas 	line = cp;
267fb9475d6Sderaadt 	while (cp < bufend) {
268fb9475d6Sderaadt 		if (*cp == '\n') {
2692040585eSniklas 			/* Check for escaped newlines.  */
270f8f1e192Sniklas 			if (cp > buf && *(cp - 1) == '\\')
2712040585eSniklas 				*(cp - 1) = *cp = ' ';
272fb9475d6Sderaadt 			else {
2732040585eSniklas 				*cp = '\0';
274e157c6afSmoritz 				conf_parse_line(trans, line, ln, cp - line);
2752040585eSniklas 				line = cp + 1;
2762040585eSniklas 			}
277e157c6afSmoritz 			ln++;
2782040585eSniklas 		}
2792040585eSniklas 		cp++;
2802040585eSniklas 	}
2812040585eSniklas 	if (cp != line)
28250eea14cSho 		log_print("conf_parse: last line unterminated, ignored.");
2832040585eSniklas }
2842040585eSniklas 
285510d8b0cSniklas /*
286510d8b0cSniklas  * Auto-generate default configuration values for the transforms and
287510d8b0cSniklas  * suites the user wants.
288510d8b0cSniklas  *
289510d8b0cSniklas  * Resulting section names can be:
290510d8b0cSniklas  *  For main mode:
291601f7947Shshoexer  *     {DES,BLF,3DES,CAST,AES,AES-{128,192,256}-{MD5,SHA,SHA2-{256,384,512}} \
292601f7947Shshoexer  *         [-GRP{1,2,5,14,15}][-{DSS,RSA_SIG}]
293510d8b0cSniklas  *  For quick mode:
294e33f6eeeSho  *     QM-{proto}[-TRP]-{cipher}[-{hash}][-PFS[-{group}]]-SUITE
295e33f6eeeSho  *     where
296e33f6eeeSho  *       {proto}  = ESP, AH
297601f7947Shshoexer  *       {cipher} = DES, 3DES, CAST, BLF, AES, AES-{128,192,256}, AESCTR
298a008c50bShshoexer  *       {hash}   = MD5, SHA, RIPEMD, SHA2-{256,384,512}
29906ee6934Shshoexer  *       {group}  = GRP1, GRP2, GRP5, GRP14, GRP15
300e33f6eeeSho  *
301e33f6eeeSho  * DH group defaults to MODP_1024.
302510d8b0cSniklas  *
303e3b891b7Sderaadt  * XXX We may want to support USE_TRIPLEDES, etc...
304510d8b0cSniklas  * XXX No EC2N DH support here yet.
305510d8b0cSniklas  */
306510d8b0cSniklas 
3070eb823c5Sniklas /* Find the value for a section+tag in the transaction list.  */
3084c8c122bSho static char *
309eee423ceSho conf_get_trans_str(int trans, char *section, char *tag)
310eee423ceSho {
311eee423ceSho 	struct conf_trans *node, *nf = 0;
312eee423ceSho 
313eee423ceSho 	for (node = TAILQ_FIRST(&conf_trans_queue); node;
314eee423ceSho 	    node = TAILQ_NEXT(node, link))
315df915834Shshoexer 		if (node->trans == trans && strcasecmp(section, node->section)
316df915834Shshoexer 		    == 0 && strcasecmp(tag, node->tag) == 0) {
317eee423ceSho 			if (!nf)
318eee423ceSho 				nf = node;
319eee423ceSho 			else if (node->override)
320eee423ceSho 				nf = node;
321eee423ceSho 		}
3220eb823c5Sniklas 	return nf ? nf->value : 0;
323eee423ceSho }
324eee423ceSho 
3254c8c122bSho #if 0
3264c8c122bSho /* XXX Currently unused.  */
3274c8c122bSho static int
328510d8b0cSniklas conf_find_trans_xf(int phase, char *xf)
329510d8b0cSniklas {
330510d8b0cSniklas 	struct conf_trans *node;
331510d8b0cSniklas 	char	*p;
332510d8b0cSniklas 
333510d8b0cSniklas 	/* Find the relevant transforms and suites, if any.  */
334510d8b0cSniklas 	for (node = TAILQ_FIRST(&conf_trans_queue); node;
335510d8b0cSniklas 	    node = TAILQ_NEXT(node, link))
336eee423ceSho 		if ((phase == 1 && strcmp("Transforms", node->tag) == 0) ||
337fb9475d6Sderaadt 		    (phase == 2 && strcmp("Suites", node->tag) == 0)) {
338510d8b0cSniklas 			p = node->value;
339510d8b0cSniklas 			while ((p = strstr(p, xf)) != NULL)
34050eea14cSho 				if (*(p + strlen(p)) &&
34150eea14cSho 				    *(p + strlen(p)) != ',')
342510d8b0cSniklas 					p += strlen(p);
343510d8b0cSniklas 				else
344510d8b0cSniklas 					return 1;
345510d8b0cSniklas 		}
346510d8b0cSniklas 	return 0;
347510d8b0cSniklas }
3484c8c122bSho #endif
349510d8b0cSniklas 
3504c8c122bSho static void
351d865f642Sho conf_load_defaults_mm(int tr, char *mme, char *mmh, char *mma, char *dhg,
352a008c50bShshoexer     char *mme_p, char *mma_p, char *dhg_p, char *mmh_p)
353510d8b0cSniklas {
354d865f642Sho 	char sect[CONF_SECT_MAX];
355510d8b0cSniklas 
356a008c50bShshoexer 	snprintf(sect, sizeof sect, "%s%s%s%s", mme_p, mmh_p, dhg_p, mma_p);
357510d8b0cSniklas 
35855665484Sho 	LOG_DBG((LOG_MISC, 95, "conf_load_defaults_mm: main mode %s", sect));
359d865f642Sho 
360d865f642Sho 	conf_set(tr, sect, "ENCRYPTION_ALGORITHM", mme, 0, 1);
361d865f642Sho 	if (strcmp(mme, "BLOWFISH_CBC") == 0)
362d865f642Sho 		conf_set(tr, sect, "KEY_LENGTH", CONF_DFLT_VAL_BLF_KEYLEN, 0,
363d865f642Sho 		    1);
364601f7947Shshoexer         else if (strcmp(mme_p, "AES-128") == 0)
365601f7947Shshoexer                 conf_set(tr, sect, "KEY_LENGTH", "128,128:128", 0, 1);
366601f7947Shshoexer         else if (strcmp(mme_p, "AES-192") == 0)
367601f7947Shshoexer                 conf_set(tr, sect, "KEY_LENGTH", "192,192:192", 0, 1);
368601f7947Shshoexer         else if (strcmp(mme_p, "AES-256") == 0)
369601f7947Shshoexer                 conf_set(tr, sect, "KEY_LENGTH", "256,256:256", 0, 1);
370d865f642Sho 	else if (strcmp(mme, "AES_CBC") == 0)
371d865f642Sho 		conf_set(tr, sect, "KEY_LENGTH", CONF_DFLT_VAL_AES_KEYLEN, 0,
372d865f642Sho 		    1);
373d865f642Sho 
374d865f642Sho 	conf_set(tr, sect, "HASH_ALGORITHM", mmh, 0, 1);
375d865f642Sho 	conf_set(tr, sect, "AUTHENTICATION_METHOD", mma, 0, 1);
376d865f642Sho 	conf_set(tr, sect, "GROUP_DESCRIPTION", dhg, 0, 1);
377d865f642Sho 	conf_set(tr, sect, "Life", CONF_DFLT_TAG_LIFE_MAIN_MODE, 0, 1);
378d865f642Sho }
379d865f642Sho 
380d865f642Sho static void
381d865f642Sho conf_load_defaults_qm(int tr, char *qme, char *qmh, char *dhg, char *qme_p,
3826d2b9615Shshoexer     char *qmh_p, char *qm_ah_id, char *dhg_p, int proto, int mode, int pfs)
383d865f642Sho {
384d865f642Sho 	char sect[CONF_SECT_MAX], tmp[CONF_SECT_MAX];
385510d8b0cSniklas 
386510d8b0cSniklas 	/* Helper #defines, incl abbreviations.  */
387510d8b0cSniklas #define PROTO(x)  ((x) ? "AH" : "ESP")
388510d8b0cSniklas #define PFS(x)    ((x) ? "-PFS" : "")
389510d8b0cSniklas #define MODE(x)   ((x) ? "TRANSPORT" : "TUNNEL")
390510d8b0cSniklas #define MODE_p(x) ((x) ? "-TRP" : "")
391d865f642Sho 
3926d2b9615Shshoexer 	/* For AH a hash must be present and no encryption is allowed */
3936d2b9615Shshoexer 	if (proto == 1 && (strcmp(qmh, "NONE") == 0 ||
3946d2b9615Shshoexer 	    strcmp(qme, "NONE") != 0))
3956d2b9615Shshoexer 		return;
3966d2b9615Shshoexer 
3976d2b9615Shshoexer 	/* For ESP encryption must be provided, an empty hash is ok. */
3986d2b9615Shshoexer 	if (proto == 0 && strcmp(qme, "NONE") == 0)
3996d2b9615Shshoexer 		return;
4006d2b9615Shshoexer 
4016d2b9615Shshoexer 	/* When PFS is disabled no DH group must be specified. */
4026d2b9615Shshoexer 	if (pfs == 0 && strcmp(dhg_p, ""))
403d865f642Sho 		return;
404d865f642Sho 
405*7ebc7616Smikeb 	/* For GCM no additional authentication must be specified */
406*7ebc7616Smikeb 	if (proto == 0 && strcmp(qmh, "NONE") != 0 &&
407*7ebc7616Smikeb 	    (strcmp(qme, "AES_GCM_16") == 0 || strcmp(qme, "AES_GMAC") == 0))
408*7ebc7616Smikeb 		return;
409*7ebc7616Smikeb 
410d865f642Sho 	snprintf(tmp, sizeof tmp, "QM-%s%s%s%s%s%s", PROTO(proto),
411d865f642Sho 	    MODE_p(mode), qme_p, qmh_p, PFS(pfs), dhg_p);
412d865f642Sho 
413d865f642Sho 	strlcpy(sect, tmp, CONF_SECT_MAX);
414d865f642Sho 	strlcat(sect, "-SUITE",	CONF_SECT_MAX);
415d865f642Sho 
41655665484Sho 	LOG_DBG((LOG_MISC, 95, "conf_load_defaults_qm: quick mode %s", sect));
417d865f642Sho 
418d865f642Sho 	conf_set(tr, sect, "Protocols", tmp, 0, 1);
419d865f642Sho 	snprintf(sect, sizeof sect, "IPSEC_%s", PROTO(proto));
420d865f642Sho 	conf_set(tr, tmp, "PROTOCOL_ID", sect, 0, 1);
421d865f642Sho 	strlcpy(sect, tmp, CONF_SECT_MAX);
422d865f642Sho 	strlcat(sect, "-XF", CONF_SECT_MAX);
423d865f642Sho 	conf_set(tr, tmp, "Transforms", sect, 0, 1);
424d865f642Sho 
425d865f642Sho 	/*
426d865f642Sho 	 * XXX For now, defaults
427d865f642Sho 	 * contain one xf per protocol.
428d865f642Sho 	 */
4296d2b9615Shshoexer 	if (proto == 0)
430d865f642Sho 		conf_set(tr, sect, "TRANSFORM_ID", qme, 0, 1);
4316d2b9615Shshoexer 	else
4326d2b9615Shshoexer 		conf_set(tr, sect, "TRANSFORM_ID", qm_ah_id, 0, 1);
433d865f642Sho 	if (strcmp(qme ,"BLOWFISH") == 0)
434d865f642Sho 		conf_set(tr, sect, "KEY_LENGTH", CONF_DFLT_VAL_BLF_KEYLEN, 0,
435d865f642Sho 			 1);
436*7ebc7616Smikeb 	else if (strcmp(qme_p, "-AES-128") == 0 ||
437*7ebc7616Smikeb 	    strcmp(qme_p, "-AESGCM-128") == 0 ||
438*7ebc7616Smikeb 	    strcmp(qme_p, "-AESGMAC-128") == 0)
439200a7bcfSderaadt 		conf_set(tr, sect, "KEY_LENGTH", "128,128:128", 0, 1);
440*7ebc7616Smikeb 	else if (strcmp(qme_p, "-AES-192") == 0 ||
441*7ebc7616Smikeb 	    strcmp(qme_p, "-AESGCM-192") == 0 ||
442*7ebc7616Smikeb 	    strcmp(qme_p, "-AESGMAC-192") == 0)
443601f7947Shshoexer 		conf_set(tr, sect, "KEY_LENGTH", "192,192:192", 0, 1);
444*7ebc7616Smikeb 	else if (strcmp(qme_p, "-AES-256") == 0 ||
445*7ebc7616Smikeb 	    strcmp(qme_p, "-AESGCM-256") == 0 ||
446*7ebc7616Smikeb 	    strcmp(qme_p, "-AESGMAC-256") == 0)
447601f7947Shshoexer 		conf_set(tr, sect, "KEY_LENGTH", "256,256:256", 0, 1);
448d865f642Sho 	else if	(strcmp(qme, "AES") == 0)
449d865f642Sho 		conf_set(tr, sect, "KEY_LENGTH", CONF_DFLT_VAL_AES_KEYLEN, 0,
450d865f642Sho 			 1);
451601f7947Shshoexer 
452d865f642Sho 	conf_set(tr, sect, "ENCAPSULATION_MODE", MODE(mode), 0, 1);
453d865f642Sho 	if (strcmp(qmh, "NONE")) {
454d865f642Sho 		conf_set(tr, sect, "AUTHENTICATION_ALGORITHM", qmh, 0, 1);
455d865f642Sho 
456d865f642Sho 		/* XXX Another shortcut to keep length down */
457d865f642Sho 		if (pfs)
458d865f642Sho 			conf_set(tr, sect, "GROUP_DESCRIPTION", dhg, 0, 1);
459d865f642Sho 	}
460d865f642Sho 
461d865f642Sho 	/* XXX Lifetimes depending on enc/auth strength? */
462d865f642Sho 	conf_set(tr, sect, "Life", CONF_DFLT_TAG_LIFE_QUICK_MODE, 0, 1);
463d865f642Sho }
464d865f642Sho 
465d865f642Sho static void
466d865f642Sho conf_load_defaults(int tr)
467d865f642Sho {
468d865f642Sho 	int	 enc, auth, hash, group, proto, mode, pfs;
469d865f642Sho 	char	*dflt;
470d865f642Sho 
471d865f642Sho 	char	*mm_auth[] = {"PRE_SHARED", "DSS", "RSA_SIG", 0};
472d865f642Sho 	char	*mm_auth_p[] = {"", "-DSS", "-RSA_SIG", 0};
473a008c50bShshoexer 	char	*mm_hash[] = {"MD5", "SHA", "SHA2_256", "SHA2_384", "SHA2_512",
474a008c50bShshoexer 		     0};
475a008c50bShshoexer 	char	*mm_hash_p[] = {"-MD5", "-SHA", "-SHA2-256", "-SHA2-384",
476a008c50bShshoexer 		    "-SHA2-512", "", 0 };
477df915834Shshoexer 	char	*mm_enc[] = {"DES_CBC", "BLOWFISH_CBC", "3DES_CBC", "CAST_CBC",
478601f7947Shshoexer 		    "AES_CBC", "AES_CBC", "AES_CBC", "AES_CBC", 0};
479601f7947Shshoexer 	char	*mm_enc_p[] = {"DES", "BLF", "3DES", "CAST", "AES", "AES-128",
480601f7947Shshoexer 		    "AES-192", "AES-256", 0};
481d865f642Sho 	char	*dhgroup[] = {"MODP_1024", "MODP_768", "MODP_1024",
48206ee6934Shshoexer 		    "MODP_1536", "MODP_2048", "MODP_3072", 0};
48306ee6934Shshoexer 	char	*dhgroup_p[] = {"", "-GRP1", "-GRP2", "-GRP5", "-GRP14",
48406ee6934Shshoexer 		    "-GRP15", 0};
485a008c50bShshoexer 	char	*qm_enc[] = {"DES", "3DES", "CAST", "BLOWFISH", "AES",
486*7ebc7616Smikeb 		    "AES", "AES", "AES", "AES_128_CTR", "AES_GCM_16",
487*7ebc7616Smikeb 		    "AES_GCM_16", "AES_GCM_16", "AES_GMAC", "AES_GMAC",
488*7ebc7616Smikeb 		    "AES_GMAC", "NULL", "NONE", 0};
489a008c50bShshoexer 	char	*qm_enc_p[] = {"-DES", "-3DES", "-CAST", "-BLF", "-AES",
490*7ebc7616Smikeb 		    "-AES-128", "-AES-192", "-AES-256", "-AESCTR",
491*7ebc7616Smikeb 		    "-AESGCM-128", "-AESGCM-192", "-AESGCM-256",
492*7ebc7616Smikeb 		    "-AESGMAC-128", "-AESGMAC-192", "-AESGMAC-256", "-NULL",
493601f7947Shshoexer 		    "", 0};
494d865f642Sho 	char	*qm_hash[] = {"HMAC_MD5", "HMAC_SHA", "HMAC_RIPEMD",
495df915834Shshoexer 		    "HMAC_SHA2_256", "HMAC_SHA2_384", "HMAC_SHA2_512", "NONE",
496df915834Shshoexer 		    0};
497d865f642Sho 	char	*qm_hash_p[] = {"-MD5", "-SHA", "-RIPEMD", "-SHA2-256",
498d865f642Sho 		    "-SHA2-384", "-SHA2-512", "", 0};
4996d2b9615Shshoexer 	char	*qm_ah_id[] = {"MD5", "SHA", "RIPEMD", "SHA2_256", "SHA2_384",
5006d2b9615Shshoexer 		    "SHA2_512", "", 0};
501510d8b0cSniklas 
502510d8b0cSniklas 	/* General and X509 defaults */
503510d8b0cSniklas 	conf_set(tr, "General", "Retransmits", CONF_DFLT_RETRANSMITS, 0, 1);
504d865f642Sho 	conf_set(tr, "General", "Exchange-max-time", CONF_DFLT_EXCH_MAX_TIME,
505d865f642Sho 	    0, 1);
506b6e0b5cbShshoexer 	conf_set(tr, "General", "Use-Keynote", CONF_DFLT_USE_KEYNOTE, 0, 1);
507510d8b0cSniklas 	conf_set(tr, "General", "Policy-file", CONF_DFLT_POLICY_FILE, 0, 1);
508d865f642Sho 	conf_set(tr, "General", "Pubkey-directory", CONF_DFLT_PUBKEY_DIR, 0,
509d865f642Sho 	    1);
510510d8b0cSniklas 
511d865f642Sho 	conf_set(tr, "X509-certificates", "CA-directory",
512d865f642Sho 	    CONF_DFLT_X509_CA_DIR, 0, 1);
513d865f642Sho 	conf_set(tr, "X509-certificates", "Cert-directory",
514d865f642Sho 	    CONF_DFLT_X509_CERT_DIR, 0, 1);
515d865f642Sho 	conf_set(tr, "X509-certificates", "Private-key",
516d865f642Sho 	    CONF_DFLT_X509_PRIVATE_KEY, 0, 1);
517db26b2b9Smsf 	conf_set(tr, "X509-certificates", "Private-key-directory",
518db26b2b9Smsf 	    CONF_DFLT_X509_PRIVATE_KEY_DIR, 0, 1);
519d865f642Sho 	conf_set(tr, "X509-certificates", "CRL-directory",
520d865f642Sho 	    CONF_DFLT_X509_CRL_DIR, 0, 1);
521510d8b0cSniklas 
522df915834Shshoexer 	conf_set(tr, "KeyNote", "Credential-directory",
523df915834Shshoexer 	    CONF_DFLT_KEYNOTE_CRED_DIR, 0, 1);
52413e19299Sniklas 
525428bd1b5Shshoexer 	conf_set(tr, "General", "Delete-SAs", CONF_DFLT_DELETE_SAS, 0, 1);
526428bd1b5Shshoexer 
52728d27e6cSangelos 	/* Lifetimes. XXX p1/p2 vs main/quick mode may be unclear.  */
528eee423ceSho 	dflt = conf_get_trans_str(tr, "General", "Default-phase-1-lifetime");
52928d27e6cSangelos 	conf_set(tr, CONF_DFLT_TAG_LIFE_MAIN_MODE, "LIFE_TYPE",
53028d27e6cSangelos 	    CONF_DFLT_TYPE_LIFE_MAIN_MODE, 0, 1);
53128d27e6cSangelos 	conf_set(tr, CONF_DFLT_TAG_LIFE_MAIN_MODE, "LIFE_DURATION",
53228d27e6cSangelos 	    (dflt ? dflt : CONF_DFLT_VAL_LIFE_MAIN_MODE), 0, 1);
53328d27e6cSangelos 
534eee423ceSho 	dflt = conf_get_trans_str(tr, "General", "Default-phase-2-lifetime");
53528d27e6cSangelos 	conf_set(tr, CONF_DFLT_TAG_LIFE_QUICK_MODE, "LIFE_TYPE",
53628d27e6cSangelos 	    CONF_DFLT_TYPE_LIFE_QUICK_MODE, 0, 1);
53728d27e6cSangelos 	conf_set(tr, CONF_DFLT_TAG_LIFE_QUICK_MODE, "LIFE_DURATION",
53828d27e6cSangelos 	    (dflt ? dflt : CONF_DFLT_VAL_LIFE_QUICK_MODE), 0, 1);
53928d27e6cSangelos 
540419caefeSho 	/* Default Phase-1 Configuration section */
541419caefeSho 	conf_set(tr, CONF_DFLT_TAG_PHASE1_CONFIG, "EXCHANGE_TYPE",
542419caefeSho 	    CONF_DFLT_PHASE1_EXCH_TYPE, 0, 1);
543419caefeSho 	conf_set(tr, CONF_DFLT_TAG_PHASE1_CONFIG, "Transforms",
544419caefeSho 	    CONF_DFLT_PHASE1_TRANSFORMS, 0, 1);
545419caefeSho 
546510d8b0cSniklas 	/* Main modes */
547d865f642Sho 	for (enc = 0; mm_enc[enc]; enc++)
548d865f642Sho 		for (hash = 0; mm_hash[hash]; hash++)
549d865f642Sho 			for (auth = 0; mm_auth[auth]; auth++)
550d865f642Sho 				for (group = 0; dhgroup_p[group]; group++)
551d865f642Sho 					conf_load_defaults_mm (tr, mm_enc[enc],
552d865f642Sho 					    mm_hash[hash], mm_auth[auth],
553d865f642Sho 					    dhgroup[group], mm_enc_p[enc],
554a008c50bShshoexer 					    mm_auth_p[auth], dhgroup_p[group],
555a008c50bShshoexer 					    mm_hash_p[hash]);
556510d8b0cSniklas 
557cc475db6Sniklas 	/* Setup a default Phase 1 entry */
558cc475db6Sniklas 	conf_set(tr, "Phase 1", "Default", "Default-phase-1", 0, 1);
559cc475db6Sniklas 	conf_set(tr, "Default-phase-1", "Phase", "1", 0, 1);
560cc475db6Sniklas 	conf_set(tr, "Default-phase-1", "Configuration",
561cc475db6Sniklas 	    "Default-phase-1-configuration", 0, 1);
562eee423ceSho 	dflt = conf_get_trans_str(tr, "General", "Default-phase-1-ID");
563cc475db6Sniklas 	if (dflt)
564cc475db6Sniklas 		conf_set(tr, "Default-phase-1", "ID", dflt, 0, 1);
565cc475db6Sniklas 
566510d8b0cSniklas 	/* Quick modes */
567d865f642Sho 	for (enc = 0; qm_enc[enc]; enc++)
568d865f642Sho 		for (proto = 0; proto < 2; proto++)
569d865f642Sho 			for (mode = 0; mode < 2; mode++)
570d865f642Sho 				for (pfs = 0; pfs < 2; pfs++)
571d865f642Sho 					for (hash = 0; qm_hash[hash]; hash++)
572d865f642Sho 						for (group = 0;
573d865f642Sho 						    dhgroup_p[group]; group++)
574d865f642Sho 							conf_load_defaults_qm(
575d865f642Sho 							    tr, qm_enc[enc],
576d865f642Sho 							    qm_hash[hash],
577d865f642Sho 							    dhgroup[group],
57880cd8be9Sderaadt 							    qm_enc_p[enc],
57980cd8be9Sderaadt 							    qm_hash_p[hash],
5806d2b9615Shshoexer 							    qm_ah_id[hash],
581d865f642Sho 							    dhgroup_p[group],
582d865f642Sho 							    proto, mode, pfs);
583510d8b0cSniklas }
584510d8b0cSniklas 
5852040585eSniklas void
5862040585eSniklas conf_init(void)
5872040585eSniklas {
588cde22268Sho 	unsigned int i;
5892040585eSniklas 
590f8f1e192Sniklas 	for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++)
591f8f1e192Sniklas 		LIST_INIT(&conf_bindings[i]);
592f8f1e192Sniklas 	TAILQ_INIT(&conf_trans_queue);
593f8f1e192Sniklas 	conf_reinit();
5942040585eSniklas }
5952040585eSniklas 
596f8f1e192Sniklas /* Open the config file and map it into our address space, then parse it.  */
597f8f1e192Sniklas void
598f8f1e192Sniklas conf_reinit(void)
599f8f1e192Sniklas {
600f8f1e192Sniklas 	struct conf_binding *cb = 0;
601cde22268Sho 	int	 fd, trans;
602cde22268Sho 	unsigned int i;
60352e9f6e6Sho 	size_t	 sz;
604f8f1e192Sniklas 	char	*new_conf_addr = 0;
605f8f1e192Sniklas 
6069276cc62Shshoexer 	fd = monitor_open(conf_path, O_RDONLY, 0);
6079276cc62Shshoexer 	if (fd == -1 || check_file_secrecy_fd(fd, conf_path, &sz) == -1) {
6089276cc62Shshoexer 		if (fd == -1 && errno != ENOENT)
6099276cc62Shshoexer 			log_error("conf_reinit: open(\"%s\", O_RDONLY, 0) "
6109276cc62Shshoexer 			    "failed", conf_path);
6119276cc62Shshoexer 		if (fd != -1)
6129276cc62Shshoexer 			close(fd);
613bda02003Sniklas 
6149276cc62Shshoexer 		trans = conf_begin();
6159276cc62Shshoexer 	} else {
616f8f1e192Sniklas 		new_conf_addr = malloc(sz);
617fb9475d6Sderaadt 		if (!new_conf_addr) {
618df915834Shshoexer 			log_error("conf_reinit: malloc (%lu) failed",
619df915834Shshoexer 			    (unsigned long)sz);
620f8f1e192Sniklas 			goto fail;
621f8f1e192Sniklas 		}
6222040585eSniklas 		/* XXX I assume short reads won't happen here.  */
623fb9475d6Sderaadt 		if (read(fd, new_conf_addr, sz) != (int)sz) {
6247eb3b581Sderaadt 			log_error("conf_reinit: read (%d, %p, %lu) failed",
6257eb3b581Sderaadt 			    fd, new_conf_addr, (unsigned long)sz);
626f8f1e192Sniklas 			goto fail;
627f8f1e192Sniklas 		}
628ea1948caSho 		close(fd);
6292040585eSniklas 
630f8f1e192Sniklas 		trans = conf_begin();
631f8f1e192Sniklas 
632f8f1e192Sniklas 		/* XXX Should we not care about errors and rollback?  */
633f8f1e192Sniklas 		conf_parse(trans, new_conf_addr, sz);
6342872008fShshoexer 	}
635f8f1e192Sniklas 
636510d8b0cSniklas 	/* Load default configuration values.  */
637510d8b0cSniklas 	conf_load_defaults(trans);
638510d8b0cSniklas 
639f8f1e192Sniklas 	/* Free potential existing configuration.  */
640fb9475d6Sderaadt 	if (conf_addr) {
641df915834Shshoexer 		for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0];
642df915834Shshoexer 		    i++)
643f8f1e192Sniklas 			for (cb = LIST_FIRST(&conf_bindings[i]); cb;
644f8f1e192Sniklas 			    cb = LIST_FIRST(&conf_bindings[i]))
645f8f1e192Sniklas 				conf_remove_now(cb->section, cb->tag);
646f8f1e192Sniklas 		free(conf_addr);
647f8f1e192Sniklas 	}
648f8f1e192Sniklas 	conf_end(trans, 1);
649f8f1e192Sniklas 	conf_addr = new_conf_addr;
650f8f1e192Sniklas 	return;
651f8f1e192Sniklas 
652f8f1e192Sniklas fail:
653f8f1e192Sniklas 	free(new_conf_addr);
654f8f1e192Sniklas 	close(fd);
6552040585eSniklas }
6562040585eSniklas 
657a2d30fd1Sniklas /*
658a2d30fd1Sniklas  * Return the numeric value denoted by TAG in section SECTION or DEF
659a2d30fd1Sniklas  * if that tag does not exist.
660a2d30fd1Sniklas  */
6612040585eSniklas int
662a2d30fd1Sniklas conf_get_num(char *section, char *tag, int def)
6632040585eSniklas {
6642040585eSniklas 	char	*value = conf_get_str(section, tag);
6652040585eSniklas 
6662040585eSniklas 	if (value)
6672040585eSniklas 		return atoi(value);
668a2d30fd1Sniklas 	return def;
6692040585eSniklas }
6702040585eSniklas 
67181c21331Sniklas /*
67281c21331Sniklas  * Return the socket endpoint address denoted by TAG in SECTION as a
67381c21331Sniklas  * struct sockaddr.  It is the callers responsibility to deallocate
67481c21331Sniklas  * this structure when it is finished with it.
67581c21331Sniklas  */
67681c21331Sniklas struct sockaddr *
67781c21331Sniklas conf_get_address(char *section, char *tag)
67881c21331Sniklas {
67981c21331Sniklas 	char	*value = conf_get_str(section, tag);
68081c21331Sniklas 	struct sockaddr *sa;
68181c21331Sniklas 
68281c21331Sniklas 	if (!value)
68381c21331Sniklas 		return 0;
684e3283cbfSmcbride 	if (text2sockaddr(value, 0, &sa, 0, 0) == -1)
68581c21331Sniklas 		return 0;
68681c21331Sniklas 	return sa;
68781c21331Sniklas }
68881c21331Sniklas 
68982d8fe06Sniklas /* Validate X according to the range denoted by TAG in section SECTION.  */
69082d8fe06Sniklas int
69182d8fe06Sniklas conf_match_num(char *section, char *tag, int x)
69282d8fe06Sniklas {
69382d8fe06Sniklas 	char	*value = conf_get_str(section, tag);
69482d8fe06Sniklas 	int	 val, min, max, n;
69582d8fe06Sniklas 
69682d8fe06Sniklas 	if (!value)
69782d8fe06Sniklas 		return 0;
69882d8fe06Sniklas 	n = sscanf(value, "%d,%d:%d", &val, &min, &max);
699fb9475d6Sderaadt 	switch (n) {
70082d8fe06Sniklas 	case 1:
70155665484Sho 		LOG_DBG((LOG_MISC, 95, "conf_match_num: %s:%s %d==%d?",
702df915834Shshoexer 		    section, tag, val, x));
70382d8fe06Sniklas 		return x == val;
70482d8fe06Sniklas 	case 3:
70555665484Sho 		LOG_DBG((LOG_MISC, 95, "conf_match_num: %s:%s %d<=%d<=%d?",
706df915834Shshoexer 		    section, tag, min, x, max));
70782d8fe06Sniklas 		return min <= x && max >= x;
70882d8fe06Sniklas 	default:
709df915834Shshoexer 		log_error("conf_match_num: section %s tag %s: invalid number "
710df915834Shshoexer 		    "spec %s", section, tag, value);
71182d8fe06Sniklas 	}
71282d8fe06Sniklas 	return 0;
71382d8fe06Sniklas }
71482d8fe06Sniklas 
7152040585eSniklas /* Return the string value denoted by TAG in section SECTION.  */
7162040585eSniklas char *
7172040585eSniklas conf_get_str(char *section, char *tag)
7182040585eSniklas {
7192040585eSniklas 	struct conf_binding *cb;
7202040585eSniklas 
721f8f1e192Sniklas 	for (cb = LIST_FIRST(&conf_bindings[conf_hash(section)]); cb;
722f8f1e192Sniklas 	    cb = LIST_NEXT(cb, link))
723df915834Shshoexer 		if (strcasecmp(section, cb->section) == 0 &&
724df915834Shshoexer 		    strcasecmp(tag, cb->tag) == 0) {
725df915834Shshoexer 			LOG_DBG((LOG_MISC, 95, "conf_get_str: [%s]:%s->%s",
726df915834Shshoexer 			    section, tag, cb->value));
7272040585eSniklas 			return cb->value;
7282040585eSniklas 		}
729395a452cSho 	LOG_DBG((LOG_MISC, 95,
730f8f1e192Sniklas 	    "conf_get_str: configuration value not found [%s]:%s", section,
73151ca15aeSniklas 	    tag));
7322040585eSniklas 	return 0;
7332040585eSniklas }
7342040585eSniklas 
735a9753648Sniklas /*
736a9753648Sniklas  * Build a list of string values out of the comma separated value denoted by
737a9753648Sniklas  * TAG in SECTION.
738a9753648Sniklas  */
7392040585eSniklas struct conf_list *
7402040585eSniklas conf_get_list(char *section, char *tag)
7412040585eSniklas {
742cde22268Sho 	char	*liststr = 0, *p, *field, *t;
7432040585eSniklas 	struct conf_list *list = 0;
74478ef4cbaScloder 	struct conf_list_node *node = 0;
7452040585eSniklas 
7462040585eSniklas 	list = malloc(sizeof *list);
7472040585eSniklas 	if (!list)
7482040585eSniklas 		goto cleanup;
7492040585eSniklas 	TAILQ_INIT(&list->fields);
7502040585eSniklas 	list->cnt = 0;
7512040585eSniklas 	liststr = conf_get_str(section, tag);
7522040585eSniklas 	if (!liststr)
7532040585eSniklas 		goto cleanup;
7542040585eSniklas 	liststr = strdup(liststr);
7552040585eSniklas 	if (!liststr)
7562040585eSniklas 		goto cleanup;
7572040585eSniklas 	p = liststr;
758fb9475d6Sderaadt 	while ((field = strsep(&p, ",")) != NULL) {
759cde22268Sho 		/* Skip leading whitespace */
760cde22268Sho 		while (isspace(*field))
761cde22268Sho 			field++;
762cde22268Sho 		/* Skip trailing whitespace */
763cde22268Sho 		if (p)
764cde22268Sho 			for (t = p - 1; t > field && isspace(*t); t--)
765cde22268Sho 				*t = '\0';
766fb9475d6Sderaadt 		if (*field == '\0') {
7672040585eSniklas 			log_print("conf_get_list: empty field, ignoring...");
7682040585eSniklas 			continue;
7692040585eSniklas 		}
7702040585eSniklas 		list->cnt++;
771a9753648Sniklas 		node = calloc(1, sizeof *node);
7722040585eSniklas 		if (!node)
7732040585eSniklas 			goto cleanup;
774a9753648Sniklas 		node->field = strdup(field);
775a9753648Sniklas 		if (!node->field)
776a9753648Sniklas 			goto cleanup;
7772040585eSniklas 		TAILQ_INSERT_TAIL(&list->fields, node, link);
7782040585eSniklas 	}
779a9753648Sniklas 	free(liststr);
7802040585eSniklas 	return list;
7812040585eSniklas 
7822040585eSniklas cleanup:
78378ef4cbaScloder 	free(node);
7842040585eSniklas 	if (list)
7852040585eSniklas 		conf_free_list(list);
7862040585eSniklas 	free(liststr);
7872040585eSniklas 	return 0;
7882040585eSniklas }
7892040585eSniklas 
79082d8fe06Sniklas struct conf_list *
79182d8fe06Sniklas conf_get_tag_list(char *section)
79282d8fe06Sniklas {
79382d8fe06Sniklas 	struct conf_list *list = 0;
79478ef4cbaScloder 	struct conf_list_node *node = 0;
79582d8fe06Sniklas 	struct conf_binding *cb;
79682d8fe06Sniklas 
79782d8fe06Sniklas 	list = malloc(sizeof *list);
79882d8fe06Sniklas 	if (!list)
79982d8fe06Sniklas 		goto cleanup;
80082d8fe06Sniklas 	TAILQ_INIT(&list->fields);
80182d8fe06Sniklas 	list->cnt = 0;
802f8f1e192Sniklas 	for (cb = LIST_FIRST(&conf_bindings[conf_hash(section)]); cb;
803f8f1e192Sniklas 	    cb = LIST_NEXT(cb, link))
804fb9475d6Sderaadt 		if (strcasecmp(section, cb->section) == 0) {
80582d8fe06Sniklas 			list->cnt++;
806a9753648Sniklas 			node = calloc(1, sizeof *node);
80782d8fe06Sniklas 			if (!node)
80882d8fe06Sniklas 				goto cleanup;
809a9753648Sniklas 			node->field = strdup(cb->tag);
810a9753648Sniklas 			if (!node->field)
811a9753648Sniklas 				goto cleanup;
81282d8fe06Sniklas 			TAILQ_INSERT_TAIL(&list->fields, node, link);
81382d8fe06Sniklas 		}
81482d8fe06Sniklas 	return list;
81582d8fe06Sniklas 
81682d8fe06Sniklas cleanup:
81778ef4cbaScloder 	free(node);
81882d8fe06Sniklas 	if (list)
81982d8fe06Sniklas 		conf_free_list(list);
82082d8fe06Sniklas 	return 0;
82182d8fe06Sniklas }
82282d8fe06Sniklas 
8232040585eSniklas void
8242040585eSniklas conf_free_list(struct conf_list *list)
8252040585eSniklas {
826a9753648Sniklas 	struct conf_list_node *node = TAILQ_FIRST(&list->fields);
827a9753648Sniklas 
828fb9475d6Sderaadt 	while (node) {
829a9753648Sniklas 		TAILQ_REMOVE(&list->fields, node, link);
830a9753648Sniklas 		free(node->field);
831a9753648Sniklas 		free(node);
832a9753648Sniklas 		node = TAILQ_FIRST(&list->fields);
833a9753648Sniklas 	}
8342040585eSniklas 	free(list);
8352040585eSniklas }
836f8f1e192Sniklas 
837f8f1e192Sniklas int
838f8f1e192Sniklas conf_begin(void)
839f8f1e192Sniklas {
840f8f1e192Sniklas 	static int	seq = 0;
841f8f1e192Sniklas 
842f8f1e192Sniklas 	return ++seq;
843f8f1e192Sniklas }
844f8f1e192Sniklas 
8456c5cd17eSmoritz static int
8466c5cd17eSmoritz conf_trans_node(int transaction, enum conf_op op, char *section, char *tag,
8476c5cd17eSmoritz     char *value, int override, int is_default)
848f8f1e192Sniklas {
849f8f1e192Sniklas 	struct conf_trans *node;
850f8f1e192Sniklas 
851f8f1e192Sniklas 	node = calloc(1, sizeof *node);
852fb9475d6Sderaadt 	if (!node) {
8537eb3b581Sderaadt 		log_error("conf_trans_node: calloc (1, %lu) failed",
8547eb3b581Sderaadt 		    (unsigned long)sizeof *node);
8556c5cd17eSmoritz 		return 1;
856f8f1e192Sniklas 	}
857f8f1e192Sniklas 	node->trans = transaction;
858f8f1e192Sniklas 	node->op = op;
8596c5cd17eSmoritz 	node->override = override;
8606c5cd17eSmoritz 	node->is_default = is_default;
8616c5cd17eSmoritz 	if (section && (node->section = strdup(section)) == NULL)
8626c5cd17eSmoritz 		goto fail;
8636c5cd17eSmoritz 	if (tag && (node->tag = strdup(tag)) == NULL)
8646c5cd17eSmoritz 		goto fail;
8656c5cd17eSmoritz 	if (value && (node->value = strdup(value)) == NULL)
8666c5cd17eSmoritz 		goto fail;
867f8f1e192Sniklas 	TAILQ_INSERT_TAIL(&conf_trans_queue, node, link);
8686c5cd17eSmoritz 	return 0;
8696c5cd17eSmoritz 
8706c5cd17eSmoritz fail:
8716c5cd17eSmoritz 	free(node->section);
8726c5cd17eSmoritz 	free(node->tag);
8736c5cd17eSmoritz 	free(node->value);
8746c5cd17eSmoritz 	free(node);
8756c5cd17eSmoritz 	return 1;
876f8f1e192Sniklas }
877f8f1e192Sniklas 
878f8f1e192Sniklas /* Queue a set operation.  */
879f8f1e192Sniklas int
880510d8b0cSniklas conf_set(int transaction, char *section, char *tag, char *value, int override,
881510d8b0cSniklas     int is_default)
882f8f1e192Sniklas {
8836c5cd17eSmoritz 	return conf_trans_node(transaction, CONF_SET, section, tag, value,
8846c5cd17eSmoritz 	    override, is_default);
885f8f1e192Sniklas }
886f8f1e192Sniklas 
887f8f1e192Sniklas /* Queue a remove operation.  */
888f8f1e192Sniklas int
889f8f1e192Sniklas conf_remove(int transaction, char *section, char *tag)
890f8f1e192Sniklas {
8916c5cd17eSmoritz 	return conf_trans_node(transaction, CONF_REMOVE, section, tag, NULL,
8926c5cd17eSmoritz 	    0, 0);
893f8f1e192Sniklas }
894f8f1e192Sniklas 
895f8f1e192Sniklas /* Queue a remove section operation.  */
896f8f1e192Sniklas int
897f8f1e192Sniklas conf_remove_section(int transaction, char *section)
898f8f1e192Sniklas {
8996c5cd17eSmoritz 	return conf_trans_node(transaction, CONF_REMOVE_SECTION, section, NULL,
9006c5cd17eSmoritz 	    NULL, 0, 0);
901f8f1e192Sniklas }
902f8f1e192Sniklas 
903f8f1e192Sniklas /* Execute all queued operations for this transaction.  Cleanup.  */
904f8f1e192Sniklas int
905f8f1e192Sniklas conf_end(int transaction, int commit)
906f8f1e192Sniklas {
907f8f1e192Sniklas 	struct conf_trans *node, *next;
908f8f1e192Sniklas 
909fb9475d6Sderaadt 	for (node = TAILQ_FIRST(&conf_trans_queue); node; node = next) {
910f8f1e192Sniklas 		next = TAILQ_NEXT(node, link);
911fb9475d6Sderaadt 		if (node->trans == transaction) {
912f8f1e192Sniklas 			if (commit)
913fb9475d6Sderaadt 				switch (node->op) {
914f8f1e192Sniklas 				case CONF_SET:
91599cdfc90Sderaadt 					conf_set_now(node->section, node->tag,
91699cdfc90Sderaadt 					    node->value, node->override,
91799cdfc90Sderaadt 					    node->is_default);
918f8f1e192Sniklas 					break;
919f8f1e192Sniklas 				case CONF_REMOVE:
920df915834Shshoexer 					conf_remove_now(node->section,
921df915834Shshoexer 					    node->tag);
922f8f1e192Sniklas 					break;
923f8f1e192Sniklas 				case CONF_REMOVE_SECTION:
924f8f1e192Sniklas 					conf_remove_section_now(node->section);
925f8f1e192Sniklas 					break;
926f8f1e192Sniklas 				default:
927df915834Shshoexer 					log_print("conf_end: unknown "
928df915834Shshoexer 					    "operation: %d", node->op);
929f8f1e192Sniklas 				}
930f8f1e192Sniklas 			TAILQ_REMOVE(&conf_trans_queue, node, link);
931074d67afSniklas 			free(node->section);
932074d67afSniklas 			free(node->tag);
933074d67afSniklas 			free(node->value);
934f8f1e192Sniklas 			free(node);
935f8f1e192Sniklas 		}
936f8f1e192Sniklas 	}
937f8f1e192Sniklas 	return 0;
938f8f1e192Sniklas }
939510d8b0cSniklas 
94094de5165Sniklas /*
94194de5165Sniklas  * Dump running configuration upon SIGUSR1.
942395a452cSho  * Configuration is "stored in reverse order", so reverse it again.
94394de5165Sniklas  */
944510d8b0cSniklas struct dumper {
945510d8b0cSniklas 	char	*s, *v;
946510d8b0cSniklas 	struct dumper *next;
947510d8b0cSniklas };
948510d8b0cSniklas 
949510d8b0cSniklas static void
950510d8b0cSniklas conf_report_dump(struct dumper *node)
951510d8b0cSniklas {
952510d8b0cSniklas 	/* Recursive, cleanup when we're done.  */
953510d8b0cSniklas 
954510d8b0cSniklas 	if (node->next)
955510d8b0cSniklas 		conf_report_dump(node->next);
956510d8b0cSniklas 
957510d8b0cSniklas 	if (node->v)
958510d8b0cSniklas 		LOG_DBG((LOG_REPORT, 0, "%s=\t%s", node->s, node->v));
959fb9475d6Sderaadt 	else if (node->s) {
960510d8b0cSniklas 		LOG_DBG((LOG_REPORT, 0, "%s", node->s));
961510d8b0cSniklas 		if (strlen(node->s) > 0)
962510d8b0cSniklas 			free(node->s);
963510d8b0cSniklas 	}
964510d8b0cSniklas 	free(node);
965510d8b0cSniklas }
966510d8b0cSniklas 
967510d8b0cSniklas void
968510d8b0cSniklas conf_report(void)
969510d8b0cSniklas {
9700eb823c5Sniklas 	struct conf_binding *cb, *last = 0;
971e9cbd6b9Sderaadt 	unsigned int	i;
972510d8b0cSniklas 	char           *current_section = (char *)0;
973510d8b0cSniklas 	struct dumper  *dumper, *dnode;
974510d8b0cSniklas 
975592a196eSniklas 	dumper = dnode = (struct dumper *)calloc(1, sizeof *dumper);
976510d8b0cSniklas 	if (!dumper)
977510d8b0cSniklas 		goto mem_fail;
978510d8b0cSniklas 
979510d8b0cSniklas 	LOG_DBG((LOG_REPORT, 0, "conf_report: dumping running configuration"));
980510d8b0cSniklas 
981510d8b0cSniklas 	for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++)
982510d8b0cSniklas 		for (cb = LIST_FIRST(&conf_bindings[i]); cb;
983fb9475d6Sderaadt 		    cb = LIST_NEXT(cb, link)) {
984fb9475d6Sderaadt 			if (!cb->is_default) {
9850eb823c5Sniklas 				/* Dump this entry.  */
986df915834Shshoexer 				if (!current_section || strcmp(cb->section,
987df915834Shshoexer 				    current_section)) {
988fb9475d6Sderaadt 					if (current_section) {
989e9cbd6b9Sderaadt 						if (asprintf(&dnode->s, "[%s]",
990e9cbd6b9Sderaadt 						    current_section) == -1)
991510d8b0cSniklas 							goto mem_fail;
99299cdfc90Sderaadt 						dnode->next = (struct dumper *)
99350eea14cSho 						    calloc(1,
99450eea14cSho 							sizeof(struct dumper));
995510d8b0cSniklas 						dnode = dnode->next;
996510d8b0cSniklas 						if (!dnode)
997510d8b0cSniklas 							goto mem_fail;
998510d8b0cSniklas 
999510d8b0cSniklas 						dnode->s = "";
100099cdfc90Sderaadt 						dnode->next = (struct dumper *)
100150eea14cSho 						    calloc(1,
100250eea14cSho 							sizeof(struct dumper));
1003510d8b0cSniklas 						dnode = dnode->next;
1004510d8b0cSniklas 						if (!dnode)
1005510d8b0cSniklas 							goto mem_fail;
1006510d8b0cSniklas 					}
1007510d8b0cSniklas 					current_section = cb->section;
1008510d8b0cSniklas 				}
1009510d8b0cSniklas 				dnode->s = cb->tag;
1010510d8b0cSniklas 				dnode->v = cb->value;
101199cdfc90Sderaadt 				dnode->next = (struct dumper *)
101299cdfc90Sderaadt 				    calloc(1, sizeof(struct dumper));
1013510d8b0cSniklas 				dnode = dnode->next;
1014510d8b0cSniklas 				if (!dnode)
1015510d8b0cSniklas 					goto mem_fail;
1016510d8b0cSniklas 				last = cb;
1017510d8b0cSniklas 			}
1018510d8b0cSniklas 		}
1019510d8b0cSniklas 
1020e9cbd6b9Sderaadt 	if (last)
1021e9cbd6b9Sderaadt 		if (asprintf(&dnode->s, "[%s]", last->section) == -1)
1022510d8b0cSniklas 			goto mem_fail;
1023510d8b0cSniklas 	conf_report_dump(dumper);
1024510d8b0cSniklas 
1025510d8b0cSniklas 	return;
1026510d8b0cSniklas 
1027510d8b0cSniklas mem_fail:
10280eb823c5Sniklas 	log_error("conf_report: malloc/calloc failed");
1029fb9475d6Sderaadt 	while ((dnode = dumper) != 0) {
1030510d8b0cSniklas 		dumper = dumper->next;
1031510d8b0cSniklas 		free(dnode->s);
1032510d8b0cSniklas 		free(dnode);
1033510d8b0cSniklas 	}
1034510d8b0cSniklas }
1035