git: 046800313ab6 - main - bsdinstall: implement rootpass with bsddialog

From: Pierre Pronchery <khorben_at_FreeBSD.org>
Date: Fri, 30 May 2025 14:49:57 UTC
The branch main has been updated by khorben:

URL: https://cgit.FreeBSD.org/src/commit/?id=046800313ab64ea058fe7e63f56b6134d6cf00ef

commit 046800313ab64ea058fe7e63f56b6134d6cf00ef
Author:     Pierre Pronchery <khorben@FreeBSD.org>
AuthorDate: 2025-05-20 11:14:49 +0000
Commit:     Pierre Pronchery <khorben@FreeBSD.org>
CommitDate: 2025-05-30 14:45:48 +0000

    bsdinstall: implement rootpass with bsddialog
    
    This provides a more consistent user experience to the FreeBSD
    installer.
    
    Tested by:      thj
    Approved by:    philip (mentor)
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D44280
---
 usr.sbin/bsdinstall/scripts/rootpass | 90 ++++++++++++++++++++++++++++++++----
 1 file changed, 81 insertions(+), 9 deletions(-)

diff --git a/usr.sbin/bsdinstall/scripts/rootpass b/usr.sbin/bsdinstall/scripts/rootpass
index 93523a9880d3..9d25569ae946 100755
--- a/usr.sbin/bsdinstall/scripts/rootpass
+++ b/usr.sbin/bsdinstall/scripts/rootpass
@@ -1,6 +1,7 @@
 #!/bin/sh
 #-
 # Copyright (c) 2011 Nathan Whitehorn
+# Copyright (c) 2024 The FreeBSD Foundation
 # All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
@@ -27,18 +28,89 @@
 
 BSDCFG_SHARE="/usr/share/bsdconfig"
 . $BSDCFG_SHARE/common.subr || exit 1
-clear
-echo "$OSNAME Installer"
-echo "========================"
-echo
 
 if [ -n "$ROOTPASS_ENC" ]; then
 	printf '%s\n' "$ROOTPASS_ENC" | pw -R $BSDINSTALL_CHROOT usermod root -H 0
+	exit $?
 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
+	exit $?
 fi
+
+: ${BSDDIALOG_OK:=0}
+
+error_get_message()
+{
+	case $1 in
+	62)
+		echo "The password cannot be empty"
+		;;
+	63)
+		echo "The passwords do not match"
+		;;
+	64) #EX_USAGE
+		echo "Command used incorrectly"
+		;;
+	65) #EX_DATAERR
+		echo "Incorrect input data"
+		;;
+	67) #EX_NOUSER
+		echo "User not found"
+		;;
+	70) #EX_SOFTWARE
+		echo "Internal software error"
+		;;
+	71) #EX_OSERR
+		echo "Operating System error detected"
+		;;
+	72) #EX_OSFILE
+		echo "Error in a system file"
+		;;
+	74) #EX_IOERR
+		echo "I/O error"
+		;;
+	77) #EX_NOPERM
+		echo "Insufficient permissions"
+		;;
+	78) #EX_CONFIG
+		echo "Configuration error"
+		;;
+	0)
+		;;
+	*)
+		echo "An unknown error occurred (code $1)"
+		return 1
+		;;
+	esac
+	return $1
+}
+
+errormsg=
+username="root"
+while true; do
+	exec 5>&1
+	output=$(bsddialog --backtitle "$OSNAME Installer" \
+		--title "Set $username password" \
+		--cancel-label "Skip" \
+		--passwordform --insecure \
+		"Please select a password for the system management account ($username)
+$errormsg" \
+		0 0 2 \
+		"Password" 0 0 '' 0 17 32 32 \
+		"Repeat password" 1 0 '' 1 17 32 32 \
+		2>&1 1>&5)
+	res=$?
+	exec 5>&-
+	[ $res -eq $BSDDIALOG_OK ] || exit 0
+
+	echo -n "$output" | (read password1
+		read password2
+		[ -n "$password1" -o -n "$password2" ] || exit 62
+		[ "$password1" = "$password2" ] || exit 63
+		echo "$password1" | chroot $BSDINSTALL_CHROOT \
+			/usr/sbin/pw usermod "$username" -h 0
+	)
+	err=$?
+	[ $err -eq 0 ] && exit 0
+	errormsg=$(error_get_message $err)
+done