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
3285c8521eSKyle Evans #include <sys/types.h>
3385c8521eSKyle Evans
349b50d902SRodney W. Grimes #include <err.h>
35603460a7SMike Barcroft #include <errno.h>
3685c8521eSKyle Evans #include <login_cap.h>
3785c8521eSKyle Evans #include <pwd.h>
3885c8521eSKyle Evans #include <stdbool.h>
399b50d902SRodney W. Grimes #include <stdio.h>
409b50d902SRodney W. Grimes #include <stdlib.h>
4185c8521eSKyle Evans #include <string.h>
429b50d902SRodney W. Grimes #include <unistd.h>
439b50d902SRodney W. Grimes
448fbe7ebfSGarance A Drosehn #include "envopts.h"
458fbe7ebfSGarance A Drosehn
469b50d902SRodney W. Grimes extern char **environ;
479b50d902SRodney W. Grimes
48f66e378bSGarance A Drosehn int env_verbosity;
49f66e378bSGarance A Drosehn
50a1b6427aSAlfonso Gregory static void usage(void) __dead2;
51d98fe205SPhilippe Charnier
525e5ceb11SJoseph Mingrone /*
535e5ceb11SJoseph Mingrone * Exit codes.
545e5ceb11SJoseph Mingrone */
555e5ceb11SJoseph Mingrone #define EXIT_CANCELED 125 /* Internal error prior to exec attempt. */
565e5ceb11SJoseph Mingrone #define EXIT_CANNOT_INVOKE 126 /* Program located, but not usable. */
575e5ceb11SJoseph Mingrone #define EXIT_ENOENT 127 /* Could not find program to exec. */
585e5ceb11SJoseph Mingrone
599b50d902SRodney W. Grimes int
main(int argc,char ** argv)60f4ac32deSDavid Malone main(int argc, char **argv)
619b50d902SRodney W. Grimes {
626f6166e4SDag-Erling Smørgrav char *altpath, *altwd, **ep, *p, **parg, term;
639b50d902SRodney W. Grimes char *cleanenv[1];
6485c8521eSKyle Evans char *login_class, *login_name;
6585c8521eSKyle Evans struct passwd *pw;
6685c8521eSKyle Evans login_cap_t *lc;
6785c8521eSKyle Evans bool login_as_user;
6885c8521eSKyle Evans uid_t uid;
69212274c3SGarance A Drosehn int ch, want_clear;
702966d28cSSean Farley int rtrn;
719b50d902SRodney W. Grimes
728fbe7ebfSGarance A Drosehn altpath = NULL;
736f6166e4SDag-Erling Smørgrav altwd = NULL;
7485c8521eSKyle Evans login_class = NULL;
7585c8521eSKyle Evans login_name = NULL;
7685c8521eSKyle Evans pw = NULL;
7785c8521eSKyle Evans lc = NULL;
7885c8521eSKyle Evans login_as_user = false;
79212274c3SGarance A Drosehn want_clear = 0;
805e5ceb11SJoseph Mingrone term = '\n';
816f6166e4SDag-Erling Smørgrav while ((ch = getopt(argc, argv, "-0C:iL:P:S:U:u:v")) != -1)
829b50d902SRodney W. Grimes switch(ch) {
839b50d902SRodney W. Grimes case '-':
8485ab7ba1SSheldon Hearn case 'i':
85212274c3SGarance A Drosehn want_clear = 1;
86f66e378bSGarance A Drosehn break;
875e5ceb11SJoseph Mingrone case '0':
885e5ceb11SJoseph Mingrone term = '\0';
895e5ceb11SJoseph Mingrone break;
906f6166e4SDag-Erling Smørgrav case 'C':
916f6166e4SDag-Erling Smørgrav altwd = optarg;
926f6166e4SDag-Erling Smørgrav break;
9385c8521eSKyle Evans case 'U':
9485c8521eSKyle Evans login_as_user = true;
9585c8521eSKyle Evans /* FALLTHROUGH */
9685c8521eSKyle Evans case 'L':
9785c8521eSKyle Evans login_name = optarg;
9885c8521eSKyle Evans break;
998fbe7ebfSGarance A Drosehn case 'P':
1006f6166e4SDag-Erling Smørgrav altpath = optarg;
1018fbe7ebfSGarance A Drosehn break;
1028fbe7ebfSGarance A Drosehn case 'S':
1038fbe7ebfSGarance A Drosehn /*
1048fbe7ebfSGarance A Drosehn * The -S option, for "split string on spaces, with
1058fbe7ebfSGarance A Drosehn * support for some simple substitutions"...
1068fbe7ebfSGarance A Drosehn */
1078fbe7ebfSGarance A Drosehn split_spaces(optarg, &optind, &argc, &argv);
1088fbe7ebfSGarance A Drosehn break;
109a414225dSGarance A Drosehn case 'u':
110a414225dSGarance A Drosehn if (env_verbosity)
111a414225dSGarance A Drosehn fprintf(stderr, "#env unset:\t%s\n", optarg);
112a414225dSGarance A Drosehn rtrn = unsetenv(optarg);
113a414225dSGarance A Drosehn if (rtrn == -1)
114a414225dSGarance A Drosehn err(EXIT_FAILURE, "unsetenv %s", optarg);
115a414225dSGarance A Drosehn break;
116f66e378bSGarance A Drosehn case 'v':
117f66e378bSGarance A Drosehn env_verbosity++;
118f66e378bSGarance A Drosehn if (env_verbosity > 1)
119f66e378bSGarance A Drosehn fprintf(stderr, "#env verbosity now at %d\n",
120f66e378bSGarance A Drosehn env_verbosity);
1219b50d902SRodney W. Grimes break;
1229b50d902SRodney W. Grimes case '?':
1239b50d902SRodney W. Grimes default:
124d98fe205SPhilippe Charnier usage();
1259b50d902SRodney W. Grimes }
126212274c3SGarance A Drosehn if (want_clear) {
127212274c3SGarance A Drosehn environ = cleanenv;
128212274c3SGarance A Drosehn cleanenv[0] = NULL;
129212274c3SGarance A Drosehn if (env_verbosity)
130212274c3SGarance A Drosehn fprintf(stderr, "#env clearing environ\n");
131212274c3SGarance A Drosehn }
13285c8521eSKyle Evans if (login_name != NULL) {
13385c8521eSKyle Evans login_class = strchr(login_name, '/');
13485c8521eSKyle Evans if (login_class)
13585c8521eSKyle Evans *login_class++ = '\0';
13655deb0a5SAndrew Gierth if (*login_name != '\0' && strcmp(login_name, "-") != 0) {
13785c8521eSKyle Evans pw = getpwnam(login_name);
13885c8521eSKyle Evans if (pw == NULL) {
13985c8521eSKyle Evans char *endp = NULL;
14085c8521eSKyle Evans errno = 0;
14185c8521eSKyle Evans uid = strtoul(login_name, &endp, 10);
14285c8521eSKyle Evans if (errno == 0 && *endp == '\0')
14385c8521eSKyle Evans pw = getpwuid(uid);
14485c8521eSKyle Evans }
14585c8521eSKyle Evans if (pw == NULL)
14685c8521eSKyle Evans errx(EXIT_FAILURE, "no such user: %s", login_name);
14755deb0a5SAndrew Gierth }
14855deb0a5SAndrew Gierth /*
14955deb0a5SAndrew Gierth * Note that it is safe for pw to be null here; the libutil
15055deb0a5SAndrew Gierth * code handles that, bypassing substitution of $ and using
15155deb0a5SAndrew Gierth * the class "default" if no class name is given either.
15255deb0a5SAndrew Gierth */
15385c8521eSKyle Evans if (login_class != NULL) {
15485c8521eSKyle Evans lc = login_getclass(login_class);
15585c8521eSKyle Evans if (lc == NULL)
15685c8521eSKyle Evans errx(EXIT_FAILURE, "no such login class: %s",
15785c8521eSKyle Evans login_class);
15885c8521eSKyle Evans } else {
15985c8521eSKyle Evans lc = login_getpwclass(pw);
16085c8521eSKyle Evans if (lc == NULL)
16185c8521eSKyle Evans errx(EXIT_FAILURE, "login_getpwclass failed");
16285c8521eSKyle Evans }
16385c8521eSKyle Evans
16485c8521eSKyle Evans /*
16585c8521eSKyle Evans * This is not done with setusercontext() because that will
16685c8521eSKyle Evans * try and use ~/.login_conf even when we don't want it to.
16785c8521eSKyle Evans */
16885c8521eSKyle Evans setclassenvironment(lc, pw, 1);
16985c8521eSKyle Evans setclassenvironment(lc, pw, 0);
17085c8521eSKyle Evans if (login_as_user) {
17185c8521eSKyle Evans login_close(lc);
17285c8521eSKyle Evans if ((lc = login_getuserclass(pw)) != NULL) {
17385c8521eSKyle Evans setclassenvironment(lc, pw, 1);
17485c8521eSKyle Evans setclassenvironment(lc, pw, 0);
17585c8521eSKyle Evans }
17685c8521eSKyle Evans }
17785c8521eSKyle Evans endpwent();
17885c8521eSKyle Evans if (lc != NULL)
17985c8521eSKyle Evans login_close(lc);
18085c8521eSKyle Evans }
181ba174a5eSAndrey A. Chernov for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) {
182f66e378bSGarance A Drosehn if (env_verbosity)
183f66e378bSGarance A Drosehn fprintf(stderr, "#env setenv:\t%s\n", *argv);
1842966d28cSSean Farley *p = '\0';
1852966d28cSSean Farley rtrn = setenv(*argv, p + 1, 1);
1862966d28cSSean Farley *p = '=';
1872966d28cSSean Farley if (rtrn == -1)
1882966d28cSSean Farley err(EXIT_FAILURE, "setenv %s", *argv);
189f66e378bSGarance A Drosehn }
1909b50d902SRodney W. Grimes if (*argv) {
1915e5ceb11SJoseph Mingrone if (term == '\0')
1925e5ceb11SJoseph Mingrone errx(EXIT_CANCELED, "cannot specify command with -0");
1936f6166e4SDag-Erling Smørgrav if (altwd && chdir(altwd) != 0)
1946f6166e4SDag-Erling Smørgrav err(EXIT_CANCELED, "cannot change directory to '%s'",
1956f6166e4SDag-Erling Smørgrav altwd);
1968fbe7ebfSGarance A Drosehn if (altpath)
1978fbe7ebfSGarance A Drosehn search_paths(altpath, argv);
198f66e378bSGarance A Drosehn if (env_verbosity) {
199f66e378bSGarance A Drosehn fprintf(stderr, "#env executing:\t%s\n", *argv);
200f66e378bSGarance A Drosehn for (parg = argv, argc = 0; *parg; parg++, argc++)
201f66e378bSGarance A Drosehn fprintf(stderr, "#env arg[%d]=\t'%s'\n",
202f66e378bSGarance A Drosehn argc, *parg);
203f66e378bSGarance A Drosehn if (env_verbosity > 1)
204f66e378bSGarance A Drosehn sleep(1);
205f66e378bSGarance A Drosehn }
2069b50d902SRodney W. Grimes execvp(*argv, argv);
2075e5ceb11SJoseph Mingrone err(errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE,
2085e5ceb11SJoseph Mingrone "%s", *argv);
2096f6166e4SDag-Erling Smørgrav } else {
2106f6166e4SDag-Erling Smørgrav if (altwd)
2116f6166e4SDag-Erling Smørgrav errx(EXIT_CANCELED, "must specify command with -C");
2126f6166e4SDag-Erling Smørgrav if (altpath)
2136f6166e4SDag-Erling Smørgrav errx(EXIT_CANCELED, "must specify command with -P");
2149b50d902SRodney W. Grimes }
2159b50d902SRodney W. Grimes for (ep = environ; *ep; ep++)
2165e5ceb11SJoseph Mingrone (void)printf("%s%c", *ep, term);
217*c2d93a80SDag-Erling Smørgrav if (fflush(stdout) != 0)
218*c2d93a80SDag-Erling Smørgrav err(1, "stdout");
2199b50d902SRodney W. Grimes exit(0);
2209b50d902SRodney W. Grimes }
221d98fe205SPhilippe Charnier
222d98fe205SPhilippe Charnier static void
usage(void)223f4ac32deSDavid Malone usage(void)
224d98fe205SPhilippe Charnier {
225d98fe205SPhilippe Charnier (void)fprintf(stderr,
2266f6166e4SDag-Erling Smørgrav "usage: env [-0iv] [-C workdir] [-L|-U user[/class]] [-P utilpath] [-S string]\n"
2276f6166e4SDag-Erling Smørgrav " [-u name] [name=value ...] [utility [argument ...]]\n");
228d98fe205SPhilippe Charnier exit(1);
229d98fe205SPhilippe Charnier }
230