#!/bin/bash
# This block defines the variables the user of the script needs to input
# when deploying using this script.
#
#
# BTCTYPE=
#
# HOSTNAME=
#
# FQDN=
#
# USERPASSWORD=
#
# SSH_KEY=
#
# SYS_SSH_IP=
####
# 0. Set Initial Variables
####
# CURRENT BITCOIN RELEASE:
# Change as necessary
export BITCOIN=bitcoin-core-0.13.2
# Set the variable $IPADDR to the IP address the new Linode receives.
IPADDR=$(/sbin/ifconfig eth0 | awk '/inet / { print $2 }' | sed 's/addr://')
# Output stdout and stderr to ~root files
exec > >(tee -a /root/stackscript.log) 2> >(tee -a /root/stackscript.log /root/stackscript.err >&2)
echo "$0 - BEGINNING NEW MACHINE SETUP STACKSCRIPT"
####
# 1. Update Hostname
####
echo $HOSTNAME > /etc/hostname
/etc/init.d/hostname.sh start
/bin/hostname $HOSTNAME
echo "$0 - Set hostname as $FQDN ($IPADDR)"
echo "$0 - TODO: Put $FQDN with IP $IPADDR in your main DNS file."
# Add localhost aliases
echo "127.0.0.1 localhost" > /etc/hosts
echo "127.0.1.1 $FQDN $HOSTNAME" >> /etc/hosts
echo "$0 - Set localhost"
####
# 2. Update Timezone
####
# Set Timezone to America/LA
TIMEZONE="America/Los_Angeles"
echo $TIMEZONE > /etc/timezone
cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime
echo "$0 - Set Time Zone to Lost Angeles"
####
# 3. Protect the Server
####
# Add firewall rules to block everything that's not Bitcoin, Ping, or SSH
cat > /etc/iptables.firewall.rules < /etc/ip6tables.firewall.rules
# Make a startup file that runs IPv4 and IPv6 rules
cat > /etc/network/if-pre-up.d/firewall <> /etc/hosts.allow
echo "sshd: ALL" >> /etc/hosts.deny
echo "$0 - Limited SSH access."
else
echo "$0 - WARNING: Your SSH access is not limited; this is a major security hole!"
fi
# Block SSH access from everywhere else
# Yes, this means that if you don't have an IP address for SSH, you can only login
# from Linode's Lish Console
####
# 4. Set Up User
####
# Create "user1" with optional password and give them sudo capability
/usr/sbin/useradd -m -p `perl -e 'printf("%s\n",crypt($ARGV[0],"password"))' "$USERPASSWORD"` -g sudo -s /bin/bash user1
/usr/sbin/adduser user1 sudo
echo "$0 - Setup user1 with sudo access."
# Set up SSH Key
if [ -n "$SSH_KEY" ]; then
mkdir ~user1/.ssh
echo "$SSH_KEY" >> ~user1/.ssh/authorized_keys
chown -R user1 ~user1/.ssh
echo "$0 - Added .ssh key to user1."
fi
# Give user some helpful bitcoin aliases
if [ "$BTCTYPE" == "Testnet" ]; then
sudo -u user1 cat >> ~user1/.bash_profile <> ~user1/.bash_profile <&1 | grep "Good signature"`
if [[ $SHASIG ]]; then
echo "VERIFICATION SUCCESS / SIG: $SHASIG"
else
(>&2 echo "VERIFICATION ERROR: Signature for Bitcoin did not verify!")
fi
# Verify Bitcoin: SHA
export TARSHA256=`/usr/bin/sha256sum ~user1/$BITCOINPLAIN-x86_64-linux-gnu.tar.gz | awk '{print $1}'`
export EXPECTEDSHA256=`cat ~user1/SHA256SUMS.asc | grep $BITCOINPLAIN-x86_64-linux-gnu.tar.gz | awk '{print $1}'`
if [ "$TARSHA256" == "$EXPECTEDSHA256" ]; then
echo "VERIFICATION SUCCESS / SHA: $TARSHA256"
else
(>&2 echo "VERIFICATION ERROR: SHA for Bitcoin did not match!")
fi
# Install Bitcoin
echo "$0 - Installinging Bitcoin."
sudo -u user1 /bin/tar xzf ~user1/$BITCOINPLAIN-x86_64-linux-gnu.tar.gz -C ~user1
/usr/bin/install -m 0755 -o root -g root -t /usr/local/bin ~user1/$BITCOINPLAIN/bin/*
/bin/rm -rf ~user1/$BITCOINPLAIN/
# Start Up Bitcoin
echo "$0 - Starting Bitcoin."
sudo -u user1 /bin/mkdir ~user1/.bitcoin
# The only variation between Mainnet and Testnet is that Testnet has the "testnet=1" variable
# The only variation between Regular and Pruned is that Pruned has the "prune=550" variable, which is the smallest possible prune
# TODO: need to test rpcpassword random below using EOF technique
# TODO: there are other more modern ways to set up rpc authentication — to investigate and document.
# TODO: since these are largely the same, maybe another technique to build bitcoin.conf?
if [ "$BTCTYPE" == "Mainnet" ]; then
cat >> ~user1/.bitcoin/bitcoin.conf << EOF
server=1
dbcache=1536
par=1
txindex=1
blocksonly=1
maxuploadtarget=137
maxconnections=16
rpcuser=bitcoinrpc
rpcpassword=$(xxd -l 16 -p /dev/urandom)
EOF
elif [ "$BTCTYPE" == "Pruned Mainnet" ]; then
cat >> ~user1/.bitcoin/bitcoin.conf << EOF
server=1
dbcache=1536
par=1
blocksonly=1
prune=550
maxuploadtarget=137
maxconnections=16
rpcuser=bitcoinrpc
rpcpassword=$(xxd -l 16 -p /dev/urandom)
EOF
elif [ "$BTCTYPE" == "Testnet" ]; then
cat >> ~user1/.bitcoin/bitcoin.conf << EOF
server=1
dbcache=1536
par=1
txindex=1
blocksonly=1
maxuploadtarget=137
maxconnections=16
testnet=1
rpcuser=bitcoinrpc
rpcpassword=$(xxd -l 16 -p /dev/urandom)
EOF
elif [ "$BTCTYPE" == "Pruned Testnet" ]; then
cat >> ~user1/.bitcoin/bitcoin.conf << EOF
server=1
dbcache=1536
par=1
blocksonly=1
prune=550
maxuploadtarget=137
maxconnections=16
testnet=1
rpcuser=bitcoinrpc
rpcpassword=$(xxd -l 16 -p /dev/urandom)
EOF
elif [ "$BTCTYPE" == "Private Regtest" ]; then
(>&2 echo "$0 - ERROR: Private Regtest is not setup yet.")
else
(>&2 echo "$0 - ERROR: Somehow you managed to select no Bitcoin Installation Type, so Bitcoin hasn't been properly setup. Whoops!")
fi
/bin/chown user1 ~user1/.bitcoin/bitcoin.conf
/bin/chmod 600 ~user1/.bitcoin/bitcoin.conf
sudo -u user1 /usr/local/bin/bitcoind -daemon
# Add Bitcoin Startup to Crontab for User1
sudo -u user1 sh -c '( /usr/bin/crontab -l -u user1 2>/dev/null; echo "@reboot /usr/local/bin/bitcoind -daemon" ) | /usr/bin/crontab -u user1 -'
# Alert User!
sudo -u user1 touch ~user1/BITCOIN-IS-READY
echo "$0 - ENDING NEW MACHINE SETUP STACKSCRIPT"