git: 3e1101f29b4f - main - bsdinstall: allow setting the root password via env variables

From: Brad Davis <brd_at_FreeBSD.org>
Date: Fri, 05 Aug 2022 15:10:53 UTC
The branch main has been updated by brd:

URL: https://cgit.FreeBSD.org/src/commit/?id=3e1101f29b4ff39a67eb10a5b41b727d8702f0f5

commit 3e1101f29b4ff39a67eb10a5b41b727d8702f0f5
Author:     Brad Davis <brd@FreeBSD.org>
AuthorDate: 2022-08-05 15:10:21 +0000
Commit:     Brad Davis <brd@FreeBSD.org>
CommitDate: 2022-08-05 15:10:21 +0000

    bsdinstall: allow setting the root password via env variables
    
    Reviewed by:    0mp, allanjude, asiciliano, dteske, pauamma, rpokala, sef
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
    Differential Revision: https://reviews.freebsd.org/D35588
---
 usr.sbin/bsdinstall/bsdinstall.8     |  9 +++++++++
 usr.sbin/bsdinstall/scripts/rootpass | 12 +++++++++---
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/usr.sbin/bsdinstall/bsdinstall.8 b/usr.sbin/bsdinstall/bsdinstall.8
index c71097fde7f8..b76cdc04b9c3 100644
--- a/usr.sbin/bsdinstall/bsdinstall.8
+++ b/usr.sbin/bsdinstall/bsdinstall.8
@@ -343,6 +343,15 @@ target is executed.
 If this directory does not already exist, it will be created.
 Default:
 .Dq Pa $TMPDIR/bsdinstall_boot
+.It Ev ROOTPASS_ENC
+Encrypted string to set the root password to in the format expected by
+.Xr pw 8
+.Fl H Ar 0 .
+This option is used if both it and
+.Ev ROOTPASS_PLAIN
+are set.
+.It Ev ROOTPASS_PLAIN
+Plain text string to set the root password to.
 .It Ev ZFSBOOT_POOL_NAME
 Name for the pool containing the base system.
 Default:
diff --git a/usr.sbin/bsdinstall/scripts/rootpass b/usr.sbin/bsdinstall/scripts/rootpass
index 308c60e47a4c..f823c397c384 100755
--- a/usr.sbin/bsdinstall/scripts/rootpass
+++ b/usr.sbin/bsdinstall/scripts/rootpass
@@ -33,7 +33,13 @@ echo "$OSNAME Installer"
 echo "========================"
 echo
 
-echo "Please select a password for the system management account (root):"
-echo "Typed characters will not be visible."
+if [ -n "$ROOTPASS_ENC" ]; then
+	printf '%s\n' "$ROOTPASS_ENC" | pw -R $BSDINSTALL_CHROOT usermod root -H 0
+elif [ -n "$ROOTPASS_PLAIN" ]; then
+	printf '%s\n' "$ROOTPASS_PLAIN" | pw -R $BSDINSTALL_CHROOT usermod root -h 0
+else
+	echo "Please select a password for the system management account (root):"
+	echo "Typed characters will not be visible."
 
-chroot $BSDINSTALL_CHROOT passwd root 2>&1
+	chroot $BSDINSTALL_CHROOT passwd root 2>&1
+fi