Skip to content

Commit

Permalink
refactor first start process
Browse files Browse the repository at this point in the history
  • Loading branch information
openoms committed Feb 13, 2021
1 parent bd985c6 commit d0a0e76
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 59 deletions.
35 changes: 20 additions & 15 deletions scripts/set.password.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

# command info
if [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
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
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
fi

# check if sudo
Expand All @@ -15,6 +15,11 @@ if [ "$EUID" -ne 0 ]
exit
fi

piUserPresent=$(compgen -u | grep -c pi)
if [ "$piUserPresent" -gt 0 ]; then
piUser="and 'pi'"
fi

# mktemp
_temp="./dialog.$$"

Expand All @@ -24,24 +29,26 @@ newPassword=$1
# if no password given by parameter - ask by dialog
if [ ${#newPassword} -eq 0 ]; then
# ask user for new password A (first time)
DIALOGRC=.dialogrc dialog \
DIALOGRC=/home/joinmarket/.dialogrc dialog\
--backtitle "JoininBox - Password Change"\
--title "JoininBox - Password Change"\
--insecure --passwordbox "
Set a new password for the users:
'joinmarket' and 'root' (use at least 8 characters)" 10 56 2>$_temp
'joinmarket' and 'root' $piUser
use at least 8 characters" 11 56 2>$_temp

# get user input
password1=$( cat $_temp )
shred $_temp

# ask user for new password A (second time)
DIALOGRC=.dialogrc dialog \
DIALOGRC=/home/joinmarket/.dialogrc dialog \
--backtitle "JoininBox - Password Change"\
--insecure \
--passwordbox "
Re-enter the password:
(This is the new password to login via SSH)" 10 56 2>$_temp
--title "Confirm Password Change"\
--insecure --passwordbox "
Re-enter the new password:
(This will be required to login via SSH)
" 11 56 2>$_temp

# get user input
password2=$( cat $_temp )
Expand Down Expand Up @@ -83,15 +90,13 @@ fi
echo "joinmarket:$newPassword" | sudo chpasswd
echo "root:$newPassword" | sudo chpasswd
# change password for 'pi' if present
if [ "$(compgen -u | grep -c pi)" -gt 0 ]; then
if [ "$piUserPresent" -gt 0 ]; then
echo "pi:$newPassword" | sudo chpasswd
piUser=" and 'pi'"
fi

sleep 1
DIALOGRC=.dialogrc dialog \
DIALOGRC=/home/joinmarket/.dialogrc dialog \
--backtitle "JoininBox - Password Change" \
--msgbox "OK - changed the password for the users:
'joinmarket', 'root' $piUser" 6 45

exit 0
96 changes: 52 additions & 44 deletions scripts/start.joininbox.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,101 @@

echo "# starting JoininBox ..."

if [ -f /home/joinmarket/joinin.conf ]; then
if [ ! -f /home/joinmarket/joinin.conf ];then
touch /home/joinmarket/joinin.conf
fi

source /home/joinmarket/_functions.sh

#############
# FIRST RUN #
#############

setupStepEntry=$(grep -c "setupStep" < /home/joinmarket/joinin.conf)
if [ "$setupStepEntry" -eq 0 ]; then
if [ -f "/mnt/hdd/raspiblitz.conf" ] ; then
runningEnv="raspiblitz"
setupStep=0
else
runningEnv="standalone"
setupStep=0
fi
echo "setupStep=$setupStep" >> /home/joinmarket/joinin.conf
runningEnvEntry=$(grep -c "runningEnv" < /home/joinmarket/joinin.conf)
if [ "$runningEnvEntry" -eq 0 ]; then
if [ "$setupStepEntry" -eq 0 ];then
echo "setupStep=0" >> /home/joinmarket/joinin.conf
fi

source /home/joinmarket/joinin.conf
if [ "$setupStep" -lt 100 ];then

# identify running env
runningEnvEntry=$(grep -c "runningEnv" < /home/joinmarket/joinin.conf)
if [ "$runningEnvEntry" -eq 0 ];then
if [ -f "/mnt/hdd/raspiblitz.conf" ];then
runningEnv="raspiblitz"
else
runningEnv="standalone"
fi
echo "runningEnv=$runningEnv" >> /home/joinmarket/joinin.conf
sed -i "s#setupStep=.*#setupStep=1#g" /home/joinmarket/joinin.conf
fi
echo "# running in the environment: $runningEnv"

# identify cpu architecture
cpuEntry=$(grep -c "cpu" < /home/joinmarket/joinin.conf)
if [ "$cpuEntry" -eq 0 ]; then
if [ "$cpuEntry" -eq 0 ];then
cpu=$(uname -m)
echo "# cpu=${cpu}"
echo "cpu=$cpu" >> /home/joinmarket/joinin.conf
sed -i "s#setupStep=.*#setupStep=2#g" /home/joinmarket/joinin.conf
fi
echo "# cpu=${cpu}"

# make sure Tor path is known
DirEntry=$(grep -c "HiddenServiceDir" < /home/joinmarket/joinin.conf)
if [ "$DirEntry" -eq 0 ]; then
if [ -d "/mnt/hdd/tor" ] ; then
if [ "$DirEntry" -eq 0 ];then
if [ -d "/mnt/hdd/tor" ];then
HiddenServiceDir="/mnt/hdd/tor"
else
HiddenServiceDir="/var/lib/tor"
fi
echo "HiddenServiceDir=$HiddenServiceDir" >> /home/joinmarket/joinin.conf
echo "HiddenServiceDir=$HiddenServiceDir" >> /home/joinmarket/joinin.conf
sed -i "s#setupStep=.*#setupStep=3#g" /home/joinmarket/joinin.conf
fi

# check for dialog
if [ "$(dialog | grep -c "ComeOn Dialog!")" -eq 0 ]; then
if [ "$(dialog | grep -c "ComeOn Dialog!")" -eq 0 ];then
sudo apt install dialog
sed -i "s#setupStep=.*#setupStep=4#g" /home/joinmarket/joinin.conf
fi

# check if JoinMarket is installed
/home/joinmarket/install.joinmarket.sh install
sed -i "s#setupStep=.*#setupStep=5#g" /home/joinmarket/joinin.conf

# change the ssh password and open the config menu if standalone
if [ "$runningEnv" = "standalone" ]; then
if [ "$runningEnv" = "standalone" ];then
# set ssh passwords on the first run
sudo /home/joinmarket/set.password.sh || exit 1
generateJMconfig
sudo sed -i "s#setupStep=.*#setupStep=100#g" /home/joinmarket/joinin.conf
/home/joinmarket/menu.config.sh
elif [ "$runningEnv" = "raspiblitz" ];then
generateJMconfig
echo
echo "# editing the joinmarket.cfg"
sed -i "s/^rpc_user =.*/rpc_user = raspibolt/g" /home/joinmarket/.joinmarket/joinmarket.cfg
PASSWORD_B=$(sudo cat /mnt/hdd/bitcoin/bitcoin.conf | grep rpcpassword | cut -c 13-)
sed -i "s/^rpc_password =.*/rpc_password = $PASSWORD_B/g" /home/joinmarket/.joinmarket/joinmarket.cfg
echo "# filled the bitcoin RPC password (PASSWORD_B)"
sed -i "s/^rpc_wallet_file =.*/rpc_wallet_file = wallet.dat/g" /home/joinmarket/.joinmarket/joinmarket.cfg
echo "# using the bitcoind wallet: wallet.dat"
# setup finished
sed -i "s#setupStep=.*#setupStep=100#g" /home/joinmarket/joinin.conf
fi
fi

source /home/joinmarket/_functions.sh
source /home/joinmarket/joinin.conf

if [ "$runningEnv" = "raspiblitz" ] && [ "$setupStep" -eq 0 ]; then
generateJMconfig
echo
echo "# editing the joinmarket.cfg"
sed -i "s/^rpc_user =.*/rpc_user = raspibolt/g" /home/joinmarket/.joinmarket/joinmarket.cfg
PASSWORD_B=$(sudo cat /mnt/hdd/bitcoin/bitcoin.conf | grep rpcpassword | cut -c 13-)
sed -i "s/^rpc_password =.*/rpc_password = $PASSWORD_B/g" /home/joinmarket/.joinmarket/joinmarket.cfg
echo "# filled the bitcoin RPC password (PASSWORD_B)"
sed -i "s/^rpc_wallet_file =.*/rpc_wallet_file = wallet.dat/g" /home/joinmarket/.joinmarket/joinmarket.cfg
echo "# using the bitcoind wallet: wallet.dat"
# setup finished
sudo sed -i "s#setupStep=.*#setupStep=100#g" /home/joinmarket/joinin.conf
fi

#############
# EVERY RUN #
#############

# check bitcoind RPC setting
# add default value to joinin config if needed
if ! grep -Eq "^RPCoverTor=" /home/joinmarket/joinin.conf; then
if ! grep -Eq "^RPCoverTor=" /home/joinmarket/joinin.conf;then
echo "RPCoverTor=off" >> /home/joinmarket/joinin.conf
fi
# check if bitcoin RPC connection is over Tor
if grep -Eq "^rpc_host = .*.onion" /home/joinmarket/.joinmarket/joinmarket.cfg; then
if grep -Eq "^rpc_host = .*.onion" /home/joinmarket/.joinmarket/joinmarket.cfg;then
echo "# RPC over Tor is on"
sed -i "s/^RPCoverTor=.*/RPCoverTor=on/g" /home/joinmarket/joinin.conf
else
Expand All @@ -98,22 +106,22 @@ fi

# check if there is only one joinmarket wallet and make default
# add default value to joinin config if needed
if ! grep -Eq "^defaultWallet=" /home/joinmarket/joinin.conf; then
if ! grep -Eq "^defaultWallet=" /home/joinmarket/joinin.conf;then
echo "defaultWallet=off" >> /home/joinmarket/joinin.conf
fi
if [ "$(ls -p /home/joinmarket/.joinmarket/wallets/ | grep -cv /)" -gt 1 ]; then
if [ "$(ls -p /home/joinmarket/.joinmarket/wallets/ | grep -cv /)" -gt 1 ];then
echo "# Found more than one wallet file"
echo "# Setting defaultWallet to off"
sed -i "s#^defaultWallet=.*#defaultWallet=off#g" /home/joinmarket/joinin.conf
elif [ "$(ls -p /home/joinmarket/.joinmarket/wallets/ | grep -cv /)" -eq 1 ]; then
elif [ "$(ls -p /home/joinmarket/.joinmarket/wallets/ | grep -cv /)" -eq 1 ];then
onlyWallet=$(ls -p /home/joinmarket/.joinmarket/wallets/ | grep -v /)
echo "# Found only one wallet file: $onlyWallet"
echo "# Using it as default"
sed -i "s#^defaultWallet=.*#defaultWallet=$onlyWallet#g" /home/joinmarket/joinin.conf
fi

# add default value to joinin config if needed
if ! grep -Eq "^network=" /home/joinmarket/joinin.conf; then
if ! grep -Eq "^network=" /home/joinmarket/joinin.conf;then
echo "network=unknown" >> /home/joinmarket/joinin.conf
fi
isMainnet=$(grep -c "network = mainnet" < /home/joinmarket/.joinmarket/joinmarket.cfg)
Expand All @@ -130,9 +138,9 @@ else
fi

# add default value to joinin config if needed
if ! grep -Eq "^localip=" /home/joinmarket/joinin.conf; then
if ! grep -Eq "^localip=" /home/joinmarket/joinin.conf;then
echo "localip=unknown" >> /home/joinmarket/joinin.conf
fi
localip=$(ip addr | grep 'state UP' -A2 | grep -Ev 'docker0|veth' | \
grep 'eth0\|wlan0\|enp0' | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
sed -i "s#^localip=.*#localip=$localip#g" /home/joinmarket/joinin.conf
sed -i "s#^localip=.*#localip=$localip#g" /home/joinmarket/joinin.conf

0 comments on commit d0a0e76

Please sign in to comment.