1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1988, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 static const char copyright[] = 34 "@(#) Copyright (c) 1988, 1993, 1994\n\ 35 The Regents of the University of California. All rights reserved.\n"; 36 #endif /* not lint */ 37 38 #if 0 39 #ifndef lint 40 static char sccsid[] = "@(#)env.c 8.3 (Berkeley) 4/2/94"; 41 #endif /* not lint */ 42 #endif 43 44 #include <sys/cdefs.h> 45 #include <sys/types.h> 46 47 #include <err.h> 48 #include <errno.h> 49 #include <login_cap.h> 50 #include <pwd.h> 51 #include <stdbool.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 57 #include "envopts.h" 58 59 extern char **environ; 60 61 int env_verbosity; 62 63 static void usage(void) __dead2; 64 65 /* 66 * Exit codes. 67 */ 68 #define EXIT_CANCELED 125 /* Internal error prior to exec attempt. */ 69 #define EXIT_CANNOT_INVOKE 126 /* Program located, but not usable. */ 70 #define EXIT_ENOENT 127 /* Could not find program to exec. */ 71 72 int 73 main(int argc, char **argv) 74 { 75 char *altpath, *altwd, **ep, *p, **parg, term; 76 char *cleanenv[1]; 77 char *login_class, *login_name; 78 struct passwd *pw; 79 login_cap_t *lc; 80 bool login_as_user; 81 uid_t uid; 82 int ch, want_clear; 83 int rtrn; 84 85 altpath = NULL; 86 altwd = NULL; 87 login_class = NULL; 88 login_name = NULL; 89 pw = NULL; 90 lc = NULL; 91 login_as_user = false; 92 want_clear = 0; 93 term = '\n'; 94 while ((ch = getopt(argc, argv, "-0C:iL:P:S:U:u:v")) != -1) 95 switch(ch) { 96 case '-': 97 case 'i': 98 want_clear = 1; 99 break; 100 case '0': 101 term = '\0'; 102 break; 103 case 'C': 104 altwd = optarg; 105 break; 106 case 'U': 107 login_as_user = true; 108 /* FALLTHROUGH */ 109 case 'L': 110 login_name = optarg; 111 break; 112 case 'P': 113 altpath = optarg; 114 break; 115 case 'S': 116 /* 117 * The -S option, for "split string on spaces, with 118 * support for some simple substitutions"... 119 */ 120 split_spaces(optarg, &optind, &argc, &argv); 121 break; 122 case 'u': 123 if (env_verbosity) 124 fprintf(stderr, "#env unset:\t%s\n", optarg); 125 rtrn = unsetenv(optarg); 126 if (rtrn == -1) 127 err(EXIT_FAILURE, "unsetenv %s", optarg); 128 break; 129 case 'v': 130 env_verbosity++; 131 if (env_verbosity > 1) 132 fprintf(stderr, "#env verbosity now at %d\n", 133 env_verbosity); 134 break; 135 case '?': 136 default: 137 usage(); 138 } 139 if (want_clear) { 140 environ = cleanenv; 141 cleanenv[0] = NULL; 142 if (env_verbosity) 143 fprintf(stderr, "#env clearing environ\n"); 144 } 145 if (login_name != NULL) { 146 login_class = strchr(login_name, '/'); 147 if (login_class) 148 *login_class++ = '\0'; 149 if (*login_name != '\0' && strcmp(login_name, "-") != 0) { 150 pw = getpwnam(login_name); 151 if (pw == NULL) { 152 char *endp = NULL; 153 errno = 0; 154 uid = strtoul(login_name, &endp, 10); 155 if (errno == 0 && *endp == '\0') 156 pw = getpwuid(uid); 157 } 158 if (pw == NULL) 159 errx(EXIT_FAILURE, "no such user: %s", login_name); 160 } 161 /* 162 * Note that it is safe for pw to be null here; the libutil 163 * code handles that, bypassing substitution of $ and using 164 * the class "default" if no class name is given either. 165 */ 166 if (login_class != NULL) { 167 lc = login_getclass(login_class); 168 if (lc == NULL) 169 errx(EXIT_FAILURE, "no such login class: %s", 170 login_class); 171 } else { 172 lc = login_getpwclass(pw); 173 if (lc == NULL) 174 errx(EXIT_FAILURE, "login_getpwclass failed"); 175 } 176 177 /* 178 * This is not done with setusercontext() because that will 179 * try and use ~/.login_conf even when we don't want it to. 180 */ 181 setclassenvironment(lc, pw, 1); 182 setclassenvironment(lc, pw, 0); 183 if (login_as_user) { 184 login_close(lc); 185 if ((lc = login_getuserclass(pw)) != NULL) { 186 setclassenvironment(lc, pw, 1); 187 setclassenvironment(lc, pw, 0); 188 } 189 } 190 endpwent(); 191 if (lc != NULL) 192 login_close(lc); 193 } 194 for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) { 195 if (env_verbosity) 196 fprintf(stderr, "#env setenv:\t%s\n", *argv); 197 *p = '\0'; 198 rtrn = setenv(*argv, p + 1, 1); 199 *p = '='; 200 if (rtrn == -1) 201 err(EXIT_FAILURE, "setenv %s", *argv); 202 } 203 if (*argv) { 204 if (term == '\0') 205 errx(EXIT_CANCELED, "cannot specify command with -0"); 206 if (altwd && chdir(altwd) != 0) 207 err(EXIT_CANCELED, "cannot change directory to '%s'", 208 altwd); 209 if (altpath) 210 search_paths(altpath, argv); 211 if (env_verbosity) { 212 fprintf(stderr, "#env executing:\t%s\n", *argv); 213 for (parg = argv, argc = 0; *parg; parg++, argc++) 214 fprintf(stderr, "#env arg[%d]=\t'%s'\n", 215 argc, *parg); 216 if (env_verbosity > 1) 217 sleep(1); 218 } 219 execvp(*argv, argv); 220 err(errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE, 221 "%s", *argv); 222 } else { 223 if (altwd) 224 errx(EXIT_CANCELED, "must specify command with -C"); 225 if (altpath) 226 errx(EXIT_CANCELED, "must specify command with -P"); 227 } 228 for (ep = environ; *ep; ep++) 229 (void)printf("%s%c", *ep, term); 230 if (fflush(stdout) != 0) 231 err(1, "stdout"); 232 exit(0); 233 } 234 235 static void 236 usage(void) 237 { 238 (void)fprintf(stderr, 239 "usage: env [-0iv] [-C workdir] [-L|-U user[/class]] [-P utilpath] [-S string]\n" 240 " [-u name] [name=value ...] [utility [argument ...]]\n"); 241 exit(1); 242 } 243