xref: /freebsd/usr.sbin/jexec/jexec.c (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
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  * $FreeBSD$
30  */
31 
32 #include <sys/param.h>
33 #include <sys/jail.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
36 
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
39 
40 #include <err.h>
41 #include <errno.h>
42 #include <jail.h>
43 #include <limits.h>
44 #include <login_cap.h>
45 #include <paths.h>
46 #include <pwd.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 extern char **environ;
53 
54 static void	get_user_info(const char *username, const struct passwd **pwdp,
55     login_cap_t **lcapp);
56 static void	usage(void);
57 
58 int
59 main(int argc, char *argv[])
60 {
61 	int jid;
62 	login_cap_t *lcap = NULL;
63 	int ch, clean, uflag, Uflag;
64 	char *cleanenv;
65 	const struct passwd *pwd = NULL;
66 	const char *username, *shell, *term;
67 
68 	ch = clean = uflag = Uflag = 0;
69 	username = NULL;
70 
71 	while ((ch = getopt(argc, argv, "lnu:U:")) != -1) {
72 		switch (ch) {
73 		case 'l':
74 			clean = 1;
75 			break;
76 		case 'n':
77 			/* Specified name, now unused */
78 			break;
79 		case 'u':
80 			username = optarg;
81 			uflag = 1;
82 			break;
83 		case 'U':
84 			username = optarg;
85 			Uflag = 1;
86 			break;
87 		default:
88 			usage();
89 		}
90 	}
91 	argc -= optind;
92 	argv += optind;
93 	if (argc < 1)
94 		usage();
95 	if (uflag && Uflag)
96 		usage();
97 	if (uflag || (clean && !Uflag))
98 		/* User info from the home environment */
99 		get_user_info(username, &pwd, &lcap);
100 
101 	/* Attach to the jail */
102 	jid = jail_getid(argv[0]);
103 	if (jid < 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(): /");
109 
110 	/* Set up user environment */
111 	if (clean || username != NULL) {
112 		if (Uflag)
113 			/* User info from the jail environment */
114 			get_user_info(username, &pwd, &lcap);
115 		if (clean) {
116 			term = getenv("TERM");
117 			cleanenv = NULL;
118 			environ = &cleanenv;
119 			setenv("PATH", "/bin:/usr/bin", 1);
120 			if (term != NULL)
121 				setenv("TERM", term, 1);
122 		}
123 		if (setgid(pwd->pw_gid) != 0)
124 			err(1, "setgid");
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");
129 		login_close(lcap);
130 		setenv("USER", pwd->pw_name, 1);
131 		setenv("HOME", pwd->pw_dir, 1);
132 		setenv("SHELL",
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);
136 		endpwent();
137 	}
138 
139 	/* Run the specified command, or the shell */
140 	if (argc > 1) {
141 		if (execvp(argv[1], argv + 1) < 0)
142 			err(1, "execvp: %s", argv[1]);
143 	} else {
144 		if (!(shell = getenv("SHELL")))
145 			shell = _PATH_BSHELL;
146 		if (execlp(shell, shell, "-i", NULL) < 0)
147 			err(1, "execlp: %s", shell);
148 	}
149 	exit(0);
150 }
151 
152 static void
153 get_user_info(const char *username, const struct passwd **pwdp,
154     login_cap_t **lcapp)
155 {
156 	uid_t uid;
157 	const struct passwd *pwd;
158 
159 	errno = 0;
160 	if (username) {
161 		pwd = getpwnam(username);
162 		if (pwd == NULL) {
163 			if (errno)
164 				err(1, "getpwnam: %s", username);
165 			else
166 				errx(1, "%s: no such user", username);
167 		}
168 	} else {
169 		uid = getuid();
170 		pwd = getpwuid(uid);
171 		if (pwd == NULL) {
172 			if (errno)
173 				err(1, "getpwuid: %d", uid);
174 			else
175 				errx(1, "unknown uid: %d", uid);
176 		}
177 	}
178 	*pwdp = pwd;
179 	*lcapp = login_getpwclass(pwd);
180 	if (*lcapp == NULL)
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);
184 }
185 
186 static void
187 usage(void)
188 {
189 
190 	fprintf(stderr, "%s\n",
191 	    "usage: jexec [-l] [-u username | -U username] jail [command ...]");
192 	exit(1);
193 }
194