-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·286 lines (246 loc) · 9.98 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/bin/bash
#
# Description:
# This script will configure a full Bitcoin onion node on a stock Raspbian install
#
# TODO:
# - Nothing yet
#
# Bash options
set -o errexit # exit script when a command fails
set -o nounset # exit script when a variable is not set
# Variables
readonly DEFAULT_USER=pi
readonly BITCOIN_USER=bitcoinuser
readonly BITCOIN_DIR=/home/"${BITCOIN_USER}"/.bitcoin
readonly ONION_DIR=/etc/onion-node
readonly CONFIG_FILES="${ONION_DIR}"/config-files
readonly INSTALL_SCRIPTS="${ONION_DIR}"/install-scripts
readonly APT_PACKAGE=macchanger # This package should be installed with apt-install-packages.sh
readonly LOCK_DIR=/tmp/tor-bitcoin.lock/
# Only run as root
if [[ "$(id -u)" != "0" ]]; then
echo "ERROR: Must be run as root...exiting script"
exit 0
fi
# Start installation
echo ""
echo "The installation of a bitcoin node takes about 1 to 1.5 hours"
sleep 4
echo "It assumes the system has a clean Raspbian installation, a working network connection and can do DNS lookups"
sleep 5
echo "If you have that covered, you can just check back in 1 to 1.5 hours"
sleep 10
echo "Alright... here we go"
echo ""
sleep 3
# Check if a lockfile/LOCKDIR exists, wait max 2 hours
tries=0
while [[ -d "${LOCK_DIR}" ]] && [[ "${tries}" -lt 120 ]]; do
echo "Temporarily not able to acquire lock on ${LOCK_DIR}"
echo "Other processes might be running...retry in 60 seconds"
sleep 60
tries=$(( ${tries} +1 ))
done
# Set lockfile/dir - mkdir is atomic
# For portability flock or other Linux only tools are not used
if mkdir "${LOCK_DIR}"; then
trap 'rmdir "${LOCK_DIR}"; exit' INT TERM EXIT # remove LOCKDIR when script is interrupted, terminated or finished
echo "Successfully acquired lock on ${LOCK_DIR}"
else
echo "Failed to acquire lock on ${LOCK_DIR}"
echo "The installation script failed...run the install.sh script again to see if you get better results."
echo "Tip: Reboot the system if the installation keeps failling."
exit 0
fi
# Make install.sh script re-runnable
# Stop bitcoin process
"${ONION_DIR}"/bitcoin-control.sh
# Remove unattended-upgrades file
if [[ -r /etc/apt/apt.conf.d/20auto-upgrades ]]; then
rm /etc/apt/apt.conf.d/20auto-upgrades
fi
# Remove unattended-upgrades file
if [[ -r /etc/apt/apt.conf.d/50unattended-upgrades ]]; then
rm /etc/apt/apt.conf.d/50unattended-upgrades
fi
# Remove user pi from 'adm' group
if id --groups --name "${DEFAULT_USER}" | grep -q adm; then
echo "Remove ${DEFAULT_USER} from adm group"
deluser "${DEFAULT_USER}" adm
fi
# Create bitcoinuser - this user runs the bitcoind process
if ! id "${BITCOIN_USER}" >> /dev/null; then
echo "Add ${BITCOIN_USER} to Onion Node"
useradd --create-home "${BITCOIN_USER}"
fi
# Lockdown bitcoinuser account - disable shell access and disable login
usermod --shell /usr/sbin/nologin --lock --expiredate 1 "${BITCOIN_USER}"
# Create directories
mkdir -p /root/.gnupg/
mkdir -p "${BITCOIN_DIR}"
# Copy files to correct locations
cp "${CONFIG_FILES}"/sysctl-kernel-hardening.conf /etc/sysctl.d/
cp "${CONFIG_FILES}"/sources.list /etc/apt/
cp "${CONFIG_FILES}"/collabora.list /etc/apt/sources.list.d/
cp "${CONFIG_FILES}"/raspi.list /etc/apt/sources.list.d/
cp "${CONFIG_FILES}"/interfaces /etc/network/
cp "${CONFIG_FILES}"/dhclient.conf /etc/dhcp/
cp "${CONFIG_FILES}"/gitconfig /root/.gitconfig
cp "${CONFIG_FILES}"/sudoers /etc/
cp "${CONFIG_FILES}"/00-discard-dhclient.conf /etc/rsyslog.d/
cp "${CONFIG_FILES}"/gpg.conf /root/.gnupg/
cp "${CONFIG_FILES}"/hkps.pool.sks-keyservers.net.pem /etc/ssl/certs/
cp "${CONFIG_FILES}"/bitcoin.conf "${BITCOIN_DIR}"
# Set correct file/folder permissions
chmod 644 /etc/sysctl.d/sysctl-kernel-hardening.conf
chown root:root /etc/sysctl.d/sysctl-kernel-hardening.conf
chmod 644 /etc/apt/sources.list
chown root:root /etc/apt/sources.list
chmod 644 /etc/apt/sources.list.d/collabora.list /etc/apt/sources.list.d/raspi.list
chown root:root /etc/apt/sources.list.d/collabora.list /etc/apt/sources.list.d/raspi.list
chmod 744 /etc/network/interfaces
chown root:root /etc/network/interfaces
chmod 644 /etc/dhcp/dhclient.conf
chown root:root /etc/dhcp/dhclient.conf
chmod 644 /root/.gitconfig
chown root:root /root/.gitconfig
chmod 440 /etc/sudoers
chown root:root /etc/sudoers
chmod 644 /etc/rsyslog.d/00-discard-dhclient.conf
chown root:root /etc/rsyslog.d/00-discard-dhclient.conf
chmod 700 /root/.gnupg/
chmod 600 /root/.gnupg/gpg.conf
chown -R root:root /root/.gnupg/
chmod 644 /etc/ssl/certs/hkps.pool.sks-keyservers.net.pem
chown root:root /etc/ssl/certs/hkps.pool.sks-keyservers.net.pem
chmod 700 "${BITCOIN_DIR}"
chown -R "${BITCOIN_USER}":"${BITCOIN_USER}" "${BITCOIN_DIR}"
# Run iptables-config.sh to configure iptables
"${INSTALL_SCRIPTS}"/iptables-config-pre-tor.sh
iptables-save > "${ONION_DIR}"/iptables.rules
# Remove unnecessary packages - assume yes '-y'
"${INSTALL_SCRIPTS}"/apt-remove.sh
# Configure network interface to go up
ifdown eth0
ifup eth0
# Wait for DHCP ip address
echo ""
echo "Wait for DHCP ip address...sleeping 120 seconds"
echo "Now is a good time to plug in the network cable...if you have not done so yet"
echo "Otherwise, sit back and relax"
echo ""
sleep 120
# Install latest updates and Tor - assume yes '-y'
"${INSTALL_SCRIPTS}"/apt-install-tor.sh
# Allow Tor proces to connect to the web
"${INSTALL_SCRIPTS}"/iptables-config.sh
iptables-save > "${ONION_DIR}"/iptables.rules
# Put torrc at correct location
cp /etc/tor/torrc /etc/tor/torrc-backup
cp "${CONFIG_FILES}"/torrc /etc/tor/torrc
chmod 644 /etc/tor/torrc
chown debian-tor:debian-tor /etc/tor/torrc
/etc/init.d/tor restart
sleep 30
# Run tor-date-check
rmdir "${LOCK_DIR}" # tor-date-check.sh has it's own lockfile
"${ONION_DIR}"/tor-date-check.sh
# Check if a lockfile/LOCKDIR exists, wait max 2 hours
tries=0
while [[ -d "${LOCK_DIR}" ]] && [[ "${tries}" -lt 120 ]]; do
echo "Temporarily not able to acquire lock on ${LOCK_DIR}"
echo "Other processes might be running...retry in 60 seconds"
sleep 60
tries=$(( ${tries} +1 ))
done
# Set lockfile/dir - mkdir is atomic
# For portability flock or other Linux only tools are not used
if mkdir "${LOCK_DIR}"; then
trap 'rmdir "${LOCK_DIR}"; exit' INT TERM EXIT # remove LOCKDIR when script is interrupted, terminated or finished
echo "Successfully acquired lock on ${LOCK_DIR}"
else
echo "Failed to acquire lock on ${LOCK_DIR}"
echo "The installation script failed...run the install.sh script again to see if you get better results."
echo "Tip: Reboot the system if the installation keeps failling."
exit 0
fi
# Check if APTPACKAGE is installed, if not run apt-install-packages.sh
# Sometimes Tor is really slow to setup a circuit and needs a request to get started
# So the apt-get requests might fail the first time, because no Tor circuit is available
# Hopefully apt will work in a later run...
tries=0
while [[ ! $(dpkg-query -W "${APT_PACKAGE}" 2>/dev/null ) ]] && [[ "${tries}" -lt 20 ]]; do
echo "${APT_PACKAGE} is not installed...will run apt-install-packages.sh"
"${INSTALL_SCRIPTS}"/apt-install-packages.sh
sleep 30
tries=$(( ${tries} +1 ))
if [[ $tries -eq 20 ]]; then
echo "ERROR: ${APT_PACKAGE} is not installed"
echo "The installation has failed...probably due to network/Tor issues"
echo "Check network/Tor connection and run the installer again and see if you get better results"
echo "The installation is aborted"
exit 0
fi
done
# Download GPG keys
"${INSTALL_SCRIPTS}"/download-gpg-keys.sh
# Install tlsdate from source
rmdir "${LOCK_DIR}" # install-tlsdate.sh has it's own lockfile
"${INSTALL_SCRIPTS}"/install-tlsdate.sh
# Check if a lockfile/LOCKDIR exists, wait max 2 hours
tries=0
while [[ -d "${LOCK_DIR}" ]] && [[ "${tries}" -lt 120 ]]; do
echo "Temporarily not able to acquire lock on ${LOCK_DIR}"
echo "Other processes might be running...retry in 60 seconds"
sleep 60
tries=$(( ${tries} +1 ))
done
# Set lockfile/dir - mkdir is atomic
# For portability flock or other Linux only tools are not used
if mkdir "${LOCK_DIR}"; then
trap 'rmdir "${LOCK_DIR}"; exit' INT TERM EXIT # remove LOCKDIR when script is interrupted, terminated or finished
echo "Successfully acquired lock on ${LOCK_DIR}"
else
echo "Failed to acquire lock on ${LOCK_DIR}"
echo "The installation script failed...run the install.sh script again to see if you get better results."
echo "Tip: Reboot the system if the installation keeps failling."
exit 0
fi
# Install bitcoin from source
rmdir "${LOCK_DIR}" # install-bitcoin.sh has it's own lockfile
"${INSTALL_SCRIPTS}"/install-bitcoin.sh
# Check if a lockfile/LOCKDIR exists, wait max 2 hours
tries=0
while [[ -d "${LOCK_DIR}" ]] && [[ "${tries}" -lt 120 ]]; do
echo "Temporarily not able to acquire lock on ${LOCK_DIR}"
echo "Other processes might be running...retry in 60 seconds"
sleep 60
tries=$(( ${tries} +1 ))
done
# Set lockfile/dir - mkdir is atomic
# For portability flock or other Linux only tools are not used
if mkdir "${LOCK_DIR}"; then
trap 'rmdir "${LOCK_DIR}"; exit' INT TERM EXIT # remove LOCKDIR when script is interrupted, terminated or finished
echo "Successfully acquired lock on ${LOCK_DIR}"
else
echo "Failed to acquire lock on ${LOCK_DIR}"
echo "The installation script failed...run the install.sh script again to see if you get better results."
echo "Tip: Reboot the system if the installation keeps failling."
exit 0
fi
# Install bitcoin node crontabs
"${INSTALL_SCRIPTS}"/install-crontabs.sh
# Copy dhcp-script-bitcoin-node to correct location
cp "${CONFIG_FILES}"/dhcp-script-bitcoin-node /etc/dhcp/dhclient-exit-hooks.d/
chmod 744 /etc/dhcp/dhclient-exit-hooks.d/dhcp-script-bitcoin-node
chown root:root /etc/dhcp/dhclient-exit-hooks.d/dhcp-script-bitcoin-node
# Copy unattended-upgrade files to correct location
cp "${CONFIG_FILES}"/20auto-upgrades /etc/apt/apt.conf.d/
cp "${CONFIG_FILES}"/50unattended-upgrades /etc/apt/apt.conf.d/
chmod 644 /etc/apt/apt.conf.d/20auto-upgrades /etc/apt/apt.conf.d/50unattended-upgrades
chown root:root /etc/apt/apt.conf.d/20auto-upgrades /etc/apt/apt.conf.d/50unattended-upgrades
# Done
echo "Script is done...system will reboot in 30 seconds"
sleep 30
shutdown -r now