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