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: releng/10.3/usr.sbin/jail/command.c 294484 2016-01-21 04:37:16Z jamie $");
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>
50214117Sjamie
51214117Sjamie#include "jailp.h"
52214117Sjamie
53214117Sjamie#define DEFAULT_STOP_TIMEOUT	10
54214117Sjamie#define PHASH_SIZE		256
55214117Sjamie
56214117SjamieLIST_HEAD(phhead, phash);
57214117Sjamie
58214117Sjamiestruct phash {
59214117Sjamie	LIST_ENTRY(phash)	le;
60214117Sjamie	struct cfjail		*j;
61214117Sjamie	pid_t			pid;
62214117Sjamie};
63214117Sjamie
64216367Sjamieint paralimit = -1;
65216367Sjamie
66214117Sjamieextern char **environ;
67214117Sjamie
68223189Sjamiestatic int run_command(struct cfjail *j);
69246804Sjamiestatic int add_proc(struct cfjail *j, pid_t pid);
70214117Sjamiestatic void clear_procs(struct cfjail *j);
71214117Sjamiestatic struct cfjail *find_proc(pid_t pid);
72216367Sjamiestatic int term_procs(struct cfjail *j);
73223189Sjamiestatic int get_user_info(struct cfjail *j, const char *username,
74223189Sjamie    const struct passwd **pwdp, login_cap_t **lcapp);
75214797Sjamiestatic int check_path(struct cfjail *j, const char *pname, const char *path,
76214805Sjamie    int isfile, const char *umount_type);
77214117Sjamie
78214117Sjamiestatic struct cfjails sleeping = TAILQ_HEAD_INITIALIZER(sleeping);
79214117Sjamiestatic struct cfjails runnable = TAILQ_HEAD_INITIALIZER(runnable);
80223368Sjamiestatic struct cfstring dummystring = { .len = 1 };
81214117Sjamiestatic struct phhead phash[PHASH_SIZE];
82214117Sjamiestatic int kq;
83214117Sjamie
84214117Sjamie/*
85223189Sjamie * Run the next command associated with a jail.
86214117Sjamie */
87214117Sjamieint
88223189Sjamienext_command(struct cfjail *j)
89214117Sjamie{
90223189Sjamie	enum intparam comparam;
91248854Sjamie	int create_failed, stopping;
92223189Sjamie
93223442Sjamie	if (paralimit == 0) {
94223442Sjamie		requeue(j, &runnable);
95223442Sjamie		return 1;
96223442Sjamie	}
97223189Sjamie	create_failed = (j->flags & (JF_STOP | JF_FAILED)) == JF_FAILED;
98248854Sjamie	stopping = (j->flags & JF_STOP) != 0;
99223442Sjamie	comparam = *j->comparam;
100223442Sjamie	for (;;) {
101223189Sjamie		if (j->comstring == NULL) {
102223442Sjamie			j->comparam += create_failed ? -1 : 1;
103223442Sjamie			switch ((comparam = *j->comparam)) {
104234988Sjamie			case IP__NULL:
105223442Sjamie				return 0;
106223189Sjamie			case IP_MOUNT_DEVFS:
107223189Sjamie				if (!bool_param(j->intparams[IP_MOUNT_DEVFS]))
108223189Sjamie					continue;
109256387Shrs				j->comstring = &dummystring;
110256387Shrs				break;
111256387Shrs			case IP_MOUNT_FDESCFS:
112256387Shrs				if (!bool_param(j->intparams[IP_MOUNT_FDESCFS]))
113256387Shrs					continue;
114256387Shrs				j->comstring = &dummystring;
115278484Sjamie				break;
116278484Sjamie			case IP_MOUNT_PROCFS:
117278484Sjamie				if (!bool_param(j->intparams[IP_MOUNT_PROCFS]))
118278484Sjamie					continue;
119278484Sjamie				j->comstring = &dummystring;
120278484Sjamie				break;
121223263Sjamie			case IP__OP:
122223189Sjamie			case IP_STOP_TIMEOUT:
123223189Sjamie				j->comstring = &dummystring;
124223189Sjamie				break;
125223189Sjamie			default:
126223189Sjamie				if (j->intparams[comparam] == NULL)
127223189Sjamie					continue;
128248854Sjamie				j->comstring = create_failed || (stopping &&
129248854Sjamie				    (j->intparams[comparam]->flags & PF_REV))
130223189Sjamie				    ? TAILQ_LAST(&j->intparams[comparam]->val,
131223189Sjamie					cfstrings)
132223189Sjamie				    : TAILQ_FIRST(&j->intparams[comparam]->val);
133223189Sjamie			}
134223442Sjamie		} else {
135223442Sjamie			j->comstring = j->comstring == &dummystring ? NULL :
136248854Sjamie			    create_failed || (stopping &&
137248854Sjamie			    (j->intparams[comparam]->flags & PF_REV))
138223442Sjamie			    ? TAILQ_PREV(j->comstring, cfstrings, tq)
139223442Sjamie			    : TAILQ_NEXT(j->comstring, tq);
140223189Sjamie		}
141223442Sjamie		if (j->comstring == NULL || j->comstring->len == 0 ||
142223442Sjamie		    (create_failed && (comparam == IP_EXEC_PRESTART ||
143223442Sjamie		    comparam == IP_EXEC_START || comparam == IP_COMMAND ||
144223442Sjamie		    comparam == IP_EXEC_POSTSTART)))
145223442Sjamie			continue;
146223442Sjamie		switch (run_command(j)) {
147223442Sjamie		case -1:
148223442Sjamie			failed(j);
149223442Sjamie			/* FALLTHROUGH */
150223442Sjamie		case 1:
151223442Sjamie			return 1;
152223189Sjamie		}
153223189Sjamie	}
154223189Sjamie}
155223189Sjamie
156223189Sjamie/*
157223189Sjamie * Check command exit status
158223189Sjamie */
159223189Sjamieint
160223189Sjamiefinish_command(struct cfjail *j)
161223189Sjamie{
162223189Sjamie	int error;
163223189Sjamie
164223189Sjamie	if (!(j->flags & JF_SLEEPQ))
165223189Sjamie		return 0;
166223189Sjamie	j->flags &= ~JF_SLEEPQ;
167223827Sjamie	if (*j->comparam == IP_STOP_TIMEOUT)
168223827Sjamie	{
169223827Sjamie		j->flags &= ~JF_TIMEOUT;
170223827Sjamie		j->pstatus = 0;
171223827Sjamie		return 0;
172223189Sjamie	}
173223827Sjamie	paralimit++;
174223827Sjamie	if (!TAILQ_EMPTY(&runnable))
175223827Sjamie		requeue(TAILQ_FIRST(&runnable), &ready);
176223189Sjamie	error = 0;
177223189Sjamie	if (j->flags & JF_TIMEOUT) {
178223189Sjamie		j->flags &= ~JF_TIMEOUT;
179223189Sjamie		if (*j->comparam != IP_STOP_TIMEOUT) {
180223189Sjamie			jail_warnx(j, "%s: timed out", j->comline);
181223189Sjamie			failed(j);
182223189Sjamie			error = -1;
183223189Sjamie		} else if (verbose > 0)
184223189Sjamie			jail_note(j, "timed out\n");
185223189Sjamie	} else if (j->pstatus != 0) {
186223189Sjamie		if (WIFSIGNALED(j->pstatus))
187223189Sjamie			jail_warnx(j, "%s: exited on signal %d",
188223189Sjamie			    j->comline, WTERMSIG(j->pstatus));
189223189Sjamie		else
190223189Sjamie			jail_warnx(j, "%s: failed", j->comline);
191223189Sjamie		j->pstatus = 0;
192223189Sjamie		failed(j);
193223189Sjamie		error = -1;
194223189Sjamie	}
195223189Sjamie	free(j->comline);
196223189Sjamie	j->comline = NULL;
197223189Sjamie	return error;
198223189Sjamie}
199223189Sjamie
200223189Sjamie/*
201223442Sjamie * Check for finished processes or timeouts.
202223189Sjamie */
203223189Sjamiestruct cfjail *
204223189Sjamienext_proc(int nonblock)
205223189Sjamie{
206223189Sjamie	struct kevent ke;
207223189Sjamie	struct timespec ts;
208223189Sjamie	struct timespec *tsp;
209223189Sjamie	struct cfjail *j;
210223189Sjamie
211223189Sjamie	if (!TAILQ_EMPTY(&sleeping)) {
212223189Sjamie	again:
213223189Sjamie		tsp = NULL;
214223189Sjamie		if ((j = TAILQ_FIRST(&sleeping)) && j->timeout.tv_sec) {
215223189Sjamie			clock_gettime(CLOCK_REALTIME, &ts);
216223189Sjamie			ts.tv_sec = j->timeout.tv_sec - ts.tv_sec;
217223189Sjamie			ts.tv_nsec = j->timeout.tv_nsec - ts.tv_nsec;
218223189Sjamie			if (ts.tv_nsec < 0) {
219223189Sjamie				ts.tv_sec--;
220223189Sjamie				ts.tv_nsec += 1000000000;
221223189Sjamie			}
222223189Sjamie			if (ts.tv_sec < 0 ||
223223189Sjamie			    (ts.tv_sec == 0 && ts.tv_nsec == 0)) {
224223189Sjamie				j->flags |= JF_TIMEOUT;
225223189Sjamie				clear_procs(j);
226223189Sjamie				return j;
227223189Sjamie			}
228223189Sjamie			tsp = &ts;
229223189Sjamie		}
230223189Sjamie		if (nonblock) {
231223189Sjamie			ts.tv_sec = 0;
232223189Sjamie			ts.tv_nsec = 0;
233223189Sjamie			tsp = &ts;
234223189Sjamie		}
235223189Sjamie		switch (kevent(kq, NULL, 0, &ke, 1, tsp)) {
236223189Sjamie		case -1:
237223189Sjamie			if (errno != EINTR)
238223189Sjamie				err(1, "kevent");
239223189Sjamie			goto again;
240223189Sjamie		case 0:
241223189Sjamie			if (!nonblock) {
242223189Sjamie				j = TAILQ_FIRST(&sleeping);
243223189Sjamie				j->flags |= JF_TIMEOUT;
244223189Sjamie				clear_procs(j);
245223189Sjamie				return j;
246223189Sjamie			}
247223189Sjamie			break;
248223189Sjamie		case 1:
249223189Sjamie			(void)waitpid(ke.ident, NULL, WNOHANG);
250223189Sjamie			if ((j = find_proc(ke.ident))) {
251223189Sjamie				j->pstatus = ke.data;
252223189Sjamie				return j;
253223189Sjamie			}
254223189Sjamie			goto again;
255223189Sjamie		}
256223189Sjamie	}
257223189Sjamie	return NULL;
258223189Sjamie}
259223189Sjamie
260223189Sjamie/*
261223189Sjamie * Run a single command for a jail, possible inside the jail.
262223189Sjamie */
263236198Sjamiestatic int
264223189Sjamierun_command(struct cfjail *j)
265223189Sjamie{
266214117Sjamie	const struct passwd *pwd;
267223189Sjamie	const struct cfstring *comstring, *s;
268214117Sjamie	login_cap_t *lcap;
269214117Sjamie	char **argv;
270223351Sjamie	char *cs, *comcs, *devpath;
271214117Sjamie	const char *jidstr, *conslog, *path, *ruleset, *term, *username;
272223189Sjamie	enum intparam comparam;
273214117Sjamie	size_t comlen;
274214117Sjamie	pid_t pid;
275214117Sjamie	int argc, bg, clean, consfd, down, fib, i, injail, sjuser, timeout;
276223351Sjamie#if defined(INET) || defined(INET6)
277269805Ssmh	char *addr, *extrap, *p, *val;
278223351Sjamie#endif
279214117Sjamie
280214117Sjamie	static char *cleanenv;
281214117Sjamie
282223263Sjamie	/* Perform some operations that aren't actually commands */
283223263Sjamie	comparam = *j->comparam;
284223263Sjamie	down = j->flags & (JF_STOP | JF_FAILED);
285223263Sjamie	switch (comparam) {
286223263Sjamie	case IP_STOP_TIMEOUT:
287223263Sjamie		return term_procs(j);
288223263Sjamie
289223263Sjamie	case IP__OP:
290223263Sjamie		if (down) {
291231238Sjamie			if (jail_remove(j->jid) < 0 && errno == EPERM) {
292231238Sjamie				jail_warnx(j, "jail_remove: %s",
293231238Sjamie					   strerror(errno));
294231238Sjamie				return -1;
295231238Sjamie			}
296223827Sjamie			if (verbose > 0 || (verbose == 0 && (j->flags & JF_STOP
297223263Sjamie			    ? note_remove : j->name != NULL)))
298223263Sjamie			    jail_note(j, "removed\n");
299223263Sjamie			j->jid = -1;
300223263Sjamie			if (j->flags & JF_STOP)
301223263Sjamie				dep_done(j, DF_LIGHT);
302223263Sjamie			else
303223263Sjamie				j->flags &= ~JF_PERSIST;
304223263Sjamie		} else {
305223442Sjamie			if (create_jail(j) < 0)
306223263Sjamie				return -1;
307236198Sjamie			if (iflag)
308236198Sjamie				printf("%d\n", j->jid);
309223263Sjamie			if (verbose >= 0 && (j->name || verbose > 0))
310223263Sjamie				jail_note(j, "created\n");
311223263Sjamie			dep_done(j, DF_LIGHT);
312223263Sjamie		}
313223442Sjamie		return 0;
314223327Sjamie
315223327Sjamie	default: ;
316223263Sjamie	}
317214117Sjamie	/*
318214117Sjamie	 * Collect exec arguments.  Internal commands for network and
319216367Sjamie	 * mounting build their own argument lists.
320214117Sjamie	 */
321223189Sjamie	comstring = j->comstring;
322223189Sjamie	bg = 0;
323216367Sjamie	switch (comparam) {
324223351Sjamie#ifdef INET
325216367Sjamie	case IP__IP4_IFADDR:
326269805Ssmh		argc = 0;
327269805Ssmh		val = alloca(strlen(comstring->s) + 1);
328269805Ssmh		strcpy(val, comstring->s);
329269805Ssmh		cs = val;
330269805Ssmh		extrap = NULL;
331269805Ssmh		while ((p = strchr(cs, ' ')) != NULL && strlen(p) > 1) {
332269805Ssmh			if (extrap == NULL) {
333269805Ssmh				*p = '\0';
334269805Ssmh				extrap = p + 1;
335269805Ssmh			}
336269805Ssmh			cs = p + 1;
337269805Ssmh			argc++;
338269805Ssmh		}
339269805Ssmh
340269805Ssmh		argv = alloca((8 + argc) * sizeof(char *));
341214117Sjamie		*(const char **)&argv[0] = _PATH_IFCONFIG;
342269805Ssmh		if ((cs = strchr(val, '|'))) {
343269805Ssmh			argv[1] = alloca(cs - val + 1);
344269805Ssmh			strlcpy(argv[1], val, cs - val + 1);
345214117Sjamie			addr = cs + 1;
346214117Sjamie		} else {
347214117Sjamie			*(const char **)&argv[1] =
348214117Sjamie			    string_param(j->intparams[IP_INTERFACE]);
349269805Ssmh			addr = val;
350214117Sjamie		}
351214117Sjamie		*(const char **)&argv[2] = "inet";
352214117Sjamie		if (!(cs = strchr(addr, '/'))) {
353214117Sjamie			argv[3] = addr;
354214117Sjamie			*(const char **)&argv[4] = "netmask";
355214117Sjamie			*(const char **)&argv[5] = "255.255.255.255";
356214117Sjamie			argc = 6;
357214117Sjamie		} else if (strchr(cs + 1, '.')) {
358214117Sjamie			argv[3] = alloca(cs - addr + 1);
359214117Sjamie			strlcpy(argv[3], addr, cs - addr + 1);
360214117Sjamie			*(const char **)&argv[4] = "netmask";
361214117Sjamie			*(const char **)&argv[5] = cs + 1;
362214117Sjamie			argc = 6;
363214117Sjamie		} else {
364214117Sjamie			argv[3] = addr;
365214117Sjamie			argc = 4;
366214117Sjamie		}
367269805Ssmh
368269805Ssmh		if (!down) {
369269805Ssmh			for (cs = strtok(extrap, " "); cs; cs = strtok(NULL, " ")) {
370269805Ssmh				size_t len = strlen(cs) + 1;
371269805Ssmh				argv[argc] = alloca(len);
372269805Ssmh				strlcpy(argv[argc++], cs, len);
373269805Ssmh			}
374269805Ssmh		}
375269805Ssmh
376214117Sjamie		*(const char **)&argv[argc] = down ? "-alias" : "alias";
377214117Sjamie		argv[argc + 1] = NULL;
378216367Sjamie		break;
379223351Sjamie#endif
380216367Sjamie
381214117Sjamie#ifdef INET6
382216367Sjamie	case IP__IP6_IFADDR:
383269805Ssmh		argc = 0;
384269805Ssmh		val = alloca(strlen(comstring->s) + 1);
385269805Ssmh		strcpy(val, comstring->s);
386269805Ssmh		cs = val;
387269805Ssmh		extrap = NULL;
388269805Ssmh		while ((p = strchr(cs, ' ')) != NULL && strlen(p) > 1) {
389269805Ssmh			if (extrap == NULL) {
390269805Ssmh				*p = '\0';
391269805Ssmh				extrap = p + 1;
392269805Ssmh			}
393269805Ssmh			cs = p + 1;
394269805Ssmh			argc++;
395269805Ssmh		}
396269805Ssmh
397269805Ssmh		argv = alloca((8 + argc) * sizeof(char *));
398214117Sjamie		*(const char **)&argv[0] = _PATH_IFCONFIG;
399269805Ssmh		if ((cs = strchr(val, '|'))) {
400269805Ssmh			argv[1] = alloca(cs - val + 1);
401269805Ssmh			strlcpy(argv[1], val, cs - val + 1);
402214117Sjamie			addr = cs + 1;
403214117Sjamie		} else {
404214117Sjamie			*(const char **)&argv[1] =
405214117Sjamie			    string_param(j->intparams[IP_INTERFACE]);
406269805Ssmh			addr = val;
407214117Sjamie		}
408214117Sjamie		*(const char **)&argv[2] = "inet6";
409214117Sjamie		argv[3] = addr;
410214117Sjamie		if (!(cs = strchr(addr, '/'))) {
411214117Sjamie			*(const char **)&argv[4] = "prefixlen";
412214117Sjamie			*(const char **)&argv[5] = "128";
413214117Sjamie			argc = 6;
414214117Sjamie		} else
415214117Sjamie			argc = 4;
416269805Ssmh
417269805Ssmh		if (!down) {
418269805Ssmh			for (cs = strtok(extrap, " "); cs; cs = strtok(NULL, " ")) {
419269805Ssmh				size_t len = strlen(cs) + 1;
420269805Ssmh				argv[argc] = alloca(len);
421269805Ssmh				strlcpy(argv[argc++], cs, len);
422269805Ssmh			}
423269805Ssmh		}
424269805Ssmh
425214117Sjamie		*(const char **)&argv[argc] = down ? "-alias" : "alias";
426214117Sjamie		argv[argc + 1] = NULL;
427216367Sjamie		break;
428214117Sjamie#endif
429216367Sjamie
430216367Sjamie	case IP_VNET_INTERFACE:
431214117Sjamie		argv = alloca(5 * sizeof(char *));
432214117Sjamie		*(const char **)&argv[0] = _PATH_IFCONFIG;
433214117Sjamie		argv[1] = comstring->s;
434214117Sjamie		*(const char **)&argv[2] = down ? "-vnet" : "vnet";
435214117Sjamie		jidstr = string_param(j->intparams[KP_JID]);
436214117Sjamie		*(const char **)&argv[3] =
437214117Sjamie			jidstr ? jidstr : string_param(j->intparams[KP_NAME]);
438214117Sjamie		argv[4] = NULL;
439216367Sjamie		break;
440216367Sjamie
441216367Sjamie	case IP_MOUNT:
442216367Sjamie	case IP__MOUNT_FROM_FSTAB:
443214117Sjamie		argv = alloca(8 * sizeof(char *));
444214117Sjamie		comcs = alloca(comstring->len + 1);
445214117Sjamie		strcpy(comcs, comstring->s);
446214117Sjamie		argc = 0;
447214117Sjamie		for (cs = strtok(comcs, " \t\f\v\r\n"); cs && argc < 4;
448214117Sjamie		     cs = strtok(NULL, " \t\f\v\r\n"))
449214117Sjamie			argv[argc++] = cs;
450214797Sjamie		if (argc == 0)
451223189Sjamie			return 0;
452214117Sjamie		if (argc < 3) {
453214783Sjamie			jail_warnx(j, "%s: %s: missing information",
454214783Sjamie			    j->intparams[comparam]->name, comstring->s);
455214117Sjamie			return -1;
456214117Sjamie		}
457214805Sjamie		if (check_path(j, j->intparams[comparam]->name, argv[1], 0,
458223442Sjamie		    down ? argv[2] : NULL) < 0)
459214797Sjamie			return -1;
460214117Sjamie		if (down) {
461214117Sjamie			argv[4] = NULL;
462214117Sjamie			argv[3] = argv[1];
463214117Sjamie			*(const char **)&argv[0] = "/sbin/umount";
464214117Sjamie		} else {
465214117Sjamie			if (argc == 4) {
466214117Sjamie				argv[7] = NULL;
467214117Sjamie				argv[6] = argv[1];
468214117Sjamie				argv[5] = argv[0];
469214117Sjamie				argv[4] = argv[3];
470214117Sjamie				*(const char **)&argv[3] = "-o";
471214117Sjamie			} else {
472214117Sjamie				argv[5] = NULL;
473214117Sjamie				argv[4] = argv[1];
474214117Sjamie				argv[3] = argv[0];
475214117Sjamie			}
476214117Sjamie			*(const char **)&argv[0] = _PATH_MOUNT;
477214117Sjamie		}
478214117Sjamie		*(const char **)&argv[1] = "-t";
479216367Sjamie		break;
480216367Sjamie
481216367Sjamie	case IP_MOUNT_DEVFS:
482232242Sjamie		argv = alloca(7 * sizeof(char *));
483214117Sjamie		path = string_param(j->intparams[KP_PATH]);
484214117Sjamie		if (path == NULL) {
485214117Sjamie			jail_warnx(j, "mount.devfs: no path");
486214117Sjamie			return -1;
487214117Sjamie		}
488214797Sjamie		devpath = alloca(strlen(path) + 5);
489214797Sjamie		sprintf(devpath, "%s/dev", path);
490214805Sjamie		if (check_path(j, "mount.devfs", devpath, 0,
491223442Sjamie		    down ? "devfs" : NULL) < 0)
492214797Sjamie			return -1;
493214117Sjamie		if (down) {
494214117Sjamie			*(const char **)&argv[0] = "/sbin/umount";
495214797Sjamie			argv[1] = devpath;
496214117Sjamie			argv[2] = NULL;
497214117Sjamie		} else {
498232242Sjamie			*(const char **)&argv[0] = _PATH_MOUNT;
499232242Sjamie			*(const char **)&argv[1] = "-t";
500232242Sjamie			*(const char **)&argv[2] = "devfs";
501232242Sjamie			ruleset = string_param(j->intparams[KP_DEVFS_RULESET]);
502232242Sjamie			if (!ruleset)
503232242Sjamie			    ruleset = "4";	/* devfsrules_jail */
504232242Sjamie			argv[3] = alloca(11 + strlen(ruleset));
505232242Sjamie			sprintf(argv[3], "-oruleset=%s", ruleset);
506232242Sjamie			*(const char **)&argv[4] = ".";
507232242Sjamie			argv[5] = devpath;
508232242Sjamie			argv[6] = NULL;
509214117Sjamie		}
510216367Sjamie		break;
511216367Sjamie
512256387Shrs	case IP_MOUNT_FDESCFS:
513256387Shrs		argv = alloca(7 * sizeof(char *));
514256387Shrs		path = string_param(j->intparams[KP_PATH]);
515256387Shrs		if (path == NULL) {
516256387Shrs			jail_warnx(j, "mount.fdescfs: no path");
517256387Shrs			return -1;
518256387Shrs		}
519256387Shrs		devpath = alloca(strlen(path) + 8);
520256387Shrs		sprintf(devpath, "%s/dev/fd", path);
521256387Shrs		if (check_path(j, "mount.fdescfs", devpath, 0,
522256387Shrs		    down ? "fdescfs" : NULL) < 0)
523256387Shrs			return -1;
524256387Shrs		if (down) {
525256387Shrs			*(const char **)&argv[0] = "/sbin/umount";
526256387Shrs			argv[1] = devpath;
527256387Shrs			argv[2] = NULL;
528256387Shrs		} else {
529256387Shrs			*(const char **)&argv[0] = _PATH_MOUNT;
530256387Shrs			*(const char **)&argv[1] = "-t";
531256387Shrs			*(const char **)&argv[2] = "fdescfs";
532256387Shrs			*(const char **)&argv[3] = ".";
533256387Shrs			argv[4] = devpath;
534256387Shrs			argv[5] = NULL;
535256387Shrs		}
536256387Shrs		break;
537256387Shrs
538278484Sjamie	case IP_MOUNT_PROCFS:
539278484Sjamie		argv = alloca(7 * sizeof(char *));
540278484Sjamie		path = string_param(j->intparams[KP_PATH]);
541278484Sjamie		if (path == NULL) {
542278484Sjamie			jail_warnx(j, "mount.procfs: no path");
543278484Sjamie			return -1;
544278484Sjamie		}
545278484Sjamie		devpath = alloca(strlen(path) + 6);
546278484Sjamie		sprintf(devpath, "%s/proc", path);
547278484Sjamie		if (check_path(j, "mount.procfs", devpath, 0,
548278484Sjamie		    down ? "procfs" : NULL) < 0)
549278484Sjamie			return -1;
550278484Sjamie		if (down) {
551278484Sjamie			*(const char **)&argv[0] = "/sbin/umount";
552278484Sjamie			argv[1] = devpath;
553278484Sjamie			argv[2] = NULL;
554278484Sjamie		} else {
555278484Sjamie			*(const char **)&argv[0] = _PATH_MOUNT;
556278484Sjamie			*(const char **)&argv[1] = "-t";
557278484Sjamie			*(const char **)&argv[2] = "procfs";
558278484Sjamie			*(const char **)&argv[3] = ".";
559278484Sjamie			argv[4] = devpath;
560278484Sjamie			argv[5] = NULL;
561278484Sjamie		}
562278484Sjamie		break;
563278484Sjamie
564216367Sjamie	case IP_COMMAND:
565216367Sjamie		if (j->name != NULL)
566216367Sjamie			goto default_command;
567214117Sjamie		argc = 0;
568223188Sjamie		TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq)
569214117Sjamie			argc++;
570214117Sjamie		argv = alloca((argc + 1) * sizeof(char *));
571214117Sjamie		argc = 0;
572223188Sjamie		TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq)
573214117Sjamie			argv[argc++] = s->s;
574214117Sjamie		argv[argc] = NULL;
575223368Sjamie		j->comstring = &dummystring;
576216367Sjamie		break;
577216367Sjamie
578216367Sjamie	default:
579216367Sjamie	default_command:
580216367Sjamie		if ((cs = strpbrk(comstring->s, "!\"$&'()*;<>?[\\]`{|}~")) &&
581216367Sjamie		    !(cs[0] == '&' && cs[1] == '\0')) {
582216367Sjamie			argv = alloca(4 * sizeof(char *));
583216367Sjamie			*(const char **)&argv[0] = _PATH_BSHELL;
584216367Sjamie			*(const char **)&argv[1] = "-c";
585216367Sjamie			argv[2] = comstring->s;
586216367Sjamie			argv[3] = NULL;
587216367Sjamie		} else {
588216367Sjamie			if (cs) {
589216367Sjamie				*cs = 0;
590216367Sjamie				bg = 1;
591216367Sjamie			}
592216367Sjamie			comcs = alloca(comstring->len + 1);
593216367Sjamie			strcpy(comcs, comstring->s);
594216367Sjamie			argc = 0;
595216367Sjamie			for (cs = strtok(comcs, " \t\f\v\r\n"); cs;
596216367Sjamie			     cs = strtok(NULL, " \t\f\v\r\n"))
597216367Sjamie				argc++;
598216367Sjamie			argv = alloca((argc + 1) * sizeof(char *));
599216367Sjamie			strcpy(comcs, comstring->s);
600216367Sjamie			argc = 0;
601216367Sjamie			for (cs = strtok(comcs, " \t\f\v\r\n"); cs;
602216367Sjamie			     cs = strtok(NULL, " \t\f\v\r\n"))
603216367Sjamie				argv[argc++] = cs;
604216367Sjamie			argv[argc] = NULL;
605214117Sjamie		}
606214117Sjamie	}
607223189Sjamie	if (argv[0] == NULL)
608223189Sjamie		return 0;
609216367Sjamie
610214117Sjamie	if (int_param(j->intparams[IP_EXEC_TIMEOUT], &timeout) &&
611214117Sjamie	    timeout != 0) {
612214117Sjamie		clock_gettime(CLOCK_REALTIME, &j->timeout);
613214117Sjamie		j->timeout.tv_sec += timeout;
614214117Sjamie	} else
615214117Sjamie		j->timeout.tv_sec = 0;
616214117Sjamie
617214117Sjamie	injail = comparam == IP_EXEC_START || comparam == IP_COMMAND ||
618214117Sjamie	    comparam == IP_EXEC_STOP;
619214117Sjamie	clean = bool_param(j->intparams[IP_EXEC_CLEAN]);
620214117Sjamie	username = string_param(j->intparams[injail
621214117Sjamie	    ? IP_EXEC_JAIL_USER : IP_EXEC_SYSTEM_USER]);
622214117Sjamie	sjuser = bool_param(j->intparams[IP_EXEC_SYSTEM_JAIL_USER]);
623214117Sjamie
624214117Sjamie	consfd = 0;
625214117Sjamie	if (injail &&
626214117Sjamie	    (conslog = string_param(j->intparams[IP_EXEC_CONSOLELOG]))) {
627223442Sjamie		if (check_path(j, "exec.consolelog", conslog, 1, NULL) < 0)
628214797Sjamie			return -1;
629214117Sjamie		consfd =
630214117Sjamie		    open(conslog, O_WRONLY | O_CREAT | O_APPEND, DEFFILEMODE);
631214117Sjamie		if (consfd < 0) {
632214117Sjamie			jail_warnx(j, "open %s: %s", conslog, strerror(errno));
633214117Sjamie			return -1;
634214117Sjamie		}
635214117Sjamie	}
636214117Sjamie
637214117Sjamie	comlen = 0;
638214117Sjamie	for (i = 0; argv[i]; i++)
639214117Sjamie		comlen += strlen(argv[i]) + 1;
640214117Sjamie	j->comline = cs = emalloc(comlen);
641214117Sjamie	for (i = 0; argv[i]; i++) {
642214117Sjamie		strcpy(cs, argv[i]);
643214117Sjamie		if (argv[i + 1]) {
644214117Sjamie			cs += strlen(argv[i]) + 1;
645214117Sjamie			cs[-1] = ' ';
646214117Sjamie		}
647214117Sjamie	}
648214117Sjamie	if (verbose > 0)
649214117Sjamie		jail_note(j, "run command%s%s%s: %s\n",
650214117Sjamie		    injail ? " in jail" : "", username ? " as " : "",
651214117Sjamie		    username ? username : "", j->comline);
652214117Sjamie
653214117Sjamie	pid = fork();
654214117Sjamie	if (pid < 0)
655214117Sjamie		err(1, "fork");
656214117Sjamie	if (pid > 0) {
657246804Sjamie		if (bg || !add_proc(j, pid)) {
658216367Sjamie			free(j->comline);
659216367Sjamie			j->comline = NULL;
660223442Sjamie			return 0;
661214117Sjamie		} else {
662216367Sjamie			paralimit--;
663223442Sjamie			return 1;
664214117Sjamie		}
665214117Sjamie	}
666214117Sjamie	if (bg)
667214117Sjamie		setsid();
668214117Sjamie
669223189Sjamie	/* Set up the environment and run the command */
670214117Sjamie	pwd = NULL;
671214117Sjamie	lcap = NULL;
672214117Sjamie	if ((clean || username) && injail && sjuser &&
673214117Sjamie	    get_user_info(j, username, &pwd, &lcap) < 0)
674214117Sjamie		exit(1);
675214117Sjamie	if (injail) {
676214117Sjamie		/* jail_attach won't chdir along with its chroot. */
677214117Sjamie		path = string_param(j->intparams[KP_PATH]);
678214117Sjamie		if (path && chdir(path) < 0) {
679214117Sjamie			jail_warnx(j, "chdir %s: %s", path, strerror(errno));
680214117Sjamie			exit(1);
681214117Sjamie		}
682214117Sjamie		if (int_param(j->intparams[IP_EXEC_FIB], &fib) &&
683214117Sjamie		    setfib(fib) < 0) {
684214117Sjamie			jail_warnx(j, "setfib: %s", strerror(errno));
685214117Sjamie			exit(1);
686214117Sjamie		}
687214117Sjamie		if (jail_attach(j->jid) < 0) {
688214117Sjamie			jail_warnx(j, "jail_attach: %s", strerror(errno));
689214117Sjamie			exit(1);
690214117Sjamie		}
691214117Sjamie	}
692214117Sjamie	if (clean || username) {
693214117Sjamie		if (!(injail && sjuser) &&
694214117Sjamie		    get_user_info(j, username, &pwd, &lcap) < 0)
695214117Sjamie			exit(1);
696214117Sjamie		if (clean) {
697214117Sjamie			term = getenv("TERM");
698214117Sjamie			environ = &cleanenv;
699214117Sjamie			setenv("PATH", "/bin:/usr/bin", 0);
700235949Sjamie			if (term != NULL)
701235949Sjamie				setenv("TERM", term, 1);
702214117Sjamie		}
703276277Sjamie		if (setgid(pwd->pw_gid) < 0) {
704276277Sjamie			jail_warnx(j, "setgid %d: %s", pwd->pw_gid,
705276277Sjamie			    strerror(errno));
706276277Sjamie			exit(1);
707276277Sjamie		}
708214117Sjamie		if (setusercontext(lcap, pwd, pwd->pw_uid, username
709214117Sjamie		    ? LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN
710214117Sjamie		    : LOGIN_SETPATH | LOGIN_SETENV) < 0) {
711214117Sjamie			jail_warnx(j, "setusercontext %s: %s", pwd->pw_name,
712214117Sjamie			    strerror(errno));
713214117Sjamie			exit(1);
714214117Sjamie		}
715214117Sjamie		login_close(lcap);
716214117Sjamie		setenv("USER", pwd->pw_name, 1);
717214117Sjamie		setenv("HOME", pwd->pw_dir, 1);
718214117Sjamie		setenv("SHELL",
719214117Sjamie		    *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1);
720214117Sjamie		if (clean && chdir(pwd->pw_dir) < 0) {
721214117Sjamie			jail_warnx(j, "chdir %s: %s",
722214117Sjamie			    pwd->pw_dir, strerror(errno));
723214117Sjamie			exit(1);
724214117Sjamie		}
725214117Sjamie		endpwent();
726214117Sjamie	}
727214117Sjamie
728214117Sjamie	if (consfd != 0 && (dup2(consfd, 1) < 0 || dup2(consfd, 2) < 0)) {
729214117Sjamie		jail_warnx(j, "exec.consolelog: %s", strerror(errno));
730214117Sjamie		exit(1);
731214117Sjamie	}
732214117Sjamie	closefrom(3);
733214117Sjamie	execvp(argv[0], argv);
734214117Sjamie	jail_warnx(j, "exec %s: %s", argv[0], strerror(errno));
735214117Sjamie	exit(1);
736214117Sjamie}
737214117Sjamie
738214117Sjamie/*
739214117Sjamie * Add a process to the hash, tied to a jail.
740214117Sjamie */
741246804Sjamiestatic int
742214117Sjamieadd_proc(struct cfjail *j, pid_t pid)
743214117Sjamie{
744214117Sjamie	struct kevent ke;
745214117Sjamie	struct cfjail *tj;
746214117Sjamie	struct phash *ph;
747214117Sjamie
748214117Sjamie	if (!kq && (kq = kqueue()) < 0)
749214117Sjamie		err(1, "kqueue");
750214117Sjamie	EV_SET(&ke, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
751246804Sjamie	if (kevent(kq, &ke, 1, NULL, 0, NULL) < 0) {
752246804Sjamie		if (errno == ESRCH)
753246804Sjamie			return 0;
754214117Sjamie		err(1, "kevent");
755246804Sjamie	}
756214117Sjamie	ph = emalloc(sizeof(struct phash));
757214117Sjamie	ph->j = j;
758214117Sjamie	ph->pid = pid;
759214117Sjamie	LIST_INSERT_HEAD(&phash[pid % PHASH_SIZE], ph, le);
760214117Sjamie	j->nprocs++;
761216367Sjamie	j->flags |= JF_SLEEPQ;
762216367Sjamie	if (j->timeout.tv_sec == 0)
763216367Sjamie		requeue(j, &sleeping);
764216367Sjamie	else {
765293290Sbdrewery		/* File the jail in the sleep queue according to its timeout. */
766214117Sjamie		TAILQ_REMOVE(j->queue, j, tq);
767214117Sjamie		TAILQ_FOREACH(tj, &sleeping, tq) {
768214117Sjamie			if (!tj->timeout.tv_sec ||
769214117Sjamie			    j->timeout.tv_sec < tj->timeout.tv_sec ||
770214117Sjamie			    (j->timeout.tv_sec == tj->timeout.tv_sec &&
771214117Sjamie			    j->timeout.tv_nsec <= tj->timeout.tv_nsec)) {
772214117Sjamie				TAILQ_INSERT_BEFORE(tj, j, tq);
773214117Sjamie				break;
774214117Sjamie			}
775214117Sjamie		}
776214117Sjamie		if (tj == NULL)
777214117Sjamie			TAILQ_INSERT_TAIL(&sleeping, j, tq);
778214117Sjamie		j->queue = &sleeping;
779216367Sjamie	}
780246804Sjamie	return 1;
781214117Sjamie}
782214117Sjamie
783214117Sjamie/*
784214117Sjamie * Remove any processes from the hash that correspond to a jail.
785214117Sjamie */
786214117Sjamiestatic void
787214117Sjamieclear_procs(struct cfjail *j)
788214117Sjamie{
789214117Sjamie	struct kevent ke;
790214117Sjamie	struct phash *ph, *tph;
791214117Sjamie	int i;
792214117Sjamie
793214117Sjamie	j->nprocs = 0;
794214117Sjamie	for (i = 0; i < PHASH_SIZE; i++)
795214117Sjamie		LIST_FOREACH_SAFE(ph, &phash[i], le, tph)
796214117Sjamie			if (ph->j == j) {
797214117Sjamie				EV_SET(&ke, ph->pid, EVFILT_PROC, EV_DELETE,
798214117Sjamie				    NOTE_EXIT, 0, NULL);
799214117Sjamie				(void)kevent(kq, &ke, 1, NULL, 0, NULL);
800214117Sjamie				LIST_REMOVE(ph, le);
801214117Sjamie				free(ph);
802214117Sjamie			}
803214117Sjamie}
804214117Sjamie
805214117Sjamie/*
806214117Sjamie * Find the jail that corresponds to an exited process.
807214117Sjamie */
808214117Sjamiestatic struct cfjail *
809214117Sjamiefind_proc(pid_t pid)
810214117Sjamie{
811214117Sjamie	struct cfjail *j;
812214117Sjamie	struct phash *ph;
813214117Sjamie
814214117Sjamie	LIST_FOREACH(ph, &phash[pid % PHASH_SIZE], le)
815214117Sjamie		if (ph->pid == pid) {
816214117Sjamie			j = ph->j;
817214117Sjamie			LIST_REMOVE(ph, le);
818214117Sjamie			free(ph);
819214117Sjamie			return --j->nprocs ? NULL : j;
820214117Sjamie		}
821214117Sjamie	return NULL;
822214117Sjamie}
823214117Sjamie
824214117Sjamie/*
825216367Sjamie * Send SIGTERM to all processes in a jail and wait for them to die.
826216367Sjamie */
827216367Sjamiestatic int
828216367Sjamieterm_procs(struct cfjail *j)
829216367Sjamie{
830216367Sjamie	struct kinfo_proc *ki;
831216367Sjamie	int i, noted, pcnt, timeout;
832216367Sjamie
833216367Sjamie	static kvm_t *kd;
834216367Sjamie
835216367Sjamie	if (!int_param(j->intparams[IP_STOP_TIMEOUT], &timeout))
836216367Sjamie		timeout = DEFAULT_STOP_TIMEOUT;
837216367Sjamie	else if (timeout == 0)
838216367Sjamie		return 0;
839216367Sjamie
840216367Sjamie	if (kd == NULL) {
841231238Sjamie		kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
842216367Sjamie		if (kd == NULL)
843231238Sjamie			return 0;
844216367Sjamie	}
845216367Sjamie
846216367Sjamie	ki = kvm_getprocs(kd, KERN_PROC_PROC, 0, &pcnt);
847216367Sjamie	if (ki == NULL)
848231238Sjamie		return 0;
849216367Sjamie	noted = 0;
850216367Sjamie	for (i = 0; i < pcnt; i++)
851216367Sjamie		if (ki[i].ki_jid == j->jid &&
852216367Sjamie		    kill(ki[i].ki_pid, SIGTERM) == 0) {
853246804Sjamie			(void)add_proc(j, ki[i].ki_pid);
854216367Sjamie			if (verbose > 0) {
855216367Sjamie				if (!noted) {
856216367Sjamie					noted = 1;
857216367Sjamie					jail_note(j, "sent SIGTERM to:");
858216367Sjamie				}
859216367Sjamie				printf(" %d", ki[i].ki_pid);
860216367Sjamie			}
861216367Sjamie		}
862216367Sjamie	if (noted)
863216367Sjamie		printf("\n");
864216367Sjamie	if (j->nprocs > 0) {
865216367Sjamie		clock_gettime(CLOCK_REALTIME, &j->timeout);
866216367Sjamie		j->timeout.tv_sec += timeout;
867216367Sjamie		return 1;
868216367Sjamie	}
869216367Sjamie	return 0;
870216367Sjamie}
871216367Sjamie
872216367Sjamie/*
873214117Sjamie * Look up a user in the passwd and login.conf files.
874214117Sjamie */
875214117Sjamiestatic int
876214117Sjamieget_user_info(struct cfjail *j, const char *username,
877214117Sjamie    const struct passwd **pwdp, login_cap_t **lcapp)
878214117Sjamie{
879214117Sjamie	const struct passwd *pwd;
880214117Sjamie
881294484Sjamie	errno = 0;
882214117Sjamie	*pwdp = pwd = username ? getpwnam(username) : getpwuid(getuid());
883214117Sjamie	if (pwd == NULL) {
884214117Sjamie		if (errno)
885214117Sjamie			jail_warnx(j, "getpwnam%s%s: %s", username ? " " : "",
886214117Sjamie			    username ? username : "", strerror(errno));
887214117Sjamie		else if (username)
888214117Sjamie			jail_warnx(j, "%s: no such user", username);
889214117Sjamie		else
890214117Sjamie			jail_warnx(j, "unknown uid %d", getuid());
891214117Sjamie		return -1;
892214117Sjamie	}
893214117Sjamie	*lcapp = login_getpwclass(pwd);
894214117Sjamie	if (*lcapp == NULL) {
895214117Sjamie		jail_warnx(j, "getpwclass %s: %s", pwd->pw_name,
896214117Sjamie		    strerror(errno));
897214117Sjamie		return -1;
898214117Sjamie	}
899214117Sjamie	/* Set the groups while the group file is still available */
900214117Sjamie	if (initgroups(pwd->pw_name, pwd->pw_gid) < 0) {
901214117Sjamie		jail_warnx(j, "initgroups %s: %s", pwd->pw_name,
902214117Sjamie		    strerror(errno));
903214117Sjamie		return -1;
904214117Sjamie	}
905214117Sjamie	return 0;
906214117Sjamie}
907214797Sjamie
908214797Sjamie/*
909214797Sjamie * Make sure a mount or consolelog path is a valid absolute pathname
910214797Sjamie * with no symlinks.
911214797Sjamie */
912214797Sjamiestatic int
913214805Sjamiecheck_path(struct cfjail *j, const char *pname, const char *path, int isfile,
914214805Sjamie    const char *umount_type)
915214797Sjamie{
916214805Sjamie	struct stat st, mpst;
917214805Sjamie	struct statfs stfs;
918214797Sjamie	char *tpath, *p;
919214797Sjamie	const char *jailpath;
920214797Sjamie	size_t jplen;
921214797Sjamie
922214797Sjamie	if (path[0] != '/') {
923214797Sjamie		jail_warnx(j, "%s: %s: not an absolute pathname",
924214797Sjamie		    pname, path);
925214797Sjamie		return -1;
926214797Sjamie	}
927214797Sjamie	/*
928214797Sjamie	 * Only check for symlinks in components below the jail's path,
929214797Sjamie	 * since that's where the security risk lies.
930214797Sjamie	 */
931214797Sjamie	jailpath = string_param(j->intparams[KP_PATH]);
932214797Sjamie	if (jailpath == NULL)
933214797Sjamie		jailpath = "";
934214797Sjamie	jplen = strlen(jailpath);
935214805Sjamie	if (!strncmp(path, jailpath, jplen) && path[jplen] == '/') {
936214805Sjamie		tpath = alloca(strlen(path) + 1);
937214805Sjamie		strcpy(tpath, path);
938214805Sjamie		for (p = tpath + jplen; p != NULL; ) {
939214805Sjamie			p = strchr(p + 1, '/');
940214805Sjamie			if (p)
941214805Sjamie				*p = '\0';
942214805Sjamie			if (lstat(tpath, &st) < 0) {
943214805Sjamie				if (errno == ENOENT && isfile && !p)
944214805Sjamie					break;
945214805Sjamie				jail_warnx(j, "%s: %s: %s", pname, tpath,
946214805Sjamie				    strerror(errno));
947214805Sjamie				return -1;
948214805Sjamie			}
949214805Sjamie			if (S_ISLNK(st.st_mode)) {
950214805Sjamie				jail_warnx(j, "%s: %s is a symbolic link",
951214805Sjamie				    pname, tpath);
952214805Sjamie				return -1;
953214805Sjamie			}
954214805Sjamie			if (p)
955214805Sjamie				*p = '/';
956214805Sjamie		}
957214805Sjamie	}
958214805Sjamie	if (umount_type != NULL) {
959214805Sjamie		if (stat(path, &st) < 0 || statfs(path, &stfs) < 0) {
960214805Sjamie			jail_warnx(j, "%s: %s: %s", pname, path,
961214797Sjamie			    strerror(errno));
962214797Sjamie			return -1;
963214797Sjamie		}
964214805Sjamie		if (stat(stfs.f_mntonname, &mpst) < 0) {
965214805Sjamie			jail_warnx(j, "%s: %s: %s", pname, stfs.f_mntonname,
966214805Sjamie			    strerror(errno));
967214797Sjamie			return -1;
968214797Sjamie		}
969214805Sjamie		if (st.st_ino != mpst.st_ino) {
970214805Sjamie			jail_warnx(j, "%s: %s: not a mount point",
971214805Sjamie			    pname, path);
972214805Sjamie			return -1;
973214805Sjamie		}
974214805Sjamie		if (strcmp(stfs.f_fstypename, umount_type)) {
975214805Sjamie			jail_warnx(j, "%s: %s: not a %s mount",
976214805Sjamie			    pname, path, umount_type);
977214805Sjamie			return -1;
978214805Sjamie		}
979214797Sjamie	}
980214797Sjamie	return 0;
981214797Sjamie}
982