1214117Sjamie/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
4223190Sjamie * Copyright (c) 2011 James Gritton.
5214117Sjamie * All rights reserved.
6214117Sjamie *
7214117Sjamie * Redistribution and use in source and binary forms, with or without
8214117Sjamie * modification, are permitted provided that the following conditions
9214117Sjamie * are met:
10214117Sjamie * 1. Redistributions of source code must retain the above copyright
11214117Sjamie *    notice, this list of conditions and the following disclaimer.
12214117Sjamie * 2. Redistributions in binary form must reproduce the above copyright
13214117Sjamie *    notice, this list of conditions and the following disclaimer in the
14214117Sjamie *    documentation and/or other materials provided with the distribution.
15214117Sjamie *
16214117Sjamie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17214117Sjamie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18214117Sjamie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19214117Sjamie * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20214117Sjamie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21214117Sjamie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22214117Sjamie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23214117Sjamie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24214117Sjamie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25214117Sjamie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26214117Sjamie * SUCH DAMAGE.
27214117Sjamie *
28214117Sjamie * $FreeBSD: stable/11/usr.sbin/jail/jailp.h 341790 2018-12-10 13:47:05Z eugen $
29214117Sjamie */
30214117Sjamie
31214117Sjamie#include <sys/param.h>
32214117Sjamie#include <sys/types.h>
33214117Sjamie#include <sys/jail.h>
34214117Sjamie#include <sys/queue.h>
35214117Sjamie#include <sys/time.h>
36214117Sjamie
37214117Sjamie#include <jail.h>
38214117Sjamie
39214117Sjamie#define CONF_FILE	"/etc/jail.conf"
40214117Sjamie
41214117Sjamie#define DEP_FROM	0
42214117Sjamie#define DEP_TO		1
43214117Sjamie
44214117Sjamie#define DF_SEEN		0x01	/* Dependency has been followed */
45214117Sjamie#define DF_LIGHT	0x02	/* Implied dependency on jail existence only */
46289677Seadler#define DF_NOFAIL	0x04	/* Don't propagate failed jails */
47214117Sjamie
48214117Sjamie#define PF_VAR		0x01	/* This is a variable, not a true parameter */
49214117Sjamie#define PF_APPEND	0x02	/* Append to existing parameter list */
50214117Sjamie#define PF_BAD		0x04	/* Unable to resolve parameter value */
51214117Sjamie#define PF_INTERNAL	0x08	/* Internal parameter, not passed to kernel */
52214117Sjamie#define PF_BOOL		0x10	/* Boolean parameter */
53214117Sjamie#define PF_INT		0x20	/* Integer parameter */
54214423Sjamie#define PF_CONV		0x40	/* Parameter duplicated in converted form */
55248854Sjamie#define PF_REV		0x80	/* Run commands in reverse order on stopping */
56285279Shrs#define	PF_IMMUTABLE	0x100	/* Immutable parameter */
57214117Sjamie
58214117Sjamie#define JF_START	0x0001	/* -c */
59214117Sjamie#define JF_SET		0x0002	/* -m */
60214117Sjamie#define JF_STOP		0x0004	/* -r */
61214117Sjamie#define JF_DEPEND	0x0008	/* Operation required by dependency */
62214117Sjamie#define JF_WILD		0x0010	/* Not specified on the command line */
63214117Sjamie#define JF_FAILED	0x0020	/* Operation failed */
64214649Sjamie#define JF_PARAMS	0x0040	/* Parameters checked and imported */
65214649Sjamie#define JF_RDTUN	0x0080	/* Create-only parameter check has been done */
66223189Sjamie#define JF_PERSIST	0x0100	/* Jail is temporarily persistent */
67223189Sjamie#define JF_TIMEOUT	0x0200	/* A command (or process kill) timed out */
68223189Sjamie#define JF_SLEEPQ	0x0400	/* Waiting on a command and/or timeout */
69302954Sjamie#define JF_FROM_RUNQ	0x0800	/* Has already been on the run queue */
70341790Seugen#define JF_SHOW		0x1000	/* -e Exhibit list of configured jails */
71214117Sjamie
72214117Sjamie#define JF_OP_MASK		(JF_START | JF_SET | JF_STOP)
73214117Sjamie#define JF_RESTART		(JF_START | JF_STOP)
74214117Sjamie#define JF_START_SET		(JF_START | JF_SET)
75214117Sjamie#define JF_SET_RESTART		(JF_SET | JF_STOP)
76214117Sjamie#define JF_START_SET_RESTART	(JF_START | JF_SET | JF_STOP)
77214117Sjamie#define JF_DO_STOP(js)		(((js) & (JF_SET | JF_STOP)) == JF_STOP)
78214117Sjamie
79214117Sjamieenum intparam {
80234988Sjamie	IP__NULL = 0,		/* Null command */
81234988Sjamie	IP_ALLOW_DYING,		/* Allow making changes to a dying jail */
82214117Sjamie	IP_COMMAND,		/* Command run inside jail at creation */
83214117Sjamie	IP_DEPEND,		/* Jail starts after (stops before) another */
84214117Sjamie	IP_EXEC_CLEAN,		/* Run commands in a clean environment */
85214117Sjamie	IP_EXEC_CONSOLELOG,	/* Redirect optput for commands run in jail */
86214117Sjamie	IP_EXEC_FIB,		/* Run jailed commands with this FIB */
87214117Sjamie	IP_EXEC_JAIL_USER,	/* Run jailed commands as this user */
88214117Sjamie	IP_EXEC_POSTSTART,	/* Commands run outside jail after creating */
89214117Sjamie	IP_EXEC_POSTSTOP,	/* Commands run outside jail after removing */
90214117Sjamie	IP_EXEC_PRESTART,	/* Commands run outside jail before creating */
91214117Sjamie	IP_EXEC_PRESTOP,	/* Commands run outside jail before removing */
92214117Sjamie	IP_EXEC_START,		/* Commands run inside jail on creation */
93214117Sjamie	IP_EXEC_STOP,		/* Commands run inside jail on removal */
94214117Sjamie	IP_EXEC_SYSTEM_JAIL_USER,/* Get jail_user from system passwd file */
95214117Sjamie	IP_EXEC_SYSTEM_USER,	/* Run non-jailed commands as this user */
96214117Sjamie	IP_EXEC_TIMEOUT,	/* Time to wait for a command to complete */
97223351Sjamie#if defined(INET) || defined(INET6)
98214117Sjamie	IP_INTERFACE,		/* Add IP addresses to this interface */
99214117Sjamie	IP_IP_HOSTNAME,		/* Get jail IP address(es) from hostname */
100223351Sjamie#endif
101214117Sjamie	IP_MOUNT,		/* Mount points in fstab(5) form */
102214117Sjamie	IP_MOUNT_DEVFS,		/* Mount /dev under prison root */
103256385Shrs	IP_MOUNT_FDESCFS,	/* Mount /dev/fd under prison root */
104278323Sjamie	IP_MOUNT_PROCFS,	/* Mount /proc under prison root */
105214423Sjamie	IP_MOUNT_FSTAB,		/* A standard fstab(5) file */
106214117Sjamie	IP_STOP_TIMEOUT,	/* Time to wait after sending SIGTERM */
107214423Sjamie	IP_VNET_INTERFACE,	/* Assign interface(s) to vnet jail */
108223351Sjamie#ifdef INET
109214117Sjamie	IP__IP4_IFADDR,		/* Copy of ip4.addr with interface/netmask */
110223351Sjamie#endif
111214117Sjamie#ifdef INET6
112214117Sjamie	IP__IP6_IFADDR,		/* Copy of ip6.addr with interface/prefixlen */
113214117Sjamie#endif
114214783Sjamie	IP__MOUNT_FROM_FSTAB,	/* Line from mount.fstab file */
115223189Sjamie	IP__OP,			/* Placeholder for requested operation */
116214423Sjamie	KP_ALLOW_CHFLAGS,
117214423Sjamie	KP_ALLOW_MOUNT,
118214423Sjamie	KP_ALLOW_RAW_SOCKETS,
119214423Sjamie	KP_ALLOW_SET_HOSTNAME,
120214423Sjamie	KP_ALLOW_SOCKET_AF,
121214423Sjamie	KP_ALLOW_SYSVIPC,
122232242Sjamie	KP_DEVFS_RULESET,
123214423Sjamie	KP_ENFORCE_STATFS,
124214423Sjamie	KP_HOST_HOSTNAME,
125223351Sjamie#ifdef INET
126214117Sjamie	KP_IP4_ADDR,
127223351Sjamie#endif
128214117Sjamie#ifdef INET6
129214117Sjamie	KP_IP6_ADDR,
130214117Sjamie#endif
131214117Sjamie	KP_JID,
132214117Sjamie	KP_NAME,
133214117Sjamie	KP_PATH,
134214117Sjamie	KP_PERSIST,
135214423Sjamie	KP_SECURELEVEL,
136214117Sjamie	KP_VNET,
137214117Sjamie	IP_NPARAM
138214117Sjamie};
139214117Sjamie
140214117SjamieSTAILQ_HEAD(cfvars, cfvar);
141214117Sjamie
142214117Sjamiestruct cfvar {
143214117Sjamie	STAILQ_ENTRY(cfvar)	tq;
144214117Sjamie	char			*name;
145214117Sjamie	size_t			pos;
146214117Sjamie};
147214117Sjamie
148223188SjamieTAILQ_HEAD(cfstrings, cfstring);
149214117Sjamie
150214117Sjamiestruct cfstring {
151223188Sjamie	TAILQ_ENTRY(cfstring)	tq;
152214117Sjamie	char			*s;
153214117Sjamie	size_t			len;
154214117Sjamie	struct cfvars		vars;
155214117Sjamie};
156214117Sjamie
157214117SjamieTAILQ_HEAD(cfparams, cfparam);
158214117Sjamie
159214117Sjamiestruct cfparam {
160214117Sjamie	TAILQ_ENTRY(cfparam)	tq;
161214117Sjamie	char			*name;
162214117Sjamie	struct cfstrings	val;
163214117Sjamie	unsigned		flags;
164214117Sjamie	int			gen;
165214117Sjamie};
166214117Sjamie
167214117SjamieTAILQ_HEAD(cfjails, cfjail);
168214117SjamieSTAILQ_HEAD(cfdepends, cfdepend);
169214117Sjamie
170214117Sjamiestruct cfjail {
171214117Sjamie	TAILQ_ENTRY(cfjail)	tq;
172214117Sjamie	char			*name;
173214117Sjamie	char			*comline;
174214117Sjamie	struct cfparams		params;
175214117Sjamie	struct cfdepends	dep[2];
176214117Sjamie	struct cfjails		*queue;
177214117Sjamie	struct cfparam		*intparams[IP_NPARAM];
178214117Sjamie	struct cfstring		*comstring;
179214117Sjamie	struct jailparam	*jp;
180214117Sjamie	struct timespec		timeout;
181223189Sjamie	const enum intparam	*comparam;
182214117Sjamie	unsigned		flags;
183214117Sjamie	int			jid;
184214117Sjamie	int			seq;
185214117Sjamie	int			pstatus;
186214117Sjamie	int			ndeps;
187214117Sjamie	int			njp;
188214117Sjamie	int			nprocs;
189214117Sjamie};
190214117Sjamie
191214117Sjamiestruct cfdepend {
192214117Sjamie	STAILQ_ENTRY(cfdepend)	tq[2];
193214117Sjamie	struct cfjail		*j[2];
194214117Sjamie	unsigned		flags;
195214117Sjamie};
196214117Sjamie
197214117Sjamieextern void *emalloc(size_t);
198214117Sjamieextern void *erealloc(void *, size_t);
199214117Sjamieextern char *estrdup(const char *);
200223263Sjamieextern int create_jail(struct cfjail *j);
201214117Sjamieextern void failed(struct cfjail *j);
202214117Sjamieextern void jail_note(const struct cfjail *j, const char *fmt, ...);
203214117Sjamieextern void jail_warnx(const struct cfjail *j, const char *fmt, ...);
204214117Sjamie
205223189Sjamieextern int next_command(struct cfjail *j);
206216367Sjamieextern int finish_command(struct cfjail *j);
207214117Sjamieextern struct cfjail *next_proc(int nonblock);
208214117Sjamie
209214117Sjamieextern void load_config(void);
210214117Sjamieextern struct cfjail *add_jail(void);
211214117Sjamieextern void add_param(struct cfjail *j, const struct cfparam *p,
212214423Sjamie    enum intparam ipnum, const char *value);
213214117Sjamieextern int bool_param(const struct cfparam *p);
214214117Sjamieextern int int_param(const struct cfparam *p, int *ip);
215214117Sjamieextern const char *string_param(const struct cfparam *p);
216214649Sjamieextern int check_intparams(struct cfjail *j);
217214117Sjamieextern int import_params(struct cfjail *j);
218214117Sjamieextern int equalopts(const char *opt1, const char *opt2);
219214117Sjamieextern int wild_jail_name(const char *wname);
220214117Sjamieextern int wild_jail_match(const char *jname, const char *wname);
221214117Sjamie
222214117Sjamieextern void dep_setup(int docf);
223214117Sjamieextern int dep_check(struct cfjail *j);
224214117Sjamieextern void dep_done(struct cfjail *j, unsigned flags);
225214117Sjamieextern void dep_reset(struct cfjail *j);
226214117Sjamieextern struct cfjail *next_jail(void);
227231238Sjamieextern int start_state(const char *target, int docf, unsigned state,
228231238Sjamie    int running);
229214117Sjamieextern void requeue(struct cfjail *j, struct cfjails *queue);
230302954Sjamieextern void requeue_head(struct cfjail *j, struct cfjails *queue);
231214117Sjamie
232214117Sjamieextern void yyerror(const char *);
233214117Sjamieextern int yylex(void);
234214117Sjamie
235214117Sjamieextern struct cfjails cfjails;
236214117Sjamieextern struct cfjails ready;
237216367Sjamieextern struct cfjails depend;
238214117Sjamieextern const char *cfname;
239236198Sjamieextern int iflag;
240223263Sjamieextern int note_remove;
241216367Sjamieextern int paralimit;
242214117Sjamieextern int verbose;
243