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