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>
50 extern char **environ;
52 static void get_user_info(const char *username, const struct passwd **pwdp,
54 static void usage(void);
57 main(int argc, char *argv[])
60 login_cap_t *lcap = NULL;
61 int ch, clean, uflag, Uflag;
63 const struct passwd *pwd = NULL;
64 const char *username, *shell, *term;
66 ch = clean = uflag = Uflag = 0;
69 while ((ch = getopt(argc, argv, "lnu:U:")) != -1) {
75 /* Specified name, now unused */
95 if (uflag || (clean && !Uflag))
96 /* User info from the home environment */
97 get_user_info(username, &pwd, &lcap);
99 /* Attach to the jail */
100 jid = jail_getid(argv[0]);
102 errx(1, "%s", jail_errmsg);
103 if (jail_attach(jid) == -1)
104 err(1, "jail_attach(%d)", jid);
105 if (chdir("/") == -1)
106 err(1, "chdir(): /");
108 /* Set up user environment */
109 if (clean || username != NULL) {
111 /* User info from the jail environment */
112 get_user_info(username, &pwd, &lcap);
114 term = getenv("TERM");
117 setenv("PATH", "/bin:/usr/bin", 1);
119 setenv("TERM", term, 1);
121 if (setgid(pwd->pw_gid) != 0)
123 if (setusercontext(lcap, pwd, pwd->pw_uid, username
124 ? LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN
125 : LOGIN_SETPATH | LOGIN_SETENV) != 0)
126 err(1, "setusercontext");
128 setenv("USER", pwd->pw_name, 1);
129 setenv("HOME", pwd->pw_dir, 1);
131 *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1);
132 if (clean && chdir(pwd->pw_dir) < 0)
133 err(1, "chdir: %s", pwd->pw_dir);
137 /* Run the specified command, or the shell */
139 if (execvp(argv[1], argv + 1) < 0)
140 err(1, "execvp: %s", argv[1]);
142 if (!(shell = getenv("SHELL")))
143 shell = _PATH_BSHELL;
144 if (execlp(shell, shell, "-i", NULL) < 0)
145 err(1, "execlp: %s", shell);
151 get_user_info(const char *username, const struct passwd **pwdp,
155 const struct passwd *pwd;
159 pwd = getpwnam(username);
162 err(1, "getpwnam: %s", username);
164 errx(1, "%s: no such user", username);
171 err(1, "getpwuid: %d", uid);
173 errx(1, "unknown uid: %d", uid);
177 *lcapp = login_getpwclass(pwd);
179 err(1, "getpwclass: %s", pwd->pw_name);
180 if (initgroups(pwd->pw_name, pwd->pw_gid) < 0)
181 err(1, "initgroups: %s", pwd->pw_name);
188 fprintf(stderr, "%s\n",
189 "usage: jexec [-l] [-u username | -U username] jail [command ...]");