17074cfa2SJamie Gritton /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
47074cfa2SJamie Gritton * Copyright (c) 1999 Poul-Henning Kamp.
591b24c18SJamie Gritton * Copyright (c) 2009-2012 James Gritton
67074cfa2SJamie Gritton * All rights reserved.
77074cfa2SJamie Gritton *
87074cfa2SJamie Gritton * Redistribution and use in source and binary forms, with or without
97074cfa2SJamie Gritton * modification, are permitted provided that the following conditions
107074cfa2SJamie Gritton * are met:
117074cfa2SJamie Gritton * 1. Redistributions of source code must retain the above copyright
127074cfa2SJamie Gritton * notice, this list of conditions and the following disclaimer.
137074cfa2SJamie Gritton * 2. Redistributions in binary form must reproduce the above copyright
147074cfa2SJamie Gritton * notice, this list of conditions and the following disclaimer in the
157074cfa2SJamie Gritton * documentation and/or other materials provided with the distribution.
167074cfa2SJamie Gritton *
177074cfa2SJamie Gritton * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
187074cfa2SJamie Gritton * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
197074cfa2SJamie Gritton * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
207074cfa2SJamie Gritton * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
217074cfa2SJamie Gritton * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
227074cfa2SJamie Gritton * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
237074cfa2SJamie Gritton * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
247074cfa2SJamie Gritton * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
257074cfa2SJamie Gritton * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
267074cfa2SJamie Gritton * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
277074cfa2SJamie Gritton * SUCH DAMAGE.
28ce5c1cd1SPoul-Henning Kamp */
29ce5c1cd1SPoul-Henning Kamp
302671ee73SJamie Gritton #include <sys/types.h>
312671ee73SJamie Gritton #include <sys/stat.h>
32413628a7SBjoern A. Zeeb #include <sys/socket.h>
337deb00ccSMatteo Riondato #include <sys/sysctl.h>
343876038aSDima Dorfman
353876038aSDima Dorfman #include <arpa/inet.h>
3673d0971bSJamie Gritton #include <netinet/in.h>
373876038aSDima Dorfman
383876038aSDima Dorfman #include <err.h>
39927b4810SMaxim Konovalov #include <errno.h>
402671ee73SJamie Gritton #include <stdarg.h>
4175c13541SPoul-Henning Kamp #include <stdio.h>
422694efd4SDima Dorfman #include <stdlib.h>
432694efd4SDima Dorfman #include <string.h>
443876038aSDima Dorfman #include <unistd.h>
4575c13541SPoul-Henning Kamp
462671ee73SJamie Gritton #include "jailp.h"
4773d0971bSJamie Gritton
482671ee73SJamie Gritton #define JP_RDTUN(jp) (((jp)->jp_ctltype & CTLFLAG_RDTUN) == CTLFLAG_RDTUN)
49413628a7SBjoern A. Zeeb
5055530438SJamie Gritton struct permspec {
5155530438SJamie Gritton const char *name;
5255530438SJamie Gritton enum intparam ipnum;
5355530438SJamie Gritton int rev;
54029cb9c7SJamie Gritton };
55029cb9c7SJamie Gritton
56a6486f60SJamie Gritton int iflag;
572b00f7baSJamie Gritton int note_remove;
582671ee73SJamie Gritton int verbose;
591e1a4743SEugene Grosbein const char *separator = "\t";
60d6131f4bSMaxim Konovalov
612671ee73SJamie Gritton static void clear_persist(struct cfjail *j);
622671ee73SJamie Gritton static int update_jail(struct cfjail *j);
632671ee73SJamie Gritton static int rdtun_params(struct cfjail *j, int dofail);
64ed31b3f4SJamie Gritton static void running_jid(struct cfjail *j);
651ca35de4SJamie Gritton static void jail_quoted_warnx(const struct cfjail *j, const char *name_msg,
661ca35de4SJamie Gritton const char *noname_msg);
672671ee73SJamie Gritton static int jailparam_set_note(const struct cfjail *j, struct jailparam *jp,
682671ee73SJamie Gritton unsigned njp, int flags);
691e1a4743SEugene Grosbein static void print_jail(FILE *fp, struct cfjail *j, int oldcl, int running);
702671ee73SJamie Gritton static void print_param(FILE *fp, const struct cfparam *p, int sep, int doname);
711e1a4743SEugene Grosbein static void show_jails(void);
7275c13541SPoul-Henning Kamp static void quoted_print(FILE *fp, char *str);
73a9cce232SAlfonso Gregory static void usage(void) __dead2;
7475c13541SPoul-Henning Kamp
7555530438SJamie Gritton static struct permspec perm_sysctl[] = {
7655530438SJamie Gritton { "security.jail.set_hostname_allowed", KP_ALLOW_SET_HOSTNAME, 0 },
7755530438SJamie Gritton { "security.jail.sysvipc_allowed", KP_ALLOW_SYSVIPC, 0 },
7855530438SJamie Gritton { "security.jail.allow_raw_sockets", KP_ALLOW_RAW_SOCKETS, 0 },
7955530438SJamie Gritton { "security.jail.chflags_allowed", KP_ALLOW_CHFLAGS, 0 },
8055530438SJamie Gritton { "security.jail.mount_allowed", KP_ALLOW_MOUNT, 0 },
8155530438SJamie Gritton { "security.jail.socket_unixiproute_only", KP_ALLOW_SOCKET_AF, 1 },
8275c13541SPoul-Henning Kamp };
8375c13541SPoul-Henning Kamp
843b40332cSJamie Gritton static const enum intparam startcommands[] = {
856fcbac3cSJamie Gritton IP__NULL,
8666005c45SRyan Moeller IP_EXEC_PREPARE,
875fb611c2SJamie Gritton #ifdef INET
883b40332cSJamie Gritton IP__IP4_IFADDR,
895fb611c2SJamie Gritton #endif
903b40332cSJamie Gritton #ifdef INET6
913b40332cSJamie Gritton IP__IP6_IFADDR,
923b40332cSJamie Gritton #endif
933b40332cSJamie Gritton IP_MOUNT,
943b40332cSJamie Gritton IP__MOUNT_FROM_FSTAB,
953b40332cSJamie Gritton IP_MOUNT_DEVFS,
96fbd868c9SHiroki Sato IP_MOUNT_FDESCFS,
9707a7869fSJamie Gritton IP_MOUNT_PROCFS,
983b40332cSJamie Gritton IP_EXEC_PRESTART,
993b40332cSJamie Gritton IP__OP,
100f6c0e63bSAlexander Leidinger IP_EXEC_CREATED,
101e0dfe185SAlexander Leidinger IP_ZFS_DATASET,
1023b40332cSJamie Gritton IP_VNET_INTERFACE,
1033b40332cSJamie Gritton IP_EXEC_START,
1043b40332cSJamie Gritton IP_COMMAND,
1053b40332cSJamie Gritton IP_EXEC_POSTSTART,
1066fcbac3cSJamie Gritton IP__NULL
1073b40332cSJamie Gritton };
1083b40332cSJamie Gritton
1093b40332cSJamie Gritton static const enum intparam stopcommands[] = {
1106fcbac3cSJamie Gritton IP__NULL,
1113b40332cSJamie Gritton IP_EXEC_PRESTOP,
1123b40332cSJamie Gritton IP_EXEC_STOP,
1133b40332cSJamie Gritton IP_STOP_TIMEOUT,
1143b40332cSJamie Gritton IP__OP,
1153b40332cSJamie Gritton IP_EXEC_POSTSTOP,
11607a7869fSJamie Gritton IP_MOUNT_PROCFS,
117fbd868c9SHiroki Sato IP_MOUNT_FDESCFS,
1183b40332cSJamie Gritton IP_MOUNT_DEVFS,
1193b40332cSJamie Gritton IP__MOUNT_FROM_FSTAB,
1203b40332cSJamie Gritton IP_MOUNT,
1213b40332cSJamie Gritton #ifdef INET6
1223b40332cSJamie Gritton IP__IP6_IFADDR,
1233b40332cSJamie Gritton #endif
1245fb611c2SJamie Gritton #ifdef INET
1253b40332cSJamie Gritton IP__IP4_IFADDR,
1265fb611c2SJamie Gritton #endif
12766005c45SRyan Moeller IP_EXEC_RELEASE,
1286fcbac3cSJamie Gritton IP__NULL
1293b40332cSJamie Gritton };
130927b4810SMaxim Konovalov
131d50685b3SJamie Gritton static const enum intparam cleancommands[] = {
132d50685b3SJamie Gritton IP__NULL,
133d50685b3SJamie Gritton IP_EXEC_POSTSTOP,
134d50685b3SJamie Gritton IP_MOUNT_PROCFS,
135d50685b3SJamie Gritton IP_MOUNT_FDESCFS,
136d50685b3SJamie Gritton IP_MOUNT_DEVFS,
137d50685b3SJamie Gritton IP__MOUNT_FROM_FSTAB,
138d50685b3SJamie Gritton IP_MOUNT,
139d50685b3SJamie Gritton #ifdef INET6
140d50685b3SJamie Gritton IP__IP6_IFADDR,
141d50685b3SJamie Gritton #endif
142d50685b3SJamie Gritton #ifdef INET
143d50685b3SJamie Gritton IP__IP4_IFADDR,
144d50685b3SJamie Gritton #endif
145d50685b3SJamie Gritton IP_EXEC_RELEASE,
146d50685b3SJamie Gritton IP__NULL
147d50685b3SJamie Gritton };
148d50685b3SJamie Gritton
149*48c738b0SMark Johnston static const struct {
150*48c738b0SMark Johnston const char *name;
151*48c738b0SMark Johnston enum intparam param;
152*48c738b0SMark Johnston } listparams[] = {
153*48c738b0SMark Johnston #ifdef INET
154*48c738b0SMark Johnston { "ip4.addr", KP_IP4_ADDR },
155*48c738b0SMark Johnston #endif
156*48c738b0SMark Johnston #ifdef INET6
157*48c738b0SMark Johnston { "ip6.addr", KP_IP6_ADDR },
158*48c738b0SMark Johnston #endif
159*48c738b0SMark Johnston { "vnet.interface", IP_VNET_INTERFACE },
160*48c738b0SMark Johnston { "zfs.dataset", IP_ZFS_DATASET },
161*48c738b0SMark Johnston };
162*48c738b0SMark Johnston
16375c13541SPoul-Henning Kamp int
main(int argc,char ** argv)16475c13541SPoul-Henning Kamp main(int argc, char **argv)
16575c13541SPoul-Henning Kamp {
1662671ee73SJamie Gritton struct stat st;
1672671ee73SJamie Gritton FILE *jfp;
1682671ee73SJamie Gritton struct cfjail *j;
1695fb611c2SJamie Gritton char *JidFile;
170086e0149SJamie Gritton const char *cfname;
171029cb9c7SJamie Gritton size_t sysvallen;
1722671ee73SJamie Gritton unsigned op, pi;
173ed31b3f4SJamie Gritton int ch, docf, dying_warned, error, i, oldcl, sysval;
1746000d46fSJamie Gritton int dflag, eflag, Rflag;
1755fb611c2SJamie Gritton #if defined(INET) || defined(INET6)
1765fb611c2SJamie Gritton char *cs, *ncs;
1775fb611c2SJamie Gritton #endif
1785fb611c2SJamie Gritton #if defined(INET) && defined(INET6)
1795fb611c2SJamie Gritton struct in6_addr addr6;
1805fb611c2SJamie Gritton #endif
18175c13541SPoul-Henning Kamp
1822671ee73SJamie Gritton op = 0;
1836000d46fSJamie Gritton dflag = eflag = Rflag = 0;
1842671ee73SJamie Gritton docf = 1;
1852671ee73SJamie Gritton cfname = CONF_FILE;
1862671ee73SJamie Gritton JidFile = NULL;
187d6131f4bSMaxim Konovalov
188d50685b3SJamie Gritton while ((ch = getopt(argc, argv, "cCde:f:hiJ:lmn:p:qrRs:u:U:v")) != -1) {
189d6131f4bSMaxim Konovalov switch (ch) {
1902671ee73SJamie Gritton case 'c':
1912671ee73SJamie Gritton op |= JF_START;
1922671ee73SJamie Gritton break;
193d50685b3SJamie Gritton case 'C':
194d50685b3SJamie Gritton op |= JF_CLEANUP;
195d50685b3SJamie Gritton break;
19673d0971bSJamie Gritton case 'd':
1972671ee73SJamie Gritton dflag = 1;
1982671ee73SJamie Gritton break;
1991e1a4743SEugene Grosbein case 'e':
2006000d46fSJamie Gritton eflag = 1;
2011e1a4743SEugene Grosbein separator = optarg;
2021e1a4743SEugene Grosbein break;
2032671ee73SJamie Gritton case 'f':
2042671ee73SJamie Gritton cfname = optarg;
20573d0971bSJamie Gritton break;
206413628a7SBjoern A. Zeeb case 'h':
2075fb611c2SJamie Gritton #if defined(INET) || defined(INET6)
20855530438SJamie Gritton add_param(NULL, NULL, IP_IP_HOSTNAME, NULL);
2095fb611c2SJamie Gritton #endif
2102671ee73SJamie Gritton docf = 0;
211413628a7SBjoern A. Zeeb break;
212ebf5d9bcSMike Barcroft case 'i':
213ebf5d9bcSMike Barcroft iflag = 1;
2142671ee73SJamie Gritton verbose = -1;
215ebf5d9bcSMike Barcroft break;
216cdafc851SPhilip Paeps case 'J':
217cdafc851SPhilip Paeps JidFile = optarg;
218d6131f4bSMaxim Konovalov break;
2195b242e8cSMaxim Konovalov case 'l':
22055530438SJamie Gritton add_param(NULL, NULL, IP_EXEC_CLEAN, NULL);
2212671ee73SJamie Gritton docf = 0;
22273d0971bSJamie Gritton break;
22373d0971bSJamie Gritton case 'm':
2242671ee73SJamie Gritton op |= JF_SET;
2252671ee73SJamie Gritton break;
2262671ee73SJamie Gritton case 'n':
22755530438SJamie Gritton add_param(NULL, NULL, KP_NAME, optarg);
2282671ee73SJamie Gritton docf = 0;
2292671ee73SJamie Gritton break;
2302671ee73SJamie Gritton case 'p':
231aa02af54SJamie Gritton paralimit = strtol(optarg, NULL, 10);
232aa02af54SJamie Gritton if (paralimit == 0)
233aa02af54SJamie Gritton paralimit = -1;
2342671ee73SJamie Gritton break;
2352671ee73SJamie Gritton case 'q':
2362671ee73SJamie Gritton verbose = -1;
23773d0971bSJamie Gritton break;
23873d0971bSJamie Gritton case 'r':
2392671ee73SJamie Gritton op |= JF_STOP;
2402671ee73SJamie Gritton break;
2412671ee73SJamie Gritton case 'R':
2422671ee73SJamie Gritton op |= JF_STOP;
2432671ee73SJamie Gritton Rflag = 1;
2442671ee73SJamie Gritton break;
2452671ee73SJamie Gritton case 's':
24655530438SJamie Gritton add_param(NULL, NULL, KP_SECURELEVEL, optarg);
2472671ee73SJamie Gritton docf = 0;
2482671ee73SJamie Gritton break;
2492671ee73SJamie Gritton case 'u':
25055530438SJamie Gritton add_param(NULL, NULL, IP_EXEC_JAIL_USER, optarg);
25155530438SJamie Gritton add_param(NULL, NULL, IP_EXEC_SYSTEM_JAIL_USER, NULL);
2522671ee73SJamie Gritton docf = 0;
2532671ee73SJamie Gritton break;
2542671ee73SJamie Gritton case 'U':
25555530438SJamie Gritton add_param(NULL, NULL, IP_EXEC_JAIL_USER, optarg);
25655530438SJamie Gritton add_param(NULL, NULL, IP_EXEC_SYSTEM_JAIL_USER,
25755530438SJamie Gritton "false");
2582671ee73SJamie Gritton docf = 0;
2592671ee73SJamie Gritton break;
2602671ee73SJamie Gritton case 'v':
2612671ee73SJamie Gritton verbose = 1;
26273d0971bSJamie Gritton break;
263d6131f4bSMaxim Konovalov default:
264d6131f4bSMaxim Konovalov usage();
265ebf5d9bcSMike Barcroft }
266d6131f4bSMaxim Konovalov }
267d6131f4bSMaxim Konovalov argc -= optind;
268d6131f4bSMaxim Konovalov argv += optind;
269413628a7SBjoern A. Zeeb
2706000d46fSJamie Gritton if (eflag) {
2716000d46fSJamie Gritton /* Just print list of all configured non-wildcard jails */
2726000d46fSJamie Gritton if (op || argc > 0)
2736000d46fSJamie Gritton usage();
2746000d46fSJamie Gritton load_config(cfname);
2756000d46fSJamie Gritton show_jails();
2766000d46fSJamie Gritton exit(0);
2776000d46fSJamie Gritton }
2786000d46fSJamie Gritton
2796000d46fSJamie Gritton /* Find out which of the command line styles this is. */
2802671ee73SJamie Gritton oldcl = 0;
2812671ee73SJamie Gritton if (!op) {
2822671ee73SJamie Gritton /* Old-style command line with four fixed parameters */
28373d0971bSJamie Gritton if (argc < 4 || argv[0][0] != '/')
2842671ee73SJamie Gritton usage();
2852671ee73SJamie Gritton op = JF_START;
2862671ee73SJamie Gritton docf = 0;
2872671ee73SJamie Gritton oldcl = 1;
28855530438SJamie Gritton add_param(NULL, NULL, KP_PATH, argv[0]);
28955530438SJamie Gritton add_param(NULL, NULL, KP_HOST_HOSTNAME, argv[1]);
2905fb611c2SJamie Gritton #if defined(INET) || defined(INET6)
2912671ee73SJamie Gritton if (argv[2][0] != '\0') {
2922671ee73SJamie Gritton for (cs = argv[2];; cs = ncs + 1) {
2932671ee73SJamie Gritton ncs = strchr(cs, ',');
2942671ee73SJamie Gritton if (ncs)
2952671ee73SJamie Gritton *ncs = '\0';
2962671ee73SJamie Gritton add_param(NULL, NULL,
2975fb611c2SJamie Gritton #if defined(INET) && defined(INET6)
2982671ee73SJamie Gritton inet_pton(AF_INET6, cs, &addr6) == 1
2995fb611c2SJamie Gritton ? KP_IP6_ADDR : KP_IP4_ADDR,
3005fb611c2SJamie Gritton #elif defined(INET)
3015fb611c2SJamie Gritton KP_IP4_ADDR,
3025fb611c2SJamie Gritton #elif defined(INET6)
3035fb611c2SJamie Gritton KP_IP6_ADDR,
30473d0971bSJamie Gritton #endif
3055fb611c2SJamie Gritton cs);
3062671ee73SJamie Gritton if (!ncs)
3072671ee73SJamie Gritton break;
3082671ee73SJamie Gritton }
3092671ee73SJamie Gritton }
31015ede760SBjoern A. Zeeb #endif
3112671ee73SJamie Gritton for (i = 3; i < argc; i++)
31255530438SJamie Gritton add_param(NULL, NULL, IP_COMMAND, argv[i]);
3132671ee73SJamie Gritton /* Emulate the defaults from security.jail.* sysctls. */
314029cb9c7SJamie Gritton sysvallen = sizeof(sysval);
315029cb9c7SJamie Gritton if (sysctlbyname("security.jail.jailed", &sysval, &sysvallen,
316029cb9c7SJamie Gritton NULL, 0) == 0 && sysval == 0) {
317029cb9c7SJamie Gritton for (pi = 0; pi < sizeof(perm_sysctl) /
318029cb9c7SJamie Gritton sizeof(perm_sysctl[0]); pi++) {
319029cb9c7SJamie Gritton sysvallen = sizeof(sysval);
32055530438SJamie Gritton if (sysctlbyname(perm_sysctl[pi].name,
321029cb9c7SJamie Gritton &sysval, &sysvallen, NULL, 0) == 0)
3222671ee73SJamie Gritton add_param(NULL, NULL,
32355530438SJamie Gritton perm_sysctl[pi].ipnum,
32455530438SJamie Gritton (sysval ? 1 : 0) ^
32555530438SJamie Gritton perm_sysctl[pi].rev
32655530438SJamie Gritton ? NULL : "false");
327029cb9c7SJamie Gritton }
328029cb9c7SJamie Gritton }
3296000d46fSJamie Gritton } else if (op == JF_STOP) {
3302671ee73SJamie Gritton /* Jail remove, perhaps using the config file */
3312671ee73SJamie Gritton if (!docf || argc == 0)
3322671ee73SJamie Gritton usage();
3332671ee73SJamie Gritton if (!Rflag)
3342671ee73SJamie Gritton for (i = 0; i < argc; i++)
3352671ee73SJamie Gritton if (strchr(argv[i], '='))
3362671ee73SJamie Gritton usage();
3372671ee73SJamie Gritton if ((docf = !Rflag &&
3382671ee73SJamie Gritton (!strcmp(cfname, "-") || stat(cfname, &st) == 0)))
339086e0149SJamie Gritton load_config(cfname);
3402b00f7baSJamie Gritton note_remove = docf || argc > 1 || wild_jail_name(argv[0]);
3412671ee73SJamie Gritton } else if (argc > 1 || (argc == 1 && strchr(argv[0], '='))) {
3422671ee73SJamie Gritton /* Single jail specified on the command line */
343d50685b3SJamie Gritton if (Rflag || (op & JF_CLEANUP))
3442671ee73SJamie Gritton usage();
3452671ee73SJamie Gritton docf = 0;
3462671ee73SJamie Gritton for (i = 0; i < argc; i++) {
347*48c738b0SMark Johnston size_t l;
348*48c738b0SMark Johnston
3492671ee73SJamie Gritton if (!strncmp(argv[i], "command", 7) &&
3502671ee73SJamie Gritton (argv[i][7] == '\0' || argv[i][7] == '=')) {
3512671ee73SJamie Gritton if (argv[i][7] == '=')
35255530438SJamie Gritton add_param(NULL, NULL, IP_COMMAND,
3532671ee73SJamie Gritton argv[i] + 8);
3542671ee73SJamie Gritton for (i++; i < argc; i++)
35555530438SJamie Gritton add_param(NULL, NULL, IP_COMMAND,
3562671ee73SJamie Gritton argv[i]);
357*48c738b0SMark Johnston continue;
358b01d7177SJamie Gritton }
359*48c738b0SMark Johnston
360*48c738b0SMark Johnston /*
361*48c738b0SMark Johnston * Is this parameter a list?
362*48c738b0SMark Johnston */
363*48c738b0SMark Johnston for (l = 0; l < nitems(listparams); l++) {
364*48c738b0SMark Johnston size_t len;
365*48c738b0SMark Johnston
366*48c738b0SMark Johnston len = strlen(listparams[l].name);
367*48c738b0SMark Johnston if (strncmp(argv[i], listparams[l].name, len) ||
368*48c738b0SMark Johnston argv[i][len] != '=')
369*48c738b0SMark Johnston continue;
370*48c738b0SMark Johnston
371*48c738b0SMark Johnston for (cs = argv[i] + len + 1;; cs = ncs + 1) {
372b01d7177SJamie Gritton ncs = strchr(cs, ',');
373b01d7177SJamie Gritton if (ncs)
374b01d7177SJamie Gritton *ncs = '\0';
375*48c738b0SMark Johnston add_param(NULL, NULL,
376*48c738b0SMark Johnston listparams[l].param, cs);
377b01d7177SJamie Gritton if (!ncs)
3782671ee73SJamie Gritton break;
37973d0971bSJamie Gritton }
380b01d7177SJamie Gritton break;
381b01d7177SJamie Gritton }
382*48c738b0SMark Johnston if (l == nitems(listparams))
38355530438SJamie Gritton add_param(NULL, NULL, 0, argv[i]);
384cdafc851SPhilip Paeps }
385cdafc851SPhilip Paeps } else {
3862671ee73SJamie Gritton /* From the config file, perhaps with a specified jail */
3872671ee73SJamie Gritton if (Rflag || !docf)
3882671ee73SJamie Gritton usage();
389086e0149SJamie Gritton load_config(cfname);
39073d0971bSJamie Gritton }
39173d0971bSJamie Gritton
3922671ee73SJamie Gritton /* Find out which jails will be run. */
3932671ee73SJamie Gritton dep_setup(docf);
3942671ee73SJamie Gritton error = 0;
395d50685b3SJamie Gritton if ((op & JF_OP_MASK) == JF_STOP) {
3962671ee73SJamie Gritton for (i = 0; i < argc; i++)
3971ca35de4SJamie Gritton if (start_state(argv[i], docf, op, Rflag) < 0)
3982671ee73SJamie Gritton error = 1;
3992671ee73SJamie Gritton } else {
4001ca35de4SJamie Gritton if (start_state(argv[0], docf, op, 0) < 0)
4012671ee73SJamie Gritton exit(1);
40273d0971bSJamie Gritton }
40373d0971bSJamie Gritton
4042671ee73SJamie Gritton jfp = NULL;
4052671ee73SJamie Gritton if (JidFile != NULL) {
4062671ee73SJamie Gritton jfp = fopen(JidFile, "w");
4072671ee73SJamie Gritton if (jfp == NULL)
4082671ee73SJamie Gritton err(1, "open %s", JidFile);
4092671ee73SJamie Gritton setlinebuf(jfp);
41073d0971bSJamie Gritton }
4112671ee73SJamie Gritton setlinebuf(stdout);
41273d0971bSJamie Gritton
413843c277bSJamie Gritton /*
4142671ee73SJamie Gritton * The main loop: Get an available jail and perform the required
4152671ee73SJamie Gritton * operation on it. When that is done, the jail may be finished,
4162671ee73SJamie Gritton * or it may go back for the next step.
417843c277bSJamie Gritton */
418ed31b3f4SJamie Gritton dying_warned = 0;
4192671ee73SJamie Gritton while ((j = next_jail()))
4202671ee73SJamie Gritton {
4212671ee73SJamie Gritton if (j->flags & JF_FAILED) {
4222671ee73SJamie Gritton error = 1;
4233b40332cSJamie Gritton if (j->comparam == NULL) {
4242671ee73SJamie Gritton dep_done(j, 0);
4252671ee73SJamie Gritton continue;
4262671ee73SJamie Gritton }
4273b40332cSJamie Gritton }
428e3c69673SJamie Gritton if (!(j->flags & JF_PARAMS))
4292671ee73SJamie Gritton {
430e3c69673SJamie Gritton j->flags |= JF_PARAMS;
4312671ee73SJamie Gritton if (dflag)
43255530438SJamie Gritton add_param(j, NULL, IP_ALLOW_DYING, NULL);
4332671ee73SJamie Gritton if (check_intparams(j) < 0)
4342671ee73SJamie Gritton continue;
435e3c69673SJamie Gritton if ((j->flags & (JF_START | JF_SET)) &&
4362671ee73SJamie Gritton import_params(j) < 0)
4372671ee73SJamie Gritton continue;
438e3c69673SJamie Gritton }
439ed31b3f4SJamie Gritton if (j->intparams[IP_ALLOW_DYING] && !dying_warned) {
440ed31b3f4SJamie Gritton warnx("%s", "the 'allow.dying' parameter and '-d' flag "
441ed31b3f4SJamie Gritton "are deprecated and have no effect.");
442ed31b3f4SJamie Gritton dying_warned = 1;
443ed31b3f4SJamie Gritton }
4442671ee73SJamie Gritton if (!j->jid)
445ed31b3f4SJamie Gritton running_jid(j);
4463b40332cSJamie Gritton if (finish_command(j))
4472671ee73SJamie Gritton continue;
4482671ee73SJamie Gritton
4492671ee73SJamie Gritton switch (j->flags & JF_OP_MASK) {
4502671ee73SJamie Gritton /*
4512671ee73SJamie Gritton * These operations just turn into a different op
4522671ee73SJamie Gritton * depending on the jail's current status.
4532671ee73SJamie Gritton */
4542671ee73SJamie Gritton case JF_START_SET:
455d50685b3SJamie Gritton j->flags = j->jid < 0
456d50685b3SJamie Gritton ? (j->flags & JF_CLEANUP) | JF_START : JF_SET;
457ebf5d9bcSMike Barcroft break;
4582671ee73SJamie Gritton case JF_SET_RESTART:
459d50685b3SJamie Gritton if (j->jid < 0 && !(j->flags & JF_CLEANUP)) {
4601ca35de4SJamie Gritton jail_quoted_warnx(j, "not found",
4611ca35de4SJamie Gritton "no jail specified");
4622671ee73SJamie Gritton failed(j);
4632671ee73SJamie Gritton continue;
4646dc044b4SMaxim Konovalov }
465d50685b3SJamie Gritton j->flags = rdtun_params(j, 0)
466d50685b3SJamie Gritton ? (j->flags & JF_CLEANUP) | JF_RESTART : JF_SET;
4672671ee73SJamie Gritton if (j->flags == JF_RESTART)
4682671ee73SJamie Gritton dep_reset(j);
4692671ee73SJamie Gritton break;
4702671ee73SJamie Gritton case JF_START_SET_RESTART:
471d50685b3SJamie Gritton j->flags = j->jid < 0 ? JF_START : rdtun_params(j, 0)
472d50685b3SJamie Gritton ? (j->flags & JF_CLEANUP) | JF_RESTART : JF_SET;
4732671ee73SJamie Gritton if (j->flags == JF_RESTART)
4742671ee73SJamie Gritton dep_reset(j);
4756dc044b4SMaxim Konovalov }
476843c277bSJamie Gritton
4772671ee73SJamie Gritton switch (j->flags & JF_OP_MASK) {
4782671ee73SJamie Gritton case JF_START:
4793b40332cSJamie Gritton if (j->comparam == NULL) {
4802671ee73SJamie Gritton if (j->jid > 0 &&
4812671ee73SJamie Gritton !(j->flags & (JF_DEPEND | JF_WILD))) {
4821ca35de4SJamie Gritton jail_quoted_warnx(j, "already exists",
4831ca35de4SJamie Gritton NULL);
4842671ee73SJamie Gritton failed(j);
4852671ee73SJamie Gritton continue;
48625639ca7SMike Barcroft }
4872671ee73SJamie Gritton if (dep_check(j))
4882671ee73SJamie Gritton continue;
4892671ee73SJamie Gritton if (j->jid > 0)
4902671ee73SJamie Gritton goto jail_create_done;
491d50685b3SJamie Gritton if (j->flags & JF_CLEANUP) {
492d50685b3SJamie Gritton j->flags |= JF_STOP;
493d50685b3SJamie Gritton j->comparam = cleancommands;
494d50685b3SJamie Gritton } else
495d50685b3SJamie Gritton j->comparam = startcommands;
49660080230SJamie Gritton j->comparam = startcommands;
49760080230SJamie Gritton j->comstring = NULL;
498cdafc851SPhilip Paeps }
4993b40332cSJamie Gritton if (next_command(j))
5002671ee73SJamie Gritton continue;
501d50685b3SJamie Gritton if (j->flags & JF_STOP)
502d50685b3SJamie Gritton goto jail_remove_done;
5033b40332cSJamie Gritton jail_create_done:
5043b40332cSJamie Gritton clear_persist(j);
5052671ee73SJamie Gritton if (jfp != NULL)
5061e1a4743SEugene Grosbein print_jail(jfp, j, oldcl, 1);
5072671ee73SJamie Gritton dep_done(j, 0);
5082671ee73SJamie Gritton break;
5092671ee73SJamie Gritton
5102671ee73SJamie Gritton case JF_SET:
5112671ee73SJamie Gritton if (j->jid < 0 && !(j->flags & JF_DEPEND)) {
5121ca35de4SJamie Gritton jail_quoted_warnx(j, "not found",
5131ca35de4SJamie Gritton "no jail specified");
5142671ee73SJamie Gritton failed(j);
515d8352076SJamie Gritton continue;
5162671ee73SJamie Gritton }
5172671ee73SJamie Gritton if (dep_check(j))
5182671ee73SJamie Gritton continue;
5192671ee73SJamie Gritton if (!(j->flags & JF_DEPEND)) {
5202671ee73SJamie Gritton if (rdtun_params(j, 1) < 0 ||
5212671ee73SJamie Gritton update_jail(j) < 0)
5222671ee73SJamie Gritton continue;
5232671ee73SJamie Gritton if (verbose >= 0 && (j->name || verbose > 0))
5242671ee73SJamie Gritton jail_note(j, "updated\n");
5252671ee73SJamie Gritton }
5262671ee73SJamie Gritton dep_done(j, 0);
5272671ee73SJamie Gritton break;
5282671ee73SJamie Gritton
5292671ee73SJamie Gritton case JF_STOP:
5302671ee73SJamie Gritton case JF_RESTART:
5313b40332cSJamie Gritton if (j->comparam == NULL) {
5322671ee73SJamie Gritton if (dep_check(j))
5332671ee73SJamie Gritton continue;
534d50685b3SJamie Gritton if (j->flags & JF_CLEANUP) {
535d50685b3SJamie Gritton j->comparam = j->jid < 0
536d50685b3SJamie Gritton ? cleancommands : stopcommands;
537d50685b3SJamie Gritton } else if (j->jid < 0) {
53884b354cbSHiroki Sato if (!(j->flags & (JF_DEPEND|JF_WILD))) {
53984b354cbSHiroki Sato if (verbose >= 0)
5401ca35de4SJamie Gritton jail_quoted_warnx(j,
5411ca35de4SJamie Gritton "not found", NULL);
54284b354cbSHiroki Sato failed(j);
54384b354cbSHiroki Sato }
5442671ee73SJamie Gritton goto jail_remove_done;
5452671ee73SJamie Gritton }
546d50685b3SJamie Gritton else
54760080230SJamie Gritton j->comparam = stopcommands;
54860080230SJamie Gritton j->comstring = NULL;
5492b00f7baSJamie Gritton } else if ((j->flags & JF_FAILED) && j->jid > 0)
5503b40332cSJamie Gritton goto jail_remove_done;
5513b40332cSJamie Gritton if (next_command(j))
5522671ee73SJamie Gritton continue;
5532671ee73SJamie Gritton jail_remove_done:
5542671ee73SJamie Gritton dep_done(j, 0);
5553b40332cSJamie Gritton if ((j->flags & (JF_START | JF_FAILED)) == JF_START) {
5563b40332cSJamie Gritton j->comparam = NULL;
557d50685b3SJamie Gritton j->flags &= ~(JF_STOP | JF_CLEANUP);
5582671ee73SJamie Gritton dep_reset(j);
5593b40332cSJamie Gritton requeue(j, j->ndeps ? &depend : &ready);
5602671ee73SJamie Gritton }
5612671ee73SJamie Gritton break;
5622671ee73SJamie Gritton }
56373d0971bSJamie Gritton }
56473d0971bSJamie Gritton
5652671ee73SJamie Gritton if (jfp != NULL)
5662671ee73SJamie Gritton fclose(jfp);
5672671ee73SJamie Gritton exit(error);
5682671ee73SJamie Gritton }
5692671ee73SJamie Gritton
5702671ee73SJamie Gritton /*
5712671ee73SJamie Gritton * Mark a jail's failure for future handling.
5722671ee73SJamie Gritton */
5732671ee73SJamie Gritton void
failed(struct cfjail * j)5742671ee73SJamie Gritton failed(struct cfjail *j)
5752671ee73SJamie Gritton {
5762671ee73SJamie Gritton j->flags |= JF_FAILED;
5772671ee73SJamie Gritton TAILQ_REMOVE(j->queue, j, tq);
5782671ee73SJamie Gritton TAILQ_INSERT_HEAD(&ready, j, tq);
5792671ee73SJamie Gritton j->queue = &ready;
5802671ee73SJamie Gritton }
5812671ee73SJamie Gritton
5822671ee73SJamie Gritton /*
5832671ee73SJamie Gritton * Exit slightly more gracefully when out of memory.
5842671ee73SJamie Gritton */
5852671ee73SJamie Gritton void *
emalloc(size_t size)5862671ee73SJamie Gritton emalloc(size_t size)
5872671ee73SJamie Gritton {
5882671ee73SJamie Gritton void *p;
5892671ee73SJamie Gritton
5902671ee73SJamie Gritton p = malloc(size);
5912671ee73SJamie Gritton if (!p)
5922671ee73SJamie Gritton err(1, "malloc");
5932671ee73SJamie Gritton return p;
5942671ee73SJamie Gritton }
5952671ee73SJamie Gritton
5962671ee73SJamie Gritton void *
erealloc(void * ptr,size_t size)5972671ee73SJamie Gritton erealloc(void *ptr, size_t size)
5982671ee73SJamie Gritton {
5992671ee73SJamie Gritton void *p;
6002671ee73SJamie Gritton
6012671ee73SJamie Gritton p = realloc(ptr, size);
6022671ee73SJamie Gritton if (!p)
6032671ee73SJamie Gritton err(1, "malloc");
6042671ee73SJamie Gritton return p;
6052671ee73SJamie Gritton }
6062671ee73SJamie Gritton
6072671ee73SJamie Gritton char *
estrdup(const char * str)6082671ee73SJamie Gritton estrdup(const char *str)
6092671ee73SJamie Gritton {
6102671ee73SJamie Gritton char *ns;
6112671ee73SJamie Gritton
6122671ee73SJamie Gritton ns = strdup(str);
6132671ee73SJamie Gritton if (!ns)
6142671ee73SJamie Gritton err(1, "malloc");
6152671ee73SJamie Gritton return ns;
6162671ee73SJamie Gritton }
6172671ee73SJamie Gritton
6182671ee73SJamie Gritton /*
6192671ee73SJamie Gritton * Print a message including an optional jail name.
6202671ee73SJamie Gritton */
6212671ee73SJamie Gritton void
jail_note(const struct cfjail * j,const char * fmt,...)6222671ee73SJamie Gritton jail_note(const struct cfjail *j, const char *fmt, ...)
6232671ee73SJamie Gritton {
6242671ee73SJamie Gritton va_list ap, tap;
6252671ee73SJamie Gritton char *cs;
6262671ee73SJamie Gritton size_t len;
6272671ee73SJamie Gritton
6282671ee73SJamie Gritton va_start(ap, fmt);
6292671ee73SJamie Gritton va_copy(tap, ap);
6302671ee73SJamie Gritton len = vsnprintf(NULL, 0, fmt, tap);
6312671ee73SJamie Gritton va_end(tap);
6322671ee73SJamie Gritton cs = alloca(len + 1);
6332671ee73SJamie Gritton (void)vsnprintf(cs, len + 1, fmt, ap);
6342671ee73SJamie Gritton va_end(ap);
6352671ee73SJamie Gritton if (j->name)
6362671ee73SJamie Gritton printf("%s: %s", j->name, cs);
6375b242e8cSMaxim Konovalov else
6382671ee73SJamie Gritton printf("%s", cs);
63973d0971bSJamie Gritton }
64073d0971bSJamie Gritton
6412671ee73SJamie Gritton /*
6422671ee73SJamie Gritton * Print a warning message including an optional jail name.
6432671ee73SJamie Gritton */
6442671ee73SJamie Gritton void
jail_warnx(const struct cfjail * j,const char * fmt,...)6452671ee73SJamie Gritton jail_warnx(const struct cfjail *j, const char *fmt, ...)
6462671ee73SJamie Gritton {
6472671ee73SJamie Gritton va_list ap, tap;
6482671ee73SJamie Gritton char *cs;
6492671ee73SJamie Gritton size_t len;
6502671ee73SJamie Gritton
6512671ee73SJamie Gritton va_start(ap, fmt);
6522671ee73SJamie Gritton va_copy(tap, ap);
6532671ee73SJamie Gritton len = vsnprintf(NULL, 0, fmt, tap);
6542671ee73SJamie Gritton va_end(tap);
6552671ee73SJamie Gritton cs = alloca(len + 1);
6562671ee73SJamie Gritton (void)vsnprintf(cs, len + 1, fmt, ap);
6572671ee73SJamie Gritton va_end(ap);
6582671ee73SJamie Gritton if (j->name)
6592671ee73SJamie Gritton warnx("%s: %s", j->name, cs);
6602671ee73SJamie Gritton else
6612671ee73SJamie Gritton warnx("%s", cs);
6622671ee73SJamie Gritton }
6632671ee73SJamie Gritton
6642671ee73SJamie Gritton /*
6652671ee73SJamie Gritton * Create a new jail.
6662671ee73SJamie Gritton */
6672b00f7baSJamie Gritton int
create_jail(struct cfjail * j)6682671ee73SJamie Gritton create_jail(struct cfjail *j)
6692671ee73SJamie Gritton {
6702671ee73SJamie Gritton struct stat st;
671ed31b3f4SJamie Gritton struct jailparam *jp, *setparams, *sjp;
6722671ee73SJamie Gritton const char *path;
673ed31b3f4SJamie Gritton int dopersist, ns;
6742671ee73SJamie Gritton
6752671ee73SJamie Gritton /*
6762671ee73SJamie Gritton * Check the jail's path, with a better error message than jail_set
6772671ee73SJamie Gritton * gives.
6782671ee73SJamie Gritton */
6792671ee73SJamie Gritton if ((path = string_param(j->intparams[KP_PATH]))) {
6807ca65ae0SJamie Gritton if (j->name != NULL && path[0] != '/') {
6818ebbf0e2SJamie Gritton jail_warnx(j, "path %s: not an absolute pathname",
6828ebbf0e2SJamie Gritton path);
6838ebbf0e2SJamie Gritton return -1;
6848ebbf0e2SJamie Gritton }
6852671ee73SJamie Gritton if (stat(path, &st) < 0) {
6862671ee73SJamie Gritton jail_warnx(j, "path %s: %s", path, strerror(errno));
6872671ee73SJamie Gritton return -1;
6882671ee73SJamie Gritton }
6892671ee73SJamie Gritton if (!S_ISDIR(st.st_mode)) {
6902671ee73SJamie Gritton jail_warnx(j, "path %s: %s", path, strerror(ENOTDIR));
6912671ee73SJamie Gritton return -1;
6922671ee73SJamie Gritton }
6932671ee73SJamie Gritton }
6942671ee73SJamie Gritton
6952671ee73SJamie Gritton /*
6962671ee73SJamie Gritton * Copy all the parameters, except that "persist" is always set when
6972671ee73SJamie Gritton * there are commands to run later.
6982671ee73SJamie Gritton */
6992671ee73SJamie Gritton dopersist = !bool_param(j->intparams[KP_PERSIST]) &&
7002671ee73SJamie Gritton (j->intparams[IP_EXEC_START] || j->intparams[IP_COMMAND] ||
7012671ee73SJamie Gritton j->intparams[IP_EXEC_POSTSTART]);
7022671ee73SJamie Gritton sjp = setparams =
7032671ee73SJamie Gritton alloca((j->njp + dopersist) * sizeof(struct jailparam));
7042671ee73SJamie Gritton if (dopersist && jailparam_init(sjp++, "persist") < 0) {
7052671ee73SJamie Gritton jail_warnx(j, "%s", jail_errmsg);
7062671ee73SJamie Gritton return -1;
7072671ee73SJamie Gritton }
7082671ee73SJamie Gritton for (jp = j->jp; jp < j->jp + j->njp; jp++)
7092671ee73SJamie Gritton if (!dopersist || !equalopts(jp->jp_name, "persist"))
7102671ee73SJamie Gritton *sjp++ = *jp;
7112671ee73SJamie Gritton ns = sjp - setparams;
7122671ee73SJamie Gritton
7132671ee73SJamie Gritton j->jid = jailparam_set_note(j, setparams, ns, JAIL_CREATE);
714ed31b3f4SJamie Gritton if (j->jid < 0) {
7152671ee73SJamie Gritton jail_warnx(j, "%s", jail_errmsg);
7162671ee73SJamie Gritton failed(j);
7172671ee73SJamie Gritton }
7182671ee73SJamie Gritton if (dopersist) {
7192671ee73SJamie Gritton jailparam_free(setparams, 1);
7202671ee73SJamie Gritton if (j->jid > 0)
7212671ee73SJamie Gritton j->flags |= JF_PERSIST;
7222671ee73SJamie Gritton }
7232671ee73SJamie Gritton return j->jid;
7242671ee73SJamie Gritton }
7252671ee73SJamie Gritton
7262671ee73SJamie Gritton /*
7272671ee73SJamie Gritton * Remove a temporarily set "persist" parameter.
7282671ee73SJamie Gritton */
72973d0971bSJamie Gritton static void
clear_persist(struct cfjail * j)7302671ee73SJamie Gritton clear_persist(struct cfjail *j)
73173d0971bSJamie Gritton {
7322671ee73SJamie Gritton struct iovec jiov[4];
7332671ee73SJamie Gritton int jid;
73473d0971bSJamie Gritton
7352671ee73SJamie Gritton if (!(j->flags & JF_PERSIST))
7362671ee73SJamie Gritton return;
7372671ee73SJamie Gritton j->flags &= ~JF_PERSIST;
738d031802bSJamie Gritton jiov[0].iov_base = __DECONST(char *, "jid");
7392671ee73SJamie Gritton jiov[0].iov_len = sizeof("jid");
7402671ee73SJamie Gritton jiov[1].iov_base = &j->jid;
7412671ee73SJamie Gritton jiov[1].iov_len = sizeof(j->jid);
742d031802bSJamie Gritton jiov[2].iov_base = __DECONST(char *, "nopersist");
7432671ee73SJamie Gritton jiov[2].iov_len = sizeof("nopersist");
7442671ee73SJamie Gritton jiov[3].iov_base = NULL;
7452671ee73SJamie Gritton jiov[3].iov_len = 0;
7462671ee73SJamie Gritton jid = jail_set(jiov, 4, JAIL_UPDATE);
7472671ee73SJamie Gritton if (verbose > 0)
7482671ee73SJamie Gritton jail_note(j, "jail_set(JAIL_UPDATE) jid=%d nopersist%s%s\n",
7492671ee73SJamie Gritton j->jid, jid < 0 ? ": " : "",
7502671ee73SJamie Gritton jid < 0 ? strerror(errno) : "");
75173d0971bSJamie Gritton }
7522671ee73SJamie Gritton
7532671ee73SJamie Gritton /*
7542671ee73SJamie Gritton * Set a jail's parameters.
7552671ee73SJamie Gritton */
7562671ee73SJamie Gritton static int
update_jail(struct cfjail * j)7572671ee73SJamie Gritton update_jail(struct cfjail *j)
7582671ee73SJamie Gritton {
7592671ee73SJamie Gritton struct jailparam *jp, *setparams, *sjp;
7602671ee73SJamie Gritton int ns, jid;
7612671ee73SJamie Gritton
7622671ee73SJamie Gritton ns = 0;
7632671ee73SJamie Gritton for (jp = j->jp; jp < j->jp + j->njp; jp++)
7642671ee73SJamie Gritton if (!JP_RDTUN(jp))
7652671ee73SJamie Gritton ns++;
7662671ee73SJamie Gritton if (ns == 0)
7672671ee73SJamie Gritton return 0;
7682671ee73SJamie Gritton sjp = setparams = alloca(++ns * sizeof(struct jailparam));
7692671ee73SJamie Gritton if (jailparam_init(sjp, "jid") < 0 ||
7702671ee73SJamie Gritton jailparam_import_raw(sjp, &j->jid, sizeof j->jid) < 0) {
7712671ee73SJamie Gritton jail_warnx(j, "%s", jail_errmsg);
7722671ee73SJamie Gritton failed(j);
7732671ee73SJamie Gritton return -1;
77473d0971bSJamie Gritton }
7752671ee73SJamie Gritton for (jp = j->jp; jp < j->jp + j->njp; jp++)
7762671ee73SJamie Gritton if (!JP_RDTUN(jp))
7772671ee73SJamie Gritton *++sjp = *jp;
77873d0971bSJamie Gritton
779ed31b3f4SJamie Gritton jid = jailparam_set_note(j, setparams, ns, JAIL_UPDATE);
7802671ee73SJamie Gritton if (jid < 0) {
7812671ee73SJamie Gritton jail_warnx(j, "%s", jail_errmsg);
7822671ee73SJamie Gritton failed(j);
7832671ee73SJamie Gritton }
7842671ee73SJamie Gritton jailparam_free(setparams, 1);
7852671ee73SJamie Gritton return jid;
7862671ee73SJamie Gritton }
78773d0971bSJamie Gritton
7882671ee73SJamie Gritton /*
7892671ee73SJamie Gritton * Return if a jail set would change any create-only parameters.
7902671ee73SJamie Gritton */
7912671ee73SJamie Gritton static int
rdtun_params(struct cfjail * j,int dofail)7922671ee73SJamie Gritton rdtun_params(struct cfjail *j, int dofail)
79373d0971bSJamie Gritton {
7942671ee73SJamie Gritton struct jailparam *jp, *rtparams, *rtjp;
7958f154378SJamie Gritton const void *jp_value;
7968f154378SJamie Gritton size_t jp_valuelen;
7978f154378SJamie Gritton int nrt, rval, bool_true;
7982671ee73SJamie Gritton
7992671ee73SJamie Gritton if (j->flags & JF_RDTUN)
8002671ee73SJamie Gritton return 0;
8012671ee73SJamie Gritton j->flags |= JF_RDTUN;
8022671ee73SJamie Gritton nrt = 0;
8032671ee73SJamie Gritton for (jp = j->jp; jp < j->jp + j->njp; jp++)
8042671ee73SJamie Gritton if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid"))
8052671ee73SJamie Gritton nrt++;
8062671ee73SJamie Gritton if (nrt == 0)
8072671ee73SJamie Gritton return 0;
8082671ee73SJamie Gritton rtjp = rtparams = alloca(++nrt * sizeof(struct jailparam));
8092671ee73SJamie Gritton if (jailparam_init(rtjp, "jid") < 0 ||
8102671ee73SJamie Gritton jailparam_import_raw(rtjp, &j->jid, sizeof j->jid) < 0) {
8112671ee73SJamie Gritton jail_warnx(j, "%s", jail_errmsg);
8122671ee73SJamie Gritton exit(1);
8132671ee73SJamie Gritton }
8142671ee73SJamie Gritton for (jp = j->jp; jp < j->jp + j->njp; jp++)
81592bceb97SJamie Gritton if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid")) {
8162671ee73SJamie Gritton *++rtjp = *jp;
81792bceb97SJamie Gritton rtjp->jp_value = NULL;
81892bceb97SJamie Gritton }
8192671ee73SJamie Gritton rval = 0;
820ed31b3f4SJamie Gritton if (jailparam_get(rtparams, nrt, 0) > 0) {
8212671ee73SJamie Gritton rtjp = rtparams + 1;
822760f2905SXin LI for (jp = j->jp; rtjp < rtparams + nrt; jp++) {
8232671ee73SJamie Gritton if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid")) {
8248f154378SJamie Gritton jp_value = jp->jp_value;
8258f154378SJamie Gritton jp_valuelen = jp->jp_valuelen;
8268f154378SJamie Gritton if (jp_value == NULL && jp_valuelen > 0) {
8278f154378SJamie Gritton if (jp->jp_flags & (JP_BOOL |
8288f154378SJamie Gritton JP_NOBOOL | JP_JAILSYS)) {
8298f154378SJamie Gritton bool_true = 1;
8308f154378SJamie Gritton jp_value = &bool_true;
8318f154378SJamie Gritton jp_valuelen = sizeof(bool_true);
8328f154378SJamie Gritton } else if ((jp->jp_ctltype & CTLTYPE) ==
8338f154378SJamie Gritton CTLTYPE_STRING)
8348f154378SJamie Gritton jp_value = "";
8358f154378SJamie Gritton else
8368f154378SJamie Gritton jp_valuelen = 0;
8378f154378SJamie Gritton }
8388f154378SJamie Gritton if (rtjp->jp_valuelen != jp_valuelen ||
8398f154378SJamie Gritton (CTLTYPE_STRING ? strncmp(rtjp->jp_value,
8408f154378SJamie Gritton jp_value, jp_valuelen)
8418f154378SJamie Gritton : memcmp(rtjp->jp_value, jp_value,
8428f154378SJamie Gritton jp_valuelen))) {
8432671ee73SJamie Gritton if (dofail) {
8442671ee73SJamie Gritton jail_warnx(j, "%s cannot be "
8452671ee73SJamie Gritton "changed after creation",
8462671ee73SJamie Gritton jp->jp_name);
8472671ee73SJamie Gritton failed(j);
8482671ee73SJamie Gritton rval = -1;
8492671ee73SJamie Gritton } else
8502671ee73SJamie Gritton rval = 1;
85173d0971bSJamie Gritton break;
85273d0971bSJamie Gritton }
8532671ee73SJamie Gritton rtjp++;
85473d0971bSJamie Gritton }
8552671ee73SJamie Gritton }
8562671ee73SJamie Gritton }
8572671ee73SJamie Gritton for (rtjp = rtparams + 1; rtjp < rtparams + nrt; rtjp++)
8582671ee73SJamie Gritton rtjp->jp_name = NULL;
8592671ee73SJamie Gritton jailparam_free(rtparams, nrt);
8602671ee73SJamie Gritton return rval;
8612671ee73SJamie Gritton }
8622671ee73SJamie Gritton
8632671ee73SJamie Gritton /*
8642671ee73SJamie Gritton * Get the jail's jid if it is running.
8652671ee73SJamie Gritton */
8662671ee73SJamie Gritton static void
running_jid(struct cfjail * j)867ed31b3f4SJamie Gritton running_jid(struct cfjail *j)
8682671ee73SJamie Gritton {
8692671ee73SJamie Gritton struct iovec jiov[2];
8702671ee73SJamie Gritton const char *pval;
8712671ee73SJamie Gritton char *ep;
8722671ee73SJamie Gritton int jid;
8732671ee73SJamie Gritton
87450f0104eSJamie Gritton if ((pval = string_param(j->intparams[KP_JID]))) {
8752671ee73SJamie Gritton if (!(jid = strtol(pval, &ep, 10)) || *ep) {
8762671ee73SJamie Gritton j->jid = -1;
8772671ee73SJamie Gritton return;
8782671ee73SJamie Gritton }
879d031802bSJamie Gritton jiov[0].iov_base = __DECONST(char *, "jid");
8802671ee73SJamie Gritton jiov[0].iov_len = sizeof("jid");
8812671ee73SJamie Gritton jiov[1].iov_base = &jid;
8822671ee73SJamie Gritton jiov[1].iov_len = sizeof(jid);
88350f0104eSJamie Gritton } else if ((pval = string_param(j->intparams[KP_NAME]))) {
884d031802bSJamie Gritton jiov[0].iov_base = __DECONST(char *, "name");
8852671ee73SJamie Gritton jiov[0].iov_len = sizeof("name");
8862671ee73SJamie Gritton jiov[1].iov_len = strlen(pval) + 1;
8872671ee73SJamie Gritton jiov[1].iov_base = alloca(jiov[1].iov_len);
8882671ee73SJamie Gritton strcpy(jiov[1].iov_base, pval);
88950f0104eSJamie Gritton } else {
89050f0104eSJamie Gritton j->jid = -1;
89150f0104eSJamie Gritton return;
8922671ee73SJamie Gritton }
893ed31b3f4SJamie Gritton j->jid = jail_get(jiov, 2, 0);
8942671ee73SJamie Gritton }
89573d0971bSJamie Gritton
89673d0971bSJamie Gritton static void
jail_quoted_warnx(const struct cfjail * j,const char * name_msg,const char * noname_msg)8971ca35de4SJamie Gritton jail_quoted_warnx(const struct cfjail *j, const char *name_msg,
8981ca35de4SJamie Gritton const char *noname_msg)
89973d0971bSJamie Gritton {
9001ca35de4SJamie Gritton const char *pval;
9011ca35de4SJamie Gritton
9021ca35de4SJamie Gritton if ((pval = j->name) || (pval = string_param(j->intparams[KP_JID])) ||
9031ca35de4SJamie Gritton (pval = string_param(j->intparams[KP_NAME])))
9041ca35de4SJamie Gritton warnx("\"%s\" %s", pval, name_msg);
9051ca35de4SJamie Gritton else
9061ca35de4SJamie Gritton warnx("%s", noname_msg);
9071ca35de4SJamie Gritton }
9081ca35de4SJamie Gritton
9092671ee73SJamie Gritton /*
910d031802bSJamie Gritton * Set jail parameters and possibly print them out.
9112671ee73SJamie Gritton */
9122671ee73SJamie Gritton static int
jailparam_set_note(const struct cfjail * j,struct jailparam * jp,unsigned njp,int flags)9132671ee73SJamie Gritton jailparam_set_note(const struct cfjail *j, struct jailparam *jp, unsigned njp,
9142671ee73SJamie Gritton int flags)
9152671ee73SJamie Gritton {
9162671ee73SJamie Gritton char *value;
9172671ee73SJamie Gritton int jid;
9182671ee73SJamie Gritton unsigned i;
9192671ee73SJamie Gritton
9202671ee73SJamie Gritton jid = jailparam_set(jp, njp, flags);
9212671ee73SJamie Gritton if (verbose > 0) {
922ed31b3f4SJamie Gritton jail_note(j, "jail_set(%s)",
9232671ee73SJamie Gritton (flags & (JAIL_CREATE | JAIL_UPDATE)) == JAIL_CREATE
924ed31b3f4SJamie Gritton ? "JAIL_CREATE" : "JAIL_UPDATE");
9252671ee73SJamie Gritton for (i = 0; i < njp; i++) {
9262671ee73SJamie Gritton printf(" %s", jp[i].jp_name);
9272671ee73SJamie Gritton if (jp[i].jp_value == NULL)
9282671ee73SJamie Gritton continue;
9292671ee73SJamie Gritton putchar('=');
9302671ee73SJamie Gritton value = jailparam_export(jp + i);
9312671ee73SJamie Gritton if (value == NULL)
9322671ee73SJamie Gritton err(1, "jailparam_export");
9332671ee73SJamie Gritton quoted_print(stdout, value);
9342671ee73SJamie Gritton free(value);
9352671ee73SJamie Gritton }
9362671ee73SJamie Gritton if (jid < 0)
9372671ee73SJamie Gritton printf(": %s", strerror(errno));
9382671ee73SJamie Gritton printf("\n");
9392671ee73SJamie Gritton }
9402671ee73SJamie Gritton return jid;
9412671ee73SJamie Gritton }
9422671ee73SJamie Gritton
9432671ee73SJamie Gritton /*
9442671ee73SJamie Gritton * Print a jail record.
9452671ee73SJamie Gritton */
9462671ee73SJamie Gritton static void
print_jail(FILE * fp,struct cfjail * j,int oldcl,int running)9471e1a4743SEugene Grosbein print_jail(FILE *fp, struct cfjail *j, int oldcl, int running)
9482671ee73SJamie Gritton {
9492671ee73SJamie Gritton struct cfparam *p;
9501e1a4743SEugene Grosbein int printsep;
9512671ee73SJamie Gritton
9522671ee73SJamie Gritton if (oldcl) {
9531e1a4743SEugene Grosbein if (running)
9541e1a4743SEugene Grosbein fprintf(fp, "%d%s", j->jid, separator);
9552671ee73SJamie Gritton print_param(fp, j->intparams[KP_PATH], ',', 0);
9561e1a4743SEugene Grosbein fputs(separator, fp);
95755530438SJamie Gritton print_param(fp, j->intparams[KP_HOST_HOSTNAME], ',', 0);
9581e1a4743SEugene Grosbein fputs(separator, fp);
95915ede760SBjoern A. Zeeb #ifdef INET
9602671ee73SJamie Gritton print_param(fp, j->intparams[KP_IP4_ADDR], ',', 0);
9612671ee73SJamie Gritton #ifdef INET6
9622671ee73SJamie Gritton if (j->intparams[KP_IP4_ADDR] &&
9635fb611c2SJamie Gritton !TAILQ_EMPTY(&j->intparams[KP_IP4_ADDR]->val) &&
9645fb611c2SJamie Gritton j->intparams[KP_IP6_ADDR] &&
9655fb611c2SJamie Gritton !TAILQ_EMPTY(&j->intparams[KP_IP6_ADDR]->val))
9662671ee73SJamie Gritton putc(',', fp);
9675fb611c2SJamie Gritton #endif
96815ede760SBjoern A. Zeeb #endif
96973d0971bSJamie Gritton #ifdef INET6
9702671ee73SJamie Gritton print_param(fp, j->intparams[KP_IP6_ADDR], ',', 0);
97173d0971bSJamie Gritton #endif
9721e1a4743SEugene Grosbein fputs(separator, fp);
9732671ee73SJamie Gritton print_param(fp, j->intparams[IP_COMMAND], ' ', 0);
9742671ee73SJamie Gritton } else {
9751e1a4743SEugene Grosbein printsep = 0;
9761e1a4743SEugene Grosbein if (running) {
9772671ee73SJamie Gritton fprintf(fp, "jid=%d", j->jid);
9781e1a4743SEugene Grosbein printsep = 1;
9791e1a4743SEugene Grosbein }
9802671ee73SJamie Gritton TAILQ_FOREACH(p, &j->params, tq)
9812671ee73SJamie Gritton if (strcmp(p->name, "jid")) {
9821e1a4743SEugene Grosbein if (printsep)
9831e1a4743SEugene Grosbein fputs(separator, fp);
9841e1a4743SEugene Grosbein else
9851e1a4743SEugene Grosbein printsep = 1;
9862671ee73SJamie Gritton print_param(fp, p, ',', 1);
98773d0971bSJamie Gritton }
9882671ee73SJamie Gritton }
9892671ee73SJamie Gritton putc('\n', fp);
99073d0971bSJamie Gritton }
99173d0971bSJamie Gritton
9922671ee73SJamie Gritton /*
9931e1a4743SEugene Grosbein * Exhibit list of all configured non-wildcard jails
9941e1a4743SEugene Grosbein */
9951e1a4743SEugene Grosbein static void
show_jails(void)9961e1a4743SEugene Grosbein show_jails(void)
9971e1a4743SEugene Grosbein {
9981e1a4743SEugene Grosbein struct cfjail *j;
9991e1a4743SEugene Grosbein
10001e1a4743SEugene Grosbein TAILQ_FOREACH(j, &cfjails, tq)
10011e1a4743SEugene Grosbein print_jail(stdout, j, 0, 0);
10021e1a4743SEugene Grosbein }
10031e1a4743SEugene Grosbein
10041e1a4743SEugene Grosbein /*
10052671ee73SJamie Gritton * Print a parameter value, or a name=value pair.
10062671ee73SJamie Gritton */
10072671ee73SJamie Gritton static void
print_param(FILE * fp,const struct cfparam * p,int sep,int doname)10082671ee73SJamie Gritton print_param(FILE *fp, const struct cfparam *p, int sep, int doname)
10092671ee73SJamie Gritton {
10102671ee73SJamie Gritton const struct cfstring *s, *ts;
10112671ee73SJamie Gritton
10122671ee73SJamie Gritton if (doname)
10132671ee73SJamie Gritton fputs(p->name, fp);
10142a194551SJamie Gritton if (p == NULL || TAILQ_EMPTY(&p->val))
10152671ee73SJamie Gritton return;
10162671ee73SJamie Gritton if (doname)
10172671ee73SJamie Gritton putc('=', fp);
10182a194551SJamie Gritton TAILQ_FOREACH_SAFE(s, &p->val, tq, ts) {
10192671ee73SJamie Gritton quoted_print(fp, s->s);
10202671ee73SJamie Gritton if (ts != NULL)
10212671ee73SJamie Gritton putc(sep, fp);
10222671ee73SJamie Gritton }
10232671ee73SJamie Gritton }
10242671ee73SJamie Gritton
10252671ee73SJamie Gritton /*
10262671ee73SJamie Gritton * Print a string with quotes around spaces.
10272671ee73SJamie Gritton */
102873d0971bSJamie Gritton static void
quoted_print(FILE * fp,char * str)102973d0971bSJamie Gritton quoted_print(FILE *fp, char *str)
103073d0971bSJamie Gritton {
103173d0971bSJamie Gritton int c, qc;
103273d0971bSJamie Gritton char *p = str;
103373d0971bSJamie Gritton
10342671ee73SJamie Gritton qc = !*p ? '"'
10352671ee73SJamie Gritton : strchr(p, '\'') ? '"'
103673d0971bSJamie Gritton : strchr(p, '"') ? '\''
103773d0971bSJamie Gritton : strchr(p, ' ') || strchr(p, '\t') ? '"'
103873d0971bSJamie Gritton : 0;
103973d0971bSJamie Gritton if (qc)
104073d0971bSJamie Gritton putc(qc, fp);
104173d0971bSJamie Gritton while ((c = *p++)) {
104273d0971bSJamie Gritton if (c == '\\' || c == qc)
104373d0971bSJamie Gritton putc('\\', fp);
104473d0971bSJamie Gritton putc(c, fp);
104573d0971bSJamie Gritton }
104673d0971bSJamie Gritton if (qc)
104773d0971bSJamie Gritton putc(qc, fp);
104873d0971bSJamie Gritton }
104973d0971bSJamie Gritton
105073d0971bSJamie Gritton static void
usage(void)1051d6131f4bSMaxim Konovalov usage(void)
1052d6131f4bSMaxim Konovalov {
1053d6131f4bSMaxim Konovalov
105473d0971bSJamie Gritton (void)fprintf(stderr,
10552671ee73SJamie Gritton "usage: jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
10562671ee73SJamie Gritton " -[cmr] param=value ... [command=command ...]\n"
10574e49fbcdSEugene Grosbein " jail [-dqv] [-f file] -[cmr] [jail]\n"
10582671ee73SJamie Gritton " jail [-qv] [-f file] -[rR] ['*' | jail ...]\n"
10592671ee73SJamie Gritton " jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
10602671ee73SJamie Gritton " [-n jailname] [-s securelevel]\n"
10618c1d956fSJamie Gritton " path hostname ip[,...] command ...\n"
10624e49fbcdSEugene Grosbein " jail [-f file] -e separator\n");
1063b026ec0eSMaxim Konovalov exit(1);
1064d6131f4bSMaxim Konovalov }
1065