xref: /freebsd/usr.sbin/jail/jailp.h (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 James Gritton.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/jail.h>
32 #include <sys/queue.h>
33 #include <sys/time.h>
34 
35 #include <jail.h>
36 #include <stdio.h>
37 
38 #define CONF_FILE	"/etc/jail.conf"
39 
40 #define DEP_FROM	0
41 #define DEP_TO		1
42 
43 #define DF_SEEN		0x01	/* Dependency has been followed */
44 #define DF_LIGHT	0x02	/* Implied dependency on jail existence only */
45 #define DF_NOFAIL	0x04	/* Don't propagate failed jails */
46 
47 #define PF_VAR		0x0001	/* This is a variable, not a true parameter */
48 #define PF_APPEND	0x0002	/* Append to existing parameter list */
49 #define PF_BAD		0x0004	/* Unable to resolve parameter value */
50 #define PF_INTERNAL	0x0008	/* Internal parameter, not passed to kernel */
51 #define PF_BOOL		0x0010	/* Boolean parameter */
52 #define PF_INT		0x0020	/* Integer parameter */
53 #define PF_CONV		0x0040	/* Parameter duplicated in converted form */
54 #define PF_REV		0x0080	/* Run commands in reverse order on stopping */
55 #define	PF_IMMUTABLE	0x0100	/* Immutable parameter */
56 #define	PF_NAMEVAL	0x0200	/* Parameter is in "name value" form */
57 
58 #define JF_START	0x0001	/* -c */
59 #define JF_SET		0x0002	/* -m */
60 #define JF_STOP		0x0004	/* -r */
61 #define JF_DEPEND	0x0008	/* Operation required by dependency */
62 #define JF_WILD		0x0010	/* Not specified on the command line */
63 #define JF_FAILED	0x0020	/* Operation failed */
64 #define JF_PARAMS	0x0040	/* Parameters checked and imported */
65 #define JF_RDTUN	0x0080	/* Create-only parameter check has been done */
66 #define JF_PERSIST	0x0100	/* Jail is temporarily persistent */
67 #define JF_TIMEOUT	0x0200	/* A command (or process kill) timed out */
68 #define JF_SLEEPQ	0x0400	/* Waiting on a command and/or timeout */
69 #define JF_FROM_RUNQ	0x0800	/* Has already been on the run queue */
70 #define JF_CLEANUP	0x1000	/* -C Run post-removal commands */
71 
72 #define JF_OP_MASK		(JF_START | JF_SET | JF_STOP)
73 #define JF_RESTART		(JF_START | JF_STOP)
74 #define JF_START_SET		(JF_START | JF_SET)
75 #define JF_SET_RESTART		(JF_SET | JF_STOP)
76 #define JF_START_SET_RESTART	(JF_START | JF_SET | JF_STOP)
77 #define JF_DO_STOP(js)		(((js) & (JF_SET | JF_STOP)) == JF_STOP)
78 
79 enum intparam {
80 	IP__NULL = 0,		/* Null command */
81 	IP_ALLOW_DYING,		/* Allow making changes to a dying jail */
82 	IP_COMMAND,		/* Command run inside jail at creation */
83 	IP_DEPEND,		/* Jail starts after (stops before) another */
84 	IP_EXEC_CLEAN,		/* Run commands in a clean environment */
85 	IP_EXEC_CONSOLELOG,	/* Redirect optput for commands run in jail */
86 	IP_EXEC_FIB,		/* Run jailed commands with this FIB */
87 	IP_EXEC_JAIL_USER,	/* Run jailed commands as this user */
88 	IP_EXEC_POSTSTART,	/* Commands run outside jail after creating */
89 	IP_EXEC_POSTSTOP,	/* Commands run outside jail after removing */
90 	IP_EXEC_PREPARE,	/* Commands run outside jail before addrs and mounting */
91 	IP_EXEC_PRESTART,	/* Commands run outside jail before creating */
92 	IP_EXEC_PRESTOP,	/* Commands run outside jail before removing */
93 	IP_EXEC_RELEASE,	/* Commands run outside jail after addrs and unmounted */
94 	IP_EXEC_CREATED,	/* Commands run outside jail right after it was started */
95 	IP_EXEC_START,		/* Commands run inside jail on creation */
96 	IP_EXEC_STOP,		/* Commands run inside jail on removal */
97 	IP_EXEC_SYSTEM_JAIL_USER,/* Get jail_user from system passwd file */
98 	IP_EXEC_SYSTEM_USER,	/* Run non-jailed commands as this user */
99 	IP_EXEC_TIMEOUT,	/* Time to wait for a command to complete */
100 #if defined(INET) || defined(INET6)
101 	IP_INTERFACE,		/* Add IP addresses to this interface */
102 	IP_IP_HOSTNAME,		/* Get jail IP address(es) from hostname */
103 #endif
104 	IP_MOUNT,		/* Mount points in fstab(5) form */
105 	IP_MOUNT_DEVFS,		/* Mount /dev under prison root */
106 	IP_MOUNT_FDESCFS,	/* Mount /dev/fd under prison root */
107 	IP_MOUNT_PROCFS,	/* Mount /proc under prison root */
108 	IP_MOUNT_FSTAB,		/* A standard fstab(5) file */
109 	IP_STOP_TIMEOUT,	/* Time to wait after sending SIGTERM */
110 	IP_VNET_INTERFACE,	/* Assign interface(s) to vnet jail */
111 	IP_ZFS_DATASET,		/* Jail ZFS datasets */
112 #ifdef INET
113 	IP__IP4_IFADDR,		/* Copy of ip4.addr with interface/netmask */
114 #endif
115 #ifdef INET6
116 	IP__IP6_IFADDR,		/* Copy of ip6.addr with interface/prefixlen */
117 #endif
118 	IP__MOUNT_FROM_FSTAB,	/* Line from mount.fstab file */
119 	IP__OP,			/* Placeholder for requested operation */
120 	KP_ALLOW_CHFLAGS,
121 	KP_ALLOW_MOUNT,
122 	KP_ALLOW_RAW_SOCKETS,
123 	KP_ALLOW_SET_HOSTNAME,
124 	KP_ALLOW_SOCKET_AF,
125 	KP_ALLOW_SYSVIPC,
126 	KP_DEVFS_RULESET,
127 	KP_HOST_HOSTNAME,
128 #ifdef INET
129 	KP_IP4_ADDR,
130 #endif
131 #ifdef INET6
132 	KP_IP6_ADDR,
133 #endif
134 	KP_JID,
135 	KP_NAME,
136 	KP_PATH,
137 	KP_PERSIST,
138 	KP_SECURELEVEL,
139 	KP_VNET,
140 	IP_NPARAM
141 };
142 
143 STAILQ_HEAD(cfvars, cfvar);
144 
145 struct cfvar {
146 	STAILQ_ENTRY(cfvar)	tq;
147 	char			*name;
148 	size_t			pos;
149 };
150 
151 TAILQ_HEAD(cfstrings, cfstring);
152 
153 struct cfstring {
154 	TAILQ_ENTRY(cfstring)	tq;
155 	char			*s;
156 	size_t			len;
157 	struct cfvars		vars;
158 };
159 
160 TAILQ_HEAD(cfparams, cfparam);
161 
162 struct cfparam {
163 	TAILQ_ENTRY(cfparam)	tq;
164 	char			*name;
165 	struct cfstrings	val;
166 	unsigned		flags;
167 	int			gen;
168 };
169 
170 TAILQ_HEAD(cfjails, cfjail);
171 STAILQ_HEAD(cfdepends, cfdepend);
172 
173 struct cfjail {
174 	TAILQ_ENTRY(cfjail)	tq;
175 	char			*name;
176 	char			*comline;
177 	struct cfparams		params;
178 	struct cfdepends	dep[2];
179 	struct cfjails		*queue;
180 	struct cfjail		*cfparent;
181 	struct cfparam		*intparams[IP_NPARAM];
182 	struct cfstring		*comstring;
183 	struct jailparam	*jp;
184 	struct timespec		timeout;
185 	const enum intparam	*comparam;
186 	unsigned		flags;
187 	int			jid;
188 	int			seq;
189 	int			pstatus;
190 	int			ndeps;
191 	int			njp;
192 	int			nprocs;
193 };
194 
195 struct cfdepend {
196 	STAILQ_ENTRY(cfdepend)	tq[2];
197 	struct cfjail		*j[2];
198 	unsigned		flags;
199 };
200 
201 struct cflex {
202 	const char		*cfname;
203 	int			error;
204 };
205 
206 extern void *emalloc(size_t);
207 extern void *erealloc(void *, size_t);
208 extern char *estrdup(const char *);
209 extern int create_jail(struct cfjail *j);
210 extern void failed(struct cfjail *j);
211 extern void jail_note(const struct cfjail *j, const char *fmt, ...);
212 extern void jail_warnx(const struct cfjail *j, const char *fmt, ...);
213 
214 extern int next_command(struct cfjail *j);
215 extern int finish_command(struct cfjail *j);
216 extern struct cfjail *next_proc(int nonblock);
217 
218 extern void load_config(const char *cfname);
219 extern void include_config(void *scanner, const char *cfname);
220 extern struct cfjail *add_jail(void);
221 extern void add_param(struct cfjail *j, const struct cfparam *p,
222     enum intparam ipnum, const char *value);
223 extern int bool_param(const struct cfparam *p);
224 extern int int_param(const struct cfparam *p, int *ip);
225 extern const char *string_param(const struct cfparam *p);
226 extern int check_intparams(struct cfjail *j);
227 extern int import_params(struct cfjail *j);
228 extern int equalopts(const char *opt1, const char *opt2);
229 extern int wild_jail_name(const char *wname);
230 extern int wild_jail_match(const char *jname, const char *wname);
231 extern void free_param_strings(struct cfparam *p);
232 
233 extern void dep_setup(int docf);
234 extern int dep_check(struct cfjail *j);
235 extern void dep_done(struct cfjail *j, unsigned flags);
236 extern void dep_reset(struct cfjail *j);
237 extern struct cfjail *next_jail(void);
238 extern int start_state(const char *target, int docf, unsigned state,
239     int running);
240 extern void requeue(struct cfjail *j, struct cfjails *queue);
241 extern void requeue_head(struct cfjail *j, struct cfjails *queue);
242 
243 extern struct cflex *yyget_extra(void *scanner);
244 extern FILE *yyget_in(void *scanner);
245 extern int yyget_lineno(void *scanner);
246 extern char *yyget_text(void *scanner);
247 
248 extern struct cfjails cfjails;
249 extern struct cfjails ready;
250 extern struct cfjails depend;
251 extern int iflag;
252 extern int note_remove;
253 extern int paralimit;
254 extern int verbose;
255