2 * Copyright (c) 2002-2004 Marko Zec <zec@fer.hr>
3 * Copyright (c) 2009 University of Zagreb
4 * Copyright (c) 2009 FreeBSD Foundation
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 AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, 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/param.h>
29 #include <sys/ioctl.h>
31 #include <sys/socket.h>
51 typedef struct vimage_status {
52 char name[MAXPATHLEN]; /* Must be first field for strcmp(). */
53 char path[MAXPATHLEN];
54 char hostname[MAXPATHLEN];
55 char domainname[MAXPATHLEN];
67 #define VST_SIZE_STEP 1024
70 static int getjail(vstat_t *, int, int);
72 static char *invocname;
79 "usage: %s [-c | -m] vname [param=value ...]\n"
81 " %s -l[rvj] [vname]\n"
82 " %s -i vname ifname [newifname]\n"
83 " %s vname [command ...]\n",
84 invocname, invocname, invocname, invocname, invocname);
89 main(int argc, char **argv)
91 struct jailparam params[MAXPARAMS];
92 char ifname[IFNAMSIZ];
97 int jid, i, s, namelen;
98 int vst_size, vst_last;
105 newcmd = cmd = VI_SWITCHTO; /* Default if no modifiers specified. */
106 while ((ch = getopt(argc, argv, "cdijlmrv")) != -1) {
135 if (cmd == VI_SWITCHTO || cmd == newcmd)
143 if ((cmd != VI_GET && (argc == 0 || recurse != 0 || verbose != 0)) ||
144 (cmd == VI_IFMOVE && (argc < 2 || argc > 3)) ||
145 (cmd == VI_MODIFY && argc < 2) || argc >= MAXPARAMS)
151 vst_size = VST_SIZE_STEP;
152 if ((vst = malloc(vst_size * sizeof(*vst))) == NULL)
155 namelen = strlen(argv[0]);
159 while ((jid = getjail(&vst[vst_last], jid, verbose)) > 0) {
160 /* Skip jails which do not own vnets. */
161 if (vst[vst_last].vnet != 1)
163 /* Skip non-matching vnames / hierarchies. */
165 ((strlen(vst[vst_last].name) < namelen ||
166 strncmp(vst[vst_last].name, argv[0], namelen) != 0)
167 || (strlen(vst[vst_last].name) > namelen &&
168 vst[vst_last].name[namelen] != '.')))
170 /* Skip any sub-trees if -r not requested. */
172 (strlen(vst[vst_last].name) < namelen ||
173 strchr(&vst[vst_last].name[namelen], '.') != NULL))
175 /* Grow vst table if necessary. */
176 if (++vst_last == vst_size) {
177 vst_size += VST_SIZE_STEP;
178 vst = realloc(vst, vst_size * sizeof(*vst));
185 /* Sort: the key is the 1st field in *vst, i.e. vimage name. */
186 qsort(vst, vst_last, sizeof(*vst), (void *) strcmp);
187 for (i = 0; i < vst_last; i++) {
189 printf("%s\n", vst[i].name);
193 printf("%s:\n", vst[i].name);
194 printf(" Path: %s\n", vst[i].path);
195 printf(" Hostname: %s\n", vst[i].hostname);
196 printf(" Domainname: %s\n", vst[i].domainname);
197 printf(" Children: %d\n", vst[i].childcnt);
202 printf(" Children limit: %d\n", vst[i].childmax);
203 printf(" CPUsetID: %d\n", vst[i].cpuset);
204 printf(" JID: %d\n", vst[i].jid);
205 printf(" PJID: %d\n", vst[i].parentjid);
206 printf(" Raw sockets allowed: %d\n", vst[i].rawsock);
207 printf(" All AF allowed: %d\n", vst[i].socket_af);
208 printf(" Mount allowed: %d\n", vst[i].mount);
214 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
216 if ((jid = jail_getid(argv[0])) < 0)
219 strncpy(ifreq.ifr_name, argv[1], sizeof(ifreq.ifr_name));
220 if (ioctl(s, SIOCSIFVNET, (caddr_t)&ifreq) < 0)
224 snprintf(ifname, sizeof(ifname), "%s", argv[2]);
226 snprintf(ifname, sizeof(ifname), "eth0");
227 ifreq.ifr_data = ifname;
228 /* Do we need to rename the ifnet? */
229 if (strcmp(ifreq.ifr_name, ifname) != 0) {
230 /* Switch to the context of the target vimage. */
231 if (jail_attach(jid) < 0)
233 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
235 for (namelen = 0; isalpha(ifname[namelen]); namelen++);
237 /* Search for a free ifunit in target vnet. Unsafe. */
238 while (ioctl(s, SIOCSIFNAME, (caddr_t)&ifreq) < 0) {
239 snprintf(&ifname[namelen],
240 sizeof(ifname) - namelen, "%d", i);
241 /* Emergency brake. */
242 if (i++ == IF_MAXUNIT)
247 printf("%s@%s\n", ifname, argv[0]);
249 printf("%s@%s\n", ifreq.ifr_name, argv[0]);
253 if (jail_setv(JAIL_CREATE,
258 "allow.raw_sockets", "true",
259 "allow.socket_af", "true",
260 "allow.mount", "true",
265 /* Not done yet, proceed to apply non-default parameters. */
268 jailparam_init(¶ms[0], "name");
269 jailparam_import(¶ms[0], argv[0]);
270 for (i = 1; i < argc; i++) {
271 for (str = argv[i]; *str != '=' && *str != 0; str++) {
272 /* Do nothing - search for '=' delimeter. */
279 jailparam_init(¶ms[i], argv[i]);
280 jailparam_import(¶ms[i], str);
284 if (jailparam_set(params, i, JAIL_UPDATE) < 0)
289 if ((jid = jail_getid(argv[0])) < 0)
291 if (jail_remove(jid) < 0)
296 if ((jid = jail_getid(argv[0])) < 0)
298 if (jail_attach(jid) < 0)
301 printf("Switched to vimage %s\n", argv[0]);
302 if ((str = getenv("SHELL")) == NULL)
303 execlp("/bin/sh", invocname, NULL);
305 execlp(str, invocname, NULL);
307 execvp(argv[1], &argv[1]);
311 /* Should be unreachable. */
316 fprintf(stderr, "Error: %s\n", jail_errmsg);
323 getjail(vstat_t *vs, int lastjid, int verbose)
325 struct jailparam params[32]; /* Must be > max(psize). */
328 bzero(params, sizeof(params));
329 bzero(vs, sizeof(*vs));
331 jailparam_init(¶ms[psize], "lastjid");
332 jailparam_import_raw(¶ms[psize++], &lastjid, sizeof lastjid);
334 jailparam_init(¶ms[psize], "vnet");
335 jailparam_import_raw(¶ms[psize++], &vs->vnet, sizeof(vs->vnet));
337 jailparam_init(¶ms[psize], "name");
338 jailparam_import_raw(¶ms[psize++], &vs->name, sizeof(vs->name));
343 jailparam_init(¶ms[psize], "path");
344 jailparam_import_raw(¶ms[psize++], &vs->path, sizeof(vs->path));
346 jailparam_init(¶ms[psize], "host.hostname");
347 jailparam_import_raw(¶ms[psize++], &vs->hostname,
348 sizeof(vs->hostname));
350 jailparam_init(¶ms[psize], "host.domainname");
351 jailparam_import_raw(¶ms[psize++], &vs->domainname,
352 sizeof(vs->domainname));
354 jailparam_init(¶ms[psize], "children.cur");
355 jailparam_import_raw(¶ms[psize++], &vs->childcnt,
356 sizeof(vs->childcnt));
361 jailparam_init(¶ms[psize], "children.max");
362 jailparam_import_raw(¶ms[psize++], &vs->childmax,
363 sizeof(vs->childmax));
365 jailparam_init(¶ms[psize], "cpuset.id");
366 jailparam_import_raw(¶ms[psize++], &vs->cpuset,
369 jailparam_init(¶ms[psize], "parent");
370 jailparam_import_raw(¶ms[psize++], &vs->parentjid,
371 sizeof(vs->parentjid));
373 jailparam_init(¶ms[psize], "allow.raw_sockets");
374 jailparam_import_raw(¶ms[psize++], &vs->rawsock,
375 sizeof(vs->rawsock));
377 jailparam_init(¶ms[psize], "allow.socket_af");
378 jailparam_import_raw(¶ms[psize++], &vs->socket_af,
379 sizeof(vs->socket_af));
381 jailparam_init(¶ms[psize], "allow.mount");
382 jailparam_import_raw(¶ms[psize++], &vs->mount, sizeof(vs->mount));
385 vs->jid = jailparam_get(params, psize, 0);
386 jailparam_free(params, psize);