forked from RPi-Distro/raspi-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raspi-config-wng
executable file
·488 lines (438 loc) · 14.7 KB
/
raspi-config-wng
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
#!/bin/sh
# Taken from https://github.com/RPi-Distro/raspi-config
#
# See LICENSE file for copyright and license details
VERSION=0.1
INTERACTIVE=True
ASK_TO_REBOOT=0
CONFIG=/boot/config.txt
export NEWT_COLORS='
root=,brightcyan
'
BACKTITLE="WiNoGATE (C) 2019 University of Applied Sciences Zwickau"
WN_TITLE="Configuration Tool for WiNoGATE (raspi-config-wng) v$VERSION"
BTN_BACK=Zurück
logger WiNoGate Configuration Tool started
# --------------------------------------------------------------------------------
is_pi () {
ARCH=$(dpkg --print-architecture)
if [ "$ARCH" = "armhf" ] ; then
return 0
else
return 1
fi
}
if is_pi ; then
CMDLINE=/boot/cmdline.txt
else
CMDLINE=/proc/cmdline
fi
# --------------------------------------------------------------------------------
is_pione() {
if grep -q "^Revision\s*:\s*00[0-9a-fA-F][0-9a-fA-F]$" /proc/cpuinfo; then
return 0
elif grep -q "^Revision\s*:\s*[ 123][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]0[0-36][0-9a-fA-F]$" /proc/cpuinfo ; then
return 0
else
return 1
fi
}
# --------------------------------------------------------------------------------
is_pitwo() {
grep -q "^Revision\s*:\s*[ 123][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]04[0-9a-fA-F]$" /proc/cpuinfo
return $?
}
# --------------------------------------------------------------------------------
is_pizero() {
grep -q "^Revision\s*:\s*[ 123][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]0[9cC][0-9a-fA-F]$" /proc/cpuinfo
return $?
}
# --------------------------------------------------------------------------------
is_pifour() {
grep -q "^Revision\s*:\s*[ 123][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]11[0-9a-fA-F]$" /proc/cpuinfo
return $?
}
# --------------------------------------------------------------------------------
get_pi_type() {
if is_pione; then
echo 1
elif is_pitwo; then
echo 2
else
echo 0
fi
}
# --------------------------------------------------------------------------------
is_live() {
grep -q "boot=live" $CMDLINE
return $?
}
# --------------------------------------------------------------------------------
is_ssh() {
if pstree -p | egrep --quiet --extended-regexp ".*sshd.*\($$\)"; then
return 0
else
return 1
fi
}
# --------------------------------------------------------------------------------
is_fkms() {
if grep -q okay /proc/device-tree/soc/v3d@7ec00000/status 2> /dev/null || grep -q okay /proc/device-tree/soc/firmwarekms@7e600000/status 2> /dev/null ; then
return 0
else
return 1
fi
}
# --------------------------------------------------------------------------------
deb_ver () {
ver=`cat /etc/debian_version | cut -d . -f 1`
echo $ver
}
# --------------------------------------------------------------------------------
calc_wt_size() {
# NOTE: it's tempting to redirect stderr to /dev/null, so supress error
# output from tput. However in this case, tput detects neither stdout or
# stderr is a tty and so only gives default 80, 24 values
WT_HEIGHT=17
WT_WIDTH=$(tput cols)
if [ -z "$WT_WIDTH" ] || [ "$WT_WIDTH" -lt 60 ]; then
WT_WIDTH=80
fi
if [ "$WT_WIDTH" -gt 178 ]; then
WT_WIDTH=120
fi
WT_MENU_HEIGHT=$(($WT_HEIGHT-7))
}
# --------------------------------------------------------------------------------
do_about() {
whiptail --msgbox "\
This tool provides a straight-forward way of doing initial configuration of the Raspberry Pi for use as WiNoGATE Gateway at the WindNODE Research Project (www.windnode.de).
Although it can be run at any time, some of the options may have difficulties if you have heavily customised your installation.\
" 20 70 1
}
# --------------------------------------------------------------------------------
#
# --------------------------------------------------------------------------------
do_winogate_install_test1() {
whiptail --title "Software-Installation" --checklist --separate-output "Choose:" 20 78 15 \
"ovpn" "OpenVPN Client" ON \
"ufw" "Firewall" ON \
"mc" "Midnight Commander" OFF \
"lnav" "LNAV" OFF \
"symcon" "IP-Symcon 5.1" off 2>results
while read choice
do
case $choice in
ovpn) echo "Installing OpenVPN..."
;;
mc) echo "Installing MC..."
;;
#lnav) echo "Installing lnav..."
lnav) apt-get install -y lnav ;;
symcon) echo "Installing IP-Symcon..."
;;
*)
;;
esac
done < results
}
# --------------------------------------------------------------------------------
#
# --------------------------------------------------------------------------------
do_winogate_install_test2() {
SEL=$(whiptail --title "Check list example" --checklist \
"Choose user's permissions" 20 78 4 \
"nopasswd" "Erlaube SSH nur mit Zertifikat, kein Passwort" ON \
"sudoer" "Benutzer 'windnode' in Sudoers aufnehmen" OFF \
"..." "..." OFF 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ];
then echo "Selected: $SEL"
else echo "Cancelled."
fi
}
# --------------------------------------------------------------------------------
#
# --------------------------------------------------------------------------------
do_winogate_test2() {
WNGID="$1"
while [ -z "$WNGID" ] && [ "$INTERACTIVE" = True ]; do
WNGID=$(whiptail --inputbox "Please enter WNGID" 20 60 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then
return 0
elif [ -z "$WNGID" ]; then
whiptail --msgbox "WNGID cannot be empty. Please try again." 20 60
fi
done
whiptail --msgbox "WNGID you entered: $WNGID" 20 60 1
}
# --------------------------------------------------------------------------------
# Create user 'windnode' and set authorized_keys
# --------------------------------------------------------------------------------
do_create_wng_user() {
RES=$(sudo useradd -d /home/windnode -s /bin/bash -m windnode)
return 0
}
# --------------------------------------------------------------------------------
# Install Prerequisites/Tools for Development: ufw
# --------------------------------------------------------------------------------
do_install_wng_dev() {
sudo apt-get update && sudo apt-get dist-upgrade $$ sudo apt-get install lnav
}
# --------------------------------------------------------------------------------
# Install Prerequisited for Production: ufw
# --------------------------------------------------------------------------------
do_install_wng_prod() {
sudo apt-get update && sudo apt-get dist-upgrade $$ sudo apt-get install ufw
}
# --------------------------------------------------------------------------------
do_change_timezone() {
if [ "$INTERACTIVE" = True ]; then
dpkg-reconfigure tzdata
else
local TIMEZONE="$1"
if [ ! -f "/usr/share/zoneinfo/$TIMEZONE" ]; then
return 1;
fi
rm /etc/localtime
echo "$TIMEZONE" > /etc/timezone
dpkg-reconfigure -f noninteractive tzdata
fi
}
# --------------------------------------------------------------------------------
get_wifi_country() {
CODE=${1:-0}
IFACE="$(list_wlan_interfaces | head -n 1)"
if [ -z "$IFACE" ]; then
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "No wireless interface found" 20 60
fi
return 1
fi
if ! wpa_cli -i "$IFACE" status > /dev/null 2>&1; then
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "Could not communicate with wpa_supplicant" 20 60
fi
return 1
fi
wpa_cli -i "$IFACE" save_config > /dev/null 2>&1
COUNTRY="$(wpa_cli -i "$IFACE" get country)"
if [ "$COUNTRY" = "FAIL" ]; then
return 1
fi
if [ $CODE = 0 ]; then
echo "$COUNTRY"
fi
return 0
}
# --------------------------------------------------------------------------------
do_wifi_country() {
IFACE="$(list_wlan_interfaces | head -n 1)"
if [ -z "$IFACE" ]; then
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "No wireless interface found" 20 60
fi
return 1
fi
if ! wpa_cli -i "$IFACE" status > /dev/null 2>&1; then
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "Could not communicate with wpa_supplicant" 20 60
fi
return 1
fi
oIFS="$IFS"
if [ "$INTERACTIVE" = True ]; then
IFS="/"
value=$(cat /usr/share/zoneinfo/iso3166.tab | tail -n +26 | tr '\t' '/' | tr '\n' '/')
COUNTRY=$(whiptail --menu "Select the country in which the Pi is to be used" 20 60 10 ${value} 3>&1 1>&2 2>&3)
IFS=$oIFS
else
COUNTRY=$1
true
fi
if [ $? -eq 0 ];then
wpa_cli -i "$IFACE" set country "$COUNTRY"
wpa_cli -i "$IFACE" save_config > /dev/null 2>&1
if ! iw reg set "$COUNTRY" 2> /dev/null; then
ASK_TO_REBOOT=1
fi
if hash rfkill 2> /dev/null; then
rfkill unblock wifi
fi
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "Wi-fi country set to $COUNTRY" 20 60 1
fi
fi
}
# --------------------------------------------------------------------------------
do_hostname() {
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "\
Please note: RFCs mandate that a hostname's labels \
may contain only the ASCII letters 'a' through 'z' (case-insensitive),
the digits '0' through '9', and the hyphen.
Hostname labels cannot begin or end with a hyphen.
No other symbols, punctuation characters, or blank spaces are permitted.\
" 20 70 1
fi
CURRENT_HOSTNAME=`cat /etc/hostname | tr -d " \t\n\r"`
if [ "$INTERACTIVE" = True ]; then
NEW_HOSTNAME=$(whiptail --inputbox "Please enter a hostname" 20 60 "$CURRENT_HOSTNAME" 3>&1 1>&2 2>&3)
else
NEW_HOSTNAME=$1
true
fi
if [ $? -eq 0 ]; then
echo $NEW_HOSTNAME > /etc/hostname
sed -i "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\t$NEW_HOSTNAME/g" /etc/hosts
ASK_TO_REBOOT=1
fi
}
# --------------------------------------------------------------------------------
get_ssh() {
if service ssh status | grep -q inactive; then
echo 1
else
echo 0
fi
}
# --------------------------------------------------------------------------------
do_ssh() {
if [ -e /var/log/regen_ssh_keys.log ] && ! grep -q "^finished" /var/log/regen_ssh_keys.log; then
whiptail --msgbox "Initial ssh key generation still running. Please wait and try again." 20 60 2
return 1
fi
DEFAULT=--defaultno
if [ $(get_ssh) -eq 0 ]; then
DEFAULT=
fi
if [ "$INTERACTIVE" = True ]; then
whiptail --yesno "Would you like the SSH server to be enabled?" $DEFAULT 20 60 2
RET=$?
else
RET=$1
fi
if [ $RET -eq 0 ]; then
ssh-keygen -A &&
update-rc.d ssh enable &&
invoke-rc.d ssh start &&
STATUS=enabled
elif [ $RET -eq 1 ]; then
update-rc.d ssh disable &&
invoke-rc.d ssh stop &&
STATUS=disabled
else
return $RET
fi
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "The SSH server is $STATUS" 20 60 1
fi
}
# --------------------------------------------------------------------------------
do_update() {
#apt-get update &&
#apt-get install raspi-config &&
#printf "Sleeping 5 seconds before reloading raspi-config\n" &&
#sleep 5 &&
#exec raspi-config
sleep 1
}
# --------------------------------------------------------------------------------
list_wlan_interfaces() {
for dir in /sys/class/net/*/wireless; do
if [ -d "$dir" ]; then
basename "$(dirname "$dir")"
fi
done
}
# --------------------------------------------------------------------------------
do_finish() {
exit
}
# --------------------------------------------------------------------------------
# ################################################################################
# --------------------------------------------------------------------------------
do_winogate_menu() {
FUN=$(whiptail --title "$WN_TITLE" --menu "WiNoGate Optionen" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button $BTN_BACK --ok-button Select \
"P1 Benutzer" "Benutzer 'windnode' anlegen und konfigurieren" \
"P2 Tools" "Hilfsprogramme und Werkzeuge installieren" \
"P1 Apps" "Anwendungen installieren" \
"P1 Konfiguration" "System konfigurieren" \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
elif [ $RET -eq 0 ]; then
case "$FUN" in
P1\ *) do_create_wng_user ;;
P1\ *) do_install_tools ;;
P1\ *) do_install_apps ;;
P1\ *) do_config ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
fi
}
# --------------------------------------------------------------------------------
do_tools_menu() {
FUN=$(whiptail --title "$WN_TITLE" --menu "Werkzeugoptionen" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button $BTN_BACK --ok-button Select \
"P1 SSH" "Enable/Disable remote command line access to your Pi using SSH" \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
elif [ $RET -eq 0 ]; then
case "$FUN" in
P1\ *) do_ssh ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
fi
}
# --------------------------------------------------------------------------------
#
# Interactive use loop
#
if [ "$INTERACTIVE" = True ]; then
[ -e $CONFIG ] || touch $CONFIG
calc_wt_size
while true; do
if is_pi ; then
FUN=$(whiptail --title "$WN_TITLE" --menu "Setup Options" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Ende --ok-button Auswahl \
"1 Setup WiNoGATE" "WiNoGATE Basisinstallation" \
"2 Tools" "Hilfsprogramme" \
"8 Update" "Dieses Tool auf neueste Version bringen" \
"9 About raspi-config" "Information über dieses Konfigurationswerkzeug" \
3>&1 1>&2 2>&3)
else
FUN=$(whiptail --title "$WN_TITLE" --menu "Setup Options" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Finish --ok-button Select \
"1 Setup WiNoGATE" "WiNoGATE Basisinstallation" \
"2 Tools" "Hilfsprogramme" \
"8 Update" "Dieses Tool auf neueste Version bringen" \
"9 About raspi-config" "Information über dieses Konfigurationswerkzeug" \
3>&1 1>&2 2>&3)
fi
RET=$?
if [ $RET -eq 1 ]; then
do_finish
elif [ $RET -eq 0 ]; then
if is_pi ; then
case "$FUN" in
1\ *) do_winogate_menu ;;
2\ *) do_tools_menu ;;
8\ *) do_update ;;
9\ *) do_about ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
else
case "$FUN" in
1\ *) do_winogate_menu ;;
2\ *) do_tools_menu ;;
8\ *) do_update ;;
9\ *) do_about ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
fi
else
exit 1
fi
done
fi