/
ehcpforceupdate.sh
executable file
·3824 lines (3237 loc) · 130 KB
/
ehcpforceupdate.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
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
# EHCP Non-Force Edition to EHCP Force Edition Upgrade Script
# By earnolmartin@gmail.com
# http://www.ehcpforce.tk
###########
#FUNCTIONS#
###########
function setGlobalVars(){
FIXDIR="/var/www/new/ehcp/fixes"
serviceNameTempFile="/root/ehcp_service_name_search_temp_file"
patchDir="/root/Downloads"
if [ ! -e "$patchDir" ]; then
mkdir -p "$patchDir"
fi
backupDir="/var/backup"
if [ ! -e "$backupDir" ]; then
mkdir -p "$backupDir"
fi
userScriptDir="/root/scripts"
if [ ! -e "$userScriptDir" ]; then
mkdir -p "$userScriptDir"
fi
ehcpConfigDir="/root/ehcp/config"
if [ ! -e "$ehcpConfigDir" ]; then
mkdir -p "$ehcpConfigDir"
fi
getVSFTPDUser
getPHPConfigPath
installerDir="$(pwd)"
initProcessStr=$(ps -p 1 | awk '{print $4}' | tail -n 1)
if [ "$initProcessStr" == "systemd" ]; then
systemdPresent=1
fi
EHCPMySQLPass=$(cat "/var/www/new/ehcp/config.php" | grep -o "dbpass=.*" | grep -o "=.*" | grep -o "[^='].*" | grep -o ".*[^';]")
UserDefinedPostScript="/root/scripts/ehcp_force_post_update_script.sh"
}
function detectRunningWebServer(){
# Get the web server type from the database
curDir=$(pwd)
cd "$patchDir"
cp "$FIXDIR/api/getwebservertype.tar.gz" "getwebservertype.tar.gz"
tar -zxvf "getwebservertype.tar.gz"
WebServerType=$(php -f getwebservertype.php | xargs)
if [ -z "$WebServerType" ] || [[ "$WebServerType" != "nginx" && "$WebServerType" != "apache2" ]]; then
nginxIsRunning=$(pidof nginx)
apache2IsRunning=$(pidof apache2)
if [ ! -z "$nginxIsRunning" ]; then
WebServerType="nginx"
fi
if [ ! -z "$apache2IsRunning" ]; then
WebServerType="apache2"
fi
fi
cd "$curDir"
}
function setWebServerModeToNginx(){
# Set the web server mode to nginx
curDir=$(pwd)
cd "$patchDir"
cp "$FIXDIR/api/use_nginx_server_type.tar.gz" "use_nginx_server_type.tar.gz"
tar -zxvf "use_nginx_server_type.tar.gz"
runPHPOutput=$(php -f use_nginx_server_type.php | xargs)
cd "$curDir"
}
function setWebServerModeToApache2(){
# Set the web server mode to apache2
curDir=$(pwd)
cd "$patchDir"
cp "$FIXDIR/api/use_apache2_server_type.tar.gz" "use_apache2_server_type.tar.gz"
tar -zxvf "use_apache2_server_type.tar.gz"
runPHPOutput=$(php -f use_apache2_server_type.php | xargs)
cd "$curDir"
}
function aptget_Update(){
apt-get update
}
function libldapFix(){ # by earnolmartin@gmail.com
# install libldap, for vsftpd fix, without prompts
#Remove originally installed libpam-ldap if it exists
origDir=$(pwd)
aptgetRemove libpam-ldap
DEBIAN_FRONTEND=noninteractive apt-get -y install libpam-ldap
cd "$patchDir"
mkdir lib32gccfix
cd lib32gccfix
cp "$FIXDIR/vsftpd/x64/ldap_conf_64bit_vsftpd.tar.gz" "ldap_conf.tar.gz"
tar -zxvf ldap_conf.tar.gz
cp ldap.conf /etc/
cd $origDir
}
function fixApacheEnvVars(){
# Check to make sure export APACHE_LOG_DIR=/var/log/apache2$SUFFIX exists
if [ -e "/etc/apache2/envvars" ]; then
APACHELOGCHECK=$(cat "/etc/apache2/envvars" | grep "APACHE_LOG_DIR=*")
if [ -z "$APACHELOGCHECK" ]; then
echo "export APACHE_LOG_DIR=/var/log/apache2\$SUFFIX" >> "/etc/apache2/envvars"
fi
fi
}
function slaveDNSApparmorFix(){ # by earnolmartin@gmail.com
if [ -e /etc/apparmor.d/usr.sbin.named ]; then
echo -e "\nChanging bind apparmor rule to allow master DNS synchronization for slave setups.\n"
sed -i 's#/etc/bind/\*\* r,#/etc/bind/\*\* rw,#g' /etc/apparmor.d/usr.sbin.named
manageService "apparmor" "restart"
fi
}
function changeApacheUser(){ # by earnolmartin@gmail.com
# Apache should run as the vsftpd account so that FTP connections own the file and php scripts can own the file
# Without this fix, files uploaded by ftp could not be changed by PHP scripts... AND
# Files uploaded / created by PHP scripts could not be modified (chmod) via FTP clients
if [ -e "/etc/apache2/envvars" ]; then
sed -i "s/export APACHE_RUN_USER=.*/export APACHE_RUN_USER=${VSFTPDUser}/g" "/etc/apache2/envvars"
if [ -e "/var/lock/apache2" ]; then
chown ${VSFTPDUser} "/var/lock/apache2"
fi
fi
# Also change nginx user
changeNginxUser
# Also change php-fpm user
if [ -e "$PHPCONFDir/fpm/pool.d/www.conf" ]; then
sed -i "s/user = .*/user = ${VSFTPDUser}/g" "$PHPCONFDir/fpm/pool.d/www.conf"
sed -i "s/group = .*/group = www-data/g" "$PHPCONFDir/fpm/pool.d/www.conf"
fi
# Create EHCP Pool and Secure WWW Pool
# VHOSTs can't run protected functions :)
createEHCPPool
# Secure Pools
securePHPFPMPools
}
function securePHPFPMPools(){
for i in `find ${PHPCONFDir}/fpm/pool.d -name "*.conf" -type f`; do
hasLocalAllowedClients=$(cat "${i}" | grep -o "^listen.allowed_clients")
if [ -z "$hasLocalAllowedClients" ]; then
echo "listen.allowed_clients = 127.0.0.1" >> "${i}"
fi
done
}
function createEHCPPool(){
# Create EHCP Pool
if [ ! -e "$PHPCONFDir/fpm/pool.d/ehcp.conf" ]; then
cp "$PHPCONFDir/fpm/pool.d/www.conf" "$PHPCONFDir/fpm/pool.d/ehcp.conf"
sed -i "s/^\[www\]/\[ehcp\]/g" "$PHPCONFDir/fpm/pool.d/ehcp.conf"
sed -i "s/^listen[^.].*/listen = 9001/g" "$PHPCONFDir/fpm/pool.d/ehcp.conf"
fi
hasDisableFunctions=$(cat "$PHPCONFDir/fpm/pool.d/www.conf" | grep -o "php_admin_value\[disable_functions\].*")
if [ -z "$hasDisableFunctions" ]; then
echo "php_admin_value[disable_functions] = exec,passthru,shell_exec,system,proc_open,popen" >> "$PHPCONFDir/fpm/pool.d/www.conf"
fi
# Remove it from ehcp if it's there
hasDisableFunctionsTwo=$(cat "$PHPCONFDir/fpm/pool.d/ehcp.conf" | grep -o "php_admin_value\[disable_functions\].*")
if [ ! -z "$hasDisableFunctionsTwo" ]; then
sed -i "s/^php_admin_value\[disable_functions\]/;php_admin_value\[disable_functions\]/g" "$PHPCONFDir/fpm/pool.d/ehcp.conf"
fi
}
function apacheUseFPM(){
a2enmod proxy_fcgi
a2enmod ssl
a2dismod php5
a2dismod php7.0
a2dismod php7.1
a2dismod php7.2
a2dismod php7.3
a2dismod php7.4
a2dismod php8.1
# We need a newer version of Apache for this to work properly!
if [[ "$distro" == "ubuntu" && "$yrelease" -eq "16" && "$mrelease" == "04" ]]; then
repoOndrejExists=$(ls /etc/apt/sources.list.d/ondrej-ubuntu-apache* 2>/dev/null | head -n 1)
if [ -z "$repoOndrejExists" ]; then
add-apt-repository -y ppa:ondrej/apache2
aptget_Update
apt-get install -y --allow-unauthenticated -o Dpkg::Options::="--force-confold" apache2
fi
fi
if [ "$distro" == "debian" ] && [ "$yrelease" -eq "9" ]; then
repoOndrejExists=$(cat /etc/apt/sources.list.d/apache2.list | grep -o "packages.sury.org/apache2")
if [ -z "$repoOndrejExists" ]; then
apt-get -y install apt-transport-https lsb-release ca-certificates curl
wget -O /etc/apt/trusted.gpg.d/apache2.gpg https://packages.sury.org/apache2/apt.gpg
sh -c 'echo "deb https://packages.sury.org/apache2/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/apache2.list'
aptget_Update
apt-get install -y --allow-unauthenticated -o Dpkg::Options::="--force-confold" apache2
fi
fi
}
function changeNginxUser(){
# Change nginx user
if [ -e "/etc/nginx/nginx.conf" ]; then
sed -i "s/^user .*/user ${VSFTPDUser} www-data;/g" "/etc/nginx/nginx.conf"
fi
}
function nginxRateLimit(){
if [ -e "/etc/nginx/nginx.conf" ]; then
NGINXHASRATELIMIT=$(cat "/etc/nginx/nginx.conf" | grep "limit_req_zone")
if [ -z "$NGINXHASRATELIMIT" ]; then
sed -i '/http {/a limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;' "/etc/nginx/nginx.conf"
fi
fi
}
# Secures BIND and prevents UDP Recursion Attacks:
# https://www.team-cymru.org/Services/Resolvers/instructions.html
# Good explanation FROM MS Forums (LOL): http://social.technet.microsoft.com/Forums/windowsserver/en-US/24ea1094-0ae4-47b5-9b74-2f77884cce15/dns-recursion?forum=winserverNIS
function disableRecursiveBIND(){ # by earnolmartin@gmail.com
# Get Resolv.conf and do not run this code if nameserver is set to 127.0.0.1
RESOLVCOUNT=$(cat "/etc/resolv.conf" | grep -c "nameserver")
RESOLVLOCAL=$(cat "/etc/resolv.conf" | grep "nameserver 127.0.0.1")
if [ "$RESOLVCOUNT" == "1" ] && [ ! -z "$RESOLVLOCAL" ]; then
echo -e "Skipping Bind Recursion Settings Due to 127.0.0.1 Nameserver"
else
bindOptionsFile="/etc/bind/named.conf.options"
bindBckFile="/etc/bind/named.conf.options_backup"
if [ -e "$bindOptionsFile" ]; then
# Create a backup of the original
if [ ! -e "$bindBckFile" ]; then
cp "$bindOptionsFile" "$bindBckFile"
fi
# Remove all blank lines at the end of the file:
# BINDNoEmptyLines=$(sed '/^ *$/d' "$bindOptionsFile")
# Better code here to strip out ending lines of empty text: http://stackoverflow.com/questions/7359527/removing-trailing-starting-newlines-with-sed-awk-tr-and-friends
# Can also do this for leading and trailing empty lines: sed -e :a -e '/./,$!d;/^\n*$/{$d;N;};/\n$/ba' file
BINDNoEmptyLines=$(sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' "$bindOptionsFile")
BINDNoEmptyLines=$(trim "$BINDNoEmptyLines")
echo "$BINDNoEmptyLines" > "$bindOptionsFile"
# Add recursion no
RecursiveSettingCheck=$( cat "$bindOptionsFile" | grep -o "^recursion .*" | grep -o " .*$" | grep -o "[^ ].*" )
if [ -z "$RecursiveSettingCheck" ]; then
# Put it one line before close pattern
sed -i '$i \recursion no;' "$bindOptionsFile"
else
sed -i 's/^recursion .*/recursion no;/g' "$bindOptionsFile"
fi
# Add additional-from-cache no
# No longer needed for Ubuntu 22.04+
if [[ "$distro" == "ubuntu" && "$yrelease" -le "20" ]] || [[ "$distro" == "debian" && "$yrelease" -le "11" ]]; then
RecursiveCacheCheck=$( cat "$bindOptionsFile" | grep -o "^additional-from-cache .*" | grep -o " .*$" | grep -o "[^ ].*" )
if [ -z "$RecursiveCacheCheck" ]; then
sed -i '$i \additional-from-cache no;' "$bindOptionsFile"
else
sed -i 's/^additional-from-cache .*/additional-from-cache no;/g' "$bindOptionsFile"
fi
fi
fi
# Extra optional step
#if [ -e "/etc/default/bind9" ]; then
# sed -i 's/^RESOLVCONF=.*/RESOLVCONF=no/g' "/etc/default/bind9"
#fi
manageService "bind9" "restart"
fi
}
function getLatestEHCPFiles(){
cd "$patchDir"
# If ehcp folder already exists, delete it
if [ -e "ehcp" ]; then
rm -Rf "ehcp"
fi
# Get the latest snapshot files of EHCP Force
if [ "$operationMode" == "update" ] || [ "$operationMode" == "upgrade" ]; then
wget -O "ehcpforce_stable_snapshot.tar.gz" -N https://github.com/earnolmartin/EHCP-Force-Edition/releases/download/1.1.1.1/ehcpforce_stable_snapshot.tar.gz
tar -zxvf "ehcpforce_stable_snapshot.tar.gz"
cd ehcp
CUREHCPDLDIR=$(pwd)
else
svnCount=0
while [ ! -e "ehcp" ]; do
if [ "$svnCount" -gt "0" ]; then
echo -e "Unable to download the EHCP files from SVN! Please make sure you are connected to the internet. Trying again..."
fi
svn checkout https://github.com/earnolmartin/EHCP-Force-Edition/trunk/ehcp ./ehcp
svnCount=$((svnCount+1))
done
rm -Rf .svn
cd ehcp
CUREHCPDLDIR=$(pwd)
fi
# Remove stock config.php (we want to use the existing one)
rm config.php
# Remove webmail stock config.php (we want to use the existing one)
if [ -e "webmail2" ]; then
cd "webmail2"
if [ -e "config" ]; then
cd "config"
if [ -e "config.php" ]; then
rm "config.php"
fi
fi
fi
# Go back to main ehcp dir
cd "$CUREHCPDLDIR"
# Make backup of previous EHCP files
LATESTBACKUPDIR="/var/www/new/ehcp_nonforce"
CurDate=$(date +%Y_%m_%d_%s)
cd ..
if [ ! -e "/var/www/new/ehcp_nonforce" ]; then
cp -R "/var/www/new/ehcp" "/var/www/new/ehcp_nonforce"
else
cp -R "/var/www/new/ehcp" "/var/www/new/ehcp_nonforce_$CurDate"
LATESTBACKUPDIR="/var/www/new/ehcp_nonforce_$CurDate"
fi
# Copy the updated downloaded EHCP files over
cp -R "ehcp" "/var/www/new/"
# Restore the old configuraiton files for EHCP and Webmail
cp "$LATESTBACKUPDIR/config.php" "/var/www/new/ehcp"
cp "$LATESTBACKUPDIR/webmail2/config/config.php" "/var/www/new/ehcp/webmail2/config"
# Fix permissions
fixEHCPPerms
logDirFix
# Move the old EHCP files into backup directory (enhance security)
if [ "$operationMode" == "update" ] || [ "$operationMode" == "update-svn" ]; then
mv "$LATESTBACKUPDIR" "$EHCPBACKUPDIR/ehcp_force_backup_$CurDate"
elif [ "$operationMode" == "upgrade" ]; then
mv "$LATESTBACKUPDIR" "$EHCPBACKUPDIR/ehcp_backup_$CurDate"
fi
}
function updateDaemon(){
manageService "ehcp" "stop"
# Copy daemon over to init dir
cp /var/www/new/ehcp/ehcp /etc/init.d/
# Get rid of experimental python daemon
# It was never used to begin with
if [ -e "/etc/init.d/ehcp_daemon.py" ]; then
rm "/etc/init.d/ehcp_daemon.py"
fi
# Use systemd Service for newer Ubuntu versions
if ([ "$distro" == "ubuntu" ] && [ "$yrelease" -ge "16" ]) || [ ! -z "$systemdPresent" ]; then
if [ -e "/lib/systemd/system" ]; then
cp "$FIXDIR/daemon/systemd/ehcp.service" "/lib/systemd/system"
rm -rf "/etc/init.d/ehcp"
elif [ -e "/etc/systemd/system" ]; then
cp "$FIXDIR/daemon/systemd/ehcp.service" "/etc/systemd/system"
rm -rf "/etc/init.d/ehcp"
fi
fi
# Reload systemd to pick up on the new daemon if applicable
systemctl daemon-reload
systemctl enable ehcp.service
if [ -e "/etc/init.d/ehcp" ]; then
update-rc.d ehcp enable
update-rc.d ehcp defaults
fi
}
function fail2ban(){
aptgetInstall fail2ban
manageService "fail2ban" "stop"
cp "/var/www/new/ehcp/fail2ban/apache-dos.conf" "/etc/fail2ban/filter.d/apache-dos.conf"
cp "/var/www/new/ehcp/fail2ban/ehcp.conf" "/etc/fail2ban/filter.d/ehcp.conf"
cp "/var/www/new/ehcp/fail2ban/postfix-sasl.conf" "/etc/fail2ban/filter.d/postfix-sasl.conf"
if [ ! -e "/etc/fail2ban/jail.local" ]; then
cp "/var/www/new/ehcp/fail2ban/jail.local" "/etc/fail2ban/jail.local"
fi
EHCPINF2BAN=$(cat /etc/fail2ban/jail.local | grep "\[ehcp\]")
APACHEDOSINF2BAN=$(cat /etc/fail2ban/jail.local | grep "\[apache-dos\]")
POSTFIXSASLINF2BAN=$(cat /etc/fail2ban/jail.local | grep "\[sasl\]")
if [ -z "$EHCPINF2BAN" ]; then
echo "
[ehcp]
# fail2ban section for Easy Hosting Control Panel, ehcp.net
enabled = true
port = http,https
filter = ehcp
logpath = /var/www/new/ehcp/log/ehcp_failed_authentication.log
maxretry = 10" >> "/etc/fail2ban/jail.local"
fi
if [ -z "$APACHEDOSINF2BAN" ]; then
echo "
[apache-dos]
# Apache Anti-DDoS Security Based Log Entries from Mod Evasive Apache Module
enabled = true
port = http,https
filter = apache-dos
logpath = /var/log/apache*/*error.log
maxretry = 5" >> "/etc/fail2ban/jail.local"
fi
if [ -z "$POSTFIXSASLINF2BAN" ]; then
echo "
[sasl]
enabled = true
port = smtp,ssmtp,imap2,imap3,imaps,pop3,pop3s
filter = postfix-sasl
# You might consider monitoring /var/log/warn.log instead
# if you are running postfix. See http://bugs.debian.org/507990
logpath = /var/log/mail.log
maxretry = 4" >> "/etc/fail2ban/jail.local"
fi
manageService "fail2ban" "restart"
}
function mysqlUseLocalHost(){
if [ -e "/etc/mysql/my.cnf" ]; then
sed -i "s/^bind-address.*/bind-address=0.0.0.0/g" "/etc/mysql/my.cnf"
fi
if [ -e "/etc/mysql/mariadb.conf.d/50-server.cnf" ]; then
sed -i "s/^bind-address.*/bind-address=0.0.0.0/g" "/etc/mysql/mariadb.conf.d/50-server.cnf"
fi
manageService "mysql" "restart"
}
function updateMySQLModeVariableIfNeeded(){
if [ -e "/etc/mysql/mariadb.conf.d/50-server.cnf" ]; then
sqlModeExists=$(cat "/etc/mysql/mariadb.conf.d/50-server.cnf" | grep -o "^sql_mode")
if [ ! -z "$sqlModeExists" ]; then
sed -i "s/^sql_mode.*/sql_mode=/g" "/etc/mysql/mariadb.conf.d/50-server.cnf"
else
mysqldSectionExists=$(cat "/etc/mysql/mariadb.conf.d/50-server.cnf" | grep -o "^\[mysqld\]")
if [ ! -z "$mysqldSectionExists" ]; then
sed -i "s/^\[mysqld\]/\[mysqld\]\\nsql_mode=/g" "/etc/mysql/mariadb.conf.d/50-server.cnf"
fi
fi
fi
if [ -e "/etc/mysql/my.cnf" ]; then
sqlModeExists=$(cat "/etc/mysql/my.cnf" | grep -o "^sql_mode")
if [ ! -z "$sqlModeExists" ]; then
sed -i "s/^sql_mode.*/sql_mode=/g" "/etc/mysql/my.cnf"
else
mysqldSectionExists=$(cat "/etc/mysql/my.cnf" | grep -o "^\[mysqld\]")
if [ ! -z "$mysqldSectionExists" ]; then
sed -i "s/^\[mysqld\]/\[mysqld\]\\nsql_mode=/g" "/etc/mysql/my.cnf"
fi
fi
fi
}
function apacheSecurity(){
aptgetInstall libapache2-mod-evasive
aptgetInstall libapache-mod-security
aptgetInstall libapache2-modsecurity
aptgetInstall libapache2-mod-security2
# Make sure the conf.d directory exists
addConfDFolder
# Shouldn't be running, but attempt to stop it anyways just in case
manageService "apache2" "stop"
# Ensures that we always get the latest security rules and apply only the latest
if [ -e "/etc/apache2/mod_security_rules" ]; then
rm -R "/etc/apache2/mod_security_rules"
fi
mkdir /etc/apache2/mod_security_rules
cd "$patchDir"
if [ -e "mod_security_rules_latest" ]; then
rm -R "mod_security_rules_latest"
fi
mkdir "mod_security_rules_latest"
# Different rules based on different versions
if [[ "$distro" == "ubuntu" && "$yrelease" == "13" && "$mrelease" == "10" ]] || [[ "$yrelease" -ge "14" && "$distro" == "ubuntu" ]] || [[ "$distro" == "debian" && "$yrelease" -ge "8" ]]; then
cp "$FIXDIR/apache2/modsecurity/mod_security_rules_13.10.tar.gz" "mod_security_rules.tar.gz"
else
cp "$FIXDIR/apache2/modsecurity/mod_security_base_rules.tar.gz" "mod_security_rules.tar.gz"
fi
tar -zxvf "mod_security_rules.tar.gz" -C "mod_security_rules_latest"
mv mod_security_rules_latest/* /etc/apache2/mod_security_rules
chown -R root:root /etc/apache2/mod_security_rules
# Config file names
ModSecureNewConfFile="security2.conf"
ModSecureNewLoadFile="security2.load"
ModEvasiveNewConfFile="evasive.conf"
ModEvasiveNewLoadFile="evasive.load"
ModSecureOldConfFile="mod-security.conf"
ModSecureOldLoadFile="mod-security.load"
ModEvasiveOldConfFile="mod-evasive.conf"
ModEvasiveOldLoadFile="mod-evasive.load"
# Cleanup mod security
if [ -e "/etc/apache2/mods-available/${ModSecureNewConfFile}" ]; then
# Overwrite official config in use
cp "/var/www/new/ehcp/mod_secure/modsecure" "/etc/apache2/mods-available/${ModSecureNewConfFile}"
# If it's enabled file also exists, copy that too
if [ -e "/etc/apache2/mods-enabled/${ModSecureNewConfFile}" ]; then
cp "/var/www/new/ehcp/mod_secure/modsecure" "/etc/apache2/mods-enabled/${ModSecureNewConfFile}"
fi
# Cleanup old config it exists from an OS upgrade
if [ -e "/etc/apache2/mods-available/${ModSecureOldConfFile}" ]; then
rm "/etc/apache2/mods-available/${ModSecureOldConfFile}"
fi
rm "/etc/apache2/mods-enabled/${ModSecureOldConfFile}"
# Remove old load file as well
if [ -e "/etc/apache2/mods-available/${ModSecureOldLoadFile}" ]; then
rm "/etc/apache2/mods-available/${ModSecureOldLoadFile}"
fi
rm "/etc/apache2/mods-enabled/${ModSecureOldLoadFile}"
# Cleanup our old version which is no longer needed since we'll use the official configuration
if [ -e "/etc/apache2/conf.d/modsecure" ]; then
rm "/etc/apache2/conf.d/modsecure"
fi
elif [ -e "/etc/apache2/mods-available/${ModSecureOldConfFile}" ]; then
# Overwrite official config in use
cp "/var/www/new/ehcp/mod_secure/modsecure" "/etc/apache2/mods-available/${ModSecureOldConfFile}"
# If it's enabled file also exists, copy that too
if [ -e "/etc/apache2/mods-enabled/${ModSecureOldConfFile}" ]; then
cp "/var/www/new/ehcp/mod_secure/modsecure" "/etc/apache2/mods-enabled/${ModSecureOldConfFile}"
fi
# Remove incorrect config file if it exists
if [ -e "/etc/apache2/mods-available/${ModSecureNewConfFile}" ]; then
rm "/etc/apache2/mods-available/${ModSecureNewConfFile}"
fi
if [ -e "/etc/apache2/mods-available/${ModSecureNewLoadFile}" ]; then
rm "/etc/apache2/mods-available/${ModSecureNewLoadFile}"
fi
rm "/etc/apache2/mods-enabled/${ModSecureNewConfFile}"
rm "/etc/apache2/mods-enabled/${ModSecureNewLoadFile}"
# Cleanup our old version which is no longer needed since we'll use the official configuration
if [ -e "/etc/apache2/conf.d/modsecure" ]; then
rm "/etc/apache2/conf.d/modsecure"
fi
else
# None exist
if [[ "$distro" == "ubuntu" && "$yrelease" -gt "13" ]] || [[ "$distro" == "ubuntu" && "$yrelease" == "13" && "$mrelease" == "10" ]] || [[ "$distro" == "debian" && "$yrelease" -ge "8" ]]; then
cp "/var/www/new/ehcp/mod_secure/modsecure" "/etc/apache2/mods-available/${ModSecureNewConfFile}"
cp "/var/www/new/ehcp/mod_secure/modsecure" "/etc/apache2/mods-enabled/${ModSecureNewConfFile}"
rm "/etc/apache2/mods-available/${ModSecureOldConfFile}"
rm "/etc/apache2/mods-enabled/${ModSecureOldConfFile}"
rm "/etc/apache2/mods-available/${ModSecureOldLoadFile}"
rm "/etc/apache2/mods-enabled/${ModSecureOldLoadFile}"
else
cp "/var/www/new/ehcp/mod_secure/modsecure" "/etc/apache2/mods-available/${ModSecureOldConfFile}"
cp "/var/www/new/ehcp/mod_secure/modsecure" "/etc/apache2/mods-enabled/${ModSecureOldConfFile}"
rm "/etc/apache2/mods-available/${ModSecureNewConfFile}"
rm "/etc/apache2/mods-enabled/${ModSecureNewConfFile}"
rm "/etc/apache2/mods-available/${ModSecureNewLoadFile}"
rm "/etc/apache2/mods-enabled/${ModSecureNewLoadFile}"
fi
# Cleanup our old version which is no longer needed since we'll use the official configuration
if [ -e "/etc/apache2/conf.d/modsecure" ]; then
rm "/etc/apache2/conf.d/modsecure"
fi
fi
# Cleanup mod evasive
if [ -e "/etc/apache2/mods-available/${ModEvasiveNewConfFile}" ]; then
# Overwrite official config in use
cp "/var/www/new/ehcp/mod_secure/modevasive" "/etc/apache2/mods-available/${ModEvasiveNewConfFile}"
# If it's enabled file also exists, copy that too
if [ -e "/etc/apache2/mods-enabled/${ModEvasiveNewConfFile}" ]; then
cp "/var/www/new/ehcp/mod_secure/modevasive" "/etc/apache2/mods-enabled/${ModEvasiveNewConfFile}"
fi
# Cleanup old config it exists from an OS upgrade
if [ -e "/etc/apache2/mods-available/${ModEvasiveOldConfFile}" ]; then
rm "/etc/apache2/mods-available/${ModEvasiveOldConfFile}"
fi
rm "/etc/apache2/mods-enabled/${ModEvasiveOldConfFile}"
# Remove old load file as well
if [ -e "/etc/apache2/mods-available/${ModEvasiveOldLoadFile}" ]; then
rm "/etc/apache2/mods-available/${ModEvasiveOldLoadFile}"
fi
rm "/etc/apache2/mods-enabled/${ModEvasiveOldLoadFile}"
# Cleanup our old version which is no longer needed since we'll use the official configuration
if [ -e "/etc/apache2/conf.d/modevasive" ]; then
rm "/etc/apache2/conf.d/modevasive"
fi
elif [ -e "/etc/apache2/mods-available/${ModEvasiveOldConfFile}" ]; then
# Overwrite official config in use
cp "/var/www/new/ehcp/mod_secure/modevasive" "/etc/apache2/mods-available/${ModEvasiveOldConfFile}"
# If it's enabled file also exists, copy that too
if [ -e "/etc/apache2/mods-enabled/${ModEvasiveOldConfFile}" ]; then
cp "/var/www/new/ehcp/mod_secure/modevasive" "/etc/apache2/mods-enabled/${ModEvasiveOldConfFile}"
fi
# Remove incorrect config file if it exists
if [ -e "/etc/apache2/mods-available/${ModEvasiveNewConfFile}" ]; then
rm "/etc/apache2/mods-available/${ModEvasiveNewConfFile}"
fi
if [ -e "/etc/apache2/mods-available/${ModEvasiveNewLoadFile}" ]; then
rm "/etc/apache2/mods-available/${ModEvasiveNewLoadFile}"
fi
rm "/etc/apache2/mods-enabled/${ModEvasiveNewConfFile}"
rm "/etc/apache2/mods-enabled/${ModEvasiveNewLoadFile}"
# Cleanup our old version which is no longer needed since we'll use the official configuration
if [ -e "/etc/apache2/conf.d/modevasive" ]; then
rm "/etc/apache2/conf.d/modevasive"
fi
else
# None exist
if [[ "$distro" == "ubuntu" && "$yrelease" -gt "13" ]] || [[ "$distro" == "ubuntu" && "$yrelease" == "13" && "$mrelease" == "10" ]] || [[ "$distro" == "debian" && "$yrelease" -ge "8" ]]; then
cp "/var/www/new/ehcp/mod_secure/modevasive" "/etc/apache2/mods-available/${ModEvasiveNewConfFile}"
cp "/var/www/new/ehcp/mod_secure/modevasive" "/etc/apache2/mods-enabled/${ModEvasiveNewConfFile}"
rm "/etc/apache2/mods-enabled/${ModEvasiveOldConfFile}"
rm "/etc/apache2/mods-enabled/${ModEvasiveOldLoadFile}"
rm "/etc/apache2/mods-available/${ModEvasiveOldConfFile}"
rm "/etc/apache2/mods-available/${ModEvasiveOldLoadFile}"
else
cp "/var/www/new/ehcp/mod_secure/modevasive" "/etc/apache2/mods-available/${ModEvasiveOldConfFile}"
cp "/var/www/new/ehcp/mod_secure/modevasive" "/etc/apache2/mods-enabled/${ModEvasiveOldConfFile}"
rm "/etc/apache2/mods-enabled/${ModEvasiveNewConfFile}"
rm "/etc/apache2/mods-enabled/${ModEvasiveNewLoadFile}"
rm "/etc/apache2/mods-available/${ModEvasiveNewConfFile}"
rm "/etc/apache2/mods-available/${ModEvasiveNewLoadFile}"
fi
# Cleanup our old version which is no longer needed since we'll use the official configuration
if [ -e "/etc/apache2/conf.d/modevasive" ]; then
rm "/etc/apache2/conf.d/modevasive"
fi
fi
if [[ "$distro" == "ubuntu" && "$yrelease" -gt "13" ]] || [[ "$distro" == "ubuntu" && "$yrelease" == "13" && "$mrelease" == "10" ]] || [[ "$distro" == "debian" && "$yrelease" -ge "8" ]]; then
a2enmod evasive
a2enmod security2
else
a2enmod mod-evasive
a2enmod mod-security
fi
}
function finalize(){
# Get web server type
detectRunningWebServer
# Get PHP Session Time
getPHPSessionTimeout
if [ -z "$SessionTimeoutSetting" ]; then
echo -e "Adding cronjob to cleanup old php session files"
# Add cronjob to clean up old php session files
addSystemCronJob "0 2 */5 * *" "/var/www/new/ehcp/scripts/cleanup/remove_old_php_tmp_session_files.sh"
fi
# Make sure all log files have correct group
ERRORLOGAPACH="/var/log/apache2/access.log"
if [ -e "$ERRORLOGAPACH" ]; then
APACHLOGGROUPOWNER=$(ls -ali "$ERRORLOGAPACH" | awk '{print $5}')
chown root:"$APACHLOGGROUPOWNER" -R "/var/log/apache2"
fi
cd "$patchDir"
# Download Let's Encrypt Certbot:
installCertBotLetsEncrypt
# Sync domains
cp "$FIXDIR/api/syncdomains_apiscript.tar.gz" "syncdomains_apiscript.tar.gz"
tar -zxvf "syncdomains_apiscript.tar.gz"
php syncdomains.php
# Sync ez install information
cp "$FIXDIR/api/update_ez_install.tar.gz" "update_ez_install.tar.gz"
tar -zxvf "update_ez_install.tar.gz"
php update_ez_install.php
# Enable php-fpm
enablePHPFPMService
# Killall and restart mysql
killAllMySQLAndRestart
# Restart PolicyD if available
if [ ! -z "$insPolicyD" ] && [ "$insPolicyD" = true ]; then
manageService "policyd_agent" "stop"
manageService "policyd_agent" "start"
fi
# Restart ehcp
manageService "ehcp" "restart"
echo -e "\nWaiting 30 seconds before restarting $WebServerType daemon so that website configs are reconstructed.\n"
sleep 30s
# Restart php-fpm before nginx if the web server is nginx
echo -e "\nRestarting PHP FPM service.\n"
managePHPFPMService
# Restart web server
echo -e "\nRestarting the ${WebServerType} web server.\n"
manageService "$WebServerType" "restart"
# Restart php-fpm once more just in case - seen some weird issues where another restart is needed
managePHPFPMService "stop"
managePHPFPMService "start"
# Make sure courier daemon is enabled (mainly for Ubuntu 16.04 and up)
echo -e "\nAdjusing and restarting courier-authdaemon defaults.\n"
update-rc.d courier-authdaemon enable
update-rc.d courier-authdaemon defaults
manageService "courier-authdaemon" "restart"
# Restart postfix
echo -e "\nRestarting postfix.\n"
manageService "postfix" "restart"
echo -e "\nRunning final cleanup.\n"
finalCleanup
if [ -e "$UserDefinedPostScript" ]; then
echo -e "\nRunning user defined post update script.\n"
bash "$UserDefinedPostScript"
fi
}
# Get distro name , by Marcel <marcelbutucea@gmail.com>, thanks to marcel for fixing whole code syntax
# No longer works in Ubuntu 13.04
# Fixed by Eric Martin <earnolmartin@gmail.com>
function checkDistro() {
# Get distro properly
# Get distro properly
if [ -e /etc/issue ]; then
distro=$( cat /etc/issue | awk '{ print $1 }' | head -n 1 )
fi
if [ ! -z "$distro" ]; then
# Convert it to lowercase
distro=$( echo $distro | awk '{print tolower($0)}' )
fi
if [ -z "$distro" ] || [[ "$distro" != "ubuntu" && "$distro" != "debian" ]]; then
if [ -e /etc/os-release ]; then
distro=$( cat /etc/os-release | grep -o "^NAME=.*" | grep -o "[^NAME=\"].*[^\"]" | awk '{print $1}' | awk '{print tolower($0)}' )
fi
fi
# Assume Ubuntu
if [ -z "$distro" ]; then
distro="ubuntu"
else
# Convert it to lowercase
distro=$( echo $distro | awk '{print tolower($0)}' )
fi
# Get actual release version information
version=$( lsb_release -r | awk '{ print $2 }' )
if [ -z "$version" ]; then
version=$( cat /etc/issue | awk '{ print $2 }' )
fi
# Separate year and version
if [[ "$version" == *.* ]]; then
yrelease=$( echo "$version" | cut -d. -f1 )
mrelease=$( echo "$version" | cut -d. -f2 )
else
yrelease="$version"
mrelease="0"
fi
# Get 64-bit OS or 32-bit OS [used in vsftpd fix]
if [ $( uname -m ) == 'x86_64' ]; then
OSBits=64
else
OSBits=32
fi
# Another way to get the version number
# version=$(lsb_release -r | awk '{ print $2 }')
echo "Your distro is $distro runnning version $version"
if [ "$distro" != "debian" ]; then
echo "Your distros yearly release is $yrelease. Your distros monthly release is $mrelease."
fi
# Get distro and version initially installed on
if [ -e "/var/www/new/ehcp/distro_during_install.txt" ]; then
oldDistro=$(cat "/var/www/new/ehcp/distro_during_install.txt")
fi
if [ -e "/var/www/new/ehcp/version_during_install.txt" ]; then
oldYRelease=$(cat "/var/www/new/ehcp/version_during_install.txt" | awk {'print $1'})
oldMRelease=$(cat "/var/www/new/ehcp/version_during_install.txt" | awk {'print $2'})
fi
if [ ! -z "$oldYRelease" ] && [ ! -z "$oldMRelease" ] && [ ! -z "$oldDistro" ]; then
if [ "$oldYRelease" != "$yrelease" ] || [ "$oldMRelease" != "$mrelease" ] || [ "$oldDistro" != "$distro" ]; then
OSUpgradeChangeDetectedFromInstall=true
else
OSUpgradeChangeDetectedFromInstall=false
fi
else
OSUpgradeChangeDetectedFromInstall=true
fi
if [ "$distro" == "debian" ] && [ "$yrelease" -lt "8" ]; then
echo "Debian 7.x and lower are no longer supported."
exit
fi
}
function fixVSFTPConfig(){ # by earnolmartin@gmail.com
# VSFTPD config file
VSFTPDCONF="/etc/vsftpd.conf"
# Check to see if the chroot_local_user setting is present
chrootLocalCheck=$(cat "$VSFTPDCONF" | grep "chroot_local_user")
if [ -z "$chrootLocalCheck" ]; then
sh -c "echo 'chroot_local_user=YES' >> $VSFTPDCONF"
else
sed -i 's/chroot_local_user=.*/chroot_local_user=YES/g' "$VSFTPDCONF"
fi
# Check to see if allow writeable chroot is present
allowWriteValue=$(cat "$VSFTPDCONF" | grep "allow_writeable_chroot")
if [ -z "$allowWriteValue" ]; then
sh -c "echo 'allow_writeable_chroot=YES' >> $VSFTPDCONF"
else
sed -i 's/allow_writeable_chroot=.*/allow_writeable_chroot=YES/g' "$VSFTPDCONF"
fi
if [ $OSBits -eq "64" ]; then
#aptgetInstall libpam-ldap # this is required in buggy vsftpd installs.. ubuntu 12.04,12.10, 13.04, now...
libldapFix
aptgetInstall libgcc1
# 64-bit 500 OOPS: priv_sock_get_cmd Fix
# seccomp_sandbox=NO
allowSandBox=$(cat "$VSFTPDCONF" | grep "seccomp_sandbox")
if [ -z "$allowSandBox" ]; then
if [[ "$distro" == "ubuntu" && "$yrelease" -ge "13" ]] || [[ "$distro" == "debian" && "$yrelease" -ge "8" ]]; then
sh -c "echo 'seccomp_sandbox=NO' >> $VSFTPDCONF"
fi
else
sed -i 's/seccomp_sandbox=.*/seccomp_sandbox=NO/g' "$VSFTPDCONF"
fi
fi
# Restart the VSFTPD service
manageService "vsftpd" "restart"
}
function remove_vsftpd(){
# Remove the hold if any:
echo "vsftpd install" | dpkg --set-selections
# Remove originally installed vsftpd
aptgetRemove vsftpd
# Just incase it's been installed already or another version has been installed using dpgk, let's remove it
dpkg --remove vsftpd
}
function fixApacheDefault(){
ApacheFile="/etc/apache2/apache2.conf"
confStr="IncludeOptional sites-enabled/\*.conf"
correctConfStr="Include sites-enabled/"
if [ -e "$ApacheFile" ]; then
ConfCheck=$( cat "$ApacheFile" | grep -o "$confStr" )
if [ ! -z "$ConfCheck" ]; then
sed -i "s#$confStr#$correctConfStr#g" "$ApacheFile"
manageService "apache2" "restart"
fi
fi
}
function removeNameVirtualHost(){
ApacheFile="/etc/apache2/ports.conf"
confStr="NameVirtualHost.*"
if [ -e "$ApacheFile" ]; then
ConfCheck=$( cat "$ApacheFile" | grep -o "$confStr" )
if [ ! -z "$ConfCheck" ]; then
sed -i "s#$confStr##g" "$ApacheFile"
manageService "apache2" "restart"
fi
fi
}
function genUbuntuFixes(){
# Ubuntu packages keep coming with new features that mess things up
# Thanks Ubuntu for the unneccessary headaches!
# Includes debian too... jesus christ
if [ ! -z "$yrelease" ]; then
# Due to template changes, we need to set older web servers to use nginx because the ondrej version of apache2 will not work in 12.04 and earlier
if [[ "$distro" == "ubuntu" && "$yrelease" -le "12" ]] || [[ "$distro" == "debian" && "$yrelease" -le "7" ]]; then
setWebServerModeToNginx
syncDomainsPostInstall=true
fi
if [[ "$distro" == "ubuntu" && "$yrelease" -ge "13" ]] || [[ "$distro" == "debian" && "$yrelease" -ge "8" ]]; then
fixApacheDefault
removeNameVirtualHost
addConfDFolder
getRidOfExtraPHPMyAdminAlias
fixApache2ConfigInvalidLockFileLine
fixMariaDBSkippingInnoDB
fi
if [[ "$distro" == "ubuntu" && "$yrelease" -ge "16" ]] || [[ "$distro" == "debian" && "$yrelease" -ge "8" ]]; then
# Fixes libpam for virtual VSFTPD and email accoutns
installPythonPamMysql
fi
# Ubuntu 18.04 and Debian 10 Fixes
if [[ "$distro" == "ubuntu" && "$yrelease" -eq "18" && "$mrelease" == "04" ]]; then
fixQuotaForEmailsPostfix3x
fi
# Ubuntu 14.04 Fix 2022
if [[ "$distro" == "ubuntu" && "$yrelease" -eq "14" ]]; then
fixUbuntu14SSL
fi
fi
}
function finalCleanup(){
if [ -e "${serviceNameTempFile}" ]; then
rm -rf "${serviceNameTempFile}"
fi
}
function genFixes(){
# Generic fixes that should be run
if [ ! -e "/etc/apache2/httpd.conf" ]; then
sed -i 's/^Include httpd.conf/#Include httpd.conf/g' "/etc/apache2/apache2.conf"
fi
# add panel conf to apache2 or nginx
if [ -e "/etc/apache2/apache2.conf" ]; then
hasEHCPPanelConf=$(cat "/etc/apache2/apache2.conf" | grep -o "apachehcp_globalpanelurls.conf")
if [ -z "$hasEHCPPanelConf" ]; then
echo -e "Include /var/www/new/ehcp/apachehcp_globalpanelurls.conf" >> "/etc/apache2/apache2.conf"
fi
fi