command.c revision 223368
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: projects/jailconf/usr.sbin/jail/command.c 223368 2011-06-21 19:13:48Z 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);
69214117Sjamiestatic void 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;
91223189Sjamie	int rval, create_failed;
92223189Sjamie
93223189Sjamie	rval = 0;
94223189Sjamie	create_failed = (j->flags & (JF_STOP | JF_FAILED)) == JF_FAILED;
95223263Sjamie	for (; (comparam = *j->comparam);
96223189Sjamie	     j->comparam += create_failed ? -1 : 1) {
97223189Sjamie		if (j->comstring == NULL) {
98223189Sjamie			switch (comparam) {
99223189Sjamie			case IP_MOUNT_DEVFS:
100223189Sjamie				if (!bool_param(j->intparams[IP_MOUNT_DEVFS]))
101223189Sjamie					continue;
102223189Sjamie				/* FALLTHROUGH */
103223263Sjamie			case IP__OP:
104223189Sjamie			case IP_STOP_TIMEOUT:
105223189Sjamie				j->comstring = &dummystring;
106223189Sjamie				break;
107223189Sjamie			default:
108223189Sjamie				if (j->intparams[comparam] == NULL)
109223189Sjamie					continue;
110223189Sjamie				j->comstring = create_failed
111223189Sjamie				    ? TAILQ_LAST(&j->intparams[comparam]->val,
112223189Sjamie					cfstrings)
113223189Sjamie				    : TAILQ_FIRST(&j->intparams[comparam]->val);
114223189Sjamie			}
115223189Sjamie		}
116223189Sjamie		for (; j->comstring != NULL;
117223368Sjamie		     j->comstring = j->comstring == &dummystring ? NULL :
118223368Sjamie			create_failed
119223189Sjamie			? TAILQ_PREV(j->comstring, cfstrings, tq)
120223189Sjamie			: TAILQ_NEXT(j->comstring, tq)) {
121223189Sjamie			if (rval != 0)
122223189Sjamie				return rval;
123223189Sjamie			if (j->comstring->len == 0 || (create_failed &&
124223189Sjamie			    (comparam == IP_EXEC_PRESTART || comparam ==
125223189Sjamie			    IP_EXEC_START || comparam == IP_COMMAND ||
126223189Sjamie			    comparam == IP_EXEC_POSTSTART)))
127223189Sjamie				continue;
128223189Sjamie			if (paralimit == 0) {
129223189Sjamie				requeue(j, &runnable);
130223189Sjamie				return 1;
131223189Sjamie			}
132223189Sjamie			rval = run_command(j);
133223189Sjamie			create_failed =
134223189Sjamie			    (j->flags & (JF_STOP | JF_FAILED)) == JF_FAILED;
135223189Sjamie		}
136223189Sjamie	}
137223189Sjamie	return rval;
138223189Sjamie}
139223189Sjamie
140223189Sjamie/*
141223189Sjamie * Check command exit status
142223189Sjamie */
143223189Sjamieint
144223189Sjamiefinish_command(struct cfjail *j)
145223189Sjamie{
146223189Sjamie	int error;
147223189Sjamie
148223189Sjamie	if (!(j->flags & JF_SLEEPQ))
149223189Sjamie		return 0;
150223189Sjamie	j->flags &= ~JF_SLEEPQ;
151223189Sjamie	if (*j->comparam != IP_STOP_TIMEOUT) {
152223189Sjamie		paralimit++;
153223189Sjamie		if (!TAILQ_EMPTY(&runnable))
154223189Sjamie			requeue(TAILQ_FIRST(&runnable), &ready);
155223189Sjamie	}
156223189Sjamie	error = 0;
157223189Sjamie	if (j->flags & JF_TIMEOUT) {
158223189Sjamie		j->flags &= ~JF_TIMEOUT;
159223189Sjamie		if (*j->comparam != IP_STOP_TIMEOUT) {
160223189Sjamie			jail_warnx(j, "%s: timed out", j->comline);
161223189Sjamie			failed(j);
162223189Sjamie			error = -1;
163223189Sjamie		} else if (verbose > 0)
164223189Sjamie			jail_note(j, "timed out\n");
165223189Sjamie	} else if (j->pstatus != 0) {
166223189Sjamie		if (WIFSIGNALED(j->pstatus))
167223189Sjamie			jail_warnx(j, "%s: exited on signal %d",
168223189Sjamie			    j->comline, WTERMSIG(j->pstatus));
169223189Sjamie		else
170223189Sjamie			jail_warnx(j, "%s: failed", j->comline);
171223189Sjamie		j->pstatus = 0;
172223189Sjamie		failed(j);
173223189Sjamie		error = -1;
174223189Sjamie	}
175223189Sjamie	free(j->comline);
176223189Sjamie	j->comline = NULL;
177223189Sjamie	return error;
178223189Sjamie}
179223189Sjamie
180223189Sjamie/*
181223189Sjamie * Check for finished processed or timeouts.
182223189Sjamie */
183223189Sjamiestruct cfjail *
184223189Sjamienext_proc(int nonblock)
185223189Sjamie{
186223189Sjamie	struct kevent ke;
187223189Sjamie	struct timespec ts;
188223189Sjamie	struct timespec *tsp;
189223189Sjamie	struct cfjail *j;
190223189Sjamie
191223189Sjamie	if (!TAILQ_EMPTY(&sleeping)) {
192223189Sjamie	again:
193223189Sjamie		tsp = NULL;
194223189Sjamie		if ((j = TAILQ_FIRST(&sleeping)) && j->timeout.tv_sec) {
195223189Sjamie			clock_gettime(CLOCK_REALTIME, &ts);
196223189Sjamie			ts.tv_sec = j->timeout.tv_sec - ts.tv_sec;
197223189Sjamie			ts.tv_nsec = j->timeout.tv_nsec - ts.tv_nsec;
198223189Sjamie			if (ts.tv_nsec < 0) {
199223189Sjamie				ts.tv_sec--;
200223189Sjamie				ts.tv_nsec += 1000000000;
201223189Sjamie			}
202223189Sjamie			if (ts.tv_sec < 0 ||
203223189Sjamie			    (ts.tv_sec == 0 && ts.tv_nsec == 0)) {
204223189Sjamie				j->flags |= JF_TIMEOUT;
205223189Sjamie				clear_procs(j);
206223189Sjamie				return j;
207223189Sjamie			}
208223189Sjamie			tsp = &ts;
209223189Sjamie		}
210223189Sjamie		if (nonblock) {
211223189Sjamie			ts.tv_sec = 0;
212223189Sjamie			ts.tv_nsec = 0;
213223189Sjamie			tsp = &ts;
214223189Sjamie		}
215223189Sjamie		switch (kevent(kq, NULL, 0, &ke, 1, tsp)) {
216223189Sjamie		case -1:
217223189Sjamie			if (errno != EINTR)
218223189Sjamie				err(1, "kevent");
219223189Sjamie			goto again;
220223189Sjamie		case 0:
221223189Sjamie			if (!nonblock) {
222223189Sjamie				j = TAILQ_FIRST(&sleeping);
223223189Sjamie				j->flags |= JF_TIMEOUT;
224223189Sjamie				clear_procs(j);
225223189Sjamie				return j;
226223189Sjamie			}
227223189Sjamie			break;
228223189Sjamie		case 1:
229223189Sjamie			(void)waitpid(ke.ident, NULL, WNOHANG);
230223189Sjamie			if ((j = find_proc(ke.ident))) {
231223189Sjamie				j->pstatus = ke.data;
232223189Sjamie				return j;
233223189Sjamie			}
234223189Sjamie			goto again;
235223189Sjamie		}
236223189Sjamie	}
237223189Sjamie	return NULL;
238223189Sjamie}
239223189Sjamie
240223189Sjamie/*
241223189Sjamie * Run a single command for a jail, possible inside the jail.
242223189Sjamie */
243223189Sjamieint
244223189Sjamierun_command(struct cfjail *j)
245223189Sjamie{
246214117Sjamie	const struct passwd *pwd;
247223189Sjamie	const struct cfstring *comstring, *s;
248214117Sjamie	login_cap_t *lcap;
249214117Sjamie	char **argv;
250223351Sjamie	char *cs, *comcs, *devpath;
251214117Sjamie	const char *jidstr, *conslog, *path, *ruleset, *term, *username;
252223189Sjamie	enum intparam comparam;
253214117Sjamie	size_t comlen;
254214117Sjamie	pid_t pid;
255214117Sjamie	int argc, bg, clean, consfd, down, fib, i, injail, sjuser, timeout;
256223351Sjamie#if defined(INET) || defined(INET6)
257223351Sjamie	char *addr;
258223351Sjamie#endif
259214117Sjamie
260214117Sjamie	static char *cleanenv;
261214117Sjamie
262223263Sjamie	/* Perform some operations that aren't actually commands */
263223263Sjamie	comparam = *j->comparam;
264223263Sjamie	down = j->flags & (JF_STOP | JF_FAILED);
265223263Sjamie	switch (comparam) {
266223263Sjamie	case IP_STOP_TIMEOUT:
267223263Sjamie		return term_procs(j);
268223263Sjamie
269223263Sjamie	case IP__OP:
270223263Sjamie		if (down) {
271223263Sjamie			if (jail_remove(j->jid) == 0 && verbose >= 0 &&
272223263Sjamie			    (verbose > 0 || (j->flags & JF_STOP
273223263Sjamie			    ? note_remove : j->name != NULL)))
274223263Sjamie			    jail_note(j, "removed\n");
275223263Sjamie			j->jid = -1;
276223263Sjamie			if (j->flags & JF_STOP)
277223263Sjamie				dep_done(j, DF_LIGHT);
278223263Sjamie			else
279223263Sjamie				j->flags &= ~JF_PERSIST;
280223263Sjamie		} else {
281223263Sjamie			if (create_jail(j) < 0) {
282223263Sjamie				failed(j);
283223263Sjamie				return -1;
284223263Sjamie			}
285223263Sjamie			if (verbose >= 0 && (j->name || verbose > 0))
286223263Sjamie				jail_note(j, "created\n");
287223263Sjamie			dep_done(j, DF_LIGHT);
288223263Sjamie		}
289223263Sjamie		requeue(j, &ready);
290223263Sjamie		return 1;
291223327Sjamie
292223327Sjamie	default: ;
293223263Sjamie	}
294214117Sjamie	/*
295214117Sjamie	 * Collect exec arguments.  Internal commands for network and
296216367Sjamie	 * mounting build their own argument lists.
297214117Sjamie	 */
298223189Sjamie	comstring = j->comstring;
299223189Sjamie	bg = 0;
300216367Sjamie	switch (comparam) {
301223351Sjamie#ifdef INET
302216367Sjamie	case IP__IP4_IFADDR:
303214117Sjamie		argv = alloca(8 * sizeof(char *));
304214117Sjamie		*(const char **)&argv[0] = _PATH_IFCONFIG;
305214117Sjamie		if ((cs = strchr(comstring->s, '|'))) {
306214117Sjamie			argv[1] = alloca(cs - comstring->s + 1);
307214117Sjamie			strlcpy(argv[1], comstring->s, cs - comstring->s + 1);
308214117Sjamie			addr = cs + 1;
309214117Sjamie		} else {
310214117Sjamie			*(const char **)&argv[1] =
311214117Sjamie			    string_param(j->intparams[IP_INTERFACE]);
312214117Sjamie			addr = comstring->s;
313214117Sjamie		}
314214117Sjamie		*(const char **)&argv[2] = "inet";
315214117Sjamie		if (!(cs = strchr(addr, '/'))) {
316214117Sjamie			argv[3] = addr;
317214117Sjamie			*(const char **)&argv[4] = "netmask";
318214117Sjamie			*(const char **)&argv[5] = "255.255.255.255";
319214117Sjamie			argc = 6;
320214117Sjamie		} else if (strchr(cs + 1, '.')) {
321214117Sjamie			argv[3] = alloca(cs - addr + 1);
322214117Sjamie			strlcpy(argv[3], addr, cs - addr + 1);
323214117Sjamie			*(const char **)&argv[4] = "netmask";
324214117Sjamie			*(const char **)&argv[5] = cs + 1;
325214117Sjamie			argc = 6;
326214117Sjamie		} else {
327214117Sjamie			argv[3] = addr;
328214117Sjamie			argc = 4;
329214117Sjamie		}
330214117Sjamie		*(const char **)&argv[argc] = down ? "-alias" : "alias";
331214117Sjamie		argv[argc + 1] = NULL;
332216367Sjamie		break;
333223351Sjamie#endif
334216367Sjamie
335214117Sjamie#ifdef INET6
336216367Sjamie	case IP__IP6_IFADDR:
337214117Sjamie		argv = alloca(8 * sizeof(char *));
338214117Sjamie		*(const char **)&argv[0] = _PATH_IFCONFIG;
339214117Sjamie		if ((cs = strchr(comstring->s, '|'))) {
340214117Sjamie			argv[1] = alloca(cs - comstring->s + 1);
341214117Sjamie			strlcpy(argv[1], comstring->s, cs - comstring->s + 1);
342214117Sjamie			addr = cs + 1;
343214117Sjamie		} else {
344214117Sjamie			*(const char **)&argv[1] =
345214117Sjamie			    string_param(j->intparams[IP_INTERFACE]);
346214117Sjamie			addr = comstring->s;
347214117Sjamie		}
348214117Sjamie		*(const char **)&argv[2] = "inet6";
349214117Sjamie		argv[3] = addr;
350214117Sjamie		if (!(cs = strchr(addr, '/'))) {
351214117Sjamie			*(const char **)&argv[4] = "prefixlen";
352214117Sjamie			*(const char **)&argv[5] = "128";
353214117Sjamie			argc = 6;
354214117Sjamie		} else
355214117Sjamie			argc = 4;
356214117Sjamie		*(const char **)&argv[argc] = down ? "-alias" : "alias";
357214117Sjamie		argv[argc + 1] = NULL;
358216367Sjamie		break;
359214117Sjamie#endif
360216367Sjamie
361216367Sjamie	case IP_VNET_INTERFACE:
362214117Sjamie		argv = alloca(5 * sizeof(char *));
363214117Sjamie		*(const char **)&argv[0] = _PATH_IFCONFIG;
364214117Sjamie		argv[1] = comstring->s;
365214117Sjamie		*(const char **)&argv[2] = down ? "-vnet" : "vnet";
366214117Sjamie		jidstr = string_param(j->intparams[KP_JID]);
367214117Sjamie		*(const char **)&argv[3] =
368214117Sjamie			jidstr ? jidstr : string_param(j->intparams[KP_NAME]);
369214117Sjamie		argv[4] = NULL;
370216367Sjamie		break;
371216367Sjamie
372216367Sjamie	case IP_MOUNT:
373216367Sjamie	case IP__MOUNT_FROM_FSTAB:
374214117Sjamie		argv = alloca(8 * sizeof(char *));
375214117Sjamie		comcs = alloca(comstring->len + 1);
376214117Sjamie		strcpy(comcs, comstring->s);
377214117Sjamie		argc = 0;
378214117Sjamie		for (cs = strtok(comcs, " \t\f\v\r\n"); cs && argc < 4;
379214117Sjamie		     cs = strtok(NULL, " \t\f\v\r\n"))
380214117Sjamie			argv[argc++] = cs;
381214797Sjamie		if (argc == 0)
382223189Sjamie			return 0;
383214117Sjamie		if (argc < 3) {
384214783Sjamie			jail_warnx(j, "%s: %s: missing information",
385214783Sjamie			    j->intparams[comparam]->name, comstring->s);
386214117Sjamie			failed(j);
387214117Sjamie			return -1;
388214117Sjamie		}
389214805Sjamie		if (check_path(j, j->intparams[comparam]->name, argv[1], 0,
390214805Sjamie		    down ? argv[2] : NULL) < 0) {
391214805Sjamie			failed(j);
392214797Sjamie			return -1;
393214805Sjamie		}
394214117Sjamie		if (down) {
395214117Sjamie			argv[4] = NULL;
396214117Sjamie			argv[3] = argv[1];
397214117Sjamie			*(const char **)&argv[0] = "/sbin/umount";
398214117Sjamie		} else {
399214117Sjamie			if (argc == 4) {
400214117Sjamie				argv[7] = NULL;
401214117Sjamie				argv[6] = argv[1];
402214117Sjamie				argv[5] = argv[0];
403214117Sjamie				argv[4] = argv[3];
404214117Sjamie				*(const char **)&argv[3] = "-o";
405214117Sjamie			} else {
406214117Sjamie				argv[5] = NULL;
407214117Sjamie				argv[4] = argv[1];
408214117Sjamie				argv[3] = argv[0];
409214117Sjamie			}
410214117Sjamie			*(const char **)&argv[0] = _PATH_MOUNT;
411214117Sjamie		}
412214117Sjamie		*(const char **)&argv[1] = "-t";
413216367Sjamie		break;
414216367Sjamie
415216367Sjamie	case IP_MOUNT_DEVFS:
416214117Sjamie		path = string_param(j->intparams[KP_PATH]);
417214117Sjamie		if (path == NULL) {
418214117Sjamie			jail_warnx(j, "mount.devfs: no path");
419214117Sjamie			failed(j);
420214117Sjamie			return -1;
421214117Sjamie		}
422214797Sjamie		devpath = alloca(strlen(path) + 5);
423214797Sjamie		sprintf(devpath, "%s/dev", path);
424214805Sjamie		if (check_path(j, "mount.devfs", devpath, 0,
425214805Sjamie		    down ? "devfs" : NULL) < 0) {
426214805Sjamie			failed(j);
427214797Sjamie			return -1;
428214805Sjamie		}
429214117Sjamie		if (down) {
430214117Sjamie			argv = alloca(3 * sizeof(char *));
431214117Sjamie			*(const char **)&argv[0] = "/sbin/umount";
432214797Sjamie			argv[1] = devpath;
433214117Sjamie			argv[2] = NULL;
434214117Sjamie		} else {
435214117Sjamie			argv = alloca(4 * sizeof(char *));
436214117Sjamie			*(const char **)&argv[0] = _PATH_BSHELL;
437214117Sjamie			*(const char **)&argv[1] = "-c";
438214117Sjamie			ruleset = string_param(j->intparams
439214117Sjamie			    [IP_MOUNT_DEVFS_RULESET]);
440214117Sjamie			argv[2] = alloca(strlen(path) +
441214117Sjamie			    (ruleset ? strlen(ruleset) + 1 : 0) + 56);
442214117Sjamie			sprintf(argv[2], ". /etc/rc.subr; load_rc_config .; "
443214117Sjamie			    "devfs_mount_jail %s/dev%s%s", path,
444214117Sjamie			    ruleset ? " " : "", ruleset ? ruleset : "");
445214117Sjamie			argv[3] = NULL;
446214117Sjamie		}
447216367Sjamie		break;
448216367Sjamie
449216367Sjamie	case IP_COMMAND:
450216367Sjamie		if (j->name != NULL)
451216367Sjamie			goto default_command;
452214117Sjamie		argc = 0;
453223188Sjamie		TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq)
454214117Sjamie			argc++;
455214117Sjamie		argv = alloca((argc + 1) * sizeof(char *));
456214117Sjamie		argc = 0;
457223188Sjamie		TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq)
458214117Sjamie			argv[argc++] = s->s;
459214117Sjamie		argv[argc] = NULL;
460223368Sjamie		j->comstring = &dummystring;
461216367Sjamie		break;
462216367Sjamie
463216367Sjamie	default:
464216367Sjamie	default_command:
465216367Sjamie		if ((cs = strpbrk(comstring->s, "!\"$&'()*;<>?[\\]`{|}~")) &&
466216367Sjamie		    !(cs[0] == '&' && cs[1] == '\0')) {
467216367Sjamie			argv = alloca(4 * sizeof(char *));
468216367Sjamie			*(const char **)&argv[0] = _PATH_BSHELL;
469216367Sjamie			*(const char **)&argv[1] = "-c";
470216367Sjamie			argv[2] = comstring->s;
471216367Sjamie			argv[3] = NULL;
472216367Sjamie		} else {
473216367Sjamie			if (cs) {
474216367Sjamie				*cs = 0;
475216367Sjamie				bg = 1;
476216367Sjamie			}
477216367Sjamie			comcs = alloca(comstring->len + 1);
478216367Sjamie			strcpy(comcs, comstring->s);
479216367Sjamie			argc = 0;
480216367Sjamie			for (cs = strtok(comcs, " \t\f\v\r\n"); cs;
481216367Sjamie			     cs = strtok(NULL, " \t\f\v\r\n"))
482216367Sjamie				argc++;
483216367Sjamie			argv = alloca((argc + 1) * sizeof(char *));
484216367Sjamie			strcpy(comcs, comstring->s);
485216367Sjamie			argc = 0;
486216367Sjamie			for (cs = strtok(comcs, " \t\f\v\r\n"); cs;
487216367Sjamie			     cs = strtok(NULL, " \t\f\v\r\n"))
488216367Sjamie				argv[argc++] = cs;
489216367Sjamie			argv[argc] = NULL;
490214117Sjamie		}
491214117Sjamie	}
492223189Sjamie	if (argv[0] == NULL)
493223189Sjamie		return 0;
494216367Sjamie
495214117Sjamie	if (int_param(j->intparams[IP_EXEC_TIMEOUT], &timeout) &&
496214117Sjamie	    timeout != 0) {
497214117Sjamie		clock_gettime(CLOCK_REALTIME, &j->timeout);
498214117Sjamie		j->timeout.tv_sec += timeout;
499214117Sjamie	} else
500214117Sjamie		j->timeout.tv_sec = 0;
501214117Sjamie
502214117Sjamie	injail = comparam == IP_EXEC_START || comparam == IP_COMMAND ||
503214117Sjamie	    comparam == IP_EXEC_STOP;
504214117Sjamie	clean = bool_param(j->intparams[IP_EXEC_CLEAN]);
505214117Sjamie	username = string_param(j->intparams[injail
506214117Sjamie	    ? IP_EXEC_JAIL_USER : IP_EXEC_SYSTEM_USER]);
507214117Sjamie	sjuser = bool_param(j->intparams[IP_EXEC_SYSTEM_JAIL_USER]);
508214117Sjamie
509214117Sjamie	consfd = 0;
510214117Sjamie	if (injail &&
511214117Sjamie	    (conslog = string_param(j->intparams[IP_EXEC_CONSOLELOG]))) {
512214805Sjamie		if (check_path(j, "exec.consolelog", conslog, 1, NULL) < 0) {
513214805Sjamie			failed(j);
514214797Sjamie			return -1;
515214805Sjamie		}
516214117Sjamie		consfd =
517214117Sjamie		    open(conslog, O_WRONLY | O_CREAT | O_APPEND, DEFFILEMODE);
518214117Sjamie		if (consfd < 0) {
519214117Sjamie			jail_warnx(j, "open %s: %s", conslog, strerror(errno));
520214117Sjamie			failed(j);
521214117Sjamie			return -1;
522214117Sjamie		}
523214117Sjamie	}
524214117Sjamie
525214117Sjamie	comlen = 0;
526214117Sjamie	for (i = 0; argv[i]; i++)
527214117Sjamie		comlen += strlen(argv[i]) + 1;
528214117Sjamie	j->comline = cs = emalloc(comlen);
529214117Sjamie	for (i = 0; argv[i]; i++) {
530214117Sjamie		strcpy(cs, argv[i]);
531214117Sjamie		if (argv[i + 1]) {
532214117Sjamie			cs += strlen(argv[i]) + 1;
533214117Sjamie			cs[-1] = ' ';
534214117Sjamie		}
535214117Sjamie	}
536214117Sjamie	if (verbose > 0)
537214117Sjamie		jail_note(j, "run command%s%s%s: %s\n",
538214117Sjamie		    injail ? " in jail" : "", username ? " as " : "",
539214117Sjamie		    username ? username : "", j->comline);
540214117Sjamie
541214117Sjamie	pid = fork();
542214117Sjamie	if (pid < 0)
543214117Sjamie		err(1, "fork");
544214117Sjamie	if (pid > 0) {
545214117Sjamie		if (bg) {
546216367Sjamie			free(j->comline);
547216367Sjamie			j->comline = NULL;
548214117Sjamie			requeue(j, &ready);
549214117Sjamie		} else {
550216367Sjamie			paralimit--;
551214117Sjamie			add_proc(j, pid);
552214117Sjamie		}
553214117Sjamie		return 1;
554214117Sjamie	}
555214117Sjamie	if (bg)
556214117Sjamie		setsid();
557214117Sjamie
558223189Sjamie	/* Set up the environment and run the command */
559214117Sjamie	pwd = NULL;
560214117Sjamie	lcap = NULL;
561214117Sjamie	if ((clean || username) && injail && sjuser &&
562214117Sjamie	    get_user_info(j, username, &pwd, &lcap) < 0)
563214117Sjamie		exit(1);
564214117Sjamie	if (injail) {
565214117Sjamie		/* jail_attach won't chdir along with its chroot. */
566214117Sjamie		path = string_param(j->intparams[KP_PATH]);
567214117Sjamie		if (path && chdir(path) < 0) {
568214117Sjamie			jail_warnx(j, "chdir %s: %s", path, strerror(errno));
569214117Sjamie			exit(1);
570214117Sjamie		}
571214117Sjamie		if (int_param(j->intparams[IP_EXEC_FIB], &fib) &&
572214117Sjamie		    setfib(fib) < 0) {
573214117Sjamie			jail_warnx(j, "setfib: %s", strerror(errno));
574214117Sjamie			exit(1);
575214117Sjamie		}
576214117Sjamie		if (jail_attach(j->jid) < 0) {
577214117Sjamie			jail_warnx(j, "jail_attach: %s", strerror(errno));
578214117Sjamie			exit(1);
579214117Sjamie		}
580214117Sjamie	}
581214117Sjamie	if (clean || username) {
582214117Sjamie		if (!(injail && sjuser) &&
583214117Sjamie		    get_user_info(j, username, &pwd, &lcap) < 0)
584214117Sjamie			exit(1);
585214117Sjamie		if (clean) {
586214117Sjamie			term = getenv("TERM");
587214117Sjamie			environ = &cleanenv;
588214117Sjamie			setenv("PATH", "/bin:/usr/bin", 0);
589214117Sjamie			setenv("TERM", term, 1);
590214117Sjamie		}
591214117Sjamie		if (setusercontext(lcap, pwd, pwd->pw_uid, username
592214117Sjamie		    ? LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN
593214117Sjamie		    : LOGIN_SETPATH | LOGIN_SETENV) < 0) {
594214117Sjamie			jail_warnx(j, "setusercontext %s: %s", pwd->pw_name,
595214117Sjamie			    strerror(errno));
596214117Sjamie			exit(1);
597214117Sjamie		}
598214117Sjamie		login_close(lcap);
599214117Sjamie		setenv("USER", pwd->pw_name, 1);
600214117Sjamie		setenv("HOME", pwd->pw_dir, 1);
601214117Sjamie		setenv("SHELL",
602214117Sjamie		    *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1);
603214117Sjamie		if (clean && chdir(pwd->pw_dir) < 0) {
604214117Sjamie			jail_warnx(j, "chdir %s: %s",
605214117Sjamie			    pwd->pw_dir, strerror(errno));
606214117Sjamie			exit(1);
607214117Sjamie		}
608214117Sjamie		endpwent();
609214117Sjamie	}
610214117Sjamie
611214117Sjamie	if (consfd != 0 && (dup2(consfd, 1) < 0 || dup2(consfd, 2) < 0)) {
612214117Sjamie		jail_warnx(j, "exec.consolelog: %s", strerror(errno));
613214117Sjamie		exit(1);
614214117Sjamie	}
615214117Sjamie	closefrom(3);
616214117Sjamie	execvp(argv[0], argv);
617214117Sjamie	jail_warnx(j, "exec %s: %s", argv[0], strerror(errno));
618214117Sjamie	exit(1);
619214117Sjamie}
620214117Sjamie
621214117Sjamie/*
622214117Sjamie * Add a process to the hash, tied to a jail.
623214117Sjamie */
624214117Sjamiestatic void
625214117Sjamieadd_proc(struct cfjail *j, pid_t pid)
626214117Sjamie{
627214117Sjamie	struct kevent ke;
628214117Sjamie	struct cfjail *tj;
629214117Sjamie	struct phash *ph;
630214117Sjamie
631214117Sjamie	if (!kq && (kq = kqueue()) < 0)
632214117Sjamie		err(1, "kqueue");
633214117Sjamie	EV_SET(&ke, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
634214117Sjamie	if (kevent(kq, &ke, 1, NULL, 0, NULL) < 0)
635214117Sjamie		err(1, "kevent");
636214117Sjamie	ph = emalloc(sizeof(struct phash));
637214117Sjamie	ph->j = j;
638214117Sjamie	ph->pid = pid;
639214117Sjamie	LIST_INSERT_HEAD(&phash[pid % PHASH_SIZE], ph, le);
640214117Sjamie	j->nprocs++;
641216367Sjamie	j->flags |= JF_SLEEPQ;
642216367Sjamie	if (j->timeout.tv_sec == 0)
643216367Sjamie		requeue(j, &sleeping);
644216367Sjamie	else {
645216367Sjamie		/* File the jail in the sleep queue acording to its timeout. */
646214117Sjamie		TAILQ_REMOVE(j->queue, j, tq);
647214117Sjamie		TAILQ_FOREACH(tj, &sleeping, tq) {
648214117Sjamie			if (!tj->timeout.tv_sec ||
649214117Sjamie			    j->timeout.tv_sec < tj->timeout.tv_sec ||
650214117Sjamie			    (j->timeout.tv_sec == tj->timeout.tv_sec &&
651214117Sjamie			    j->timeout.tv_nsec <= tj->timeout.tv_nsec)) {
652214117Sjamie				TAILQ_INSERT_BEFORE(tj, j, tq);
653214117Sjamie				break;
654214117Sjamie			}
655214117Sjamie		}
656214117Sjamie		if (tj == NULL)
657214117Sjamie			TAILQ_INSERT_TAIL(&sleeping, j, tq);
658214117Sjamie		j->queue = &sleeping;
659216367Sjamie	}
660214117Sjamie}
661214117Sjamie
662214117Sjamie/*
663214117Sjamie * Remove any processes from the hash that correspond to a jail.
664214117Sjamie */
665214117Sjamiestatic void
666214117Sjamieclear_procs(struct cfjail *j)
667214117Sjamie{
668214117Sjamie	struct kevent ke;
669214117Sjamie	struct phash *ph, *tph;
670214117Sjamie	int i;
671214117Sjamie
672214117Sjamie	j->nprocs = 0;
673214117Sjamie	for (i = 0; i < PHASH_SIZE; i++)
674214117Sjamie		LIST_FOREACH_SAFE(ph, &phash[i], le, tph)
675214117Sjamie			if (ph->j == j) {
676214117Sjamie				EV_SET(&ke, ph->pid, EVFILT_PROC, EV_DELETE,
677214117Sjamie				    NOTE_EXIT, 0, NULL);
678214117Sjamie				(void)kevent(kq, &ke, 1, NULL, 0, NULL);
679214117Sjamie				LIST_REMOVE(ph, le);
680214117Sjamie				free(ph);
681214117Sjamie			}
682214117Sjamie}
683214117Sjamie
684214117Sjamie/*
685214117Sjamie * Find the jail that corresponds to an exited process.
686214117Sjamie */
687214117Sjamiestatic struct cfjail *
688214117Sjamiefind_proc(pid_t pid)
689214117Sjamie{
690214117Sjamie	struct cfjail *j;
691214117Sjamie	struct phash *ph;
692214117Sjamie
693214117Sjamie	LIST_FOREACH(ph, &phash[pid % PHASH_SIZE], le)
694214117Sjamie		if (ph->pid == pid) {
695214117Sjamie			j = ph->j;
696214117Sjamie			LIST_REMOVE(ph, le);
697214117Sjamie			free(ph);
698214117Sjamie			return --j->nprocs ? NULL : j;
699214117Sjamie		}
700214117Sjamie	return NULL;
701214117Sjamie}
702214117Sjamie
703214117Sjamie/*
704216367Sjamie * Send SIGTERM to all processes in a jail and wait for them to die.
705216367Sjamie */
706216367Sjamiestatic int
707216367Sjamieterm_procs(struct cfjail *j)
708216367Sjamie{
709216367Sjamie	struct kinfo_proc *ki;
710216367Sjamie	int i, noted, pcnt, timeout;
711216367Sjamie
712216367Sjamie	static kvm_t *kd;
713216367Sjamie
714216367Sjamie	if (!int_param(j->intparams[IP_STOP_TIMEOUT], &timeout))
715216367Sjamie		timeout = DEFAULT_STOP_TIMEOUT;
716216367Sjamie	else if (timeout == 0)
717216367Sjamie		return 0;
718216367Sjamie
719216367Sjamie	if (kd == NULL) {
720216367Sjamie		kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "jail");
721216367Sjamie		if (kd == NULL)
722216367Sjamie			exit(1);
723216367Sjamie	}
724216367Sjamie
725216367Sjamie	ki = kvm_getprocs(kd, KERN_PROC_PROC, 0, &pcnt);
726216367Sjamie	if (ki == NULL)
727216367Sjamie		exit(1);
728216367Sjamie	noted = 0;
729216367Sjamie	for (i = 0; i < pcnt; i++)
730216367Sjamie		if (ki[i].ki_jid == j->jid &&
731216367Sjamie		    kill(ki[i].ki_pid, SIGTERM) == 0) {
732216367Sjamie			add_proc(j, ki[i].ki_pid);
733216367Sjamie			if (verbose > 0) {
734216367Sjamie				if (!noted) {
735216367Sjamie					noted = 1;
736216367Sjamie					jail_note(j, "sent SIGTERM to:");
737216367Sjamie				}
738216367Sjamie				printf(" %d", ki[i].ki_pid);
739216367Sjamie			}
740216367Sjamie		}
741216367Sjamie	if (noted)
742216367Sjamie		printf("\n");
743216367Sjamie	if (j->nprocs > 0) {
744216367Sjamie		clock_gettime(CLOCK_REALTIME, &j->timeout);
745216367Sjamie		j->timeout.tv_sec += timeout;
746216367Sjamie		return 1;
747216367Sjamie	}
748216367Sjamie	return 0;
749216367Sjamie}
750216367Sjamie
751216367Sjamie/*
752214117Sjamie * Look up a user in the passwd and login.conf files.
753214117Sjamie */
754214117Sjamiestatic int
755214117Sjamieget_user_info(struct cfjail *j, const char *username,
756214117Sjamie    const struct passwd **pwdp, login_cap_t **lcapp)
757214117Sjamie{
758214117Sjamie	const struct passwd *pwd;
759214117Sjamie
760214117Sjamie	*pwdp = pwd = username ? getpwnam(username) : getpwuid(getuid());
761214117Sjamie	if (pwd == NULL) {
762214117Sjamie		if (errno)
763214117Sjamie			jail_warnx(j, "getpwnam%s%s: %s", username ? " " : "",
764214117Sjamie			    username ? username : "", strerror(errno));
765214117Sjamie		else if (username)
766214117Sjamie			jail_warnx(j, "%s: no such user", username);
767214117Sjamie		else
768214117Sjamie			jail_warnx(j, "unknown uid %d", getuid());
769214117Sjamie		return -1;
770214117Sjamie	}
771214117Sjamie	*lcapp = login_getpwclass(pwd);
772214117Sjamie	if (*lcapp == NULL) {
773214117Sjamie		jail_warnx(j, "getpwclass %s: %s", pwd->pw_name,
774214117Sjamie		    strerror(errno));
775214117Sjamie		return -1;
776214117Sjamie	}
777214117Sjamie	/* Set the groups while the group file is still available */
778214117Sjamie	if (initgroups(pwd->pw_name, pwd->pw_gid) < 0) {
779214117Sjamie		jail_warnx(j, "initgroups %s: %s", pwd->pw_name,
780214117Sjamie		    strerror(errno));
781214117Sjamie		return -1;
782214117Sjamie	}
783214117Sjamie	return 0;
784214117Sjamie}
785214797Sjamie
786214797Sjamie/*
787214797Sjamie * Make sure a mount or consolelog path is a valid absolute pathname
788214797Sjamie * with no symlinks.
789214797Sjamie */
790214797Sjamiestatic int
791214805Sjamiecheck_path(struct cfjail *j, const char *pname, const char *path, int isfile,
792214805Sjamie    const char *umount_type)
793214797Sjamie{
794214805Sjamie	struct stat st, mpst;
795214805Sjamie	struct statfs stfs;
796214797Sjamie	char *tpath, *p;
797214797Sjamie	const char *jailpath;
798214797Sjamie	size_t jplen;
799214797Sjamie
800214797Sjamie	if (path[0] != '/') {
801214797Sjamie		jail_warnx(j, "%s: %s: not an absolute pathname",
802214797Sjamie		    pname, path);
803214797Sjamie		return -1;
804214797Sjamie	}
805214797Sjamie	/*
806214797Sjamie	 * Only check for symlinks in components below the jail's path,
807214797Sjamie	 * since that's where the security risk lies.
808214797Sjamie	 */
809214797Sjamie	jailpath = string_param(j->intparams[KP_PATH]);
810214797Sjamie	if (jailpath == NULL)
811214797Sjamie		jailpath = "";
812214797Sjamie	jplen = strlen(jailpath);
813214805Sjamie	if (!strncmp(path, jailpath, jplen) && path[jplen] == '/') {
814214805Sjamie		tpath = alloca(strlen(path) + 1);
815214805Sjamie		strcpy(tpath, path);
816214805Sjamie		for (p = tpath + jplen; p != NULL; ) {
817214805Sjamie			p = strchr(p + 1, '/');
818214805Sjamie			if (p)
819214805Sjamie				*p = '\0';
820214805Sjamie			if (lstat(tpath, &st) < 0) {
821214805Sjamie				if (errno == ENOENT && isfile && !p)
822214805Sjamie					break;
823214805Sjamie				jail_warnx(j, "%s: %s: %s", pname, tpath,
824214805Sjamie				    strerror(errno));
825214805Sjamie				return -1;
826214805Sjamie			}
827214805Sjamie			if (S_ISLNK(st.st_mode)) {
828214805Sjamie				jail_warnx(j, "%s: %s is a symbolic link",
829214805Sjamie				    pname, tpath);
830214805Sjamie				return -1;
831214805Sjamie			}
832214805Sjamie			if (p)
833214805Sjamie				*p = '/';
834214805Sjamie		}
835214805Sjamie	}
836214805Sjamie	if (umount_type != NULL) {
837214805Sjamie		if (stat(path, &st) < 0 || statfs(path, &stfs) < 0) {
838214805Sjamie			jail_warnx(j, "%s: %s: %s", pname, path,
839214797Sjamie			    strerror(errno));
840214797Sjamie			return -1;
841214797Sjamie		}
842214805Sjamie		if (stat(stfs.f_mntonname, &mpst) < 0) {
843214805Sjamie			jail_warnx(j, "%s: %s: %s", pname, stfs.f_mntonname,
844214805Sjamie			    strerror(errno));
845214797Sjamie			return -1;
846214797Sjamie		}
847214805Sjamie		if (st.st_ino != mpst.st_ino) {
848214805Sjamie			jail_warnx(j, "%s: %s: not a mount point",
849214805Sjamie			    pname, path);
850214805Sjamie			return -1;
851214805Sjamie		}
852214805Sjamie		if (strcmp(stfs.f_fstypename, umount_type)) {
853214805Sjamie			jail_warnx(j, "%s: %s: not a %s mount",
854214805Sjamie			    pname, path, umount_type);
855214805Sjamie			return -1;
856214805Sjamie		}
857214797Sjamie	}
858214797Sjamie	return 0;
859214797Sjamie}
860