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