Page MenuHomeFreeBSD

D61930.1795483851.diff
No OneTemporary

Authored By
Unknown
Size
11 KB
Referenced Files
None
Subscribers
None

D61930.1795483851.diff

diff --git a/usr.sbin/sysrc/sysrc b/usr.sbin/sysrc/sysrc
--- a/usr.sbin/sysrc/sysrc
+++ b/usr.sbin/sysrc/sysrc
@@ -16,7 +16,7 @@
#
# Version information
#
-SYSRC_VERSION="9.3 Nov-18,2026"
+SYSRC_VERSION="9.4 Nov-22,2026"
#
# Options
@@ -27,7 +27,9 @@
DESCRIBE= # -d
EXISTING_ONLY= # -E
IGNORE_UNKNOWNS= # -i
+HUSER= # -u user
JAIL= # -j jail
+JUSER= # -U user
LIST_CONFS= # -l
LIST_SERVICE_CONFS= # -L
QUIET= # -q
@@ -147,6 +149,12 @@
"Process additional \`rc.conf.d' entries for service name."
f_err "$optfmt" "" \
"Ignored if \`-f file' is given."
+ f_err "$optfmt" "-U user" \
+ "Run as user inside the jail (jexec -U). Requires \`-j'."
+ f_err "$optfmt" "-u user" \
+ "Run as user from the host password database."
+ f_err "$optfmt" "" \
+ "Passed to jexec -u and chroot -u. Requires \`-j' or \`-R'."
f_err "$optfmt" "-v" \
"Verbose. Print the pathname of the specific rc.conf(5)"
f_err "$optfmt" "" \
@@ -252,7 +260,7 @@
#
# Process command-line flags
#
-while getopts AacDdEeFf:hij:LlNnqR:rs:vXx flag; do
+while getopts AacDdEeFf:hij:LlNnqR:rs:U:u:vXx flag; do
case "$flag" in
A) SHOW_ALL=2 ;;
a) SHOW_ALL=${SHOW_ALL:-1} ;;
@@ -280,6 +288,12 @@
s) [ "$OPTARG" ] ||
die "Missing or null argument to \`-s' flag"
SERVICE="$OPTARG" ;;
+ U) [ "$OPTARG" ] ||
+ die "Missing or null argument to \`-U' flag"
+ JUSER="$OPTARG" ;;
+ u) [ "$OPTARG" ] ||
+ die "Missing or null argument to \`-u' flag"
+ HUSER="$OPTARG" ;;
v) VERBOSE=1 QUIET= ;;
X) DELETE=2 ;;
x) DELETE=${DELETE:-1} ;;
@@ -525,6 +539,15 @@
#
# Process `-j jail' and `-R dir' command-line options
#
+if [ "$HUSER" -a "$JUSER" ]; then
+ die "-u and -U are mutually exclusive"
+fi
+if [ "$HUSER" -a ! "$JAIL" -a ! "$ROOTDIR" ]; then
+ die "-u requires \`-j jail' or \`-R dir'"
+fi
+if [ "$JUSER" -a ! "$JAIL" -a ! "$ROOTDIR" ]; then
+ die "-U requires \`-j jail'"
+fi
if [ "$JAIL" -o "$ROOTDIR" ]; then
#
# Reconstruct the arguments that we want to carry-over
@@ -572,12 +595,16 @@
if [ "$JAIL" ]; then
#
# Re-execute ourselves with sh(1) via jexec(8)
+ # NB: -U looked up in the jail password database, after attach
#
+ set -- "$JAIL"
+ [ "$HUSER" ] && set -- -u "$HUSER" "$@"
+ [ "$JUSER" ] && set -- -U "$JUSER" "$@"
( echo set -- $args
jail_depend
cat $0
) | env - RC_DEFAULTS="$RC_DEFAULTS" \
- /usr/sbin/jexec "$JAIL" /bin/sh
+ /usr/sbin/jexec "$@" /bin/sh
exit $?
elif [ "$ROOTDIR" ]; then
#
@@ -614,26 +641,39 @@
if [ "$jid" ]; then
#
# Re-execute outselves with sh(1) via jexec(8)
+ # NB: -U looked up in the jail, after attach
#
+ set -- "$jid"
+ [ "$HUSER" ] && set -- -u "$HUSER" "$@"
+ [ "$JUSER" ] && set -- -U "$JUSER" "$@"
( echo set -- $args
jail_depend
cat $0
) | env - RC_DEFAULTS="$RC_DEFAULTS" \
- /usr/sbin/jexec "$jid" /bin/sh
+ /usr/sbin/jexec "$@" /bin/sh
exit $?
fi
# Otherwise, fall through and allow chroot(8)
fi
+ #
+ # -U is the jail password lookup. chroot(8) has no equivalent.
+ #
+ if [ "$JUSER" ]; then
+ die "-U requires a running jail (use \`-j jail')"
+ fi
+
#
# Re-execute ourselves with sh(1) via chroot(8)
#
+ set -- "$ROOTDIR"
+ [ "$HUSER" ] && set -- -u "$HUSER" "$@"
( echo set -- $args
jail_depend
cat $0
) | env - RC_DEFAULTS="$RC_DEFAULTS" \
- /usr/sbin/chroot "$ROOTDIR" /bin/sh
+ /usr/sbin/chroot "$@" /bin/sh
exit $?
fi
fi
diff --git a/usr.sbin/sysrc/sysrc.8 b/usr.sbin/sysrc/sysrc.8
--- a/usr.sbin/sysrc/sysrc.8
+++ b/usr.sbin/sysrc/sysrc.8
@@ -25,7 +25,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd November 18, 2026
+.Dd November 22, 2026
.Dt SYSRC 8
.Os
.Sh NAME
@@ -37,6 +37,7 @@
.Op Fl f Ar file
.Op Fl j Ar jail | Fl R Ar dir
.Op Fl s Ar name
+.Op Fl u Ar user | Fl U Ar user
.Ar name Ns Op Ns Oo +|-|. Oc Ns = Ns Ar value
.Ar ...
.Nm
@@ -44,6 +45,7 @@
.Op Fl f Ar file
.Op Fl j Ar jail | Fl R Ar dir
.Op Fl s Ar name
+.Op Fl u Ar user | Fl U Ar user
.Fl A | a
.Nm
.Op Fl E
@@ -178,6 +180,34 @@
Can be combined with
.Sq Fl l
to list configuration files used by service at startup.
+.It Fl U Ar user
+Run inside the jail as
+.Ar user .
+The name is looked up in the jail password database after attach
+.Pq Xr jexec 8 Fl U .
+Requires
+.Fl j ,
+or
+.Fl R
+pointing at a running jail.
+Cannot be combined with
+.Fl u .
+.It Fl u Ar user
+Run as
+.Ar user
+from the host password database.
+Passed to
+.Xr jexec 8
+and
+.Xr chroot 8
+as
+.Fl u .
+Requires
+.Fl j
+or
+.Fl R .
+Cannot be combined with
+.Fl U .
.It Fl v
Verbose.
Print the pathname of the specific
diff --git a/usr.sbin/sysrc/tests/sysrc_test.sh b/usr.sbin/sysrc/tests/sysrc_test.sh
--- a/usr.sbin/sysrc/tests/sysrc_test.sh
+++ b/usr.sbin/sysrc/tests/sysrc_test.sh
@@ -571,6 +571,201 @@
atf_check -o file:expected cat "${SYSRC_CONF}"
}
+# Populate root with sh(1) and the tools a sysrc assignment executes,
+# plus the shared libraries those programs need.
+sysrc_mkroot()
+{
+ local root="$1"
+ local name path lib
+
+ shift # root
+ atf_check mkdir -p "$root/bin" "$root/tmp" "$root/etc" \
+ "$root/libexec"
+ atf_check chmod 1777 "$root/tmp"
+ atf_check cp /libexec/ld-elf.so.1 "$root/libexec/ld-elf.so.1"
+ for name in "$@"; do
+ path=$( command -v "$name" ) || atf_fail "$name not found"
+ atf_check mkdir -p "$root${path%/*}"
+ atf_check cp "$path" "$root$path"
+ for lib in $( ldd -f '%p\n' "$path" | grep '^/' ); do
+ atf_check mkdir -p "$root${lib%/*}"
+ atf_check cp "$lib" "$root$lib"
+ done
+ done
+}
+
+# A user that exists only in this root's password database.
+sysrc_mkuser()
+{
+ local root="$1" name="$2" uid="$3"
+
+ printf '%s\n' "$name:*:$uid:$uid::0:0:sysrc test:/tmp:/bin/sh" \
+ > "$root/etc/master.passwd"
+ atf_check chmod 600 "$root/etc/master.passwd"
+ printf '%s\n' "$name:*:$uid:" > "$root/etc/group"
+ atf_check cp /etc/login.conf "$root/etc/login.conf"
+ atf_check pwd_mkdb -d "$root/etc" "$root/etc/master.passwd"
+}
+
+sysrc_jail_cleanup()
+{
+ local name
+
+ [ -f jail.name ] || return 0
+ name=$( cat jail.name )
+ # jail -r drops the prison and leaves mount.devfs mounted
+ jls -j "$name" > /dev/null 2>&1 && jail -r "$name"
+ sysrc_dev_cleanup
+}
+
+# The re-exec redirects to /dev/null. A chroot has no devfs of its own
+sysrc_mount_dev()
+{
+ local root="$1"
+
+ atf_check mkdir -p "$root/dev"
+ printf '%s\n' "$root/dev" > dev.mount
+ atf_check mount -t devfs devfs "$root/dev"
+}
+
+sysrc_dev_cleanup()
+{
+ local mnt
+
+ [ -f dev.mount ] || return 0
+ mnt=$( cat dev.mount )
+ mount -t devfs | grep -F " on $mnt (" > /dev/null 2>&1 || return 0
+ umount "$mnt"
+}
+
+atf_test_case u_flag_chroot cleanup
+u_flag_chroot_head()
+{
+ atf_set "descr" "-u drops to the host user under chroot"
+ atf_set "require.user" "root"
+}
+u_flag_chroot_body()
+{
+ local root="$PWD/root"
+
+ sysrc_mkroot "$root" sh awk grep mktemp mv chmod chown stat
+ sysrc_mount_dev "$root"
+ printf '%s\n' 'test_enable="NO"' > "$root/etc/rc.conf"
+ # rename(2) needs a writable directory, not only a writable file
+ atf_check chown nobody:nobody "$root/etc" "$root/etc/rc.conf"
+
+ # -U is the jail database. chroot has no equivalent
+ printf '%s\n' \
+ "sysrc: -U requires a running jail (use \`-j jail')" \
+ > denied
+ atf_check -s exit:1 -e file:denied \
+ sysrc -U nobody -R "$root" -f /etc/rc.conf test_enable=YES
+ atf_check -o inline:'test_enable="NO"\n' cat "$root/etc/rc.conf"
+
+ atf_check -o inline:"test_enable: NO -> YES\n" \
+ sysrc -u nobody -R "$root" -f /etc/rc.conf test_enable=YES
+ atf_check -o inline:'test_enable="YES"\n' cat "$root/etc/rc.conf"
+
+ # The re-exec is piped into sh, so the program name is sh
+ # Readable so sh -n runs; not writable, so the assignment is refused
+ printf '%s\n' 'test_enable="NO"' > "$root/etc/rc.conf"
+ atf_check chown root:wheel "$root/etc/rc.conf"
+ atf_check chmod 644 "$root/etc/rc.conf"
+ printf '%s\n' "sh: cannot create /etc/rc.conf: Permission denied" \
+ > denied
+ atf_check -s exit:1 -e file:denied \
+ sysrc -u nobody -R "$root" -f /etc/rc.conf test_enable=YES
+ atf_check -o inline:'test_enable="NO"\n' cat "$root/etc/rc.conf"
+}
+u_flag_chroot_cleanup()
+{
+ sysrc_dev_cleanup
+}
+
+atf_test_case u_flag_jail cleanup
+u_flag_jail_head()
+{
+ atf_set "descr" "-u drops to the host user under jexec"
+ atf_set "require.user" "root"
+}
+u_flag_jail_body()
+{
+ local root="$PWD/root" name="sysrc_u_$$"
+
+ # A nested jail cannot be created here
+ [ "$( sysctl -n security.jail.jailed )" = 0 ] ||
+ atf_skip "cannot create a jail from inside a jail"
+ sysrc_mkroot "$root" sh awk grep mktemp mv chmod chown stat
+ printf '%s\n' 'test_enable="NO"' > "$root/etc/rc.conf"
+ # rename(2) needs a writable directory, not only a writable file
+ atf_check chown nobody:nobody "$root/etc" "$root/etc/rc.conf"
+ printf '%s\n' "$name" > jail.name
+ atf_check mkdir -p "$root/dev"
+ printf '%s\n' "$root/dev" > dev.mount
+ atf_check -o ignore jail -c name="$name" path="$root" \
+ host.hostname="$name" ip4=disable ip6=disable \
+ mount.devfs persist
+ atf_check -o inline:"test_enable: NO -> YES\n" \
+ sysrc -u nobody -j "$name" -f /etc/rc.conf test_enable=YES
+ atf_check -o inline:'test_enable="YES"\n' cat "$root/etc/rc.conf"
+
+ # Readable so sh -n runs; not writable, so the assignment is refused
+ printf '%s\n' 'test_enable="NO"' > "$root/etc/rc.conf"
+ atf_check chown root:wheel "$root/etc/rc.conf"
+ atf_check chmod 644 "$root/etc/rc.conf"
+ printf '%s\n' "sh: cannot create /etc/rc.conf: Permission denied" \
+ > denied
+ atf_check -s exit:1 -e file:denied \
+ sysrc -u nobody -j "$name" -f /etc/rc.conf test_enable=YES
+ atf_check -o inline:'test_enable="NO"\n' cat "$root/etc/rc.conf"
+}
+u_flag_jail_cleanup()
+{
+ sysrc_jail_cleanup
+}
+
+atf_test_case U_flag_jail cleanup
+U_flag_jail_head()
+{
+ atf_set "descr" "-U uses the jail password database"
+ atf_set "require.user" "root"
+}
+U_flag_jail_body()
+{
+ local root="$PWD/root" name="sysrc_U_$$" user="sysrctest"
+
+ # A nested jail cannot be created here
+ [ "$( sysctl -n security.jail.jailed )" = 0 ] ||
+ atf_skip "cannot create a jail from inside a jail"
+ id "$user" > /dev/null 2>&1 &&
+ atf_skip "$user exists on the host"
+ sysrc_mkroot "$root" sh awk grep mktemp mv chmod chown stat
+ sysrc_mkuser "$root" "$user" 9999
+ printf '%s\n' 'test_enable="NO"' > "$root/etc/rc.conf"
+ # rename(2) needs a writable directory, not only a writable file
+ atf_check chown 9999:9999 "$root/etc" "$root/etc/rc.conf"
+ printf '%s\n' "$name" > jail.name
+ atf_check mkdir -p "$root/dev"
+ printf '%s\n' "$root/dev" > dev.mount
+ atf_check -o ignore jail -c name="$name" path="$root" \
+ host.hostname="$name" ip4=disable ip6=disable \
+ mount.devfs persist
+
+ # -u is the host database. This name is not in it
+ printf '%s\n' "jexec: $user: no such user" > denied
+ atf_check -s exit:1 -e file:denied \
+ sysrc -u "$user" -j "$name" -f /etc/rc.conf test_enable=YES
+ atf_check -o inline:'test_enable="NO"\n' cat "$root/etc/rc.conf"
+
+ atf_check -o inline:"test_enable: NO -> YES\n" \
+ sysrc -U "$user" -j "$name" -f /etc/rc.conf test_enable=YES
+ atf_check -o inline:'test_enable="YES"\n' cat "$root/etc/rc.conf"
+}
+U_flag_jail_cleanup()
+{
+ sysrc_jail_cleanup
+}
+
atf_init_test_cases()
{
atf_add_test_case check_variable
@@ -590,4 +785,7 @@
atf_add_test_case f_flag
atf_add_test_case F_flag
atf_add_test_case r_flag
+ atf_add_test_case u_flag_chroot
+ atf_add_test_case u_flag_jail
+ atf_add_test_case U_flag_jail
}

File Metadata

Mime Type
text/plain
Expires
Tue, Nov 24, 1:30 AM (23 h, 17 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41944143
Default Alt Text
D61930.1795483851.diff (11 KB)

Event Timeline