raspiblitz/home.admin/config.scripts/lnd.credentials.sh

228 lines
8.2 KiB
Bash
Raw Normal View History

2020-05-01 00:19:24 +02:00
#!/bin/bash
# command info
if [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
echo "tool to reset or sync credentials (e.g. macaroons)"
echo "lnd.credentials.sh [reset|sync|check] [?tls|macaroons|keepold]"
2020-05-01 00:19:24 +02:00
exit 1
fi
# interactive choose type of action
if [ "$1" = "" ] || [ $# -eq 0 ]; then
OPTIONS=()
OPTIONS+=(RESET "Recreate Macaroons + TLS")
2020-05-02 17:47:19 +02:00
OPTIONS+=(SYNC "Sync with RaspiBlitz Apps/Users")
OPTIONS+=(EXPORT "Get Macaroons and TLS.cert")
CHOICE=$(dialog --clear \
--backtitle "RaspiBlitz" \
--title "Manage LND credentials" \
--menu "Choose action" \
11 50 7 \
"${OPTIONS[@]}" \
2>&1 >/dev/tty)
clear
case $CHOICE in
RESET)
sudo /home/admin/config.scripts/lnd.credentials.sh reset
echo "Press ENTER to return to main menu."
read key
exit 0
;;
SYNC)
sudo /home/admin/config.scripts/lnd.credentials.sh sync
echo "Press ENTER to return to main menu."
read key
exit 0
;;
EXPORT)
sudo /home/admin/config.scripts/lnd.export.sh
exit 0
;;
esac
fi
2020-05-01 00:19:24 +02:00
# load data from config
source /mnt/hdd/raspiblitz.conf
###########################
# FUNCTIONS
###########################
function copy_mac_set_perms() {
local file_name=${1} # the file name (e.g. admin.macaroon)
local group_name=${2} # the unix group name (e.g. lndadmin)
local n=${3:-bitcoin} # the network (e.g. bitcoin or litecoin) defaults to bitcoin
local c=${4:-main} # the chain (e.g. main, test, sim, reg) defaults to main (for mainnet)
sudo /bin/cp /mnt/hdd/lnd/data/chain/"${n}"/"${c}"net/"${file_name}" /mnt/hdd/app-data/lnd/data/chain/"${n}"/"${c}"net/"${file_name}"
sudo /bin/chown --silent admin:"${group_name}" /mnt/hdd/app-data/lnd/data/chain/"${n}"/"${c}"net/"${file_name}"
sudo /bin/chmod --silent 640 /mnt/hdd/app-data/lnd/data/chain/"${n}"/"${c}"net/"${file_name}"
}
function check_macaroons() {
macaroons="admin.macaroon invoice.macaroon readonly.macaroon invoices.macaroon chainnotifier.macaroon signer.macaroon walletkit.macaroon router.macaroon"
missing=0
for macaroon in $macaroons
do
local file_name=${macaroon}
local n=${1:-bitcoin} # the network (e.g. bitcoin or litecoin) defaults to bitcoin
local c=${2:-main} # the chain (e.g. main, test, sim, reg) defaults to main (for mainnet)
if [ ! -f /mnt/hdd/app-data/lnd/data/chain/"${n}"/"${c}"net/"${macaroon}" ]; then
missing=$(($missing + 1))
echo "# ${macaroon} is missing ($missing)"
else
echo "# ${macaroon} is present"
fi
done
}
2020-05-01 00:19:24 +02:00
###########################
# RESET Macaroons and TLS
###########################
if [ "$1" = "reset" ]; then
2020-05-28 14:44:16 +02:00
2020-05-01 00:19:24 +02:00
clear
2020-05-28 14:44:16 +02:00
echo "### lnd.credentials.sh reset"
# default reset both
resetTLS=1
resetMacaroons=1
# optional second paramter to just reset one on them
if [ "$2" == "tls" ]; then
echo "# just resetting TLS"
resetTLS=1
resetMacaroons=0
fi
if [ "$2" == "macaroons" ]; then
echo "# just resetting macaroons"
2020-05-28 14:44:16 +02:00
resetTLS=0
resetMacaroons=1
keepOldMacaroons=0
2020-05-28 14:44:16 +02:00
fi
if [ "$2" == "keepold" ]; then
echo "# add the missing default macaroons without deauthenticating the old ones"
resetTLS=0
resetMacaroons=1
keepOldMacaroons=1
fi
2020-05-28 14:44:16 +02:00
if [ ${resetMacaroons} -eq 1 ]; then
echo "## Resetting Macaroons"
echo "# all your macaroons get deleted and recreated"
cd || exit
sudo find /mnt/hdd/app-data/lnd/data/chain/"${network}"/"${chain}"net/ -iname '*.macaroon' -delete
sudo find /home/bitcoin/.lnd/data/chain/"${network}"/"${chain}"net/ -iname '*.macaroon' -delete
if [ ${keepOldMacaroons} -eq 0 ]; then
sudo rm /home/bitcoin/.lnd/data/chain/"${network}"/"${chain}"net/macaroons.db
fi
2020-05-28 14:44:16 +02:00
fi
if [ ${resetTLS} -eq 1 ]; then
echo "## Resetting TLS"
echo "# tls cert gets deleted and recreated"
cd || exit
sudo /home/admin/config.scripts/lnd.tlscert.sh refresh
fi
# unlock wallet after restart
2020-05-28 15:32:26 +02:00
echo "# restarting LND ... wait 10 secs"
2020-05-01 00:19:24 +02:00
sudo systemctl start lnd
sleep 10
2020-05-28 14:44:16 +02:00
# unlock wallet after restart
sudo /home/admin/config.scripts/lnd.unlock.sh
2020-05-01 00:19:24 +02:00
sleep 10
2020-05-28 14:44:16 +02:00
if [ ${resetMacaroons} -eq 1 ]; then
echo "# copy new macaroons to central app-data directory and ensure unix ownerships and permissions"
copy_mac_set_perms admin.macaroon lndadmin "${network}" "${chain}"
copy_mac_set_perms invoice.macaroon lndinvoice "${network}" "${chain}"
copy_mac_set_perms readonly.macaroon lndreadonly "${network}" "${chain}"
echo "# OK DONE"
fi
2020-05-01 00:19:24 +02:00
###########################
# SYNC
###########################
2020-05-01 11:15:32 +02:00
elif [ "$1" = "sync" ]; then
2020-05-28 14:44:16 +02:00
2020-05-01 00:19:24 +02:00
echo "###### SYNCING MACAROONS, RPC Password AND TLS Certificate ######"
echo "# make sure LND app-data directories exist"
sudo /bin/mkdir --mode 0755 --parents /mnt/hdd/app-data/lnd/data/chain/"${network}"/"${chain}"net/
echo `# make sure all user groups exit for default macaroons`
sudo /usr/sbin/groupadd --force --gid 9700 lndadmin
sudo /usr/sbin/groupadd --force --gid 9701 lndinvoice
sudo /usr/sbin/groupadd --force --gid 9702 lndreadonly
sudo /usr/sbin/groupadd --force --gid 9703 lndinvoices
sudo /usr/sbin/groupadd --force --gid 9704 lndchainnotifier
sudo /usr/sbin/groupadd --force --gid 9705 lndsigner
sudo /usr/sbin/groupadd --force --gid 9706 lndwalletkit
sudo /usr/sbin/groupadd --force --gid 9707 lndrouter
2020-05-01 00:19:24 +02:00
echo "# copy macaroons to central app-data directory and ensure unix ownerships and permissions"
copy_mac_set_perms admin.macaroon lndadmin "${network}" "${chain}"
copy_mac_set_perms invoice.macaroon lndinvoice "${network}" "${chain}"
copy_mac_set_perms readonly.macaroon lndreadonly "${network}" "${chain}"
copy_mac_set_perms invoices.macaroon lndinvoices "${network}" "${chain}"
copy_mac_set_perms chainnotifier.macaroon lndchainnotifier "${network}" "${chain}"
copy_mac_set_perms signer.macaroon lndsigner "${network}" "${chain}"
copy_mac_set_perms walletkit.macaroon lndwalletkit "${network}" "${chain}"
copy_mac_set_perms router.macaroon lndrouter "${network}" "${chain}"
2020-05-01 00:19:24 +02:00
echo "# make sure admin has a symlink at ~/.lnd to /mnt/hdd/app-data/lnd/"
if ! [[ -L "/home/admin/.lnd" ]]; then
sudo rm -rf "/home/admin/.lnd" # not a symlink.. delete it silently
ln -s /mnt/hdd/app-data/lnd/ /home/admin/.lnd # and create symlink
fi
echo "# make sure network (bitcoin/litecoin) RPC password is set correctly in lnd.conf"
source <(sudo cat /mnt/hdd/"${network}"/"${network}".conf 2>/dev/null | grep "rpcpass" | sed 's/^[a-z]*\./lnd/g')
if [ "${#rpcpassword}" -gt 0 ]; then
sudo sed -i 's/^"${network}"d.rpcpass=.*/"${network}"d.rpcpass="${rpcpassword}"/g' /mnt/hdd/lnd/lnd.conf 2>/dev/null
else
echo "# WARN: could not get value 'rpcpass' from network config (e.g. bitcoin.conf)"
fi
2020-05-01 13:58:21 +02:00
echo "# make sure LND conf is readable and symlinked"
sudo chmod 644 "/mnt/hdd/lnd/lnd.conf"
sudo chown bitcoin:bitcoin "/mnt/hdd/lnd/lnd.conf"
if ! [[ -L "/mnt/hdd/app-data/lnd/lnd.conf" ]]; then
sudo rm -rf "/mnt/hdd/app-data/lnd/lnd.conf" # not a symlink.. delete it silently
2020-05-01 14:00:38 +02:00
sudo ln -s "/mnt/hdd/lnd/lnd.conf" "/mnt/hdd/app-data/lnd/lnd.conf" # and create symlink
2020-05-01 13:58:21 +02:00
fi
2020-05-01 11:15:32 +02:00
echo "# make sure TLS certificate is readable and symlinked"
2020-05-01 13:58:21 +02:00
sudo chmod 644 "/mnt/hdd/lnd/tls.cert"
2020-05-01 00:26:41 +02:00
sudo chown bitcoin:bitcoin "/mnt/hdd/lnd/tls.cert"
2020-05-01 11:15:32 +02:00
if ! [[ -L "/mnt/hdd/app-data/lnd/tls.cert" ]]; then
sudo rm -rf "/mnt/hdd/app-data/lnd/tls.cert" # not a symlink.. delete it silently
2020-05-01 14:00:38 +02:00
sudo ln -s "/mnt/hdd/lnd/tls.cert" "/mnt/hdd/app-data/lnd/tls.cert" # and create symlink
2020-05-01 11:15:32 +02:00
fi
if [ "${LNBits}" = "on" ]; then
echo "# fix the macaroon for LNbits"
# https://github.com/rootzoll/raspiblitz/pull/1156#issuecomment-623293240
sudo -u admin /home/admin/config.scripts/bonus.lnbits.sh write-macaroons
fi
###########################
# Check Macaroons and fix missing
###########################
elif [ "$1" = "check" ]; then
check_macaroons ${network} ${chain}
if [ $missing -gt 0 ]; then
/home/admin/config.scrips/lnd.creds.sh reset keepold
fi
2020-05-01 11:15:32 +02:00
###########################
# UNKNOWN
###########################
else
echo "# FAIL: parameter not known - run with -h for help"
exit 1
2020-05-01 00:19:24 +02:00
fi