xref: /freebsd/usr.sbin/bsdinstall/scripts/rootpass (revision 046800313ab64ea058fe7e63f56b6134d6cf00ef)
1#!/bin/sh
2#-
3# Copyright (c) 2011 Nathan Whitehorn
4# Copyright (c) 2024 The FreeBSD Foundation
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29BSDCFG_SHARE="/usr/share/bsdconfig"
30. $BSDCFG_SHARE/common.subr || exit 1
31
32if [ -n "$ROOTPASS_ENC" ]; then
33	printf '%s\n' "$ROOTPASS_ENC" | pw -R $BSDINSTALL_CHROOT usermod root -H 0
34	exit $?
35elif [ -n "$ROOTPASS_PLAIN" ]; then
36	printf '%s\n' "$ROOTPASS_PLAIN" | pw -R $BSDINSTALL_CHROOT usermod root -h 0
37	exit $?
38fi
39
40: ${BSDDIALOG_OK:=0}
41
42error_get_message()
43{
44	case $1 in
45	62)
46		echo "The password cannot be empty"
47		;;
48	63)
49		echo "The passwords do not match"
50		;;
51	64) #EX_USAGE
52		echo "Command used incorrectly"
53		;;
54	65) #EX_DATAERR
55		echo "Incorrect input data"
56		;;
57	67) #EX_NOUSER
58		echo "User not found"
59		;;
60	70) #EX_SOFTWARE
61		echo "Internal software error"
62		;;
63	71) #EX_OSERR
64		echo "Operating System error detected"
65		;;
66	72) #EX_OSFILE
67		echo "Error in a system file"
68		;;
69	74) #EX_IOERR
70		echo "I/O error"
71		;;
72	77) #EX_NOPERM
73		echo "Insufficient permissions"
74		;;
75	78) #EX_CONFIG
76		echo "Configuration error"
77		;;
78	0)
79		;;
80	*)
81		echo "An unknown error occurred (code $1)"
82		return 1
83		;;
84	esac
85	return $1
86}
87
88errormsg=
89username="root"
90while true; do
91	exec 5>&1
92	output=$(bsddialog --backtitle "$OSNAME Installer" \
93		--title "Set $username password" \
94		--cancel-label "Skip" \
95		--passwordform --insecure \
96		"Please select a password for the system management account ($username)
97$errormsg" \
98		0 0 2 \
99		"Password" 0 0 '' 0 17 32 32 \
100		"Repeat password" 1 0 '' 1 17 32 32 \
101		2>&1 1>&5)
102	res=$?
103	exec 5>&-
104	[ $res -eq $BSDDIALOG_OK ] || exit 0
105
106	echo -n "$output" | (read password1
107		read password2
108		[ -n "$password1" -o -n "$password2" ] || exit 62
109		[ "$password1" = "$password2" ] || exit 63
110		echo "$password1" | chroot $BSDINSTALL_CHROOT \
111			/usr/sbin/pw usermod "$username" -h 0
112	)
113	err=$?
114	[ $err -eq 0 ] && exit 0
115	errormsg=$(error_get_message $err)
116done
117