-
Notifications
You must be signed in to change notification settings - Fork 1
/
rrutorrent-install-deamon-script.bash
2646 lines (2354 loc) · 80.7 KB
/
rrutorrent-install-deamon-script.bash
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
export NCURSES_NO_UTF8_ACS=1
#dialog output seperator
separator=":"
#user associated with stdin "who am i"
stdin_user=$(who -m | cut -d' ' -f1)
#Install Logfile
logfile=/home/$stdin_user/install.log
#Remove Logfile
removelogfile=/home/$stdin_user/remove.log
#Output redirection /dev/null or logfile
LOG_REDIRECTION="/dev/null"
#LOG_REDIRECTION=$logfile
#rtorrent daemon user
the_user=rtorrent-daemon
the_group=rtorrent-common
change_on_script=true
#Script versionnumber
script_versionumber="V2.9"
#Fullmenu true,false
fullmenu=false
#Window dimensions
height=20
small_height=6
width=70
#Window position
x=2
small_x=8
y=5
#os-release
#ID: | NAME | VERSION_ID | /etc/debian_version | /etc/issue | /etc/rpi-issue | ->Distribution
#--------------------------------------------------------------------------------------------------------------------------------------------------------------
#linuxmint | LMDE | release number | (debian)point release number | NAME+VERSION \n \l | - | LMDE
# | Linux Mint | point release number | (debian)VERSION_CODENAME/sid | PRETTYNAME+VERSION_CODENAME \n \l | - | Linux Mint
#--------------------------------------------------------------------------------------------------------------------------------------------------------------
#debian | Debian GNU/Linux | release number | point release number | NAME+VERSION_ID \n \l | - | Debian
# | Debian GNU/Linux | release number | point release number | NAME+VERSION_ID \n \l | yes | Raspberry Pi OS
#--------------------------------------------------------------------------------------------------------------------------------------------------------------
#ubuntu | Ubuntu | point release number | (debian)VERSION_CODENAME/sid | PRETTY_NAME \n \l | - | Ubuntu
# | Ubuntu | point release number | (debian)VERSION_CODENAME/sid | PRETTY_NAME \n \l | - | Ubuntu LTS
#--------------------------------------------------------------------------------------------------------------------------------------------------------------
#raspbian | Raspbian GNU/Linux | (debian)release number | (debian)point release number | NAME+VERSION_ID \n \l | yes | Raspbian
#
#system
architecture=$(dpkg --print-architecture)
bits=$(getconf LONG_BIT)
distributor=$(cat /etc/os-release | grep ^ID= | cut -d'=' -f2)
name=$(cat /etc/os-release | grep ^NAME | cut -d'"' -f2)
codename=$(cat /etc/os-release | grep ^VERSION= | cut -d'(' -f2 | cut -d')' -f1)
#https://stackoverflow.com/questions/16396146/using-the-operator-in-an-if-statement
if [[ -e "/etc/debian_version" && $name == "Debian GNU/Linux" ]]
then
version=$(cat /etc/debian_version)
else
version=$(cat /etc/os-release | grep ^VERSION_ID | cut -d'"' -f2)
fi
if [[ -e "/etc/rpi-issue" && $distributor != "raspbian" ]]
then
distributor="$distributor (raspbian)"
fi
if [[ $distributor == "ubuntu" || $distributor == "linuxmint" ]]
then
debian_version=$(cat /etc/debian_version | cut -d'/' -f1)
codename="$codename ($debian_version)"
fi
#rtorrent
rtorrent_version=$(apt-cache policy rtorrent | head -3 | tail -1 | cut -d' ' -f4)
rtorrent_version_micro=$(echo "$rtorrent_version" | cut -d'-' -f1 | cut -d'.' -f3)
libtorrent_version=$(apt-cache policy libtorrent?? | head -3 | tail -1 | cut -d' ' -f4)
#python
python_path=$(ls -l /usr/bin/python? | tail -1 | rev | cut -d' ' -f3 | rev)
python_version=$($python_path -V | cut -d' ' -f2)
python_version_major=$(echo "$python_version" | cut -d'.' -f1)
python_version_minor=$(echo "$python_version" | cut -d'.' -f2)
python_pip=python$python_version_major-pip
#php
#php_version="$(apt-cache policy php | head -3 | tail -1 | cut -d' ' -f4 | cut -d':' -f2 | cut -d'+' -f1)"
php_version=$(apt-cache policy php | head -3 | tail -1 | cut -d' ' -f4 | cut -d':' -f2 | cut -d'+' -f1-3)
#apache2
apache2_version=$(apt-cache policy apache2 | head -3 | tail -1 | cut -d' ' -f4 | cut -d':' -f2 | cut -d'+' -f1)
## get mini UID limit ##
low=$(cat /etc/login.defs | grep "^UID_MIN" | grep -o '[[:digit:]]*')
## get max UID limit ##
high=$(cat /etc/login.defs | grep "^UID_MAX" | grep -o '[[:digit:]]*')
#system_low = 1 prevent root from using
system_low=1
let system_high=$((low - 1))
#ruTorrents
#ALL_VERSION=$(wget -q https://api.github.com/repos/Novik/ruTorrent/releases -O - | grep tag_name | cut -d'"' -f4)
ALL_VERSION=$(wget -q https://api.github.com/repos/Novik/ruTorrent/tags -O - | grep name | cut -d'"' -f4 | grep -v 'rutorrent\|plugins')
#remove v4.0 "All Linux Distributions should mark version 4 as "unstable" due to caching issues and use the v4.0.1 hot fix release instead"
#https://github.com/Novik/ruTorrent/releases/tag/v4.0.1-hotfix
STABLE_VERSION=$(echo "$ALL_VERSION" | grep -v 'beta\|v4.0-stable')
#set rutorrent list to all versions(ALL_VERSION) or stable versions(STABLE_VERSION) only
LIST=$STABLE_VERSION
#https://stackoverflow.com/questions/9293887/reading-a-space-delimited-string-into-an-array-in-bash
#https://unix.stackexchange.com/questions/412120/array-into-whiptail-checkbox-bash
last='""off'
variablenname=$(echo $LIST | sed 's/ /""off"/g')
full="$variablenname$last"
IFS='"' read -a VERSIONS <<< "$full"
function SCRIPT_BASE_INSTALL {
DIALOG_CHECK="$(dpkg-query -W -f='${Status}\n' dialog 2>/dev/null | grep -c "ok installed")"
WGET_CHECK="$(dpkg-query -W -f='${Status}\n' wget 2>/dev/null | grep -c "ok installed")"
if [ "$DIALOG_CHECK" -ne 1 ] || [ "$WGET_CHECK" -ne 1 ];
then
if [ "$DIALOG_CHECK" -ne 1 ];
then
base0=dialog
fi
if [ "$WGET_CHECK" -ne 1 ];
then
base1=wget
fi
apt-get -y install $base0 $base1
#1> /dev/null
fi
}
function MENU {
LOG_REDIRECTION="/dev/null"
menu_options=("0" "System Information"
"1" "Licence"
"2" "Changelog"
"I" "Scripted Installation"
"T" "Update/Change ruTorrent"
"V" "Change VHost"
"S" "Enable/Renew SSL for VHost"
"W" "Enable/Disable WebAuth"
"A" "Add User to WebAuth"
"U" "Remove User from WebAuth"
"R" "Move to unrar-nonfree variant"
"H" "Add/Remove Softlink to/from the rtorrent users homedir"
"G" "Edit rtorrent.rc/Move rtorrent basedir"
"C" "Change rtorrent user"
"X" "Remove complete rtorrent & ruTorrent installation")
if [ -f $logfile ]
then
menu_options+=("L" "Show Installation log")
fi
if [ -f $removelogfile ]
then
menu_options+=("M" "Show Remove log")
fi
if $fullmenu
then
menu_options+=("9" "Add User"
"6" "Remove User"
"4" "Allow SSH"
"5" "Deny SSH"
"7" "Install webserver & php")
fi
# "Z" "Install Complete"
# "N" "Script"
SELECTED=$(dialog \
--backtitle "rtorrent & ruTorrent Installation Script $script_versionumber" \
--title "Menu" \
--stdout \
--begin $x $y \
--colors \
--cancel-label "Exit" \
--menu "Options" $height $width 13 "${menu_options[@]}")
EXITCODE=$?
# Get exit status
# 0 means user hit OK button.
# 1 means user hit CANCEL button.
# 2 means user hit HELP button.
# 3 means user hit EXTRA button.
# 255 means user hit [Esc] key.
case $EXITCODE in
0) MENU_OPTIONS $SELECTED;;
1|255) EXIT;;
esac
}
function MENU_OPTIONS () {
case $1 in
0) HEADER;;
1) LICENSE;;
2) CHANGELOG;;
I) SCRIPTED_INSTALL;;
T) MENU_RUTORRENT;;
V) CHANGE_VHOST;;
S) SSL_FOR_WEBSERVER;;
W) WEBAUTH_TOGGLE;;
A) ADD_USER_TO_WEBAUTH;;
U) REMOVE_WEBAUTH_USER;;
R) USE_UNRAR_NONFREE;;
H) SOFTLINK_TO_HOMEDIR;;
G) MOVE_RTORRENT_BASEDIR;;
X) REMOVE_EVERYTHING;;
L) INSTALLLOG;;
M) REMOVELOG;;
9) ADD_USER;;
6) REMOVE_USER;;
4) ALLOW_SSH;;
5) DENY_SSH;;
7) APACHE2;;
Z) INSTALL_COMPLETE;;
N) SCRIPT;;
C) SELECT_USER;;
esac
}
function EXIT {
#https://stackoverflow.com/questions/49733211/bash-jump-to-bottom-of-terminal
tput cup $(tput lines) 0
echo ""
echo "goodbye!"
exit 0
}
function INSTALLLOG {
dialog --title "Installation log" --stdout --begin $x $y --ok-label "Exit" --extra-button --extra-label "Remove \"Installation Log\"" --no-collapse --textbox $logfile $height $width
EXITCODE=$?
# Get exit status
# 0 means user hit OK button.
# 1 means user hit CANCEL button.
# 2 means user hit HELP button.
# 3 means user hit EXTRA button.
# 255 means user hit [Esc] key.
case $EXITCODE in
0|1|255) ;;
3) rm -f $logfile;;
esac
MENU
}
function REMOVELOG {
dialog --title "Remove log" --stdout --begin $x $y --ok-label "Exit" --extra-button --extra-label "Remove \"Remove Log\"" --no-collapse --textbox $removelogfile $height $width
EXITCODE=$?
# Get exit status
# 0 means user hit OK button.
# 1 means user hit CANCEL button.
# 2 means user hit HELP button.
# 3 means user hit EXTRA button.
# 255 means user hit [Esc] key.
case $EXITCODE in
0|1|255) ;;
3) rm -f $removelogfile;;
esac
MENU
}
function HEADER {
systemupdate=$(stat /var/cache/apt/ | head -6 | tail -1 | cut -d' ' -f2- | cut -d'.' -f1)
dialog \
--backtitle "last systemupdate on $systemupdate" \
--title "System Information" \
--stdout \
--begin $x $y \
--colors \
--msgbox "\
System:\n\
Architecture: \Z4$architecture\Z0\n\
Address Register: \Z4$bits Bit\Z0\n\
\n\
Operation System:\n\
OS: \Z4$name\Z0\n\
Version: \Z4$version\Z0\n\
OS Codename: \Z4$codename\Z0\n\
Distributor: \Z4$distributor\Z0\n\
\n\
Software Versions:\n\
rtorrent: \Z4$rtorrent_version\Z0\n\
libtorrent: \Z4$libtorrent_version\Z0\n\
Python: \Z4$python_version\Z0\n\
Apache2: \Z4$apache2_version\Z0\n\
PHP: \Z4$php_version\Z0"\
$height $width
EXITCODE=$?
# Get exit status
# 0 means user hit OK button.
# 1 means user hit CANCEL button.
# 2 means user hit HELP button.
# 3 means user hit EXTRA button.
# 255 means user hit [Esc] key.
case $EXITCODE in
0|1|255) ;;
esac
MENU
}
function LICENSE {
dialog \
--title "Licence" \
--stdout \
--begin $x $y \
--colors \
--msgbox "\
\n\
THE BEER-WARE LICENSE (Revision 42)©:\n\
\n\
\Z2I\Z0 wrote this script. As long as you retain this notice you\n\
can do whatever you want with this stuff. If we meet some day,\n\
and you think this stuff is worth it, you can buy me a beer\n\
in return.\n\
\n\
Contact me? use Github\n\
\n\
© Poul-Henning Kamp's beerware license."\
$height $width
EXITCODE=$?
# Get exit status
# 0 means user hit OK button.
# 1 means user hit CANCEL button.
# 2 means user hit HELP button.
# 3 means user hit EXTRA button.
# 255 means user hit [Esc] key.
case $EXITCODE in
0|1|255) ;;
esac
MENU
}
function CHANGELOG {
#https://superuser.com/questions/802650/make-a-web-request-cat-response-to-stdout
# local homedir
#link=$(cat $(getent passwd "$stdin_user" | cut -d':' -f6)/changelog)
#link=$(cat /home/$stdin_user/changelog)
# github
link=$(wget -q -O - https://raw.githubusercontent.com/MarkusLange/r_ru-torrent-install-script/main/changelog)
dialog --title "Changelog" --stdout --begin $x $y --no-collapse \
--backtitle "rtorrent & ruTorrent Installation Script $script_versionumber" \
--msgbox "$link" $height $width
EXITCODE=$?
# Get exit status
# 0 means user hit OK button.
# 1 means user hit CANCEL button.
# 2 means user hit HELP button.
# 3 means user hit EXTRA button.
# 255 means user hit [Esc] key.
case $EXITCODE in
0|1|255) ;;
esac
MENU
}
function SELECT_USER () {
## use awk to print if UID >= $MIN and UID <= $MAX ##
#user=$(getent passwd {1000..2000} | cut -d':' -f1)
user=$(awk -F':' -v "min=${low}" -v "max=${high}" '{ if ( $3 >= min && $3 <= max ) print $0}' /etc/passwd | cut -d':' -f1 | sort)
system_user=$(awk -F':' -v "min=${system_low}" -v "max=${system_high}" '{ if ( $3 >= min && $3 <= max ) print $0}' /etc/passwd | cut -d':' -f1 | sort)
end='""off'
variablenname=$(echo $user | sed 's/ /""off"/g')
full="$variablenname$end"
IFS='"' read -a USERS <<< "$full"
system_last='""off"add new system user""off'
variablensystemname=$(echo $system_user | sed 's/ /""off"/g')
systemfull="$variablensystemname$system_last"
IFS='"' read -a SYSTEM_USERS <<< "$systemfull"
rtorrent_user_name=$(cat /etc/systemd/system/rtorrent.service | grep 'User' | cut -d'=' -f2)
rtorrent_user_group=$(groups $rtorrent_user_name | cut -d' ' -f3)
user_of_rtorrent_group=$(grep $rtorrent_user_group /etc/group | cut -d':' -f4)
IFS=',' read -a list_of_rtorrent_group_user <<< "$user_of_rtorrent_group"
rtorrentuser=${list_of_rtorrent_group_user[@]/"www-data"}
rtorrentuser="${rtorrentuser[@]}"
#remove space from String
rtorrentuser=$(echo $rtorrentuser | sed 's/ //g')
#https://stackoverflow.com/questions/15028567/get-the-index-of-a-value-in-a-bash-array
for i in "${!USERS[@]}"
do
[[ "${USERS[$i]}" = "${rtorrentuser}" ]] && break
done
echo $i
for each in "${USERS[@]}"
do
echo "$each"
done
USERS[$i+2]=on
SELECTED=$(dialog \
--title "Select rtorrent User" \
--stdout \
--begin $x $y \
--extra-button \
--extra-label "Add User"\
--radiolist "current rtorrent User (*)" $height $width 13 "${USERS[@]}")
EXITCODE=$?
# Get exit status
# 0 means user hit OK button.
# 1 means user hit CANCEL button.
# 2 means user hit HELP button.
# 3 means user hit EXTRA button.
# 255 means user hit [Esc] key.
case $EXITCODE in
0) PRESENT_USER $SELECTED;;
1|255) ;;
3) ADD_USER;;
esac
MENU
}
#rtorrent daemon user
#rtorrent_daemon_user=rtorrent-daemon
#rtorrent_daemon_group=rtorrent-common
function PRESENT_USER () {
rtorrent_user_name=$(cat /etc/systemd/system/rtorrent.service | grep 'User' | cut -d'=' -f2)
rtorrent_user_group=$(groups $rtorrent_user_name | cut -d' ' -f3)
user_of_rtorrent_group=$(grep $rtorrent_user_group /etc/group | cut -d':' -f4)
IFS=',' read -a list_of_rtorrent_group_user <<< "$user_of_rtorrent_group"
rtorrentuser=${list_of_rtorrent_group_user[@]/"www-data"}
rtorrentuser="${rtorrentuser[@]}"
#remove space from String
rtorrentuser=$(echo $rtorrentuser | sed 's/ //g')
location=$(cat /etc/systemd/system/rtorrent.service | grep "ExecStart" | cut -d'=' -f3 | cut -d'.' -f1)
status=$(ls -lrt /home/$rtorrentuser | grep "rtorrent" | grep -c "^l")
if [ $status == "1" ]
then
unlink /home/$rtorrentuser/rtorrent 2> /dev/null
fi
deluser $rtorrentuser $rtorrent_user_group 1> /dev/null
usermod -a -G $rtorrent_user_group $1
if [ $status == "1" ]
then
ln -s $location /home/$1/rtorrent
fi
}
function ADD_USER () {
unset OUTPUT
OUTPUT=$(dialog \
--title "New User" \
--stdout \
--begin $x $y \
--insecure "$@" \
--trim \
--output-separator $separator \
--mixedform "Create new User for rtorrent" \
$height $width 0 \
"Username :" 1 1 "" 1 21 12 0 0 \
"Password :" 2 1 "" 2 21 10 0 1 \
"Retype Password :" 3 1 "" 3 21 10 0 1 \
"Allow SSH (yes/no):" 4 1 "yes" 4 21 5 0 0 \
)
EXITCODE=$?
# Get exit status
# 0 means user hit OK button.
# 1 means user hit CANCEL button.
# 2 means user hit HELP button.
# 3 means user hit EXTRA button.
# 255 means user hit [Esc] key.
#echo $OUTPUT
#remove spaces from String
OUTPUT=$(echo $OUTPUT | sed 's/ //g')
IFS=$separator read -a SHOWN <<< "$OUTPUT"
case $EXITCODE in
0) CREATE_USER "${SHOWN[@]}"
PRESENT_USER "${SHOWN[0]}";;
1|255) ;;
esac
MENU
}
function CREATE_USER () {
arr=("$@")
#https://acloudguru.com/blog/engineering/conditions-in-bash-scripting-if-statements
unset state
if [ -z "${arr[0]}" ]
then
answer1="No Username entered"
state="error"
else
if [[ $(getent passwd | grep -c ^${arr[0]}:) -ne 0 ]]
then
answer1="User exist"
state="error"
else
answer1=""
fi
fi
if [ -z "${arr[1]}" ] || [ -z "${arr[2]}" ]
then
answer2="Password is empty"
state="error"
else
if [ "${arr[1]}" != "${arr[2]}" ]
then
answer2="Passwords does not match"
state="error"
else
answer2=""
fi
fi
if [[ $state == "error" ]]
then
dialog --title "Error" \
--stdout \
--begin $small_x $y \
--msgbox "\
$answer1\n\
$answer2\
"\
$small_height $width
EXITCODE=$?
# Get exit status
# 0 means user hit OK button.
# 1 means user hit CANCEL button.
# 2 means user hit HELP button.
# 3 means user hit EXTRA button.
# 255 means user hit [Esc] key.
case $EXITCODE in
0|1|255) ADD_USER;;
esac
else
(echo ${arr[1]}; echo ${arr[1]}) | sudo adduser --force-badname --gecos "" ${arr[0]} --quiet 2> /dev/null
#https://stackoverflow.com/questions/11392189/how-can-i-convert-a-string-from-uppercase-to-lowercase-in-bash
if [ "${arr[3],,}" == "no" ];
then
#echo "Deny SSH"
if grep -q '^DenyUsers' '/etc/ssh/sshd_config';
then
present=$(grep "^DenyUsers" /etc/ssh/sshd_config)
sudo sed -i 's/^DenyUsers.*/'"$present"' '"$1"'/g' /etc/ssh/sshd_config
else
sudo sed -i '$aDenyUsers\t'"$1"'' /etc/ssh/sshd_config
fi
systemctl restart ssh
fi
dialog --title "Done" --stdout --begin $small_x $y --msgbox "\nNew User ${arr[0]} created" $small_height $width
fi
}
function ALLOW_SSH () {
if grep -q '^DenyUsers' '/etc/ssh/sshd_config';
then
present=$(grep "^DenyUsers" /etc/ssh/sshd_config)
#change tab to space
present2=$(echo $present)
IFS=' ' read -a DENYLIST <<< "$present2"
DENYLIST_NAMES=${DENYLIST[@]/"DenyUsers"}
last='""off'
variablenname=$(echo $DENYLIST_NAMES | sed 's/ /""off"/g')
full="$variablenname$last"
IFS='"' read -a DENYLISTUSERS <<< "$full"
SELECTED=$(dialog --title "Remove User from SSH DenyUsers List" --stdout --begin $x $y --radiolist "Select User" $height $width 13 "${DENYLISTUSERS[@]}")
EXITCODE=$?
# Get exit status
# 0 means user hit OK button.
# 1 means user hit CANCEL button.
# 2 means user hit HELP button.
# 3 means user hit EXTRA button.
# 255 means user hit [Esc] key.
#echo $EXITCODE
#echo $SELECTED
DENYLIST_NEW=${DENYLIST_NAMES[@]/$SELECTED}
new_string="${DENYLIST_NEW[@]}"
#remove double space from String
new_string=$(echo $new_string | sed 's/ / /g')
#echo $new_string
case $EXITCODE in
0) sudo sed -i 's/^DenyUsers.*/DenyUsers\t'"$new_string"'/g' /etc/ssh/sshd_config
#https://serverfault.com/questions/477503/check-if-array-is-empty-in-bash
if [ "$(echo -ne ${DENYLIST_NEW} | wc -m)" -eq 0 ]
then
#echo "Array is empty"
sudo sed -i '/^DenyUsers/d' /etc/ssh/sshd_config
fi
systemctl restart ssh
dialog --title "Done" --stdout --begin $small_x $y --msgbox "User $SELECTED removed from DenyUsers List" $small_height $width;;
1|255) ;;
esac
else
dialog --title "Error" --stdout --begin $small_x $y --msgbox "DenyUsers does not exist" $small_height $width
fi
MENU
}
#testfunction only
function DIFF_USERS () {
user=$(awk -F':' -v "min=${low}" -v "max=${high}" '{ if ( $3 >= min && $3 <= max ) print $0}' /etc/passwd | cut -d':' -f1)
#echo $user
present=$(grep "^DenyUsers" /etc/ssh/sshd_config)
present2=$(echo $present | cut -d' ' -f2-)
#echo $present2
#https://stackoverflow.com/questions/454427/string-difference-in-bash
diff=$(comm -23 <(tr ' ' $'\n' <<< $user | sort) <(tr ' ' $'\n' <<< $present2 | sort))
#echo $diff
}
function DENY_SSH () {
user=$(awk -F':' -v "min=${low}" -v "max=${high}" '{ if ( $3 >= min && $3 <= max ) print $0}' /etc/passwd | cut -d':' -f1)
present=$(grep "^DenyUsers" /etc/ssh/sshd_config)
#change TAB to space and remove DenyUsers from String
present2=$(echo $present | cut -d' ' -f2-)
#https://stackoverflow.com/questions/454427/string-difference-in-bash
diff=$(comm -23 <(tr ' ' $'\n' <<< $user | sort) <(tr ' ' $'\n' <<< $present2 | sort))
end='""off'
variablenname=$(echo $diff | sed 's/ /""off"/g')
full="$variablenname$end"
IFS='"' read -a USERS <<< "$full"
SELECTED=$(dialog --title "Remove SSH from User" --stdout --begin $x $y --radiolist "Add User to SSH DenyUsers List" $height $width 13 "${USERS[@]}")
EXITCODE=$?
# Get exit status
# 0 means user hit OK button.
# 1 means user hit CANCEL button.
# 2 means user hit HELP button.
# 3 means user hit EXTRA button.
# 255 means user hit [Esc] key.
case $EXITCODE in
0) DENY_SSH_FORM_USER $SELECTED;;
1|255) ;;
esac
MENU
}
function DENY_SSH_FORM_USER () {
if grep -q '^DenyUsers' '/etc/ssh/sshd_config';
then
present=$(grep "^DenyUsers" /etc/ssh/sshd_config)
if grep -q $1 <<< "$present";
then
dialog --title "Error" --stdout --begin $small_x $y --msgbox "User $1 is allready denyed from SSH" $small_height $width
else
sudo sed -i 's/^DenyUsers.*/'"$present"' '"$1"'/g' /etc/ssh/sshd_config
dialog --title "Done" --stdout --begin $small_x $y --msgbox "User $1 is now denyed from SSH" $small_height $width
fi
else
sudo sed -i '$aDenyUsers\t'"$1"'' /etc/ssh/sshd_config
dialog --title "Done" --stdout --begin $small_x $y --msgbox "User $1 is now denyed from SSH" $small_height $width
fi
systemctl restart ssh
}
function REMOVE_USER () {
user=$(awk -F':' -v "min=${low}" -v "max=${high}" '{ if ( $3 >= min && $3 <= max ) print $0}' /etc/passwd | cut -d':' -f1 | sort)
end='""off'
variablenname=$(echo $user | sed 's/ /""off"/g')
full="$variablenname$end"
IFS='"' read -a USERS <<< "$full"
SELECTED=$(dialog --title "Remove Linux User" --stdout --begin $x $y --radiolist "Select User" $height $width 13 "${USERS[@]}")
EXITCODE=$?
# Get exit status
# 0 means user hit OK button.
# 1 means user hit CANCEL button.
# 2 means user hit HELP button.
# 3 means user hit EXTRA button.
# 255 means user hit [Esc] key.
case $EXITCODE in
0) DEL_USER $SELECTED;;
1|255) ;;
esac
MENU
}
function DEL_USER () {
if [ $(id -u $1) == "$low" ]
then
#echo "first user"
dialog --title "Error" --begin $small_x $y --stdout --msgbox "Won't delete first User" $small_height $width
else
#echo "someone else"
userdel -f -r $1 >& /dev/null
if grep -q '^DenyUsers' '/etc/ssh/sshd_config';
then
#echo "DenyUsers exist"
present=$(grep "^DenyUsers" /etc/ssh/sshd_config)
if grep -q $1 <<< "$present";
then
#change tab to space
present2=$(echo $present)
IFS=' ' read -a DENYLIST <<< "$present2"
DENYLIST_NAMES=${DENYLIST[@]/"DenyUsers"}
DENYLIST_NEW=${DENYLIST_NAMES[@]/#%$1}
new_string="${DENYLIST_NEW[@]}"
#remove double space from String
new_string=$(echo $new_string | sed 's/ / /g')
sudo sed -i 's/^DenyUsers.*/DenyUsers\t'"$new_string"'/g' /etc/ssh/sshd_config
if [ "$(echo -ne ${DENYLIST_NEW} | wc -m)" -eq 0 ]
then
#echo "Array is empty"
sudo sed -i '/^DenyUsers/d' /etc/ssh/sshd_config
fi
systemctl restart ssh
fi
else
:
fi
dialog --title "Done" --stdout --begin $small_x $y --msgbox "User $1 deleted" $small_heightheight $width
fi
}
function SYSTEM_UPDATE {
apt-get update 1>> $LOG_REDIRECTION
apt-get -y dist-upgrade 1>> $LOG_REDIRECTION
}
function APACHE2 {
apt-get -y install openssl apache2 apache2-utils php$PHP_VERSION php$PHP_VERSION-mbstring php$PHP_VERSION-curl php$PHP_VERSION-cli php$PHP_VERSION-xml libapache2-mod-php$PHP_VERSION unzip curl 2>/dev/null 1>> $LOG_REDIRECTION
#https://www.digitalocean.com/community/tutorials/apache-configuration-error-ah00558-could-not-reliably-determine-the-server-s-fully-qualified-domain-name
echo "ServerName 127.0.0.1" >> /etc/apache2/apache2.conf
#https://www.inmotionhosting.com/support/server/apache/hide-apache-version-and-linux-os/
#https://stackoverflow.com/questions/24889346/how-to-uncomment-a-line-that-contains-a-specific-string-using-sed
sed -i '/ServerTokens OS/ s/^/#/' /etc/apache2/conf-enabled/security.conf
sed -i '/#ServerTokens Full/a ServerTokens Prod' /etc/apache2/conf-enabled/security.conf
sed -i '/ServerSignature On/ s/^/#/' /etc/apache2/conf-enabled/security.conf
sed -i '/ServerSignature Off/ s/^#//' /etc/apache2/conf-enabled/security.conf
systemctl reload apache2.service 1>> $LOG_REDIRECTION
systemctl restart apache2.service 1>> $LOG_REDIRECTION
}
function RTORRENT () {
arr=("$@")
# USER[_] 0 User attribute, 1 Username, 2 User password, 3 Usergroup = Username, 4 User homedir, 5 User SSH status
USER[0]=
USER[1]=${arr[1]}
USER[2]=
USER[3]=${arr[3]}
USER[4]=${arr[4]}
USER[5]=
#echo ${USER[1]}
#echo ${USER[3]}
#echo ${USER[4]}
apt-get -y install rtorrent 1>> $LOG_REDIRECTION
#https://github.com/rakshasa/rtorrent/wiki/CONFIG-Template
wget -q -O - "https://raw.githubusercontent.com/wiki/rakshasa/rtorrent/CONFIG-Template.md" | sed -ne "/^######/,/^### END/p" | sed -re "s:/home/USERNAME:/srv:" >${USER[4]}/.rtorrent.rc
chown -R ${USER[1]}:${USER[3]} ${USER[4]}/.rtorrent.rc
CREATE_TMPFILES "${USER[@]}"
#https://github.com/rakshasa/rtorrent/issues/949#issuecomment-572528586
sed -i '/^system.umask.*/a session.use_lock.set = no' ${USER[4]}/.rtorrent.rc
#rpc enabled from local socket
#https://github.com/rakshasa/rtorrent/wiki/RPC-Setup-XMLRPC
#https://www.cyberciti.biz/faq/how-to-use-sed-to-find-and-replace-text-in-files-in-linux-unix-shell/
sed -i '/(session.path),rpc.socket)/ s/^#//' ${USER[4]}/.rtorrent.rc
#not usable links sessiondata also to run/torrent removes everything on reboot
#sed -i 's:(cfg.basedir),".session/":"/run/rtorrent/":' ${USER[4]}/.rtorrent.rc
#move socket to run
sed -i 's:(cat,(session.path),rpc.socket):/run/rtorrent/rpc.socket:' ${USER[4]}/.rtorrent.rc
#sed -i ':rtorrent.pid: s:(session.path):/run/rtorrent/:' ${USER[4]}/.rtorrent.rc ##not working correctly
sed -i 's: (session.path): /run/rtorrent/:' ${USER[4]}/.rtorrent.rc
##sed -i '/rtorrent.pid/ s/(session.path)/\/run\/rtorrent\//' ${USER[4]}/.rtorrent.rc
#set rights to 770
sed -i '27iexecute.throw = chmod, -R, 770, (cat, (cfg.basedir))' ${USER[4]}/.rtorrent.rc
if (( $rtorrent_version_micro <= 6 ))
then
echo "rtorrent Version is equal or lower than 0.9.6" 1>> $LOG_REDIRECTION
apt-get install -y tmux 1>> $LOG_REDIRECTION
RTORRENT_TMUX_SERVICE "${USER[@]}"
else
echo "daemon mode enabled since 0.9.7+" 1>> $LOG_REDIRECTION
sed -i '/system.daemon.set/ s/^#//' ${USER[4]}/.rtorrent.rc
RTORRENT_SERVICE "${USER[@]}"
fi
echo "rtorrent.service" 1>> $LOG_REDIRECTION
cat /etc/systemd/system/rtorrent.service >> $LOG_REDIRECTION
}
function CREATE_TMPFILES () {
arr=("$@")
#https://serverfault.com/questions/779634/create-a-directory-under-var-run-at-boot
cat > "/usr/lib/tmpfiles.d/rtorrent.conf" <<-EOF
#Type Path Mode UID GID Age Argument
d /run/rtorrent 0770 ${arr[1]} ${arr[3]} - -
EOF
# inital placement for the direct run
mkdir -p /run/rtorrent
chown -R ${arr[1]}:${arr[3]} /run/rtorrent
#chmod -R 775 /run/rtorrent
}
#tmux session explanations
#http://man.openbsd.org/OpenBSD-current/man1/tmux.1
#https://coderwall.com/p/omqa2w/tmux-basics
#https://stackoverflow.com/questions/39523167/how-do-you-send-keys-to-a-specific-window-in-tmux
#tmux -2 new-session -d -s rtorrent-session /usr/bin/rtorrent
#
# -2 [Force tmux to assume the terminal supports 256 colours. This is equivalent to -T 256]
# new-session -s rtorrent-session [Creating named session which can be used we want the session to run the background]
# -d [detach session from current terminal]
# /usr/bin/rtorrent [Programm that startet inside the tmux session]
#
#tmux send-keys -t rtorrent-session C-q
# send-keys -t [Send a key or keys to a window or client, target panel {-t}]
# rtorrent-session [{session}]
# C-q [Send keys]
#
#tmux kill-session -t rtorrent-session
# kill-session -t [Destroy the given session]
function RTORRENT_TMUX_SERVICE () {
arr=("$@")
cat > "/etc/systemd/system/rtorrent.service" <<-EOF
[Unit]
Description=rtorrent (in tmux)
Requires=network-online.target
After=apache2.service
[Service]
Type=oneshot
RemainAfterExit=yes
User=${arr[1]}
ExecStart=/usr/bin/tmux -2 new-session -d -s rtorrent-session /usr/bin/rtorrent -n -o import=${arr[4]}/.rtorrent.rc
ExecStop=/usr/bin/tmux send-keys -t rtorrent-session C-q
[Install]
WantedBy=default.target
EOF
}
#https://www.freedesktop.org/software/systemd/man/systemd.kill.html#KillSignal=
#https://github.com/rakshasa/rtorrent/wiki/User-Guide
function RTORRENT_SERVICE () {
arr=("$@")
cat > "/etc/systemd/system/rtorrent.service" <<-EOF
[Unit]
Description=rtorrent deamon
Requires=network-online.target
After=apache2.service
[Service]
Type=simple
RemainAfterExit=yes
User=${arr[1]}
ExecStart=/usr/bin/rtorrent -n -o import=${arr[4]}/.rtorrent.rc
KillMode=mixed
KillSignal=SIGINT
#Stop -> SIGINT - 10s - SIGTERM (if not stopped)
#TimeoutStopSec=10
#SendSIGKILL=yes
#FinalKillSignal=SIGTERM
[Install]
WantedBy=default.target
EOF
}
function MOVE_RTORRENT_BASEDIR () {
rtorrent_rc_path=$(cat /etc/systemd/system/rtorrent.service | grep "ExecStart" | cut -d'=' -f3)
rtorrent_basedir=$(echo $rtorrent_rc_path | rev | cut -d'/' -f3- | rev)
rtorrent_user_name=$(cat /etc/systemd/system/rtorrent.service | grep 'User' | cut -d'=' -f2)
rtorrent_user_group=$(groups $rtorrent_user_name | cut -d' ' -f3)
rtorrentuser=${list_of_rtorrent_group_user[@]/"www-data"}
rtorrentuser="${rtorrentuser[@]}"
#remove space from String
rtorrentuser=$(echo $rtorrentuser | sed 's/ //g')
#location=$(cat /etc/systemd/system/rtorrent.service | grep "ExecStart" | cut -d'=' -f3 | cut -d'.' -f1)
status=$(ls -lrt /home/$rtorrentuser | grep "rtorrent" | grep -c "^l")
PORT_RANGE=$(grep 'port_range.set' $rtorrent_rc_path | cut -d' ' -f3)
PORT_RANGE_MIN=$(echo $PORT_RANGE | cut -d'-' -f1)
PORT_RANGE_MAX=$(echo $PORT_RANGE | cut -d'-' -f2)
PORT_SET=$(grep 'port_random.set' $rtorrent_rc_path | cut -d' ' -f3)
DLFOLDER=$rtorrent_basedir
PRESENT_PORT_SET=$PORT_SET
PRESENT_DLFOLDER=$DLFOLDER
while :; do
OUTPUT=$(dialog \
--title "Edit rtorrent.rc" \
--stdout \
--begin $x $y \
--trim \
--extra-button \
--colors \
--extra-label "Change Basedir" \
--output-separator $separator \
--default-button "ok" \
--mixedform "Port Range defines the usable Ports for rtorrent\n
Random Listening Port let rtorrent set the Port randomly\n
rtorrent folder stucture:\n
\Z4$DLFOLDER\Zn \n
└── /rtorrent \n
├── /.session \n
├── /download \n
├── /log \n
└── /watch \n
├── /load \n
└── /start"\
$height $width 0 \
"Port Range :" 1 1 " $PORT_RANGE_MIN" 1 33 6 0 0 \
"-" 1 39 " $PORT_RANGE_MAX" 1 40 6 0 0 \
"Random Listening Port (yes/no):" 2 1 "$PORT_SET" 2 33 5 0 0 \
"rtorrent basedir :" 3 1 "$DLFOLDER" 3 33 31 0 2 \
)
EXITCODE=$?
#remove spaces from String
OUTPUT=$(echo $OUTPUT | sed 's/ //g')
IFS=$separator read -a SHOWN <<< "$OUTPUT"
# Get exit status
# 0 means user hit OK button.
# 1 means user hit CANCEL button.
# 2 means user hit HELP button.
# 3 means user hit EXTRA button.
# 255 means user hit [Esc] key.
case $EXITCODE in
0)
EXITLOOP=0
break;;
1|255)
EXITLOOP=1
break;;
3)
RETURN=$(dialog --stdout --begin $x $y --dselect "$DLFOLDER" 10 $width)
EXITCODE=$?
#https://stackoverflow.com/questions/9018723/what-is-the-simplest-way-to-remove-a-trailing-slash-from-each-parameter
RETURN=$(echo "$RETURN" | sed 's:/*$::')
# Get exit status
# 0 means user hit OK button.
# 1 means user hit CANCEL button.
# 2 means user hit HELP button.
# 3 means user hit EXTRA button.
# 255 means user hit [Esc] key.
case $EXITCODE in
0) PORT_RANGE_MIN=${SHOWN[0]}
PORT_RANGE_MAX=${SHOWN[1]}
PORT_SET=${SHOWN[2]}
DLFOLDER=$RETURN;;
1|255) EXITLOOP=1
break;;
esac
esac
done
case $EXITLOOP in
0) # RC[_] 0 Portrange, 1 random port set, 2 rtorrent basedir
RC[0]="${SHOWN[0]}-${SHOWN[1]}"
RC[1]="${SHOWN[2]}"
RC[2]="${SHOWN[3]}"
systemctl stop rtorrent.service 1> /dev/null