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
27214117Sjamie#include <sys/cdefs.h>
28214117Sjamie__FBSDID("$FreeBSD$");
29214117Sjamie
30214117Sjamie#include <sys/types.h>
31214117Sjamie#include <sys/event.h>
32214805Sjamie#include <sys/mount.h>
33214117Sjamie#include <sys/stat.h>
34214117Sjamie#include <sys/sysctl.h>
35214117Sjamie#include <sys/user.h>
36214117Sjamie#include <sys/wait.h>
37214117Sjamie
38214117Sjamie#include <err.h>
39214117Sjamie#include <errno.h>
40214117Sjamie#include <fcntl.h>
41214117Sjamie#include <kvm.h>
42214117Sjamie#include <login_cap.h>
43214117Sjamie#include <paths.h>
44214117Sjamie#include <pwd.h>
45214117Sjamie#include <signal.h>
46214117Sjamie#include <stdio.h>
47214117Sjamie#include <stdlib.h>
48214117Sjamie#include <string.h>
49214117Sjamie#include <unistd.h>
50298729Sjamie#include <vis.h>
51214117Sjamie
52214117Sjamie#include "jailp.h"
53214117Sjamie
54214117Sjamie#define DEFAULT_STOP_TIMEOUT	10
55214117Sjamie#define PHASH_SIZE		256
56214117Sjamie
57214117SjamieLIST_HEAD(phhead, phash);
58214117Sjamie
59214117Sjamiestruct phash {
60214117Sjamie	LIST_ENTRY(phash)	le;
61214117Sjamie	struct cfjail		*j;
62214117Sjamie	pid_t			pid;
63214117Sjamie};
64214117Sjamie
65216367Sjamieint paralimit = -1;
66216367Sjamie
67214117Sjamieextern char **environ;
68214117Sjamie
69223189Sjamiestatic int run_command(struct cfjail *j);
70246804Sjamiestatic int add_proc(struct cfjail *j, pid_t pid);
71214117Sjamiestatic void clear_procs(struct cfjail *j);
72214117Sjamiestatic struct cfjail *find_proc(pid_t pid);
73216367Sjamiestatic int term_procs(struct cfjail *j);
74223189Sjamiestatic int get_user_info(struct cfjail *j, const char *username,
75223189Sjamie    const struct passwd **pwdp, login_cap_t **lcapp);
76214797Sjamiestatic int check_path(struct cfjail *j, const char *pname, const char *path,
77214805Sjamie    int isfile, const char *umount_type);
78214117Sjamie
79214117Sjamiestatic struct cfjails sleeping = TAILQ_HEAD_INITIALIZER(sleeping);
80214117Sjamiestatic struct cfjails runnable = TAILQ_HEAD_INITIALIZER(runnable);
81223368Sjamiestatic struct cfstring dummystring = { .len = 1 };
82214117Sjamiestatic struct phhead phash[PHASH_SIZE];
83214117Sjamiestatic int kq;
84214117Sjamie
85214117Sjamie/*
86223189Sjamie * Run the next command associated with a jail.
87214117Sjamie */
88214117Sjamieint
89223189Sjamienext_command(struct cfjail *j)
90214117Sjamie{
91223189Sjamie	enum intparam comparam;
92248854Sjamie	int create_failed, stopping;
93223189Sjamie
94223442Sjamie	if (paralimit == 0) {
95302958Sjamie		if (j->flags & JF_FROM_RUNQ)
96302958Sjamie			requeue_head(j, &runnable);
97302958Sjamie		else
98302958Sjamie			requeue(j, &runnable);
99223442Sjamie		return 1;
100223442Sjamie	}
101302958Sjamie	j->flags &= ~JF_FROM_RUNQ;
102223189Sjamie	create_failed = (j->flags & (JF_STOP | JF_FAILED)) == JF_FAILED;
103248854Sjamie	stopping = (j->flags & JF_STOP) != 0;
104223442Sjamie	comparam = *j->comparam;
105223442Sjamie	for (;;) {
106223189Sjamie		if (j->comstring == NULL) {
107223442Sjamie			j->comparam += create_failed ? -1 : 1;
108223442Sjamie			switch ((comparam = *j->comparam)) {
109234988Sjamie			case IP__NULL:
110223442Sjamie				return 0;
111223189Sjamie			case IP_MOUNT_DEVFS:
112223189Sjamie				if (!bool_param(j->intparams[IP_MOUNT_DEVFS]))
113223189Sjamie					continue;
114256387Shrs				j->comstring = &dummystring;
115256387Shrs				break;
116256387Shrs			case IP_MOUNT_FDESCFS:
117256387Shrs				if (!bool_param(j->intparams[IP_MOUNT_FDESCFS]))
118256387Shrs					continue;
119256387Shrs				j->comstring = &dummystring;
120278484Sjamie				break;
121278484Sjamie			case IP_MOUNT_PROCFS:
122278484Sjamie				if (!bool_param(j->intparams[IP_MOUNT_PROCFS]))
123278484Sjamie					continue;
124278484Sjamie				j->comstring = &dummystring;
125278484Sjamie				break;
126223263Sjamie			case IP__OP:
127223189Sjamie			case IP_STOP_TIMEOUT:
128223189Sjamie				j->comstring = &dummystring;
129223189Sjamie				break;
130223189Sjamie			default:
131223189Sjamie				if (j->intparams[comparam] == NULL)
132223189Sjamie					continue;
133248854Sjamie				j->comstring = create_failed || (stopping &&
134248854Sjamie				    (j->intparams[comparam]->flags & PF_REV))
135223189Sjamie				    ? TAILQ_LAST(&j->intparams[comparam]->val,
136223189Sjamie					cfstrings)
137223189Sjamie				    : TAILQ_FIRST(&j->intparams[comparam]->val);
138223189Sjamie			}
139223442Sjamie		} else {
140223442Sjamie			j->comstring = j->comstring == &dummystring ? NULL :
141248854Sjamie			    create_failed || (stopping &&
142248854Sjamie			    (j->intparams[comparam]->flags & PF_REV))
143223442Sjamie			    ? TAILQ_PREV(j->comstring, cfstrings, tq)
144223442Sjamie			    : TAILQ_NEXT(j->comstring, tq);
145223189Sjamie		}
146223442Sjamie		if (j->comstring == NULL || j->comstring->len == 0 ||
147223442Sjamie		    (create_failed && (comparam == IP_EXEC_PRESTART ||
148223442Sjamie		    comparam == IP_EXEC_START || comparam == IP_COMMAND ||
149223442Sjamie		    comparam == IP_EXEC_POSTSTART)))
150223442Sjamie			continue;
151223442Sjamie		switch (run_command(j)) {
152223442Sjamie		case -1:
153223442Sjamie			failed(j);
154223442Sjamie			/* FALLTHROUGH */
155223442Sjamie		case 1:
156223442Sjamie			return 1;
157223189Sjamie		}
158223189Sjamie	}
159223189Sjamie}
160223189Sjamie
161223189Sjamie/*
162223189Sjamie * Check command exit status
163223189Sjamie */
164223189Sjamieint
165223189Sjamiefinish_command(struct cfjail *j)
166223189Sjamie{
167302958Sjamie	struct cfjail *rj;
168223189Sjamie	int error;
169223189Sjamie
170223189Sjamie	if (!(j->flags & JF_SLEEPQ))
171223189Sjamie		return 0;
172223189Sjamie	j->flags &= ~JF_SLEEPQ;
173302958Sjamie	if (*j->comparam == IP_STOP_TIMEOUT) {
174223827Sjamie		j->flags &= ~JF_TIMEOUT;
175223827Sjamie		j->pstatus = 0;
176223827Sjamie		return 0;
177223189Sjamie	}
178223827Sjamie	paralimit++;
179302958Sjamie	if (!TAILQ_EMPTY(&runnable)) {
180302958Sjamie		rj = TAILQ_FIRST(&runnable);
181302958Sjamie		rj->flags |= JF_FROM_RUNQ;
182302958Sjamie		requeue(rj, &ready);
183302958Sjamie	}
184223189Sjamie	error = 0;
185223189Sjamie	if (j->flags & JF_TIMEOUT) {
186223189Sjamie		j->flags &= ~JF_TIMEOUT;
187223189Sjamie		if (*j->comparam != IP_STOP_TIMEOUT) {
188223189Sjamie			jail_warnx(j, "%s: timed out", j->comline);
189223189Sjamie			failed(j);
190223189Sjamie			error = -1;
191223189Sjamie		} else if (verbose > 0)
192223189Sjamie			jail_note(j, "timed out\n");
193223189Sjamie	} else if (j->pstatus != 0) {
194223189Sjamie		if (WIFSIGNALED(j->pstatus))
195223189Sjamie			jail_warnx(j, "%s: exited on signal %d",
196223189Sjamie			    j->comline, WTERMSIG(j->pstatus));
197223189Sjamie		else
198223189Sjamie			jail_warnx(j, "%s: failed", j->comline);
199223189Sjamie		j->pstatus = 0;
200223189Sjamie		failed(j);
201223189Sjamie		error = -1;
202223189Sjamie	}
203223189Sjamie	free(j->comline);
204223189Sjamie	j->comline = NULL;
205223189Sjamie	return error;
206223189Sjamie}
207223189Sjamie
208223189Sjamie/*
209223442Sjamie * Check for finished processes or timeouts.
210223189Sjamie */
211223189Sjamiestruct cfjail *
212223189Sjamienext_proc(int nonblock)
213223189Sjamie{
214223189Sjamie	struct kevent ke;
215223189Sjamie	struct timespec ts;
216223189Sjamie	struct timespec *tsp;
217223189Sjamie	struct cfjail *j;
218223189Sjamie
219223189Sjamie	if (!TAILQ_EMPTY(&sleeping)) {
220223189Sjamie	again:
221223189Sjamie		tsp = NULL;
222223189Sjamie		if ((j = TAILQ_FIRST(&sleeping)) && j->timeout.tv_sec) {
223223189Sjamie			clock_gettime(CLOCK_REALTIME, &ts);
224223189Sjamie			ts.tv_sec = j->timeout.tv_sec - ts.tv_sec;
225223189Sjamie			ts.tv_nsec = j->timeout.tv_nsec - ts.tv_nsec;
226223189Sjamie			if (ts.tv_nsec < 0) {
227223189Sjamie				ts.tv_sec--;
228223189Sjamie				ts.tv_nsec += 1000000000;
229223189Sjamie			}
230223189Sjamie			if (ts.tv_sec < 0 ||
231223189Sjamie			    (ts.tv_sec == 0 && ts.tv_nsec == 0)) {
232223189Sjamie				j->flags |= JF_TIMEOUT;
233223189Sjamie				clear_procs(j);
234223189Sjamie				return j;
235223189Sjamie			}
236223189Sjamie			tsp = &ts;
237223189Sjamie		}
238223189Sjamie		if (nonblock) {
239223189Sjamie			ts.tv_sec = 0;
240223189Sjamie			ts.tv_nsec = 0;
241223189Sjamie			tsp = &ts;
242223189Sjamie		}
243223189Sjamie		switch (kevent(kq, NULL, 0, &ke, 1, tsp)) {
244223189Sjamie		case -1:
245223189Sjamie			if (errno != EINTR)
246223189Sjamie				err(1, "kevent");
247223189Sjamie			goto again;
248223189Sjamie		case 0:
249223189Sjamie			if (!nonblock) {
250223189Sjamie				j = TAILQ_FIRST(&sleeping);
251223189Sjamie				j->flags |= JF_TIMEOUT;
252223189Sjamie				clear_procs(j);
253223189Sjamie				return j;
254223189Sjamie			}
255223189Sjamie			break;
256223189Sjamie		case 1:
257223189Sjamie			(void)waitpid(ke.ident, NULL, WNOHANG);
258223189Sjamie			if ((j = find_proc(ke.ident))) {
259223189Sjamie				j->pstatus = ke.data;
260223189Sjamie				return j;
261223189Sjamie			}
262223189Sjamie			goto again;
263223189Sjamie		}
264223189Sjamie	}
265223189Sjamie	return NULL;
266223189Sjamie}
267223189Sjamie
268223189Sjamie/*
269302958Sjamie * Run a single command for a jail, possibly inside the jail.
270223189Sjamie */
271236198Sjamiestatic int
272223189Sjamierun_command(struct cfjail *j)
273223189Sjamie{
274214117Sjamie	const struct passwd *pwd;
275223189Sjamie	const struct cfstring *comstring, *s;
276214117Sjamie	login_cap_t *lcap;
277214117Sjamie	char **argv;
278223351Sjamie	char *cs, *comcs, *devpath;
279214117Sjamie	const char *jidstr, *conslog, *path, *ruleset, *term, *username;
280223189Sjamie	enum intparam comparam;
281214117Sjamie	size_t comlen;
282214117Sjamie	pid_t pid;
283214117Sjamie	int argc, bg, clean, consfd, down, fib, i, injail, sjuser, timeout;
284223351Sjamie#if defined(INET) || defined(INET6)
285269805Ssmh	char *addr, *extrap, *p, *val;
286223351Sjamie#endif
287214117Sjamie
288214117Sjamie	static char *cleanenv;
289214117Sjamie
290223263Sjamie	/* Perform some operations that aren't actually commands */
291223263Sjamie	comparam = *j->comparam;
292223263Sjamie	down = j->flags & (JF_STOP | JF_FAILED);
293223263Sjamie	switch (comparam) {
294223263Sjamie	case IP_STOP_TIMEOUT:
295223263Sjamie		return term_procs(j);
296223263Sjamie
297223263Sjamie	case IP__OP:
298223263Sjamie		if (down) {
299231238Sjamie			if (jail_remove(j->jid) < 0 && errno == EPERM) {
300231238Sjamie				jail_warnx(j, "jail_remove: %s",
301231238Sjamie					   strerror(errno));
302231238Sjamie				return -1;
303231238Sjamie			}
304223827Sjamie			if (verbose > 0 || (verbose == 0 && (j->flags & JF_STOP
305223263Sjamie			    ? note_remove : j->name != NULL)))
306223263Sjamie			    jail_note(j, "removed\n");
307223263Sjamie			j->jid = -1;
308223263Sjamie			if (j->flags & JF_STOP)
309223263Sjamie				dep_done(j, DF_LIGHT);
310223263Sjamie			else
311223263Sjamie				j->flags &= ~JF_PERSIST;
312223263Sjamie		} else {
313223442Sjamie			if (create_jail(j) < 0)
314223263Sjamie				return -1;
315236198Sjamie			if (iflag)
316236198Sjamie				printf("%d\n", j->jid);
317223263Sjamie			if (verbose >= 0 && (j->name || verbose > 0))
318223263Sjamie				jail_note(j, "created\n");
319223263Sjamie			dep_done(j, DF_LIGHT);
320223263Sjamie		}
321223442Sjamie		return 0;
322223327Sjamie
323223327Sjamie	default: ;
324223263Sjamie	}
325214117Sjamie	/*
326214117Sjamie	 * Collect exec arguments.  Internal commands for network and
327216367Sjamie	 * mounting build their own argument lists.
328214117Sjamie	 */
329223189Sjamie	comstring = j->comstring;
330223189Sjamie	bg = 0;
331216367Sjamie	switch (comparam) {
332223351Sjamie#ifdef INET
333216367Sjamie	case IP__IP4_IFADDR:
334269805Ssmh		argc = 0;
335269805Ssmh		val = alloca(strlen(comstring->s) + 1);
336269805Ssmh		strcpy(val, comstring->s);
337269805Ssmh		cs = val;
338269805Ssmh		extrap = NULL;
339269805Ssmh		while ((p = strchr(cs, ' ')) != NULL && strlen(p) > 1) {
340269805Ssmh			if (extrap == NULL) {
341269805Ssmh				*p = '\0';
342269805Ssmh				extrap = p + 1;
343269805Ssmh			}
344269805Ssmh			cs = p + 1;
345269805Ssmh			argc++;
346269805Ssmh		}
347269805Ssmh
348269805Ssmh		argv = alloca((8 + argc) * sizeof(char *));
349214117Sjamie		*(const char **)&argv[0] = _PATH_IFCONFIG;
350269805Ssmh		if ((cs = strchr(val, '|'))) {
351269805Ssmh			argv[1] = alloca(cs - val + 1);
352269805Ssmh			strlcpy(argv[1], val, cs - val + 1);
353214117Sjamie			addr = cs + 1;
354214117Sjamie		} else {
355214117Sjamie			*(const char **)&argv[1] =
356214117Sjamie			    string_param(j->intparams[IP_INTERFACE]);
357269805Ssmh			addr = val;
358214117Sjamie		}
359214117Sjamie		*(const char **)&argv[2] = "inet";
360214117Sjamie		if (!(cs = strchr(addr, '/'))) {
361214117Sjamie			argv[3] = addr;
362214117Sjamie			*(const char **)&argv[4] = "netmask";
363214117Sjamie			*(const char **)&argv[5] = "255.255.255.255";
364214117Sjamie			argc = 6;
365214117Sjamie		} else if (strchr(cs + 1, '.')) {
366214117Sjamie			argv[3] = alloca(cs - addr + 1);
367214117Sjamie			strlcpy(argv[3], addr, cs - addr + 1);
368214117Sjamie			*(const char **)&argv[4] = "netmask";
369214117Sjamie			*(const char **)&argv[5] = cs + 1;
370214117Sjamie			argc = 6;
371214117Sjamie		} else {
372214117Sjamie			argv[3] = addr;
373214117Sjamie			argc = 4;
374214117Sjamie		}
375269805Ssmh
376269805Ssmh		if (!down) {
377269805Ssmh			for (cs = strtok(extrap, " "); cs; cs = strtok(NULL, " ")) {
378269805Ssmh				size_t len = strlen(cs) + 1;
379269805Ssmh				argv[argc] = alloca(len);
380269805Ssmh				strlcpy(argv[argc++], cs, len);
381269805Ssmh			}
382269805Ssmh		}
383269805Ssmh
384214117Sjamie		*(const char **)&argv[argc] = down ? "-alias" : "alias";
385214117Sjamie		argv[argc + 1] = NULL;
386216367Sjamie		break;
387223351Sjamie#endif
388216367Sjamie
389214117Sjamie#ifdef INET6
390216367Sjamie	case IP__IP6_IFADDR:
391269805Ssmh		argc = 0;
392269805Ssmh		val = alloca(strlen(comstring->s) + 1);
393269805Ssmh		strcpy(val, comstring->s);
394269805Ssmh		cs = val;
395269805Ssmh		extrap = NULL;
396269805Ssmh		while ((p = strchr(cs, ' ')) != NULL && strlen(p) > 1) {
397269805Ssmh			if (extrap == NULL) {
398269805Ssmh				*p = '\0';
399269805Ssmh				extrap = p + 1;
400269805Ssmh			}
401269805Ssmh			cs = p + 1;
402269805Ssmh			argc++;
403269805Ssmh		}
404269805Ssmh
405269805Ssmh		argv = alloca((8 + argc) * sizeof(char *));
406214117Sjamie		*(const char **)&argv[0] = _PATH_IFCONFIG;
407269805Ssmh		if ((cs = strchr(val, '|'))) {
408269805Ssmh			argv[1] = alloca(cs - val + 1);
409269805Ssmh			strlcpy(argv[1], val, cs - val + 1);
410214117Sjamie			addr = cs + 1;
411214117Sjamie		} else {
412214117Sjamie			*(const char **)&argv[1] =
413214117Sjamie			    string_param(j->intparams[IP_INTERFACE]);
414269805Ssmh			addr = val;
415214117Sjamie		}
416214117Sjamie		*(const char **)&argv[2] = "inet6";
417214117Sjamie		argv[3] = addr;
418214117Sjamie		if (!(cs = strchr(addr, '/'))) {
419214117Sjamie			*(const char **)&argv[4] = "prefixlen";
420214117Sjamie			*(const char **)&argv[5] = "128";
421214117Sjamie			argc = 6;
422214117Sjamie		} else
423214117Sjamie			argc = 4;
424269805Ssmh
425269805Ssmh		if (!down) {
426269805Ssmh			for (cs = strtok(extrap, " "); cs; cs = strtok(NULL, " ")) {
427269805Ssmh				size_t len = strlen(cs) + 1;
428269805Ssmh				argv[argc] = alloca(len);
429269805Ssmh				strlcpy(argv[argc++], cs, len);
430269805Ssmh			}
431269805Ssmh		}
432269805Ssmh
433214117Sjamie		*(const char **)&argv[argc] = down ? "-alias" : "alias";
434214117Sjamie		argv[argc + 1] = NULL;
435216367Sjamie		break;
436214117Sjamie#endif
437216367Sjamie
438216367Sjamie	case IP_VNET_INTERFACE:
439214117Sjamie		argv = alloca(5 * sizeof(char *));
440214117Sjamie		*(const char **)&argv[0] = _PATH_IFCONFIG;
441214117Sjamie		argv[1] = comstring->s;
442214117Sjamie		*(const char **)&argv[2] = down ? "-vnet" : "vnet";
443214117Sjamie		jidstr = string_param(j->intparams[KP_JID]);
444214117Sjamie		*(const char **)&argv[3] =
445214117Sjamie			jidstr ? jidstr : string_param(j->intparams[KP_NAME]);
446214117Sjamie		argv[4] = NULL;
447216367Sjamie		break;
448216367Sjamie
449216367Sjamie	case IP_MOUNT:
450216367Sjamie	case IP__MOUNT_FROM_FSTAB:
451214117Sjamie		argv = alloca(8 * sizeof(char *));
452214117Sjamie		comcs = alloca(comstring->len + 1);
453214117Sjamie		strcpy(comcs, comstring->s);
454214117Sjamie		argc = 0;
455214117Sjamie		for (cs = strtok(comcs, " \t\f\v\r\n"); cs && argc < 4;
456298729Sjamie		     cs = strtok(NULL, " \t\f\v\r\n")) {
457298729Sjamie			if (argc <= 1 && strunvis(cs, cs) < 0) {
458298729Sjamie				jail_warnx(j, "%s: %s: fstab parse error",
459298729Sjamie				    j->intparams[comparam]->name, comstring->s);
460298729Sjamie				return -1;
461298729Sjamie			}
462214117Sjamie			argv[argc++] = cs;
463298729Sjamie		}
464214797Sjamie		if (argc == 0)
465223189Sjamie			return 0;
466214117Sjamie		if (argc < 3) {
467214783Sjamie			jail_warnx(j, "%s: %s: missing information",
468214783Sjamie			    j->intparams[comparam]->name, comstring->s);
469214117Sjamie			return -1;
470214117Sjamie		}
471214805Sjamie		if (check_path(j, j->intparams[comparam]->name, argv[1], 0,
472223442Sjamie		    down ? argv[2] : NULL) < 0)
473214797Sjamie			return -1;
474214117Sjamie		if (down) {
475214117Sjamie			argv[4] = NULL;
476214117Sjamie			argv[3] = argv[1];
477214117Sjamie			*(const char **)&argv[0] = "/sbin/umount";
478214117Sjamie		} else {
479214117Sjamie			if (argc == 4) {
480214117Sjamie				argv[7] = NULL;
481214117Sjamie				argv[6] = argv[1];
482214117Sjamie				argv[5] = argv[0];
483214117Sjamie				argv[4] = argv[3];
484214117Sjamie				*(const char **)&argv[3] = "-o";
485214117Sjamie			} else {
486214117Sjamie				argv[5] = NULL;
487214117Sjamie				argv[4] = argv[1];
488214117Sjamie				argv[3] = argv[0];
489214117Sjamie			}
490214117Sjamie			*(const char **)&argv[0] = _PATH_MOUNT;
491214117Sjamie		}
492214117Sjamie		*(const char **)&argv[1] = "-t";
493216367Sjamie		break;
494216367Sjamie
495216367Sjamie	case IP_MOUNT_DEVFS:
496232242Sjamie		argv = alloca(7 * sizeof(char *));
497214117Sjamie		path = string_param(j->intparams[KP_PATH]);
498214117Sjamie		if (path == NULL) {
499214117Sjamie			jail_warnx(j, "mount.devfs: no path");
500214117Sjamie			return -1;
501214117Sjamie		}
502214797Sjamie		devpath = alloca(strlen(path) + 5);
503214797Sjamie		sprintf(devpath, "%s/dev", path);
504214805Sjamie		if (check_path(j, "mount.devfs", devpath, 0,
505223442Sjamie		    down ? "devfs" : NULL) < 0)
506214797Sjamie			return -1;
507214117Sjamie		if (down) {
508214117Sjamie			*(const char **)&argv[0] = "/sbin/umount";
509214797Sjamie			argv[1] = devpath;
510214117Sjamie			argv[2] = NULL;
511214117Sjamie		} else {
512232242Sjamie			*(const char **)&argv[0] = _PATH_MOUNT;
513232242Sjamie			*(const char **)&argv[1] = "-t";
514232242Sjamie			*(const char **)&argv[2] = "devfs";
515232242Sjamie			ruleset = string_param(j->intparams[KP_DEVFS_RULESET]);
516232242Sjamie			if (!ruleset)
517232242Sjamie			    ruleset = "4";	/* devfsrules_jail */
518232242Sjamie			argv[3] = alloca(11 + strlen(ruleset));
519232242Sjamie			sprintf(argv[3], "-oruleset=%s", ruleset);
520232242Sjamie			*(const char **)&argv[4] = ".";
521232242Sjamie			argv[5] = devpath;
522232242Sjamie			argv[6] = NULL;
523214117Sjamie		}
524216367Sjamie		break;
525216367Sjamie
526256387Shrs	case IP_MOUNT_FDESCFS:
527256387Shrs		argv = alloca(7 * sizeof(char *));
528256387Shrs		path = string_param(j->intparams[KP_PATH]);
529256387Shrs		if (path == NULL) {
530256387Shrs			jail_warnx(j, "mount.fdescfs: no path");
531256387Shrs			return -1;
532256387Shrs		}
533256387Shrs		devpath = alloca(strlen(path) + 8);
534256387Shrs		sprintf(devpath, "%s/dev/fd", path);
535256387Shrs		if (check_path(j, "mount.fdescfs", devpath, 0,
536256387Shrs		    down ? "fdescfs" : NULL) < 0)
537256387Shrs			return -1;
538256387Shrs		if (down) {
539256387Shrs			*(const char **)&argv[0] = "/sbin/umount";
540256387Shrs			argv[1] = devpath;
541256387Shrs			argv[2] = NULL;
542256387Shrs		} else {
543256387Shrs			*(const char **)&argv[0] = _PATH_MOUNT;
544256387Shrs			*(const char **)&argv[1] = "-t";
545256387Shrs			*(const char **)&argv[2] = "fdescfs";
546256387Shrs			*(const char **)&argv[3] = ".";
547256387Shrs			argv[4] = devpath;
548256387Shrs			argv[5] = NULL;
549256387Shrs		}
550256387Shrs		break;
551256387Shrs
552278484Sjamie	case IP_MOUNT_PROCFS:
553278484Sjamie		argv = alloca(7 * sizeof(char *));
554278484Sjamie		path = string_param(j->intparams[KP_PATH]);
555278484Sjamie		if (path == NULL) {
556278484Sjamie			jail_warnx(j, "mount.procfs: no path");
557278484Sjamie			return -1;
558278484Sjamie		}
559278484Sjamie		devpath = alloca(strlen(path) + 6);
560278484Sjamie		sprintf(devpath, "%s/proc", path);
561278484Sjamie		if (check_path(j, "mount.procfs", devpath, 0,
562278484Sjamie		    down ? "procfs" : NULL) < 0)
563278484Sjamie			return -1;
564278484Sjamie		if (down) {
565278484Sjamie			*(const char **)&argv[0] = "/sbin/umount";
566278484Sjamie			argv[1] = devpath;
567278484Sjamie			argv[2] = NULL;
568278484Sjamie		} else {
569278484Sjamie			*(const char **)&argv[0] = _PATH_MOUNT;
570278484Sjamie			*(const char **)&argv[1] = "-t";
571278484Sjamie			*(const char **)&argv[2] = "procfs";
572278484Sjamie			*(const char **)&argv[3] = ".";
573278484Sjamie			argv[4] = devpath;
574278484Sjamie			argv[5] = NULL;
575278484Sjamie		}
576278484Sjamie		break;
577278484Sjamie
578216367Sjamie	case IP_COMMAND:
579216367Sjamie		if (j->name != NULL)
580216367Sjamie			goto default_command;
581214117Sjamie		argc = 0;
582223188Sjamie		TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq)
583214117Sjamie			argc++;
584214117Sjamie		argv = alloca((argc + 1) * sizeof(char *));
585214117Sjamie		argc = 0;
586223188Sjamie		TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq)
587214117Sjamie			argv[argc++] = s->s;
588214117Sjamie		argv[argc] = NULL;
589223368Sjamie		j->comstring = &dummystring;
590216367Sjamie		break;
591216367Sjamie
592216367Sjamie	default:
593216367Sjamie	default_command:
594216367Sjamie		if ((cs = strpbrk(comstring->s, "!\"$&'()*;<>?[\\]`{|}~")) &&
595216367Sjamie		    !(cs[0] == '&' && cs[1] == '\0')) {
596216367Sjamie			argv = alloca(4 * sizeof(char *));
597216367Sjamie			*(const char **)&argv[0] = _PATH_BSHELL;
598216367Sjamie			*(const char **)&argv[1] = "-c";
599216367Sjamie			argv[2] = comstring->s;
600216367Sjamie			argv[3] = NULL;
601216367Sjamie		} else {
602216367Sjamie			if (cs) {
603216367Sjamie				*cs = 0;
604216367Sjamie				bg = 1;
605216367Sjamie			}
606216367Sjamie			comcs = alloca(comstring->len + 1);
607216367Sjamie			strcpy(comcs, comstring->s);
608216367Sjamie			argc = 0;
609216367Sjamie			for (cs = strtok(comcs, " \t\f\v\r\n"); cs;
610216367Sjamie			     cs = strtok(NULL, " \t\f\v\r\n"))
611216367Sjamie				argc++;
612216367Sjamie			argv = alloca((argc + 1) * sizeof(char *));
613216367Sjamie			strcpy(comcs, comstring->s);
614216367Sjamie			argc = 0;
615216367Sjamie			for (cs = strtok(comcs, " \t\f\v\r\n"); cs;
616216367Sjamie			     cs = strtok(NULL, " \t\f\v\r\n"))
617216367Sjamie				argv[argc++] = cs;
618216367Sjamie			argv[argc] = NULL;
619214117Sjamie		}
620214117Sjamie	}
621223189Sjamie	if (argv[0] == NULL)
622223189Sjamie		return 0;
623216367Sjamie
624214117Sjamie	if (int_param(j->intparams[IP_EXEC_TIMEOUT], &timeout) &&
625214117Sjamie	    timeout != 0) {
626214117Sjamie		clock_gettime(CLOCK_REALTIME, &j->timeout);
627214117Sjamie		j->timeout.tv_sec += timeout;
628214117Sjamie	} else
629214117Sjamie		j->timeout.tv_sec = 0;
630214117Sjamie
631214117Sjamie	injail = comparam == IP_EXEC_START || comparam == IP_COMMAND ||
632214117Sjamie	    comparam == IP_EXEC_STOP;
633214117Sjamie	clean = bool_param(j->intparams[IP_EXEC_CLEAN]);
634214117Sjamie	username = string_param(j->intparams[injail
635214117Sjamie	    ? IP_EXEC_JAIL_USER : IP_EXEC_SYSTEM_USER]);
636214117Sjamie	sjuser = bool_param(j->intparams[IP_EXEC_SYSTEM_JAIL_USER]);
637214117Sjamie
638214117Sjamie	consfd = 0;
639214117Sjamie	if (injail &&
640214117Sjamie	    (conslog = string_param(j->intparams[IP_EXEC_CONSOLELOG]))) {
641223442Sjamie		if (check_path(j, "exec.consolelog", conslog, 1, NULL) < 0)
642214797Sjamie			return -1;
643214117Sjamie		consfd =
644214117Sjamie		    open(conslog, O_WRONLY | O_CREAT | O_APPEND, DEFFILEMODE);
645214117Sjamie		if (consfd < 0) {
646214117Sjamie			jail_warnx(j, "open %s: %s", conslog, strerror(errno));
647214117Sjamie			return -1;
648214117Sjamie		}
649214117Sjamie	}
650214117Sjamie
651214117Sjamie	comlen = 0;
652214117Sjamie	for (i = 0; argv[i]; i++)
653214117Sjamie		comlen += strlen(argv[i]) + 1;
654214117Sjamie	j->comline = cs = emalloc(comlen);
655214117Sjamie	for (i = 0; argv[i]; i++) {
656214117Sjamie		strcpy(cs, argv[i]);
657214117Sjamie		if (argv[i + 1]) {
658214117Sjamie			cs += strlen(argv[i]) + 1;
659214117Sjamie			cs[-1] = ' ';
660214117Sjamie		}
661214117Sjamie	}
662214117Sjamie	if (verbose > 0)
663214117Sjamie		jail_note(j, "run command%s%s%s: %s\n",
664214117Sjamie		    injail ? " in jail" : "", username ? " as " : "",
665214117Sjamie		    username ? username : "", j->comline);
666214117Sjamie
667214117Sjamie	pid = fork();
668214117Sjamie	if (pid < 0)
669214117Sjamie		err(1, "fork");
670214117Sjamie	if (pid > 0) {
671246804Sjamie		if (bg || !add_proc(j, pid)) {
672216367Sjamie			free(j->comline);
673216367Sjamie			j->comline = NULL;
674223442Sjamie			return 0;
675214117Sjamie		} else {
676216367Sjamie			paralimit--;
677223442Sjamie			return 1;
678214117Sjamie		}
679214117Sjamie	}
680214117Sjamie	if (bg)
681214117Sjamie		setsid();
682214117Sjamie
683223189Sjamie	/* Set up the environment and run the command */
684214117Sjamie	pwd = NULL;
685214117Sjamie	lcap = NULL;
686214117Sjamie	if ((clean || username) && injail && sjuser &&
687214117Sjamie	    get_user_info(j, username, &pwd, &lcap) < 0)
688214117Sjamie		exit(1);
689214117Sjamie	if (injail) {
690214117Sjamie		/* jail_attach won't chdir along with its chroot. */
691214117Sjamie		path = string_param(j->intparams[KP_PATH]);
692214117Sjamie		if (path && chdir(path) < 0) {
693214117Sjamie			jail_warnx(j, "chdir %s: %s", path, strerror(errno));
694214117Sjamie			exit(1);
695214117Sjamie		}
696214117Sjamie		if (int_param(j->intparams[IP_EXEC_FIB], &fib) &&
697214117Sjamie		    setfib(fib) < 0) {
698214117Sjamie			jail_warnx(j, "setfib: %s", strerror(errno));
699214117Sjamie			exit(1);
700214117Sjamie		}
701214117Sjamie		if (jail_attach(j->jid) < 0) {
702214117Sjamie			jail_warnx(j, "jail_attach: %s", strerror(errno));
703214117Sjamie			exit(1);
704214117Sjamie		}
705214117Sjamie	}
706214117Sjamie	if (clean || username) {
707214117Sjamie		if (!(injail && sjuser) &&
708214117Sjamie		    get_user_info(j, username, &pwd, &lcap) < 0)
709214117Sjamie			exit(1);
710214117Sjamie		if (clean) {
711214117Sjamie			term = getenv("TERM");
712214117Sjamie			environ = &cleanenv;
713214117Sjamie			setenv("PATH", "/bin:/usr/bin", 0);
714235949Sjamie			if (term != NULL)
715235949Sjamie				setenv("TERM", term, 1);
716214117Sjamie		}
717276277Sjamie		if (setgid(pwd->pw_gid) < 0) {
718276277Sjamie			jail_warnx(j, "setgid %d: %s", pwd->pw_gid,
719276277Sjamie			    strerror(errno));
720276277Sjamie			exit(1);
721276277Sjamie		}
722214117Sjamie		if (setusercontext(lcap, pwd, pwd->pw_uid, username
723214117Sjamie		    ? LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN
724214117Sjamie		    : LOGIN_SETPATH | LOGIN_SETENV) < 0) {
725214117Sjamie			jail_warnx(j, "setusercontext %s: %s", pwd->pw_name,
726214117Sjamie			    strerror(errno));
727214117Sjamie			exit(1);
728214117Sjamie		}
729214117Sjamie		login_close(lcap);
730214117Sjamie		setenv("USER", pwd->pw_name, 1);
731214117Sjamie		setenv("HOME", pwd->pw_dir, 1);
732214117Sjamie		setenv("SHELL",
733214117Sjamie		    *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1);
734214117Sjamie		if (clean && chdir(pwd->pw_dir) < 0) {
735214117Sjamie			jail_warnx(j, "chdir %s: %s",
736214117Sjamie			    pwd->pw_dir, strerror(errno));
737214117Sjamie			exit(1);
738214117Sjamie		}
739214117Sjamie		endpwent();
740214117Sjamie	}
741214117Sjamie
742214117Sjamie	if (consfd != 0 && (dup2(consfd, 1) < 0 || dup2(consfd, 2) < 0)) {
743214117Sjamie		jail_warnx(j, "exec.consolelog: %s", strerror(errno));
744214117Sjamie		exit(1);
745214117Sjamie	}
746214117Sjamie	closefrom(3);
747214117Sjamie	execvp(argv[0], argv);
748214117Sjamie	jail_warnx(j, "exec %s: %s", argv[0], strerror(errno));
749214117Sjamie	exit(1);
750214117Sjamie}
751214117Sjamie
752214117Sjamie/*
753214117Sjamie * Add a process to the hash, tied to a jail.
754214117Sjamie */
755246804Sjamiestatic int
756214117Sjamieadd_proc(struct cfjail *j, pid_t pid)
757214117Sjamie{
758214117Sjamie	struct kevent ke;
759214117Sjamie	struct cfjail *tj;
760214117Sjamie	struct phash *ph;
761214117Sjamie
762214117Sjamie	if (!kq && (kq = kqueue()) < 0)
763214117Sjamie		err(1, "kqueue");
764214117Sjamie	EV_SET(&ke, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
765246804Sjamie	if (kevent(kq, &ke, 1, NULL, 0, NULL) < 0) {
766246804Sjamie		if (errno == ESRCH)
767246804Sjamie			return 0;
768214117Sjamie		err(1, "kevent");
769246804Sjamie	}
770214117Sjamie	ph = emalloc(sizeof(struct phash));
771214117Sjamie	ph->j = j;
772214117Sjamie	ph->pid = pid;
773214117Sjamie	LIST_INSERT_HEAD(&phash[pid % PHASH_SIZE], ph, le);
774214117Sjamie	j->nprocs++;
775216367Sjamie	j->flags |= JF_SLEEPQ;
776216367Sjamie	if (j->timeout.tv_sec == 0)
777216367Sjamie		requeue(j, &sleeping);
778216367Sjamie	else {
779293290Sbdrewery		/* File the jail in the sleep queue according to its timeout. */
780214117Sjamie		TAILQ_REMOVE(j->queue, j, tq);
781214117Sjamie		TAILQ_FOREACH(tj, &sleeping, tq) {
782214117Sjamie			if (!tj->timeout.tv_sec ||
783214117Sjamie			    j->timeout.tv_sec < tj->timeout.tv_sec ||
784214117Sjamie			    (j->timeout.tv_sec == tj->timeout.tv_sec &&
785214117Sjamie			    j->timeout.tv_nsec <= tj->timeout.tv_nsec)) {
786214117Sjamie				TAILQ_INSERT_BEFORE(tj, j, tq);
787214117Sjamie				break;
788214117Sjamie			}
789214117Sjamie		}
790214117Sjamie		if (tj == NULL)
791214117Sjamie			TAILQ_INSERT_TAIL(&sleeping, j, tq);
792214117Sjamie		j->queue = &sleeping;
793216367Sjamie	}
794246804Sjamie	return 1;
795214117Sjamie}
796214117Sjamie
797214117Sjamie/*
798214117Sjamie * Remove any processes from the hash that correspond to a jail.
799214117Sjamie */
800214117Sjamiestatic void
801214117Sjamieclear_procs(struct cfjail *j)
802214117Sjamie{
803214117Sjamie	struct kevent ke;
804214117Sjamie	struct phash *ph, *tph;
805214117Sjamie	int i;
806214117Sjamie
807214117Sjamie	j->nprocs = 0;
808214117Sjamie	for (i = 0; i < PHASH_SIZE; i++)
809214117Sjamie		LIST_FOREACH_SAFE(ph, &phash[i], le, tph)
810214117Sjamie			if (ph->j == j) {
811214117Sjamie				EV_SET(&ke, ph->pid, EVFILT_PROC, EV_DELETE,
812214117Sjamie				    NOTE_EXIT, 0, NULL);
813214117Sjamie				(void)kevent(kq, &ke, 1, NULL, 0, NULL);
814214117Sjamie				LIST_REMOVE(ph, le);
815214117Sjamie				free(ph);
816214117Sjamie			}
817214117Sjamie}
818214117Sjamie
819214117Sjamie/*
820214117Sjamie * Find the jail that corresponds to an exited process.
821214117Sjamie */
822214117Sjamiestatic struct cfjail *
823214117Sjamiefind_proc(pid_t pid)
824214117Sjamie{
825214117Sjamie	struct cfjail *j;
826214117Sjamie	struct phash *ph;
827214117Sjamie
828214117Sjamie	LIST_FOREACH(ph, &phash[pid % PHASH_SIZE], le)
829214117Sjamie		if (ph->pid == pid) {
830214117Sjamie			j = ph->j;
831214117Sjamie			LIST_REMOVE(ph, le);
832214117Sjamie			free(ph);
833214117Sjamie			return --j->nprocs ? NULL : j;
834214117Sjamie		}
835214117Sjamie	return NULL;
836214117Sjamie}
837214117Sjamie
838214117Sjamie/*
839216367Sjamie * Send SIGTERM to all processes in a jail and wait for them to die.
840216367Sjamie */
841216367Sjamiestatic int
842216367Sjamieterm_procs(struct cfjail *j)
843216367Sjamie{
844216367Sjamie	struct kinfo_proc *ki;
845216367Sjamie	int i, noted, pcnt, timeout;
846216367Sjamie
847216367Sjamie	static kvm_t *kd;
848216367Sjamie
849216367Sjamie	if (!int_param(j->intparams[IP_STOP_TIMEOUT], &timeout))
850216367Sjamie		timeout = DEFAULT_STOP_TIMEOUT;
851216367Sjamie	else if (timeout == 0)
852216367Sjamie		return 0;
853216367Sjamie
854216367Sjamie	if (kd == NULL) {
855231238Sjamie		kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
856216367Sjamie		if (kd == NULL)
857231238Sjamie			return 0;
858216367Sjamie	}
859216367Sjamie
860216367Sjamie	ki = kvm_getprocs(kd, KERN_PROC_PROC, 0, &pcnt);
861216367Sjamie	if (ki == NULL)
862231238Sjamie		return 0;
863216367Sjamie	noted = 0;
864216367Sjamie	for (i = 0; i < pcnt; i++)
865216367Sjamie		if (ki[i].ki_jid == j->jid &&
866216367Sjamie		    kill(ki[i].ki_pid, SIGTERM) == 0) {
867246804Sjamie			(void)add_proc(j, ki[i].ki_pid);
868216367Sjamie			if (verbose > 0) {
869216367Sjamie				if (!noted) {
870216367Sjamie					noted = 1;
871216367Sjamie					jail_note(j, "sent SIGTERM to:");
872216367Sjamie				}
873216367Sjamie				printf(" %d", ki[i].ki_pid);
874216367Sjamie			}
875216367Sjamie		}
876216367Sjamie	if (noted)
877216367Sjamie		printf("\n");
878216367Sjamie	if (j->nprocs > 0) {
879216367Sjamie		clock_gettime(CLOCK_REALTIME, &j->timeout);
880216367Sjamie		j->timeout.tv_sec += timeout;
881216367Sjamie		return 1;
882216367Sjamie	}
883216367Sjamie	return 0;
884216367Sjamie}
885216367Sjamie
886216367Sjamie/*
887214117Sjamie * Look up a user in the passwd and login.conf files.
888214117Sjamie */
889214117Sjamiestatic int
890214117Sjamieget_user_info(struct cfjail *j, const char *username,
891214117Sjamie    const struct passwd **pwdp, login_cap_t **lcapp)
892214117Sjamie{
893214117Sjamie	const struct passwd *pwd;
894214117Sjamie
895294484Sjamie	errno = 0;
896214117Sjamie	*pwdp = pwd = username ? getpwnam(username) : getpwuid(getuid());
897214117Sjamie	if (pwd == NULL) {
898214117Sjamie		if (errno)
899214117Sjamie			jail_warnx(j, "getpwnam%s%s: %s", username ? " " : "",
900214117Sjamie			    username ? username : "", strerror(errno));
901214117Sjamie		else if (username)
902214117Sjamie			jail_warnx(j, "%s: no such user", username);
903214117Sjamie		else
904214117Sjamie			jail_warnx(j, "unknown uid %d", getuid());
905214117Sjamie		return -1;
906214117Sjamie	}
907214117Sjamie	*lcapp = login_getpwclass(pwd);
908214117Sjamie	if (*lcapp == NULL) {
909214117Sjamie		jail_warnx(j, "getpwclass %s: %s", pwd->pw_name,
910214117Sjamie		    strerror(errno));
911214117Sjamie		return -1;
912214117Sjamie	}
913214117Sjamie	/* Set the groups while the group file is still available */
914214117Sjamie	if (initgroups(pwd->pw_name, pwd->pw_gid) < 0) {
915214117Sjamie		jail_warnx(j, "initgroups %s: %s", pwd->pw_name,
916214117Sjamie		    strerror(errno));
917214117Sjamie		return -1;
918214117Sjamie	}
919214117Sjamie	return 0;
920214117Sjamie}
921214797Sjamie
922214797Sjamie/*
923214797Sjamie * Make sure a mount or consolelog path is a valid absolute pathname
924214797Sjamie * with no symlinks.
925214797Sjamie */
926214797Sjamiestatic int
927214805Sjamiecheck_path(struct cfjail *j, const char *pname, const char *path, int isfile,
928214805Sjamie    const char *umount_type)
929214797Sjamie{
930214805Sjamie	struct stat st, mpst;
931214805Sjamie	struct statfs stfs;
932214797Sjamie	char *tpath, *p;
933214797Sjamie	const char *jailpath;
934214797Sjamie	size_t jplen;
935214797Sjamie
936214797Sjamie	if (path[0] != '/') {
937214797Sjamie		jail_warnx(j, "%s: %s: not an absolute pathname",
938214797Sjamie		    pname, path);
939214797Sjamie		return -1;
940214797Sjamie	}
941214797Sjamie	/*
942214797Sjamie	 * Only check for symlinks in components below the jail's path,
943214797Sjamie	 * since that's where the security risk lies.
944214797Sjamie	 */
945214797Sjamie	jailpath = string_param(j->intparams[KP_PATH]);
946214797Sjamie	if (jailpath == NULL)
947214797Sjamie		jailpath = "";
948214797Sjamie	jplen = strlen(jailpath);
949214805Sjamie	if (!strncmp(path, jailpath, jplen) && path[jplen] == '/') {
950214805Sjamie		tpath = alloca(strlen(path) + 1);
951214805Sjamie		strcpy(tpath, path);
952214805Sjamie		for (p = tpath + jplen; p != NULL; ) {
953214805Sjamie			p = strchr(p + 1, '/');
954214805Sjamie			if (p)
955214805Sjamie				*p = '\0';
956214805Sjamie			if (lstat(tpath, &st) < 0) {
957214805Sjamie				if (errno == ENOENT && isfile && !p)
958214805Sjamie					break;
959214805Sjamie				jail_warnx(j, "%s: %s: %s", pname, tpath,
960214805Sjamie				    strerror(errno));
961214805Sjamie				return -1;
962214805Sjamie			}
963214805Sjamie			if (S_ISLNK(st.st_mode)) {
964214805Sjamie				jail_warnx(j, "%s: %s is a symbolic link",
965214805Sjamie				    pname, tpath);
966214805Sjamie				return -1;
967214805Sjamie			}
968214805Sjamie			if (p)
969214805Sjamie				*p = '/';
970214805Sjamie		}
971214805Sjamie	}
972214805Sjamie	if (umount_type != NULL) {
973214805Sjamie		if (stat(path, &st) < 0 || statfs(path, &stfs) < 0) {
974214805Sjamie			jail_warnx(j, "%s: %s: %s", pname, path,
975214797Sjamie			    strerror(errno));
976214797Sjamie			return -1;
977214797Sjamie		}
978214805Sjamie		if (stat(stfs.f_mntonname, &mpst) < 0) {
979214805Sjamie			jail_warnx(j, "%s: %s: %s", pname, stfs.f_mntonname,
980214805Sjamie			    strerror(errno));
981214797Sjamie			return -1;
982214797Sjamie		}
983214805Sjamie		if (st.st_ino != mpst.st_ino) {
984214805Sjamie			jail_warnx(j, "%s: %s: not a mount point",
985214805Sjamie			    pname, path);
986214805Sjamie			return -1;
987214805Sjamie		}
988214805Sjamie		if (strcmp(stfs.f_fstypename, umount_type)) {
989214805Sjamie			jail_warnx(j, "%s: %s: not a %s mount",
990214805Sjamie			    pname, path, umount_type);
991214805Sjamie			return -1;
992214805Sjamie		}
993214797Sjamie	}
994214797Sjamie	return 0;
995214797Sjamie}
996