2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/mount.h>
45 #define MNTTYPE_ZFS 222
47 static void jailparam_add(const char *name, const char *val);
48 static int jailparam_del(const char *name);
49 static bool jailparam_addarg(char *arg);
50 static int jailparam_delarg(char *arg);
52 static int bectl_search_jail_paths(const char *mnt);
53 static int bectl_locate_jail(const char *ident);
54 static int bectl_jail_cleanup(char *mountpoint, int jid);
56 static char mnt_loc[BE_MAXPATHLEN];
57 static nvlist_t *jailparams;
59 static const char *disabled_params[] = {
60 "command", "exec.start", "nopersist", "persist", NULL
65 jailparam_add(const char *name, const char *val)
68 nvlist_add_string(jailparams, name, val);
72 jailparam_del(const char *name)
75 nvlist_remove_all(jailparams, name);
80 jailparam_addarg(char *arg)
88 if ((val = strchr(arg, '=')) == NULL) {
89 fprintf(stderr, "bectl jail: malformed jail option '%s'\n",
95 if (strcmp(name, "path") == 0) {
96 if (strlen(val) >= BE_MAXPATHLEN) {
98 "bectl jail: skipping too long path assignment '%s' (max length = %d)\n",
102 strlcpy(mnt_loc, val, sizeof(mnt_loc));
105 for (i = 0; disabled_params[i] != NULL; i++) {
106 len = strlen(disabled_params[i]);
107 if (strncmp(disabled_params[i], name, len) == 0) {
108 fprintf(stderr, "invalid jail parameter: %s\n", name);
113 jailparam_add(name, val);
118 jailparam_delarg(char *arg)
125 if ((val = strchr(name, '=')) != NULL)
128 if (strcmp(name, "path") == 0)
130 return (jailparam_del(name));
134 build_jailcmd(char ***argvp, bool interactive, int argc, char *argv[])
136 char *cmd, **jargv, *name, *val;
138 size_t i, iarg, nargv;
143 if (nvlist_size(jailparams, &nargv, NV_ENCODE_NATIVE) != 0)
147 * Number of args + "/usr/sbin/jail", "-c", and ending NULL.
148 * If interactive also include command.
158 jargv = *argvp = calloc(nargv, sizeof(*jargv));
162 jargv[iarg++] = strdup("/usr/sbin/jail");
163 jargv[iarg++] = strdup("-c");
164 while ((nvp = nvlist_next_nvpair(jailparams, nvp)) != NULL) {
165 name = nvpair_name(nvp);
166 if (nvpair_value_string(nvp, &val) != 0)
169 if (asprintf(&jargv[iarg++], "%s=%s", name, val) < 0)
174 cmd = strdup("/bin/sh");
181 if (asprintf(&jargv[iarg++], "command=%s", cmd) < 0) {
189 for (; argc > 0; argc--) {
190 if (asprintf(&jargv[iarg++], "%s", argv[0]) < 0)
199 if (interactive && argc < 1)
201 for (; i < iarg - 1; i++) {
208 /* Remove jail and cleanup any non zfs mounts. */
210 bectl_jail_cleanup(char *mountpoint, int jid)
212 struct statfs *mntbuf;
213 size_t i, searchlen, mntsize;
215 if (jid >= 0 && jail_remove(jid) != 0) {
216 fprintf(stderr, "unable to remove jail");
220 searchlen = strnlen(mountpoint, MAXPATHLEN);
221 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
222 for (i = 0; i < mntsize; i++) {
223 if (strncmp(mountpoint, mntbuf[i].f_mntonname, searchlen) == 0 &&
224 mntbuf[i].f_type != MNTTYPE_ZFS) {
226 if (unmount(mntbuf[i].f_mntonname, 0) != 0) {
227 fprintf(stderr, "bectl jail: unable to unmount filesystem %s",
228 mntbuf[i].f_mntonname);
238 bectl_cmd_jail(int argc, char *argv[])
240 char *bootenv, **jargv, *mountpoint;
241 int i, jid, mntflags, opt, ret;
242 bool default_hostname, interactive, unjail;
246 /* XXX TODO: Allow shallow */
247 mntflags = BE_MNT_DEEP;
248 default_hostname = interactive = unjail = true;
250 if ((nvlist_alloc(&jailparams, NV_UNIQUE_NAME, 0)) != 0) {
251 fprintf(stderr, "nvlist_alloc() failed\n");
255 jailparam_add("persist", "true");
256 jailparam_add("allow.mount", "true");
257 jailparam_add("allow.mount.devfs", "true");
258 jailparam_add("enforce_statfs", "1");
260 while ((opt = getopt(argc, argv, "bo:Uu:")) != -1) {
266 if (jailparam_addarg(optarg)) {
268 * optarg has been modified to null terminate
269 * at the assignment operator.
271 if (strcmp(optarg, "host.hostname") == 0)
272 default_hostname = false;
281 if ((ret = jailparam_delarg(optarg)) == 0) {
282 if (strcmp(optarg, "host.hostname") == 0)
283 default_hostname = true;
284 } else if (ret != ENOENT) {
286 "bectl jail: error unsetting \"%s\"\n",
292 fprintf(stderr, "bectl jail: unknown option '-%c'\n",
294 return (usage(false));
302 fprintf(stderr, "bectl jail: missing boot environment name\n");
303 return (usage(false));
311 * XXX TODO: if its already mounted, perhaps there should be a flag to
312 * indicate its okay to proceed??
314 if (*mnt_loc == '\0')
317 mountpoint = mnt_loc;
318 if (be_mount(be, bootenv, mountpoint, mntflags, mnt_loc) != BE_ERR_SUCCESS) {
319 fprintf(stderr, "could not mount bootenv\n");
323 if (default_hostname)
324 jailparam_add("host.hostname", bootenv);
327 * This is our indicator that path was not set by the user, so we'll use
328 * the path that libbe generated for us.
330 if (mountpoint == NULL) {
331 jailparam_add("path", mnt_loc);
332 mountpoint = mnt_loc;
335 if ((build_jailcmd(&jargv, interactive, argc, argv)) != 0) {
336 fprintf(stderr, "unable to build argument list for jail command\n");
347 execv("/usr/sbin/jail", jargv);
348 fprintf(stderr, "bectl jail: failed to execute\n");
351 waitpid(pid, NULL, 0);
354 for (i = 0; jargv[i] != NULL; i++) {
364 * We're not checking the jail id result here because in the
365 * case of invalid param, or last command in jail was an error
366 * the jail will not exist upon exit. bectl_jail_cleanup will
367 * only jail_remove if the jid is >= 0.
369 jid = bectl_locate_jail(bootenv);
370 bectl_jail_cleanup(mountpoint, jid);
371 be_unmount(be, bootenv, 0);
378 bectl_search_jail_paths(const char *mnt)
382 char jailpath[MAXPATHLEN];
384 /* jail_getv expects name/value strings */
385 snprintf(lastjid, sizeof(lastjid), "%d", 0);
387 while ((jid = jail_getv(0, "lastjid", lastjid, "path", &jailpath,
390 /* the jail we've been looking for */
391 if (strcmp(jailpath, mnt) == 0)
394 /* update lastjid and keep on looking */
395 snprintf(lastjid, sizeof(lastjid), "%d", jid);
402 * Locate a jail based on an arbitrary identifier. This may be either a name,
403 * a jid, or a BE name. Returns the jid or -1 on failure.
406 bectl_locate_jail(const char *ident)
408 nvlist_t *belist, *props;
412 /* Try the easy-match first */
413 jid = jail_getid(ident);
415 * jail_getid(0) will always return 0, because this prison does exist.
416 * bectl(8) knows that this is not what it wants, so we should fall
417 * back to mount point search.
422 /* Attempt to try it as a BE name, first */
423 if (be_prop_list_alloc(&belist) != 0)
426 if (be_get_bootenv_props(be, belist) != 0)
429 if (nvlist_lookup_nvlist(belist, ident, &props) == 0) {
431 /* path where a boot environment is mounted */
432 if (nvlist_lookup_string(props, "mounted", &mnt) == 0) {
434 /* looking for a jail that matches our bootenv path */
435 jid = bectl_search_jail_paths(mnt);
436 be_prop_list_free(belist);
440 be_prop_list_free(belist);
447 bectl_cmd_unjail(int argc, char *argv[])
449 char path[MAXPATHLEN];
450 char *cmd, *name, *target;
453 /* Store alias used */
457 fprintf(stderr, "bectl %s: wrong number of arguments\n", cmd);
458 return (usage(false));
463 /* Locate the jail */
464 if ((jid = bectl_locate_jail(target)) == -1) {
465 fprintf(stderr, "bectl %s: failed to locate BE by '%s'\n", cmd,
470 bzero(&path, MAXPATHLEN);
471 name = jail_getname(jid);
472 if (jail_getv(0, "name", name, "path", path, NULL) != jid) {
475 "bectl %s: failed to get path for jail requested by '%s'\n",
482 if (be_mounted_at(be, path, NULL) != 0) {
483 fprintf(stderr, "bectl %s: jail requested by '%s' not a BE\n",
488 bectl_jail_cleanup(path, jid);
489 be_unmount(be, target, 0);