forked from openoms/joininbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_joininbox.sh
517 lines (481 loc) · 16.6 KB
/
build_joininbox.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#!/bin/bash
########################################################################
# setup a Linux environment see:
# https://github.com/openoms/joininbox#tested-environments-for-joininbox
# login with SSH or boot directly
# run this script as root or with sudo
# can specify donwloading from a branch or forked repo:
# bash build_joininbox.sh [branch] [github user]
########################################################################
# The JoininBox Build Script is partially based on:
# https://github.com/rootzoll/raspiblitz/blob/master/build_sdcard.sh
# command info
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "JoininBox Build Script"
echo "Usage: bash build_joininbox.sh [branch] [github user]"
echo "Example: bash bash build_joininbox.sh dev openoms"
exit 1
fi
# check if sudo
if [ "$EUID" -ne 0 ]; then
echo "Please run as root or with sudo"
echo "Root access is needed to create the dedicated user to install system dependencies"
exit 1
fi
echo
echo "##########################"
echo "# JOININBOX BUILD SCRIPT #"
echo "##########################"
echo
echo "# Check the command options"
wantedBranch="$1"
if [ ${#wantedBranch} -eq 0 ]; then
wantedBranch="master"
fi
githubUser="$2"
if [ ${#githubUser} -eq 0 ]; then
githubUser="openoms"
fi
echo "
# Installing JoininBox from:
# https://github.com/${githubUser}/joininbox/tree/${wantedBranch}
# Press ENTER to confirm or CTRL+C to exit"
read key
echo
echo "###################################"
echo "# Identify the CPU and base image #"
echo "###################################"
echo
cpu=$(uname -m)
echo "# CPU: ${cpu}"
baseImage="?"
isBuster=$(grep -c 'buster' < /etc/os-release)
isBionic=$(grep -c 'bionic' < /etc/os-release)
isFocal=$(grep -c 'focal' < /etc/os-release)
isDietPi=$(uname -n | grep -c 'DietPi')
isRaspbian=$(grep -c 'Raspbian' < /etc/os-release)
if [ ${isBuster} -gt 0 ]; then
baseImage="buster"
fi
if [ ${isBionic} -gt 0 ]; then
baseImage="bionic"
fi
if [ ${isFocal} -gt 0 ]; then
baseImage="focal"
fi
if [ ${isDietPi} -gt 0 ]; then
baseImage="dietpi"
fi
if [ ${isRaspbian} -gt 0 ]; then
baseImage="raspbian"
fi
if [ "${baseImage}" = "?" ]; then
cat /etc/os-release 2>/dev/null
echo "# !!! FAIL !!!"
echo "# Base image cannot be detected or is not supported."
exit 1
else
echo "# Base image: ${baseImage}"
fi
echo
echo "############################"
echo "# Preparing the base image #"
echo "############################"
echo
if [ "${baseImage}" = "raspbian" ]||[ "${baseImage}" = "dietpi" ]||\
[ "${baseImage}" = "buster" ]; then
# fixing locales for build
# https://github.com/rootzoll/raspiblitz/issues/138
# https://daker.me/2014/10/how-to-fix-perl-warning-setting-locale-failed-in-raspbian.html
# https://stackoverflow.com/questions/38188762/generate-all-locales-in-a-docker-image
echo
echo "# FIXING LOCALES FOR BUILD "
apt install -y locales
sed -i "s/^# en_US.UTF-8 UTF-8.*/en_US.UTF-8 UTF-8/g" /etc/locale.gen
sed -i "s/^# en_US ISO-8859-1.*/en_US ISO-8859-1/g" /etc/locale.gen
locale-gen
export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
# https://github.com/rootzoll/raspiblitz/issues/684
sed -i "s/^ SendEnv LANG LC.*/# SendEnv LANG LC_*/g" /etc/ssh/ssh_config
# only on RaspberryOS
# remove unnecessary files
rm -rf /home/pi/MagPi
# https://www.reddit.com/r/linux/comments/lbu0t1/microsoft_repo_installed_on_all_raspberry_pis/
rm -f /etc/apt/sources.list.d/vscode.list
rm -f /etc/apt/trusted.gpg.d/microsoft.gpg
fi
if [ -f "/usr/bin/python3.7" ]; then
# make sure /usr/bin/python exists (and calls Python3.7 in Debian Buster)
update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1
echo "# python calls python3.7"
elif [ -f "/usr/bin/python3.8" ]; then
# use python 3.8 if available
update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
echo "# python calls python3.8"
else
echo "!!! FAIL !!!"
echo "There is no tested version of python present"
exit 1
fi
echo
echo "# PREPARE ${baseImage} "
# special prepare when Raspbian
if [ "${baseImage}" = "raspbian" ]; then
# do memory split (16MB)
raspi-config nonint do_memory_split 16
# set to wait until network is available on boot (0 seems to yes)
raspi-config nonint do_boot_wait 0
# set WIFI country so boot does not block
raspi-config nonint do_wifi_country US
# see https://github.com/rootzoll/raspiblitz/issues/428#issuecomment-472822840
echo "max_usb_current=1" |tee -a /boot/config.txt
# run fsck on sd boot partition on every startup to prevent "maintenance login" screen
# see: https://github.com/rootzoll/raspiblitz/issues/782#issuecomment-564981630
# use command to check last fsck check: tune2fs -l /dev/mmcblk0p2
tune2fs -c 1 /dev/mmcblk0p2
# see https://github.com/rootzoll/raspiblitz/issues/1053#issuecomment-600878695
sed -i 's/^/fsck.mode=force fsck.repair=yes /g' /boot/cmdline.txt
fi
echo
echo "# change log rotates"
# see https://github.com/rootzoll/raspiblitz/issues/394#issuecomment-471535483
echo "/var/log/syslog" >> ./rsyslog
echo "{" >> ./rsyslog
echo " rotate 7" >> ./rsyslog
echo " daily" >> ./rsyslog
echo " missingok" >> ./rsyslog
echo " notifempty" >> ./rsyslog
echo " delaycompress" >> ./rsyslog
echo " compress" >> ./rsyslog
echo " postrotate" >> ./rsyslog
echo " invoke-rc.d rsyslog rotate > /dev/null" >> ./rsyslog
echo " endscript" >> ./rsyslog
echo "}" >> ./rsyslog
echo "" >> ./rsyslog
echo "/var/log/mail.info" >> ./rsyslog
echo "/var/log/mail.warn" >> ./rsyslog
echo "/var/log/mail.err" >> ./rsyslog
echo "/var/log/mail.log" >> ./rsyslog
echo "/var/log/daemon.log" >> ./rsyslog
echo "{" >> ./rsyslog
echo " rotate 4" >> ./rsyslog
echo " size=100M" >> ./rsyslog
echo " missingok" >> ./rsyslog
echo " notifempty" >> ./rsyslog
echo " compress" >> ./rsyslog
echo " delaycompress" >> ./rsyslog
echo " sharedscripts" >> ./rsyslog
echo " postrotate" >> ./rsyslog
echo " invoke-rc.d rsyslog rotate > /dev/null" >> ./rsyslog
echo " endscript" >> ./rsyslog
echo "}" >> ./rsyslog
echo "" >> ./rsyslog
echo "/var/log/kern.log" >> ./rsyslog
echo "/var/log/auth.log" >> ./rsyslog
echo "{" >> ./rsyslog
echo " rotate 4" >> ./rsyslog
echo " size=100M" >> ./rsyslog
echo " missingok" >> ./rsyslog
echo " notifempty" >> ./rsyslog
echo " compress" >> ./rsyslog
echo " delaycompress" >> ./rsyslog
echo " sharedscripts" >> ./rsyslog
echo " postrotate" >> ./rsyslog
echo " invoke-rc.d rsyslog rotate > /dev/null" >> ./rsyslog
echo " endscript" >> ./rsyslog
echo "}" >> ./rsyslog
echo "" >> ./rsyslog
echo "/var/log/user.log" >> ./rsyslog
echo "/var/log/lpr.log" >> ./rsyslog
echo "/var/log/cron.log" >> ./rsyslog
echo "/var/log/debug" >> ./rsyslog
echo "/var/log/messages" >> ./rsyslog
echo "{" >> ./rsyslog
echo " rotate 4" >> ./rsyslog
echo " weekly" >> ./rsyslog
echo " missingok" >> ./rsyslog
echo " notifempty" >> ./rsyslog
echo " compress" >> ./rsyslog
echo " delaycompress" >> ./rsyslog
echo " sharedscripts" >> ./rsyslog
echo " postrotate" >> ./rsyslog
echo " invoke-rc.d rsyslog rotate > /dev/null" >> ./rsyslog
echo " endscript" >> ./rsyslog
echo "}" >> ./rsyslog
mv ./rsyslog /etc/logrotate.d/rsyslog
chown root:root /etc/logrotate.d/rsyslog
service rsyslog restart
echo
echo "###############################"
echo "# Apt update & upgrade #"
echo "###############################"
echo
apt-get update -y
apt-get upgrade -f -y
echo
echo "##########################"
echo "# Tools and dependencies #"
echo "##########################"
echo
apt-get install -y htop git curl bash-completion vim jq bsdmainutils
# prepare for display graphics mode
# see https://github.com/rootzoll/raspiblitz/pull/334
apt-get install -y fbi
# check for dependencies on DietPi, Ubuntu, Armbian
apt install -y build-essential
# dependencies for python
apt install -y python3-venv python3-dev python3-wheel python3-jinja2 \
python3-pip
# make sure /usr/bin/pip exists (and calls pip3 in Debian Buster)
update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
# install ifconfig
apt install -y net-tools
# to display hex codes
apt install -y xxd
# setuptools needed for Nyx
pip install setuptools
# netcat
apt install -y netcat
# install killall, fuser
apt-get install -y psmisc
# dialog
apt install -y dialog
# qrencode
apt install -y qrencode
# unzip for the pruned node snapshot
apt install -y unzip
apt-get clean
apt-get -y autoremove
echo
echo "#############"
echo "# JoininBox #"
echo "#############"
echo
echo "# add the 'joinmarket' user"
adduser --disabled-password --gecos "" joinmarket
echo "# clone the joininbox repo and copy the scripts"
cd /home/joinmarket || exit 1
sudo -u joinmarket git clone -b ${wantedBranch} https://github.com/${githubUser}/joininbox.git
sudo -u joinmarket cp ./joininbox/scripts/* /home/joinmarket/
sudo -u joinmarket cp ./joininbox/scripts/.* /home/joinmarket/ 2>/dev/null
chmod +x /home/joinmarket/*.sh
sudo -u joinmarket cp -r ./joininbox/scripts/standalone /home/joinmarket/
chmod +x /home/joinmarket/standalone/*.sh
echo "# set the default password 'joininbox' for the users 'pi', \
'joinmarket' and 'root'"
adduser joinmarket sudo
# chsh joinmarket -s /bin/bash
# configure for usage without password entry for the joinmarket user
# https://www.tecmint.com/run-sudo-command-without-password-linux/
echo 'joinmarket ALL=(ALL) NOPASSWD:ALL' | EDITOR='tee -a' visudo
echo "root:joininbox" | chpasswd
echo "joinmarket:joininbox" | chpasswd
if [ $(grep -c pi < /etc/passwd) -gt 0 ];then
echo "pi:joininbox" | chpasswd
fi
echo "# create the joinin.conf"
sudo -u joinmarket touch /home/joinmarket/joinin.conf
echo
echo "#######"
echo "# Tor #"
echo "#######"
echo
# add default value to joinin config if needed
checkTorEntry=$(sudo -u joinmarket cat /home/joinmarket/joinin.conf | \
grep -c "runBehindTor")
if [ ${checkTorEntry} -eq 0 ]; then
echo "runBehindTor=off" | tee -a /home/joinmarket/joinin.conf
fi
torTest=$(curl --socks5 localhost:9050 --socks5-hostname localhost:9050 -s \
https://check.torproject.org/ | cat | grep -m 1 Congratulations | xargs)
if [ "$torTest" != "Congratulations. This browser is configured to use Tor." ]
then
echo "# install the Tor repo"
echo
echo "# Install dirmngr"
apt install -y dirmngr apt-transport-https
echo
echo "# Adding KEYS deb.torproject.org "
torKeyAvailable=$(gpg --list-keys | grep -c \
"A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89")
echo "torKeyAvailable=${torKeyAvailable}"
if [ ${torKeyAvailable} -eq 0 ]; then
# https://support.torproject.org/apt/tor-deb-repo/
wget -qO- https://deb.torproject.org/torproject.org/A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89.asc | gpg --import
gpg --export A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89 | apt-key add -
echo "OK"
else
echo "# Tor key is available"
fi
echo "# Adding Tor Sources to sources.list"
torSourceListAvailable=$(cat /etc/apt/sources.list | grep -c \
'https://deb.torproject.org/torproject.org')
echo "torSourceListAvailable=${torSourceListAvailable}"
if [ ${torSourceListAvailable} -eq 0 ]; then
echo "Adding Tor sources ..."
if [ "${baseImage}" = "raspbian" ]||[ "${baseImage}" = "buster" ]||[ "${baseImage}" = "dietpi" ]; then
echo "deb https://deb.torproject.org/torproject.org buster main" | tee -a /etc/apt/sources.list
echo "deb-src https://deb.torproject.org/torproject.org buster main" | tee -a /etc/apt/sources.list
elif [ "${baseImage}" = "bionic" ]; then
echo "deb https://deb.torproject.org/torproject.org bionic main" | tee -a /etc/apt/sources.list
echo "deb-src https://deb.torproject.org/torproject.org bionic main" | tee -a /etc/apt/sources.list
elif [ "${baseImage}" = "focal" ]; then
echo "deb https://deb.torproject.org/torproject.org focal main" | tee -a /etc/apt/sources.list
echo "deb-src https://deb.torproject.org/torproject.org focal main" | tee -a /etc/apt/sources.list
fi
echo "OK"
else
echo "Tor sources are available"
fi
apt update
if [ "$cpu" = "armv6l" ]; then
# https://2019.www.torproject.org/docs/debian#source
echo "# running on armv6l - need to compile Tor from source"
apt install -y build-essential fakeroot devscripts
apt build-dep -y tor deb.torproject.org-keyring
mkdir ~/debian-packages; cd ~/debian-packages
apt source tor
cd tor-* || exit 1
debuild -rfakeroot -uc -us
cd .. || exit 1
dpkg -i tor_*.deb
# setup Tor in the backgound
# TODO - test if remains in the background after the Tor service is started
tor &
else
echo "# Install Tor"
apt install -y tor
fi
fi
# test Tor
tries=0
while [ "$torTest" != "Congratulations. This browser is configured to use Tor." ]
do
echo "# waiting another 10 seconds for Tor"
echo "# press CTRL + C to abort"
sleep 10
tries=$((tries+1))
if [ $tries = 100 ]; then
echo "# FAIL - Tor was not set up successfully"
exit 1
fi
torTest=$(curl --socks5 localhost:9050 --socks5-hostname localhost:9050 -s \
https://check.torproject.org/ | cat | grep -m 1 Congratulations | xargs)
done
echo
echo "# $torTest"
echo
echo "# Tor has been tested successfully"
echo
echo "# install torsocks and nyx"
apt install -y torsocks tor-arm
# Tor config
# torrc
if ! grep -Eq "^DataDirectory" /etc/tor/torrc; then
echo "DataDirectory /var/lib/tor" | tee -a /etc/tor/torrc
fi
if ! grep -Eq "^ControlPort 9051" /etc/tor/torrc; then
echo "ControlPort 9051" | tee -a /etc/tor/torrc
fi
if ! grep -Eq "^CookieAuthentication 1" /etc/tor/torrc; then
echo "CookieAuthentication 1" | tee -a /etc/tor/torrc
fi
sed -i "s:^CookieAuthFile*:#CookieAuthFile:g" /etc/tor/torrc
# torsocks.conf
if ! grep -Eq "^AllowOutboundLocalhost 1" /etc/tor/torsocks.conf; then
echo "AllowOutboundLocalhost 1" | tee -a /etc/tor/torsocks.conf
fi
# add the joinmarket user to the tor group
usermod -a -G debian-tor joinmarket
# setting value in joinin config
sed -i "s/^runBehindTor=.*/runBehindTor=on/g" /home/joinmarket/joinin.conf
echo
echo "#############"
echo "# Hardening #"
echo "#############"
echo
# install packages
apt install -y virtualenv fail2ban ufw
# autostart fail2ban
systemctl enable fail2ban
# set up the firewall
ufw default deny incoming
ufw default allow outgoing
ufw allow 22 comment 'allow SSH'
old_kernel=$(uname -a | grep -c "4.14.165")
if [ $old_kernel -gt 0 ]; then
# due to the old kernel iptables needs to be configured
# https://superuser.com/questions/1480986/iptables-1-8-2-failed-to-initialize-nft-protocol-not-supported
echo "switching to iptables-legacy"
update-alternatives --set iptables /usr/sbin/iptables-legacy
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
fi
echo "# enabling the firewall"
ufw --force enable
systemctl enable ufw
ufw status
# make a folder for authorized keys
sudo -u joinmarket mkdir -p /home/joinmarket/.ssh
chmod -R 700 /home/joinmarket/.ssh
# deny root login via ssh
if grep -Eq "^PermitRootLogin" /etc/ssh/sshd_config; then
sed -i "s/^PermitRootLogin.*/PermitRootLogin no/g" /etc/ssh/sshd_config
else
echo "PermitRootLogin no" >> /etc/ssh/sshd_config
fi
systemctl restart ssh
echo
echo "##########"
echo "# Extras #"
echo "##########"
echo
# install a command-line fuzzy finder (https://github.com/junegunn/fzf)
apt -y install fzf
bash -c "echo 'source /usr/share/doc/fzf/examples/key-bindings.bash' >> \
/home/joinmarket/.bashrc"
# install tmux
apt -y install tmux
echo
echo "#############"
echo "# Autostart #"
echo "#############"
echo "
if [ -f \"/home/joinmarket/joinmarket-clientserver/jmvenv/bin/activate\" ]; then
. /home/joinmarket/joinmarket-clientserver/jmvenv/bin/activate
/home/joinmarket/joinmarket-clientserver/jmvenv/bin/python -c \"import PySide2\"
cd /home/joinmarket/joinmarket-clientserver/scripts/
fi
# shortcut commands
source /home/joinmarket/_commands.sh
# automatically start main menu for joinmarket unless
# when running in a tmux session
if [ -z \"\$TMUX\" ]; then
/home/joinmarket/menu.sh
fi
" | sudo -u joinmarket tee -a /home/joinmarket/.bashrc
echo "#########################"
echo "# Download Bitcoin Core #"
echo "#########################"
echo
sudo -u joinmarket /home/joinmarket/install.bitcoincore.sh downloadCoreOnly
echo
echo "######################"
echo "# Install JoinMarket #"
echo "######################"
sudo -u joinmarket /home/joinmarket/install.joinmarket.sh install
echo
echo "###########################"
echo "# The Base Image is ready #"
echo "###########################"
echo
echo "Look through / save this output and continue with:"
echo "'su - joinmarket'"
echo
echo "To make an SDcard image safe to share use:"
echo "'/home/joinmarket/standalone/prepare.release.sh'"
echo
echo "the ssh login credentials are until the first login:"
echo "user:joinmarket"
echo "password:joininbox"
echo