2 * Copyright (c) 1999 Poul-Henning Kamp.
3 * Copyright (c) 2009 James Gritton
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/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <sys/sysctl.h>
36 #include <arpa/inet.h>
37 #include <netinet/in.h>
44 #include <login_cap.h>
53 static struct jailparam *params;
54 static char **param_values;
57 static char *ip4_addr;
59 static char *ip6_addr;
62 static void add_ip_addr(char **addrp, char *newaddr);
64 static void add_ip_addr46(char *newaddr);
66 static void add_ip_addrinfo(int ai_flags, char *value);
67 static void quoted_print(FILE *fp, char *str);
68 static void set_param(const char *name, char *value);
69 static void usage(void);
71 static const char *perm_sysctl[][3] = {
72 { "security.jail.set_hostname_allowed",
73 "allow.noset_hostname", "allow.set_hostname" },
74 { "security.jail.sysvipc_allowed",
75 "allow.nosysvipc", "allow.sysvipc" },
76 { "security.jail.allow_raw_sockets",
77 "allow.noraw_sockets", "allow.raw_sockets" },
78 { "security.jail.chflags_allowed",
79 "allow.nochflags", "allow.chflags" },
80 { "security.jail.mount_allowed",
81 "allow.nomount", "allow.mount" },
82 { "security.jail.socket_unixiproute_only",
83 "allow.socket_af", "allow.nosocket_af" },
86 extern char **environ;
88 #define GET_USER_INFO do { \
89 pwd = getpwnam(username); \
92 err(1, "getpwnam: %s", username); \
94 errx(1, "%s: no such user", username); \
96 lcap = login_getpwclass(pwd); \
98 err(1, "getpwclass: %s", username); \
99 ngroups = ngroups_max; \
100 if (getgrouplist(username, pwd->pw_gid, groups, &ngroups) != 0) \
101 err(1, "getgrouplist: %s", username); \
105 main(int argc, char **argv)
107 login_cap_t *lcap = NULL;
108 struct passwd *pwd = NULL;
111 int ch, cmdarg, i, jail_set_flags, jid, ngroups, sysval;
112 int hflag, iflag, Jflag, lflag, rflag, uflag, Uflag;
115 char *jailname, *securelevel, *username, *JidFile;
116 char enforce_statfs[4];
117 static char *cleanenv;
118 const char *shell, *p = NULL;
121 hflag = iflag = Jflag = lflag = rflag = uflag = Uflag =
124 jailname = securelevel = username = JidFile = cleanenv = NULL;
127 ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
128 if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
131 while ((ch = getopt(argc, argv, "cdhilmn:r:s:u:U:J:")) != -1) {
134 jail_set_flags |= JAIL_DYING;
150 securelevel = optarg;
164 jail_set_flags |= JAIL_CREATE;
167 jail_set_flags |= JAIL_UPDATE;
170 jid = jail_getid(optarg);
172 errx(1, "%s", jail_errmsg);
182 if (argc > 0 || iflag || Jflag || lflag || uflag || Uflag)
184 if (jail_remove(jid) < 0)
185 err(1, "jail_remove");
192 if (lflag && username == NULL)
198 set_param("name", jailname);
200 set_param("securelevel", securelevel);
201 if (jail_set_flags) {
202 for (i = 0; i < argc; i++) {
203 if (!strncmp(argv[i], "command=", 8)) {
206 jail_set_flags |= JAIL_ATTACH;
210 if (!strncmp(argv[i], "ip4.addr=", 9)) {
211 add_ip_addr(&ip4_addr, argv[i] + 9);
215 if (!strncmp(argv[i], "ip6.addr=", 9)) {
216 add_ip_addr(&ip6_addr, argv[i] + 9);
220 if (!strncmp(argv[i], "host.hostname=", 14))
221 add_ip_addrinfo(0, argv[i] + 14);
223 set_param(NULL, argv[i]);
226 if (argc < 4 || argv[0][0] != '/')
228 "no -c or -m, so this must be an old-style command.",
229 "But it doesn't look like one.");
230 set_param("path", argv[0]);
231 set_param("host.hostname", argv[1]);
233 add_ip_addrinfo(0, argv[1]);
234 if (argv[2][0] != '\0')
236 add_ip_addr46(argv[2]);
238 add_ip_addr(&ip4_addr, argv[2]);
241 /* Emulate the defaults from security.jail.* sysctls */
242 sysvallen = sizeof(sysval);
243 if (sysctlbyname("security.jail.jailed", &sysval, &sysvallen,
244 NULL, 0) == 0 && sysval == 0) {
245 for (pi = 0; pi < sizeof(perm_sysctl) /
246 sizeof(perm_sysctl[0]); pi++) {
247 sysvallen = sizeof(sysval);
248 if (sysctlbyname(perm_sysctl[pi][0],
249 &sysval, &sysvallen, NULL, 0) == 0)
250 set_param(perm_sysctl[pi]
251 [sysval ? 2 : 1], NULL);
253 sysvallen = sizeof(sysval);
254 if (sysctlbyname("security.jail.enforce_statfs",
255 &sysval, &sysvallen, NULL, 0) == 0) {
256 snprintf(enforce_statfs,
257 sizeof(enforce_statfs), "%d", sysval);
258 set_param("enforce_statfs", enforce_statfs);
262 if (ip4_addr != NULL)
263 set_param("ip4.addr", ip4_addr);
265 if (ip6_addr != NULL)
266 set_param("ip6.addr", ip6_addr);
270 fp = fopen(JidFile, "w");
272 errx(1, "Could not create JidFile: %s", JidFile);
274 jid = jailparam_set(params, nparams,
275 jail_set_flags ? jail_set_flags : JAIL_CREATE | JAIL_ATTACH);
277 errx(1, "%s", jail_errmsg);
283 if (jail_set_flags) {
284 fprintf(fp, "jid=%d", jid);
285 for (i = 0; i < nparams; i++)
286 if (strcmp(params[i].jp_name, "jid")) {
288 (char *)params[i].jp_name);
289 if (param_values[i]) {
297 for (i = 0; i < nparams; i++)
298 if (!strcmp(params[i].jp_name, "path"))
301 fprintf(fp, "%d\t%s\t%s\t%s%s%s\t%s\n",
303 ? (char *)params[i].jp_value : argv[0],
304 argv[1], ip4_addr ? ip4_addr : "",
305 ip4_addr && ip4_addr[0] && ip6_addr && ip6_addr[0]
306 ? "," : "", ip6_addr ? ip6_addr : "", argv[3]);
308 fprintf(fp, "%d\t%s\t%s\t%s\t%s\n",
310 ? (char *)params[i].jp_value : argv[0],
311 argv[1], ip4_addr ? ip4_addr : "", argv[3]);
318 if (username != NULL) {
325 if (setgroups(ngroups, groups) != 0)
327 if (setgid(pwd->pw_gid) != 0)
329 if (setusercontext(lcap, pwd, pwd->pw_uid,
330 LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN) != 0)
331 err(1, "setusercontext");
336 shell = pwd->pw_shell;
338 shell = _PATH_BSHELL;
339 if (chdir(pwd->pw_dir) < 0)
340 errx(1, "no home directory");
341 setenv("HOME", pwd->pw_dir, 1);
342 setenv("SHELL", shell, 1);
343 setenv("USER", pwd->pw_name, 1);
345 setenv("TERM", p, 1);
347 execvp(argv[cmdarg], argv + cmdarg);
348 err(1, "execvp: %s", argv[cmdarg]);
352 add_ip_addr(char **addrp, char *value)
358 *addrp = strdup(value);
361 } else if (value[0]) {
362 addrlen = strlen(*addrp) + strlen(value) + 2;
363 addr = malloc(addrlen);
366 snprintf(addr, addrlen, "%s,%s", *addrp, value);
374 add_ip_addr46(char *value)
378 for (p = value;; p = np + 1)
383 add_ip_addrinfo(AI_NUMERICHOST, p);
391 add_ip_addrinfo(int ai_flags, char *value)
393 struct addrinfo hints, *ai0, *ai;
394 struct in_addr addr4;
398 char avalue4[INET_ADDRSTRLEN];
400 struct in6_addr addr6;
402 char avalue6[INET6_ADDRSTRLEN];
405 /* Look up the hostname (or get the address) */
406 memset(&hints, 0, sizeof(hints));
407 hints.ai_socktype = SOCK_STREAM;
409 hints.ai_family = PF_UNSPEC;
411 hints.ai_family = PF_INET;
413 hints.ai_flags = ai_flags;
414 error = getaddrinfo(value, NULL, &hints, &ai0);
416 errx(1, "hostname %s: %s", value, gai_strerror(error));
419 * Silently ignore unsupported address families from DNS lookups.
420 * But if this is a numeric address, let the kernel give the error.
422 if (ai_flags & AI_NUMERICHOST)
430 ip4ok = (sysctlnametomib("security.jail.param.ip4", mib,
434 ip6ok = (sysctlnametomib("security.jail.param.ip6", mib,
439 /* Convert the addresses to ASCII so set_param can convert them back. */
440 for (ai = ai0; ai; ai = ai->ai_next)
441 switch (ai->ai_family) {
445 memcpy(&addr4, &((struct sockaddr_in *)
446 (void *)ai->ai_addr)->sin_addr, sizeof(addr4));
447 if (inet_ntop(AF_INET, &addr4, avalue4,
448 INET_ADDRSTRLEN) == NULL)
450 add_ip_addr(&ip4_addr, avalue4);
456 memcpy(&addr6, &((struct sockaddr_in6 *)
457 (void *)ai->ai_addr)->sin6_addr, sizeof(addr6));
458 if (inet_ntop(AF_INET6, &addr6, avalue6,
459 INET6_ADDRSTRLEN) == NULL)
461 add_ip_addr(&ip6_addr, avalue6);
469 quoted_print(FILE *fp, char *str)
474 /* An empty string needs quoting. */
481 * The value will be surrounded by quotes if it contains spaces
484 qc = strchr(p, '\'') ? '"'
485 : strchr(p, '"') ? '\''
486 : strchr(p, ' ') || strchr(p, '\t') ? '"'
491 if (c == '\\' || c == qc)
500 set_param(const char *name, char *value)
502 struct jailparam *param;
505 static int paramlistsize;
507 /* Separate the name from the value, if not done already. */
510 if ((value = strchr(value, '=')))
514 /* jail_set won't chdir along with its chroot, so do it here. */
515 if (!strcmp(name, "path") && chdir(value) < 0)
516 err(1, "chdir: %s", value);
518 /* Check for repeat parameters */
519 for (i = 0; i < nparams; i++)
520 if (!strcmp(name, params[i].jp_name)) {
521 jailparam_free(params + i, 1);
522 memcpy(params + i, params + i + 1,
523 (--nparams - i) * sizeof(struct jailparam));
527 /* Make sure there is room for the new param record. */
530 params = malloc(paramlistsize * sizeof(*params));
531 param_values = malloc(paramlistsize * sizeof(*param_values));
532 if (params == NULL || param_values == NULL)
534 } else if (nparams >= paramlistsize) {
536 params = realloc(params, paramlistsize * sizeof(*params));
537 param_values = realloc(param_values,
538 paramlistsize * sizeof(*param_values));
543 /* Look up the paramter. */
544 param_values[nparams] = value;
545 param = params + nparams++;
546 if (jailparam_init(param, name) < 0 ||
547 jailparam_import(param, value) < 0)
548 errx(1, "%s", jail_errmsg);
555 (void)fprintf(stderr,
556 "usage: jail [-d] [-h] [-i] [-J jid_file] "
557 "[-l -u username | -U username]\n"
558 " [-c | -m] param=value ... [command=command ...]\n"
559 " jail [-r jail]\n");