xref: /src/usr.sbin/bsdinstall/scripts/rootpass (revision 046800313ab64ea058fe7e63f56b6134d6cf00ef)
12118f387SNathan Whitehorn#!/bin/sh
22118f387SNathan Whitehorn#-
32118f387SNathan Whitehorn# Copyright (c) 2011 Nathan Whitehorn
404680031SPierre Pronchery# Copyright (c) 2024 The FreeBSD Foundation
52118f387SNathan Whitehorn# All rights reserved.
62118f387SNathan Whitehorn#
72118f387SNathan Whitehorn# Redistribution and use in source and binary forms, with or without
82118f387SNathan Whitehorn# modification, are permitted provided that the following conditions
92118f387SNathan Whitehorn# are met:
102118f387SNathan Whitehorn# 1. Redistributions of source code must retain the above copyright
112118f387SNathan Whitehorn#    notice, this list of conditions and the following disclaimer.
122118f387SNathan Whitehorn# 2. Redistributions in binary form must reproduce the above copyright
132118f387SNathan Whitehorn#    notice, this list of conditions and the following disclaimer in the
142118f387SNathan Whitehorn#    documentation and/or other materials provided with the distribution.
152118f387SNathan Whitehorn#
162118f387SNathan Whitehorn# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172118f387SNathan Whitehorn# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182118f387SNathan Whitehorn# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192118f387SNathan Whitehorn# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202118f387SNathan Whitehorn# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212118f387SNathan Whitehorn# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222118f387SNathan Whitehorn# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232118f387SNathan Whitehorn# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242118f387SNathan Whitehorn# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252118f387SNathan Whitehorn# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262118f387SNathan Whitehorn# SUCH DAMAGE.
272118f387SNathan Whitehorn#
282118f387SNathan Whitehorn
29cc42ef53SBrad DavisBSDCFG_SHARE="/usr/share/bsdconfig"
30cc42ef53SBrad Davis. $BSDCFG_SHARE/common.subr || exit 1
312118f387SNathan Whitehorn
323e1101f2SBrad Davisif [ -n "$ROOTPASS_ENC" ]; then
333e1101f2SBrad Davis	printf '%s\n' "$ROOTPASS_ENC" | pw -R $BSDINSTALL_CHROOT usermod root -H 0
3404680031SPierre Pronchery	exit $?
353e1101f2SBrad Daviselif [ -n "$ROOTPASS_PLAIN" ]; then
363e1101f2SBrad Davis	printf '%s\n' "$ROOTPASS_PLAIN" | pw -R $BSDINSTALL_CHROOT usermod root -h 0
3704680031SPierre Pronchery	exit $?
383e1101f2SBrad Davisfi
3904680031SPierre Pronchery
4004680031SPierre Pronchery: ${BSDDIALOG_OK:=0}
4104680031SPierre Pronchery
4204680031SPierre Proncheryerror_get_message()
4304680031SPierre Pronchery{
4404680031SPierre Pronchery	case $1 in
4504680031SPierre Pronchery	62)
4604680031SPierre Pronchery		echo "The password cannot be empty"
4704680031SPierre Pronchery		;;
4804680031SPierre Pronchery	63)
4904680031SPierre Pronchery		echo "The passwords do not match"
5004680031SPierre Pronchery		;;
5104680031SPierre Pronchery	64) #EX_USAGE
5204680031SPierre Pronchery		echo "Command used incorrectly"
5304680031SPierre Pronchery		;;
5404680031SPierre Pronchery	65) #EX_DATAERR
5504680031SPierre Pronchery		echo "Incorrect input data"
5604680031SPierre Pronchery		;;
5704680031SPierre Pronchery	67) #EX_NOUSER
5804680031SPierre Pronchery		echo "User not found"
5904680031SPierre Pronchery		;;
6004680031SPierre Pronchery	70) #EX_SOFTWARE
6104680031SPierre Pronchery		echo "Internal software error"
6204680031SPierre Pronchery		;;
6304680031SPierre Pronchery	71) #EX_OSERR
6404680031SPierre Pronchery		echo "Operating System error detected"
6504680031SPierre Pronchery		;;
6604680031SPierre Pronchery	72) #EX_OSFILE
6704680031SPierre Pronchery		echo "Error in a system file"
6804680031SPierre Pronchery		;;
6904680031SPierre Pronchery	74) #EX_IOERR
7004680031SPierre Pronchery		echo "I/O error"
7104680031SPierre Pronchery		;;
7204680031SPierre Pronchery	77) #EX_NOPERM
7304680031SPierre Pronchery		echo "Insufficient permissions"
7404680031SPierre Pronchery		;;
7504680031SPierre Pronchery	78) #EX_CONFIG
7604680031SPierre Pronchery		echo "Configuration error"
7704680031SPierre Pronchery		;;
7804680031SPierre Pronchery	0)
7904680031SPierre Pronchery		;;
8004680031SPierre Pronchery	*)
8104680031SPierre Pronchery		echo "An unknown error occurred (code $1)"
8204680031SPierre Pronchery		return 1
8304680031SPierre Pronchery		;;
8404680031SPierre Pronchery	esac
8504680031SPierre Pronchery	return $1
8604680031SPierre Pronchery}
8704680031SPierre Pronchery
8804680031SPierre Proncheryerrormsg=
8904680031SPierre Proncheryusername="root"
9004680031SPierre Proncherywhile true; do
9104680031SPierre Pronchery	exec 5>&1
9204680031SPierre Pronchery	output=$(bsddialog --backtitle "$OSNAME Installer" \
9304680031SPierre Pronchery		--title "Set $username password" \
9404680031SPierre Pronchery		--cancel-label "Skip" \
9504680031SPierre Pronchery		--passwordform --insecure \
9604680031SPierre Pronchery		"Please select a password for the system management account ($username)
9704680031SPierre Pronchery$errormsg" \
9804680031SPierre Pronchery		0 0 2 \
9904680031SPierre Pronchery		"Password" 0 0 '' 0 17 32 32 \
10004680031SPierre Pronchery		"Repeat password" 1 0 '' 1 17 32 32 \
10104680031SPierre Pronchery		2>&1 1>&5)
10204680031SPierre Pronchery	res=$?
10304680031SPierre Pronchery	exec 5>&-
10404680031SPierre Pronchery	[ $res -eq $BSDDIALOG_OK ] || exit 0
10504680031SPierre Pronchery
10604680031SPierre Pronchery	echo -n "$output" | (read password1
10704680031SPierre Pronchery		read password2
10804680031SPierre Pronchery		[ -n "$password1" -o -n "$password2" ] || exit 62
10904680031SPierre Pronchery		[ "$password1" = "$password2" ] || exit 63
11004680031SPierre Pronchery		echo "$password1" | chroot $BSDINSTALL_CHROOT \
11104680031SPierre Pronchery			/usr/sbin/pw usermod "$username" -h 0
11204680031SPierre Pronchery	)
11304680031SPierre Pronchery	err=$?
11404680031SPierre Pronchery	[ $err -eq 0 ] && exit 0
11504680031SPierre Pronchery	errormsg=$(error_get_message $err)
11604680031SPierre Proncherydone
117