2 * Copyright (c) 1999 Poul-Henning Kamp.
3 * Copyright (c) 2009-2011 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/types.h>
33 #include <sys/socket.h>
34 #include <sys/sysctl.h>
36 #include <arpa/inet.h>
37 #include <netinet/in.h>
49 #define JP_RDTUN(jp) (((jp)->jp_ctltype & CTLFLAG_RDTUN) == CTLFLAG_RDTUN)
61 static void clear_persist(struct cfjail *j);
62 static int update_jail(struct cfjail *j);
63 static int rdtun_params(struct cfjail *j, int dofail);
64 static void running_jid(struct cfjail *j, int dflag);
65 static void jail_quoted_warnx(const struct cfjail *j, const char *name_msg,
66 const char *noname_msg);
67 static int jailparam_set_note(const struct cfjail *j, struct jailparam *jp,
68 unsigned njp, int flags);
69 static void print_jail(FILE *fp, struct cfjail *j, int oldcl);
70 static void print_param(FILE *fp, const struct cfparam *p, int sep, int doname);
71 static void quoted_print(FILE *fp, char *str);
72 static void usage(void);
74 static struct permspec perm_sysctl[] = {
75 { "security.jail.set_hostname_allowed", KP_ALLOW_SET_HOSTNAME, 0 },
76 { "security.jail.sysvipc_allowed", KP_ALLOW_SYSVIPC, 0 },
77 { "security.jail.allow_raw_sockets", KP_ALLOW_RAW_SOCKETS, 0 },
78 { "security.jail.chflags_allowed", KP_ALLOW_CHFLAGS, 0 },
79 { "security.jail.mount_allowed", KP_ALLOW_MOUNT, 0 },
80 { "security.jail.socket_unixiproute_only", KP_ALLOW_SOCKET_AF, 1 },
83 static const enum intparam startcommands[] = {
103 static const enum intparam stopcommands[] = {
111 IP__MOUNT_FROM_FSTAB,
123 main(int argc, char **argv)
131 int ch, docf, error, i, oldcl, sysval;
132 int dflag, iflag, Rflag;
133 char enforce_statfs[4];
134 #if defined(INET) || defined(INET6)
137 #if defined(INET) && defined(INET6)
138 struct in6_addr addr6;
142 dflag = iflag = Rflag = 0;
147 while ((ch = getopt(argc, argv, "cdf:hiJ:lmn:p:qrRs:U:v")) != -1) {
159 #if defined(INET) || defined(INET6)
160 add_param(NULL, NULL, IP_IP_HOSTNAME, NULL);
172 add_param(NULL, NULL, IP_EXEC_CLEAN, NULL);
179 add_param(NULL, NULL, KP_NAME, optarg);
183 paralimit = strtol(optarg, NULL, 10);
198 add_param(NULL, NULL, KP_SECURELEVEL, optarg);
202 add_param(NULL, NULL, IP_EXEC_JAIL_USER, optarg);
203 add_param(NULL, NULL, IP_EXEC_SYSTEM_JAIL_USER, NULL);
207 add_param(NULL, NULL, IP_EXEC_JAIL_USER, optarg);
208 add_param(NULL, NULL, IP_EXEC_SYSTEM_JAIL_USER,
222 /* Find out which of the four command line styles this is. */
225 /* Old-style command line with four fixed parameters */
226 if (argc < 4 || argv[0][0] != '/')
231 add_param(NULL, NULL, KP_PATH, argv[0]);
232 add_param(NULL, NULL, KP_HOST_HOSTNAME, argv[1]);
233 #if defined(INET) || defined(INET6)
234 if (argv[2][0] != '\0') {
235 for (cs = argv[2];; cs = ncs + 1) {
236 ncs = strchr(cs, ',');
239 add_param(NULL, NULL,
240 #if defined(INET) && defined(INET6)
241 inet_pton(AF_INET6, cs, &addr6) == 1
242 ? KP_IP6_ADDR : KP_IP4_ADDR,
254 for (i = 3; i < argc; i++)
255 add_param(NULL, NULL, IP_COMMAND, argv[i]);
256 /* Emulate the defaults from security.jail.* sysctls. */
257 sysvallen = sizeof(sysval);
258 if (sysctlbyname("security.jail.jailed", &sysval, &sysvallen,
259 NULL, 0) == 0 && sysval == 0) {
260 for (pi = 0; pi < sizeof(perm_sysctl) /
261 sizeof(perm_sysctl[0]); pi++) {
262 sysvallen = sizeof(sysval);
263 if (sysctlbyname(perm_sysctl[pi].name,
264 &sysval, &sysvallen, NULL, 0) == 0)
265 add_param(NULL, NULL,
266 perm_sysctl[pi].ipnum,
271 sysvallen = sizeof(sysval);
272 if (sysctlbyname("security.jail.enforce_statfs",
273 &sysval, &sysvallen, NULL, 0) == 0) {
274 snprintf(enforce_statfs,
275 sizeof(enforce_statfs), "%d", sysval);
276 add_param(NULL, NULL, KP_ENFORCE_STATFS,
280 } else if (op == JF_STOP) {
281 /* Jail remove, perhaps using the config file */
282 if (!docf || argc == 0)
285 for (i = 0; i < argc; i++)
286 if (strchr(argv[i], '='))
288 if ((docf = !Rflag &&
289 (!strcmp(cfname, "-") || stat(cfname, &st) == 0)))
291 note_remove = docf || argc > 1 || wild_jail_name(argv[0]);
292 } else if (argc > 1 || (argc == 1 && strchr(argv[0], '='))) {
293 /* Single jail specified on the command line */
297 for (i = 0; i < argc; i++) {
298 if (!strncmp(argv[i], "command", 7) &&
299 (argv[i][7] == '\0' || argv[i][7] == '=')) {
300 if (argv[i][7] == '=')
301 add_param(NULL, NULL, IP_COMMAND,
303 for (i++; i < argc; i++)
304 add_param(NULL, NULL, IP_COMMAND,
308 add_param(NULL, NULL, 0, argv[i]);
311 /* From the config file, perhaps with a specified jail */
317 /* Find out which jails will be run. */
321 for (i = 0; i < argc; i++)
322 if (start_state(argv[i], docf, op, Rflag) < 0)
325 if (start_state(argv[0], docf, op, 0) < 0)
330 if (JidFile != NULL) {
331 jfp = fopen(JidFile, "w");
333 err(1, "open %s", JidFile);
339 * The main loop: Get an available jail and perform the required
340 * operation on it. When that is done, the jail may be finished,
341 * or it may go back for the next step.
343 while ((j = next_jail()))
345 if (j->flags & JF_FAILED) {
347 if (j->comparam == NULL) {
352 if (!(j->flags & JF_PARAMS))
354 j->flags |= JF_PARAMS;
356 add_param(j, NULL, IP_ALLOW_DYING, NULL);
357 if (check_intparams(j) < 0)
359 if ((j->flags & (JF_START | JF_SET)) &&
360 import_params(j) < 0)
365 (j->flags & (JF_SET | JF_DEPEND)) == JF_SET
366 ? dflag || bool_param(j->intparams[IP_ALLOW_DYING])
368 if (finish_command(j))
371 switch (j->flags & JF_OP_MASK) {
373 * These operations just turn into a different op
374 * depending on the jail's current status.
377 j->flags = j->jid < 0 ? JF_START : JF_SET;
381 jail_quoted_warnx(j, "not found",
382 "no jail specified");
386 j->flags = rdtun_params(j, 0) ? JF_RESTART : JF_SET;
387 if (j->flags == JF_RESTART)
390 case JF_START_SET_RESTART:
391 j->flags = j->jid < 0 ? JF_START
392 : rdtun_params(j, 0) ? JF_RESTART : JF_SET;
393 if (j->flags == JF_RESTART)
397 switch (j->flags & JF_OP_MASK) {
399 if (j->comparam == NULL) {
401 !(j->flags & (JF_DEPEND | JF_WILD))) {
402 jail_quoted_warnx(j, "already exists",
410 goto jail_create_done;
411 j->comparam = startcommands;
419 printf("%d\n", j->jid);
421 print_jail(jfp, j, oldcl);
426 if (j->jid < 0 && !(j->flags & JF_DEPEND)) {
427 jail_quoted_warnx(j, "not found",
428 "no jail specified");
434 if (!(j->flags & JF_DEPEND)) {
435 if (rdtun_params(j, 1) < 0 ||
438 if (verbose >= 0 && (j->name || verbose > 0))
439 jail_note(j, "updated\n");
446 if (j->comparam == NULL) {
450 if (!(j->flags & (JF_DEPEND | JF_WILD))
454 goto jail_remove_done;
456 j->comparam = stopcommands;
458 } else if ((j->flags & JF_FAILED) && j->jid > 0)
459 goto jail_remove_done;
464 if ((j->flags & (JF_START | JF_FAILED)) == JF_START) {
466 j->flags &= ~JF_STOP;
468 requeue(j, j->ndeps ? &depend : &ready);
480 * Mark a jail's failure for future handling.
483 failed(struct cfjail *j)
485 j->flags |= JF_FAILED;
486 TAILQ_REMOVE(j->queue, j, tq);
487 TAILQ_INSERT_HEAD(&ready, j, tq);
492 * Exit slightly more gracefully when out of memory.
506 erealloc(void *ptr, size_t size)
510 p = realloc(ptr, size);
517 estrdup(const char *str)
528 * Print a message including an optional jail name.
531 jail_note(const struct cfjail *j, const char *fmt, ...)
539 len = vsnprintf(NULL, 0, fmt, tap);
541 cs = alloca(len + 1);
542 (void)vsnprintf(cs, len + 1, fmt, ap);
545 printf("%s: %s", j->name, cs);
551 * Print a warning message including an optional jail name.
554 jail_warnx(const struct cfjail *j, const char *fmt, ...)
562 len = vsnprintf(NULL, 0, fmt, tap);
564 cs = alloca(len + 1);
565 (void)vsnprintf(cs, len + 1, fmt, ap);
568 warnx("%s: %s", j->name, cs);
577 create_jail(struct cfjail *j)
579 struct iovec jiov[4];
581 struct jailparam *jp, *setparams, *setparams2, *sjp;
583 int dopersist, ns, jid, dying, didfail;
586 * Check the jail's path, with a better error message than jail_set
589 if ((path = string_param(j->intparams[KP_PATH]))) {
590 if (j->name != NULL && path[0] != '/') {
591 jail_warnx(j, "path %s: not an absolute pathname",
595 if (stat(path, &st) < 0) {
596 jail_warnx(j, "path %s: %s", path, strerror(errno));
599 if (!S_ISDIR(st.st_mode)) {
600 jail_warnx(j, "path %s: %s", path, strerror(ENOTDIR));
606 * Copy all the parameters, except that "persist" is always set when
607 * there are commands to run later.
609 dopersist = !bool_param(j->intparams[KP_PERSIST]) &&
610 (j->intparams[IP_EXEC_START] || j->intparams[IP_COMMAND] ||
611 j->intparams[IP_EXEC_POSTSTART]);
613 alloca((j->njp + dopersist) * sizeof(struct jailparam));
614 if (dopersist && jailparam_init(sjp++, "persist") < 0) {
615 jail_warnx(j, "%s", jail_errmsg);
618 for (jp = j->jp; jp < j->jp + j->njp; jp++)
619 if (!dopersist || !equalopts(jp->jp_name, "persist"))
621 ns = sjp - setparams;
624 j->jid = jailparam_set_note(j, setparams, ns, JAIL_CREATE);
625 if (j->jid < 0 && errno == EEXIST &&
626 bool_param(j->intparams[IP_ALLOW_DYING]) &&
627 int_param(j->intparams[KP_JID], &jid) && jid != 0) {
629 * The jail already exists, but may be dying.
630 * Make sure it is, in which case an update is appropriate.
632 *(const void **)&jiov[0].iov_base = "jid";
633 jiov[0].iov_len = sizeof("jid");
634 jiov[1].iov_base = &jid;
635 jiov[1].iov_len = sizeof(jid);
636 *(const void **)&jiov[2].iov_base = "dying";
637 jiov[2].iov_len = sizeof("dying");
638 jiov[3].iov_base = &dying;
639 jiov[3].iov_len = sizeof(dying);
640 if (jail_get(jiov, 4, JAIL_DYING) < 0) {
642 * It could be that the jail just barely finished
643 * dying, or it could be that the jid never existed
644 * but the name does. In either case, another try
645 * at creating the jail should do the right thing.
648 j->jid = jailparam_set_note(j, setparams, ns,
652 if (rdtun_params(j, 1) < 0) {
656 sjp = setparams2 = alloca((j->njp + dopersist) *
657 sizeof(struct jailparam));
658 for (jp = setparams; jp < setparams + ns; jp++)
660 !strcmp(jp->jp_name, "jid"))
662 j->jid = jailparam_set_note(j, setparams2,
663 sjp - setparams2, JAIL_UPDATE | JAIL_DYING);
665 * Again, perhaps the jail just finished dying.
667 if (j->jid < 0 && errno == ENOENT)
668 j->jid = jailparam_set_note(j,
669 setparams, ns, JAIL_CREATE);
673 if (j->jid < 0 && !didfail) {
674 jail_warnx(j, "%s", jail_errmsg);
678 jailparam_free(setparams, 1);
680 j->flags |= JF_PERSIST;
686 * Remove a temporarily set "persist" parameter.
689 clear_persist(struct cfjail *j)
691 struct iovec jiov[4];
694 if (!(j->flags & JF_PERSIST))
696 j->flags &= ~JF_PERSIST;
697 *(const void **)&jiov[0].iov_base = "jid";
698 jiov[0].iov_len = sizeof("jid");
699 jiov[1].iov_base = &j->jid;
700 jiov[1].iov_len = sizeof(j->jid);
701 *(const void **)&jiov[2].iov_base = "nopersist";
702 jiov[2].iov_len = sizeof("nopersist");
703 jiov[3].iov_base = NULL;
705 jid = jail_set(jiov, 4, JAIL_UPDATE);
707 jail_note(j, "jail_set(JAIL_UPDATE) jid=%d nopersist%s%s\n",
708 j->jid, jid < 0 ? ": " : "",
709 jid < 0 ? strerror(errno) : "");
713 * Set a jail's parameters.
716 update_jail(struct cfjail *j)
718 struct jailparam *jp, *setparams, *sjp;
722 for (jp = j->jp; jp < j->jp + j->njp; jp++)
727 sjp = setparams = alloca(++ns * sizeof(struct jailparam));
728 if (jailparam_init(sjp, "jid") < 0 ||
729 jailparam_import_raw(sjp, &j->jid, sizeof j->jid) < 0) {
730 jail_warnx(j, "%s", jail_errmsg);
734 for (jp = j->jp; jp < j->jp + j->njp; jp++)
738 jid = jailparam_set_note(j, setparams, ns,
739 bool_param(j->intparams[IP_ALLOW_DYING])
740 ? JAIL_UPDATE | JAIL_DYING : JAIL_UPDATE);
742 jail_warnx(j, "%s", jail_errmsg);
745 jailparam_free(setparams, 1);
750 * Return if a jail set would change any create-only parameters.
753 rdtun_params(struct cfjail *j, int dofail)
755 struct jailparam *jp, *rtparams, *rtjp;
758 if (j->flags & JF_RDTUN)
760 j->flags |= JF_RDTUN;
762 for (jp = j->jp; jp < j->jp + j->njp; jp++)
763 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid"))
767 rtjp = rtparams = alloca(++nrt * sizeof(struct jailparam));
768 if (jailparam_init(rtjp, "jid") < 0 ||
769 jailparam_import_raw(rtjp, &j->jid, sizeof j->jid) < 0) {
770 jail_warnx(j, "%s", jail_errmsg);
773 for (jp = j->jp; jp < j->jp + j->njp; jp++)
774 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid"))
777 if (jailparam_get(rtparams, nrt,
778 bool_param(j->intparams[IP_ALLOW_DYING]) ? JAIL_DYING : 0) > 0) {
780 for (jp = j->jp, rtjp = rtparams + 1; rtjp < rtparams + nrt;
782 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid")) {
783 if (!((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
784 jp->jp_valuelen == 0 &&
785 *(int *)jp->jp_value) &&
786 !(rtjp->jp_valuelen == jp->jp_valuelen &&
787 !memcmp(rtjp->jp_value, jp->jp_value,
790 jail_warnx(j, "%s cannot be "
791 "changed after creation",
803 for (rtjp = rtparams + 1; rtjp < rtparams + nrt; rtjp++)
804 rtjp->jp_name = NULL;
805 jailparam_free(rtparams, nrt);
810 * Get the jail's jid if it is running.
813 running_jid(struct cfjail *j, int dflag)
815 struct iovec jiov[2];
820 if ((pval = string_param(j->intparams[KP_JID]))) {
821 if (!(jid = strtol(pval, &ep, 10)) || *ep) {
825 *(const void **)&jiov[0].iov_base = "jid";
826 jiov[0].iov_len = sizeof("jid");
827 jiov[1].iov_base = &jid;
828 jiov[1].iov_len = sizeof(jid);
829 } else if ((pval = string_param(j->intparams[KP_NAME]))) {
830 *(const void **)&jiov[0].iov_base = "name";
831 jiov[0].iov_len = sizeof("name");
832 jiov[1].iov_len = strlen(pval) + 1;
833 jiov[1].iov_base = alloca(jiov[1].iov_len);
834 strcpy(jiov[1].iov_base, pval);
839 j->jid = jail_get(jiov, 2, dflag ? JAIL_DYING : 0);
843 jail_quoted_warnx(const struct cfjail *j, const char *name_msg,
844 const char *noname_msg)
848 if ((pval = j->name) || (pval = string_param(j->intparams[KP_JID])) ||
849 (pval = string_param(j->intparams[KP_NAME])))
850 warnx("\"%s\" %s", pval, name_msg);
852 warnx("%s", noname_msg);
856 * Set jail parameters and possible print them out.
859 jailparam_set_note(const struct cfjail *j, struct jailparam *jp, unsigned njp,
866 jid = jailparam_set(jp, njp, flags);
868 jail_note(j, "jail_set(%s%s)",
869 (flags & (JAIL_CREATE | JAIL_UPDATE)) == JAIL_CREATE
870 ? "JAIL_CREATE" : "JAIL_UPDATE",
871 (flags & JAIL_DYING) ? " | JAIL_DYING" : "");
872 for (i = 0; i < njp; i++) {
873 printf(" %s", jp[i].jp_name);
874 if (jp[i].jp_value == NULL)
877 value = jailparam_export(jp + i);
879 err(1, "jailparam_export");
880 quoted_print(stdout, value);
884 printf(": %s", strerror(errno));
891 * Print a jail record.
894 print_jail(FILE *fp, struct cfjail *j, int oldcl)
899 fprintf(fp, "%d\t", j->jid);
900 print_param(fp, j->intparams[KP_PATH], ',', 0);
902 print_param(fp, j->intparams[KP_HOST_HOSTNAME], ',', 0);
905 print_param(fp, j->intparams[KP_IP4_ADDR], ',', 0);
907 if (j->intparams[KP_IP4_ADDR] &&
908 !TAILQ_EMPTY(&j->intparams[KP_IP4_ADDR]->val) &&
909 j->intparams[KP_IP6_ADDR] &&
910 !TAILQ_EMPTY(&j->intparams[KP_IP6_ADDR]->val))
915 print_param(fp, j->intparams[KP_IP6_ADDR], ',', 0);
918 print_param(fp, j->intparams[IP_COMMAND], ' ', 0);
920 fprintf(fp, "jid=%d", j->jid);
921 TAILQ_FOREACH(p, &j->params, tq)
922 if (strcmp(p->name, "jid")) {
924 print_param(fp, p, ',', 1);
931 * Print a parameter value, or a name=value pair.
934 print_param(FILE *fp, const struct cfparam *p, int sep, int doname)
936 const struct cfstring *s, *ts;
940 if (p == NULL || TAILQ_EMPTY(&p->val))
944 TAILQ_FOREACH_SAFE(s, &p->val, tq, ts) {
945 quoted_print(fp, s->s);
952 * Print a string with quotes around spaces.
955 quoted_print(FILE *fp, char *str)
961 : strchr(p, '\'') ? '"'
962 : strchr(p, '"') ? '\''
963 : strchr(p, ' ') || strchr(p, '\t') ? '"'
968 if (c == '\\' || c == qc)
980 (void)fprintf(stderr,
981 "usage: jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
982 " -[cmr] param=value ... [command=command ...]\n"
983 " jail [-dqv] [-f file] -[cmr] [jail]\n"
984 " jail [-qv] [-f file] -[rR] ['*' | jail ...]\n"
985 " jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
986 " [-n jailname] [-s securelevel]\n"
987 " path hostname [ip[,...]] command ...\n");