xref: /openbsd/sbin/isakmpd/conf.c (revision 26c588cc)
1*26c588ccSmpi /* $OpenBSD: conf.c,v 1.107 2017/10/27 08:29:32 mpi 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 
3382bad92dSderaadt #include <sys/types.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;
LIST_HEAD(conf_bindings,conf_binding)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) {
95025f5691Sderaadt 		hash = ((hash << 1) | (hash >> 7)) ^ tolower((unsigned char)*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
conf_remove_now(char * section,char * tag)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
conf_remove_section_now(char * section)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
conf_set_now(char * section,char * tag,char * value,int override,int is_default)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
conf_parse_line(int trans,char * line,int ln,size_t sz)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 &&
245025f5691Sderaadt 			    isspace((unsigned char)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
conf_parse(int trans,char * buf,size_t sz)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:
2910e800071Snaddy  *     {BLF,3DES,CAST,AES,AES-{128,192,256}-{MD5,SHA,SHA2-{256,384,512}} \
292*26c588ccSmpi  *         [-GRP{1,2,5,14-21,25-30}][-{DSS,RSA_SIG}]
293510d8b0cSniklas  *  For quick mode:
294e33f6eeeSho  *     QM-{proto}[-TRP]-{cipher}[-{hash}][-PFS[-{group}]]-SUITE
295e33f6eeeSho  *     where
296e33f6eeeSho  *       {proto}  = ESP, AH
2970e800071Snaddy  *       {cipher} = 3DES, CAST, BLF, AES, AES-{128,192,256}, AESCTR
298a008c50bShshoexer  *       {hash}   = MD5, SHA, RIPEMD, SHA2-{256,384,512}
299*26c588ccSmpi  *       {group}  = GRP{1,2,5,14-21,25-30}
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 *
conf_get_trans_str(int trans,char * section,char * tag)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
conf_load_defaults_mm(int tr,char * mme,char * mmh,char * mma,char * dhg,char * mme_p,char * mma_p,char * dhg_p,char * mmh_p)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
conf_load_defaults_qm(int tr,char * qme,char * qmh,char * dhg,char * qme_p,char * qmh_p,char * qm_ah_id,char * dhg_p,int proto,int mode,int pfs)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 
4057ebc7616Smikeb 	/* For GCM no additional authentication must be specified */
4067ebc7616Smikeb 	if (proto == 0 && strcmp(qmh, "NONE") != 0 &&
4077ebc7616Smikeb 	    (strcmp(qme, "AES_GCM_16") == 0 || strcmp(qme, "AES_GMAC") == 0))
4087ebc7616Smikeb 		return;
4097ebc7616Smikeb 
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);
4367ebc7616Smikeb 	else if (strcmp(qme_p, "-AES-128") == 0 ||
4375f649d51Snaddy 	    strcmp(qme_p, "-AESCTR-128") == 0 ||
4387ebc7616Smikeb 	    strcmp(qme_p, "-AESGCM-128") == 0 ||
4397ebc7616Smikeb 	    strcmp(qme_p, "-AESGMAC-128") == 0)
440200a7bcfSderaadt 		conf_set(tr, sect, "KEY_LENGTH", "128,128:128", 0, 1);
4417ebc7616Smikeb 	else if (strcmp(qme_p, "-AES-192") == 0 ||
4425f649d51Snaddy 	    strcmp(qme_p, "-AESCTR-192") == 0 ||
4437ebc7616Smikeb 	    strcmp(qme_p, "-AESGCM-192") == 0 ||
4447ebc7616Smikeb 	    strcmp(qme_p, "-AESGMAC-192") == 0)
445601f7947Shshoexer 		conf_set(tr, sect, "KEY_LENGTH", "192,192:192", 0, 1);
4467ebc7616Smikeb 	else if (strcmp(qme_p, "-AES-256") == 0 ||
4475f649d51Snaddy 	    strcmp(qme_p, "-AESCTR-256") == 0 ||
4487ebc7616Smikeb 	    strcmp(qme_p, "-AESGCM-256") == 0 ||
4497ebc7616Smikeb 	    strcmp(qme_p, "-AESGMAC-256") == 0)
450601f7947Shshoexer 		conf_set(tr, sect, "KEY_LENGTH", "256,256:256", 0, 1);
451d865f642Sho 	else if	(strcmp(qme, "AES") == 0)
452d865f642Sho 		conf_set(tr, sect, "KEY_LENGTH", CONF_DFLT_VAL_AES_KEYLEN, 0,
453d865f642Sho 			 1);
454601f7947Shshoexer 
455d865f642Sho 	conf_set(tr, sect, "ENCAPSULATION_MODE", MODE(mode), 0, 1);
456d865f642Sho 	if (strcmp(qmh, "NONE")) {
457d865f642Sho 		conf_set(tr, sect, "AUTHENTICATION_ALGORITHM", qmh, 0, 1);
458d865f642Sho 
459d865f642Sho 		/* XXX Another shortcut to keep length down */
460d865f642Sho 		if (pfs)
461d865f642Sho 			conf_set(tr, sect, "GROUP_DESCRIPTION", dhg, 0, 1);
462d865f642Sho 	}
463d865f642Sho 
464d865f642Sho 	/* XXX Lifetimes depending on enc/auth strength? */
465d865f642Sho 	conf_set(tr, sect, "Life", CONF_DFLT_TAG_LIFE_QUICK_MODE, 0, 1);
466d865f642Sho }
467d865f642Sho 
468d865f642Sho static void
conf_load_defaults(int tr)469d865f642Sho conf_load_defaults(int tr)
470d865f642Sho {
471d865f642Sho 	int	 enc, auth, hash, group, proto, mode, pfs;
472d865f642Sho 	char	*dflt;
473d865f642Sho 
474d865f642Sho 	char	*mm_auth[] = {"PRE_SHARED", "DSS", "RSA_SIG", 0};
475d865f642Sho 	char	*mm_auth_p[] = {"", "-DSS", "-RSA_SIG", 0};
476a008c50bShshoexer 	char	*mm_hash[] = {"MD5", "SHA", "SHA2_256", "SHA2_384", "SHA2_512",
477a008c50bShshoexer 		     0};
478a008c50bShshoexer 	char	*mm_hash_p[] = {"-MD5", "-SHA", "-SHA2-256", "-SHA2-384",
479a008c50bShshoexer 		    "-SHA2-512", "", 0 };
4800e800071Snaddy 	char	*mm_enc[] = {"BLOWFISH_CBC", "3DES_CBC", "CAST_CBC",
481601f7947Shshoexer 		    "AES_CBC", "AES_CBC", "AES_CBC", "AES_CBC", 0};
4820e800071Snaddy 	char	*mm_enc_p[] = {"BLF", "3DES", "CAST", "AES", "AES-128",
483601f7947Shshoexer 		    "AES-192", "AES-256", 0};
484d865f642Sho 	char	*dhgroup[] = {"MODP_1024", "MODP_768", "MODP_1024",
4851c415274Smikeb 		    "MODP_1536", "MODP_2048", "MODP_3072", "MODP_4096",
486*26c588ccSmpi 		    "MODP_6144", "MODP_8192",
487*26c588ccSmpi 		    "ECP_256", "ECP_384", "ECP_521", "ECP_192", "ECP_224",
488*26c588ccSmpi 		    "BP_224", "BP_256", "BP_384", "BP_512", 0};
48906ee6934Shshoexer 	char	*dhgroup_p[] = {"", "-GRP1", "-GRP2", "-GRP5", "-GRP14",
490*26c588ccSmpi 		    "-GRP15", "-GRP16", "-GRP17", "-GRP18", "-GRP19", "-GRP20",
491*26c588ccSmpi 		    "-GRP21", "-GRP25", "-GRP26", "-GRP27", "-GRP28", "-GRP29",
492*26c588ccSmpi 		    "-GRP30", 0};
4930e800071Snaddy 	char	*qm_enc[] = {"3DES", "CAST", "BLOWFISH", "AES",
4945f649d51Snaddy 		    "AES", "AES", "AES", "AES_CTR", "AES_CTR", "AES_CTR",
4955f649d51Snaddy 		    "AES_CTR", "AES_GCM_16",
4967ebc7616Smikeb 		    "AES_GCM_16", "AES_GCM_16", "AES_GMAC", "AES_GMAC",
4977ebc7616Smikeb 		    "AES_GMAC", "NULL", "NONE", 0};
4980e800071Snaddy 	char	*qm_enc_p[] = {"-3DES", "-CAST", "-BLF", "-AES",
4997ebc7616Smikeb 		    "-AES-128", "-AES-192", "-AES-256", "-AESCTR",
5005f649d51Snaddy 		    "-AESCTR-128", "-AESCTR-192", "-AESCTR-256",
5017ebc7616Smikeb 		    "-AESGCM-128", "-AESGCM-192", "-AESGCM-256",
5027ebc7616Smikeb 		    "-AESGMAC-128", "-AESGMAC-192", "-AESGMAC-256", "-NULL",
503601f7947Shshoexer 		    "", 0};
504d865f642Sho 	char	*qm_hash[] = {"HMAC_MD5", "HMAC_SHA", "HMAC_RIPEMD",
505df915834Shshoexer 		    "HMAC_SHA2_256", "HMAC_SHA2_384", "HMAC_SHA2_512", "NONE",
506df915834Shshoexer 		    0};
507d865f642Sho 	char	*qm_hash_p[] = {"-MD5", "-SHA", "-RIPEMD", "-SHA2-256",
508d865f642Sho 		    "-SHA2-384", "-SHA2-512", "", 0};
5096d2b9615Shshoexer 	char	*qm_ah_id[] = {"MD5", "SHA", "RIPEMD", "SHA2_256", "SHA2_384",
5106d2b9615Shshoexer 		    "SHA2_512", "", 0};
511510d8b0cSniklas 
512510d8b0cSniklas 	/* General and X509 defaults */
513510d8b0cSniklas 	conf_set(tr, "General", "Retransmits", CONF_DFLT_RETRANSMITS, 0, 1);
514d865f642Sho 	conf_set(tr, "General", "Exchange-max-time", CONF_DFLT_EXCH_MAX_TIME,
515d865f642Sho 	    0, 1);
516b6e0b5cbShshoexer 	conf_set(tr, "General", "Use-Keynote", CONF_DFLT_USE_KEYNOTE, 0, 1);
517510d8b0cSniklas 	conf_set(tr, "General", "Policy-file", CONF_DFLT_POLICY_FILE, 0, 1);
518d865f642Sho 	conf_set(tr, "General", "Pubkey-directory", CONF_DFLT_PUBKEY_DIR, 0,
519d865f642Sho 	    1);
520510d8b0cSniklas 
521d865f642Sho 	conf_set(tr, "X509-certificates", "CA-directory",
522d865f642Sho 	    CONF_DFLT_X509_CA_DIR, 0, 1);
523d865f642Sho 	conf_set(tr, "X509-certificates", "Cert-directory",
524d865f642Sho 	    CONF_DFLT_X509_CERT_DIR, 0, 1);
525d865f642Sho 	conf_set(tr, "X509-certificates", "Private-key",
526d865f642Sho 	    CONF_DFLT_X509_PRIVATE_KEY, 0, 1);
527db26b2b9Smsf 	conf_set(tr, "X509-certificates", "Private-key-directory",
528db26b2b9Smsf 	    CONF_DFLT_X509_PRIVATE_KEY_DIR, 0, 1);
529d865f642Sho 	conf_set(tr, "X509-certificates", "CRL-directory",
530d865f642Sho 	    CONF_DFLT_X509_CRL_DIR, 0, 1);
531510d8b0cSniklas 
532df915834Shshoexer 	conf_set(tr, "KeyNote", "Credential-directory",
533df915834Shshoexer 	    CONF_DFLT_KEYNOTE_CRED_DIR, 0, 1);
53413e19299Sniklas 
535428bd1b5Shshoexer 	conf_set(tr, "General", "Delete-SAs", CONF_DFLT_DELETE_SAS, 0, 1);
536428bd1b5Shshoexer 
53728d27e6cSangelos 	/* Lifetimes. XXX p1/p2 vs main/quick mode may be unclear.  */
538eee423ceSho 	dflt = conf_get_trans_str(tr, "General", "Default-phase-1-lifetime");
53928d27e6cSangelos 	conf_set(tr, CONF_DFLT_TAG_LIFE_MAIN_MODE, "LIFE_TYPE",
54028d27e6cSangelos 	    CONF_DFLT_TYPE_LIFE_MAIN_MODE, 0, 1);
54128d27e6cSangelos 	conf_set(tr, CONF_DFLT_TAG_LIFE_MAIN_MODE, "LIFE_DURATION",
54228d27e6cSangelos 	    (dflt ? dflt : CONF_DFLT_VAL_LIFE_MAIN_MODE), 0, 1);
54328d27e6cSangelos 
544eee423ceSho 	dflt = conf_get_trans_str(tr, "General", "Default-phase-2-lifetime");
54528d27e6cSangelos 	conf_set(tr, CONF_DFLT_TAG_LIFE_QUICK_MODE, "LIFE_TYPE",
54628d27e6cSangelos 	    CONF_DFLT_TYPE_LIFE_QUICK_MODE, 0, 1);
54728d27e6cSangelos 	conf_set(tr, CONF_DFLT_TAG_LIFE_QUICK_MODE, "LIFE_DURATION",
54828d27e6cSangelos 	    (dflt ? dflt : CONF_DFLT_VAL_LIFE_QUICK_MODE), 0, 1);
54928d27e6cSangelos 
550419caefeSho 	/* Default Phase-1 Configuration section */
551419caefeSho 	conf_set(tr, CONF_DFLT_TAG_PHASE1_CONFIG, "EXCHANGE_TYPE",
552419caefeSho 	    CONF_DFLT_PHASE1_EXCH_TYPE, 0, 1);
553419caefeSho 	conf_set(tr, CONF_DFLT_TAG_PHASE1_CONFIG, "Transforms",
554419caefeSho 	    CONF_DFLT_PHASE1_TRANSFORMS, 0, 1);
555419caefeSho 
556510d8b0cSniklas 	/* Main modes */
557d865f642Sho 	for (enc = 0; mm_enc[enc]; enc++)
558d865f642Sho 		for (hash = 0; mm_hash[hash]; hash++)
559d865f642Sho 			for (auth = 0; mm_auth[auth]; auth++)
560d865f642Sho 				for (group = 0; dhgroup_p[group]; group++)
561d865f642Sho 					conf_load_defaults_mm (tr, mm_enc[enc],
562d865f642Sho 					    mm_hash[hash], mm_auth[auth],
563d865f642Sho 					    dhgroup[group], mm_enc_p[enc],
564a008c50bShshoexer 					    mm_auth_p[auth], dhgroup_p[group],
565a008c50bShshoexer 					    mm_hash_p[hash]);
566510d8b0cSniklas 
567cc475db6Sniklas 	/* Setup a default Phase 1 entry */
568cc475db6Sniklas 	conf_set(tr, "Phase 1", "Default", "Default-phase-1", 0, 1);
569cc475db6Sniklas 	conf_set(tr, "Default-phase-1", "Phase", "1", 0, 1);
570cc475db6Sniklas 	conf_set(tr, "Default-phase-1", "Configuration",
571cc475db6Sniklas 	    "Default-phase-1-configuration", 0, 1);
572eee423ceSho 	dflt = conf_get_trans_str(tr, "General", "Default-phase-1-ID");
573cc475db6Sniklas 	if (dflt)
574cc475db6Sniklas 		conf_set(tr, "Default-phase-1", "ID", dflt, 0, 1);
575cc475db6Sniklas 
576510d8b0cSniklas 	/* Quick modes */
577d865f642Sho 	for (enc = 0; qm_enc[enc]; enc++)
578d865f642Sho 		for (proto = 0; proto < 2; proto++)
579d865f642Sho 			for (mode = 0; mode < 2; mode++)
580d865f642Sho 				for (pfs = 0; pfs < 2; pfs++)
581d865f642Sho 					for (hash = 0; qm_hash[hash]; hash++)
582d865f642Sho 						for (group = 0;
583d865f642Sho 						    dhgroup_p[group]; group++)
584d865f642Sho 							conf_load_defaults_qm(
585d865f642Sho 							    tr, qm_enc[enc],
586d865f642Sho 							    qm_hash[hash],
587d865f642Sho 							    dhgroup[group],
58880cd8be9Sderaadt 							    qm_enc_p[enc],
58980cd8be9Sderaadt 							    qm_hash_p[hash],
5906d2b9615Shshoexer 							    qm_ah_id[hash],
591d865f642Sho 							    dhgroup_p[group],
592d865f642Sho 							    proto, mode, pfs);
593510d8b0cSniklas }
594510d8b0cSniklas 
5952040585eSniklas void
conf_init(void)5962040585eSniklas conf_init(void)
5972040585eSniklas {
598cde22268Sho 	unsigned int i;
5992040585eSniklas 
600f8f1e192Sniklas 	for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++)
601f8f1e192Sniklas 		LIST_INIT(&conf_bindings[i]);
602f8f1e192Sniklas 	TAILQ_INIT(&conf_trans_queue);
603f8f1e192Sniklas 	conf_reinit();
6042040585eSniklas }
6052040585eSniklas 
606f8f1e192Sniklas /* Open the config file and map it into our address space, then parse it.  */
607f8f1e192Sniklas void
conf_reinit(void)608f8f1e192Sniklas conf_reinit(void)
609f8f1e192Sniklas {
610f8f1e192Sniklas 	struct conf_binding *cb = 0;
611cde22268Sho 	int	 fd, trans;
612cde22268Sho 	unsigned int i;
61352e9f6e6Sho 	size_t	 sz;
614f8f1e192Sniklas 	char	*new_conf_addr = 0;
615f8f1e192Sniklas 
6169276cc62Shshoexer 	fd = monitor_open(conf_path, O_RDONLY, 0);
6179276cc62Shshoexer 	if (fd == -1 || check_file_secrecy_fd(fd, conf_path, &sz) == -1) {
6189276cc62Shshoexer 		if (fd == -1 && errno != ENOENT)
6199276cc62Shshoexer 			log_error("conf_reinit: open(\"%s\", O_RDONLY, 0) "
6209276cc62Shshoexer 			    "failed", conf_path);
6219276cc62Shshoexer 		if (fd != -1)
6229276cc62Shshoexer 			close(fd);
623bda02003Sniklas 
6249276cc62Shshoexer 		trans = conf_begin();
6259276cc62Shshoexer 	} else {
626f8f1e192Sniklas 		new_conf_addr = malloc(sz);
627fb9475d6Sderaadt 		if (!new_conf_addr) {
628df915834Shshoexer 			log_error("conf_reinit: malloc (%lu) failed",
629df915834Shshoexer 			    (unsigned long)sz);
630f8f1e192Sniklas 			goto fail;
631f8f1e192Sniklas 		}
6322040585eSniklas 		/* XXX I assume short reads won't happen here.  */
633fb9475d6Sderaadt 		if (read(fd, new_conf_addr, sz) != (int)sz) {
6347eb3b581Sderaadt 			log_error("conf_reinit: read (%d, %p, %lu) failed",
6357eb3b581Sderaadt 			    fd, new_conf_addr, (unsigned long)sz);
636f8f1e192Sniklas 			goto fail;
637f8f1e192Sniklas 		}
638ea1948caSho 		close(fd);
6392040585eSniklas 
640f8f1e192Sniklas 		trans = conf_begin();
641f8f1e192Sniklas 
642f8f1e192Sniklas 		/* XXX Should we not care about errors and rollback?  */
643f8f1e192Sniklas 		conf_parse(trans, new_conf_addr, sz);
6442872008fShshoexer 	}
645f8f1e192Sniklas 
646510d8b0cSniklas 	/* Load default configuration values.  */
647510d8b0cSniklas 	conf_load_defaults(trans);
648510d8b0cSniklas 
649f8f1e192Sniklas 	/* Free potential existing configuration.  */
650fb9475d6Sderaadt 	if (conf_addr) {
651df915834Shshoexer 		for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0];
652df915834Shshoexer 		    i++)
653f8f1e192Sniklas 			for (cb = LIST_FIRST(&conf_bindings[i]); cb;
654f8f1e192Sniklas 			    cb = LIST_FIRST(&conf_bindings[i]))
655f8f1e192Sniklas 				conf_remove_now(cb->section, cb->tag);
656f8f1e192Sniklas 		free(conf_addr);
657f8f1e192Sniklas 	}
658f8f1e192Sniklas 	conf_end(trans, 1);
659f8f1e192Sniklas 	conf_addr = new_conf_addr;
660f8f1e192Sniklas 	return;
661f8f1e192Sniklas 
662f8f1e192Sniklas fail:
663f8f1e192Sniklas 	free(new_conf_addr);
664f8f1e192Sniklas 	close(fd);
6652040585eSniklas }
6662040585eSniklas 
667a2d30fd1Sniklas /*
668a2d30fd1Sniklas  * Return the numeric value denoted by TAG in section SECTION or DEF
669a2d30fd1Sniklas  * if that tag does not exist.
670a2d30fd1Sniklas  */
6712040585eSniklas int
conf_get_num(char * section,char * tag,int def)672a2d30fd1Sniklas conf_get_num(char *section, char *tag, int def)
6732040585eSniklas {
6742040585eSniklas 	char	*value = conf_get_str(section, tag);
6752040585eSniklas 
6762040585eSniklas 	if (value)
6772040585eSniklas 		return atoi(value);
678a2d30fd1Sniklas 	return def;
6792040585eSniklas }
6802040585eSniklas 
68181c21331Sniklas /*
68281c21331Sniklas  * Return the socket endpoint address denoted by TAG in SECTION as a
68381c21331Sniklas  * struct sockaddr.  It is the callers responsibility to deallocate
68481c21331Sniklas  * this structure when it is finished with it.
68581c21331Sniklas  */
68681c21331Sniklas struct sockaddr *
conf_get_address(char * section,char * tag)68781c21331Sniklas conf_get_address(char *section, char *tag)
68881c21331Sniklas {
68981c21331Sniklas 	char	*value = conf_get_str(section, tag);
69081c21331Sniklas 	struct sockaddr *sa;
69181c21331Sniklas 
69281c21331Sniklas 	if (!value)
69381c21331Sniklas 		return 0;
694e3283cbfSmcbride 	if (text2sockaddr(value, 0, &sa, 0, 0) == -1)
69581c21331Sniklas 		return 0;
69681c21331Sniklas 	return sa;
69781c21331Sniklas }
69881c21331Sniklas 
69982d8fe06Sniklas /* Validate X according to the range denoted by TAG in section SECTION.  */
70082d8fe06Sniklas int
conf_match_num(char * section,char * tag,int x)70182d8fe06Sniklas conf_match_num(char *section, char *tag, int x)
70282d8fe06Sniklas {
70382d8fe06Sniklas 	char	*value = conf_get_str(section, tag);
70482d8fe06Sniklas 	int	 val, min, max, n;
70582d8fe06Sniklas 
70682d8fe06Sniklas 	if (!value)
70782d8fe06Sniklas 		return 0;
70882d8fe06Sniklas 	n = sscanf(value, "%d,%d:%d", &val, &min, &max);
709fb9475d6Sderaadt 	switch (n) {
71082d8fe06Sniklas 	case 1:
71155665484Sho 		LOG_DBG((LOG_MISC, 95, "conf_match_num: %s:%s %d==%d?",
712df915834Shshoexer 		    section, tag, val, x));
71382d8fe06Sniklas 		return x == val;
71482d8fe06Sniklas 	case 3:
71555665484Sho 		LOG_DBG((LOG_MISC, 95, "conf_match_num: %s:%s %d<=%d<=%d?",
716df915834Shshoexer 		    section, tag, min, x, max));
71782d8fe06Sniklas 		return min <= x && max >= x;
71882d8fe06Sniklas 	default:
719df915834Shshoexer 		log_error("conf_match_num: section %s tag %s: invalid number "
720df915834Shshoexer 		    "spec %s", section, tag, value);
72182d8fe06Sniklas 	}
72282d8fe06Sniklas 	return 0;
72382d8fe06Sniklas }
72482d8fe06Sniklas 
7252040585eSniklas /* Return the string value denoted by TAG in section SECTION.  */
7262040585eSniklas char *
conf_get_str(char * section,char * tag)7272040585eSniklas conf_get_str(char *section, char *tag)
7282040585eSniklas {
7292040585eSniklas 	struct conf_binding *cb;
7302040585eSniklas 
731f8f1e192Sniklas 	for (cb = LIST_FIRST(&conf_bindings[conf_hash(section)]); cb;
732f8f1e192Sniklas 	    cb = LIST_NEXT(cb, link))
733df915834Shshoexer 		if (strcasecmp(section, cb->section) == 0 &&
734df915834Shshoexer 		    strcasecmp(tag, cb->tag) == 0) {
735df915834Shshoexer 			LOG_DBG((LOG_MISC, 95, "conf_get_str: [%s]:%s->%s",
736df915834Shshoexer 			    section, tag, cb->value));
7372040585eSniklas 			return cb->value;
7382040585eSniklas 		}
739395a452cSho 	LOG_DBG((LOG_MISC, 95,
740f8f1e192Sniklas 	    "conf_get_str: configuration value not found [%s]:%s", section,
74151ca15aeSniklas 	    tag));
7422040585eSniklas 	return 0;
7432040585eSniklas }
7442040585eSniklas 
745a9753648Sniklas /*
746a9753648Sniklas  * Build a list of string values out of the comma separated value denoted by
747a9753648Sniklas  * TAG in SECTION.
748a9753648Sniklas  */
7492040585eSniklas struct conf_list *
conf_get_list(char * section,char * tag)7502040585eSniklas conf_get_list(char *section, char *tag)
7512040585eSniklas {
752cde22268Sho 	char	*liststr = 0, *p, *field, *t;
7532040585eSniklas 	struct conf_list *list = 0;
75478ef4cbaScloder 	struct conf_list_node *node = 0;
7552040585eSniklas 
7562040585eSniklas 	list = malloc(sizeof *list);
7572040585eSniklas 	if (!list)
7582040585eSniklas 		goto cleanup;
7592040585eSniklas 	TAILQ_INIT(&list->fields);
7602040585eSniklas 	list->cnt = 0;
7612040585eSniklas 	liststr = conf_get_str(section, tag);
7622040585eSniklas 	if (!liststr)
7632040585eSniklas 		goto cleanup;
7642040585eSniklas 	liststr = strdup(liststr);
7652040585eSniklas 	if (!liststr)
7662040585eSniklas 		goto cleanup;
7672040585eSniklas 	p = liststr;
768fb9475d6Sderaadt 	while ((field = strsep(&p, ",")) != NULL) {
769cde22268Sho 		/* Skip leading whitespace */
770025f5691Sderaadt 		while (isspace((unsigned char)*field))
771cde22268Sho 			field++;
772cde22268Sho 		/* Skip trailing whitespace */
773cde22268Sho 		if (p)
774025f5691Sderaadt 			for (t = p - 1; t > field && isspace((unsigned char)*t); t--)
775cde22268Sho 				*t = '\0';
776fb9475d6Sderaadt 		if (*field == '\0') {
7772040585eSniklas 			log_print("conf_get_list: empty field, ignoring...");
7782040585eSniklas 			continue;
7792040585eSniklas 		}
7802040585eSniklas 		list->cnt++;
781a9753648Sniklas 		node = calloc(1, sizeof *node);
7822040585eSniklas 		if (!node)
7832040585eSniklas 			goto cleanup;
784a9753648Sniklas 		node->field = strdup(field);
785a9753648Sniklas 		if (!node->field)
786a9753648Sniklas 			goto cleanup;
7872040585eSniklas 		TAILQ_INSERT_TAIL(&list->fields, node, link);
7882040585eSniklas 	}
789a9753648Sniklas 	free(liststr);
7902040585eSniklas 	return list;
7912040585eSniklas 
7922040585eSniklas cleanup:
79378ef4cbaScloder 	free(node);
7942040585eSniklas 	if (list)
7952040585eSniklas 		conf_free_list(list);
7962040585eSniklas 	free(liststr);
7972040585eSniklas 	return 0;
7982040585eSniklas }
7992040585eSniklas 
80082d8fe06Sniklas struct conf_list *
conf_get_tag_list(char * section)80182d8fe06Sniklas conf_get_tag_list(char *section)
80282d8fe06Sniklas {
80382d8fe06Sniklas 	struct conf_list *list = 0;
80478ef4cbaScloder 	struct conf_list_node *node = 0;
80582d8fe06Sniklas 	struct conf_binding *cb;
80682d8fe06Sniklas 
80782d8fe06Sniklas 	list = malloc(sizeof *list);
80882d8fe06Sniklas 	if (!list)
80982d8fe06Sniklas 		goto cleanup;
81082d8fe06Sniklas 	TAILQ_INIT(&list->fields);
81182d8fe06Sniklas 	list->cnt = 0;
812f8f1e192Sniklas 	for (cb = LIST_FIRST(&conf_bindings[conf_hash(section)]); cb;
813f8f1e192Sniklas 	    cb = LIST_NEXT(cb, link))
814fb9475d6Sderaadt 		if (strcasecmp(section, cb->section) == 0) {
81582d8fe06Sniklas 			list->cnt++;
816a9753648Sniklas 			node = calloc(1, sizeof *node);
81782d8fe06Sniklas 			if (!node)
81882d8fe06Sniklas 				goto cleanup;
819a9753648Sniklas 			node->field = strdup(cb->tag);
820a9753648Sniklas 			if (!node->field)
821a9753648Sniklas 				goto cleanup;
82282d8fe06Sniklas 			TAILQ_INSERT_TAIL(&list->fields, node, link);
82382d8fe06Sniklas 		}
82482d8fe06Sniklas 	return list;
82582d8fe06Sniklas 
82682d8fe06Sniklas cleanup:
82778ef4cbaScloder 	free(node);
82882d8fe06Sniklas 	if (list)
82982d8fe06Sniklas 		conf_free_list(list);
83082d8fe06Sniklas 	return 0;
83182d8fe06Sniklas }
83282d8fe06Sniklas 
8332040585eSniklas void
conf_free_list(struct conf_list * list)8342040585eSniklas conf_free_list(struct conf_list *list)
8352040585eSniklas {
836a9753648Sniklas 	struct conf_list_node *node = TAILQ_FIRST(&list->fields);
837a9753648Sniklas 
838fb9475d6Sderaadt 	while (node) {
839a9753648Sniklas 		TAILQ_REMOVE(&list->fields, node, link);
840a9753648Sniklas 		free(node->field);
841a9753648Sniklas 		free(node);
842a9753648Sniklas 		node = TAILQ_FIRST(&list->fields);
843a9753648Sniklas 	}
8442040585eSniklas 	free(list);
8452040585eSniklas }
846f8f1e192Sniklas 
847f8f1e192Sniklas int
conf_begin(void)848f8f1e192Sniklas conf_begin(void)
849f8f1e192Sniklas {
850f8f1e192Sniklas 	static int	seq = 0;
851f8f1e192Sniklas 
852f8f1e192Sniklas 	return ++seq;
853f8f1e192Sniklas }
854f8f1e192Sniklas 
8556c5cd17eSmoritz static int
conf_trans_node(int transaction,enum conf_op op,char * section,char * tag,char * value,int override,int is_default)8566c5cd17eSmoritz conf_trans_node(int transaction, enum conf_op op, char *section, char *tag,
8576c5cd17eSmoritz     char *value, int override, int is_default)
858f8f1e192Sniklas {
859f8f1e192Sniklas 	struct conf_trans *node;
860f8f1e192Sniklas 
861f8f1e192Sniklas 	node = calloc(1, sizeof *node);
862fb9475d6Sderaadt 	if (!node) {
8637eb3b581Sderaadt 		log_error("conf_trans_node: calloc (1, %lu) failed",
8647eb3b581Sderaadt 		    (unsigned long)sizeof *node);
8656c5cd17eSmoritz 		return 1;
866f8f1e192Sniklas 	}
867f8f1e192Sniklas 	node->trans = transaction;
868f8f1e192Sniklas 	node->op = op;
8696c5cd17eSmoritz 	node->override = override;
8706c5cd17eSmoritz 	node->is_default = is_default;
8716c5cd17eSmoritz 	if (section && (node->section = strdup(section)) == NULL)
8726c5cd17eSmoritz 		goto fail;
8736c5cd17eSmoritz 	if (tag && (node->tag = strdup(tag)) == NULL)
8746c5cd17eSmoritz 		goto fail;
8756c5cd17eSmoritz 	if (value && (node->value = strdup(value)) == NULL)
8766c5cd17eSmoritz 		goto fail;
877f8f1e192Sniklas 	TAILQ_INSERT_TAIL(&conf_trans_queue, node, link);
8786c5cd17eSmoritz 	return 0;
8796c5cd17eSmoritz 
8806c5cd17eSmoritz fail:
8816c5cd17eSmoritz 	free(node->section);
8826c5cd17eSmoritz 	free(node->tag);
8836c5cd17eSmoritz 	free(node->value);
8846c5cd17eSmoritz 	free(node);
8856c5cd17eSmoritz 	return 1;
886f8f1e192Sniklas }
887f8f1e192Sniklas 
888f8f1e192Sniklas /* Queue a set operation.  */
889f8f1e192Sniklas int
conf_set(int transaction,char * section,char * tag,char * value,int override,int is_default)890510d8b0cSniklas conf_set(int transaction, char *section, char *tag, char *value, int override,
891510d8b0cSniklas     int is_default)
892f8f1e192Sniklas {
8936c5cd17eSmoritz 	return conf_trans_node(transaction, CONF_SET, section, tag, value,
8946c5cd17eSmoritz 	    override, is_default);
895f8f1e192Sniklas }
896f8f1e192Sniklas 
897f8f1e192Sniklas /* Queue a remove operation.  */
898f8f1e192Sniklas int
conf_remove(int transaction,char * section,char * tag)899f8f1e192Sniklas conf_remove(int transaction, char *section, char *tag)
900f8f1e192Sniklas {
9016c5cd17eSmoritz 	return conf_trans_node(transaction, CONF_REMOVE, section, tag, NULL,
9026c5cd17eSmoritz 	    0, 0);
903f8f1e192Sniklas }
904f8f1e192Sniklas 
905f8f1e192Sniklas /* Queue a remove section operation.  */
906f8f1e192Sniklas int
conf_remove_section(int transaction,char * section)907f8f1e192Sniklas conf_remove_section(int transaction, char *section)
908f8f1e192Sniklas {
9096c5cd17eSmoritz 	return conf_trans_node(transaction, CONF_REMOVE_SECTION, section, NULL,
9106c5cd17eSmoritz 	    NULL, 0, 0);
911f8f1e192Sniklas }
912f8f1e192Sniklas 
913f8f1e192Sniklas /* Execute all queued operations for this transaction.  Cleanup.  */
914f8f1e192Sniklas int
conf_end(int transaction,int commit)915f8f1e192Sniklas conf_end(int transaction, int commit)
916f8f1e192Sniklas {
917f8f1e192Sniklas 	struct conf_trans *node, *next;
918f8f1e192Sniklas 
919fb9475d6Sderaadt 	for (node = TAILQ_FIRST(&conf_trans_queue); node; node = next) {
920f8f1e192Sniklas 		next = TAILQ_NEXT(node, link);
921fb9475d6Sderaadt 		if (node->trans == transaction) {
922f8f1e192Sniklas 			if (commit)
923fb9475d6Sderaadt 				switch (node->op) {
924f8f1e192Sniklas 				case CONF_SET:
92599cdfc90Sderaadt 					conf_set_now(node->section, node->tag,
92699cdfc90Sderaadt 					    node->value, node->override,
92799cdfc90Sderaadt 					    node->is_default);
928f8f1e192Sniklas 					break;
929f8f1e192Sniklas 				case CONF_REMOVE:
930df915834Shshoexer 					conf_remove_now(node->section,
931df915834Shshoexer 					    node->tag);
932f8f1e192Sniklas 					break;
933f8f1e192Sniklas 				case CONF_REMOVE_SECTION:
934f8f1e192Sniklas 					conf_remove_section_now(node->section);
935f8f1e192Sniklas 					break;
936f8f1e192Sniklas 				default:
937df915834Shshoexer 					log_print("conf_end: unknown "
938df915834Shshoexer 					    "operation: %d", node->op);
939f8f1e192Sniklas 				}
940f8f1e192Sniklas 			TAILQ_REMOVE(&conf_trans_queue, node, link);
941074d67afSniklas 			free(node->section);
942074d67afSniklas 			free(node->tag);
943074d67afSniklas 			free(node->value);
944f8f1e192Sniklas 			free(node);
945f8f1e192Sniklas 		}
946f8f1e192Sniklas 	}
947f8f1e192Sniklas 	return 0;
948f8f1e192Sniklas }
949510d8b0cSniklas 
95094de5165Sniklas /*
95194de5165Sniklas  * Dump running configuration upon SIGUSR1.
952395a452cSho  * Configuration is "stored in reverse order", so reverse it again.
95394de5165Sniklas  */
954510d8b0cSniklas struct dumper {
955510d8b0cSniklas 	char	*s, *v;
956510d8b0cSniklas 	struct dumper *next;
957510d8b0cSniklas };
958510d8b0cSniklas 
959510d8b0cSniklas static void
conf_report_dump(struct dumper * node)960510d8b0cSniklas conf_report_dump(struct dumper *node)
961510d8b0cSniklas {
962510d8b0cSniklas 	/* Recursive, cleanup when we're done.  */
963510d8b0cSniklas 
964510d8b0cSniklas 	if (node->next)
965510d8b0cSniklas 		conf_report_dump(node->next);
966510d8b0cSniklas 
967510d8b0cSniklas 	if (node->v)
968510d8b0cSniklas 		LOG_DBG((LOG_REPORT, 0, "%s=\t%s", node->s, node->v));
969fb9475d6Sderaadt 	else if (node->s) {
970510d8b0cSniklas 		LOG_DBG((LOG_REPORT, 0, "%s", node->s));
971510d8b0cSniklas 		if (strlen(node->s) > 0)
972510d8b0cSniklas 			free(node->s);
973510d8b0cSniklas 	}
974510d8b0cSniklas 	free(node);
975510d8b0cSniklas }
976510d8b0cSniklas 
977510d8b0cSniklas void
conf_report(void)978510d8b0cSniklas conf_report(void)
979510d8b0cSniklas {
9800eb823c5Sniklas 	struct conf_binding *cb, *last = 0;
981e9cbd6b9Sderaadt 	unsigned int	i;
982c9899b11Skrw 	char           *current_section = NULL;
983510d8b0cSniklas 	struct dumper  *dumper, *dnode;
984510d8b0cSniklas 
9855ae94ef8Sderaadt 	dumper = dnode = calloc(1, sizeof *dumper);
986510d8b0cSniklas 	if (!dumper)
987510d8b0cSniklas 		goto mem_fail;
988510d8b0cSniklas 
989510d8b0cSniklas 	LOG_DBG((LOG_REPORT, 0, "conf_report: dumping running configuration"));
990510d8b0cSniklas 
991510d8b0cSniklas 	for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++)
992510d8b0cSniklas 		for (cb = LIST_FIRST(&conf_bindings[i]); cb;
993fb9475d6Sderaadt 		    cb = LIST_NEXT(cb, link)) {
994fb9475d6Sderaadt 			if (!cb->is_default) {
9950eb823c5Sniklas 				/* Dump this entry.  */
996df915834Shshoexer 				if (!current_section || strcmp(cb->section,
997df915834Shshoexer 				    current_section)) {
998fb9475d6Sderaadt 					if (current_section) {
999e9cbd6b9Sderaadt 						if (asprintf(&dnode->s, "[%s]",
1000e9cbd6b9Sderaadt 						    current_section) == -1)
1001510d8b0cSniklas 							goto mem_fail;
10025ae94ef8Sderaadt 						dnode->next = calloc(1,
100350eea14cSho 						    sizeof(struct dumper));
1004510d8b0cSniklas 						dnode = dnode->next;
1005510d8b0cSniklas 						if (!dnode)
1006510d8b0cSniklas 							goto mem_fail;
1007510d8b0cSniklas 
1008510d8b0cSniklas 						dnode->s = "";
10095ae94ef8Sderaadt 						dnode->next = calloc(1,
101050eea14cSho 						    sizeof(struct dumper));
1011510d8b0cSniklas 						dnode = dnode->next;
1012510d8b0cSniklas 						if (!dnode)
1013510d8b0cSniklas 							goto mem_fail;
1014510d8b0cSniklas 					}
1015510d8b0cSniklas 					current_section = cb->section;
1016510d8b0cSniklas 				}
1017510d8b0cSniklas 				dnode->s = cb->tag;
1018510d8b0cSniklas 				dnode->v = cb->value;
10195ae94ef8Sderaadt 				dnode->next = calloc(1, sizeof(struct dumper));
1020510d8b0cSniklas 				dnode = dnode->next;
1021510d8b0cSniklas 				if (!dnode)
1022510d8b0cSniklas 					goto mem_fail;
1023510d8b0cSniklas 				last = cb;
1024510d8b0cSniklas 			}
1025510d8b0cSniklas 		}
1026510d8b0cSniklas 
1027e9cbd6b9Sderaadt 	if (last)
1028e9cbd6b9Sderaadt 		if (asprintf(&dnode->s, "[%s]", last->section) == -1)
1029510d8b0cSniklas 			goto mem_fail;
1030510d8b0cSniklas 	conf_report_dump(dumper);
1031510d8b0cSniklas 
1032510d8b0cSniklas 	return;
1033510d8b0cSniklas 
1034510d8b0cSniklas mem_fail:
10350eb823c5Sniklas 	log_error("conf_report: malloc/calloc failed");
1036fb9475d6Sderaadt 	while ((dnode = dumper) != 0) {
1037510d8b0cSniklas 		dumper = dumper->next;
1038510d8b0cSniklas 		free(dnode->s);
1039510d8b0cSniklas 		free(dnode);
1040510d8b0cSniklas 	}
1041510d8b0cSniklas }
1042