jailp.h revision 223188
1214117Sjamie/*-
2214117Sjamie * Copyright (c) 2010 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: projects/jailconf/usr.sbin/jail/jailp.h 223188 2011-06-17 16:06:13Z jamie $
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 */
44214117Sjamie#define DF_NOFAIL	0x04	/* Don't propigate 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 */
53214117Sjamie
54214117Sjamie#define JF_START	0x0001	/* -c */
55214117Sjamie#define JF_SET		0x0002	/* -m */
56214117Sjamie#define JF_STOP		0x0004	/* -r */
57214117Sjamie#define JF_DEPEND	0x0008	/* Operation required by dependency */
58214117Sjamie#define JF_WILD		0x0010	/* Not specified on the command line */
59214117Sjamie#define JF_FAILED	0x0020	/* Operation failed */
60214649Sjamie#define JF_PARAMS	0x0040	/* Parameters checked and imported */
61214649Sjamie#define JF_RDTUN	0x0080	/* Create-only parameter check has been done */
62214649Sjamie#define JF_IFUP		0x0100	/* IP addresses have been configured */
63214649Sjamie#define JF_MOUNTED	0x0200	/* Filesystems have been mounted */
64214649Sjamie#define JF_PERSIST	0x0400	/* Jail is temporarily persistent */
65214649Sjamie#define JF_TIMEOUT	0x0800	/* A command (or process kill) timed out */
66216367Sjamie#define JF_SLEEPQ	0x2000	/* Waiting on a command and/or timeout */
67214117Sjamie
68214117Sjamie#define JF_OP_MASK		(JF_START | JF_SET | JF_STOP)
69214117Sjamie#define JF_RESTART		(JF_START | JF_STOP)
70214117Sjamie#define JF_START_SET		(JF_START | JF_SET)
71214117Sjamie#define JF_SET_RESTART		(JF_SET | JF_STOP)
72214117Sjamie#define JF_START_SET_RESTART	(JF_START | JF_SET | JF_STOP)
73214117Sjamie#define JF_DO_STOP(js)		(((js) & (JF_SET | JF_STOP)) == JF_STOP)
74214117Sjamie
75214117Sjamieenum intparam {
76214117Sjamie	IP_ALLOW_DYING = 1,	/* Allow making changes to a dying jail */
77214117Sjamie	IP_COMMAND,		/* Command run inside jail at creation */
78214117Sjamie	IP_DEPEND,		/* Jail starts after (stops before) another */
79214117Sjamie	IP_EXEC_CLEAN,		/* Run commands in a clean environment */
80214117Sjamie	IP_EXEC_CONSOLELOG,	/* Redirect optput for commands run in jail */
81214117Sjamie	IP_EXEC_FIB,		/* Run jailed commands with this FIB */
82214117Sjamie	IP_EXEC_JAIL_USER,	/* Run jailed commands as this user */
83214117Sjamie	IP_EXEC_POSTSTART,	/* Commands run outside jail after creating */
84214117Sjamie	IP_EXEC_POSTSTOP,	/* Commands run outside jail after removing */
85214117Sjamie	IP_EXEC_PRESTART,	/* Commands run outside jail before creating */
86214117Sjamie	IP_EXEC_PRESTOP,	/* Commands run outside jail before removing */
87214117Sjamie	IP_EXEC_START,		/* Commands run inside jail on creation */
88214117Sjamie	IP_EXEC_STOP,		/* Commands run inside jail on removal */
89214117Sjamie	IP_EXEC_SYSTEM_JAIL_USER,/* Get jail_user from system passwd file */
90214117Sjamie	IP_EXEC_SYSTEM_USER,	/* Run non-jailed commands as this user */
91214117Sjamie	IP_EXEC_TIMEOUT,	/* Time to wait for a command to complete */
92214117Sjamie	IP_INTERFACE,		/* Add IP addresses to this interface */
93214117Sjamie	IP_IP_HOSTNAME,		/* Get jail IP address(es) from hostname */
94214117Sjamie	IP_MOUNT,		/* Mount points in fstab(5) form */
95214117Sjamie	IP_MOUNT_DEVFS,		/* Mount /dev under prison root */
96214117Sjamie	IP_MOUNT_DEVFS_RULESET,	/* Ruleset for the devfs mount */
97214423Sjamie	IP_MOUNT_FSTAB,		/* A standard fstab(5) file */
98214117Sjamie	IP_STOP_TIMEOUT,	/* Time to wait after sending SIGTERM */
99214423Sjamie	IP_VNET_INTERFACE,	/* Assign interface(s) to vnet jail */
100214117Sjamie	IP__IP4_IFADDR,		/* Copy of ip4.addr with interface/netmask */
101214117Sjamie#ifdef INET6
102214117Sjamie	IP__IP6_IFADDR,		/* Copy of ip6.addr with interface/prefixlen */
103214117Sjamie#endif
104214783Sjamie	IP__MOUNT_FROM_FSTAB,	/* Line from mount.fstab file */
105214423Sjamie	KP_ALLOW_CHFLAGS,
106214423Sjamie	KP_ALLOW_MOUNT,
107214423Sjamie	KP_ALLOW_RAW_SOCKETS,
108214423Sjamie	KP_ALLOW_SET_HOSTNAME,
109214423Sjamie	KP_ALLOW_SOCKET_AF,
110214423Sjamie	KP_ALLOW_SYSVIPC,
111214423Sjamie	KP_ENFORCE_STATFS,
112214423Sjamie	KP_HOST_HOSTNAME,
113214117Sjamie	KP_IP4_ADDR,
114214117Sjamie#ifdef INET6
115214117Sjamie	KP_IP6_ADDR,
116214117Sjamie#endif
117214117Sjamie	KP_JID,
118214117Sjamie	KP_NAME,
119214117Sjamie	KP_PATH,
120214117Sjamie	KP_PERSIST,
121214423Sjamie	KP_SECURELEVEL,
122214117Sjamie	KP_VNET,
123214117Sjamie	IP_NPARAM
124214117Sjamie};
125214117Sjamie
126214117SjamieSTAILQ_HEAD(cfvars, cfvar);
127214117Sjamie
128214117Sjamiestruct cfvar {
129214117Sjamie	STAILQ_ENTRY(cfvar)	tq;
130214117Sjamie	char			*name;
131214117Sjamie	size_t			pos;
132214117Sjamie};
133214117Sjamie
134223188SjamieTAILQ_HEAD(cfstrings, cfstring);
135214117Sjamie
136214117Sjamiestruct cfstring {
137223188Sjamie	TAILQ_ENTRY(cfstring)	tq;
138214117Sjamie	char			*s;
139214117Sjamie	size_t			len;
140214117Sjamie	struct cfvars		vars;
141214117Sjamie};
142214117Sjamie
143214117SjamieTAILQ_HEAD(cfparams, cfparam);
144214117Sjamie
145214117Sjamiestruct cfparam {
146214117Sjamie	TAILQ_ENTRY(cfparam)	tq;
147214117Sjamie	char			*name;
148214117Sjamie	struct cfstrings	val;
149214117Sjamie	unsigned		flags;
150214117Sjamie	int			gen;
151214117Sjamie};
152214117Sjamie
153214117SjamieTAILQ_HEAD(cfjails, cfjail);
154214117SjamieSTAILQ_HEAD(cfdepends, cfdepend);
155214117Sjamie
156214117Sjamiestruct cfjail {
157214117Sjamie	TAILQ_ENTRY(cfjail)	tq;
158214117Sjamie	char			*name;
159214117Sjamie	char			*comline;
160214117Sjamie	struct cfparams		params;
161214117Sjamie	struct cfdepends	dep[2];
162214117Sjamie	struct cfjails		*queue;
163214117Sjamie	struct cfparam		*intparams[IP_NPARAM];
164214117Sjamie	struct cfstring		*comstring;
165214117Sjamie	struct jailparam	*jp;
166214117Sjamie	struct timespec		timeout;
167214117Sjamie	enum intparam		comparam;
168214117Sjamie	unsigned		flags;
169214117Sjamie	int			jid;
170214117Sjamie	int			seq;
171214117Sjamie	int			pstatus;
172214117Sjamie	int			ndeps;
173214117Sjamie	int			njp;
174214117Sjamie	int			nprocs;
175214117Sjamie};
176214117Sjamie
177214117Sjamiestruct cfdepend {
178214117Sjamie	STAILQ_ENTRY(cfdepend)	tq[2];
179214117Sjamie	struct cfjail		*j[2];
180214117Sjamie	unsigned		flags;
181214117Sjamie};
182214117Sjamie
183214117Sjamieextern void *emalloc(size_t);
184214117Sjamieextern void *erealloc(void *, size_t);
185214117Sjamieextern char *estrdup(const char *);
186214117Sjamieextern void failed(struct cfjail *j);
187214117Sjamieextern void jail_note(const struct cfjail *j, const char *fmt, ...);
188214117Sjamieextern void jail_warnx(const struct cfjail *j, const char *fmt, ...);
189214117Sjamie
190216367Sjamieextern int run_command(struct cfjail *j, enum intparam comparam);
191216367Sjamieextern int finish_command(struct cfjail *j);
192214117Sjamieextern struct cfjail *next_proc(int nonblock);
193214117Sjamie
194214117Sjamieextern void load_config(void);
195214117Sjamieextern struct cfjail *add_jail(void);
196214117Sjamieextern void add_param(struct cfjail *j, const struct cfparam *p,
197214423Sjamie    enum intparam ipnum, const char *value);
198214117Sjamieextern int bool_param(const struct cfparam *p);
199214117Sjamieextern int int_param(const struct cfparam *p, int *ip);
200214117Sjamieextern const char *string_param(const struct cfparam *p);
201214649Sjamieextern int check_intparams(struct cfjail *j);
202214117Sjamieextern int import_params(struct cfjail *j);
203214117Sjamieextern int equalopts(const char *opt1, const char *opt2);
204214117Sjamieextern int wild_jail_name(const char *wname);
205214117Sjamieextern int wild_jail_match(const char *jname, const char *wname);
206214117Sjamie
207214117Sjamieextern void dep_setup(int docf);
208214117Sjamieextern int dep_check(struct cfjail *j);
209214117Sjamieextern void dep_done(struct cfjail *j, unsigned flags);
210214117Sjamieextern void dep_reset(struct cfjail *j);
211214117Sjamieextern struct cfjail *next_jail(void);
212214117Sjamieextern int start_state(const char *target, unsigned state, int running);
213214117Sjamieextern void requeue(struct cfjail *j, struct cfjails *queue);
214214117Sjamie
215214117Sjamieextern void yyerror(const char *);
216214117Sjamieextern int yylex(void);
217214117Sjamieextern int yyparse(void);
218214117Sjamie
219214117Sjamieextern struct cfjails cfjails;
220214117Sjamieextern struct cfjails ready;
221216367Sjamieextern struct cfjails depend;
222214117Sjamieextern const char *cfname;
223216367Sjamieextern int paralimit;
224214117Sjamieextern int verbose;
225