xref: /freebsd/usr.bin/env/env.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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, **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 	login_class = NULL;
87 	login_name = NULL;
88 	pw = NULL;
89 	lc = NULL;
90 	login_as_user = false;
91 	want_clear = 0;
92 	term = '\n';
93 	while ((ch = getopt(argc, argv, "-0iL:P:S:U:u:v")) != -1)
94 		switch(ch) {
95 		case '-':
96 		case 'i':
97 			want_clear = 1;
98 			break;
99 		case '0':
100 			term = '\0';
101 			break;
102 		case 'U':
103 			login_as_user = true;
104 			/* FALLTHROUGH */
105 		case 'L':
106 			login_name = optarg;
107 			break;
108 		case 'P':
109 			altpath = strdup(optarg);
110 			break;
111 		case 'S':
112 			/*
113 			 * The -S option, for "split string on spaces, with
114 			 * support for some simple substitutions"...
115 			 */
116 			split_spaces(optarg, &optind, &argc, &argv);
117 			break;
118 		case 'u':
119 			if (env_verbosity)
120 				fprintf(stderr, "#env unset:\t%s\n", optarg);
121 			rtrn = unsetenv(optarg);
122 			if (rtrn == -1)
123 				err(EXIT_FAILURE, "unsetenv %s", optarg);
124 			break;
125 		case 'v':
126 			env_verbosity++;
127 			if (env_verbosity > 1)
128 				fprintf(stderr, "#env verbosity now at %d\n",
129 				    env_verbosity);
130 			break;
131 		case '?':
132 		default:
133 			usage();
134 		}
135 	if (want_clear) {
136 		environ = cleanenv;
137 		cleanenv[0] = NULL;
138 		if (env_verbosity)
139 			fprintf(stderr, "#env clearing environ\n");
140 	}
141 	if (login_name != NULL) {
142 		login_class = strchr(login_name, '/');
143 		if (login_class)
144 			*login_class++ = '\0';
145 		if (*login_name != '\0' && strcmp(login_name, "-") != 0) {
146 			pw = getpwnam(login_name);
147 			if (pw == NULL) {
148 				char *endp = NULL;
149 				errno = 0;
150 				uid = strtoul(login_name, &endp, 10);
151 				if (errno == 0 && *endp == '\0')
152 					pw = getpwuid(uid);
153 			}
154 			if (pw == NULL)
155 				errx(EXIT_FAILURE, "no such user: %s", login_name);
156 		}
157 		/*
158 		 * Note that it is safe for pw to be null here; the libutil
159 		 * code handles that, bypassing substitution of $ and using
160 		 * the class "default" if no class name is given either.
161 		 */
162 		if (login_class != NULL) {
163 			lc = login_getclass(login_class);
164 			if (lc == NULL)
165 				errx(EXIT_FAILURE, "no such login class: %s",
166 				    login_class);
167 		} else {
168 			lc = login_getpwclass(pw);
169 			if (lc == NULL)
170 				errx(EXIT_FAILURE, "login_getpwclass failed");
171 		}
172 
173 		/*
174 		 * This is not done with setusercontext() because that will
175 		 * try and use ~/.login_conf even when we don't want it to.
176 		 */
177 		setclassenvironment(lc, pw, 1);
178 		setclassenvironment(lc, pw, 0);
179 		if (login_as_user) {
180 			login_close(lc);
181 			if ((lc = login_getuserclass(pw)) != NULL) {
182 				setclassenvironment(lc, pw, 1);
183 				setclassenvironment(lc, pw, 0);
184 			}
185 		}
186 		endpwent();
187 		if (lc != NULL)
188 			login_close(lc);
189 	}
190 	for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) {
191 		if (env_verbosity)
192 			fprintf(stderr, "#env setenv:\t%s\n", *argv);
193 		*p = '\0';
194 		rtrn = setenv(*argv, p + 1, 1);
195 		*p = '=';
196 		if (rtrn == -1)
197 			err(EXIT_FAILURE, "setenv %s", *argv);
198 	}
199 	if (*argv) {
200 		if (term == '\0')
201 			errx(EXIT_CANCELED, "cannot specify command with -0");
202 		if (altpath)
203 			search_paths(altpath, argv);
204 		if (env_verbosity) {
205 			fprintf(stderr, "#env executing:\t%s\n", *argv);
206 			for (parg = argv, argc = 0; *parg; parg++, argc++)
207 				fprintf(stderr, "#env    arg[%d]=\t'%s'\n",
208 				    argc, *parg);
209 			if (env_verbosity > 1)
210 				sleep(1);
211 		}
212 		execvp(*argv, argv);
213 		err(errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE,
214 		    "%s", *argv);
215 	}
216 	for (ep = environ; *ep; ep++)
217 		(void)printf("%s%c", *ep, term);
218 	exit(0);
219 }
220 
221 static void
222 usage(void)
223 {
224 	(void)fprintf(stderr,
225 	    "usage: env [-0iv] [-L|-U user[/class]] [-P utilpath] [-S string] [-u name]\n"
226 	    "           [name=value ...] [utility [argument ...]]\n");
227 	exit(1);
228 }
229