2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 James Gritton.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
38 #include <arpa/inet.h>
39 #include <netinet/in.h>
50 #define SJPARAM "security.jail.param"
53 #define JPS_IN6_ADDR 2
55 #define ARRAY_SANITY 5
59 static int jailparam_import_enum(const char **values, int nvalues,
60 const char *valstr, size_t valsize, int *value);
61 static int jailparam_type(struct jailparam *jp);
62 static char *noname(const char *name);
63 static char *nononame(const char *name);
65 char jail_errmsg[JAIL_ERRMSGLEN];
67 static const char *bool_values[] = { "false", "true" };
68 static const char *jailsys_values[] = { "disable", "new", "inherit" };
72 * Import a null-terminated parameter list and set a jail with the flags
76 jail_setv(int flags, ...)
80 const char *name, *value;
83 /* Create the parameter list and import the parameters. */
86 for (njp = 0; va_arg(tap, char *) != NULL; njp++)
87 (void)va_arg(tap, char *);
89 jp = alloca(njp * sizeof(struct jailparam));
90 for (njp = 0; (name = va_arg(ap, char *)) != NULL;) {
91 value = va_arg(ap, char *);
92 if (jailparam_init(jp + njp, name) < 0)
94 if (jailparam_import(jp + njp++, value) < 0)
98 jid = jailparam_set(jp, njp, flags);
99 jailparam_free(jp, njp);
103 jailparam_free(jp, njp);
109 * Read a null-terminated parameter list, get the referenced jail, and export
110 * the parameters to the list.
113 jail_getv(int flags, ...)
116 struct jailparam *jp, *jp_lastjid, *jp_jid, *jp_name, *jp_key;
117 char *valarg, *value;
118 const char *name, *key_value, *lastjid_value, *jid_value, *name_value;
121 /* Create the parameter list and find the key. */
124 for (njp = 0; va_arg(tap, char *) != NULL; njp++)
125 (void)va_arg(tap, char *);
128 jp = alloca(njp * sizeof(struct jailparam));
130 jp_lastjid = jp_jid = jp_name = NULL;
131 lastjid_value = jid_value = name_value = NULL;
132 for (njp = 0; (name = va_arg(tap, char *)) != NULL; njp++) {
133 value = va_arg(tap, char *);
134 if (jailparam_init(jp + njp, name) < 0) {
138 if (!strcmp(jp[njp].jp_name, "lastjid")) {
139 jp_lastjid = jp + njp;
140 lastjid_value = value;
141 } else if (!strcmp(jp[njp].jp_name, "jid")) {
144 } if (!strcmp(jp[njp].jp_name, "name")) {
150 /* Import the key parameter. */
151 if (jp_lastjid != NULL) {
153 key_value = lastjid_value;
154 } else if (jp_jid != NULL && strtol(jid_value, NULL, 10) != 0) {
156 key_value = jid_value;
157 } else if (jp_name != NULL) {
159 key_value = name_value;
161 strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN);
165 if (jailparam_import(jp_key, key_value) < 0)
167 /* Get the jail and export the parameters. */
168 jid = jailparam_get(jp, njp, flags);
171 for (i = 0; i < njp; i++) {
172 (void)va_arg(ap, char *);
173 valarg = va_arg(ap, char *);
174 if (jp + i != jp_key) {
175 /* It's up to the caller to ensure there's room. */
176 if ((jp[i].jp_ctltype & CTLTYPE) == CTLTYPE_STRING)
177 strcpy(valarg, jp[i].jp_value);
179 value = jailparam_export(jp + i);
182 strcpy(valarg, value);
187 jailparam_free(jp, njp);
192 jailparam_free(jp, njp);
198 * Return a list of all known parameters.
201 jailparam_all(struct jailparam **jpp)
203 struct jailparam *jp, *tjp;
204 size_t mlen1, mlen2, buflen;
206 int mib1[CTL_MAXNAME], mib2[CTL_MAXNAME - 2];
207 char buf[MAXPATHLEN];
211 jp = malloc(nlist * sizeof(*jp));
213 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
218 mlen1 = CTL_MAXNAME - 2;
219 if (sysctlnametomib(SJPARAM, mib1 + 2, &mlen1) < 0) {
220 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
221 "sysctlnametomib(" SJPARAM "): %s", strerror(errno));
225 /* Get the next parameter. */
226 mlen2 = sizeof(mib2);
227 if (sysctl(mib1, mlen1 + 2, mib2, &mlen2, NULL, 0) < 0) {
228 if (errno == ENOENT) {
229 /* No more entries. */
232 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
233 "sysctl(0.2): %s", strerror(errno));
236 if (mib2[0] != mib1[2] ||
237 mib2[1] != mib1[3] ||
240 /* Convert it to an ascii name. */
241 memcpy(mib1 + 2, mib2, mlen2);
242 mlen1 = mlen2 / sizeof(int);
244 buflen = sizeof(buf);
245 if (sysctl(mib1, mlen1 + 2, buf, &buflen, NULL, 0) < 0) {
246 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
247 "sysctl(0.1): %s", strerror(errno));
250 if (buf[buflen - 2] == '.')
251 buf[buflen - 2] = '\0';
252 /* Add the parameter to the list */
255 tjp = reallocarray(jp, nlist, sizeof(*jp));
260 if (jailparam_init(jp + njp, buf + sizeof(SJPARAM)) < 0)
264 jp = reallocarray(jp, njp, sizeof(*jp));
269 jailparam_free(jp, njp);
275 * Clear a jail parameter and copy in its name.
278 jailparam_init(struct jailparam *jp, const char *name)
281 memset(jp, 0, sizeof(*jp));
282 jp->jp_name = strdup(name);
283 if (jp->jp_name == NULL) {
284 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
287 if (jailparam_type(jp) < 0) {
288 jailparam_free(jp, 1);
297 * Put a name and value into a jail parameter element, converting the value
301 jailparam_import(struct jailparam *jp, const char *value)
303 char *p, *ep, *tvalue;
309 if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
310 jp->jp_value = strdup(value);
311 if (jp->jp_value == NULL) {
312 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
318 if (jp->jp_elemlen) {
319 if (value[0] == '\0' || (value[0] == '-' && value[1] == '\0')) {
320 jp->jp_value = strdup("");
321 if (jp->jp_value == NULL) {
322 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
328 for (p = strchr(value, ','); p; p = strchr(p + 1, ','))
330 jp->jp_valuelen = jp->jp_elemlen * nval;
332 jp->jp_value = malloc(jp->jp_valuelen);
333 if (jp->jp_value == NULL) {
334 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
338 for (i = 0; i < nval; i++) {
339 fw = nval == 1 ? strlen(avalue) : strcspn(avalue, ",");
340 switch (jp->jp_ctltype & CTLTYPE) {
342 if (jp->jp_flags & (JP_BOOL | JP_NOBOOL)) {
343 if (!jailparam_import_enum(bool_values, 2,
344 avalue, fw, &((int *)jp->jp_value)[i])) {
345 snprintf(jail_errmsg,
346 JAIL_ERRMSGLEN, "%s: "
347 "unknown boolean value \"%.*s\"",
348 jp->jp_name, fw, avalue);
354 if (jp->jp_flags & JP_JAILSYS) {
356 * Allow setting a jailsys parameter to "new"
357 * in a booleanesque fashion.
359 if (value[0] == '\0')
360 ((int *)jp->jp_value)[i] = JAIL_SYS_NEW;
361 else if (!jailparam_import_enum(jailsys_values,
362 sizeof(jailsys_values) /
363 sizeof(jailsys_values[0]), avalue, fw,
364 &((int *)jp->jp_value)[i])) {
365 snprintf(jail_errmsg,
366 JAIL_ERRMSGLEN, "%s: "
367 "unknown jailsys value \"%.*s\"",
368 jp->jp_name, fw, avalue);
374 ((int *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
376 if (ep != avalue + fw) {
377 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
378 "%s: non-integer value \"%.*s\"",
379 jp->jp_name, fw, avalue);
385 ((unsigned *)jp->jp_value)[i] =
386 strtoul(avalue, &ep, 10);
389 ((long *)jp->jp_value)[i] = strtol(avalue, &ep, 10);
392 ((unsigned long *)jp->jp_value)[i] =
393 strtoul(avalue, &ep, 10);
396 ((int64_t *)jp->jp_value)[i] =
397 strtoimax(avalue, &ep, 10);
400 ((uint64_t *)jp->jp_value)[i] =
401 strtoumax(avalue, &ep, 10);
404 tvalue = alloca(fw + 1);
405 strlcpy(tvalue, avalue, fw + 1);
406 switch (jp->jp_structtype) {
408 if (inet_pton(AF_INET, tvalue,
409 &((struct in_addr *)jp->jp_value)[i]) != 1)
411 snprintf(jail_errmsg,
413 "%s: not an IPv4 address: %s",
414 jp->jp_name, tvalue);
420 if (inet_pton(AF_INET6, tvalue,
421 &((struct in6_addr *)jp->jp_value)[i]) != 1)
423 snprintf(jail_errmsg,
425 "%s: not an IPv6 address: %s",
426 jp->jp_name, tvalue);
437 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
438 "unknown type for %s", jp->jp_name);
453 jailparam_import_enum(const char **values, int nvalues, const char *valstr,
454 size_t valsize, int *value)
459 for (i = 0; i < nvalues; i++)
460 if (valsize == strlen(values[i]) &&
461 !strncasecmp(valstr, values[i], valsize)) {
465 *value = strtol(valstr, &ep, 10);
466 return (ep == valstr + valsize);
470 * Put a name and value into a jail parameter element, copying the value
471 * but not altering it.
474 jailparam_import_raw(struct jailparam *jp, void *value, size_t valuelen)
477 jp->jp_value = value;
478 jp->jp_valuelen = valuelen;
479 jp->jp_flags |= JP_RAWVALUE;
484 * Run the jail_set and jail_get system calls on a parameter list.
487 jailparam_set(struct jailparam *jp, unsigned njp, int flags)
494 jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
496 for (i = j = 0; j < njp; j++) {
497 jiov[i].iov_base = jp[j].jp_name;
498 jiov[i].iov_len = strlen(jp[j].jp_name) + 1;
500 if (jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) {
502 * Set booleans without values. If one has a value of
503 * zero, change it to (or from) its "no" counterpart.
505 jiov[i].iov_base = NULL;
507 if (jp[j].jp_value != NULL &&
508 jp[j].jp_valuelen == sizeof(int) &&
509 !*(int *)jp[j].jp_value) {
511 nname = jp[j].jp_flags & JP_BOOL
512 ? noname(jp[j].jp_name)
513 : nononame(jp[j].jp_name);
519 jiov[i - 1].iov_base = nname;
520 jiov[i - 1].iov_len = strlen(nname) + 1;
525 * Try to fill in missing values with an empty string.
527 if (jp[j].jp_value == NULL && jp[j].jp_valuelen > 0 &&
528 jailparam_import(jp + j, "") < 0) {
533 jiov[i].iov_base = jp[j].jp_value;
535 (jp[j].jp_ctltype & CTLTYPE) == CTLTYPE_STRING
536 ? strlen(jp[j].jp_value) + 1
541 jiov[i].iov_base = __DECONST(char *, "errmsg");
542 jiov[i].iov_len = sizeof("errmsg");
544 jiov[i].iov_base = jail_errmsg;
545 jiov[i].iov_len = JAIL_ERRMSGLEN;
548 jid = jail_set(jiov, i, flags);
549 if (jid < 0 && !jail_errmsg[0])
550 snprintf(jail_errmsg, sizeof(jail_errmsg), "jail_set: %s",
554 for (j = 0; j < njp; j++)
555 if ((jp[j].jp_flags & (JP_BOOL | JP_NOBOOL)) &&
556 jp[j].jp_value != NULL &&
557 jp[j].jp_valuelen == sizeof(int) &&
558 !*(int *)jp[j].jp_value)
559 free(jiov[j * 2].iov_base);
564 jailparam_get(struct jailparam *jp, unsigned njp, int flags)
567 struct jailparam *jp_lastjid, *jp_jid, *jp_name, *jp_key;
568 int i, ai, ki, jid, arrays, sanity;
572 * Get the types for all parameters.
573 * Find the key and any array parameters.
575 jiov = alloca(sizeof(struct iovec) * 2 * (njp + 1));
576 jp_lastjid = jp_jid = jp_name = NULL;
578 for (ai = j = 0; j < njp; j++) {
579 if (!strcmp(jp[j].jp_name, "lastjid"))
581 else if (!strcmp(jp[j].jp_name, "jid"))
583 else if (!strcmp(jp[j].jp_name, "name"))
585 else if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
587 jiov[ai].iov_base = jp[j].jp_name;
588 jiov[ai].iov_len = strlen(jp[j].jp_name) + 1;
590 jiov[ai].iov_base = NULL;
591 jiov[ai].iov_len = 0;
595 jp_key = jp_lastjid ? jp_lastjid :
596 jp_jid && jp_jid->jp_valuelen == sizeof(int) &&
597 jp_jid->jp_value && *(int *)jp_jid->jp_value ? jp_jid : jp_name;
598 if (jp_key == NULL || jp_key->jp_value == NULL) {
599 strlcpy(jail_errmsg, "no jail specified", JAIL_ERRMSGLEN);
604 jiov[ki].iov_base = jp_key->jp_name;
605 jiov[ki].iov_len = strlen(jp_key->jp_name) + 1;
607 jiov[ki].iov_base = jp_key->jp_value;
608 jiov[ki].iov_len = (jp_key->jp_ctltype & CTLTYPE) == CTLTYPE_STRING
609 ? strlen(jp_key->jp_value) + 1 : jp_key->jp_valuelen;
611 jiov[ki].iov_base = __DECONST(char *, "errmsg");
612 jiov[ki].iov_len = sizeof("errmsg");
614 jiov[ki].iov_base = jail_errmsg;
615 jiov[ki].iov_len = JAIL_ERRMSGLEN;
618 if (arrays && jail_get(jiov, ki, flags) < 0) {
620 snprintf(jail_errmsg, sizeof(jail_errmsg),
621 "jail_get: %s", strerror(errno));
624 /* Allocate storage for all parameters. */
625 for (ai = j = 0, i = ki; j < njp; j++) {
626 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
628 jiov[ai].iov_len += jp[j].jp_elemlen * ARRAY_SLOP;
629 if (jp[j].jp_valuelen >= jiov[ai].iov_len)
630 jiov[ai].iov_len = jp[j].jp_valuelen;
632 jp[j].jp_valuelen = jiov[ai].iov_len;
633 if (jp[j].jp_value != NULL)
634 free(jp[j].jp_value);
635 jp[j].jp_value = malloc(jp[j].jp_valuelen);
636 if (jp[j].jp_value == NULL) {
637 strerror_r(errno, jail_errmsg,
642 jiov[ai].iov_base = jp[j].jp_value;
643 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
645 } else if (jp + j != jp_key) {
646 jiov[i].iov_base = jp[j].jp_name;
647 jiov[i].iov_len = strlen(jp[j].jp_name) + 1;
649 if (jp[j].jp_value == NULL &&
650 !(jp[j].jp_flags & JP_RAWVALUE)) {
651 jp[j].jp_value = malloc(jp[j].jp_valuelen);
652 if (jp[j].jp_value == NULL) {
653 strerror_r(errno, jail_errmsg,
658 jiov[i].iov_base = jp[j].jp_value;
659 jiov[i].iov_len = jp[j].jp_valuelen;
660 memset(jiov[i].iov_base, 0, jiov[i].iov_len);
665 * Get the prison. If there are array elements, retry a few times
666 * in case their sizes changed from under us.
668 for (sanity = 0;; sanity++) {
669 jid = jail_get(jiov, i, flags);
670 if (jid >= 0 || !arrays || sanity == ARRAY_SANITY ||
671 errno != EINVAL || jail_errmsg[0])
673 for (ai = j = 0; j < njp; j++) {
674 if (jp[j].jp_elemlen &&
675 !(jp[j].jp_flags & JP_RAWVALUE)) {
677 jiov[ai].iov_base = NULL;
678 jiov[ai].iov_len = 0;
682 if (jail_get(jiov, ki, flags) < 0)
684 for (ai = j = 0; j < njp; j++) {
685 if (jp[j].jp_elemlen &&
686 !(jp[j].jp_flags & JP_RAWVALUE)) {
689 jp[j].jp_elemlen * ARRAY_SLOP;
690 if (jp[j].jp_valuelen >= jiov[ai].iov_len)
691 jiov[ai].iov_len = jp[j].jp_valuelen;
693 jp[j].jp_valuelen = jiov[ai].iov_len;
694 if (jp[j].jp_value != NULL)
695 free(jp[j].jp_value);
697 malloc(jiov[ai].iov_len);
698 if (jp[j].jp_value == NULL) {
699 strerror_r(errno, jail_errmsg,
704 jiov[ai].iov_base = jp[j].jp_value;
705 memset(jiov[ai].iov_base, 0, jiov[ai].iov_len);
710 if (jid < 0 && !jail_errmsg[0])
711 snprintf(jail_errmsg, sizeof(jail_errmsg),
712 "jail_get: %s", strerror(errno));
713 for (ai = j = 0, i = ki; j < njp; j++) {
714 if (jp[j].jp_elemlen && !(jp[j].jp_flags & JP_RAWVALUE)) {
716 jp[j].jp_valuelen = jiov[ai].iov_len;
718 } else if (jp + j != jp_key) {
720 jp[j].jp_valuelen = jiov[i].iov_len;
728 * Convert a jail parameter's value to external form.
731 jailparam_export(struct jailparam *jp)
734 char *value, *tvalue, **values;
737 char valbuf[INET6_ADDRSTRLEN];
739 if ((jp->jp_ctltype & CTLTYPE) == CTLTYPE_STRING) {
740 value = strdup(jp->jp_value);
742 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
745 nval = jp->jp_elemlen ? jp->jp_valuelen / jp->jp_elemlen : 1;
749 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
752 values = alloca(nval * sizeof(char *));
753 valuelens = alloca(nval * sizeof(size_t));
755 for (i = 0; i < nval; i++) {
756 switch (jp->jp_ctltype & CTLTYPE) {
758 ival = ((int *)jp->jp_value)[i];
759 if ((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
760 (unsigned)ival < 2) {
761 strlcpy(valbuf, bool_values[ival],
765 if ((jp->jp_flags & JP_JAILSYS) &&
766 (unsigned)ival < sizeof(jailsys_values) /
767 sizeof(jailsys_values[0])) {
768 strlcpy(valbuf, jailsys_values[ival],
772 snprintf(valbuf, sizeof(valbuf), "%d", ival);
775 snprintf(valbuf, sizeof(valbuf), "%u",
776 ((unsigned *)jp->jp_value)[i]);
779 snprintf(valbuf, sizeof(valbuf), "%ld",
780 ((long *)jp->jp_value)[i]);
783 snprintf(valbuf, sizeof(valbuf), "%lu",
784 ((unsigned long *)jp->jp_value)[i]);
787 snprintf(valbuf, sizeof(valbuf), "%jd",
788 (intmax_t)((int64_t *)jp->jp_value)[i]);
791 snprintf(valbuf, sizeof(valbuf), "%ju",
792 (uintmax_t)((uint64_t *)jp->jp_value)[i]);
795 switch (jp->jp_structtype) {
797 if (inet_ntop(AF_INET,
798 &((struct in_addr *)jp->jp_value)[i],
799 valbuf, sizeof(valbuf)) == NULL) {
800 strerror_r(errno, jail_errmsg,
806 if (inet_ntop(AF_INET6,
807 &((struct in6_addr *)jp->jp_value)[i],
808 valbuf, sizeof(valbuf)) == NULL) {
809 strerror_r(errno, jail_errmsg,
820 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
821 "unknown type for %s", jp->jp_name);
825 valuelens[i] = strlen(valbuf) + 1;
826 valuelen += valuelens[i];
827 values[i] = alloca(valuelens[i]);
828 strcpy(values[i], valbuf);
830 value = malloc(valuelen);
832 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
835 for (i = 0; i < nval; i++) {
836 strcpy(tvalue, values[i]);
838 tvalue += valuelens[i];
847 * Free the contents of a jail parameter list (but not the list itself).
850 jailparam_free(struct jailparam *jp, unsigned njp)
854 for (j = 0; j < njp; j++) {
856 if (!(jp[j].jp_flags & JP_RAWVALUE))
857 free(jp[j].jp_value);
862 * Find a parameter's type and size from its MIB.
865 jailparam_type(struct jailparam *jp)
867 char *p, *name, *nname;
868 size_t miblen, desclen;
874 int mib[CTL_MAXNAME];
876 /* The "lastjid" parameter isn't real. */
878 if (!strcmp(name, "lastjid")) {
879 jp->jp_valuelen = sizeof(int);
880 jp->jp_ctltype = CTLTYPE_INT | CTLFLAG_WR;
884 /* Find the sysctl that describes the parameter. */
887 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", name);
888 miblen = sizeof(mib) - 2 * sizeof(int);
889 if (sysctl(mib, 2, mib + 2, &miblen, desc.s, strlen(desc.s)) < 0) {
890 if (errno != ENOENT) {
891 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
892 "sysctl(0.3.%s): %s", name, strerror(errno));
896 * The parameter probably doesn't exist. But it might be
897 * the "no" counterpart to a boolean.
899 nname = nononame(name);
902 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
903 "unknown parameter: %s", jp->jp_name);
907 name = alloca(strlen(nname) + 1);
910 snprintf(desc.s, sizeof(desc.s), SJPARAM ".%s", name);
911 miblen = sizeof(mib) - 2 * sizeof(int);
912 if (sysctl(mib, 2, mib + 2, &miblen, desc.s,
914 goto unknown_parameter;
915 jp->jp_flags |= JP_NOBOOL;
919 desclen = sizeof(desc);
920 if (sysctl(mib, (miblen / sizeof(int)) + 2, &desc, &desclen,
922 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
923 "sysctl(0.4.%s): %s", name, strerror(errno));
926 jp->jp_ctltype = desc.i;
927 /* If this came from removing a "no", it better be a boolean. */
928 if (jp->jp_flags & JP_NOBOOL) {
929 if ((desc.i & CTLTYPE) == CTLTYPE_INT && desc.s[0] == 'B') {
930 jp->jp_valuelen = sizeof(int);
933 else if ((desc.i & CTLTYPE) != CTLTYPE_NODE)
934 goto unknown_parameter;
936 /* See if this is an array type. */
937 p = strchr(desc.s, '\0');
939 if (p - 2 < desc.s || strcmp(p - 2, ",a"))
945 /* Look for types we understand. */
946 switch (desc.i & CTLTYPE) {
948 if (desc.s[0] == 'B')
949 jp->jp_flags |= JP_BOOL;
950 else if (!strcmp(desc.s, "E,jailsys"))
951 jp->jp_flags |= JP_JAILSYS;
953 jp->jp_valuelen = sizeof(int);
957 jp->jp_valuelen = sizeof(long);
961 jp->jp_valuelen = sizeof(int64_t);
965 desclen = sizeof(desc.s);
966 if (sysctl(mib + 2, miblen / sizeof(int), desc.s, &desclen,
968 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
969 "sysctl(" SJPARAM ".%s): %s", name,
973 jp->jp_valuelen = strtoul(desc.s, NULL, 10);
976 if (!strcmp(desc.s, "S,in_addr")) {
977 jp->jp_structtype = JPS_IN_ADDR;
978 jp->jp_valuelen = sizeof(struct in_addr);
979 } else if (!strcmp(desc.s, "S,in6_addr")) {
980 jp->jp_structtype = JPS_IN6_ADDR;
981 jp->jp_valuelen = sizeof(struct in6_addr);
984 if (sysctl(mib + 2, miblen / sizeof(int),
985 NULL, &jp->jp_valuelen, NULL, 0) < 0) {
986 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
987 "sysctl(" SJPARAM ".%s): %s", name,
995 * A node might be described by an empty-named child,
996 * which would be immediately before or after the node itself.
999 miblen += sizeof(int);
1000 for (i = -1; i <= 1; i += 2) {
1001 mib[(miblen / sizeof(int)) + 1] =
1002 mib[(miblen / sizeof(int))] + i;
1003 desclen = sizeof(desc.s);
1004 if (sysctl(mib, (miblen / sizeof(int)) + 2, desc.s,
1005 &desclen, NULL, 0) < 0) {
1006 if (errno == ENOENT)
1008 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1009 "sysctl(0.1): %s", strerror(errno));
1012 if (desclen == sizeof(SJPARAM) + strlen(name) + 2 &&
1013 memcmp(SJPARAM ".", desc.s, sizeof(SJPARAM)) == 0 &&
1014 memcmp(name, desc.s + sizeof(SJPARAM),
1015 desclen - sizeof(SJPARAM) - 2) == 0 &&
1016 desc.s[desclen - 2] == '.')
1019 goto unknown_parameter;
1021 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1022 "unknown type for %s", jp->jp_name);
1027 jp->jp_elemlen = jp->jp_valuelen;
1028 jp->jp_valuelen = 0;
1034 * Change a boolean parameter name into its "no" counterpart or vice versa.
1037 noname(const char *name)
1041 nname = malloc(strlen(name) + 3);
1042 if (nname == NULL) {
1043 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1046 p = strrchr(name, '.');
1048 sprintf(nname, "%.*s.no%s", (int)(p - name), name, p + 1);
1050 sprintf(nname, "no%s", name);
1055 nononame(const char *name)
1059 p = strrchr(name, '.');
1060 if (strncmp(p ? p + 1 : name, "no", 2)) {
1061 snprintf(jail_errmsg, sizeof(jail_errmsg),
1062 "mismatched boolean: %s", name);
1066 nname = malloc(strlen(name) - 1);
1067 if (nname == NULL) {
1068 strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
1072 sprintf(nname, "%.*s.%s", (int)(p - name), name, p + 3);
1074 strcpy(nname, name + 2);