xref: /freebsd/usr.bin/env/env.c (revision 55deb0a5f089c8a27cfc1666655b93881c2b47ae)
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 
4785c8521eSKyle Evans #include <sys/types.h>
4885c8521eSKyle Evans 
499b50d902SRodney W. Grimes #include <err.h>
50603460a7SMike Barcroft #include <errno.h>
5185c8521eSKyle Evans #include <login_cap.h>
5285c8521eSKyle Evans #include <pwd.h>
5385c8521eSKyle Evans #include <stdbool.h>
549b50d902SRodney W. Grimes #include <stdio.h>
559b50d902SRodney W. Grimes #include <stdlib.h>
5685c8521eSKyle 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];
7985c8521eSKyle Evans 	char *login_class, *login_name;
8085c8521eSKyle Evans 	struct passwd *pw;
8185c8521eSKyle Evans 	login_cap_t *lc;
8285c8521eSKyle Evans 	bool login_as_user;
8385c8521eSKyle Evans 	uid_t uid;
84212274c3SGarance A Drosehn 	int ch, want_clear;
852966d28cSSean Farley 	int rtrn;
869b50d902SRodney W. Grimes 
878fbe7ebfSGarance A Drosehn 	altpath = NULL;
8885c8521eSKyle Evans 	login_class = NULL;
8985c8521eSKyle Evans 	login_name = NULL;
9085c8521eSKyle Evans 	pw = NULL;
9185c8521eSKyle Evans 	lc = NULL;
9285c8521eSKyle Evans 	login_as_user = false;
93212274c3SGarance A Drosehn 	want_clear = 0;
945e5ceb11SJoseph Mingrone 	term = '\n';
9585c8521eSKyle 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;
10485c8521eSKyle Evans 		case 'U':
10585c8521eSKyle Evans 			login_as_user = true;
10685c8521eSKyle Evans 			/* FALLTHROUGH */
10785c8521eSKyle Evans 		case 'L':
10885c8521eSKyle Evans 			login_name = optarg;
10985c8521eSKyle 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 	}
14385c8521eSKyle Evans 	if (login_name != NULL) {
14485c8521eSKyle Evans 		login_class = strchr(login_name, '/');
14585c8521eSKyle Evans 		if (login_class)
14685c8521eSKyle Evans 			*login_class++ = '\0';
147*55deb0a5SAndrew Gierth 		if (*login_name != '\0' && strcmp(login_name,"-") != 0) {
14885c8521eSKyle Evans 			pw = getpwnam(login_name);
14985c8521eSKyle Evans 			if (pw == NULL) {
15085c8521eSKyle Evans 				char *endp = NULL;
15185c8521eSKyle Evans 				errno = 0;
15285c8521eSKyle Evans 				uid = strtoul(login_name, &endp, 10);
15385c8521eSKyle Evans 				if (errno == 0 && *endp == '\0')
15485c8521eSKyle Evans 					pw = getpwuid(uid);
15585c8521eSKyle Evans 			}
15685c8521eSKyle Evans 			if (pw == NULL)
15785c8521eSKyle Evans 				errx(EXIT_FAILURE, "no such user: %s", login_name);
158*55deb0a5SAndrew Gierth 		}
159*55deb0a5SAndrew Gierth 		/*
160*55deb0a5SAndrew Gierth 		 * Note that it is safe for pw to be null here; the libutil
161*55deb0a5SAndrew Gierth 		 * code handles that, bypassing substitution of $ and using
162*55deb0a5SAndrew Gierth 		 * the class "default" if no class name is given either.
163*55deb0a5SAndrew Gierth 		 */
16485c8521eSKyle Evans 		if (login_class != NULL) {
16585c8521eSKyle Evans 			lc = login_getclass(login_class);
16685c8521eSKyle Evans 			if (lc == NULL)
16785c8521eSKyle Evans 				errx(EXIT_FAILURE, "no such login class: %s",
16885c8521eSKyle Evans 				    login_class);
16985c8521eSKyle Evans 		} else {
17085c8521eSKyle Evans 			lc = login_getpwclass(pw);
17185c8521eSKyle Evans 			if (lc == NULL)
17285c8521eSKyle Evans 				errx(EXIT_FAILURE, "login_getpwclass failed");
17385c8521eSKyle Evans 		}
17485c8521eSKyle Evans 
17585c8521eSKyle Evans 		/*
17685c8521eSKyle Evans 		 * This is not done with setusercontext() because that will
17785c8521eSKyle Evans 		 * try and use ~/.login_conf even when we don't want it to.
17885c8521eSKyle Evans 		 */
17985c8521eSKyle Evans 		setclassenvironment(lc, pw, 1);
18085c8521eSKyle Evans 		setclassenvironment(lc, pw, 0);
18185c8521eSKyle Evans 		if (login_as_user) {
18285c8521eSKyle Evans 			login_close(lc);
18385c8521eSKyle Evans 			if ((lc = login_getuserclass(pw)) != NULL) {
18485c8521eSKyle Evans 				setclassenvironment(lc, pw, 1);
18585c8521eSKyle Evans 				setclassenvironment(lc, pw, 0);
18685c8521eSKyle Evans 			}
18785c8521eSKyle Evans 		}
18885c8521eSKyle Evans 		endpwent();
18985c8521eSKyle Evans 		if (lc != NULL)
19085c8521eSKyle Evans 			login_close(lc);
19185c8521eSKyle Evans 	}
192ba174a5eSAndrey A. Chernov 	for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) {
193f66e378bSGarance A Drosehn 		if (env_verbosity)
194f66e378bSGarance A Drosehn 			fprintf(stderr, "#env setenv:\t%s\n", *argv);
1952966d28cSSean Farley 		*p = '\0';
1962966d28cSSean Farley 		rtrn = setenv(*argv, p + 1, 1);
1972966d28cSSean Farley 		*p = '=';
1982966d28cSSean Farley 		if (rtrn == -1)
1992966d28cSSean Farley 			err(EXIT_FAILURE, "setenv %s", *argv);
200f66e378bSGarance A Drosehn 	}
2019b50d902SRodney W. Grimes 	if (*argv) {
2025e5ceb11SJoseph Mingrone 		if (term == '\0')
2035e5ceb11SJoseph Mingrone 			errx(EXIT_CANCELED, "cannot specify command with -0");
2048fbe7ebfSGarance A Drosehn 		if (altpath)
2058fbe7ebfSGarance A Drosehn 			search_paths(altpath, argv);
206f66e378bSGarance A Drosehn 		if (env_verbosity) {
207f66e378bSGarance A Drosehn 			fprintf(stderr, "#env executing:\t%s\n", *argv);
208f66e378bSGarance A Drosehn 			for (parg = argv, argc = 0; *parg; parg++, argc++)
209f66e378bSGarance A Drosehn 				fprintf(stderr, "#env    arg[%d]=\t'%s'\n",
210f66e378bSGarance A Drosehn 				    argc, *parg);
211f66e378bSGarance A Drosehn 			if (env_verbosity > 1)
212f66e378bSGarance A Drosehn 				sleep(1);
213f66e378bSGarance A Drosehn 		}
2149b50d902SRodney W. Grimes 		execvp(*argv, argv);
2155e5ceb11SJoseph Mingrone 		err(errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE,
2165e5ceb11SJoseph Mingrone 		    "%s", *argv);
2179b50d902SRodney W. Grimes 	}
2189b50d902SRodney W. Grimes 	for (ep = environ; *ep; ep++)
2195e5ceb11SJoseph Mingrone 		(void)printf("%s%c", *ep, term);
2209b50d902SRodney W. Grimes 	exit(0);
2219b50d902SRodney W. Grimes }
222d98fe205SPhilippe Charnier 
223d98fe205SPhilippe Charnier static void
224f4ac32deSDavid Malone usage(void)
225d98fe205SPhilippe Charnier {
226d98fe205SPhilippe Charnier 	(void)fprintf(stderr,
22785c8521eSKyle Evans 	    "usage: env [-0iv] [-L|-U user[/class]] [-P utilpath] [-S string] [-u name]\n"
228a414225dSGarance A Drosehn 	    "           [name=value ...] [utility [argument ...]]\n");
229d98fe205SPhilippe Charnier 	exit(1);
230d98fe205SPhilippe Charnier }
231