xref: /freebsd/usr.sbin/jexec/jexec.c (revision b670c9bafc0e31c7609969bf374b2e80bdc00211)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
5  * Copyright (c) 2008 Bjoern A. Zeeb <bz@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/param.h>
31 #include <sys/jail.h>
32 #include <sys/socket.h>
33 #include <sys/sysctl.h>
34 
35 #include <arpa/inet.h>
36 #include <netinet/in.h>
37 
38 #include <err.h>
39 #include <errno.h>
40 #include <jail.h>
41 #include <limits.h>
42 #include <login_cap.h>
43 #include <paths.h>
44 #include <pwd.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 extern char **environ;
51 
52 static void	get_user_info(const char *username, const struct passwd **pwdp,
53     login_cap_t **lcapp);
54 static void	usage(void);
55 
56 int
57 main(int argc, char *argv[])
58 {
59 	int jid;
60 	login_cap_t *lcap = NULL;
61 	int ch, clean, dflag, uflag, Uflag;
62 	char *cleanenv;
63 	const struct passwd *pwd = NULL;
64 	const char *username, *shell, *term;
65 	const char *workdir;
66 
67 	ch = clean = dflag = uflag = Uflag = 0;
68 	username = NULL;
69 	workdir = "/";
70 
71 	while ((ch = getopt(argc, argv, "d:lnu:U:")) != -1) {
72 		switch (ch) {
73 		case 'd':
74 			workdir = optarg;
75 			dflag = 1;
76 			break;
77 		case 'l':
78 			clean = 1;
79 			break;
80 		case 'n':
81 			/* Specified name, now unused */
82 			break;
83 		case 'u':
84 			username = optarg;
85 			uflag = 1;
86 			break;
87 		case 'U':
88 			username = optarg;
89 			Uflag = 1;
90 			break;
91 		default:
92 			usage();
93 		}
94 	}
95 	argc -= optind;
96 	argv += optind;
97 	if (argc < 1)
98 		usage();
99 	if (uflag && Uflag)
100 		usage();
101 	if (uflag || (clean && !Uflag))
102 		/* User info from the home environment */
103 		get_user_info(username, &pwd, &lcap);
104 
105 	/* Attach to the jail */
106 	jid = jail_getid(argv[0]);
107 	if (jid < 0)
108 		errx(1, "%s", jail_errmsg);
109 	if (jail_attach(jid) == -1)
110 		err(1, "jail_attach(%d)", jid);
111 	if (chdir(workdir) == -1)
112 		err(1, "chdir(): %s", workdir);
113 
114 	/* Set up user environment */
115 	if (clean || username != NULL) {
116 		if (Uflag)
117 			/* User info from the jail environment */
118 			get_user_info(username, &pwd, &lcap);
119 		if (clean) {
120 			term = getenv("TERM");
121 			cleanenv = NULL;
122 			environ = &cleanenv;
123 			setenv("PATH", "/bin:/usr/bin", 1);
124 			if (term != NULL)
125 				setenv("TERM", term, 1);
126 		}
127 		if (setgid(pwd->pw_gid) != 0)
128 			err(1, "setgid");
129 		if (setusercontext(lcap, pwd, pwd->pw_uid, username
130 		    ? LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN
131 		    : LOGIN_SETPATH | LOGIN_SETENV) != 0)
132 			err(1, "setusercontext");
133 		login_close(lcap);
134 		setenv("USER", pwd->pw_name, 1);
135 		setenv("HOME", pwd->pw_dir, 1);
136 		setenv("SHELL",
137 		    *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1);
138 		if (clean && username && !dflag && chdir(pwd->pw_dir) < 0)
139 			err(1, "chdir: %s", pwd->pw_dir);
140 		endpwent();
141 	}
142 
143 	/* Run the specified command, or the shell */
144 	if (argc > 1) {
145 		if (execvp(argv[1], argv + 1) < 0)
146 			err(1, "execvp: %s", argv[1]);
147 	} else {
148 		if (!(shell = getenv("SHELL")))
149 			shell = _PATH_BSHELL;
150 		if (execlp(shell, shell, "-i", NULL) < 0)
151 			err(1, "execlp: %s", shell);
152 	}
153 	exit(0);
154 }
155 
156 static void
157 get_user_info(const char *username, const struct passwd **pwdp,
158     login_cap_t **lcapp)
159 {
160 	uid_t uid;
161 	const struct passwd *pwd;
162 
163 	errno = 0;
164 	if (username) {
165 		pwd = getpwnam(username);
166 		if (pwd == NULL) {
167 			if (errno)
168 				err(1, "getpwnam: %s", username);
169 			else
170 				errx(1, "%s: no such user", username);
171 		}
172 	} else {
173 		uid = getuid();
174 		pwd = getpwuid(uid);
175 		if (pwd == NULL) {
176 			if (errno)
177 				err(1, "getpwuid: %d", uid);
178 			else
179 				errx(1, "unknown uid: %d", uid);
180 		}
181 	}
182 	*pwdp = pwd;
183 	*lcapp = login_getpwclass(pwd);
184 	if (*lcapp == NULL)
185 		err(1, "getpwclass: %s", pwd->pw_name);
186 	if (initgroups(pwd->pw_name, pwd->pw_gid) < 0)
187 		err(1, "initgroups: %s", pwd->pw_name);
188 }
189 
190 static void
191 usage(void)
192 {
193 
194 	fprintf(stderr, "%s\n",
195 	    "usage: jexec [-l] [-d working-directory] [-u username | -U username] jail\n"
196 	    "       [command ...]");
197 	exit(1);
198 }
199