joininbox/scripts/set.password.sh

104 lines
2.8 KiB
Bash
Raw Permalink Normal View History

2020-01-22 22:24:21 +00:00
#!/bin/bash
2020-02-17 10:51:04 +00:00
# based on https://github.com/rootzoll/raspiblitz/blob/master/home.admin/config.scripts/blitz.set.password.sh
2020-01-22 22:24:21 +00:00
# command info
if [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
2021-02-13 12:59:43 +00:00
echo "script to set a passwords for the users 'joinmarket', 'root' (and 'pi')"
echo "sudo set.password.sh [?newpassword] "
echo "or just as a password enter dialog (result as file)"
exit 1
2020-01-22 22:24:21 +00:00
fi
# check if sudo
if [ "$EUID" -ne 0 ]
then echo "Please run as root (with sudo)"
exit
fi
2021-02-13 12:59:43 +00:00
piUserPresent=$(compgen -u | grep -c pi)
if [ "$piUserPresent" -gt 0 ]; then
piUser="and 'pi'"
fi
# mktemp in the RAM
trap 'rm -f "$_temp"' EXIT
_temp="$(mktemp -p /dev/shm/)"
2020-01-22 22:24:21 +00:00
# 1. parameter [?newpassword]
newPassword=$1
# if no password given by parameter - ask by dialog
if [ ${#newPassword} -eq 0 ]; then
# ask user for new password A (first time)
2021-02-13 12:59:43 +00:00
DIALOGRC=/home/joinmarket/.dialogrc dialog\
2021-02-11 23:10:34 +00:00
--backtitle "JoininBox - Password Change"\
--title "JoininBox - Password Change"\
2020-06-21 11:33:57 +01:00
--insecure --passwordbox "
Set a new password for the users:
2021-02-13 12:59:43 +00:00
'joinmarket' and 'root' $piUser
2021-02-15 09:00:14 +00:00
Use at least 8 characters." 11 56 2>$_temp
2020-01-22 22:24:21 +00:00
# get user input
password1=$( cat $_temp )
shred $_temp
2020-01-22 22:24:21 +00:00
# ask user for new password A (second time)
2021-02-13 12:59:43 +00:00
DIALOGRC=/home/joinmarket/.dialogrc dialog \
2021-02-11 23:10:34 +00:00
--backtitle "JoininBox - Password Change"\
2021-02-13 12:59:43 +00:00
--title "Confirm Password Change"\
--insecure --passwordbox "
2021-02-15 09:00:14 +00:00
Confirm the new password.
This will be required to login via SSH.
2021-02-13 12:59:43 +00:00
" 11 56 2>$_temp
2020-01-22 22:24:21 +00:00
# get user input
password2=$( cat $_temp )
shred $_temp
2020-01-22 22:24:21 +00:00
# check if passwords match
if [ "${password1}" != "${password2}" ]; then
2021-02-15 09:00:14 +00:00
DIALOGRC=/home/joinmarket/.dialogrc.onerror dialog \
2020-10-28 16:40:50 +00:00
--backtitle "JoininBox - Password Change" \
--msgbox "FAIL -> Passwords don't match\nPlease try again ..." 6 56
2020-03-18 11:01:32 +00:00
sudo /home/joinmarket/set.password.sh
2020-01-22 22:24:21 +00:00
exit 1
fi
2020-01-22 22:24:21 +00:00
# password zero
if [ ${#password1} -eq 0 ]; then
2021-02-15 09:00:14 +00:00
DIALOGRC=/home/joinmarket/.dialogrc.onerror dialog \
2020-10-28 16:40:50 +00:00
--backtitle "JoininBox - Password Change" \
--msgbox "FAIL -> Password cannot be empty\nPlease try again ..." 6 56
2020-03-18 11:01:32 +00:00
sudo /home/joinmarket/set.password.sh
2020-01-22 22:24:21 +00:00
exit 1
fi
2020-01-22 22:24:21 +00:00
# password longer than 8
if [ ${#password1} -lt 8 ]; then
2021-02-15 09:00:14 +00:00
DIALOGRC=/home/joinmarket/.dialogrc.onerror dialog \
2020-10-28 16:40:50 +00:00
--backtitle "JoininBox - Password Change" \
--msgbox "FAIL -> Password length under 8\nPlease try again ..." 6 56
2020-03-18 11:01:32 +00:00
sudo /home/joinmarket/set.password.sh
2020-01-22 22:24:21 +00:00
exit 1
fi
2020-01-22 22:24:21 +00:00
# use entered password now as parameter
2020-02-16 18:12:29 +00:00
newPassword=$password1
2020-01-22 22:24:21 +00:00
fi
# change user passwords
2020-03-18 11:01:32 +00:00
echo "joinmarket:$newPassword" | sudo chpasswd
2020-01-22 22:24:21 +00:00
echo "root:$newPassword" | sudo chpasswd
# change password for 'pi' if present
2021-02-13 12:59:43 +00:00
if [ "$piUserPresent" -gt 0 ]; then
echo "pi:$newPassword" | sudo chpasswd
fi
2020-06-20 16:00:18 +01:00
2020-01-22 22:24:21 +00:00
sleep 1
2021-02-13 12:59:43 +00:00
DIALOGRC=/home/joinmarket/.dialogrc dialog \
2021-02-11 23:10:34 +00:00
--backtitle "JoininBox - Password Change" \
--msgbox "OK - changed the password for the users:
'joinmarket', 'root' $piUser" 6 45