18a16b7a1SPedro F. Giffuni /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 49b50d902SRodney W. Grimes * Copyright (c) 1988, 1993, 1994 59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 69b50d902SRodney W. Grimes * 79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 99b50d902SRodney W. Grimes * are met: 109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 179b50d902SRodney W. Grimes * without specific prior written permission. 189b50d902SRodney W. Grimes * 199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 299b50d902SRodney W. Grimes * SUCH DAMAGE. 309b50d902SRodney W. Grimes */ 319b50d902SRodney W. Grimes 329b50d902SRodney W. Grimes #ifndef lint 3306469209SMike Barcroft static const char copyright[] = 349b50d902SRodney W. Grimes "@(#) Copyright (c) 1988, 1993, 1994\n\ 359b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 369b50d902SRodney W. Grimes #endif /* not lint */ 379b50d902SRodney W. Grimes 3806469209SMike Barcroft #if 0 399b50d902SRodney W. Grimes #ifndef lint 40603460a7SMike Barcroft static char sccsid[] = "@(#)env.c 8.3 (Berkeley) 4/2/94"; 419b50d902SRodney W. Grimes #endif /* not lint */ 42603460a7SMike Barcroft #endif 43603460a7SMike Barcroft 44603460a7SMike Barcroft #include <sys/cdefs.h> 45603460a7SMike Barcroft __FBSDID("$FreeBSD$"); 469b50d902SRodney W. Grimes 47*85c8521eSKyle Evans #include <sys/types.h> 48*85c8521eSKyle Evans 499b50d902SRodney W. Grimes #include <err.h> 50603460a7SMike Barcroft #include <errno.h> 51*85c8521eSKyle Evans #include <login_cap.h> 52*85c8521eSKyle Evans #include <pwd.h> 53*85c8521eSKyle Evans #include <stdbool.h> 549b50d902SRodney W. Grimes #include <stdio.h> 559b50d902SRodney W. Grimes #include <stdlib.h> 56*85c8521eSKyle Evans #include <string.h> 579b50d902SRodney W. Grimes #include <unistd.h> 589b50d902SRodney W. Grimes 598fbe7ebfSGarance A Drosehn #include "envopts.h" 608fbe7ebfSGarance A Drosehn 619b50d902SRodney W. Grimes extern char **environ; 629b50d902SRodney W. Grimes 63f66e378bSGarance A Drosehn int env_verbosity; 64f66e378bSGarance A Drosehn 65f1bb2cd2SWarner Losh static void usage(void); 66d98fe205SPhilippe Charnier 675e5ceb11SJoseph Mingrone /* 685e5ceb11SJoseph Mingrone * Exit codes. 695e5ceb11SJoseph Mingrone */ 705e5ceb11SJoseph Mingrone #define EXIT_CANCELED 125 /* Internal error prior to exec attempt. */ 715e5ceb11SJoseph Mingrone #define EXIT_CANNOT_INVOKE 126 /* Program located, but not usable. */ 725e5ceb11SJoseph Mingrone #define EXIT_ENOENT 127 /* Could not find program to exec. */ 735e5ceb11SJoseph Mingrone 749b50d902SRodney W. Grimes int 75f4ac32deSDavid Malone main(int argc, char **argv) 769b50d902SRodney W. Grimes { 775e5ceb11SJoseph Mingrone char *altpath, **ep, *p, **parg, term; 789b50d902SRodney W. Grimes char *cleanenv[1]; 79*85c8521eSKyle Evans char *login_class, *login_name; 80*85c8521eSKyle Evans struct passwd *pw; 81*85c8521eSKyle Evans login_cap_t *lc; 82*85c8521eSKyle Evans bool login_as_user; 83*85c8521eSKyle Evans uid_t uid; 84212274c3SGarance A Drosehn int ch, want_clear; 852966d28cSSean Farley int rtrn; 869b50d902SRodney W. Grimes 878fbe7ebfSGarance A Drosehn altpath = NULL; 88*85c8521eSKyle Evans login_class = NULL; 89*85c8521eSKyle Evans login_name = NULL; 90*85c8521eSKyle Evans pw = NULL; 91*85c8521eSKyle Evans lc = NULL; 92*85c8521eSKyle Evans login_as_user = false; 93212274c3SGarance A Drosehn want_clear = 0; 945e5ceb11SJoseph Mingrone term = '\n'; 95*85c8521eSKyle Evans while ((ch = getopt(argc, argv, "-0iL:P:S:U:u:v")) != -1) 969b50d902SRodney W. Grimes switch(ch) { 979b50d902SRodney W. Grimes case '-': 9885ab7ba1SSheldon Hearn case 'i': 99212274c3SGarance A Drosehn want_clear = 1; 100f66e378bSGarance A Drosehn break; 1015e5ceb11SJoseph Mingrone case '0': 1025e5ceb11SJoseph Mingrone term = '\0'; 1035e5ceb11SJoseph Mingrone break; 104*85c8521eSKyle Evans case 'U': 105*85c8521eSKyle Evans login_as_user = true; 106*85c8521eSKyle Evans /* FALLTHROUGH */ 107*85c8521eSKyle Evans case 'L': 108*85c8521eSKyle Evans login_name = optarg; 109*85c8521eSKyle Evans break; 1108fbe7ebfSGarance A Drosehn case 'P': 1118fbe7ebfSGarance A Drosehn altpath = strdup(optarg); 1128fbe7ebfSGarance A Drosehn break; 1138fbe7ebfSGarance A Drosehn case 'S': 1148fbe7ebfSGarance A Drosehn /* 1158fbe7ebfSGarance A Drosehn * The -S option, for "split string on spaces, with 1168fbe7ebfSGarance A Drosehn * support for some simple substitutions"... 1178fbe7ebfSGarance A Drosehn */ 1188fbe7ebfSGarance A Drosehn split_spaces(optarg, &optind, &argc, &argv); 1198fbe7ebfSGarance A Drosehn break; 120a414225dSGarance A Drosehn case 'u': 121a414225dSGarance A Drosehn if (env_verbosity) 122a414225dSGarance A Drosehn fprintf(stderr, "#env unset:\t%s\n", optarg); 123a414225dSGarance A Drosehn rtrn = unsetenv(optarg); 124a414225dSGarance A Drosehn if (rtrn == -1) 125a414225dSGarance A Drosehn err(EXIT_FAILURE, "unsetenv %s", optarg); 126a414225dSGarance A Drosehn break; 127f66e378bSGarance A Drosehn case 'v': 128f66e378bSGarance A Drosehn env_verbosity++; 129f66e378bSGarance A Drosehn if (env_verbosity > 1) 130f66e378bSGarance A Drosehn fprintf(stderr, "#env verbosity now at %d\n", 131f66e378bSGarance A Drosehn env_verbosity); 1329b50d902SRodney W. Grimes break; 1339b50d902SRodney W. Grimes case '?': 1349b50d902SRodney W. Grimes default: 135d98fe205SPhilippe Charnier usage(); 1369b50d902SRodney W. Grimes } 137212274c3SGarance A Drosehn if (want_clear) { 138212274c3SGarance A Drosehn environ = cleanenv; 139212274c3SGarance A Drosehn cleanenv[0] = NULL; 140212274c3SGarance A Drosehn if (env_verbosity) 141212274c3SGarance A Drosehn fprintf(stderr, "#env clearing environ\n"); 142212274c3SGarance A Drosehn } 143*85c8521eSKyle Evans if (login_name != NULL) { 144*85c8521eSKyle Evans login_class = strchr(login_name, '/'); 145*85c8521eSKyle Evans if (login_class) 146*85c8521eSKyle Evans *login_class++ = '\0'; 147*85c8521eSKyle Evans pw = getpwnam(login_name); 148*85c8521eSKyle Evans if (pw == NULL) { 149*85c8521eSKyle Evans char *endp = NULL; 150*85c8521eSKyle Evans errno = 0; 151*85c8521eSKyle Evans uid = strtoul(login_name, &endp, 10); 152*85c8521eSKyle Evans if (errno == 0 && *endp == '\0') 153*85c8521eSKyle Evans pw = getpwuid(uid); 154*85c8521eSKyle Evans } 155*85c8521eSKyle Evans if (pw == NULL) 156*85c8521eSKyle Evans errx(EXIT_FAILURE, "no such user: %s", login_name); 157*85c8521eSKyle Evans if (login_class != NULL) { 158*85c8521eSKyle Evans lc = login_getclass(login_class); 159*85c8521eSKyle Evans if (lc == NULL) 160*85c8521eSKyle Evans errx(EXIT_FAILURE, "no such login class: %s", 161*85c8521eSKyle Evans login_class); 162*85c8521eSKyle Evans } else { 163*85c8521eSKyle Evans lc = login_getpwclass(pw); 164*85c8521eSKyle Evans if (lc == NULL) 165*85c8521eSKyle Evans errx(EXIT_FAILURE, "login_getpwclass failed"); 166*85c8521eSKyle Evans } 167*85c8521eSKyle Evans 168*85c8521eSKyle Evans /* 169*85c8521eSKyle Evans * This is not done with setusercontext() because that will 170*85c8521eSKyle Evans * try and use ~/.login_conf even when we don't want it to. 171*85c8521eSKyle Evans */ 172*85c8521eSKyle Evans setclassenvironment(lc, pw, 1); 173*85c8521eSKyle Evans setclassenvironment(lc, pw, 0); 174*85c8521eSKyle Evans if (login_as_user) { 175*85c8521eSKyle Evans login_close(lc); 176*85c8521eSKyle Evans if ((lc = login_getuserclass(pw)) != NULL) { 177*85c8521eSKyle Evans setclassenvironment(lc, pw, 1); 178*85c8521eSKyle Evans setclassenvironment(lc, pw, 0); 179*85c8521eSKyle Evans } 180*85c8521eSKyle Evans } 181*85c8521eSKyle Evans endpwent(); 182*85c8521eSKyle Evans if (lc != NULL) 183*85c8521eSKyle Evans login_close(lc); 184*85c8521eSKyle Evans } 185ba174a5eSAndrey A. Chernov for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) { 186f66e378bSGarance A Drosehn if (env_verbosity) 187f66e378bSGarance A Drosehn fprintf(stderr, "#env setenv:\t%s\n", *argv); 1882966d28cSSean Farley *p = '\0'; 1892966d28cSSean Farley rtrn = setenv(*argv, p + 1, 1); 1902966d28cSSean Farley *p = '='; 1912966d28cSSean Farley if (rtrn == -1) 1922966d28cSSean Farley err(EXIT_FAILURE, "setenv %s", *argv); 193f66e378bSGarance A Drosehn } 1949b50d902SRodney W. Grimes if (*argv) { 1955e5ceb11SJoseph Mingrone if (term == '\0') 1965e5ceb11SJoseph Mingrone errx(EXIT_CANCELED, "cannot specify command with -0"); 1978fbe7ebfSGarance A Drosehn if (altpath) 1988fbe7ebfSGarance A Drosehn search_paths(altpath, argv); 199f66e378bSGarance A Drosehn if (env_verbosity) { 200f66e378bSGarance A Drosehn fprintf(stderr, "#env executing:\t%s\n", *argv); 201f66e378bSGarance A Drosehn for (parg = argv, argc = 0; *parg; parg++, argc++) 202f66e378bSGarance A Drosehn fprintf(stderr, "#env arg[%d]=\t'%s'\n", 203f66e378bSGarance A Drosehn argc, *parg); 204f66e378bSGarance A Drosehn if (env_verbosity > 1) 205f66e378bSGarance A Drosehn sleep(1); 206f66e378bSGarance A Drosehn } 2079b50d902SRodney W. Grimes execvp(*argv, argv); 2085e5ceb11SJoseph Mingrone err(errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE, 2095e5ceb11SJoseph Mingrone "%s", *argv); 2109b50d902SRodney W. Grimes } 2119b50d902SRodney W. Grimes for (ep = environ; *ep; ep++) 2125e5ceb11SJoseph Mingrone (void)printf("%s%c", *ep, term); 2139b50d902SRodney W. Grimes exit(0); 2149b50d902SRodney W. Grimes } 215d98fe205SPhilippe Charnier 216d98fe205SPhilippe Charnier static void 217f4ac32deSDavid Malone usage(void) 218d98fe205SPhilippe Charnier { 219d98fe205SPhilippe Charnier (void)fprintf(stderr, 220*85c8521eSKyle Evans "usage: env [-0iv] [-L|-U user[/class]] [-P utilpath] [-S string] [-u name]\n" 221a414225dSGarance A Drosehn " [name=value ...] [utility [argument ...]]\n"); 222d98fe205SPhilippe Charnier exit(1); 223d98fe205SPhilippe Charnier } 224