jail.c revision 217616
156067Smarkm/*-
256067Smarkm * Copyright (c) 2009 James Gritton.
395509Sru * All rights reserved.
4103962Smarkm *
556067Smarkm * Redistribution and use in source and binary forms, with or without
656067Smarkm * modification, are permitted provided that the following conditions
756067Smarkm * are met:
856067Smarkm * 1. Redistributions of source code must retain the above copyright
956067Smarkm *    notice, this list of conditions and the following disclaimer.
1056067Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1181104Smarkm *    notice, this list of conditions and the following disclaimer in the
1256067Smarkm *    documentation and/or other materials provided with the distribution.
1396462Sru *
1456549Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1595509Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1657672Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1756549Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1856549Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1956549Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2056549Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2156067Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2256067Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2356067Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2456067Smarkm * SUCH DAMAGE.
2556067Smarkm */
2656067Smarkm
2756067Smarkm#include <sys/cdefs.h>
2856067Smarkm__FBSDID("$FreeBSD: head/lib/libjail/jail.c 217616 2011-01-19 23:00:25Z mdf $");
2956067Smarkm
3056067Smarkm#include <sys/param.h>
3156067Smarkm#include <sys/types.h>
3256067Smarkm#include <sys/jail.h>
3356067Smarkm#include <sys/socket.h>
3456067Smarkm#include <sys/sysctl.h>
3556067Smarkm
3656067Smarkm#include <arpa/inet.h>
3756067Smarkm#include <netinet/in.h>
3856067Smarkm
3956067Smarkm#include <errno.h>
4056067Smarkm#include <inttypes.h>
4156067Smarkm#include <stdio.h>
4256067Smarkm#include <stdarg.h>
4356067Smarkm#include <stdlib.h>
4456067Smarkm#include <string.h>
4572450Sassar
4672450Sassar#include "jail.h"
4772450Sassar
4872450Sassar#define	SJPARAM		"security.jail.param"
4972450Sassar
5072450Sassar#define JPS_IN_ADDR	1
5156067Smarkm#define JPS_IN6_ADDR	2
5256067Smarkm
5356067Smarkm#define ARRAY_SANITY	5
5456067Smarkm#define ARRAY_SLOP	5
5556067Smarkm
5656067Smarkm
5756067Smarkmstatic int jailparam_import_enum(const char **values, int nvalues,
5856067Smarkm    const char *valstr, size_t valsize, int *value);
5956067Smarkmstatic int jailparam_type(struct jailparam *jp);
6056067Smarkmstatic char *noname(const char *name);
6156067Smarkmstatic char *nononame(const char *name);
6256067Smarkm
6356067Smarkmchar jail_errmsg[JAIL_ERRMSGLEN];
6456067Smarkm
6556067Smarkmstatic const char *bool_values[] = { "false", "true" };
6656067Smarkmstatic const char *jailsys_values[] = { "disable", "new", "inherit" };
6756067Smarkm
6856067Smarkm
6956067Smarkm/*
7056067Smarkm * Import a null-terminated parameter list and set a jail with the flags
7156067Smarkm * and parameters.
7256067Smarkm */
7356067Smarkmint
7456067Smarkmjail_setv(int flags, ...)
7556067Smarkm{
7656067Smarkm	va_list ap, tap;
7756067Smarkm	struct jailparam *jp;
7856067Smarkm	const char *name, *value;
7956067Smarkm	int njp, jid;
8056067Smarkm
8156067Smarkm	/* Create the parameter list and import the parameters. */
8256067Smarkm	va_start(ap, flags);
8356067Smarkm	va_copy(tap, ap);
8456067Smarkm	for (njp = 0; va_arg(tap, char *) != NULL; njp++)
8556067Smarkm		(void)va_arg(tap, char *);
8656067Smarkm	va_end(tap);
8756067Smarkm	jp = alloca(njp * sizeof(struct jailparam));
8856067Smarkm	for (njp = 0; (name = va_arg(ap, char *)) != NULL; njp++) {
8956067Smarkm		value = va_arg(ap, char *);
9056067Smarkm		if (jailparam_init(jp + njp, name) < 0 ||
9156067Smarkm		    jailparam_import(jp + njp, value) < 0) {
9256067Smarkm			jailparam_free(jp, njp);
9356067Smarkm			va_end(ap);
9456067Smarkm			return (-1);
9556067Smarkm		}
9656067Smarkm	}
9756067Smarkm	va_end(ap);
9856067Smarkm	jid = jailparam_set(jp, njp, flags);
9981104Smarkm	jailparam_free(jp, njp);
10081104Smarkm	return (jid);
10181104Smarkm}
10281104Smarkm
10381104Smarkm/*
10481104Smarkm * Read a null-terminated parameter list, get the referenced jail, and export
10556067Smarkm * the parameters to the list.
10656067Smarkm */
10756067Smarkmint
10856067Smarkmjail_getv(int flags, ...)
10956067Smarkm{
11056067Smarkm	va_list ap, tap;
11156067Smarkm	struct jailparam *jp, *jp_lastjid, *jp_jid, *jp_name, *jp_key;
11256067Smarkm	char *valarg, *value;
11356067Smarkm	const char *name, *key_value, *lastjid_value, *jid_value, *name_value;
11456067Smarkm	int njp, i, jid;
11556067Smarkm
11656067Smarkm	/* Create the parameter list and find the key. */
11756067Smarkm	va_start(ap, flags);
11856067Smarkm	va_copy(tap, ap);
11956067Smarkm	for (njp = 0; va_arg(tap, char *) != NULL; njp++)
12056067Smarkm		(void)va_arg(tap, char *);
12156067Smarkm	va_end(tap);
12256067Smarkm
12356067Smarkm	jp = alloca(njp * sizeof(struct jailparam));
12456067Smarkm	va_copy(tap, ap);
12556067Smarkm	jp_lastjid = jp_jid = jp_name = NULL;
12656067Smarkm	lastjid_value = jid_value = name_value = NULL;
12756067Smarkm	for (njp = 0; (name = va_arg(tap, char *)) != NULL; njp++) {
12856067Smarkm		value = va_arg(tap, char *);
12956067Smarkm		if (jailparam_init(jp + njp, name) < 0) {
13056067Smarkm			va_end(tap);
13156067Smarkm			goto error;
13256067Smarkm		}
13356067Smarkm		if (!strcmp(jp[njp].jp_name, "lastjid")) {
13456067Smarkm			jp_lastjid = jp + njp;
13556067Smarkm			lastjid_value = value;
13656067Smarkm		} else if (!strcmp(jp[njp].jp_name, "jid")) {
13756067Smarkm			jp_jid = jp + njp;
13856067Smarkm			jid_value = value;
13956067Smarkm		} if (!strcmp(jp[njp].jp_name, "name")) {
14056067Smarkm			jp_name = jp + njp;
14156067Smarkm			name_value = value;
14256067Smarkm		}
14390931Snectar	}
14490931Snectar	va_end(tap);
14590931Snectar	/* Import the key parameter. */
14690931Snectar	if (jp_lastjid != NULL) {
14790931Snectar		jp_key = jp_lastjid;
14890931Snectar		key_value = lastjid_value;
14990931Snectar	} else if (jp_jid != NULL && strtol(jid_value, NULL, 10) != 0) {
15090931Snectar		jp_key = jp_jid;
15157452Smarkm		key_value = jid_value;
15257452Smarkm	} else if (jp_name != NULL) {
15357452Smarkm		jp_key = jp_name;
15457452Smarkm		key_value = name_value;
15557452Smarkm	} else {
15657452Smarkm		strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN);
15757452Smarkm		errno = ENOENT;
15857452Smarkm		goto error;
15957452Smarkm	}
16057452Smarkm	if (jailparam_import(jp_key, key_value) < 0)
16156067Smarkm		goto error;
16256067Smarkm	/* Get the jail and export the parameters. */
16356067Smarkm	jid = jailparam_get(jp, njp, flags);
16456067Smarkm	if (jid < 0)
16556067Smarkm		goto error;
16656067Smarkm	for (i = 0; i < njp; i++) {
16756067Smarkm		(void)va_arg(ap, char *);
168		valarg = va_arg(ap, char *);
169		if (jp + i != jp_key) {
170			/* It's up to the caller to ensure there's room. */
171			if ((jp[i].jp_ctltype & CTLTYPE) == CTLTYPE_STRING)
172				strcpy(valarg, jp[i].jp_value);
173			else {
174				value = jailparam_export(jp + i);
175				if (value == NULL)
176					goto error;
177				strcpy(valarg, value);
178				free(value);
179			}
180		}
181	}
182	jailparam_free(jp, njp);
183	va_end(ap);
184	return (jid);
185
186 error:
187	jailparam_free(jp, njp);
188	va_end(ap);
189	return (-1);
190}
191
192/*
193 * Return a list of all known parameters.
194 */
195int
196jailparam_all(struct jailparam **jpp)
197{
198	struct jailparam *jp;
199	size_t mlen1, mlen2, buflen;
200	int njp, nlist;
201	int mib1[CTL_MAXNAME], mib2[CTL_MAXNAME - 2];
202	char buf[MAXPATHLEN];
203
204	njp = 0;
205	nlist = 32;
206	jp = malloc(nlist * sizeof(*jp));
207	if (jp == NULL) {
208		strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
209		return (-1);
210	}
211	mib1[0] = 0;
212	mib1[1] = 2;
213	mlen1 = CTL_MAXNAME - 2;
214	if (sysctlnametomib(SJPARAM, mib1 + 2, &mlen1) < 0) {
215		snprintf(jail_errmsg, JAIL_ERRMSGLEN,
216		    "sysctlnametomib(" SJPARAM "): %s", strerror(errno));
217		goto error;
218	}
219	for (;; njp++) {
220		/* Get the next parameter. */
221		mlen2 = sizeof(mib2);
222		if (sysctl(mib1, mlen1 + 2, mib2, &mlen2, NULL, 0) < 0) {
223			snprintf(jail_errmsg, JAIL_ERRMSGLEN,
224			    "sysctl(0.2): %s", strerror(errno));
225			goto error;
226		}
227		if (mib2[0] != mib1[2] || mib2[1] != mib1[3] ||
228		    mib2[2] != mib1[4])
229			break;
230		/* Convert it to an ascii name. */
231		memcpy(mib1 + 2, mib2, mlen2);
232		mlen1 = mlen2 / sizeof(int);
233		mib1[1] = 1;
234		buflen = sizeof(buf);
235		if (sysctl(mib1, mlen1 + 2, buf, &buflen, NULL, 0) < 0) {
236			snprintf(jail_errmsg, JAIL_ERRMSGLEN,
237			    "sysctl(0.1): %s", strerror(errno));
238			goto error;
239		}
240		if (buf[buflen - 2] == '.')
241			buf[buflen - 2] = '\0';
242		/* Add the parameter to the list */
243		if (njp >= nlist) {
244			nlist *= 2;
245			jp = realloc(jp, nlist * sizeof(*jp));
246			if (jp == NULL) {
247				jailparam_free(jp, njp);
248				return (-1);
249			}
250		}
251		if (jailparam_init(jp + njp, buf + sizeof(SJPARAM)) < 0)
252			goto error;
253		mib1[1] = 2;
254	}
255	jp = realloc(jp, njp * sizeof(*jp));
256	*jpp = jp;
257	return (njp);
258
259 error:
260	jailparam_free(jp, njp);
261	free(jp);
262	return (-1);
263}
264
265/*
266 * Clear a jail parameter and copy in its name.
267 */
268int
269jailparam_init(struct jailparam *jp, const char *name)
270{
271
272	memset(jp, 0, sizeof(*jp));
273	jp->jp_name = strdup(name);
274	if (jp->jp_name == NULL) {
275		strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
276		return (-1);
277	}
278	if (jailparam_type(jp) < 0) {
279		jailparam_free(jp, 1);
280		return (-1);
281	}
282	return (0);
283}
284
285/*
286 * Put a name and value into a jail parameter element, converting the value
287 * to internal form.
288 */
289int
290jailparam_import(struct jailparam *jp, const char *value)
291{
292	char *p, *ep, *tvalue;
293	const char *avalue;
294	int i, nval, fw;
295
296	if (value == NULL)
297		return (0);
298	if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
299		jp->jp_value = strdup(value);
300		if (jp->jp_value == NULL) {
301			strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
302			return (-1);
303		}
304		return (0);
305	}
306	nval = 1;
307	if (jp->jp_elemlen) {
308		if (value[0] == '\0' || (value[0] == '-' && value[1] == '\0')) {
309			jp->jp_value = strdup("");
310			if (jp->jp_value == NULL) {
311				strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
312				return (-1);
313			}
314			jp->jp_valuelen = 0;
315			return (0);
316		}
317		for (p = strchr(value, ','); p; p = strchr(p + 1, ','))
318			nval++;
319		jp->jp_valuelen = jp->jp_elemlen * nval;
320	}
321	jp->jp_value = malloc(jp->jp_valuelen);
322	if (jp->jp_value == NULL) {
323		strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
324		return (-1);
325	}
326	avalue = value;
327	for (i = 0; i < nval; i++) {
328		fw = nval == 1 ? strlen(avalue) : strcspn(avalue, ",");
329		switch (jp->jp_ctltype & CTLTYPE) {
330		case CTLTYPE_INT:
331			if (jp->jp_flags & (JP_BOOL | JP_NOBOOL)) {
332				if (!jailparam_import_enum(bool_values, 2,
333				    avalue, fw, &((int *)jp->jp_value)[i])) {
334					snprintf(jail_errmsg,
335					    JAIL_ERRMSGLEN, "%s: "
336					    "unknown boolean value \"%.*s\"",
337					    jp->jp_name, fw, avalue);
338					errno = EINVAL;
339					goto error;
340				}
341				break;
342			}
343			if (jp->jp_flags & JP_JAILSYS) {
344				/*
345				 * Allow setting a jailsys parameter to "new"
346				 * in a booleanesque fashion.
347				 */
348				if (value[0] == '\0')
349					((int *)jp->jp_value)[i] = JAIL_SYS_NEW;
350				else if (!jailparam_import_enum(jailsys_values,
351				    sizeof(jailsys_values) /
352				    sizeof(jailsys_values[0]), avalue, fw,
353				    &((int *)jp->jp_value)[i])) {
354					snprintf(jail_errmsg,
355					    JAIL_ERRMSGLEN, "%s: "
356					    "unknown jailsys value \"%.*s\"",
357					    jp->jp_name, fw, avalue);
358					errno = EINVAL;
359					goto error;
360				}
361				break;
362			}
363			((int *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
364		integer_test:
365			if (ep != avalue + fw) {
366				snprintf(jail_errmsg, JAIL_ERRMSGLEN,
367				    "%s: non-integer value \"%.*s\"",
368				    jp->jp_name, fw, avalue);
369				errno = EINVAL;
370				goto error;
371			}
372			break;
373		case CTLTYPE_UINT:
374			((unsigned *)jp->jp_value)[i] =
375			    strtoul(avalue, &ep, 10);
376			goto integer_test;
377		case CTLTYPE_LONG:
378			((long *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
379			goto integer_test;
380		case CTLTYPE_ULONG:
381			((unsigned long *)jp->jp_value)[i] =
382			    strtoul(avalue, &ep, 10);
383			goto integer_test;
384		case CTLTYPE_S64:
385			((int64_t *)jp->jp_value)[i] =
386			    strtoimax(avalue, &ep, 10);
387			goto integer_test;
388		case CTLTYPE_U64:
389			((uint64_t *)jp->jp_value)[i] =
390			    strtoumax(avalue, &ep, 10);
391			goto integer_test;
392		case CTLTYPE_STRUCT:
393			tvalue = alloca(fw + 1);
394			strlcpy(tvalue, avalue, fw + 1);
395			switch (jp->jp_structtype) {
396			case JPS_IN_ADDR:
397				if (inet_pton(AF_INET, tvalue,
398				    &((struct in_addr *)jp->jp_value)[i]) != 1)
399				{
400					snprintf(jail_errmsg,
401					    JAIL_ERRMSGLEN,
402					    "%s: not an IPv4 address: %s",
403					    jp->jp_name, tvalue);
404					errno = EINVAL;
405					goto error;
406				}
407				break;
408			case JPS_IN6_ADDR:
409				if (inet_pton(AF_INET6, tvalue,
410				    &((struct in6_addr *)jp->jp_value)[i]) != 1)
411				{
412					snprintf(jail_errmsg,
413					    JAIL_ERRMSGLEN,
414					    "%s: not an IPv6 address: %s",
415					    jp->jp_name, tvalue);
416					errno = EINVAL;
417					goto error;
418				}
419				break;
420			default:
421				goto unknown_type;
422			}
423			break;
424		default:
425		unknown_type:
426			snprintf(jail_errmsg, JAIL_ERRMSGLEN,
427			    "unknown type for %s", jp->jp_name);
428			errno = ENOENT;
429			goto error;
430		}
431		avalue += fw + 1;
432	}
433	return (0);
434
435 error:
436	free(jp->jp_value);
437	jp->jp_value = NULL;
438	return (-1);
439}
440
441static int
442jailparam_import_enum(const char **values, int nvalues, const char *valstr,
443    size_t valsize, int *value)
444{
445	char *ep;
446	int i;
447
448	for (i = 0; i < nvalues; i++)
449		if (valsize == strlen(values[i]) &&
450		    !strncasecmp(valstr, values[i], valsize)) {
451			*value = i;
452			return 1;
453		}
454	*value = strtol(valstr, &ep, 10);
455	return (ep == valstr + valsize);
456}
457
458/*
459 * Put a name and value into a jail parameter element, copying the value
460 * but not altering it.
461 */
462int
463jailparam_import_raw(struct jailparam *jp, void *value, size_t valuelen)
464{
465
466	jp->jp_value = value;
467	jp->jp_valuelen = valuelen;
468	jp->jp_flags |= JP_RAWVALUE;
469	return (0);
470}
471
472/*
473 * Run the jail_set and jail_get system calls on a parameter list.
474 */
475int
476jailparam_set(struct jailparam *jp, unsigned njp, int flags)
477{
478	struct iovec *jiov;
479	char *nname;
480	int i, jid, bool0;
481	unsigned j;
482
483	jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
484	bool0 = 0;
485	for (i = j = 0; j < njp; j++) {
486		jiov[i].iov_base = jp[j].jp_name;
487		jiov[i].iov_len = strlen(jp[j].jp_name) + 1;
488		i++;
489		if (jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) {
490			/*
491			 * Set booleans without values.  If one has a value of
492			 * zero, change it to (or from) its "no" counterpart.
493			 */
494			jiov[i].iov_base = NULL;
495			jiov[i].iov_len = 0;
496			if (jp[j].jp_value != NULL &&
497			    jp[j].jp_valuelen == sizeof(int) &&
498			    !*(int *)jp[j].jp_value) {
499				bool0 = 1;
500				nname = jp[j].jp_flags & JP_BOOL
501				    ? noname(jp[j].jp_name)
502				    : nononame(jp[j].jp_name);
503				if (nname == NULL) {
504					njp = j;
505					jid = -1;
506					goto done;
507				}
508				jiov[i - 1].iov_base = nname;
509				jiov[i - 1].iov_len = strlen(nname) + 1;
510
511			}
512		} else {
513			/*
514			 * Try to fill in missing values with an empty string.
515			 */
516			if (jp[j].jp_value == NULL && jp[j].jp_valuelen > 0 &&
517			    jailparam_import(jp + j, "") < 0) {
518				njp = j;
519				jid = -1;
520				goto done;
521			}
522			jiov[i].iov_base = jp[j].jp_value;
523			jiov[i].iov_len =
524			    (jp[j].jp_ctltype & CTLTYPE) == CTLTYPE_STRING
525			    ? strlen(jp[j].jp_value) + 1
526			    : jp[j].jp_valuelen;
527		}
528		i++;
529	}
530	*(const void **)&jiov[i].iov_base = "errmsg";
531	jiov[i].iov_len = sizeof("errmsg");
532	i++;
533	jiov[i].iov_base = jail_errmsg;
534	jiov[i].iov_len = JAIL_ERRMSGLEN;
535	i++;
536	jail_errmsg[0] = 0;
537	jid = jail_set(jiov, i, flags);
538	if (jid < 0 && !jail_errmsg[0])
539		snprintf(jail_errmsg, sizeof(jail_errmsg), "jail_set: %s",
540		    strerror(errno));
541 done:
542	if (bool0)
543		for (j = 0; j < njp; j++)
544			if ((jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) &&
545			    jp[j].jp_value != NULL &&
546			    jp[j].jp_valuelen == sizeof(int) &&
547			    !*(int *)jp[j].jp_value)
548				free(jiov[j * 2].iov_base);
549	return (jid);
550}
551
552int
553jailparam_get(struct jailparam *jp, unsigned njp, int flags)
554{
555	struct iovec *jiov;
556	struct jailparam *jp_lastjid, *jp_jid, *jp_name, *jp_key;
557	int i, ai, ki, jid, arrays, sanity;
558	unsigned j;
559
560	/*
561	 * Get the types for all parameters.
562	 * Find the key and any array parameters.
563	 */
564	jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
565	jp_lastjid = jp_jid = jp_name = NULL;
566	arrays = 0;
567	for (ai = j = 0; j < njp; j++) {
568		if (!strcmp(jp[j].jp_name, "lastjid"))
569			jp_lastjid = jp + j;
570		else if (!strcmp(jp[j].jp_name, "jid"))
571			jp_jid = jp + j;
572		else if (!strcmp(jp[j].jp_name, "name"))
573			jp_name = jp + j;
574		else if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
575			arrays = 1;
576			jiov[ai].iov_base = jp[j].jp_name;
577			jiov[ai].iov_len = strlen(jp[j].jp_name) + 1;
578			ai++;
579			jiov[ai].iov_base = NULL;
580			jiov[ai].iov_len = 0;
581			ai++;
582		}
583	}
584	jp_key = jp_lastjid ? jp_lastjid :
585	    jp_jid && jp_jid->jp_valuelen == sizeof(int) &&
586	    jp_jid->jp_value && *(int *)jp_jid->jp_value ? jp_jid : jp_name;
587	if (jp_key == NULL || jp_key->jp_value == NULL) {
588		strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN);
589		errno = ENOENT;
590		return (-1);
591	}
592	ki = ai;
593	jiov[ki].iov_base = jp_key->jp_name;
594	jiov[ki].iov_len = strlen(jp_key->jp_name) + 1;
595	ki++;
596	jiov[ki].iov_base = jp_key->jp_value;
597	jiov[ki].iov_len = (jp_key->jp_ctltype & CTLTYPE) == CTLTYPE_STRING
598	    ? strlen(jp_key->jp_value) + 1 : jp_key->jp_valuelen;
599	ki++;
600	*(const void **)&jiov[ki].iov_base = "errmsg";
601	jiov[ki].iov_len = sizeof("errmsg");
602	ki++;
603	jiov[ki].iov_base = jail_errmsg;
604	jiov[ki].iov_len = JAIL_ERRMSGLEN;
605	ki++;
606	jail_errmsg[0] = 0;
607	if (arrays && jail_get(jiov, ki, flags) < 0) {
608		if (!jail_errmsg[0])
609			snprintf(jail_errmsg, sizeof(jail_errmsg),
610			    "jail_get: %s", strerror(errno));
611		return (-1);
612	}
613	/* Allocate storage for all parameters. */
614	for (ai = j = 0, i = ki; j < njp; j++) {
615		if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
616			ai++;
617			jiov[ai].iov_len += jp[j].jp_elemlen * ARRAY_SLOP;
618			if (jp[j].jp_valuelen >= jiov[ai].iov_len)
619				jiov[ai].iov_len = jp[j].jp_valuelen;
620			else {
621				jp[j].jp_valuelen = jiov[ai].iov_len;
622				if (jp[j].jp_value != NULL)
623					free(jp[j].jp_value);
624				jp[j].jp_value = malloc(jp[j].jp_valuelen);
625				if (jp[j].jp_value == NULL) {
626					strerror_r(errno, jail_errmsg,
627					    JAIL_ERRMSGLEN);
628					return (-1);
629				}
630			}
631			jiov[ai].iov_base = jp[j].jp_value;
632			memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
633			ai++;
634		} else if (jp + j != jp_key) {
635			jiov[i].iov_base = jp[j].jp_name;
636			jiov[i].iov_len = strlen(jp[j].jp_name) + 1;
637			i++;
638			if (jp[j].jp_value == NULL &&
639			    !(jp[j].jp_flags & JP_RAWVALUE)) {
640				jp[j].jp_value = malloc(jp[j].jp_valuelen);
641				if (jp[j].jp_value == NULL) {
642					strerror_r(errno, jail_errmsg,
643					    JAIL_ERRMSGLEN);
644					return (-1);
645				}
646			}
647			jiov[i].iov_base = jp[j].jp_value;
648			jiov[i].iov_len = jp[j].jp_valuelen;
649			memset(jiov[i].iov_base, 0, jiov[i].iov_len);
650			i++;
651		}
652	}
653	/*
654	 * Get the prison.  If there are array elements, retry a few times
655	 * in case their sizes changed from under us.
656	 */
657	for (sanity = 0;; sanity++) {
658		jid = jail_get(jiov, i, flags);
659		if (jid >= 0 || !arrays || sanity == ARRAY_SANITY ||
660		    errno != EINVAL || jail_errmsg[0])
661			break;
662		for (ai = j = 0; j < njp; j++) {
663			if (jp[j].jp_elemlen &&
664			    !(jp[j].jp_flags & JP_RAWVALUE)) {
665				ai++;
666				jiov[ai].iov_base = NULL;
667				jiov[ai].iov_len = 0;
668				ai++;
669			}
670		}
671		if (jail_get(jiov, ki, flags) < 0)
672			break;
673		for (ai = j = 0; j < njp; j++) {
674			if (jp[j].jp_elemlen &&
675			    !(jp[j].jp_flags & JP_RAWVALUE)) {
676				ai++;
677				jiov[ai].iov_len +=
678				    jp[j].jp_elemlen * ARRAY_SLOP;
679				if (jp[j].jp_valuelen >= jiov[ai].iov_len)
680					jiov[ai].iov_len = jp[j].jp_valuelen;
681				else {
682					jp[j].jp_valuelen = jiov[ai].iov_len;
683					if (jp[j].jp_value != NULL)
684						free(jp[j].jp_value);
685					jp[j].jp_value =
686					    malloc(jiov[ai].iov_len);
687					if (jp[j].jp_value == NULL) {
688						strerror_r(errno, jail_errmsg,
689						    JAIL_ERRMSGLEN);
690						return (-1);
691					}
692				}
693				jiov[ai].iov_base = jp[j].jp_value;
694				memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
695				ai++;
696			}
697		}
698	}
699	if (jid < 0 && !jail_errmsg[0])
700		snprintf(jail_errmsg, sizeof(jail_errmsg),
701		    "jail_get: %s", strerror(errno));
702	for (ai = j = 0, i = ki; j < njp; j++) {
703		if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
704			ai++;
705			jp[j].jp_valuelen = jiov[ai].iov_len;
706			ai++;
707		} else if (jp + j != jp_key) {
708			i++;
709			jp[j].jp_valuelen = jiov[i].iov_len;
710			i++;
711		}
712	}
713	return (jid);
714}
715
716/*
717 * Convert a jail parameter's value to external form.
718 */
719char *
720jailparam_export(struct jailparam *jp)
721{
722	size_t *valuelens;
723	char *value, *tvalue, **values;
724	size_t valuelen;
725	int i, nval, ival;
726	char valbuf[INET6_ADDRSTRLEN];
727
728	if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
729		value = strdup(jp->jp_value);
730		if (value == NULL)
731			strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
732		return (value);
733	}
734	nval = jp->jp_elemlen ? jp->jp_valuelen / jp->jp_elemlen : 1;
735	if (nval == 0) {
736		value = strdup("");
737		if (value == NULL)
738			strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
739		return (value);
740	}
741	values = alloca(nval * sizeof(char *));
742	valuelens = alloca(nval * sizeof(size_t));
743	valuelen = 0;
744	for (i = 0; i < nval; i++) {
745		switch (jp->jp_ctltype & CTLTYPE) {
746		case CTLTYPE_INT:
747			ival = ((int *)jp->jp_value)[i];
748			if ((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
749			    (unsigned)ival < 2) {
750				strlcpy(valbuf, bool_values[ival],
751				    sizeof(valbuf));
752				break;
753			}
754			if ((jp->jp_flags & JP_JAILSYS) &&
755			    (unsigned)ival < sizeof(jailsys_values) /
756			    sizeof(jailsys_values[0])) {
757				strlcpy(valbuf, jailsys_values[ival],
758				    sizeof(valbuf));
759				break;
760			}
761			snprintf(valbuf, sizeof(valbuf), "%d", ival);
762			break;
763		case CTLTYPE_UINT:
764			snprintf(valbuf, sizeof(valbuf), "%u",
765			    ((unsigned *)jp->jp_value)[i]);
766			break;
767		case CTLTYPE_LONG:
768			snprintf(valbuf, sizeof(valbuf), "%ld",
769			    ((long *)jp->jp_value)[i]);
770			break;
771		case CTLTYPE_ULONG:
772			snprintf(valbuf, sizeof(valbuf), "%lu",
773			    ((unsigned long *)jp->jp_value)[i]);
774			break;
775		case CTLTYPE_S64:
776			snprintf(valbuf, sizeof(valbuf), "%jd",
777			    (intmax_t)((int64_t *)jp->jp_value)[i]);
778			break;
779		case CTLTYPE_U64:
780			snprintf(valbuf, sizeof(valbuf), "%ju",
781			    (uintmax_t)((uint64_t *)jp->jp_value)[i]);
782			break;
783		case CTLTYPE_STRUCT:
784			switch (jp->jp_structtype) {
785			case JPS_IN_ADDR:
786				if (inet_ntop(AF_INET,
787				    &((struct in_addr *)jp->jp_value)[i],
788				    valbuf, sizeof(valbuf)) == NULL) {
789					strerror_r(errno, jail_errmsg,
790					    JAIL_ERRMSGLEN);
791					return (NULL);
792				}
793				break;
794			case JPS_IN6_ADDR:
795				if (inet_ntop(AF_INET6,
796				    &((struct in6_addr *)jp->jp_value)[i],
797				    valbuf, sizeof(valbuf)) == NULL) {
798					strerror_r(errno, jail_errmsg,
799					    JAIL_ERRMSGLEN);
800					return (NULL);
801				}
802				break;
803			default:
804				goto unknown_type;
805			}
806			break;
807		default:
808		unknown_type:
809			snprintf(jail_errmsg, JAIL_ERRMSGLEN,
810			    "unknown type for %s", jp->jp_name);
811			errno = ENOENT;
812			return (NULL);
813		}
814		valuelens[i] = strlen(valbuf) + 1;
815		valuelen += valuelens[i];
816		values[i] = alloca(valuelens[i]);
817		strcpy(values[i], valbuf);
818	}
819	value = malloc(valuelen);
820	if (value == NULL)
821		strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
822	else {
823		tvalue = value;
824		for (i = 0; i < nval; i++) {
825			strcpy(tvalue, values[i]);
826			if (i < nval - 1) {
827				tvalue += valuelens[i];
828				tvalue[-1] = ',';
829			}
830		}
831	}
832	return (value);
833}
834
835/*
836 * Free the contents of a jail parameter list (but not the list itself).
837 */
838void
839jailparam_free(struct jailparam *jp, unsigned njp)
840{
841	unsigned j;
842
843	for (j = 0; j < njp; j++) {
844		free(jp[j].jp_name);
845		if (!(jp[j].jp_flags & JP_RAWVALUE))
846			free(jp[j].jp_value);
847	}
848}
849
850/*
851 * Find a parameter's type and size from its MIB.
852 */
853static int
854jailparam_type(struct jailparam *jp)
855{
856	char *p, *nname;
857	size_t miblen, desclen;
858	int isarray;
859	struct {
860	    int i;
861	    char s[MAXPATHLEN];
862	} desc;
863	int mib[CTL_MAXNAME];
864
865	/* The "lastjid" parameter isn't real. */
866	if (!strcmp(jp->jp_name, "lastjid")) {
867		jp->jp_valuelen = sizeof(int);
868		jp->jp_ctltype = CTLTYPE_INT | CTLFLAG_WR;
869		return (0);
870	}
871
872	/* Find the sysctl that describes the parameter. */
873	mib[0] = 0;
874	mib[1] = 3;
875	snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", jp->jp_name);
876	miblen = sizeof(mib) - 2 * sizeof(int);
877	if (sysctl(mib, 2, mib + 2, &miblen, desc.s, strlen(desc.s)) < 0) {
878		if (errno != ENOENT) {
879			snprintf(jail_errmsg, JAIL_ERRMSGLEN,
880			    "sysctl(0.3.%s): %s", jp->jp_name, strerror(errno));
881			return (-1);
882		}
883		/*
884		 * The parameter probably doesn't exist.  But it might be
885		 * the "no" counterpart to a boolean.
886		 */
887		nname = nononame(jp->jp_name);
888		if (nname != NULL) {
889			snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", nname);
890			free(nname);
891			miblen = sizeof(mib) - 2 * sizeof(int);
892			if (sysctl(mib, 2, mib + 2, &miblen, desc.s,
893			    strlen(desc.s)) >= 0) {
894				mib[1] = 4;
895				desclen = sizeof(desc);
896				if (sysctl(mib, (miblen / sizeof(int)) + 2,
897				    &desc, &desclen, NULL, 0) < 0) {
898					snprintf(jail_errmsg,
899					    JAIL_ERRMSGLEN,
900					    "sysctl(0.4.%s): %s", desc.s,
901					    strerror(errno));
902					return (-1);
903				}
904				if ((desc.i & CTLTYPE) == CTLTYPE_INT &&
905				    desc.s[0] == 'B') {
906					jp->jp_ctltype = desc.i;
907					jp->jp_flags |= JP_NOBOOL;
908					jp->jp_valuelen = sizeof(int);
909					return (0);
910				}
911			}
912		}
913	unknown_parameter:
914		snprintf(jail_errmsg, JAIL_ERRMSGLEN,
915		    "unknown parameter: %s", jp->jp_name);
916		errno = ENOENT;
917		return (-1);
918	}
919 mib_desc:
920	mib[1] = 4;
921	desclen = sizeof(desc);
922	if (sysctl(mib, (miblen / sizeof(int)) + 2, &desc, &desclen,
923	    NULL, 0) < 0) {
924		snprintf(jail_errmsg, JAIL_ERRMSGLEN,
925		    "sysctl(0.4.%s): %s", jp->jp_name, strerror(errno));
926		return (-1);
927	}
928	/* See if this is an array type. */
929	p = strchr(desc.s, '\0');
930	isarray  = 0;
931	if (p - 2 < desc.s || strcmp(p - 2, ",a"))
932		isarray = 0;
933	else {
934		isarray = 1;
935		p[-2] = 0;
936	}
937	/* Look for types we understand. */
938	jp->jp_ctltype = desc.i;
939	switch (desc.i & CTLTYPE) {
940	case CTLTYPE_INT:
941		if (desc.s[0] == 'B')
942			jp->jp_flags |= JP_BOOL;
943		else if (!strcmp(desc.s, "E,jailsys"))
944			jp->jp_flags |= JP_JAILSYS;
945	case CTLTYPE_UINT:
946		jp->jp_valuelen = sizeof(int);
947		break;
948	case CTLTYPE_LONG:
949	case CTLTYPE_ULONG:
950		jp->jp_valuelen = sizeof(long);
951		break;
952	case CTLTYPE_S64:
953	case CTLTYPE_U64:
954		jp->jp_valuelen = sizeof(int64_t);
955		break;
956	case CTLTYPE_STRING:
957		desc.s[0] = 0;
958		desclen = sizeof(desc.s);
959		if (sysctl(mib + 2, miblen / sizeof(int), desc.s, &desclen,
960		    NULL, 0) < 0) {
961			snprintf(jail_errmsg, JAIL_ERRMSGLEN,
962			    "sysctl(" SJPARAM ".%s): %s", jp->jp_name,
963			    strerror(errno));
964			return (-1);
965		}
966		jp->jp_valuelen = strtoul(desc.s, NULL, 10);
967		break;
968	case CTLTYPE_STRUCT:
969		if (!strcmp(desc.s, "S,in_addr")) {
970			jp->jp_structtype = JPS_IN_ADDR;
971			jp->jp_valuelen = sizeof(struct in_addr);
972		} else if (!strcmp(desc.s, "S,in6_addr")) {
973			jp->jp_structtype = JPS_IN6_ADDR;
974			jp->jp_valuelen = sizeof(struct in6_addr);
975		} else {
976			desclen = 0;
977			if (sysctl(mib + 2, miblen / sizeof(int),
978			    NULL, &jp->jp_valuelen, NULL, 0) < 0) {
979				snprintf(jail_errmsg, JAIL_ERRMSGLEN,
980				    "sysctl(" SJPARAM ".%s): %s", jp->jp_name,
981				    strerror(errno));
982				return (-1);
983			}
984		}
985		break;
986	case CTLTYPE_NODE:
987		/* A node might be described by an empty-named child. */
988		mib[1] = 1;
989		mib[(miblen / sizeof(int)) + 2] =
990		    mib[(miblen / sizeof(int)) + 1] - 1;
991		miblen += sizeof(int);
992		desclen = sizeof(desc.s);
993		if (sysctl(mib, (miblen / sizeof(int)) + 2, desc.s, &desclen,
994		    NULL, 0) < 0) {
995			snprintf(jail_errmsg, JAIL_ERRMSGLEN,
996			    "sysctl(0.1): %s", strerror(errno));
997			return (-1);
998		}
999		if (desc.s[desclen - 2] != '.')
1000			goto unknown_parameter;
1001		goto mib_desc;
1002	default:
1003		snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1004		    "unknown type for %s", jp->jp_name);
1005		errno = ENOENT;
1006		return (-1);
1007	}
1008	if (isarray) {
1009		jp->jp_elemlen = jp->jp_valuelen;
1010		jp->jp_valuelen = 0;
1011	}
1012	return (0);
1013}
1014
1015/*
1016 * Change a boolean parameter name into its "no" counterpart or vice versa.
1017 */
1018static char *
1019noname(const char *name)
1020{
1021	char *nname, *p;
1022
1023	nname = malloc(strlen(name) + 3);
1024	if (nname == NULL) {
1025		strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1026		return (NULL);
1027	}
1028	p = strrchr(name, '.');
1029	if (p != NULL)
1030		sprintf(nname, "%.*s.no%s", (int)(p - name), name, p + 1);
1031	else
1032		sprintf(nname, "no%s", name);
1033	return (nname);
1034}
1035
1036static char *
1037nononame(const char *name)
1038{
1039	char *p, *nname;
1040
1041	p = strrchr(name, '.');
1042	if (strncmp(p ? p + 1 : name, "no", 2)) {
1043		snprintf(jail_errmsg, sizeof(jail_errmsg),
1044		    "mismatched boolean: %s", name);
1045		errno = EINVAL;
1046		return (NULL);
1047	}
1048	nname = malloc(strlen(name) - 1);
1049	if (nname == NULL) {
1050		strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1051		return (NULL);
1052	}
1053	if (p != NULL)
1054		sprintf(nname, "%.*s.%s", (int)(p - name), name, p + 3);
1055	else
1056		strcpy(nname, name + 2);
1057	return (nname);
1058}
1059