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 #endif 40 41 #include <sys/cdefs.h> 42 #include <sys/types.h> 43 44 #include <err.h> 45 #include <errno.h> 46 #include <login_cap.h> 47 #include <pwd.h> 48 #include <stdbool.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 54 #include "envopts.h" 55 56 extern char **environ; 57 58 int env_verbosity; 59 60 static void usage(void) __dead2; 61 62 /* 63 * Exit codes. 64 */ 65 #define EXIT_CANCELED 125 /* Internal error prior to exec attempt. */ 66 #define EXIT_CANNOT_INVOKE 126 /* Program located, but not usable. */ 67 #define EXIT_ENOENT 127 /* Could not find program to exec. */ 68 69 int 70 main(int argc, char **argv) 71 { 72 char *altpath, **ep, *p, **parg, term; 73 char *cleanenv[1]; 74 char *login_class, *login_name; 75 struct passwd *pw; 76 login_cap_t *lc; 77 bool login_as_user; 78 uid_t uid; 79 int ch, want_clear; 80 int rtrn; 81 82 altpath = NULL; 83 login_class = NULL; 84 login_name = NULL; 85 pw = NULL; 86 lc = NULL; 87 login_as_user = false; 88 want_clear = 0; 89 term = '\n'; 90 while ((ch = getopt(argc, argv, "-0iL:P:S:U:u:v")) != -1) 91 switch(ch) { 92 case '-': 93 case 'i': 94 want_clear = 1; 95 break; 96 case '0': 97 term = '\0'; 98 break; 99 case 'U': 100 login_as_user = true; 101 /* FALLTHROUGH */ 102 case 'L': 103 login_name = optarg; 104 break; 105 case 'P': 106 altpath = strdup(optarg); 107 break; 108 case 'S': 109 /* 110 * The -S option, for "split string on spaces, with 111 * support for some simple substitutions"... 112 */ 113 split_spaces(optarg, &optind, &argc, &argv); 114 break; 115 case 'u': 116 if (env_verbosity) 117 fprintf(stderr, "#env unset:\t%s\n", optarg); 118 rtrn = unsetenv(optarg); 119 if (rtrn == -1) 120 err(EXIT_FAILURE, "unsetenv %s", optarg); 121 break; 122 case 'v': 123 env_verbosity++; 124 if (env_verbosity > 1) 125 fprintf(stderr, "#env verbosity now at %d\n", 126 env_verbosity); 127 break; 128 case '?': 129 default: 130 usage(); 131 } 132 if (want_clear) { 133 environ = cleanenv; 134 cleanenv[0] = NULL; 135 if (env_verbosity) 136 fprintf(stderr, "#env clearing environ\n"); 137 } 138 if (login_name != NULL) { 139 login_class = strchr(login_name, '/'); 140 if (login_class) 141 *login_class++ = '\0'; 142 if (*login_name != '\0' && strcmp(login_name, "-") != 0) { 143 pw = getpwnam(login_name); 144 if (pw == NULL) { 145 char *endp = NULL; 146 errno = 0; 147 uid = strtoul(login_name, &endp, 10); 148 if (errno == 0 && *endp == '\0') 149 pw = getpwuid(uid); 150 } 151 if (pw == NULL) 152 errx(EXIT_FAILURE, "no such user: %s", login_name); 153 } 154 /* 155 * Note that it is safe for pw to be null here; the libutil 156 * code handles that, bypassing substitution of $ and using 157 * the class "default" if no class name is given either. 158 */ 159 if (login_class != NULL) { 160 lc = login_getclass(login_class); 161 if (lc == NULL) 162 errx(EXIT_FAILURE, "no such login class: %s", 163 login_class); 164 } else { 165 lc = login_getpwclass(pw); 166 if (lc == NULL) 167 errx(EXIT_FAILURE, "login_getpwclass failed"); 168 } 169 170 /* 171 * This is not done with setusercontext() because that will 172 * try and use ~/.login_conf even when we don't want it to. 173 */ 174 setclassenvironment(lc, pw, 1); 175 setclassenvironment(lc, pw, 0); 176 if (login_as_user) { 177 login_close(lc); 178 if ((lc = login_getuserclass(pw)) != NULL) { 179 setclassenvironment(lc, pw, 1); 180 setclassenvironment(lc, pw, 0); 181 } 182 } 183 endpwent(); 184 if (lc != NULL) 185 login_close(lc); 186 } 187 for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) { 188 if (env_verbosity) 189 fprintf(stderr, "#env setenv:\t%s\n", *argv); 190 *p = '\0'; 191 rtrn = setenv(*argv, p + 1, 1); 192 *p = '='; 193 if (rtrn == -1) 194 err(EXIT_FAILURE, "setenv %s", *argv); 195 } 196 if (*argv) { 197 if (term == '\0') 198 errx(EXIT_CANCELED, "cannot specify command with -0"); 199 if (altpath) 200 search_paths(altpath, argv); 201 if (env_verbosity) { 202 fprintf(stderr, "#env executing:\t%s\n", *argv); 203 for (parg = argv, argc = 0; *parg; parg++, argc++) 204 fprintf(stderr, "#env arg[%d]=\t'%s'\n", 205 argc, *parg); 206 if (env_verbosity > 1) 207 sleep(1); 208 } 209 execvp(*argv, argv); 210 err(errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE, 211 "%s", *argv); 212 } 213 for (ep = environ; *ep; ep++) 214 (void)printf("%s%c", *ep, term); 215 exit(0); 216 } 217 218 static void 219 usage(void) 220 { 221 (void)fprintf(stderr, 222 "usage: env [-0iv] [-L|-U user[/class]] [-P utilpath] [-S string] [-u name]\n" 223 " [name=value ...] [utility [argument ...]]\n"); 224 exit(1); 225 } 226