xref: /freebsd/usr.sbin/bsdinstall/scripts/rootpass (revision 046800313ab64ea058fe7e63f56b6134d6cf00ef)
12118f387SNathan Whitehorn#!/bin/sh
22118f387SNathan Whitehorn#-
32118f387SNathan Whitehorn# Copyright (c) 2011 Nathan Whitehorn
4*04680031SPierre 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
34*04680031SPierre Pronchery	exit $?
353e1101f2SBrad Daviselif [ -n "$ROOTPASS_PLAIN" ]; then
363e1101f2SBrad Davis	printf '%s\n' "$ROOTPASS_PLAIN" | pw -R $BSDINSTALL_CHROOT usermod root -h 0
37*04680031SPierre Pronchery	exit $?
383e1101f2SBrad Davisfi
39*04680031SPierre Pronchery
40*04680031SPierre Pronchery: ${BSDDIALOG_OK:=0}
41*04680031SPierre Pronchery
42*04680031SPierre Proncheryerror_get_message()
43*04680031SPierre Pronchery{
44*04680031SPierre Pronchery	case $1 in
45*04680031SPierre Pronchery	62)
46*04680031SPierre Pronchery		echo "The password cannot be empty"
47*04680031SPierre Pronchery		;;
48*04680031SPierre Pronchery	63)
49*04680031SPierre Pronchery		echo "The passwords do not match"
50*04680031SPierre Pronchery		;;
51*04680031SPierre Pronchery	64) #EX_USAGE
52*04680031SPierre Pronchery		echo "Command used incorrectly"
53*04680031SPierre Pronchery		;;
54*04680031SPierre Pronchery	65) #EX_DATAERR
55*04680031SPierre Pronchery		echo "Incorrect input data"
56*04680031SPierre Pronchery		;;
57*04680031SPierre Pronchery	67) #EX_NOUSER
58*04680031SPierre Pronchery		echo "User not found"
59*04680031SPierre Pronchery		;;
60*04680031SPierre Pronchery	70) #EX_SOFTWARE
61*04680031SPierre Pronchery		echo "Internal software error"
62*04680031SPierre Pronchery		;;
63*04680031SPierre Pronchery	71) #EX_OSERR
64*04680031SPierre Pronchery		echo "Operating System error detected"
65*04680031SPierre Pronchery		;;
66*04680031SPierre Pronchery	72) #EX_OSFILE
67*04680031SPierre Pronchery		echo "Error in a system file"
68*04680031SPierre Pronchery		;;
69*04680031SPierre Pronchery	74) #EX_IOERR
70*04680031SPierre Pronchery		echo "I/O error"
71*04680031SPierre Pronchery		;;
72*04680031SPierre Pronchery	77) #EX_NOPERM
73*04680031SPierre Pronchery		echo "Insufficient permissions"
74*04680031SPierre Pronchery		;;
75*04680031SPierre Pronchery	78) #EX_CONFIG
76*04680031SPierre Pronchery		echo "Configuration error"
77*04680031SPierre Pronchery		;;
78*04680031SPierre Pronchery	0)
79*04680031SPierre Pronchery		;;
80*04680031SPierre Pronchery	*)
81*04680031SPierre Pronchery		echo "An unknown error occurred (code $1)"
82*04680031SPierre Pronchery		return 1
83*04680031SPierre Pronchery		;;
84*04680031SPierre Pronchery	esac
85*04680031SPierre Pronchery	return $1
86*04680031SPierre Pronchery}
87*04680031SPierre Pronchery
88*04680031SPierre Proncheryerrormsg=
89*04680031SPierre Proncheryusername="root"
90*04680031SPierre Proncherywhile true; do
91*04680031SPierre Pronchery	exec 5>&1
92*04680031SPierre Pronchery	output=$(bsddialog --backtitle "$OSNAME Installer" \
93*04680031SPierre Pronchery		--title "Set $username password" \
94*04680031SPierre Pronchery		--cancel-label "Skip" \
95*04680031SPierre Pronchery		--passwordform --insecure \
96*04680031SPierre Pronchery		"Please select a password for the system management account ($username)
97*04680031SPierre Pronchery$errormsg" \
98*04680031SPierre Pronchery		0 0 2 \
99*04680031SPierre Pronchery		"Password" 0 0 '' 0 17 32 32 \
100*04680031SPierre Pronchery		"Repeat password" 1 0 '' 1 17 32 32 \
101*04680031SPierre Pronchery		2>&1 1>&5)
102*04680031SPierre Pronchery	res=$?
103*04680031SPierre Pronchery	exec 5>&-
104*04680031SPierre Pronchery	[ $res -eq $BSDDIALOG_OK ] || exit 0
105*04680031SPierre Pronchery
106*04680031SPierre Pronchery	echo -n "$output" | (read password1
107*04680031SPierre Pronchery		read password2
108*04680031SPierre Pronchery		[ -n "$password1" -o -n "$password2" ] || exit 62
109*04680031SPierre Pronchery		[ "$password1" = "$password2" ] || exit 63
110*04680031SPierre Pronchery		echo "$password1" | chroot $BSDINSTALL_CHROOT \
111*04680031SPierre Pronchery			/usr/sbin/pw usermod "$username" -h 0
112*04680031SPierre Pronchery	)
113*04680031SPierre Pronchery	err=$?
114*04680031SPierre Pronchery	[ $err -eq 0 ] && exit 0
115*04680031SPierre Pronchery	errormsg=$(error_get_message $err)
116*04680031SPierre Proncherydone
117