2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
5 * Copyright (c) 2008 Bjoern A. Zeeb <bz@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
44 #include <login_cap.h>
52 extern char **environ;
54 static void get_user_info(const char *username, const struct passwd **pwdp,
56 static void usage(void);
59 main(int argc, char *argv[])
62 login_cap_t *lcap = NULL;
63 int ch, clean, uflag, Uflag;
65 const struct passwd *pwd = NULL;
66 const char *username, *shell, *term;
68 ch = clean = uflag = Uflag = 0;
71 while ((ch = getopt(argc, argv, "lnu:U:")) != -1) {
77 /* Specified name, now unused */
97 if (uflag || (clean && !Uflag))
98 /* User info from the home environment */
99 get_user_info(username, &pwd, &lcap);
101 /* Attach to the jail */
102 jid = jail_getid(argv[0]);
104 errx(1, "%s", jail_errmsg);
105 if (jail_attach(jid) == -1)
106 err(1, "jail_attach(%d)", jid);
107 if (chdir("/") == -1)
108 err(1, "chdir(): /");
110 /* Set up user environment */
111 if (clean || username != NULL) {
113 /* User info from the jail environment */
114 get_user_info(username, &pwd, &lcap);
116 term = getenv("TERM");
119 setenv("PATH", "/bin:/usr/bin", 1);
121 setenv("TERM", term, 1);
123 if (setgid(pwd->pw_gid) != 0)
125 if (setusercontext(lcap, pwd, pwd->pw_uid, username
126 ? LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN
127 : LOGIN_SETPATH | LOGIN_SETENV) != 0)
128 err(1, "setusercontext");
130 setenv("USER", pwd->pw_name, 1);
131 setenv("HOME", pwd->pw_dir, 1);
133 *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1);
134 if (clean && chdir(pwd->pw_dir) < 0)
135 err(1, "chdir: %s", pwd->pw_dir);
139 /* Run the specified command, or the shell */
141 if (execvp(argv[1], argv + 1) < 0)
142 err(1, "execvp: %s", argv[1]);
144 if (!(shell = getenv("SHELL")))
145 shell = _PATH_BSHELL;
146 if (execlp(shell, shell, "-i", NULL) < 0)
147 err(1, "execlp: %s", shell);
153 get_user_info(const char *username, const struct passwd **pwdp,
157 const struct passwd *pwd;
161 pwd = getpwnam(username);
164 err(1, "getpwnam: %s", username);
166 errx(1, "%s: no such user", username);
173 err(1, "getpwuid: %d", uid);
175 errx(1, "unknown uid: %d", uid);
179 *lcapp = login_getpwclass(pwd);
181 err(1, "getpwclass: %s", pwd->pw_name);
182 if (initgroups(pwd->pw_name, pwd->pw_gid) < 0)
183 err(1, "initgroups: %s", pwd->pw_name);
190 fprintf(stderr, "%s\n",
191 "usage: jexec [-l] [-u username | -U username] jail [command ...]");