Skip to content

Commit

Permalink
service(8): use an environment more consistent with init(8)
Browse files Browse the repository at this point in the history
init(8) sets the "daemon" login class without specifying a pw
entry (so no substitutions are done on the variables). service(8)'s
use of env -L had the effect of specifying root's pw entry, with two
effects: getpwnam and getpwuid are being called, which may not be
entirely safe depending on what nsswitch is up to and what stage of
boot we are at, and substitutions would have been done.

Fix by teaching env(8) to allow -L -/classname to set the class
environment with no pw entry at all specified, and use it in
service(8).

PR:		253959

(cherry picked from commit 55deb0a)
(cherry picked from commit 0c1a5ea)
  • Loading branch information
RhodiumToad authored and kevans91 committed Mar 7, 2021
1 parent bdd61b6 commit 872ec7e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
7 changes: 6 additions & 1 deletion usr.bin/env/env.1
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
.\" From FreeBSD: src/usr.bin/printenv/printenv.1,v 1.17 2002/11/26 17:33:35 ru Exp
.\" $FreeBSD$
.\"
.Dd November 11, 2020
.Dd March 3, 2021
.Dt ENV 1
.Os
.Sh NAME
Expand Down Expand Up @@ -104,6 +104,11 @@ is used, then the specified user's
.Pa ~/.login_conf
is read as well.
The user may be specified by name or by uid.
If a username of
.Sq Li \&-
is given, then no user lookup will be done, the login class will default to
.Sq Li default
if not explicitly given, and no substitutions will be done on the values.
.\" -P
.It Fl P Ar altpath
Search the set of directories as specified by
Expand Down
25 changes: 16 additions & 9 deletions usr.bin/env/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,23 @@ main(int argc, char **argv)
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 (*login_name != '\0' && strcmp(login_name, "-") != 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 (pw == NULL)
errx(EXIT_FAILURE, "no such user: %s", login_name);
/*
* Note that it is safe for pw to be null here; the libutil
* code handles that, bypassing substitution of $ and using
* the class "default" if no class name is given either.
*/
if (login_class != NULL) {
lc = login_getclass(login_class);
if (lc == NULL)
Expand Down
2 changes: 1 addition & 1 deletion usr.sbin/service/service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ cd /
for dir in /etc/rc.d $local_startup; do
if [ -x "$dir/$script" ]; then
[ -n "$VERBOSE" ] && echo "$script is located in $dir"
exec env -i -L 0/daemon HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin "$dir/$script" "$@"
exec env -i -L -/daemon HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin "$dir/$script" "$@"
fi
done

Expand Down

0 comments on commit 872ec7e

Please sign in to comment.