env(1): grow -L user/class and -U user/class options

This allows one to set the environment of the specified user either from
login.conf alone (-L) or both login.conf and ~/.login_conf if present (-U).

This is a supporting feature to allow service(8) to pull in the environment
of the "daemon" class before invoking the rc script.

This is a part of D21481.

Submitted by:	Andrew Gierth < andrew_tao173.riddles.org.uk>
This commit is contained in:
Kyle Evans 2020-02-05 04:29:55 +00:00
parent 21c1a93c04
commit 85c8521e67
3 changed files with 100 additions and 5 deletions

View File

@ -4,4 +4,6 @@
PROG= env PROG= env
SRCS= env.c envopts.c SRCS= env.c envopts.c
LIBADD= util
.include <bsd.prog.mk> .include <bsd.prog.mk>

34
usr.bin/env/env.1 vendored
View File

@ -31,7 +31,7 @@
.\" From FreeBSD: src/usr.bin/printenv/printenv.1,v 1.17 2002/11/26 17:33:35 ru Exp .\" From FreeBSD: src/usr.bin/printenv/printenv.1,v 1.17 2002/11/26 17:33:35 ru Exp
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd November 7, 2019 .Dd January 19, 2020
.Dt ENV 1 .Dt ENV 1
.Os .Os
.Sh NAME .Sh NAME
@ -40,6 +40,7 @@
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl 0iv .Op Fl 0iv
.Op Fl L Ns | Ns Fl U Ar user Ns Op / Ns Ar class
.Op Fl P Ar altpath .Op Fl P Ar altpath
.Op Fl S Ar string .Op Fl S Ar string
.Op Fl u Ar name .Op Fl u Ar name
@ -76,6 +77,28 @@ The environment inherited
by by
.Nm .Nm
is ignored completely. is ignored completely.
.\" -L | -U
.It Fl L | Fl U Ar user Ns Op / Ns Ar class
Add the environment variable definitions from
.Xr login.conf 5
for the specified user and login class to the environment, after
processing any
.Fl i
or
.Fl u
options, but before processing any
.Ar name Ns = Ns Ar value
options.
If
.Fl L
is used, only the system-wide
.Pa /etc/login.conf.db
file is read; if
.Fl U
is used, then the specified user's
.Pa ~/.login_conf
is read as well.
The user may be specified by name or by uid.
.\" -P .\" -P
.It Fl P Ar altpath .It Fl P Ar altpath
Search the set of directories as specified by Search the set of directories as specified by
@ -450,6 +473,7 @@ option as a synonym for
.Xr printenv 1 , .Xr printenv 1 ,
.Xr sh 1 , .Xr sh 1 ,
.Xr execvp 3 , .Xr execvp 3 ,
.Xr login.conf 5 ,
.Xr environ 7 .Xr environ 7
.Sh STANDARDS .Sh STANDARDS
The The
@ -457,7 +481,7 @@ The
utility conforms to utility conforms to
.St -p1003.1-2001 . .St -p1003.1-2001 .
The The
.Fl P , S , u .Fl 0 , L , P , S , U , u
and and
.Fl v .Fl v
options are non-standard extensions supported by options are non-standard extensions supported by
@ -474,6 +498,12 @@ and
.Fl v .Fl v
options were added in options were added in
.Fx 6.0 . .Fx 6.0 .
The
.Fl 0 , L
and
.Fl U
options were added in
.Fx 13.0 .
.Sh BUGS .Sh BUGS
The The
.Nm .Nm

69
usr.bin/env/env.c vendored
View File

@ -44,11 +44,16 @@ static char sccsid[] = "@(#)env.c 8.3 (Berkeley) 4/2/94";
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD$"); __FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <err.h> #include <err.h>
#include <errno.h> #include <errno.h>
#include <login_cap.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include "envopts.h" #include "envopts.h"
@ -71,13 +76,23 @@ main(int argc, char **argv)
{ {
char *altpath, **ep, *p, **parg, term; char *altpath, **ep, *p, **parg, term;
char *cleanenv[1]; char *cleanenv[1];
char *login_class, *login_name;
struct passwd *pw;
login_cap_t *lc;
bool login_as_user;
uid_t uid;
int ch, want_clear; int ch, want_clear;
int rtrn; int rtrn;
altpath = NULL; altpath = NULL;
login_class = NULL;
login_name = NULL;
pw = NULL;
lc = NULL;
login_as_user = false;
want_clear = 0; want_clear = 0;
term = '\n'; term = '\n';
while ((ch = getopt(argc, argv, "-0iP:S:u:v")) != -1) while ((ch = getopt(argc, argv, "-0iL:P:S:U:u:v")) != -1)
switch(ch) { switch(ch) {
case '-': case '-':
case 'i': case 'i':
@ -86,6 +101,12 @@ main(int argc, char **argv)
case '0': case '0':
term = '\0'; term = '\0';
break; break;
case 'U':
login_as_user = true;
/* FALLTHROUGH */
case 'L':
login_name = optarg;
break;
case 'P': case 'P':
altpath = strdup(optarg); altpath = strdup(optarg);
break; break;
@ -119,6 +140,48 @@ main(int argc, char **argv)
if (env_verbosity) if (env_verbosity)
fprintf(stderr, "#env clearing environ\n"); fprintf(stderr, "#env clearing environ\n");
} }
if (login_name != NULL) {
login_class = strchr(login_name, '/');
if (login_class)
*login_class++ = '\0';
pw = getpwnam(login_name);
if (pw == NULL) {
char *endp = NULL;
errno = 0;
uid = strtoul(login_name, &endp, 10);
if (errno == 0 && *endp == '\0')
pw = getpwuid(uid);
}
if (pw == NULL)
errx(EXIT_FAILURE, "no such user: %s", login_name);
if (login_class != NULL) {
lc = login_getclass(login_class);
if (lc == NULL)
errx(EXIT_FAILURE, "no such login class: %s",
login_class);
} else {
lc = login_getpwclass(pw);
if (lc == NULL)
errx(EXIT_FAILURE, "login_getpwclass failed");
}
/*
* This is not done with setusercontext() because that will
* try and use ~/.login_conf even when we don't want it to.
*/
setclassenvironment(lc, pw, 1);
setclassenvironment(lc, pw, 0);
if (login_as_user) {
login_close(lc);
if ((lc = login_getuserclass(pw)) != NULL) {
setclassenvironment(lc, pw, 1);
setclassenvironment(lc, pw, 0);
}
}
endpwent();
if (lc != NULL)
login_close(lc);
}
for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) { for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) {
if (env_verbosity) if (env_verbosity)
fprintf(stderr, "#env setenv:\t%s\n", *argv); fprintf(stderr, "#env setenv:\t%s\n", *argv);
@ -154,7 +217,7 @@ static void
usage(void) usage(void)
{ {
(void)fprintf(stderr, (void)fprintf(stderr,
"usage: env [-0iv] [-P utilpath] [-S string] [-u name]\n" "usage: env [-0iv] [-L|-U user[/class]] [-P utilpath] [-S string] [-u name]\n"
" [name=value ...] [utility [argument ...]]\n"); " [name=value ...] [utility [argument ...]]\n");
exit(1); exit(1);
} }