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