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 int env_argc = argc; 63 char **env_argv = argv; 64 char *cleanenv; 65 const struct passwd *pwd = NULL; 66 const char *username, *shell, *term; 67 const char *workdir; 68 const char *jexec_args = "d:e:lnu:U:"; 69 70 ch = clean = dflag = uflag = Uflag = 0; 71 username = NULL; 72 workdir = "/"; 73 74 while ((ch = getopt(argc, argv, jexec_args)) != -1) { 75 switch (ch) { 76 case 'd': 77 workdir = optarg; 78 dflag = 1; 79 break; 80 case 'e': 81 /* Used later. */ 82 if (strchr(optarg, '=') == NULL) 83 errx(1, "%s: Invalid environment variable.", optarg); 84 break; 85 case 'l': 86 clean = 1; 87 break; 88 case 'n': 89 /* Specified name, now unused */ 90 break; 91 case 'u': 92 username = optarg; 93 uflag = 1; 94 break; 95 case 'U': 96 username = optarg; 97 Uflag = 1; 98 break; 99 default: 100 usage(); 101 } 102 } 103 argc -= optind; 104 argv += optind; 105 if (argc < 1) 106 usage(); 107 if (uflag && Uflag) 108 usage(); 109 if (uflag || (clean && !Uflag)) 110 /* User info from the home environment */ 111 get_user_info(username, &pwd, &lcap); 112 113 /* Attach to the jail */ 114 jid = jail_getid(argv[0]); 115 if (jid < 0) 116 errx(1, "%s", jail_errmsg); 117 if (jail_attach(jid) == -1) 118 err(1, "jail_attach(%d)", jid); 119 if (chdir(workdir) == -1) 120 err(1, "chdir(): %s", workdir); 121 122 /* Set up user environment */ 123 if (clean || username != NULL) { 124 if (Uflag) 125 /* User info from the jail environment */ 126 get_user_info(username, &pwd, &lcap); 127 if (clean) { 128 term = getenv("TERM"); 129 cleanenv = NULL; 130 environ = &cleanenv; 131 setenv("PATH", "/bin:/usr/bin", 1); 132 if (term != NULL) 133 setenv("TERM", term, 1); 134 } 135 if (setgid(pwd->pw_gid) != 0) 136 err(1, "setgid"); 137 if (setusercontext(lcap, pwd, pwd->pw_uid, username 138 ? LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN 139 : LOGIN_SETPATH | LOGIN_SETENV) != 0) 140 err(1, "setusercontext"); 141 login_close(lcap); 142 setenv("USER", pwd->pw_name, 1); 143 setenv("HOME", pwd->pw_dir, 1); 144 setenv("SHELL", 145 *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1); 146 if (clean && username && !dflag && chdir(pwd->pw_dir) < 0) 147 err(1, "chdir: %s", pwd->pw_dir); 148 endpwent(); 149 } 150 151 optreset = 1; 152 optind = 1; 153 154 /* Custom environment */ 155 while ((ch = getopt(env_argc, env_argv, jexec_args)) != -1) { 156 switch (ch) { 157 case 'e': 158 if (putenv(optarg) == -1) 159 err(1, "putenv"); 160 break; 161 } 162 } 163 164 /* Run the specified command, or the shell */ 165 if (argc > 1) { 166 if (execvp(argv[1], argv + 1) < 0) 167 err(1, "execvp: %s", argv[1]); 168 } else { 169 if (!(shell = getenv("SHELL"))) 170 shell = _PATH_BSHELL; 171 if (execlp(shell, shell, "-i", NULL) < 0) 172 err(1, "execlp: %s", shell); 173 } 174 exit(0); 175 } 176 177 static void 178 get_user_info(const char *username, const struct passwd **pwdp, 179 login_cap_t **lcapp) 180 { 181 uid_t uid; 182 const struct passwd *pwd; 183 184 errno = 0; 185 if (username) { 186 pwd = getpwnam(username); 187 if (pwd == NULL) { 188 if (errno) 189 err(1, "getpwnam: %s", username); 190 else 191 errx(1, "%s: no such user", username); 192 } 193 } else { 194 uid = getuid(); 195 pwd = getpwuid(uid); 196 if (pwd == NULL) { 197 if (errno) 198 err(1, "getpwuid: %d", uid); 199 else 200 errx(1, "unknown uid: %d", uid); 201 } 202 } 203 *pwdp = pwd; 204 *lcapp = login_getpwclass(pwd); 205 if (*lcapp == NULL) 206 err(1, "getpwclass: %s", pwd->pw_name); 207 if (initgroups(pwd->pw_name, pwd->pw_gid) < 0) 208 err(1, "initgroups: %s", pwd->pw_name); 209 } 210 211 static void 212 usage(void) 213 { 214 215 fprintf(stderr, "%s\n", 216 "usage: jexec [-l] [-d working-directory] [[-e name=value] ...]\n" 217 " [-u username | -U username] jail [command ...]"); 218 exit(1); 219 } 220