config.c revision 223327
1274079Sngie/*-
2272343Sngie * Copyright (c) 2011 James Gritton
3272343Sngie * All rights reserved.
4272343Sngie *
5272343Sngie * Redistribution and use in source and binary forms, with or without
6272343Sngie * modification, are permitted provided that the following conditions
7272343Sngie * are met:
8272343Sngie * 1. Redistributions of source code must retain the above copyright
9272343Sngie *    notice, this list of conditions and the following disclaimer.
10272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer in the
12272343Sngie *    documentation and/or other materials provided with the distribution.
13272343Sngie *
14272343Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15272343Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16272343Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17272343Sngie * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18272343Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20272343Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21272343Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22272343Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23272343Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24272343Sngie * SUCH DAMAGE.
25272343Sngie */
26272343Sngie
27272343Sngie#include <sys/cdefs.h>
28272343Sngie__FBSDID("$FreeBSD: projects/jailconf/usr.sbin/jail/config.c 223327 2011-06-20 07:58:44Z jamie $");
29272343Sngie
30272343Sngie#include <sys/types.h>
31272343Sngie#include <sys/errno.h>
32274079Sngie#include <sys/socket.h>
33272343Sngie#include <sys/sysctl.h>
34272343Sngie
35272343Sngie#include <arpa/inet.h>
36272343Sngie#include <netinet/in.h>
37272343Sngie
38272343Sngie#include <err.h>
39272343Sngie#include <netdb.h>
40272343Sngie#include <stdio.h>
41272343Sngie#include <stdlib.h>
42272343Sngie#include <string.h>
43272343Sngie
44272343Sngie#include "jailp.h"
45272343Sngie
46272343Sngiestruct ipspec {
47272343Sngie	const char	*name;
48272343Sngie	unsigned	flags;
49272343Sngie};
50272343Sngie
51272343Sngieextern FILE *yyin;
52272343Sngieextern int yynerrs;
53272343Sngie
54272343Sngiestruct cfjails cfjails = TAILQ_HEAD_INITIALIZER(cfjails);
55272343Sngie
56272343Sngiestatic void free_param(struct cfparams *pp, struct cfparam *p);
57272343Sngiestatic void free_param_strings(struct cfparam *p);
58272343Sngie
59272343Sngiestatic const struct ipspec intparams[] = {
60272343Sngie    [IP_ALLOW_DYING] =		{"allow.dying",		PF_INTERNAL | PF_BOOL},
61272343Sngie    [IP_COMMAND] =		{"command",		PF_INTERNAL},
62272343Sngie    [IP_DEPEND] =		{"depend",		PF_INTERNAL},
63272343Sngie    [IP_EXEC_CLEAN] =		{"exec.clean",		PF_INTERNAL | PF_BOOL},
64272343Sngie    [IP_EXEC_CONSOLELOG] =	{"exec.consolelog",	PF_INTERNAL},
65272343Sngie    [IP_EXEC_FIB] =		{"exec.fib",		PF_INTERNAL | PF_INT},
66272343Sngie    [IP_EXEC_JAIL_USER] =	{"exec.jail_user",	PF_INTERNAL},
67272343Sngie    [IP_EXEC_POSTSTART] =	{"exec.poststart",	PF_INTERNAL},
68272343Sngie    [IP_EXEC_POSTSTOP] =	{"exec.poststop",	PF_INTERNAL},
69272343Sngie    [IP_EXEC_PRESTART] =	{"exec.prestart",	PF_INTERNAL},
70272343Sngie    [IP_EXEC_PRESTOP] =		{"exec.prestop",	PF_INTERNAL},
71272343Sngie    [IP_EXEC_START] =		{"exec.start",		PF_INTERNAL},
72272343Sngie    [IP_EXEC_STOP] =		{"exec.stop",		PF_INTERNAL},
73272343Sngie    [IP_EXEC_SYSTEM_JAIL_USER]=	{"exec.system_jail_user",
74272343Sngie							PF_INTERNAL | PF_BOOL},
75272343Sngie    [IP_EXEC_SYSTEM_USER] =	{"exec.system_user",	PF_INTERNAL},
76272343Sngie    [IP_EXEC_TIMEOUT] =		{"exec.timeout",	PF_INTERNAL | PF_INT},
77272343Sngie    [IP_INTERFACE] =		{"interface",		PF_INTERNAL},
78272343Sngie    [IP_IP_HOSTNAME] =		{"ip_hostname",		PF_INTERNAL | PF_BOOL},
79272343Sngie    [IP_MOUNT] =		{"mount",		PF_INTERNAL},
80272343Sngie    [IP_MOUNT_DEVFS] =		{"mount.devfs",		PF_INTERNAL | PF_BOOL},
81272343Sngie    [IP_MOUNT_DEVFS_RULESET]=	{"mount.devfs.ruleset",	PF_INTERNAL},
82272343Sngie    [IP_MOUNT_FSTAB] =		{"mount.fstab",		PF_INTERNAL},
83272343Sngie    [IP_STOP_TIMEOUT] =		{"stop.timeout",	PF_INTERNAL | PF_INT},
84272343Sngie    [IP_VNET_INTERFACE] =	{"vnet.interface",	PF_INTERNAL},
85272343Sngie    [IP__IP4_IFADDR] =		{"ip4.addr",		PF_INTERNAL | PF_CONV},
86272343Sngie#ifdef INET6
87272343Sngie    [IP__IP6_IFADDR] =		{"ip6.addr",		PF_INTERNAL | PF_CONV},
88272343Sngie#endif
89272343Sngie    [IP__MOUNT_FROM_FSTAB] =	{"mount.fstab",		PF_INTERNAL | PF_CONV},
90272343Sngie    [IP__OP] =			{NULL,			PF_CONV},
91272343Sngie    [KP_ALLOW_CHFLAGS] =	{"allow.chflags",	0},
92272343Sngie    [KP_ALLOW_MOUNT] =		{"allow.mount",		0},
93272343Sngie    [KP_ALLOW_RAW_SOCKETS] =	{"allow.raw_sockets",	0},
94272343Sngie    [KP_ALLOW_SET_HOSTNAME]=	{"allow.set_hostname",	0},
95272343Sngie    [KP_ALLOW_SOCKET_AF] =	{"allow.socket_af",	0},
96272343Sngie    [KP_ALLOW_SYSVIPC] =	{"allow.sysvipc",	0},
97272343Sngie    [KP_ENFORCE_STATFS] =	{"enforce_statfs",	0},
98272343Sngie    [KP_HOST_HOSTNAME] =	{"host.hostname",	0},
99272343Sngie    [KP_IP4_ADDR] =		{"ip4.addr",		0},
100272343Sngie#ifdef INET6
101272343Sngie    [KP_IP6_ADDR] =		{"ip6.addr",		0},
102272343Sngie#endif
103272343Sngie    [KP_JID] =			{"jid",			0},
104272343Sngie    [KP_NAME] =			{"name",		0},
105272343Sngie    [KP_PATH] =			{"path",		0},
106272343Sngie    [KP_PERSIST] =		{"persist",		0},
107272343Sngie    [KP_SECURELEVEL] =		{"securelevel",		0},
108272343Sngie    [KP_VNET] =			{"vnet",		0},
109272343Sngie};
110272343Sngie
111272343Sngie/*
112272343Sngie * Parse the jail configuration file.
113272343Sngie */
114272343Sngievoid
115272343Sngieload_config(void)
116272343Sngie{
117272343Sngie	struct cfjails wild;
118272343Sngie	struct cfparams opp;
119272343Sngie	struct cfjail *j, *tj, *wj;
120272343Sngie	struct cfparam *p, *vp, *tp;
121272343Sngie	struct cfstring *s, *vs, *ns;
122272343Sngie	struct cfvar *v;
123272343Sngie	char *ep;
124272343Sngie	size_t varoff;
125272343Sngie	int did_self, jseq, pgen;
126272343Sngie
127272343Sngie	if (!strcmp(cfname, "-")) {
128272343Sngie		cfname = "STDIN";
129272343Sngie		yyin = stdin;
130272343Sngie	} else {
131272343Sngie		yyin = fopen(cfname, "r");
132272343Sngie		if (!yyin)
133272343Sngie			err(1, "%s", cfname);
134272343Sngie	}
135272343Sngie	if (yyparse() || yynerrs)
136272343Sngie		exit(1);
137272343Sngie
138272343Sngie	/* Separate the wildcard jails out from the actual jails. */
139272343Sngie	jseq = 0;
140272343Sngie	TAILQ_INIT(&wild);
141272343Sngie	TAILQ_FOREACH_SAFE(j, &cfjails, tq, tj) {
142272343Sngie		j->seq = ++jseq;
143272343Sngie		if (wild_jail_name(j->name))
144272343Sngie			requeue(j, &wild);
145272343Sngie	}
146272343Sngie
147272343Sngie	TAILQ_FOREACH(j, &cfjails, tq) {
148272343Sngie		/* Set aside the jail's parameters. */
149272343Sngie		TAILQ_INIT(&opp);
150272343Sngie		TAILQ_CONCAT(&opp, &j->params, tq);
151272343Sngie		/*
152272343Sngie		 * The jail name implies its "name" or "jid" parameter,
153272343Sngie		 * though they may also be explicitly set later on.
154272343Sngie		 */
155272343Sngie		add_param(j, NULL,
156272343Sngie		    strtol(j->name, &ep, 10) && !*ep ? KP_JID : KP_NAME,
157272343Sngie		    j->name);
158272343Sngie		/*
159272343Sngie		 * Collect parameters for the jail, global parameters/variables,
160272343Sngie		 * and any matching wildcard jails.
161272343Sngie		 */
162272343Sngie		did_self = 0;
163272343Sngie		TAILQ_FOREACH(wj, &wild, tq) {
164272343Sngie			if (j->seq < wj->seq && !did_self) {
165272343Sngie				TAILQ_FOREACH(p, &opp, tq)
166272343Sngie					add_param(j, p, 0, NULL);
167272343Sngie				did_self = 1;
168272343Sngie			}
169272343Sngie			if (wild_jail_match(j->name, wj->name))
170272343Sngie				TAILQ_FOREACH(p, &wj->params, tq)
171272343Sngie					add_param(j, p, 0, NULL);
172272343Sngie		}
173272343Sngie		if (!did_self)
174272343Sngie			TAILQ_FOREACH(p, &opp, tq)
175272343Sngie				add_param(j, p, 0, NULL);
176272343Sngie
177272343Sngie		/* Resolve any variable substitutions. */
178272343Sngie		pgen = 0;
179272343Sngie		TAILQ_FOREACH(p, &j->params, tq) {
180272343Sngie		    p->gen = ++pgen;
181272343Sngie		find_vars:
182272343Sngie		    TAILQ_FOREACH(s, &p->val, tq) {
183272343Sngie			varoff = 0;
184272343Sngie			while ((v = STAILQ_FIRST(&s->vars))) {
185272343Sngie				TAILQ_FOREACH(vp, &j->params, tq)
186272343Sngie					if (!strcmp(vp->name, v->name))
187272343Sngie						break;
188272343Sngie				if (!vp) {
189272343Sngie					jail_warnx(j,
190272343Sngie					    "%s: variable \"%s\" not found",
191272343Sngie					    p->name, v->name);
192272343Sngie				bad_var:
193272343Sngie					j->flags |= JF_FAILED;
194272343Sngie					TAILQ_FOREACH(vp, &j->params, tq)
195272343Sngie						if (vp->gen == pgen)
196272343Sngie							vp->flags |= PF_BAD;
197272343Sngie					goto free_var;
198272343Sngie				}
199272343Sngie				if (vp->flags & PF_BAD)
200272343Sngie					goto bad_var;
201272343Sngie				if (vp->gen == pgen) {
202272343Sngie					jail_warnx(j, "%s: variable loop",
203272343Sngie					    v->name);
204272343Sngie					goto bad_var;
205				}
206				TAILQ_FOREACH(vs, &vp->val, tq)
207					if (!STAILQ_EMPTY(&vs->vars)) {
208						vp->gen = pgen;
209						TAILQ_REMOVE(&j->params, vp,
210						    tq);
211						TAILQ_INSERT_BEFORE(p, vp, tq);
212						p = vp;
213						goto find_vars;
214					}
215				vs = TAILQ_FIRST(&vp->val);
216				if (TAILQ_NEXT(vs, tq) != NULL &&
217				    (s->s[0] != '\0' ||
218				     STAILQ_NEXT(v, tq))) {
219					jail_warnx(j, "%s: array cannot be "
220					    "substituted inline",
221					    p->name);
222					goto bad_var;
223				}
224				s->s = erealloc(s->s, s->len + vs->len + 1);
225				memmove(s->s + v->pos + varoff + vs->len,
226				    s->s + v->pos + varoff,
227				    s->len - (v->pos + varoff) + 1);
228				memcpy(s->s + v->pos + varoff, vs->s, vs->len);
229				varoff += vs->len;
230				s->len += vs->len;
231				while ((vs = TAILQ_NEXT(vs, tq))) {
232					ns = emalloc(sizeof(struct cfstring));
233					ns->s = estrdup(vs->s);
234					ns->len = vs->len;
235					STAILQ_INIT(&ns->vars);
236					TAILQ_INSERT_AFTER(&p->val, s, ns, tq);
237					s = ns;
238				}
239			free_var:
240				free(v->name);
241				STAILQ_REMOVE_HEAD(&s->vars, tq);
242				free(v);
243			}
244		    }
245		}
246
247		/* Free the jail's original parameter list and any variables. */
248		while ((p = TAILQ_FIRST(&opp)))
249			free_param(&opp, p);
250		TAILQ_FOREACH_SAFE(p, &j->params, tq, tp)
251			if (p->flags & PF_VAR)
252				free_param(&j->params, p);
253	}
254	while ((wj = TAILQ_FIRST(&wild))) {
255		free(wj->name);
256		while ((p = TAILQ_FIRST(&wj->params)))
257			free_param(&wj->params, p);
258		TAILQ_REMOVE(&wild, wj, tq);
259	}
260}
261
262/*
263 * Create a new jail record.
264 */
265struct cfjail *
266add_jail(void)
267{
268	struct cfjail *j;
269
270	j = emalloc(sizeof(struct cfjail));
271	memset(j, 0, sizeof(struct cfjail));
272	TAILQ_INIT(&j->params);
273	STAILQ_INIT(&j->dep[DEP_FROM]);
274	STAILQ_INIT(&j->dep[DEP_TO]);
275	j->queue = &cfjails;
276	TAILQ_INSERT_TAIL(&cfjails, j, tq);
277	return j;
278}
279
280/*
281 * Add a parameter to a jail.
282 */
283void
284add_param(struct cfjail *j, const struct cfparam *p, enum intparam ipnum,
285    const char *value)
286{
287	struct cfstrings nss;
288	struct cfparam *dp, *np;
289	struct cfstring *s, *ns;
290	struct cfvar *v, *nv;
291	const char *name;
292	char *cs, *tname;
293	unsigned flags;
294
295	if (j == NULL) {
296		/* Create a single anonymous jail if one doesn't yet exist. */
297		j = TAILQ_LAST(&cfjails, cfjails);
298		if (j == NULL)
299			j = add_jail();
300	}
301	TAILQ_INIT(&nss);
302	if (p != NULL) {
303		name = p->name;
304		flags = p->flags;
305		/*
306		 * Make a copy of the parameter's string list,
307		 * which may be freed if it's overridden later.
308		 */
309		TAILQ_FOREACH(s, &p->val, tq) {
310			ns = emalloc(sizeof(struct cfstring));
311			ns->s = estrdup(s->s);
312			ns->len = s->len;
313			STAILQ_INIT(&ns->vars);
314			STAILQ_FOREACH(v, &s->vars, tq) {
315				nv = emalloc(sizeof(struct cfvar));
316				nv->name = strdup(v->name);
317				nv->pos = v->pos;
318				STAILQ_INSERT_TAIL(&ns->vars, nv, tq);
319			}
320			TAILQ_INSERT_TAIL(&nss, ns, tq);
321		}
322	} else {
323		flags = PF_APPEND;
324		if (ipnum != 0) {
325			name = intparams[ipnum].name;
326			flags |= intparams[ipnum].flags;
327		} else if ((cs = strchr(value, '='))) {
328			tname = alloca(cs - value + 1);
329			strlcpy(tname, value, cs - value + 1);
330			name = tname;
331			value = cs + 1;
332		} else {
333			name = value;
334			value = NULL;
335		}
336		if (value != NULL) {
337			ns = emalloc(sizeof(struct cfstring));
338			ns->s = estrdup(value);
339			ns->len = strlen(value);
340			STAILQ_INIT(&ns->vars);
341			TAILQ_INSERT_TAIL(&nss, ns, tq);
342		}
343	}
344
345	/* See if this parameter has already been added. */
346	if (ipnum != 0)
347		dp = j->intparams[ipnum];
348	else
349		TAILQ_FOREACH(dp, &j->params, tq)
350			if (!(dp->flags & PF_CONV) && equalopts(dp->name, name))
351				break;
352	if (dp != NULL) {
353		/* Found it - append or replace. */
354		if (strcmp(dp->name, name)) {
355			free(dp->name);
356			dp->name = estrdup(name);
357		}
358		if (!(flags & PF_APPEND) || TAILQ_EMPTY(&nss))
359			free_param_strings(dp);
360		TAILQ_CONCAT(&dp->val, &nss, tq);
361		dp->flags |= flags;
362	} else {
363		/* Not found - add it. */
364		np = emalloc(sizeof(struct cfparam));
365		np->name = estrdup(name);
366		TAILQ_INIT(&np->val);
367		TAILQ_CONCAT(&np->val, &nss, tq);
368		np->flags = flags;
369		np->gen = 0;
370		TAILQ_INSERT_TAIL(&j->params, np, tq);
371		if (ipnum != 0)
372			j->intparams[ipnum] = np;
373		else
374			for (ipnum = 1; ipnum < IP_NPARAM; ipnum++)
375				if (!(intparams[ipnum].flags & PF_CONV) &&
376				    equalopts(name, intparams[ipnum].name)) {
377					j->intparams[ipnum] = np;
378					np->flags |= intparams[ipnum].flags;
379					break;
380				}
381	}
382}
383
384/*
385 * Return if a boolean parameter exists and is true.
386 */
387int
388bool_param(const struct cfparam *p)
389{
390	const char *cs;
391
392	if (p == NULL)
393		return 0;
394	cs = strrchr(p->name, '.');
395	return !strncmp(cs ? cs + 1 : p->name, "no", 2) ^
396	    (TAILQ_EMPTY(&p->val) ||
397	     !strcasecmp(TAILQ_LAST(&p->val, cfstrings)->s, "true") ||
398	     (strtol(TAILQ_LAST(&p->val, cfstrings)->s, NULL, 10)));
399}
400
401/*
402 * Set an integer if a parameter if it exists.
403 */
404int
405int_param(const struct cfparam *p, int *ip)
406{
407	if (p == NULL || TAILQ_EMPTY(&p->val))
408		return 0;
409	*ip = strtol(TAILQ_LAST(&p->val, cfstrings)->s, NULL, 10);
410	return 1;
411}
412
413/*
414 * Return the string value of a scalar parameter if it exists.
415 */
416const char *
417string_param(const struct cfparam *p)
418{
419	return (p && !TAILQ_EMPTY(&p->val)
420	    ? TAILQ_LAST(&p->val, cfstrings)->s : NULL);
421}
422
423/*
424 * Check syntax and values of internal parameters.  Set some internal
425 * parameters based on the values of others.
426 */
427int
428check_intparams(struct cfjail *j)
429{
430	struct in_addr addr4;
431	struct addrinfo hints;
432	struct addrinfo *ai0, *ai;
433	struct cfparam *p;
434	struct cfstring *s;
435	FILE *f;
436	const char *hostname, *val;
437	char *cs, *ep, *ln;
438	size_t size, lnlen;
439	int error, gicode, ip4ok, defif, prefix;
440	int mib[4];
441	char avalue4[INET_ADDRSTRLEN];
442#ifdef INET6
443	struct in6_addr addr6;
444	int ip6ok, isip6;
445	char avalue6[INET6_ADDRSTRLEN];
446#endif
447
448	error = 0;
449	/* Check format of boolan and integer values. */
450	TAILQ_FOREACH(p, &j->params, tq) {
451		if (!TAILQ_EMPTY(&p->val) && (p->flags & (PF_BOOL | PF_INT))) {
452			val = TAILQ_LAST(&p->val, cfstrings)->s;
453			if (p->flags & PF_BOOL) {
454				if (strcasecmp(val, "false") &&
455				    strcasecmp(val, "true") &&
456				    ((void)strtol(val, &ep, 10), *ep)) {
457					jail_warnx(j,
458					    "%s: unknown boolean value \"%s\"",
459					    p->name, val);
460					error = -1;
461				}
462			} else {
463				(void)strtol(val, &ep, 10);
464				if (ep == val || *ep) {
465					jail_warnx(j,
466					    "%s: non-integer value \"%s\"",
467					    p->name, val);
468					error = -1;
469				}
470			}
471		}
472	}
473
474	/*
475	 * The ip_hostname parameter looks up the hostname, and adds parameters
476	 * for any IP addresses it finds.
477	 */
478	if (((j->flags & JF_OP_MASK) != JF_STOP ||
479	    j->intparams[IP_INTERFACE] != NULL) &&
480	    bool_param(j->intparams[IP_IP_HOSTNAME]) &&
481	    (hostname = string_param(j->intparams[KP_HOST_HOSTNAME]))) {
482		j->intparams[IP_IP_HOSTNAME] = NULL;
483		/*
484		 * Silently ignore unsupported address families from
485		 * DNS lookups.
486		 */
487		size = 4;
488		ip4ok = sysctlnametomib("security.jail.param.ip4", mib, &size)
489		    == 0;
490#ifdef INET6
491		size = 4;
492		ip6ok = sysctlnametomib("security.jail.param.ip6", mib, &size)
493		    == 0;
494#endif
495		if (ip4ok
496#ifdef INET6
497		    || ip6ok
498#endif
499			    ) {
500			/* Look up the hostname (or get the address) */
501			memset(&hints, 0, sizeof(hints));
502			hints.ai_socktype = SOCK_STREAM;
503			hints.ai_family =
504#ifdef INET6
505			    ip6ok ? (ip4ok ? PF_UNSPEC : PF_INET6) :
506#endif
507			    PF_INET;
508			gicode = getaddrinfo(hostname, NULL, &hints, &ai0);
509			if (gicode != 0) {
510				jail_warnx(j, "host.hostname %s: %s", hostname,
511				    gai_strerror(gicode));
512				error = -1;
513			} else {
514				/*
515				 * Convert the addresses to ASCII so jailparam
516				 * can convert them back.  Errors are not
517				 * expected here.
518				 */
519				for (ai = ai0; ai; ai = ai->ai_next)
520					switch (ai->ai_family) {
521					case AF_INET:
522						memcpy(&addr4,
523						    &((struct sockaddr_in *)
524						    (void *)ai->ai_addr)->
525						    sin_addr, sizeof(addr4));
526						if (inet_ntop(AF_INET,
527						    &addr4, avalue4,
528						    INET_ADDRSTRLEN) == NULL)
529							err(1, "inet_ntop");
530						add_param(j, NULL, KP_IP4_ADDR,
531						    avalue4);
532						break;
533#ifdef INET6
534					case AF_INET6:
535						memcpy(&addr6,
536						    &((struct sockaddr_in6 *)
537						    (void *)ai->ai_addr)->
538						    sin6_addr, sizeof(addr6));
539						if (inet_ntop(AF_INET6,
540						    &addr6, avalue6,
541						    INET6_ADDRSTRLEN) == NULL)
542							err(1, "inet_ntop");
543						add_param(j, NULL, KP_IP6_ADDR,
544						    avalue6);
545						break;
546#endif
547					}
548				freeaddrinfo(ai0);
549			}
550		}
551	}
552
553	/*
554	 * IP addresses may include an interface to set that address on,
555	 * and a netmask/suffix for that address.
556	 */
557	defif = string_param(j->intparams[IP_INTERFACE]) != NULL;
558#ifdef INET6
559	for (isip6 = 0; isip6 <= 1; isip6++)
560#else
561#define isip6 0
562	do
563#endif
564	{
565		if (j->intparams[KP_IP4_ADDR + isip6] == NULL)
566			continue;
567		TAILQ_FOREACH(s, &j->intparams[KP_IP4_ADDR + isip6]->val, tq) {
568			cs = strchr(s->s, '|');
569			if (cs || defif)
570				add_param(j, NULL, IP__IP4_IFADDR + isip6,
571				    s->s);
572			if (cs) {
573				strcpy(s->s, cs + 1);
574				s->len -= cs + 1 - s->s;
575			}
576			if ((cs = strchr(s->s, '/'))) {
577				prefix = strtol(cs + 1, &ep, 10);
578				if (
579#ifdef INET6
580				    !isip6 &&
581#endif
582				    *ep == '.'
583				    ? inet_pton(AF_INET, cs + 1, &addr4) != 1
584				    : *ep || prefix < 0 || prefix > (
585#ifdef INET6
586				      isip6 ? 128 :
587#endif
588				      32)) {
589					jail_warnx(j,
590#ifdef INET6
591					    isip6
592					    ? "ip6.addr: bad prefixlen \"%s\"" :
593#endif
594					    "ip4.addr: bad netmask \"%s\"",
595					    cs);
596					error = -1;
597				}
598				*cs = '\0';
599				s->len = cs - s->s + 1;
600			}
601		}
602	}
603#ifndef INET6
604	while (0);
605#endif
606
607	/*
608	 * Read mount.fstab file(s), and treat each line as its own mount
609	 * parameter.
610	 */
611	if (j->intparams[IP_MOUNT_FSTAB] != NULL) {
612		TAILQ_FOREACH(s, &j->intparams[IP_MOUNT_FSTAB]->val, tq) {
613			if (s->len == 0)
614				continue;
615			f = fopen(s->s, "r");
616			if (f == NULL) {
617				jail_warnx(j, "mount.fstab: %s: %s",
618				    s->s, strerror(errno));
619				error = -1;
620				continue;
621			}
622			while ((ln = fgetln(f, &lnlen))) {
623				if ((cs = memchr(ln, '#', lnlen - 1)))
624					lnlen = cs - ln + 1;
625				if (ln[lnlen - 1] == '\n' ||
626				    ln[lnlen - 1] == '#')
627					ln[lnlen - 1] = '\0';
628				else {
629					cs = alloca(lnlen + 1);
630					strlcpy(cs, ln, lnlen + 1);
631					ln = cs;
632				}
633				add_param(j, NULL, IP__MOUNT_FROM_FSTAB, ln);
634			}
635			fclose(f);
636		}
637	}
638	if (error)
639		failed(j);
640	return error;
641}
642
643/*
644 * Import parameters into libjail's binary jailparam format.
645 */
646int
647import_params(struct cfjail *j)
648{
649	struct cfparam *p;
650	struct cfstring *s, *ts;
651	struct jailparam *jp;
652	char *value, *cs;
653	size_t vallen;
654	int error;
655
656	error = 0;
657	j->njp = 0;
658	TAILQ_FOREACH(p, &j->params, tq)
659		if (!(p->flags & PF_INTERNAL))
660			j->njp++;
661	j->jp = jp = emalloc(j->njp * sizeof(struct jailparam));
662	TAILQ_FOREACH(p, &j->params, tq) {
663		if (p->flags & PF_INTERNAL)
664			continue;
665		if (jailparam_init(jp, p->name) < 0) {
666			error = -1;
667			jail_warnx(j, "%s", jail_errmsg);
668			continue;
669		}
670		if (TAILQ_EMPTY(&p->val))
671			value = NULL;
672		else if (!jp->jp_elemlen ||
673			 !TAILQ_NEXT(TAILQ_FIRST(&p->val), tq)) {
674			/*
675			 * Scalar parameters silently discard multiple (array)
676			 * values, keeping only the last value added.  This
677			 * lets values added from the command line append to
678			 * arrays wthout pre-checking the type.
679			 */
680			value = TAILQ_LAST(&p->val, cfstrings)->s;
681		} else {
682			/*
683			 * Convert arrays into comma-separated strings, which
684			 * jailparam_import will then convert back into arrays.
685			 */
686			vallen = 0;
687			TAILQ_FOREACH(s, &p->val, tq)
688				vallen += s->len + 1;
689			value = alloca(vallen);
690			cs = value;
691			TAILQ_FOREACH_SAFE(s, &p->val, tq, ts) {
692				strcpy(cs, s->s);
693				if (ts != NULL) {
694					cs += s->len + 1;
695					cs[-1] = ',';
696				}
697			}
698		}
699		if (jailparam_import(jp, value) < 0) {
700			error = -1;
701			jail_warnx(j, "%s", jail_errmsg);
702		}
703		jp++;
704	}
705	if (error) {
706		jailparam_free(j->jp, j->njp);
707		free(j->jp);
708		j->jp = NULL;
709		failed(j);
710	}
711	return error;
712}
713
714/*
715 * Check if options are equal (with or without the "no" prefix).
716 */
717int
718equalopts(const char *opt1, const char *opt2)
719{
720	char *p;
721
722	/* "opt" vs. "opt" or "noopt" vs. "noopt" */
723	if (strcmp(opt1, opt2) == 0)
724		return (1);
725	/* "noopt" vs. "opt" */
726	if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
727		return (1);
728	/* "opt" vs. "noopt" */
729	if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
730		return (1);
731	while ((p = strchr(opt1, '.')) != NULL &&
732	    !strncmp(opt1, opt2, ++p - opt1)) {
733		opt2 += p - opt1;
734		opt1 = p;
735		/* "foo.noopt" vs. "foo.opt" */
736		if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
737			return (1);
738		/* "foo.opt" vs. "foo.noopt" */
739		if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
740			return (1);
741	}
742	return (0);
743}
744
745/*
746 * See if a jail name matches a wildcard.
747 */
748int
749wild_jail_match(const char *jname, const char *wname)
750{
751	const char *jc, *jd, *wc, *wd;
752
753	/*
754	 * A non-final "*" component in the wild name matches a single jail
755	 * component, and a final "*" matches one or more jail components.
756	 */
757	for (jc = jname, wc = wname;
758	     (jd = strchr(jc, '.')) && (wd = strchr(wc, '.'));
759	     jc = jd + 1, wc = wd + 1)
760		if (strncmp(jc, wc, jd - jc + 1) && strncmp(wc, "*.", 2))
761			return 0;
762	return (!strcmp(jc, wc) || !strcmp(wc, "*"));
763}
764
765/*
766 * Return if a jail name is a wildcard.
767 */
768int
769wild_jail_name(const char *wname)
770{
771	const char *wc;
772
773	for (wc = strchr(wname, '*'); wc; wc = strchr(wc + 1, '*'))
774		if ((wc == wname || wc[-1] == '.') &&
775		    (wc[1] == '\0' || wc[1] == '.'))
776			return 1;
777	return 0;
778}
779
780/*
781 * Free a parameter record and all its strings and variables.
782 */
783static void
784free_param(struct cfparams *pp, struct cfparam *p)
785{
786	free(p->name);
787	free_param_strings(p);
788	TAILQ_REMOVE(pp, p, tq);
789	free(p);
790}
791
792static void
793free_param_strings(struct cfparam *p)
794{
795	struct cfstring *s;
796	struct cfvar *v;
797
798	while ((s = TAILQ_FIRST(&p->val))) {
799		free(s->s);
800		while ((v = STAILQ_FIRST(&s->vars))) {
801			free(v->name);
802			STAILQ_REMOVE_HEAD(&s->vars, tq);
803			free(v);
804		}
805		TAILQ_REMOVE(&p->val, s, tq);
806		free(s);
807	}
808}
809