xref: /openbsd-src/usr.sbin/ldapd/parse.y (revision 08f6ba1906d01c4a24fa700d568400f71bcf9611)
1*08f6ba19Snaddy /*	$OpenBSD: parse.y,v 1.43 2021/10/15 15:01:28 naddy Exp $ */
25d465952Smartinh 
35d465952Smartinh /*
4a2a43363Smartinh  * Copyright (c) 2009, 2010 Martin Hedenfalk <martinh@openbsd.org>
55d465952Smartinh  * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
65d465952Smartinh  * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
75d465952Smartinh  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
85d465952Smartinh  * Copyright (c) 2001 Daniel Hartmeier.  All rights reserved.
95d465952Smartinh  * Copyright (c) 2001 Theo de Raadt.  All rights reserved.
105d465952Smartinh  *
115d465952Smartinh  * Permission to use, copy, modify, and distribute this software for any
125d465952Smartinh  * purpose with or without fee is hereby granted, provided that the above
135d465952Smartinh  * copyright notice and this permission notice appear in all copies.
145d465952Smartinh  *
155d465952Smartinh  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
165d465952Smartinh  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
175d465952Smartinh  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
185d465952Smartinh  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
195d465952Smartinh  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
205d465952Smartinh  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
215d465952Smartinh  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
225d465952Smartinh  */
235d465952Smartinh 
245d465952Smartinh %{
255d465952Smartinh #include <sys/types.h>
265d465952Smartinh #include <sys/queue.h>
275d465952Smartinh #include <sys/tree.h>
285d465952Smartinh #include <sys/socket.h>
295d465952Smartinh #include <sys/stat.h>
305d465952Smartinh #include <sys/un.h>
315d465952Smartinh #include <netinet/in.h>
325d465952Smartinh #include <arpa/inet.h>
335d465952Smartinh 
345d465952Smartinh #include <ctype.h>
355d465952Smartinh #include <err.h>
365d465952Smartinh #include <errno.h>
375d465952Smartinh #include <ifaddrs.h>
385d465952Smartinh #include <limits.h>
395d465952Smartinh #include <netdb.h>
405d465952Smartinh #include <stdarg.h>
415d465952Smartinh #include <stdio.h>
425d465952Smartinh #include <stdlib.h>
435d465952Smartinh #include <string.h>
445d465952Smartinh #include <syslog.h>
455d465952Smartinh #include <unistd.h>
465d465952Smartinh 
475d465952Smartinh #include "ldapd.h"
48fdd30f56Sbenno #include "log.h"
495d465952Smartinh 
505d465952Smartinh TAILQ_HEAD(files, file)		 files = TAILQ_HEAD_INITIALIZER(files);
515d465952Smartinh static struct file {
525d465952Smartinh 	TAILQ_ENTRY(file)	 entry;
535d465952Smartinh 	FILE			*stream;
545d465952Smartinh 	char			*name;
55cc70a1e5Sdenis 	size_t			 ungetpos;
56cc70a1e5Sdenis 	size_t			 ungetsize;
57cc70a1e5Sdenis 	u_char			*ungetbuf;
58cc70a1e5Sdenis 	int			 eof_reached;
595d465952Smartinh 	int			 lineno;
605d465952Smartinh 	int			 errors;
615d465952Smartinh } *file, *topfile;
625d465952Smartinh struct file	*pushfile(const char *, int);
635d465952Smartinh int		 popfile(void);
645d465952Smartinh int		 check_file_secrecy(int, const char *);
655d465952Smartinh int		 yyparse(void);
665d465952Smartinh int		 yylex(void);
670f79392cSdoug int		 yyerror(const char *, ...)
680f79392cSdoug     __attribute__((__format__ (printf, 1, 2)))
690f79392cSdoug     __attribute__((__nonnull__ (1)));
705d465952Smartinh int		 kw_cmp(const void *, const void *);
715d465952Smartinh int		 lookup(char *);
72cc70a1e5Sdenis int		 igetc(void);
735d465952Smartinh int		 lgetc(int);
74cc70a1e5Sdenis void		 lungetc(int);
755d465952Smartinh int		 findeol(void);
765d465952Smartinh 
775d465952Smartinh struct listener *host_unix(const char *path);
785d465952Smartinh struct listener	*host_v4(const char *, in_port_t);
795d465952Smartinh struct listener	*host_v6(const char *, in_port_t);
805d465952Smartinh int		 host_dns(const char *, const char *,
8106cb3bdbSrob 		    struct listenerlist *, in_port_t, u_int8_t);
825d465952Smartinh int		 host(const char *, const char *,
8306cb3bdbSrob 		    struct listenerlist *, in_port_t, u_int8_t);
845d465952Smartinh int		 interface(const char *, const char *,
8506cb3bdbSrob 		    struct listenerlist *, in_port_t, u_int8_t);
86bf448d9dStb int		 load_certfile(struct ldapd_config *, const char *, u_int8_t, u_int8_t);
875d465952Smartinh 
885d465952Smartinh TAILQ_HEAD(symhead, sym)	 symhead = TAILQ_HEAD_INITIALIZER(symhead);
895d465952Smartinh struct sym {
905d465952Smartinh 	TAILQ_ENTRY(sym)	 entry;
915d465952Smartinh 	int			 used;
925d465952Smartinh 	int			 persist;
935d465952Smartinh 	char			*nam;
945d465952Smartinh 	char			*val;
955d465952Smartinh };
965d465952Smartinh int		 symset(const char *, const char *, int);
975d465952Smartinh char		*symget(const char *);
985d465952Smartinh 
995d465952Smartinh struct ldapd_config	*conf;
1005d465952Smartinh 
10157f46873Sjmatthew SPLAY_GENERATE(ssltree, ssl, ssl_nodes, ssl_cmp);
10257f46873Sjmatthew 
1035d465952Smartinh static struct aci	*mk_aci(int type, int rights, enum scope scope,
104841a002bSreyk 				char *target, char *subject, char *attr);
1055d465952Smartinh 
1065d465952Smartinh typedef struct {
1075d465952Smartinh 	union {
1085d465952Smartinh 		int64_t		 number;
1095d465952Smartinh 		char		*string;
1105d465952Smartinh 		struct aci	*aci;
1115d465952Smartinh 	} v;
1125d465952Smartinh 	int lineno;
1135d465952Smartinh } YYSTYPE;
1145d465952Smartinh 
115a2a43363Smartinh static struct namespace *current_ns = NULL;
1165d465952Smartinh 
1175d465952Smartinh %}
1185d465952Smartinh 
119bf448d9dStb %token	ERROR LISTEN ON LEGACY TLS LDAPS PORT NAMESPACE ROOTDN ROOTPW INDEX
120a2a43363Smartinh %token	SECURE RELAX STRICT SCHEMA USE COMPRESSION LEVEL
1215d465952Smartinh %token	INCLUDE CERTIFICATE FSYNC CACHE_SIZE INDEX_CACHE_SIZE
12238c09006Smartinh %token	DENY ALLOW READ WRITE BIND ACCESS TO ROOT REFERRAL
1235d465952Smartinh %token	ANY CHILDREN OF ATTRIBUTE IN SUBTREE BY SELF
1245d465952Smartinh %token	<v.string>	STRING
1255d465952Smartinh %token  <v.number>	NUMBER
126bf448d9dStb %type	<v.number>	port ssl boolean comp_level legacy protocol
1275d465952Smartinh %type	<v.number>	aci_type aci_access aci_rights aci_right aci_scope
128841a002bSreyk %type	<v.string>	aci_target aci_attr aci_subject certname
1295d465952Smartinh %type	<v.aci>		aci
1305d465952Smartinh 
1315d465952Smartinh %%
1325d465952Smartinh 
1335d465952Smartinh grammar		: /* empty */
134a2a43363Smartinh 		| grammar '\n'
135a2a43363Smartinh 		| grammar include '\n'
136a2a43363Smartinh 		| grammar varset '\n'
137a2a43363Smartinh 		| grammar conf_main '\n'
138a2a43363Smartinh 		| grammar error '\n'		{ file->errors++; }
139a2a43363Smartinh 		| grammar namespace '\n'
140a2a43363Smartinh 		| grammar aci '\n'		{
1415d465952Smartinh 			SIMPLEQ_INSERT_TAIL(&conf->acl, $2, entry);
1425d465952Smartinh 		}
143a2a43363Smartinh 		| grammar schema '\n'
1445d465952Smartinh 		;
1455d465952Smartinh 
146bf448d9dStb legacy		: /* empty */			{ $$ = 0; }
147bf448d9dStb 		| LEGACY			{ $$ = F_LEGACY; }
148bf448d9dStb 		;
149bf448d9dStb 
150bf448d9dStb protocol	: /* empty */			{ $$ = 0; }
1515d465952Smartinh 		| TLS				{ $$ = F_STARTTLS; }
1525d465952Smartinh 		| LDAPS				{ $$ = F_LDAPS; }
1535d465952Smartinh 		| SECURE			{ $$ = F_SECURE; }
1545d465952Smartinh 		;
1555d465952Smartinh 
156bf448d9dStb ssl		: legacy protocol		{ $$ = $1 | $2; }
157bf448d9dStb 		;
158bf448d9dStb 
1595d465952Smartinh certname	: /* empty */			{ $$ = NULL; }
1605d465952Smartinh 		| CERTIFICATE STRING		{ $$ = $2; }
1615d465952Smartinh 		;
1625d465952Smartinh 
1635d465952Smartinh port		: PORT STRING			{
1645d465952Smartinh 			struct servent	*servent;
1655d465952Smartinh 
1665d465952Smartinh 			servent = getservbyname($2, "tcp");
1675d465952Smartinh 			if (servent == NULL) {
1685d465952Smartinh 				yyerror("port %s is invalid", $2);
1695d465952Smartinh 				free($2);
1705d465952Smartinh 				YYERROR;
1715d465952Smartinh 			}
1725d465952Smartinh 			$$ = servent->s_port;
1735d465952Smartinh 			free($2);
1745d465952Smartinh 		}
1755d465952Smartinh 		| PORT NUMBER			{
1760ead3a23Sflorian 			if ($2 <= 0 || $2 > (int)USHRT_MAX) {
1775d465952Smartinh 				yyerror("invalid port: %lld", $2);
1785d465952Smartinh 				YYERROR;
1795d465952Smartinh 			}
1805d465952Smartinh 			$$ = htons($2);
1815d465952Smartinh 		}
1825d465952Smartinh 		| /* empty */			{
1835d465952Smartinh 			$$ = 0;
1845d465952Smartinh 		}
1855d465952Smartinh 		;
1865d465952Smartinh 
1875d465952Smartinh conf_main	: LISTEN ON STRING port ssl certname	{
1885d465952Smartinh 			char			*cert;
1895d465952Smartinh 
1905d465952Smartinh 			if ($4 == 0) {
191bf448d9dStb 				if ($5 & F_LDAPS)
1925d465952Smartinh 					$4 = htons(LDAPS_PORT);
1935d465952Smartinh 				else
1945d465952Smartinh 					$4 = htons(LDAP_PORT);
1955d465952Smartinh 			}
1965d465952Smartinh 
1975d465952Smartinh 			cert = ($6 != NULL) ? $6 : $3;
1985d465952Smartinh 
199bf448d9dStb 			if (($5 & F_SSL) &&
200bf448d9dStb 			    load_certfile(conf, cert, F_SCERT, $5) < 0) {
2015d465952Smartinh 				yyerror("cannot load certificate: %s", cert);
2025d465952Smartinh 				free($6);
2035d465952Smartinh 				free($3);
2045d465952Smartinh 				YYERROR;
2055d465952Smartinh 			}
2065d465952Smartinh 
2075d465952Smartinh 			if (! interface($3, cert, &conf->listeners,
20806cb3bdbSrob 			    $4, $5)) {
2095d465952Smartinh 				if (host($3, cert, &conf->listeners,
210af5fcce9Sclaudio 				    $4, $5) != 1) {
2115d465952Smartinh 					yyerror("invalid virtual ip or interface: %s", $3);
2125d465952Smartinh 					free($6);
2135d465952Smartinh 					free($3);
2145d465952Smartinh 					YYERROR;
2155d465952Smartinh 				}
2165d465952Smartinh 			}
2175d465952Smartinh 			free($6);
2185d465952Smartinh 			free($3);
2195d465952Smartinh 		}
22038c09006Smartinh 		| REFERRAL STRING		{
22138c09006Smartinh 			struct referral	*ref;
22238c09006Smartinh 			if ((ref = calloc(1, sizeof(*ref))) == NULL) {
22338c09006Smartinh 				yyerror("calloc");
22438c09006Smartinh 				free($2);
22538c09006Smartinh 				YYERROR;
22638c09006Smartinh 			}
22738c09006Smartinh 			ref->url = $2;
22838c09006Smartinh 			SLIST_INSERT_HEAD(&conf->referrals, ref, next);
22938c09006Smartinh 		}
2303cf00f2aSmartinh 		| ROOTDN STRING			{
2313cf00f2aSmartinh 			conf->rootdn = $2;
2323cf00f2aSmartinh 			normalize_dn(conf->rootdn);
2333cf00f2aSmartinh 		}
2343cf00f2aSmartinh 		| ROOTPW STRING			{ conf->rootpw = $2; }
2355d465952Smartinh 		;
2365d465952Smartinh 
237a2a43363Smartinh namespace	: NAMESPACE STRING '{' '\n'		{
238a2a43363Smartinh 			log_debug("parsing namespace %s", $2);
239a2a43363Smartinh 			current_ns = namespace_new($2);
2405d465952Smartinh 			free($2);
241a2a43363Smartinh 			TAILQ_INSERT_TAIL(&conf->namespaces, current_ns, next);
242a2a43363Smartinh 		} ns_opts '}'			{ current_ns = NULL; }
2435d465952Smartinh 		;
2445d465952Smartinh 
2455d465952Smartinh boolean		: STRING			{
2465d465952Smartinh 			if (strcasecmp($1, "true") == 0 ||
2475d465952Smartinh 			    strcasecmp($1, "yes") == 0)
2485d465952Smartinh 				$$ = 1;
2495d465952Smartinh 			else if (strcasecmp($1, "false") == 0 ||
2505d465952Smartinh 			    strcasecmp($1, "off") == 0 ||
2515d465952Smartinh 			    strcasecmp($1, "no") == 0)
2525d465952Smartinh 				$$ = 0;
2535d465952Smartinh 			else {
2545d465952Smartinh 				yyerror("invalid boolean value '%s'", $1);
2555d465952Smartinh 				free($1);
2565d465952Smartinh 				YYERROR;
2575d465952Smartinh 			}
2585d465952Smartinh 			free($1);
2595d465952Smartinh 		}
2605d465952Smartinh 		| ON				{ $$ = 1; }
2615d465952Smartinh 		;
2625d465952Smartinh 
2635d465952Smartinh ns_opts		: /* empty */
264a2a43363Smartinh 		| ns_opts '\n'
265a2a43363Smartinh 		| ns_opts ns_opt '\n'
266a2a43363Smartinh 		;
267a2a43363Smartinh 
268a2a43363Smartinh ns_opt		: ROOTDN STRING			{
269a2a43363Smartinh 			current_ns->rootdn = $2;
270a2a43363Smartinh 			normalize_dn(current_ns->rootdn);
2715d465952Smartinh 		}
272a2a43363Smartinh 		| ROOTPW STRING			{ current_ns->rootpw = $2; }
273a2a43363Smartinh 		| INDEX STRING			{
2745d465952Smartinh 			struct attr_index	*ai;
2755d465952Smartinh 			if ((ai = calloc(1, sizeof(*ai))) == NULL) {
2765d465952Smartinh 				yyerror("calloc");
277a2a43363Smartinh                                 free($2);
2785d465952Smartinh 				YYERROR;
2795d465952Smartinh 			}
280a2a43363Smartinh 			ai->attr = $2;
2815d465952Smartinh 			ai->type = INDEX_EQUAL;
282a2a43363Smartinh 			TAILQ_INSERT_TAIL(&current_ns->indices, ai, next);
2835d465952Smartinh 		}
284a2a43363Smartinh 		| CACHE_SIZE NUMBER		{ current_ns->cache_size = $2; }
285a2a43363Smartinh 		| INDEX_CACHE_SIZE NUMBER	{ current_ns->index_cache_size = $2; }
286a2a43363Smartinh 		| FSYNC boolean			{ current_ns->sync = $2; }
287a2a43363Smartinh 		| aci				{
288a2a43363Smartinh 			SIMPLEQ_INSERT_TAIL(&current_ns->acl, $1, entry);
2895d465952Smartinh 		}
290a2a43363Smartinh 		| RELAX SCHEMA			{ current_ns->relax = 1; }
291a2a43363Smartinh 		| STRICT SCHEMA			{ current_ns->relax = 0; }
292a2a43363Smartinh 		| USE COMPRESSION comp_level	{ current_ns->compression_level = $3; }
29338c09006Smartinh 		| REFERRAL STRING		{
29438c09006Smartinh 			struct referral	*ref;
29538c09006Smartinh 			if ((ref = calloc(1, sizeof(*ref))) == NULL) {
29638c09006Smartinh 				yyerror("calloc");
29738c09006Smartinh 				free($2);
29838c09006Smartinh 				YYERROR;
29938c09006Smartinh 			}
30038c09006Smartinh 			ref->url = $2;
30138c09006Smartinh 			SLIST_INSERT_HEAD(&current_ns->referrals, ref, next);
30238c09006Smartinh 		}
3035d465952Smartinh 		;
3045d465952Smartinh 
3055d465952Smartinh comp_level	: /* empty */			{ $$ = 6; }
3065d465952Smartinh 		| LEVEL NUMBER			{ $$ = $2; }
3075d465952Smartinh 		;
3085d465952Smartinh 
309841a002bSreyk aci		: aci_type aci_access TO aci_scope aci_target aci_attr aci_subject {
310841a002bSreyk 			if (($$ = mk_aci($1, $2, $4, $5, $6, $7)) == NULL) {
3115d465952Smartinh 				free($5);
3125d465952Smartinh 				free($6);
3135d465952Smartinh 				YYERROR;
3145d465952Smartinh 			}
3155d465952Smartinh 		}
3165d465952Smartinh 		| aci_type aci_access {
3175d465952Smartinh 			if (($$ = mk_aci($1, $2, LDAP_SCOPE_SUBTREE, NULL,
318841a002bSreyk 			    NULL, NULL)) == NULL) {
3195d465952Smartinh 				YYERROR;
3205d465952Smartinh 			}
3215d465952Smartinh 		}
3225d465952Smartinh 		;
3235d465952Smartinh 
3245d465952Smartinh aci_type	: DENY				{ $$ = ACI_DENY; }
3255d465952Smartinh 		| ALLOW				{ $$ = ACI_ALLOW; }
3265d465952Smartinh 		;
3275d465952Smartinh 
3288bc0bdcaSsthen aci_access	: /* empty */			{ $$ = ACI_ALL; }
3298bc0bdcaSsthen 		| ACCESS			{ $$ = ACI_ALL; }
3308bc0bdcaSsthen 		| aci_rights ACCESS		{ $$ = $1; }
3315d465952Smartinh 		;
3325d465952Smartinh 
3335d465952Smartinh aci_rights	: aci_right			{ $$ = $1; }
3345d465952Smartinh 		| aci_rights ',' aci_right	{ $$ = $1 | $3; }
3355d465952Smartinh 		;
3365d465952Smartinh 
3378bc0bdcaSsthen aci_right	: READ				{ $$ = ACI_READ; }
3388bc0bdcaSsthen 		| WRITE				{ $$ = ACI_WRITE; }
3398bc0bdcaSsthen 		| BIND				{ $$ = ACI_BIND; }
3405d465952Smartinh 		;
3415d465952Smartinh 
3425d465952Smartinh 
3435d465952Smartinh aci_scope	: /* empty */			{ $$ = LDAP_SCOPE_BASE; }
3445d465952Smartinh 		| SUBTREE			{ $$ = LDAP_SCOPE_SUBTREE; }
3455d465952Smartinh 		| CHILDREN OF			{ $$ = LDAP_SCOPE_ONELEVEL; }
3465d465952Smartinh 		;
3475d465952Smartinh 
3485d465952Smartinh aci_target	: ANY				{ $$ = NULL; }
3495d465952Smartinh 		| ROOT				{ $$ = strdup(""); }
3505d465952Smartinh 		| STRING			{ $$ = $1; normalize_dn($$); }
3515d465952Smartinh 		;
3525d465952Smartinh 
353841a002bSreyk aci_attr	: /* empty */			{ $$ = NULL; }
354841a002bSreyk 		| ATTRIBUTE STRING		{ $$ = $2; }
355841a002bSreyk 		;
356841a002bSreyk 
3575d465952Smartinh aci_subject	: /* empty */			{ $$ = NULL; }
3585d465952Smartinh 		| BY ANY			{ $$ = NULL; }
3595d465952Smartinh 		| BY STRING			{ $$ = $2; normalize_dn($$); }
3605d465952Smartinh 		| BY SELF			{ $$ = strdup("@"); }
3615d465952Smartinh 		;
3625d465952Smartinh 
3635d465952Smartinh include		: INCLUDE STRING		{
3645d465952Smartinh 			struct file	*nfile;
3655d465952Smartinh 
3665d465952Smartinh 			if ((nfile = pushfile($2, 1)) == NULL) {
3675d465952Smartinh 				yyerror("failed to include file %s", $2);
3685d465952Smartinh 				free($2);
3695d465952Smartinh 				YYERROR;
3705d465952Smartinh 			}
3715d465952Smartinh 			free($2);
3725d465952Smartinh 
3735d465952Smartinh 			file = nfile;
374a2a43363Smartinh 			lungetc('\n');
3755d465952Smartinh 		}
3765d465952Smartinh 		;
3775d465952Smartinh 
3785d465952Smartinh varset		: STRING '=' STRING		{
3790c7b4ca6Sbenno 			char *s = $1;
3800c7b4ca6Sbenno 			while (*s++) {
3810c7b4ca6Sbenno 				if (isspace((unsigned char)*s)) {
3820c7b4ca6Sbenno 					yyerror("macro name cannot contain "
3830c7b4ca6Sbenno 					    "whitespace");
38416a0a906Skrw 					free($1);
38516a0a906Skrw 					free($3);
3860c7b4ca6Sbenno 					YYERROR;
3870c7b4ca6Sbenno 				}
3880c7b4ca6Sbenno 			}
3895d465952Smartinh 			if (symset($1, $3, 0) == -1)
3905d465952Smartinh 				fatal("cannot store variable");
3915d465952Smartinh 			free($1);
3925d465952Smartinh 			free($3);
3935d465952Smartinh 		}
3945d465952Smartinh 		;
395a2a43363Smartinh 
396a2a43363Smartinh schema		: SCHEMA STRING			{
397a2a43363Smartinh 			int	 ret;
398a2a43363Smartinh 
399a2a43363Smartinh 			ret = schema_parse(conf->schema, $2);
400a2a43363Smartinh 			free($2);
401a2a43363Smartinh 			if (ret != 0) {
402a2a43363Smartinh 				YYERROR;
403a2a43363Smartinh 			}
404a2a43363Smartinh 		}
405a2a43363Smartinh 		;
406a2a43363Smartinh 
4075d465952Smartinh %%
4085d465952Smartinh 
4095d465952Smartinh struct keywords {
4105d465952Smartinh 	const char	*k_name;
4115d465952Smartinh 	int		 k_val;
4125d465952Smartinh };
4135d465952Smartinh 
4145d465952Smartinh int
yyerror(const char * fmt,...)4155d465952Smartinh yyerror(const char *fmt, ...)
4165d465952Smartinh {
4175d465952Smartinh 	va_list		 ap;
418e3490c9cSbluhm 	char		*msg;
4195d465952Smartinh 
4205d465952Smartinh 	file->errors++;
4215d465952Smartinh 	va_start(ap, fmt);
422e3490c9cSbluhm 	if (vasprintf(&msg, fmt, ap) == -1)
423e3490c9cSbluhm 		fatalx("yyerror vasprintf");
4245d465952Smartinh 	va_end(ap);
425e3490c9cSbluhm 	logit(LOG_CRIT, "%s:%d: %s", file->name, yylval.lineno, msg);
426e3490c9cSbluhm 	free(msg);
4275d465952Smartinh 	return (0);
4285d465952Smartinh }
4295d465952Smartinh 
4305d465952Smartinh int
kw_cmp(const void * k,const void * e)4315d465952Smartinh kw_cmp(const void *k, const void *e)
4325d465952Smartinh {
4335d465952Smartinh 	return (strcmp(k, ((const struct keywords *)e)->k_name));
4345d465952Smartinh }
4355d465952Smartinh 
4365d465952Smartinh int
lookup(char * s)4375d465952Smartinh lookup(char *s)
4385d465952Smartinh {
4395d465952Smartinh 	/* this has to be sorted always */
4405d465952Smartinh 	static const struct keywords keywords[] = {
4415d465952Smartinh 		{ "access",		ACCESS },
4425d465952Smartinh 		{ "allow",		ALLOW },
4435d465952Smartinh 		{ "any",		ANY },
444841a002bSreyk 		{ "attribute",		ATTRIBUTE },
4455d465952Smartinh 		{ "bind",		BIND },
4465d465952Smartinh 		{ "by",			BY },
4475d465952Smartinh 		{ "cache-size",		CACHE_SIZE },
4485d465952Smartinh 		{ "certificate",	CERTIFICATE },
4495d465952Smartinh 		{ "children",		CHILDREN },
4505d465952Smartinh 		{ "compression",	COMPRESSION },
4515d465952Smartinh 		{ "deny",		DENY },
4525d465952Smartinh 		{ "fsync",		FSYNC },
4535d465952Smartinh 		{ "in",			IN },
4545d465952Smartinh 		{ "include",		INCLUDE },
4555d465952Smartinh 		{ "index",		INDEX },
4565d465952Smartinh 		{ "index-cache-size",	INDEX_CACHE_SIZE },
4575d465952Smartinh 		{ "ldaps",		LDAPS },
458bf448d9dStb 		{ "legacy",		LEGACY },
4595d465952Smartinh 		{ "level",		LEVEL },
4605d465952Smartinh 		{ "listen",		LISTEN },
4615d465952Smartinh 		{ "namespace",		NAMESPACE },
4625d465952Smartinh 		{ "of",			OF },
4635d465952Smartinh 		{ "on",			ON },
4645d465952Smartinh 		{ "port",		PORT },
4655d465952Smartinh 		{ "read",		READ },
46638c09006Smartinh 		{ "referral",		REFERRAL },
4675d465952Smartinh 		{ "relax",		RELAX },
4685d465952Smartinh 		{ "root",		ROOT },
4695d465952Smartinh 		{ "rootdn",		ROOTDN },
4705d465952Smartinh 		{ "rootpw",		ROOTPW },
4715d465952Smartinh 		{ "schema",		SCHEMA },
4725d465952Smartinh 		{ "secure",		SECURE },
4735d465952Smartinh 		{ "self",		SELF },
4745d465952Smartinh 		{ "strict",		STRICT },
4755d465952Smartinh 		{ "subtree",		SUBTREE },
4765d465952Smartinh 		{ "tls",		TLS },
4775d465952Smartinh 		{ "to",			TO },
4785d465952Smartinh 		{ "use",		USE },
4795d465952Smartinh 		{ "write",		WRITE },
4805d465952Smartinh 
4815d465952Smartinh 	};
4825d465952Smartinh 	const struct keywords	*p;
4835d465952Smartinh 
4845d465952Smartinh 	p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
4855d465952Smartinh 	    sizeof(keywords[0]), kw_cmp);
4865d465952Smartinh 
4875d465952Smartinh 	if (p)
4885d465952Smartinh 		return (p->k_val);
4895d465952Smartinh 	else
4905d465952Smartinh 		return (STRING);
4915d465952Smartinh }
4925d465952Smartinh 
493cc70a1e5Sdenis #define	START_EXPAND	1
494cc70a1e5Sdenis #define	DONE_EXPAND	2
4955d465952Smartinh 
496cc70a1e5Sdenis static int	expanding;
497cc70a1e5Sdenis 
498cc70a1e5Sdenis int
igetc(void)499cc70a1e5Sdenis igetc(void)
500cc70a1e5Sdenis {
501cc70a1e5Sdenis 	int	c;
502cc70a1e5Sdenis 
503cc70a1e5Sdenis 	while (1) {
504cc70a1e5Sdenis 		if (file->ungetpos > 0)
505cc70a1e5Sdenis 			c = file->ungetbuf[--file->ungetpos];
506cc70a1e5Sdenis 		else
507cc70a1e5Sdenis 			c = getc(file->stream);
508cc70a1e5Sdenis 
509cc70a1e5Sdenis 		if (c == START_EXPAND)
510cc70a1e5Sdenis 			expanding = 1;
511cc70a1e5Sdenis 		else if (c == DONE_EXPAND)
512cc70a1e5Sdenis 			expanding = 0;
513cc70a1e5Sdenis 		else
514cc70a1e5Sdenis 			break;
515cc70a1e5Sdenis 	}
516cc70a1e5Sdenis 	return (c);
517cc70a1e5Sdenis }
5185d465952Smartinh 
5195d465952Smartinh int
lgetc(int quotec)5205d465952Smartinh lgetc(int quotec)
5215d465952Smartinh {
5225d465952Smartinh 	int		c, next;
5235d465952Smartinh 
5245d465952Smartinh 	if (quotec) {
525cc70a1e5Sdenis 		if ((c = igetc()) == EOF) {
5265d465952Smartinh 			yyerror("reached end of file while parsing "
5275d465952Smartinh 			    "quoted string");
5285d465952Smartinh 			if (file == topfile || popfile() == EOF)
5295d465952Smartinh 				return (EOF);
5305d465952Smartinh 			return (quotec);
5315d465952Smartinh 		}
5325d465952Smartinh 		return (c);
5335d465952Smartinh 	}
5345d465952Smartinh 
535cc70a1e5Sdenis 	while ((c = igetc()) == '\\') {
536cc70a1e5Sdenis 		next = igetc();
5375d465952Smartinh 		if (next != '\n') {
5385d465952Smartinh 			c = next;
5395d465952Smartinh 			break;
5405d465952Smartinh 		}
5415d465952Smartinh 		yylval.lineno = file->lineno;
5425d465952Smartinh 		file->lineno++;
5435d465952Smartinh 	}
5445d465952Smartinh 
545cc70a1e5Sdenis 	if (c == EOF) {
546cc70a1e5Sdenis 		/*
547cc70a1e5Sdenis 		 * Fake EOL when hit EOF for the first time. This gets line
548cc70a1e5Sdenis 		 * count right if last line in included file is syntactically
549cc70a1e5Sdenis 		 * invalid and has no newline.
550cc70a1e5Sdenis 		 */
551cc70a1e5Sdenis 		if (file->eof_reached == 0) {
552cc70a1e5Sdenis 			file->eof_reached = 1;
553cc70a1e5Sdenis 			return ('\n');
554cc70a1e5Sdenis 		}
5555d465952Smartinh 		while (c == EOF) {
5565d465952Smartinh 			if (file == topfile || popfile() == EOF)
5575d465952Smartinh 				return (EOF);
558cc70a1e5Sdenis 			c = igetc();
559cc70a1e5Sdenis 		}
5605d465952Smartinh 	}
5615d465952Smartinh 	return (c);
5625d465952Smartinh }
5635d465952Smartinh 
564cc70a1e5Sdenis void
lungetc(int c)5655d465952Smartinh lungetc(int c)
5665d465952Smartinh {
5675d465952Smartinh 	if (c == EOF)
568cc70a1e5Sdenis 		return;
569cc70a1e5Sdenis 
570cc70a1e5Sdenis 	if (file->ungetpos >= file->ungetsize) {
571cc70a1e5Sdenis 		void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
572cc70a1e5Sdenis 		if (p == NULL)
573a062aa9dSkrw 			err(1, "%s", __func__);
574cc70a1e5Sdenis 		file->ungetbuf = p;
575cc70a1e5Sdenis 		file->ungetsize *= 2;
5765d465952Smartinh 	}
577cc70a1e5Sdenis 	file->ungetbuf[file->ungetpos++] = c;
5785d465952Smartinh }
5795d465952Smartinh 
5805d465952Smartinh int
findeol(void)5815d465952Smartinh findeol(void)
5825d465952Smartinh {
5835d465952Smartinh 	int	c;
5845d465952Smartinh 
5855d465952Smartinh 	/* skip to either EOF or the first real EOL */
5865d465952Smartinh 	while (1) {
5875d465952Smartinh 		c = lgetc(0);
5885d465952Smartinh 		if (c == '\n') {
5895d465952Smartinh 			file->lineno++;
5905d465952Smartinh 			break;
5915d465952Smartinh 		}
5925d465952Smartinh 		if (c == EOF)
5935d465952Smartinh 			break;
5945d465952Smartinh 	}
5955d465952Smartinh 	return (ERROR);
5965d465952Smartinh }
5975d465952Smartinh 
5985d465952Smartinh int
yylex(void)5995d465952Smartinh yylex(void)
6005d465952Smartinh {
601*08f6ba19Snaddy 	char	 buf[4096];
602*08f6ba19Snaddy 	char	*p, *val;
6035d465952Smartinh 	int	 quotec, next, c;
6045d465952Smartinh 	int	 token;
6055d465952Smartinh 
6065d465952Smartinh top:
6075d465952Smartinh 	p = buf;
6085d465952Smartinh 	while ((c = lgetc(0)) == ' ' || c == '\t')
6095d465952Smartinh 		; /* nothing */
6105d465952Smartinh 
6115d465952Smartinh 	yylval.lineno = file->lineno;
6125d465952Smartinh 	if (c == '#')
6135d465952Smartinh 		while ((c = lgetc(0)) != '\n' && c != EOF)
6145d465952Smartinh 			; /* nothing */
615cc70a1e5Sdenis 	if (c == '$' && !expanding) {
6165d465952Smartinh 		while (1) {
6175d465952Smartinh 			if ((c = lgetc(0)) == EOF)
6185d465952Smartinh 				return (0);
6195d465952Smartinh 
6205d465952Smartinh 			if (p + 1 >= buf + sizeof(buf) - 1) {
6215d465952Smartinh 				yyerror("string too long");
6225d465952Smartinh 				return (findeol());
6235d465952Smartinh 			}
6245d465952Smartinh 			if (isalnum(c) || c == '_') {
625015d7b4dSbenno 				*p++ = c;
6265d465952Smartinh 				continue;
6275d465952Smartinh 			}
6285d465952Smartinh 			*p = '\0';
6295d465952Smartinh 			lungetc(c);
6305d465952Smartinh 			break;
6315d465952Smartinh 		}
6325d465952Smartinh 		val = symget(buf);
6335d465952Smartinh 		if (val == NULL) {
6345d465952Smartinh 			yyerror("macro '%s' not defined", buf);
6355d465952Smartinh 			return (findeol());
6365d465952Smartinh 		}
637cc70a1e5Sdenis 		p = val + strlen(val) - 1;
638cc70a1e5Sdenis 		lungetc(DONE_EXPAND);
639cc70a1e5Sdenis 		while (p >= val) {
640*08f6ba19Snaddy 			lungetc((unsigned char)*p);
641cc70a1e5Sdenis 			p--;
642cc70a1e5Sdenis 		}
643cc70a1e5Sdenis 		lungetc(START_EXPAND);
6445d465952Smartinh 		goto top;
6455d465952Smartinh 	}
6465d465952Smartinh 
6475d465952Smartinh 	switch (c) {
6485d465952Smartinh 	case '\'':
6495d465952Smartinh 	case '"':
6505d465952Smartinh 		quotec = c;
6515d465952Smartinh 		while (1) {
6525d465952Smartinh 			if ((c = lgetc(quotec)) == EOF)
6535d465952Smartinh 				return (0);
6545d465952Smartinh 			if (c == '\n') {
6555d465952Smartinh 				file->lineno++;
6565d465952Smartinh 				continue;
6575d465952Smartinh 			} else if (c == '\\') {
6585d465952Smartinh 				if ((next = lgetc(quotec)) == EOF)
6595d465952Smartinh 					return (0);
660a1533359Ssashan 				if (next == quotec || next == ' ' ||
661a1533359Ssashan 				    next == '\t')
6625d465952Smartinh 					c = next;
663daf24110Shenning 				else if (next == '\n') {
664daf24110Shenning 					file->lineno++;
6655d465952Smartinh 					continue;
666daf24110Shenning 				} else
6675d465952Smartinh 					lungetc(next);
6685d465952Smartinh 			} else if (c == quotec) {
6695d465952Smartinh 				*p = '\0';
6705d465952Smartinh 				break;
67141eef22fSjsg 			} else if (c == '\0') {
67241eef22fSjsg 				yyerror("syntax error");
67341eef22fSjsg 				return (findeol());
6745d465952Smartinh 			}
6755d465952Smartinh 			if (p + 1 >= buf + sizeof(buf) - 1) {
676a2a43363Smartinh 				log_warnx("string too long");
6775d465952Smartinh 				return (findeol());
6785d465952Smartinh 			}
679015d7b4dSbenno 			*p++ = c;
6805d465952Smartinh 		}
6815d465952Smartinh 		yylval.v.string = strdup(buf);
6825d465952Smartinh 		if (yylval.v.string == NULL)
6835d465952Smartinh 			fatal("yylex: strdup");
6845d465952Smartinh 		return (STRING);
6855d465952Smartinh 	}
6865d465952Smartinh 
6875d465952Smartinh #define allowed_to_end_number(x) \
6885d465952Smartinh 	(isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
6895d465952Smartinh 
6905d465952Smartinh 	if (c == '-' || isdigit(c)) {
6915d465952Smartinh 		do {
6925d465952Smartinh 			*p++ = c;
693915c3f33Sderaadt 			if ((size_t)(p-buf) >= sizeof(buf)) {
6945d465952Smartinh 				yyerror("string too long");
6955d465952Smartinh 				return (findeol());
6965d465952Smartinh 			}
6975d465952Smartinh 		} while ((c = lgetc(0)) != EOF && isdigit(c));
6985d465952Smartinh 		lungetc(c);
6995d465952Smartinh 		if (p == buf + 1 && buf[0] == '-')
7005d465952Smartinh 			goto nodigits;
7015d465952Smartinh 		if (c == EOF || allowed_to_end_number(c)) {
7025d465952Smartinh 			const char *errstr = NULL;
7035d465952Smartinh 
7045d465952Smartinh 			*p = '\0';
7055d465952Smartinh 			yylval.v.number = strtonum(buf, LLONG_MIN,
7065d465952Smartinh 			    LLONG_MAX, &errstr);
7075d465952Smartinh 			if (errstr) {
7085d465952Smartinh 				yyerror("\"%s\" invalid number: %s",
7095d465952Smartinh 				    buf, errstr);
7105d465952Smartinh 				return (findeol());
7115d465952Smartinh 			}
7125d465952Smartinh 			return (NUMBER);
7135d465952Smartinh 		} else {
7145d465952Smartinh nodigits:
7155d465952Smartinh 			while (p > buf + 1)
716*08f6ba19Snaddy 				lungetc((unsigned char)*--p);
717*08f6ba19Snaddy 			c = (unsigned char)*--p;
7185d465952Smartinh 			if (c == '-')
7195d465952Smartinh 				return (c);
7205d465952Smartinh 		}
7215d465952Smartinh 	}
7225d465952Smartinh 
7235d465952Smartinh #define allowed_in_string(x) \
7245d465952Smartinh 	(isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
7255d465952Smartinh 	x != '{' && x != '}' && x != '<' && x != '>' && \
7265d465952Smartinh 	x != '!' && x != '=' && x != '/' && x != '#' && \
7275d465952Smartinh 	x != ','))
7285d465952Smartinh 
7295d465952Smartinh 	if (isalnum(c) || c == ':' || c == '_' || c == '*') {
7305d465952Smartinh 		do {
7315d465952Smartinh 			*p++ = c;
732915c3f33Sderaadt 			if ((size_t)(p-buf) >= sizeof(buf)) {
7335d465952Smartinh 				yyerror("string too long");
7345d465952Smartinh 				return (findeol());
7355d465952Smartinh 			}
7365d465952Smartinh 		} while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
7375d465952Smartinh 		lungetc(c);
7385d465952Smartinh 		*p = '\0';
7395d465952Smartinh 		if ((token = lookup(buf)) == STRING)
7405d465952Smartinh 			if ((yylval.v.string = strdup(buf)) == NULL)
7415d465952Smartinh 				fatal("yylex: strdup");
7425d465952Smartinh 		return (token);
7435d465952Smartinh 	}
7445d465952Smartinh 	if (c == '\n') {
7455d465952Smartinh 		yylval.lineno = file->lineno;
7465d465952Smartinh 		file->lineno++;
7475d465952Smartinh 	}
7485d465952Smartinh 	if (c == EOF)
7495d465952Smartinh 		return (0);
7505d465952Smartinh 	return (c);
7515d465952Smartinh }
7525d465952Smartinh 
7535d465952Smartinh int
check_file_secrecy(int fd,const char * fname)7545d465952Smartinh check_file_secrecy(int fd, const char *fname)
7555d465952Smartinh {
7565d465952Smartinh 	struct stat	st;
7575d465952Smartinh 
7585d465952Smartinh 	if (fstat(fd, &st)) {
7595d465952Smartinh 		log_warn("cannot stat %s", fname);
7605d465952Smartinh 		return (-1);
7615d465952Smartinh 	}
7625d465952Smartinh 	if (st.st_uid != 0 && st.st_uid != getuid()) {
7635d465952Smartinh 		log_warnx("%s: owner not root or current user", fname);
7645d465952Smartinh 		return (-1);
7655d465952Smartinh 	}
7667140c133Shenning 	if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO)) {
7677140c133Shenning 		log_warnx("%s: group writable or world read/writable", fname);
7685d465952Smartinh 		return (-1);
7695d465952Smartinh 	}
7705d465952Smartinh 	return (0);
7715d465952Smartinh }
7725d465952Smartinh 
7735d465952Smartinh struct file *
pushfile(const char * name,int secret)7745d465952Smartinh pushfile(const char *name, int secret)
7755d465952Smartinh {
7765d465952Smartinh 	struct file	*nfile;
7775d465952Smartinh 
7785d465952Smartinh 	log_debug("parsing config %s", name);
7795d465952Smartinh 
7805d465952Smartinh 	if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
7816a3d55f9Skrw 		log_warn("%s", __func__);
7825d465952Smartinh 		return (NULL);
7835d465952Smartinh 	}
7845d465952Smartinh 	if ((nfile->name = strdup(name)) == NULL) {
7856a3d55f9Skrw 		log_warn("%s", __func__);
7865d465952Smartinh 		free(nfile);
7875d465952Smartinh 		return (NULL);
7885d465952Smartinh 	}
7895d465952Smartinh 	if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
7906a3d55f9Skrw 		log_warn("%s: %s", __func__, nfile->name);
7915d465952Smartinh 		free(nfile->name);
7925d465952Smartinh 		free(nfile);
7935d465952Smartinh 		return (NULL);
7945d465952Smartinh 	}
7955d465952Smartinh 	if (secret &&
7965d465952Smartinh 	    check_file_secrecy(fileno(nfile->stream), nfile->name)) {
7975d465952Smartinh 		fclose(nfile->stream);
7985d465952Smartinh 		free(nfile->name);
7995d465952Smartinh 		free(nfile);
8005d465952Smartinh 		return (NULL);
8015d465952Smartinh 	}
802cc70a1e5Sdenis 	nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
803cc70a1e5Sdenis 	nfile->ungetsize = 16;
804cc70a1e5Sdenis 	nfile->ungetbuf = malloc(nfile->ungetsize);
805cc70a1e5Sdenis 	if (nfile->ungetbuf == NULL) {
8066a3d55f9Skrw 		log_warn("%s", __func__);
807cc70a1e5Sdenis 		fclose(nfile->stream);
808cc70a1e5Sdenis 		free(nfile->name);
809cc70a1e5Sdenis 		free(nfile);
810cc70a1e5Sdenis 		return (NULL);
811cc70a1e5Sdenis 	}
8125d465952Smartinh 	TAILQ_INSERT_TAIL(&files, nfile, entry);
8135d465952Smartinh 	return (nfile);
8145d465952Smartinh }
8155d465952Smartinh 
8165d465952Smartinh int
popfile(void)8175d465952Smartinh popfile(void)
8185d465952Smartinh {
8195d465952Smartinh 	struct file	*prev;
8205d465952Smartinh 
8215d465952Smartinh 	if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
8225d465952Smartinh 		prev->errors += file->errors;
8235d465952Smartinh 
8245d465952Smartinh 	TAILQ_REMOVE(&files, file, entry);
8255d465952Smartinh 	fclose(file->stream);
8265d465952Smartinh 	free(file->name);
827cc70a1e5Sdenis 	free(file->ungetbuf);
8285d465952Smartinh 	free(file);
8295d465952Smartinh 	file = prev;
8305d465952Smartinh 	return (file ? 0 : EOF);
8315d465952Smartinh }
8325d465952Smartinh 
8335d465952Smartinh int
parse_config(char * filename)8345d465952Smartinh parse_config(char *filename)
8355d465952Smartinh {
8365d465952Smartinh 	struct sym		*sym, *next;
8375d465952Smartinh 	int			 errors = 0;
8385d465952Smartinh 
8395d465952Smartinh 	if ((conf = calloc(1, sizeof(struct ldapd_config))) == NULL)
8405d465952Smartinh 		fatal(NULL);
8415d465952Smartinh 
842a2a43363Smartinh 	conf->schema = schema_new();
843a2a43363Smartinh 	if (conf->schema == NULL)
844a2a43363Smartinh 		fatal("schema_new");
845a2a43363Smartinh 
8465d465952Smartinh 	TAILQ_INIT(&conf->namespaces);
8475d465952Smartinh 	TAILQ_INIT(&conf->listeners);
8485d465952Smartinh 	if ((conf->sc_ssl = calloc(1, sizeof(*conf->sc_ssl))) == NULL)
8495d465952Smartinh 		fatal(NULL);
8505d465952Smartinh 	SPLAY_INIT(conf->sc_ssl);
8515d465952Smartinh 	SIMPLEQ_INIT(&conf->acl);
85238c09006Smartinh 	SLIST_INIT(&conf->referrals);
8535d465952Smartinh 
8545d465952Smartinh 	if ((file = pushfile(filename, 1)) == NULL) {
8555d465952Smartinh 		free(conf);
8565d465952Smartinh 		return (-1);
8575d465952Smartinh 	}
8585d465952Smartinh 	topfile = file;
8595d465952Smartinh 
8605d465952Smartinh 	yyparse();
8615d465952Smartinh 	errors = file->errors;
8625d465952Smartinh 	popfile();
8635d465952Smartinh 
8645d465952Smartinh 	/* Free macros and check which have not been used. */
86546bca67bSkrw 	TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
866a2a43363Smartinh 		log_debug("warning: macro \"%s\" not used", sym->nam);
8675d465952Smartinh 		if (!sym->persist) {
8685d465952Smartinh 			free(sym->nam);
8695d465952Smartinh 			free(sym->val);
8705d465952Smartinh 			TAILQ_REMOVE(&symhead, sym, entry);
8715d465952Smartinh 			free(sym);
8725d465952Smartinh 		}
8735d465952Smartinh 	}
8745d465952Smartinh 
8755d465952Smartinh 	return (errors ? -1 : 0);
8765d465952Smartinh }
8775d465952Smartinh 
8785d465952Smartinh int
symset(const char * nam,const char * val,int persist)8795d465952Smartinh symset(const char *nam, const char *val, int persist)
8805d465952Smartinh {
8815d465952Smartinh 	struct sym	*sym;
8825d465952Smartinh 
88354c95b7aSkrw 	TAILQ_FOREACH(sym, &symhead, entry) {
88454c95b7aSkrw 		if (strcmp(nam, sym->nam) == 0)
88554c95b7aSkrw 			break;
88654c95b7aSkrw 	}
8875d465952Smartinh 
8885d465952Smartinh 	if (sym != NULL) {
8895d465952Smartinh 		if (sym->persist == 1)
8905d465952Smartinh 			return (0);
8915d465952Smartinh 		else {
8925d465952Smartinh 			free(sym->nam);
8935d465952Smartinh 			free(sym->val);
8945d465952Smartinh 			TAILQ_REMOVE(&symhead, sym, entry);
8955d465952Smartinh 			free(sym);
8965d465952Smartinh 		}
8975d465952Smartinh 	}
8985d465952Smartinh 	if ((sym = calloc(1, sizeof(*sym))) == NULL)
8995d465952Smartinh 		return (-1);
9005d465952Smartinh 
9015d465952Smartinh 	sym->nam = strdup(nam);
9025d465952Smartinh 	if (sym->nam == NULL) {
9035d465952Smartinh 		free(sym);
9045d465952Smartinh 		return (-1);
9055d465952Smartinh 	}
9065d465952Smartinh 	sym->val = strdup(val);
9075d465952Smartinh 	if (sym->val == NULL) {
9085d465952Smartinh 		free(sym->nam);
9095d465952Smartinh 		free(sym);
9105d465952Smartinh 		return (-1);
9115d465952Smartinh 	}
9125d465952Smartinh 	sym->used = 0;
9135d465952Smartinh 	sym->persist = persist;
9145d465952Smartinh 	TAILQ_INSERT_TAIL(&symhead, sym, entry);
9155d465952Smartinh 	return (0);
9165d465952Smartinh }
9175d465952Smartinh 
9185d465952Smartinh int
cmdline_symset(char * s)9195d465952Smartinh cmdline_symset(char *s)
9205d465952Smartinh {
9215d465952Smartinh 	char	*sym, *val;
9225d465952Smartinh 	int	ret;
9235d465952Smartinh 
9245d465952Smartinh 	if ((val = strrchr(s, '=')) == NULL)
9255d465952Smartinh 		return (-1);
926ed1b9eb8Smiko 	sym = strndup(s, val - s);
927ed1b9eb8Smiko 	if (sym == NULL)
928ed1b9eb8Smiko 		fatal("%s: strndup", __func__);
9295d465952Smartinh 	ret = symset(sym, val + 1, 1);
9305d465952Smartinh 	free(sym);
9315d465952Smartinh 
9325d465952Smartinh 	return (ret);
9335d465952Smartinh }
9345d465952Smartinh 
9355d465952Smartinh char *
symget(const char * nam)9365d465952Smartinh symget(const char *nam)
9375d465952Smartinh {
9385d465952Smartinh 	struct sym	*sym;
9395d465952Smartinh 
94054c95b7aSkrw 	TAILQ_FOREACH(sym, &symhead, entry) {
9415d465952Smartinh 		if (strcmp(nam, sym->nam) == 0) {
9425d465952Smartinh 			sym->used = 1;
9435d465952Smartinh 			return (sym->val);
9445d465952Smartinh 		}
94554c95b7aSkrw 	}
9465d465952Smartinh 	return (NULL);
9475d465952Smartinh }
9485d465952Smartinh 
9495d465952Smartinh struct listener *
host_unix(const char * path)9505d465952Smartinh host_unix(const char *path)
9515d465952Smartinh {
9525d465952Smartinh 	struct sockaddr_un	*saun;
9535d465952Smartinh 	struct listener		*h;
9545d465952Smartinh 
9555d465952Smartinh 	if (*path != '/')
9565d465952Smartinh 		return (NULL);
9575d465952Smartinh 
9585d465952Smartinh 	if ((h = calloc(1, sizeof(*h))) == NULL)
9595d465952Smartinh 		fatal(NULL);
9605d465952Smartinh 	saun = (struct sockaddr_un *)&h->ss;
9615d465952Smartinh 	saun->sun_len = sizeof(struct sockaddr_un);
9625d465952Smartinh 	saun->sun_family = AF_UNIX;
9635d465952Smartinh 	if (strlcpy(saun->sun_path, path, sizeof(saun->sun_path)) >=
9645d465952Smartinh 	    sizeof(saun->sun_path))
9655d465952Smartinh 		fatal("socket path too long");
9665d465952Smartinh 	h->flags = F_SECURE;
9675d465952Smartinh 
9685d465952Smartinh 	return (h);
9695d465952Smartinh }
9705d465952Smartinh 
9715d465952Smartinh struct listener *
host_v4(const char * s,in_port_t port)9725d465952Smartinh host_v4(const char *s, in_port_t port)
9735d465952Smartinh {
9745d465952Smartinh 	struct in_addr		 ina;
9755d465952Smartinh 	struct sockaddr_in	*sain;
9765d465952Smartinh 	struct listener		*h;
9775d465952Smartinh 
978a0e3979fSgsoares 	memset(&ina, 0, sizeof(ina));
9795d465952Smartinh 	if (inet_pton(AF_INET, s, &ina) != 1)
9805d465952Smartinh 		return (NULL);
9815d465952Smartinh 
9825d465952Smartinh 	if ((h = calloc(1, sizeof(*h))) == NULL)
9835d465952Smartinh 		fatal(NULL);
9845d465952Smartinh 	sain = (struct sockaddr_in *)&h->ss;
9855d465952Smartinh 	sain->sin_len = sizeof(struct sockaddr_in);
9865d465952Smartinh 	sain->sin_family = AF_INET;
9875d465952Smartinh 	sain->sin_addr.s_addr = ina.s_addr;
9885d465952Smartinh 	sain->sin_port = port;
9895d465952Smartinh 
9905d465952Smartinh 	return (h);
9915d465952Smartinh }
9925d465952Smartinh 
9935d465952Smartinh struct listener *
host_v6(const char * s,in_port_t port)9945d465952Smartinh host_v6(const char *s, in_port_t port)
9955d465952Smartinh {
9965d465952Smartinh 	struct in6_addr		 ina6;
9975d465952Smartinh 	struct sockaddr_in6	*sin6;
9985d465952Smartinh 	struct listener		*h;
9995d465952Smartinh 
1000a0e3979fSgsoares 	memset(&ina6, 0, sizeof(ina6));
10015d465952Smartinh 	if (inet_pton(AF_INET6, s, &ina6) != 1)
10025d465952Smartinh 		return (NULL);
10035d465952Smartinh 
10045d465952Smartinh 	if ((h = calloc(1, sizeof(*h))) == NULL)
10055d465952Smartinh 		fatal(NULL);
10065d465952Smartinh 	sin6 = (struct sockaddr_in6 *)&h->ss;
10075d465952Smartinh 	sin6->sin6_len = sizeof(struct sockaddr_in6);
10085d465952Smartinh 	sin6->sin6_family = AF_INET6;
10095d465952Smartinh 	sin6->sin6_port = port;
10105d465952Smartinh 	memcpy(&sin6->sin6_addr, &ina6, sizeof(ina6));
10115d465952Smartinh 
10125d465952Smartinh 	return (h);
10135d465952Smartinh }
10145d465952Smartinh 
10155d465952Smartinh int
host_dns(const char * s,const char * cert,struct listenerlist * al,in_port_t port,u_int8_t flags)10165d465952Smartinh host_dns(const char *s, const char *cert,
101706cb3bdbSrob     struct listenerlist *al, in_port_t port, u_int8_t flags)
10185d465952Smartinh {
10195d465952Smartinh 	struct addrinfo		 hints, *res0, *res;
102006cb3bdbSrob 	int			 error;
10215d465952Smartinh 	struct sockaddr_in	*sain;
10225d465952Smartinh 	struct sockaddr_in6	*sin6;
10235d465952Smartinh 	struct listener		*h;
10245d465952Smartinh 
1025a0e3979fSgsoares 	memset(&hints, 0, sizeof(hints));
10265d465952Smartinh 	hints.ai_family = PF_UNSPEC;
10275d465952Smartinh 	hints.ai_socktype = SOCK_DGRAM; /* DUMMY */
10285d465952Smartinh 	error = getaddrinfo(s, NULL, &hints, &res0);
10295d465952Smartinh 	if (error == EAI_AGAIN || error == EAI_NODATA || error == EAI_NONAME)
10305d465952Smartinh 		return (0);
10315d465952Smartinh 	if (error) {
10325d465952Smartinh 		log_warnx("host_dns: could not parse \"%s\": %s", s,
10335d465952Smartinh 		    gai_strerror(error));
10345d465952Smartinh 		return (-1);
10355d465952Smartinh 	}
10365d465952Smartinh 
103706cb3bdbSrob 	for (res = res0; res; res = res->ai_next) {
10385d465952Smartinh 		if (res->ai_family != AF_INET &&
10395d465952Smartinh 		    res->ai_family != AF_INET6)
10405d465952Smartinh 			continue;
10415d465952Smartinh 		if ((h = calloc(1, sizeof(*h))) == NULL)
10425d465952Smartinh 			fatal(NULL);
10435d465952Smartinh 
10445d465952Smartinh 		h->port = port;
10455d465952Smartinh 		h->flags = flags;
10465d465952Smartinh 		h->ss.ss_family = res->ai_family;
10475d465952Smartinh 		h->ssl = NULL;
10485d465952Smartinh 		h->ssl_cert_name[0] = '\0';
10495d465952Smartinh 		if (cert != NULL)
10505d465952Smartinh 			(void)strlcpy(h->ssl_cert_name, cert, sizeof(h->ssl_cert_name));
10515d465952Smartinh 
10525d465952Smartinh 		if (res->ai_family == AF_INET) {
10535d465952Smartinh 			sain = (struct sockaddr_in *)&h->ss;
10545d465952Smartinh 			sain->sin_len = sizeof(struct sockaddr_in);
10555d465952Smartinh 			sain->sin_addr.s_addr = ((struct sockaddr_in *)
10565d465952Smartinh 			    res->ai_addr)->sin_addr.s_addr;
10575d465952Smartinh 			sain->sin_port = port;
10585d465952Smartinh 		} else {
10595d465952Smartinh 			sin6 = (struct sockaddr_in6 *)&h->ss;
10605d465952Smartinh 			sin6->sin6_len = sizeof(struct sockaddr_in6);
10615d465952Smartinh 			memcpy(&sin6->sin6_addr, &((struct sockaddr_in6 *)
10625d465952Smartinh 			    res->ai_addr)->sin6_addr, sizeof(struct in6_addr));
10635d465952Smartinh 			sin6->sin6_port = port;
10645d465952Smartinh 		}
10655d465952Smartinh 
10665d465952Smartinh 		TAILQ_INSERT_HEAD(al, h, entry);
10675d465952Smartinh 	}
10685d465952Smartinh 	freeaddrinfo(res0);
1069aa09fe16Smartijn 	return 1;
10705d465952Smartinh }
10715d465952Smartinh 
10725d465952Smartinh int
host(const char * s,const char * cert,struct listenerlist * al,in_port_t port,u_int8_t flags)10735d465952Smartinh host(const char *s, const char *cert, struct listenerlist *al,
107406cb3bdbSrob     in_port_t port, u_int8_t flags)
10755d465952Smartinh {
10765d465952Smartinh 	struct listener *h;
10775d465952Smartinh 
10785d465952Smartinh 	/* Unix socket path? */
10795d465952Smartinh 	h = host_unix(s);
10805d465952Smartinh 
10815d465952Smartinh 	/* IPv4 address? */
10825d465952Smartinh 	if (h == NULL)
10835d465952Smartinh 		h = host_v4(s, port);
10845d465952Smartinh 
10855d465952Smartinh 	/* IPv6 address? */
10865d465952Smartinh 	if (h == NULL)
10875d465952Smartinh 		h = host_v6(s, port);
10885d465952Smartinh 
10895d465952Smartinh 	if (h != NULL) {
10905d465952Smartinh 		h->port = port;
10915d465952Smartinh 		h->flags |= flags;
10925d465952Smartinh 		h->ssl = NULL;
10935d465952Smartinh 		h->ssl_cert_name[0] = '\0';
10945d465952Smartinh 		if (cert != NULL)
10955d465952Smartinh 			strlcpy(h->ssl_cert_name, cert, sizeof(h->ssl_cert_name));
10965d465952Smartinh 
10975d465952Smartinh 		TAILQ_INSERT_HEAD(al, h, entry);
10985d465952Smartinh 		return (1);
10995d465952Smartinh 	}
11005d465952Smartinh 
110106cb3bdbSrob 	return (host_dns(s, cert, al, port, flags));
11025d465952Smartinh }
11035d465952Smartinh 
11045d465952Smartinh int
interface(const char * s,const char * cert,struct listenerlist * al,in_port_t port,u_int8_t flags)11055d465952Smartinh interface(const char *s, const char *cert,
110606cb3bdbSrob     struct listenerlist *al, in_port_t port, u_int8_t flags)
11075d465952Smartinh {
11085d465952Smartinh 	int			 ret = 0;
11095d465952Smartinh 	struct ifaddrs		*ifap, *p;
11105d465952Smartinh 	struct sockaddr_in	*sain;
11115d465952Smartinh 	struct sockaddr_in6	*sin6;
11125d465952Smartinh 	struct listener		*h;
11135d465952Smartinh 
11145d465952Smartinh 	if (getifaddrs(&ifap) == -1)
11155d465952Smartinh 		fatal("getifaddrs");
11165d465952Smartinh 
11175d465952Smartinh 	for (p = ifap; p != NULL; p = p->ifa_next) {
11185d465952Smartinh 		if (strcmp(s, p->ifa_name) != 0)
11195d465952Smartinh 			continue;
11208435d35aSbenno 		if (p->ifa_addr == NULL)
11218435d35aSbenno 			continue;
11225d465952Smartinh 
11235d465952Smartinh 		switch (p->ifa_addr->sa_family) {
11245d465952Smartinh 		case AF_INET:
11255d465952Smartinh 			if ((h = calloc(1, sizeof(*h))) == NULL)
11265d465952Smartinh 				fatal(NULL);
11275d465952Smartinh 			sain = (struct sockaddr_in *)&h->ss;
11285d465952Smartinh 			*sain = *(struct sockaddr_in *)p->ifa_addr;
11295d465952Smartinh 			sain->sin_len = sizeof(struct sockaddr_in);
11305d465952Smartinh 			sain->sin_port = port;
11315d465952Smartinh 
11325d465952Smartinh 			h->fd = -1;
11335d465952Smartinh 			h->port = port;
11345d465952Smartinh 			h->flags = flags;
11355d465952Smartinh 			h->ssl = NULL;
11365d465952Smartinh 			h->ssl_cert_name[0] = '\0';
11375d465952Smartinh 			if (cert != NULL)
11385d465952Smartinh 				(void)strlcpy(h->ssl_cert_name, cert, sizeof(h->ssl_cert_name));
11395d465952Smartinh 
11405d465952Smartinh 			ret = 1;
11415d465952Smartinh 			TAILQ_INSERT_HEAD(al, h, entry);
11425d465952Smartinh 
11435d465952Smartinh 			break;
11445d465952Smartinh 
11455d465952Smartinh 		case AF_INET6:
11465d465952Smartinh 			if ((h = calloc(1, sizeof(*h))) == NULL)
11475d465952Smartinh 				fatal(NULL);
11485d465952Smartinh 			sin6 = (struct sockaddr_in6 *)&h->ss;
11495d465952Smartinh 			*sin6 = *(struct sockaddr_in6 *)p->ifa_addr;
11505d465952Smartinh 			sin6->sin6_len = sizeof(struct sockaddr_in6);
11515d465952Smartinh 			sin6->sin6_port = port;
11525d465952Smartinh 
11535d465952Smartinh 			h->fd = -1;
11545d465952Smartinh 			h->port = port;
11555d465952Smartinh 			h->flags = flags;
11565d465952Smartinh 			h->ssl = NULL;
11575d465952Smartinh 			h->ssl_cert_name[0] = '\0';
11585d465952Smartinh 			if (cert != NULL)
11595d465952Smartinh 				(void)strlcpy(h->ssl_cert_name, cert, sizeof(h->ssl_cert_name));
11605d465952Smartinh 
11615d465952Smartinh 			ret = 1;
11625d465952Smartinh 			TAILQ_INSERT_HEAD(al, h, entry);
11635d465952Smartinh 
11645d465952Smartinh 			break;
11655d465952Smartinh 		}
11665d465952Smartinh 	}
11675d465952Smartinh 
11685d465952Smartinh 	freeifaddrs(ifap);
11695d465952Smartinh 
11705d465952Smartinh 	return ret;
11715d465952Smartinh }
11725d465952Smartinh 
11735d465952Smartinh static struct aci *
mk_aci(int type,int rights,enum scope scope,char * target,char * attr,char * subject)1174841a002bSreyk mk_aci(int type, int rights, enum scope scope, char *target, char *attr,
1175841a002bSreyk     char *subject)
11765d465952Smartinh {
11775d465952Smartinh 	struct aci	*aci;
11785d465952Smartinh 
11795d465952Smartinh 	if ((aci = calloc(1, sizeof(*aci))) == NULL) {
11805d465952Smartinh 		yyerror("calloc");
11815d465952Smartinh 		return NULL;
11825d465952Smartinh 	}
11835d465952Smartinh 	aci->type = type;
11845d465952Smartinh 	aci->rights = rights;
11855d465952Smartinh 	aci->scope = scope;
11865d465952Smartinh 	aci->target = target;
1187841a002bSreyk 	aci->attribute = attr;
11885d465952Smartinh 	aci->subject = subject;
11895d465952Smartinh 
1190841a002bSreyk 	log_debug("%s %02X access to %s%s%s scope %d by %s",
11915d465952Smartinh 	    aci->type == ACI_DENY ? "deny" : "allow",
11925d465952Smartinh 	    aci->rights,
11933b4314efSdaniel 	    aci->target ? aci->target : "any",
1194841a002bSreyk 	    aci->attribute ? " attribute " : "",
1195841a002bSreyk 	    aci->attribute ? aci->attribute : "",
11965d465952Smartinh 	    aci->scope,
11973b4314efSdaniel 	    aci->subject ? aci->subject : "any");
11985d465952Smartinh 
11995d465952Smartinh 	return aci;
12005d465952Smartinh }
12015d465952Smartinh 
1202f5f15bc0Smartinh struct namespace *
namespace_new(const char * suffix)1203f5f15bc0Smartinh namespace_new(const char *suffix)
1204f5f15bc0Smartinh {
1205f5f15bc0Smartinh 	struct namespace		*ns;
1206f5f15bc0Smartinh 
1207f5f15bc0Smartinh 	if ((ns = calloc(1, sizeof(*ns))) == NULL)
1208f5f15bc0Smartinh 		return NULL;
1209f5f15bc0Smartinh 	ns->sync = 1;
121085e920f6Smartinh 	ns->cache_size = 1024;
121185e920f6Smartinh 	ns->index_cache_size = 512;
1212d142e1faSclaudio 	ns->suffix = strdup(suffix);
1213f5f15bc0Smartinh 	if (ns->suffix == NULL) {
1214f5f15bc0Smartinh 		free(ns->suffix);
1215f5f15bc0Smartinh 		free(ns);
1216f5f15bc0Smartinh 		return NULL;
1217f5f15bc0Smartinh 	}
1218d142e1faSclaudio 	normalize_dn(ns->suffix);
1219f5f15bc0Smartinh 	TAILQ_INIT(&ns->indices);
1220f5f15bc0Smartinh 	TAILQ_INIT(&ns->request_queue);
1221f5f15bc0Smartinh 	SIMPLEQ_INIT(&ns->acl);
122238c09006Smartinh 	SLIST_INIT(&ns->referrals);
1223f5f15bc0Smartinh 
1224f5f15bc0Smartinh 	return ns;
1225f5f15bc0Smartinh }
1226f5f15bc0Smartinh 
122757f46873Sjmatthew int
ssl_cmp(struct ssl * s1,struct ssl * s2)122857f46873Sjmatthew ssl_cmp(struct ssl *s1, struct ssl *s2)
122957f46873Sjmatthew {
123057f46873Sjmatthew 	return (strcmp(s1->ssl_name, s2->ssl_name));
123157f46873Sjmatthew }
123257f46873Sjmatthew 
123357f46873Sjmatthew int
load_certfile(struct ldapd_config * env,const char * name,u_int8_t flags,u_int8_t protocol)1234bf448d9dStb load_certfile(struct ldapd_config *env, const char *name, u_int8_t flags,
1235bf448d9dStb     u_int8_t protocol)
123657f46873Sjmatthew {
123757f46873Sjmatthew 	struct ssl	*s;
123857f46873Sjmatthew 	struct ssl	 key;
123957f46873Sjmatthew 	char		 certfile[PATH_MAX];
1240bf448d9dStb 	uint32_t	 tls_protocols = TLS_PROTOCOLS_DEFAULT;
1241bf448d9dStb 	const char	*tls_ciphers = "default";
124257f46873Sjmatthew 
124357f46873Sjmatthew 	if (strlcpy(key.ssl_name, name, sizeof(key.ssl_name))
124457f46873Sjmatthew 	    >= sizeof(key.ssl_name)) {
124557f46873Sjmatthew 		log_warn("load_certfile: certificate name truncated");
124657f46873Sjmatthew 		return -1;
124757f46873Sjmatthew 	}
124857f46873Sjmatthew 
124957f46873Sjmatthew 	s = SPLAY_FIND(ssltree, env->sc_ssl, &key);
125057f46873Sjmatthew 	if (s != NULL) {
125157f46873Sjmatthew 		s->flags |= flags;
125257f46873Sjmatthew 		return 0;
125357f46873Sjmatthew 	}
125457f46873Sjmatthew 
125557f46873Sjmatthew 	if ((s = calloc(1, sizeof(*s))) == NULL)
125657f46873Sjmatthew 		fatal(NULL);
125757f46873Sjmatthew 
125857f46873Sjmatthew 	s->flags = flags;
125957f46873Sjmatthew 	(void)strlcpy(s->ssl_name, key.ssl_name, sizeof(s->ssl_name));
126057f46873Sjmatthew 
126157f46873Sjmatthew 	s->config = tls_config_new();
126257f46873Sjmatthew 	if (s->config == NULL)
126357f46873Sjmatthew 		goto err;
126457f46873Sjmatthew 
1265bf448d9dStb 	if (protocol & F_LEGACY) {
1266bf448d9dStb 		tls_protocols = TLS_PROTOCOLS_ALL;
1267bf448d9dStb 		tls_ciphers = "all";
1268bf448d9dStb 	}
1269bf448d9dStb 	if (tls_config_set_protocols(s->config, tls_protocols) != 0) {
1270aeb8fb1fSmestre 		log_warn("load_certfile: failed to set tls protocols: %s",
1271aeb8fb1fSmestre 		    tls_config_error(s->config));
1272aeb8fb1fSmestre 		goto err;
1273aeb8fb1fSmestre 	}
1274bf448d9dStb 	if (tls_config_set_ciphers(s->config, tls_ciphers)) {
127557f46873Sjmatthew 		log_warn("load_certfile: failed to set tls ciphers: %s",
127657f46873Sjmatthew 		    tls_config_error(s->config));
127757f46873Sjmatthew 		goto err;
127857f46873Sjmatthew 	}
127957f46873Sjmatthew 
1280f8c23ab2Stb 	if (name[0] == '/') {
1281f8c23ab2Stb 		if (!bsnprintf(certfile, sizeof(certfile), "%s.crt", name)) {
128257f46873Sjmatthew 			log_warn("load_certfile: path truncated");
128357f46873Sjmatthew 			goto err;
128457f46873Sjmatthew 		}
1285f8c23ab2Stb 	} else {
1286f8c23ab2Stb 		if (!bsnprintf(certfile, sizeof(certfile),
1287f8c23ab2Stb 		    "/etc/ldap/certs/%s.crt", name)) {
1288f8c23ab2Stb 			log_warn("load_certfile: path truncated");
1289f8c23ab2Stb 			goto err;
1290f8c23ab2Stb 		}
1291f8c23ab2Stb 	}
129257f46873Sjmatthew 
129357f46873Sjmatthew 	log_debug("loading certificate file %s", certfile);
129457f46873Sjmatthew 	s->ssl_cert = tls_load_file(certfile, &s->ssl_cert_len, NULL);
129557f46873Sjmatthew 	if (s->ssl_cert == NULL)
129657f46873Sjmatthew 		goto err;
129757f46873Sjmatthew 
129857f46873Sjmatthew 	if (tls_config_set_cert_mem(s->config, s->ssl_cert, s->ssl_cert_len)) {
129957f46873Sjmatthew 		log_warn("load_certfile: failed to set tls certificate: %s",
130057f46873Sjmatthew 		    tls_config_error(s->config));
130157f46873Sjmatthew 		goto err;
130257f46873Sjmatthew 	}
130357f46873Sjmatthew 
1304f8c23ab2Stb 	if (name[0] == '/') {
1305f8c23ab2Stb 		if (!bsnprintf(certfile, sizeof(certfile), "%s.key", name)) {
130657f46873Sjmatthew 			log_warn("load_certfile: path truncated");
130757f46873Sjmatthew 			goto err;
130857f46873Sjmatthew 		}
1309f8c23ab2Stb 	} else {
1310f8c23ab2Stb 		if (!bsnprintf(certfile, sizeof(certfile),
1311f8c23ab2Stb 		    "/etc/ldap/certs/%s.key", name)) {
1312f8c23ab2Stb 			log_warn("load_certfile: path truncated");
1313f8c23ab2Stb 			goto err;
1314f8c23ab2Stb 		}
1315f8c23ab2Stb 	}
131657f46873Sjmatthew 
131757f46873Sjmatthew 	log_debug("loading key file %s", certfile);
131857f46873Sjmatthew 	s->ssl_key = tls_load_file(certfile, &s->ssl_key_len, NULL);
131957f46873Sjmatthew 	if (s->ssl_key == NULL)
132057f46873Sjmatthew 		goto err;
132157f46873Sjmatthew 
132257f46873Sjmatthew 	if (tls_config_set_key_mem(s->config, s->ssl_key, s->ssl_key_len)) {
132357f46873Sjmatthew 		log_warn("load_certfile: failed to set tls key: %s",
132457f46873Sjmatthew 		    tls_config_error(s->config));
132557f46873Sjmatthew 		goto err;
132657f46873Sjmatthew 	}
132757f46873Sjmatthew 
132857f46873Sjmatthew 	SPLAY_INSERT(ssltree, env->sc_ssl, s);
132957f46873Sjmatthew 
133057f46873Sjmatthew 	return (0);
133157f46873Sjmatthew err:
133257f46873Sjmatthew 	free(s->ssl_cert);
133357f46873Sjmatthew 	free(s->ssl_key);
133457f46873Sjmatthew 	tls_config_free(s->config);
133557f46873Sjmatthew 	free(s);
133657f46873Sjmatthew 	return (-1);
133757f46873Sjmatthew }
1338