2 * Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
3 * Copyright (c) 2008 Bjoern A. Zeeb <bz@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <sys/sysctl.h>
35 #include <arpa/inet.h>
36 #include <netinet/in.h>
42 #include <login_cap.h>
49 static void usage(void);
51 #define GET_USER_INFO do { \
52 pwd = getpwnam(username); \
55 err(1, "getpwnam: %s", username); \
57 errx(1, "%s: no such user", username); \
59 lcap = login_getpwclass(pwd); \
61 err(1, "getpwclass: %s", username); \
62 ngroups = ngroups_max; \
63 if (getgrouplist(username, pwd->pw_gid, groups, &ngroups) != 0) \
64 err(1, "getgrouplist: %s", username); \
68 main(int argc, char *argv[])
71 login_cap_t *lcap = NULL;
72 struct passwd *pwd = NULL;
74 int ch, ngroups, uflag, Uflag;
78 ch = uflag = Uflag = 0;
80 ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
81 if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
84 while ((ch = getopt(argc, argv, "nu:U:")) != -1) {
87 /* Specified name, now unused */
109 jid = jail_getid(argv[0]);
111 errx(1, "%s", jail_errmsg);
112 if (jail_attach(jid) == -1)
113 err(1, "jail_attach(%d)", jid);
114 if (chdir("/") == -1)
115 err(1, "chdir(): /");
116 if (username != NULL) {
119 if (setgroups(ngroups, groups) != 0)
121 if (setgid(pwd->pw_gid) != 0)
123 if (setusercontext(lcap, pwd, pwd->pw_uid,
124 LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN) != 0)
125 err(1, "setusercontext");
128 if (execvp(argv[1], argv + 1) == -1)
129 err(1, "execvp(): %s", argv[1]);
137 fprintf(stderr, "%s\n",
138 "usage: jexec [-u username | -U username] jail command ...");