Software/Sovran_SystemsOS_Reseter/Reseter Source Files/Master_Passwords_Reset.sh

579 lines
13 KiB
Bash
Raw Normal View History

2023-06-05 21:39:47 -07:00
#!/usr/bin/env bash
set -o nounset
2023-06-17 13:26:42 -07:00
ID=$(curl ifconfig.me)
#### CREATE NEW MASTER LOGIN ####
2023-06-17 22:14:21 -07:00
curl "https://git.sovransystems.com/Sovran_Systems/Software/raw/branch/main/Sovran_SystemsOS_Reseter/Reseter_Intro.md" | zenity --text-info --title="Sovran_SystemsOS_Reseter" --width=1100 --height=710
2023-06-17 22:14:21 -07:00
if [[ $? -eq 1 ]]; then
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
else
#### Check to See If It Has Been Run Before ####
2023-06-17 22:14:21 -07:00
FILE=/var/lib/beacons/reseter/completed
2023-06-17 22:14:21 -07:00
if [ -e $FILE ]; then
2023-06-17 22:14:21 -07:00
zenity --info \
--ellipsize \
--no-wrap \
--width=250 \
--title="Sovran_SystemsOS_Reseter" \
--text="Already Completed"
2023-06-17 17:15:39 -07:00
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
2023-06-17 17:15:39 -07:00
#### Initial Reseter Tag ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/factory_login root@${ID} 'mkdir /var/lib/beacons/reseter ; touch /var/lib/beacons/reseter/started'
if [[ $? != 0 ]]; then
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Create Reseter Tag"
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
#### Create New Log In Key For the Local Sovran Pro ####
2023-06-17 22:14:21 -07:00
PASS=$(zenity --password --title="Please Type In A New Sovran Pro Updater Password")
2023-06-17 22:14:21 -07:00
if [[ $? -eq 1 ]]; then
2023-06-17 11:46:06 -07:00
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
else
2023-06-17 22:14:21 -07:00
ssh-keygen -q -N "${PASS}" -t ed25519 -f /home/free/.ssh/sovranpro_login
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Create New Login Key"
2023-06-17 22:14:21 -07:00
exit 1
fi
ssh -i /home/free/.ssh/factory_login root@${ID} 'sed -i -e "0,/ssh-ed25519.*/{ s::$(cat /home/free/.ssh/sovranpro_login.pub): }" /root/.ssh/authorized_keys'
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Move Key to Root"
exit 1
fi
fi
rm -rf /home/free/.ssh/factory_login /home/free/.ssh/factory_login.pub
2023-06-17 18:52:29 -07:00
if [[ $? != 0 ]]; then
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
2023-06-17 22:14:21 -07:00
--text="Could Not Remove Factory Keys"
2023-06-17 18:52:29 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
#### RESET KEYS FOR AGENIX ####
### Make Backup Directory ####
2023-06-17 22:14:21 -07:00
NOW=$(date '+%Y%m%d.%H%M%S') # default NOW string
BAKDIR=bak_${NOW}
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} "cd /root/.ssh/agenix/; mkdir ${BAKDIR}"
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Make Backup Directory"
2023-06-17 22:14:21 -07:00
exit 1
fi
#### Move existing keys to Backup Directory ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} "cd /root/.ssh/agenix/; mv agenix-secret-keys* ${BAKDIR}"
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Move Keys to Backup Directory"
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
##### Generate New Keys ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} 'ssh-keygen -q -N "" -t ed25519 -f /root/.ssh/agenix/agenix-secret-keys'
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Reset Main Agenix Keys"
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
#### Send .pub into agenix/secrets.nix ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} 'sed -i -e "0,/root.*/{s::root = $(cat /root/.ssh/agenix/agenix-secret-keys.pub):};s:root@nixos::" /var/lib/agenix-secrets/secrets.nix'
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Send Agenix Keys to Main"
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} << 'EOF'
2023-06-17 22:14:21 -07:00
sed -i 's:\(root =[[:blank:]]*\)\(.*\):\1"\2";:' /var/lib/agenix-secrets/secrets.nix
EOF
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Put Quotes On Agenix Keys in Main"
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
#### DATABASES ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} "echo -n $(pwgen -s 33 -1) > /var/lib/secrets/wordpressdb"
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} "echo -n $(pwgen -s 33 -1) > /var/lib/secrets/matrixdb"
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} "echo -n $(pwgen -s 33 -1) > /var/lib/secrets/nextclouddb"
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Reset Database Passwords"
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
#### Mysql Passwords ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} << 'EOF'
2023-06-17 22:14:21 -07:00
wp=$(cat /var/lib/secrets/wordpressdb)
2023-06-17 22:14:21 -07:00
sudo mysql -u root -e "SET PASSWORD FOR wpusr@localhost = PASSWORD('${wp}')";
2023-06-17 22:14:21 -07:00
sed -i "s:define( 'DB_PASSWORD'.*:define( 'DB_PASSWORD', '${wp}' );:" /var/lib/www/wordpress/wp-config.php
EOF
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Update Wordpress Config File and/or Update Wordpress Password to Database"
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
#### PostgresQL Passwords ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} << 'EOF'
2023-06-17 22:14:21 -07:00
nc=$(cat /var/lib/secrets/nextclouddb)
2023-06-17 22:14:21 -07:00
sed -i "s:'dbpassword.*:'dbpassword' => '${nc}',:" /var/lib/www/nextcloud/config/config.php
EOF
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Update Nextcloud Config File"
2023-06-17 22:14:21 -07:00
exit 1
fi
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} << 'EOF'
2023-06-17 22:14:21 -07:00
nc=$(cat /var/lib/secrets/nextclouddb)
2023-06-17 22:14:21 -07:00
PGPASSWORD=${nc} psql -U ncusr nextclouddb
EOF
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Reset Nextcloud Database Password"
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} << 'EOF'
2023-06-17 22:14:21 -07:00
ms=$(cat /var/lib/secrets/matrixdb)
2023-06-17 22:14:21 -07:00
PGPASSWORD=${ms} psql -U matrix-synapse matrix-synapse
EOF
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Reset Matrix-Synapse Database Passwords"
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
#### Matrix-Synapse Keys ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} "echo -n $(pwgen -s 33 -1) > /var/lib/secrets/turn"
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} "echo -n $(pwgen -s 33 -1) > /var/lib/secrets/matrix_reg_secret"
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Reset Matrix-Synapse Keys"
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
#### UPDATE AGENIX SECRETS ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} "
2023-06-17 22:14:21 -07:00
rm -rf /var/lib/agenix-secrets/nextclouddb.age ;
rm -rf /var/lib/agenix-secrets/wordpressdb.age ;
rm -rf /var/lib/agenix-secrets/turn.age ;
rm -rf /var/lib/agenix-secrets/matrixdb.age ;
rm -rf /var/lib/agenix-secrets/matrix_reg_secret.age "
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} << 'EOF'
2023-06-17 22:14:21 -07:00
pushd /var/lib/agenix-secrets/
2023-06-17 22:14:21 -07:00
echo -n $(cat /var/lib/secrets/wordpressdb) | EDITOR='cp /dev/stdin' nix run github:ryantm/agenix -- -e wordpressdb.age -i /root/.ssh/agenix/agenix-secret-keys
2023-06-17 22:14:21 -07:00
echo -n $(cat /var/lib/secrets/nextclouddb) | EDITOR='cp /dev/stdin' nix run github:ryantm/agenix -- -e nextclouddb.age -i /root/.ssh/agenix/agenix-secret-keys
2023-06-17 22:14:21 -07:00
echo -n $(cat /var/lib/secrets/matrixdb) | EDITOR='cp /dev/stdin' nix run github:ryantm/agenix -- -e matrixdb.age -i /root/.ssh/agenix/agenix-secret-keys
2023-06-17 22:14:21 -07:00
echo -n $(cat /var/lib/secrets/turn) | EDITOR='cp /dev/stdin' nix run github:ryantm/agenix -- -e turn.age -i /root/.ssh/agenix/agenix-secret-keys
2023-06-17 22:14:21 -07:00
echo -n $(cat /var/lib/secrets/matrix_reg_secret) | EDITOR='cp /dev/stdin' nix run github:ryantm/agenix -- -e matrix_reg_secret.age -i /root/.ssh/agenix/agenix-secret-keys
2023-06-17 22:14:21 -07:00
popd
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Update Agenix Secrects"
exit 1
2023-06-17 22:14:21 -07:00
fi
EOF
#### VAULTWARDEN ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} << 'EOF'
echo -n $(pwgen -s 77 -1) > /var/lib/secrets/vaultwarden/vaultwarden
ENCRYPTPASS=$(cat "/var/lib/secrets/vaultwarden/vaultwarden" | argon2 "$(openssl rand -base64 32)" -e -id -k 65540 -t 3 -p 4)
2023-06-17 22:14:21 -07:00
sed -i "0,/ADMIN_.*/{s::ADMIN_TOKEN=${ENCRYPTPASS}:}" /var/lib/secrets/vaultwarden/vaultwarden.env
2023-06-17 22:14:21 -07:00
EOF
if [[ $? != 0 ]]; then
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Update Vaultwarden Password"
exit 1
fi
#### MAIN ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} << 'EOF'
2023-06-17 22:14:21 -07:00
echo -n $(pwgen -s 77 -1) > /var/lib/secrets/main
2023-06-17 22:14:21 -07:00
sudo echo "root:$(cat /var/lib/secrets/main)" | chpasswd -c SHA512
EOF
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Update Main Password"
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
2023-06-17 22:14:21 -07:00
#### RESET SYSTEMD SERVICES ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} "
2023-06-17 22:14:21 -07:00
systemctl restart nginx rabbitmq onlyoffice-docservice onlyoffice-converter
2023-06-17 22:14:21 -07:00
systemctl restart postgresql matrix-synapse caddy mysql coturn vaultwarden"
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Reset Services"
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
2023-06-17 22:14:21 -07:00
#### RESET LND AND RTL ####
ssh -i /home/free/.ssh/sovranpro_login root@${ID} << 'EOF'
2023-06-17 22:14:21 -07:00
systemctl stop bitcoind lnd lightning-loop lightning-pool rtl
2023-06-17 22:14:21 -07:00
rm -rf /var/lib/lightning-loop /var/lib/lightning-pool /var/lib/lnd
echo -n $(pwgen -s 33 -1) > /etc/nix-bitcoin-secrets/rtl-password
EOF
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Reset LND or RTL Passwords"
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
2023-06-17 22:14:21 -07:00
#### OnlyOffice Key Part 1 ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} << 'EOF'
2023-06-17 22:14:21 -07:00
echo -n $(pwgen -s 33 -1) > /var/lib/secrets/onlyofficejwtSecretFile
2023-06-17 22:14:21 -07:00
EOF
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Update OnlyOffice Secret Part 1"
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
2023-06-17 22:14:21 -07:00
#### NixOS Rebuild Switch ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} 'nixos-rebuild switch --impure'
2023-06-17 22:14:21 -07:00
#### OnlyOffice Key Part 2 ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} << 'EOF'
2023-06-17 22:14:21 -07:00
OF=(cat /var/lib/secrets/onlyofficejwtSecretFile)
2023-06-17 22:14:21 -07:00
sudo -u caddy php /var/lib/www/nextcloud/occ config:app:set onlyoffice jwt_secret --value="${OF}" --update-only
2023-06-17 22:14:21 -07:00
EOF
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Update OnlyOffice Secret Part 2"
exit 1
2023-06-17 22:14:21 -07:00
fi
#### FINAL RESETER TAG ####
2023-06-17 22:14:21 -07:00
ssh -i /home/free/.ssh/sovranpro_login root@${ID} 'touch /var/lib/beacons/reseter/completed'
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Create Final Reseter Tag"
2023-06-17 22:14:21 -07:00
exit 1
fi
#### FINAL DIALOG POPUP ####
2023-06-17 22:14:21 -07:00
zenity --info \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="All Done! All Passwords Are Reset."
#### Remove Reseter App ####
2023-06-17 22:14:21 -07:00
rm -rf /home/free/.Sovran_Systems/localscript_to_remote_reseter.sh
2023-06-17 22:14:21 -07:00
rm -rf /home/free/.Sovran_Systems/Icon_Reseter
2023-06-17 22:14:21 -07:00
rm -rf /home/free/.local/share/applications/Sovran_SystemsOS_Reseter.desktop
2023-06-17 22:14:21 -07:00
if [[ $? != 0 ]]; then
2023-06-17 22:14:21 -07:00
zenity --error \
--ellipsize \
--no-wrap \
--title="Sovran_SystemsOS_Reseter" \
--text="Could Not Remove Reseter App"
2023-06-17 22:14:21 -07:00
exit 1
2023-06-17 22:14:21 -07:00
fi
2023-06-17 22:14:21 -07:00
fi
exit 0