xref: /freebsd/usr.sbin/jail/config.c (revision 78b9f0095b4af3aca6c931b2c7b009ddb8a05125)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 James Gritton
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include <sys/errno.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
36 
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
39 
40 #include <err.h>
41 #include <netdb.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include "jailp.h"
48 
49 struct ipspec {
50 	const char	*name;
51 	unsigned	flags;
52 };
53 
54 extern FILE *yyin;
55 extern int yynerrs;
56 
57 extern int yyparse(void);
58 
59 struct cfjails cfjails = TAILQ_HEAD_INITIALIZER(cfjails);
60 
61 static void free_param(struct cfparams *pp, struct cfparam *p);
62 static void free_param_strings(struct cfparam *p);
63 
64 static const struct ipspec intparams[] = {
65     [IP_ALLOW_DYING] =		{"allow.dying",		PF_INTERNAL | PF_BOOL},
66     [IP_COMMAND] =		{"command",		PF_INTERNAL},
67     [IP_DEPEND] =		{"depend",		PF_INTERNAL},
68     [IP_EXEC_CLEAN] =		{"exec.clean",		PF_INTERNAL | PF_BOOL},
69     [IP_EXEC_CONSOLELOG] =	{"exec.consolelog",	PF_INTERNAL},
70     [IP_EXEC_FIB] =		{"exec.fib",		PF_INTERNAL | PF_INT},
71     [IP_EXEC_JAIL_USER] =	{"exec.jail_user",	PF_INTERNAL},
72     [IP_EXEC_POSTSTART] =	{"exec.poststart",	PF_INTERNAL},
73     [IP_EXEC_POSTSTOP] =	{"exec.poststop",	PF_INTERNAL},
74     [IP_EXEC_PRESTART] =	{"exec.prestart",	PF_INTERNAL},
75     [IP_EXEC_PRESTOP] =		{"exec.prestop",	PF_INTERNAL},
76     [IP_EXEC_CREATED] =		{"exec.created",	PF_INTERNAL},
77     [IP_EXEC_START] =		{"exec.start",		PF_INTERNAL},
78     [IP_EXEC_STOP] =		{"exec.stop",		PF_INTERNAL},
79     [IP_EXEC_SYSTEM_JAIL_USER]=	{"exec.system_jail_user",
80 							PF_INTERNAL | PF_BOOL},
81     [IP_EXEC_SYSTEM_USER] =	{"exec.system_user",	PF_INTERNAL},
82     [IP_EXEC_TIMEOUT] =		{"exec.timeout",	PF_INTERNAL | PF_INT},
83 #if defined(INET) || defined(INET6)
84     [IP_INTERFACE] =		{"interface",		PF_INTERNAL},
85     [IP_IP_HOSTNAME] =		{"ip_hostname",		PF_INTERNAL | PF_BOOL},
86 #endif
87     [IP_MOUNT] =		{"mount",		PF_INTERNAL | PF_REV},
88     [IP_MOUNT_DEVFS] =		{"mount.devfs",		PF_INTERNAL | PF_BOOL},
89     [IP_MOUNT_FDESCFS] =	{"mount.fdescfs",	PF_INTERNAL | PF_BOOL},
90     [IP_MOUNT_PROCFS] =		{"mount.procfs",	PF_INTERNAL | PF_BOOL},
91     [IP_MOUNT_FSTAB] =		{"mount.fstab",		PF_INTERNAL},
92     [IP_STOP_TIMEOUT] =		{"stop.timeout",	PF_INTERNAL | PF_INT},
93     [IP_VNET_INTERFACE] =	{"vnet.interface",	PF_INTERNAL},
94 #ifdef INET
95     [IP__IP4_IFADDR] =		{"ip4.addr",	PF_INTERNAL | PF_CONV | PF_REV},
96 #endif
97 #ifdef INET6
98     [IP__IP6_IFADDR] =		{"ip6.addr",	PF_INTERNAL | PF_CONV | PF_REV},
99 #endif
100     [IP__MOUNT_FROM_FSTAB] =	{"mount.fstab",	PF_INTERNAL | PF_CONV | PF_REV},
101     [IP__OP] =			{NULL,			PF_CONV},
102     [KP_ALLOW_CHFLAGS] =	{"allow.chflags",	0},
103     [KP_ALLOW_MOUNT] =		{"allow.mount",		0},
104     [KP_ALLOW_RAW_SOCKETS] =	{"allow.raw_sockets",	0},
105     [KP_ALLOW_SET_HOSTNAME]=	{"allow.set_hostname",	0},
106     [KP_ALLOW_SOCKET_AF] =	{"allow.socket_af",	0},
107     [KP_ALLOW_SYSVIPC] =	{"allow.sysvipc",	0},
108     [KP_DEVFS_RULESET] =	{"devfs_ruleset",	0},
109     [KP_ENFORCE_STATFS] =	{"enforce_statfs",	0},
110     [KP_HOST_HOSTNAME] =	{"host.hostname",	0},
111 #ifdef INET
112     [KP_IP4_ADDR] =		{"ip4.addr",		0},
113 #endif
114 #ifdef INET6
115     [KP_IP6_ADDR] =		{"ip6.addr",		0},
116 #endif
117     [KP_JID] =			{"jid",			PF_IMMUTABLE},
118     [KP_NAME] =			{"name",		PF_IMMUTABLE},
119     [KP_PATH] =			{"path",		0},
120     [KP_PERSIST] =		{"persist",		0},
121     [KP_SECURELEVEL] =		{"securelevel",		0},
122     [KP_VNET] =			{"vnet",		0},
123 };
124 
125 /*
126  * Parse the jail configuration file.
127  */
128 void
129 load_config(void)
130 {
131 	struct cfjails wild;
132 	struct cfparams opp;
133 	struct cfjail *j, *tj, *wj;
134 	struct cfparam *p, *vp, *tp;
135 	struct cfstring *s, *vs, *ns;
136 	struct cfvar *v, *vv;
137 	char *ep;
138 	int did_self, jseq, pgen;
139 
140 	if (!strcmp(cfname, "-")) {
141 		cfname = "STDIN";
142 		yyin = stdin;
143 	} else {
144 		yyin = fopen(cfname, "r");
145 		if (!yyin)
146 			err(1, "%s", cfname);
147 	}
148 	if (yyparse() || yynerrs)
149 		exit(1);
150 
151 	/* Separate the wildcard jails out from the actual jails. */
152 	jseq = 0;
153 	TAILQ_INIT(&wild);
154 	TAILQ_FOREACH_SAFE(j, &cfjails, tq, tj) {
155 		j->seq = ++jseq;
156 		if (wild_jail_name(j->name))
157 			requeue(j, &wild);
158 	}
159 
160 	TAILQ_FOREACH(j, &cfjails, tq) {
161 		/* Set aside the jail's parameters. */
162 		TAILQ_INIT(&opp);
163 		TAILQ_CONCAT(&opp, &j->params, tq);
164 		/*
165 		 * The jail name implies its "name" or "jid" parameter,
166 		 * though they may also be explicitly set later on.
167 		 */
168 		add_param(j, NULL,
169 		    strtol(j->name, &ep, 10) && !*ep ? KP_JID : KP_NAME,
170 		    j->name);
171 		/*
172 		 * Collect parameters for the jail, global parameters/variables,
173 		 * and any matching wildcard jails.
174 		 */
175 		did_self = 0;
176 		TAILQ_FOREACH(wj, &wild, tq) {
177 			if (j->seq < wj->seq && !did_self) {
178 				TAILQ_FOREACH(p, &opp, tq)
179 					add_param(j, p, 0, NULL);
180 				did_self = 1;
181 			}
182 			if (wild_jail_match(j->name, wj->name))
183 				TAILQ_FOREACH(p, &wj->params, tq)
184 					add_param(j, p, 0, NULL);
185 		}
186 		if (!did_self)
187 			TAILQ_FOREACH(p, &opp, tq)
188 				add_param(j, p, 0, NULL);
189 
190 		/* Resolve any variable substitutions. */
191 		pgen = 0;
192 		TAILQ_FOREACH(p, &j->params, tq) {
193 		    p->gen = ++pgen;
194 		find_vars:
195 		    TAILQ_FOREACH(s, &p->val, tq) {
196 			while ((v = STAILQ_FIRST(&s->vars))) {
197 				TAILQ_FOREACH(vp, &j->params, tq)
198 					if (!strcmp(vp->name, v->name))
199 						break;
200 				if (!vp) {
201 					jail_warnx(j,
202 					    "%s: variable \"%s\" not found",
203 					    p->name, v->name);
204 				bad_var:
205 					j->flags |= JF_FAILED;
206 					TAILQ_FOREACH(vp, &j->params, tq)
207 						if (vp->gen == pgen)
208 							vp->flags |= PF_BAD;
209 					goto free_var;
210 				}
211 				if (vp->flags & PF_BAD)
212 					goto bad_var;
213 				if (vp->gen == pgen) {
214 					jail_warnx(j, "%s: variable loop",
215 					    v->name);
216 					goto bad_var;
217 				}
218 				TAILQ_FOREACH(vs, &vp->val, tq)
219 					if (!STAILQ_EMPTY(&vs->vars)) {
220 						vp->gen = pgen;
221 						TAILQ_REMOVE(&j->params, vp,
222 						    tq);
223 						TAILQ_INSERT_BEFORE(p, vp, tq);
224 						p = vp;
225 						goto find_vars;
226 					}
227 				vs = TAILQ_FIRST(&vp->val);
228 				if (TAILQ_NEXT(vs, tq) != NULL &&
229 				    (s->s[0] != '\0' ||
230 				     STAILQ_NEXT(v, tq))) {
231 					jail_warnx(j, "%s: array cannot be "
232 					    "substituted inline",
233 					    p->name);
234 					goto bad_var;
235 				}
236 				s->s = erealloc(s->s, s->len + vs->len + 1);
237 				memmove(s->s + v->pos + vs->len,
238 				    s->s + v->pos,
239 				    s->len - v->pos + 1);
240 				memcpy(s->s + v->pos, vs->s, vs->len);
241 				vv = v;
242 				while ((vv = STAILQ_NEXT(vv, tq)))
243 					vv->pos += vs->len;
244 				s->len += vs->len;
245 				while ((vs = TAILQ_NEXT(vs, tq))) {
246 					ns = emalloc(sizeof(struct cfstring));
247 					ns->s = estrdup(vs->s);
248 					ns->len = vs->len;
249 					STAILQ_INIT(&ns->vars);
250 					TAILQ_INSERT_AFTER(&p->val, s, ns, tq);
251 					s = ns;
252 				}
253 			free_var:
254 				free(v->name);
255 				STAILQ_REMOVE_HEAD(&s->vars, tq);
256 				free(v);
257 			}
258 		    }
259 		}
260 
261 		/* Free the jail's original parameter list and any variables. */
262 		while ((p = TAILQ_FIRST(&opp)))
263 			free_param(&opp, p);
264 		TAILQ_FOREACH_SAFE(p, &j->params, tq, tp)
265 			if (p->flags & PF_VAR)
266 				free_param(&j->params, p);
267 	}
268 	while ((wj = TAILQ_FIRST(&wild))) {
269 		free(wj->name);
270 		while ((p = TAILQ_FIRST(&wj->params)))
271 			free_param(&wj->params, p);
272 		TAILQ_REMOVE(&wild, wj, tq);
273 	}
274 }
275 
276 /*
277  * Create a new jail record.
278  */
279 struct cfjail *
280 add_jail(void)
281 {
282 	struct cfjail *j;
283 
284 	j = emalloc(sizeof(struct cfjail));
285 	memset(j, 0, sizeof(struct cfjail));
286 	TAILQ_INIT(&j->params);
287 	STAILQ_INIT(&j->dep[DEP_FROM]);
288 	STAILQ_INIT(&j->dep[DEP_TO]);
289 	j->queue = &cfjails;
290 	TAILQ_INSERT_TAIL(&cfjails, j, tq);
291 	return j;
292 }
293 
294 /*
295  * Add a parameter to a jail.
296  */
297 void
298 add_param(struct cfjail *j, const struct cfparam *p, enum intparam ipnum,
299     const char *value)
300 {
301 	struct cfstrings nss;
302 	struct cfparam *dp, *np;
303 	struct cfstring *s, *ns;
304 	struct cfvar *v, *nv;
305 	const char *name;
306 	char *cs, *tname;
307 	unsigned flags;
308 
309 	if (j == NULL) {
310 		/* Create a single anonymous jail if one doesn't yet exist. */
311 		j = TAILQ_LAST(&cfjails, cfjails);
312 		if (j == NULL)
313 			j = add_jail();
314 	}
315 	TAILQ_INIT(&nss);
316 	if (p != NULL) {
317 		name = p->name;
318 		flags = p->flags;
319 		/*
320 		 * Make a copy of the parameter's string list,
321 		 * which may be freed if it's overridden later.
322 		 */
323 		TAILQ_FOREACH(s, &p->val, tq) {
324 			ns = emalloc(sizeof(struct cfstring));
325 			ns->s = estrdup(s->s);
326 			ns->len = s->len;
327 			STAILQ_INIT(&ns->vars);
328 			STAILQ_FOREACH(v, &s->vars, tq) {
329 				nv = emalloc(sizeof(struct cfvar));
330 				nv->name = strdup(v->name);
331 				nv->pos = v->pos;
332 				STAILQ_INSERT_TAIL(&ns->vars, nv, tq);
333 			}
334 			TAILQ_INSERT_TAIL(&nss, ns, tq);
335 		}
336 	} else {
337 		flags = PF_APPEND;
338 		if (ipnum != IP__NULL) {
339 			name = intparams[ipnum].name;
340 			flags |= intparams[ipnum].flags;
341 		} else if ((cs = strchr(value, '='))) {
342 			tname = alloca(cs - value + 1);
343 			strlcpy(tname, value, cs - value + 1);
344 			name = tname;
345 			value = cs + 1;
346 		} else {
347 			name = value;
348 			value = NULL;
349 		}
350 		if (value != NULL) {
351 			ns = emalloc(sizeof(struct cfstring));
352 			ns->s = estrdup(value);
353 			ns->len = strlen(value);
354 			STAILQ_INIT(&ns->vars);
355 			TAILQ_INSERT_TAIL(&nss, ns, tq);
356 		}
357 	}
358 
359 	/* See if this parameter has already been added. */
360 	if (ipnum != IP__NULL)
361 		dp = j->intparams[ipnum];
362 	else
363 		TAILQ_FOREACH(dp, &j->params, tq)
364 			if (!(dp->flags & PF_CONV) && equalopts(dp->name, name))
365 				break;
366 	if (dp != NULL) {
367 		/* Found it - append or replace. */
368 		if (dp->flags & PF_IMMUTABLE) {
369 			jail_warnx(j, "cannot redefine variable \"%s\".",
370 			    dp->name);
371 			return;
372 		}
373 		if (strcmp(dp->name, name)) {
374 			free(dp->name);
375 			dp->name = estrdup(name);
376 		}
377 		if (!(flags & PF_APPEND) || TAILQ_EMPTY(&nss))
378 			free_param_strings(dp);
379 		TAILQ_CONCAT(&dp->val, &nss, tq);
380 		dp->flags |= flags;
381 	} else {
382 		/* Not found - add it. */
383 		np = emalloc(sizeof(struct cfparam));
384 		np->name = estrdup(name);
385 		TAILQ_INIT(&np->val);
386 		TAILQ_CONCAT(&np->val, &nss, tq);
387 		np->flags = flags;
388 		np->gen = 0;
389 		TAILQ_INSERT_TAIL(&j->params, np, tq);
390 		if (ipnum != IP__NULL)
391 			j->intparams[ipnum] = np;
392 		else
393 			for (ipnum = IP__NULL + 1; ipnum < IP_NPARAM; ipnum++)
394 				if (!(intparams[ipnum].flags & PF_CONV) &&
395 				    equalopts(name, intparams[ipnum].name)) {
396 					j->intparams[ipnum] = np;
397 					np->flags |= intparams[ipnum].flags;
398 					break;
399 				}
400 	}
401 }
402 
403 /*
404  * Return if a boolean parameter exists and is true.
405  */
406 int
407 bool_param(const struct cfparam *p)
408 {
409 	const char *cs;
410 
411 	if (p == NULL)
412 		return 0;
413 	cs = strrchr(p->name, '.');
414 	return !strncmp(cs ? cs + 1 : p->name, "no", 2) ^
415 	    (TAILQ_EMPTY(&p->val) ||
416 	     !strcasecmp(TAILQ_LAST(&p->val, cfstrings)->s, "true") ||
417 	     (strtol(TAILQ_LAST(&p->val, cfstrings)->s, NULL, 10)));
418 }
419 
420 /*
421  * Set an integer if a parameter if it exists.
422  */
423 int
424 int_param(const struct cfparam *p, int *ip)
425 {
426 	if (p == NULL || TAILQ_EMPTY(&p->val))
427 		return 0;
428 	*ip = strtol(TAILQ_LAST(&p->val, cfstrings)->s, NULL, 10);
429 	return 1;
430 }
431 
432 /*
433  * Return the string value of a scalar parameter if it exists.
434  */
435 const char *
436 string_param(const struct cfparam *p)
437 {
438 	return (p && !TAILQ_EMPTY(&p->val)
439 	    ? TAILQ_LAST(&p->val, cfstrings)->s : NULL);
440 }
441 
442 /*
443  * Check syntax and values of internal parameters.  Set some internal
444  * parameters based on the values of others.
445  */
446 int
447 check_intparams(struct cfjail *j)
448 {
449 	struct cfparam *p;
450 	struct cfstring *s;
451 	FILE *f;
452 	const char *val;
453 	char *cs, *ep, *ln;
454 	size_t lnlen;
455 	int error;
456 #if defined(INET) || defined(INET6)
457 	struct addrinfo hints;
458 	struct addrinfo *ai0, *ai;
459 	const char *hostname;
460 	int gicode, defif;
461 #endif
462 #ifdef INET
463 	struct in_addr addr4;
464 	int ip4ok;
465 	char avalue4[INET_ADDRSTRLEN];
466 #endif
467 #ifdef INET6
468 	struct in6_addr addr6;
469 	int ip6ok;
470 	char avalue6[INET6_ADDRSTRLEN];
471 #endif
472 
473 	error = 0;
474 	/* Check format of boolan and integer values. */
475 	TAILQ_FOREACH(p, &j->params, tq) {
476 		if (!TAILQ_EMPTY(&p->val) && (p->flags & (PF_BOOL | PF_INT))) {
477 			val = TAILQ_LAST(&p->val, cfstrings)->s;
478 			if (p->flags & PF_BOOL) {
479 				if (strcasecmp(val, "false") &&
480 				    strcasecmp(val, "true") &&
481 				    ((void)strtol(val, &ep, 10), *ep)) {
482 					jail_warnx(j,
483 					    "%s: unknown boolean value \"%s\"",
484 					    p->name, val);
485 					error = -1;
486 				}
487 			} else {
488 				(void)strtol(val, &ep, 10);
489 				if (ep == val || *ep) {
490 					jail_warnx(j,
491 					    "%s: non-integer value \"%s\"",
492 					    p->name, val);
493 					error = -1;
494 				}
495 			}
496 		}
497 	}
498 
499 #if defined(INET) || defined(INET6)
500 	/*
501 	 * The ip_hostname parameter looks up the hostname, and adds parameters
502 	 * for any IP addresses it finds.
503 	 */
504 	if (((j->flags & JF_OP_MASK) != JF_STOP ||
505 	    j->intparams[IP_INTERFACE] != NULL) &&
506 	    bool_param(j->intparams[IP_IP_HOSTNAME]) &&
507 	    (hostname = string_param(j->intparams[KP_HOST_HOSTNAME]))) {
508 		j->intparams[IP_IP_HOSTNAME] = NULL;
509 		/*
510 		 * Silently ignore unsupported address families from
511 		 * DNS lookups.
512 		 */
513 #ifdef INET
514 		ip4ok = feature_present("inet");
515 #endif
516 #ifdef INET6
517 		ip6ok = feature_present("inet6");
518 #endif
519 		if (
520 #if defined(INET) && defined(INET6)
521 		    ip4ok || ip6ok
522 #elif defined(INET)
523 		    ip4ok
524 #elif defined(INET6)
525 		    ip6ok
526 #endif
527 			 ) {
528 			/* Look up the hostname (or get the address) */
529 			memset(&hints, 0, sizeof(hints));
530 			hints.ai_socktype = SOCK_STREAM;
531 			hints.ai_family =
532 #if defined(INET) && defined(INET6)
533 			    ip4ok ? (ip6ok ? PF_UNSPEC : PF_INET) :  PF_INET6;
534 #elif defined(INET)
535 			    PF_INET;
536 #elif defined(INET6)
537 			    PF_INET6;
538 #endif
539 			gicode = getaddrinfo(hostname, NULL, &hints, &ai0);
540 			if (gicode != 0) {
541 				jail_warnx(j, "host.hostname %s: %s", hostname,
542 				    gai_strerror(gicode));
543 				error = -1;
544 			} else {
545 				/*
546 				 * Convert the addresses to ASCII so jailparam
547 				 * can convert them back.  Errors are not
548 				 * expected here.
549 				 */
550 				for (ai = ai0; ai; ai = ai->ai_next)
551 					switch (ai->ai_family) {
552 #ifdef INET
553 					case AF_INET:
554 						memcpy(&addr4,
555 						    &((struct sockaddr_in *)
556 						    (void *)ai->ai_addr)->
557 						    sin_addr, sizeof(addr4));
558 						if (inet_ntop(AF_INET,
559 						    &addr4, avalue4,
560 						    INET_ADDRSTRLEN) == NULL)
561 							err(1, "inet_ntop");
562 						add_param(j, NULL, KP_IP4_ADDR,
563 						    avalue4);
564 						break;
565 #endif
566 #ifdef INET6
567 					case AF_INET6:
568 						memcpy(&addr6,
569 						    &((struct sockaddr_in6 *)
570 						    (void *)ai->ai_addr)->
571 						    sin6_addr, sizeof(addr6));
572 						if (inet_ntop(AF_INET6,
573 						    &addr6, avalue6,
574 						    INET6_ADDRSTRLEN) == NULL)
575 							err(1, "inet_ntop");
576 						add_param(j, NULL, KP_IP6_ADDR,
577 						    avalue6);
578 						break;
579 #endif
580 					}
581 				freeaddrinfo(ai0);
582 			}
583 		}
584 	}
585 
586 	/*
587 	 * IP addresses may include an interface to set that address on,
588 	 * a netmask/suffix for that address and options for ifconfig.
589 	 * These are copied to an internal command parameter and then stripped
590 	 * so they won't be passed on to jailparam_set.
591 	 */
592 	defif = string_param(j->intparams[IP_INTERFACE]) != NULL;
593 #ifdef INET
594 	if (j->intparams[KP_IP4_ADDR] != NULL) {
595 		TAILQ_FOREACH(s, &j->intparams[KP_IP4_ADDR]->val, tq) {
596 			cs = strchr(s->s, '|');
597 			if (cs || defif)
598 				add_param(j, NULL, IP__IP4_IFADDR, s->s);
599 			if (cs) {
600 				strcpy(s->s, cs + 1);
601 				s->len -= cs + 1 - s->s;
602 			}
603 			if ((cs = strchr(s->s, '/')) != NULL) {
604 				*cs = '\0';
605 				s->len = cs - s->s;
606 			}
607 			if ((cs = strchr(s->s, ' ')) != NULL) {
608 				*cs = '\0';
609 				s->len = cs - s->s;
610 			}
611 		}
612 	}
613 #endif
614 #ifdef INET6
615 	if (j->intparams[KP_IP6_ADDR] != NULL) {
616 		TAILQ_FOREACH(s, &j->intparams[KP_IP6_ADDR]->val, tq) {
617 			cs = strchr(s->s, '|');
618 			if (cs || defif)
619 				add_param(j, NULL, IP__IP6_IFADDR, s->s);
620 			if (cs) {
621 				strcpy(s->s, cs + 1);
622 				s->len -= cs + 1 - s->s;
623 			}
624 			if ((cs = strchr(s->s, '/')) != NULL) {
625 				*cs = '\0';
626 				s->len = cs - s->s;
627 			}
628 			if ((cs = strchr(s->s, ' ')) != NULL) {
629 				*cs = '\0';
630 				s->len = cs - s->s;
631 			}
632 		}
633 	}
634 #endif
635 #endif
636 
637 	/*
638 	 * Read mount.fstab file(s), and treat each line as its own mount
639 	 * parameter.
640 	 */
641 	if (j->intparams[IP_MOUNT_FSTAB] != NULL) {
642 		TAILQ_FOREACH(s, &j->intparams[IP_MOUNT_FSTAB]->val, tq) {
643 			if (s->len == 0)
644 				continue;
645 			f = fopen(s->s, "r");
646 			if (f == NULL) {
647 				jail_warnx(j, "mount.fstab: %s: %s",
648 				    s->s, strerror(errno));
649 				error = -1;
650 				continue;
651 			}
652 			while ((ln = fgetln(f, &lnlen))) {
653 				if ((cs = memchr(ln, '#', lnlen - 1)))
654 					lnlen = cs - ln + 1;
655 				if (ln[lnlen - 1] == '\n' ||
656 				    ln[lnlen - 1] == '#')
657 					ln[lnlen - 1] = '\0';
658 				else {
659 					cs = alloca(lnlen + 1);
660 					strlcpy(cs, ln, lnlen + 1);
661 					ln = cs;
662 				}
663 				add_param(j, NULL, IP__MOUNT_FROM_FSTAB, ln);
664 			}
665 			fclose(f);
666 		}
667 	}
668 	if (error)
669 		failed(j);
670 	return error;
671 }
672 
673 /*
674  * Import parameters into libjail's binary jailparam format.
675  */
676 int
677 import_params(struct cfjail *j)
678 {
679 	struct cfparam *p;
680 	struct cfstring *s, *ts;
681 	struct jailparam *jp;
682 	char *value, *cs;
683 	size_t vallen;
684 	int error;
685 
686 	error = 0;
687 	j->njp = 0;
688 	TAILQ_FOREACH(p, &j->params, tq)
689 		if (!(p->flags & PF_INTERNAL))
690 			j->njp++;
691 	j->jp = jp = emalloc(j->njp * sizeof(struct jailparam));
692 	TAILQ_FOREACH(p, &j->params, tq) {
693 		if (p->flags & PF_INTERNAL)
694 			continue;
695 		if (jailparam_init(jp, p->name) < 0) {
696 			error = -1;
697 			jail_warnx(j, "%s", jail_errmsg);
698 			jp++;
699 			continue;
700 		}
701 		if (TAILQ_EMPTY(&p->val))
702 			value = NULL;
703 		else if (!jp->jp_elemlen ||
704 			 !TAILQ_NEXT(TAILQ_FIRST(&p->val), tq)) {
705 			/*
706 			 * Scalar parameters silently discard multiple (array)
707 			 * values, keeping only the last value added.  This
708 			 * lets values added from the command line append to
709 			 * arrays wthout pre-checking the type.
710 			 */
711 			value = TAILQ_LAST(&p->val, cfstrings)->s;
712 		} else {
713 			/*
714 			 * Convert arrays into comma-separated strings, which
715 			 * jailparam_import will then convert back into arrays.
716 			 */
717 			vallen = 0;
718 			TAILQ_FOREACH(s, &p->val, tq)
719 				vallen += s->len + 1;
720 			value = alloca(vallen);
721 			cs = value;
722 			TAILQ_FOREACH_SAFE(s, &p->val, tq, ts) {
723 				memcpy(cs, s->s, s->len);
724 				cs += s->len + 1;
725 				cs[-1] = ',';
726 			}
727 			value[vallen - 1] = '\0';
728 		}
729 		if (jailparam_import(jp, value) < 0) {
730 			error = -1;
731 			jail_warnx(j, "%s", jail_errmsg);
732 		}
733 		jp++;
734 	}
735 	if (error) {
736 		jailparam_free(j->jp, j->njp);
737 		free(j->jp);
738 		j->jp = NULL;
739 		failed(j);
740 	}
741 	return error;
742 }
743 
744 /*
745  * Check if options are equal (with or without the "no" prefix).
746  */
747 int
748 equalopts(const char *opt1, const char *opt2)
749 {
750 	char *p;
751 
752 	/* "opt" vs. "opt" or "noopt" vs. "noopt" */
753 	if (strcmp(opt1, opt2) == 0)
754 		return (1);
755 	/* "noopt" vs. "opt" */
756 	if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
757 		return (1);
758 	/* "opt" vs. "noopt" */
759 	if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
760 		return (1);
761 	while ((p = strchr(opt1, '.')) != NULL &&
762 	    !strncmp(opt1, opt2, ++p - opt1)) {
763 		opt2 += p - opt1;
764 		opt1 = p;
765 		/* "foo.noopt" vs. "foo.opt" */
766 		if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
767 			return (1);
768 		/* "foo.opt" vs. "foo.noopt" */
769 		if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
770 			return (1);
771 	}
772 	return (0);
773 }
774 
775 /*
776  * See if a jail name matches a wildcard.
777  */
778 int
779 wild_jail_match(const char *jname, const char *wname)
780 {
781 	const char *jc, *jd, *wc, *wd;
782 
783 	/*
784 	 * A non-final "*" component in the wild name matches a single jail
785 	 * component, and a final "*" matches one or more jail components.
786 	 */
787 	for (jc = jname, wc = wname;
788 	     (jd = strchr(jc, '.')) && (wd = strchr(wc, '.'));
789 	     jc = jd + 1, wc = wd + 1)
790 		if (strncmp(jc, wc, jd - jc + 1) && strncmp(wc, "*.", 2))
791 			return 0;
792 	return (!strcmp(jc, wc) || !strcmp(wc, "*"));
793 }
794 
795 /*
796  * Return if a jail name is a wildcard.
797  */
798 int
799 wild_jail_name(const char *wname)
800 {
801 	const char *wc;
802 
803 	for (wc = strchr(wname, '*'); wc; wc = strchr(wc + 1, '*'))
804 		if ((wc == wname || wc[-1] == '.') &&
805 		    (wc[1] == '\0' || wc[1] == '.'))
806 			return 1;
807 	return 0;
808 }
809 
810 /*
811  * Free a parameter record and all its strings and variables.
812  */
813 static void
814 free_param(struct cfparams *pp, struct cfparam *p)
815 {
816 	free(p->name);
817 	free_param_strings(p);
818 	TAILQ_REMOVE(pp, p, tq);
819 	free(p);
820 }
821 
822 static void
823 free_param_strings(struct cfparam *p)
824 {
825 	struct cfstring *s;
826 	struct cfvar *v;
827 
828 	while ((s = TAILQ_FIRST(&p->val))) {
829 		free(s->s);
830 		while ((v = STAILQ_FIRST(&s->vars))) {
831 			free(v->name);
832 			STAILQ_REMOVE_HEAD(&s->vars, tq);
833 			free(v);
834 		}
835 		TAILQ_REMOVE(&p->val, s, tq);
836 		free(s);
837 	}
838 }
839