1214117Sjamie/*-
2223190Sjamie * Copyright (c) 2011 James Gritton.
3214117Sjamie * All rights reserved.
4214117Sjamie *
5214117Sjamie * Redistribution and use in source and binary forms, with or without
6214117Sjamie * modification, are permitted provided that the following conditions
7214117Sjamie * are met:
8214117Sjamie * 1. Redistributions of source code must retain the above copyright
9214117Sjamie *    notice, this list of conditions and the following disclaimer.
10214117Sjamie * 2. Redistributions in binary form must reproduce the above copyright
11214117Sjamie *    notice, this list of conditions and the following disclaimer in the
12214117Sjamie *    documentation and/or other materials provided with the distribution.
13214117Sjamie *
14214117Sjamie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15214117Sjamie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16214117Sjamie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17214117Sjamie * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18214117Sjamie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19214117Sjamie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20214117Sjamie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21214117Sjamie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22214117Sjamie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23214117Sjamie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24214117Sjamie * SUCH DAMAGE.
25214117Sjamie *
26214117Sjamie * $FreeBSD$
27214117Sjamie */
28214117Sjamie
29214117Sjamie#include <sys/param.h>
30214117Sjamie#include <sys/types.h>
31214117Sjamie#include <sys/jail.h>
32214117Sjamie#include <sys/queue.h>
33214117Sjamie#include <sys/time.h>
34214117Sjamie
35214117Sjamie#include <jail.h>
36214117Sjamie
37214117Sjamie#define CONF_FILE	"/etc/jail.conf"
38214117Sjamie
39214117Sjamie#define DEP_FROM	0
40214117Sjamie#define DEP_TO		1
41214117Sjamie
42214117Sjamie#define DF_SEEN		0x01	/* Dependency has been followed */
43214117Sjamie#define DF_LIGHT	0x02	/* Implied dependency on jail existence only */
44293290Sbdrewery#define DF_NOFAIL	0x04	/* Don't propagate failed jails */
45214117Sjamie
46214117Sjamie#define PF_VAR		0x01	/* This is a variable, not a true parameter */
47214117Sjamie#define PF_APPEND	0x02	/* Append to existing parameter list */
48214117Sjamie#define PF_BAD		0x04	/* Unable to resolve parameter value */
49214117Sjamie#define PF_INTERNAL	0x08	/* Internal parameter, not passed to kernel */
50214117Sjamie#define PF_BOOL		0x10	/* Boolean parameter */
51214117Sjamie#define PF_INT		0x20	/* Integer parameter */
52214423Sjamie#define PF_CONV		0x40	/* Parameter duplicated in converted form */
53248854Sjamie#define PF_REV		0x80	/* Run commands in reverse order on stopping */
54285827Shrs#define	PF_IMMUTABLE	0x100	/* Immutable parameter */
55214117Sjamie
56214117Sjamie#define JF_START	0x0001	/* -c */
57214117Sjamie#define JF_SET		0x0002	/* -m */
58214117Sjamie#define JF_STOP		0x0004	/* -r */
59214117Sjamie#define JF_DEPEND	0x0008	/* Operation required by dependency */
60214117Sjamie#define JF_WILD		0x0010	/* Not specified on the command line */
61214117Sjamie#define JF_FAILED	0x0020	/* Operation failed */
62214649Sjamie#define JF_PARAMS	0x0040	/* Parameters checked and imported */
63214649Sjamie#define JF_RDTUN	0x0080	/* Create-only parameter check has been done */
64223189Sjamie#define JF_PERSIST	0x0100	/* Jail is temporarily persistent */
65223189Sjamie#define JF_TIMEOUT	0x0200	/* A command (or process kill) timed out */
66223189Sjamie#define JF_SLEEPQ	0x0400	/* Waiting on a command and/or timeout */
67302958Sjamie#define JF_FROM_RUNQ	0x0800	/* Has already been on the run queue */
68214117Sjamie
69214117Sjamie#define JF_OP_MASK		(JF_START | JF_SET | JF_STOP)
70214117Sjamie#define JF_RESTART		(JF_START | JF_STOP)
71214117Sjamie#define JF_START_SET		(JF_START | JF_SET)
72214117Sjamie#define JF_SET_RESTART		(JF_SET | JF_STOP)
73214117Sjamie#define JF_START_SET_RESTART	(JF_START | JF_SET | JF_STOP)
74214117Sjamie#define JF_DO_STOP(js)		(((js) & (JF_SET | JF_STOP)) == JF_STOP)
75214117Sjamie
76214117Sjamieenum intparam {
77234988Sjamie	IP__NULL = 0,		/* Null command */
78234988Sjamie	IP_ALLOW_DYING,		/* Allow making changes to a dying jail */
79214117Sjamie	IP_COMMAND,		/* Command run inside jail at creation */
80214117Sjamie	IP_DEPEND,		/* Jail starts after (stops before) another */
81214117Sjamie	IP_EXEC_CLEAN,		/* Run commands in a clean environment */
82214117Sjamie	IP_EXEC_CONSOLELOG,	/* Redirect optput for commands run in jail */
83214117Sjamie	IP_EXEC_FIB,		/* Run jailed commands with this FIB */
84214117Sjamie	IP_EXEC_JAIL_USER,	/* Run jailed commands as this user */
85214117Sjamie	IP_EXEC_POSTSTART,	/* Commands run outside jail after creating */
86214117Sjamie	IP_EXEC_POSTSTOP,	/* Commands run outside jail after removing */
87214117Sjamie	IP_EXEC_PRESTART,	/* Commands run outside jail before creating */
88214117Sjamie	IP_EXEC_PRESTOP,	/* Commands run outside jail before removing */
89214117Sjamie	IP_EXEC_START,		/* Commands run inside jail on creation */
90214117Sjamie	IP_EXEC_STOP,		/* Commands run inside jail on removal */
91214117Sjamie	IP_EXEC_SYSTEM_JAIL_USER,/* Get jail_user from system passwd file */
92214117Sjamie	IP_EXEC_SYSTEM_USER,	/* Run non-jailed commands as this user */
93214117Sjamie	IP_EXEC_TIMEOUT,	/* Time to wait for a command to complete */
94223351Sjamie#if defined(INET) || defined(INET6)
95214117Sjamie	IP_INTERFACE,		/* Add IP addresses to this interface */
96214117Sjamie	IP_IP_HOSTNAME,		/* Get jail IP address(es) from hostname */
97223351Sjamie#endif
98214117Sjamie	IP_MOUNT,		/* Mount points in fstab(5) form */
99214117Sjamie	IP_MOUNT_DEVFS,		/* Mount /dev under prison root */
100256387Shrs	IP_MOUNT_FDESCFS,	/* Mount /dev/fd under prison root */
101278484Sjamie	IP_MOUNT_PROCFS,	/* Mount /proc under prison root */
102214423Sjamie	IP_MOUNT_FSTAB,		/* A standard fstab(5) file */
103214117Sjamie	IP_STOP_TIMEOUT,	/* Time to wait after sending SIGTERM */
104214423Sjamie	IP_VNET_INTERFACE,	/* Assign interface(s) to vnet jail */
105223351Sjamie#ifdef INET
106214117Sjamie	IP__IP4_IFADDR,		/* Copy of ip4.addr with interface/netmask */
107223351Sjamie#endif
108214117Sjamie#ifdef INET6
109214117Sjamie	IP__IP6_IFADDR,		/* Copy of ip6.addr with interface/prefixlen */
110214117Sjamie#endif
111214783Sjamie	IP__MOUNT_FROM_FSTAB,	/* Line from mount.fstab file */
112223189Sjamie	IP__OP,			/* Placeholder for requested operation */
113214423Sjamie	KP_ALLOW_CHFLAGS,
114214423Sjamie	KP_ALLOW_MOUNT,
115214423Sjamie	KP_ALLOW_RAW_SOCKETS,
116214423Sjamie	KP_ALLOW_SET_HOSTNAME,
117214423Sjamie	KP_ALLOW_SOCKET_AF,
118214423Sjamie	KP_ALLOW_SYSVIPC,
119232242Sjamie	KP_DEVFS_RULESET,
120214423Sjamie	KP_ENFORCE_STATFS,
121214423Sjamie	KP_HOST_HOSTNAME,
122223351Sjamie#ifdef INET
123214117Sjamie	KP_IP4_ADDR,
124223351Sjamie#endif
125214117Sjamie#ifdef INET6
126214117Sjamie	KP_IP6_ADDR,
127214117Sjamie#endif
128214117Sjamie	KP_JID,
129214117Sjamie	KP_NAME,
130214117Sjamie	KP_PATH,
131214117Sjamie	KP_PERSIST,
132214423Sjamie	KP_SECURELEVEL,
133214117Sjamie	KP_VNET,
134214117Sjamie	IP_NPARAM
135214117Sjamie};
136214117Sjamie
137214117SjamieSTAILQ_HEAD(cfvars, cfvar);
138214117Sjamie
139214117Sjamiestruct cfvar {
140214117Sjamie	STAILQ_ENTRY(cfvar)	tq;
141214117Sjamie	char			*name;
142214117Sjamie	size_t			pos;
143214117Sjamie};
144214117Sjamie
145223188SjamieTAILQ_HEAD(cfstrings, cfstring);
146214117Sjamie
147214117Sjamiestruct cfstring {
148223188Sjamie	TAILQ_ENTRY(cfstring)	tq;
149214117Sjamie	char			*s;
150214117Sjamie	size_t			len;
151214117Sjamie	struct cfvars		vars;
152214117Sjamie};
153214117Sjamie
154214117SjamieTAILQ_HEAD(cfparams, cfparam);
155214117Sjamie
156214117Sjamiestruct cfparam {
157214117Sjamie	TAILQ_ENTRY(cfparam)	tq;
158214117Sjamie	char			*name;
159214117Sjamie	struct cfstrings	val;
160214117Sjamie	unsigned		flags;
161214117Sjamie	int			gen;
162214117Sjamie};
163214117Sjamie
164214117SjamieTAILQ_HEAD(cfjails, cfjail);
165214117SjamieSTAILQ_HEAD(cfdepends, cfdepend);
166214117Sjamie
167214117Sjamiestruct cfjail {
168214117Sjamie	TAILQ_ENTRY(cfjail)	tq;
169214117Sjamie	char			*name;
170214117Sjamie	char			*comline;
171214117Sjamie	struct cfparams		params;
172214117Sjamie	struct cfdepends	dep[2];
173214117Sjamie	struct cfjails		*queue;
174214117Sjamie	struct cfparam		*intparams[IP_NPARAM];
175214117Sjamie	struct cfstring		*comstring;
176214117Sjamie	struct jailparam	*jp;
177214117Sjamie	struct timespec		timeout;
178223189Sjamie	const enum intparam	*comparam;
179214117Sjamie	unsigned		flags;
180214117Sjamie	int			jid;
181214117Sjamie	int			seq;
182214117Sjamie	int			pstatus;
183214117Sjamie	int			ndeps;
184214117Sjamie	int			njp;
185214117Sjamie	int			nprocs;
186214117Sjamie};
187214117Sjamie
188214117Sjamiestruct cfdepend {
189214117Sjamie	STAILQ_ENTRY(cfdepend)	tq[2];
190214117Sjamie	struct cfjail		*j[2];
191214117Sjamie	unsigned		flags;
192214117Sjamie};
193214117Sjamie
194214117Sjamieextern void *emalloc(size_t);
195214117Sjamieextern void *erealloc(void *, size_t);
196214117Sjamieextern char *estrdup(const char *);
197223263Sjamieextern int create_jail(struct cfjail *j);
198214117Sjamieextern void failed(struct cfjail *j);
199214117Sjamieextern void jail_note(const struct cfjail *j, const char *fmt, ...);
200214117Sjamieextern void jail_warnx(const struct cfjail *j, const char *fmt, ...);
201214117Sjamie
202223189Sjamieextern int next_command(struct cfjail *j);
203216367Sjamieextern int finish_command(struct cfjail *j);
204214117Sjamieextern struct cfjail *next_proc(int nonblock);
205214117Sjamie
206214117Sjamieextern void load_config(void);
207214117Sjamieextern struct cfjail *add_jail(void);
208214117Sjamieextern void add_param(struct cfjail *j, const struct cfparam *p,
209214423Sjamie    enum intparam ipnum, const char *value);
210214117Sjamieextern int bool_param(const struct cfparam *p);
211214117Sjamieextern int int_param(const struct cfparam *p, int *ip);
212214117Sjamieextern const char *string_param(const struct cfparam *p);
213214649Sjamieextern int check_intparams(struct cfjail *j);
214214117Sjamieextern int import_params(struct cfjail *j);
215214117Sjamieextern int equalopts(const char *opt1, const char *opt2);
216214117Sjamieextern int wild_jail_name(const char *wname);
217214117Sjamieextern int wild_jail_match(const char *jname, const char *wname);
218214117Sjamie
219214117Sjamieextern void dep_setup(int docf);
220214117Sjamieextern int dep_check(struct cfjail *j);
221214117Sjamieextern void dep_done(struct cfjail *j, unsigned flags);
222214117Sjamieextern void dep_reset(struct cfjail *j);
223214117Sjamieextern struct cfjail *next_jail(void);
224231238Sjamieextern int start_state(const char *target, int docf, unsigned state,
225231238Sjamie    int running);
226214117Sjamieextern void requeue(struct cfjail *j, struct cfjails *queue);
227302958Sjamieextern void requeue_head(struct cfjail *j, struct cfjails *queue);
228214117Sjamie
229214117Sjamieextern void yyerror(const char *);
230214117Sjamieextern int yylex(void);
231214117Sjamie
232214117Sjamieextern struct cfjails cfjails;
233214117Sjamieextern struct cfjails ready;
234216367Sjamieextern struct cfjails depend;
235214117Sjamieextern const char *cfname;
236236198Sjamieextern int iflag;
237223263Sjamieextern int note_remove;
238216367Sjamieextern int paralimit;
239214117Sjamieextern int verbose;
240