-
Notifications
You must be signed in to change notification settings - Fork 2
/
vpbxgahafirewall.sh
244 lines (231 loc) · 13.3 KB
/
vpbxgahafirewall.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
#!/bin/bash
# This code is the property of VitalPBX LLC Company
# License: Proprietary
# Date: 19-Aug-2020
# VitalPBX Hight Availability Update Firewall Rules
#
set -e
function jumpto
{
label=$start
cmd=$(sed -n "/$label:/{:a;n;p;ba};" $0 | grep -v ':$')
eval "$cmd"
exit
}
echo -e "\n"
echo -e "************************************************************"
echo -e "* Welcome to the VitalPBX high availability installation *"
echo -e "* You need three server for this implementation *"
echo -e "* All options are mandatory *"
echo -e "************************************************************"
filename="config.txt"
if [ -f $filename ]; then
echo -e "config file"
n=1
while read line; do
case $n in
1)
ip_master=$line
;;
2)
ip_standby=$line
;;
3)
ip_app=$line
;;
esac
n=$((n+1))
done < $filename
echo -e "IP Master................ > $ip_master"
echo -e "IP Standby............... > $ip_standby"
echo -e "IP Application........... > $ip_app"
fi
while [[ $ip_master == '' ]]
do
read -p "IP Master................ > " ip_master
done
while [[ $ip_standby == '' ]]
do
read -p "IP Standby............... > " ip_standby
done
while [[ $ip_app == '' ]]
do
read -p "IP Application........... > " ip_app
done
echo -e "************************************************************"
echo -e "* Check Information *"
echo -e "* Make sure you have internet on three servers *"
echo -e "************************************************************"
while [[ $veryfy_info != yes && $veryfy_info != no ]]
do
read -p "Are you sure to continue with this settings? (yes,no) > " veryfy_info
done
if [ "$veryfy_info" = yes ] ;then
echo -e "************************************************************"
echo -e "* Starting to run the scripts - Update Firewall *"
echo -e "************************************************************"
else
exit;
fi
stepFile=stepfirewall.txt
if [ -f $stepFile ]; then
step=`cat $stepFile`
else
step=0
fi
echo -e "Start in step: " $step
start="configuring_firewall"
case $step in
1)
start="configuring_firewall"
;;
2)
start="create_ami_user"
;;
3)
start="vitalpbx_cluster_ok"
;;
esac
jumpto $start
echo -e "*** Done Step 1 ***"
echo -e "1" > stepfirewall.txt
configuring_firewall:
echo -e "************************************************************"
echo -e "* Configuring Temporal Firewall *"
echo -e "************************************************************"
#Create temporal Firewall Rules in Server 1 and 2
firewall-cmd --permanent --add-service=high-availability
firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --permanent --zone=public --add-port=4567/tcp
firewall-cmd --permanent --zone=public --add-port=4568/tcp
firewall-cmd --permanent --zone=public --add-port=4444/tcp
firewall-cmd --permanent --zone=public --add-port=4567/udp
firewall-cmd --permanent --zone=public --add-port=5038/udp
firewall-cmd --permanent --zone=public --add-rich-rule 'rule family='ipv4' source address='$ip_app' port port=5038 protocol=tcp accept'
firewall-cmd --reload
ssh root@$ip_standby "firewall-cmd --permanent --add-service=high-availability"
ssh root@$ip_standby "firewall-cmd --permanent --zone=public --add-port=3306/tcp"
ssh root@$ip_standby "firewall-cmd --permanent --zone=public --add-port=4567/tcp"
ssh root@$ip_standby "firewall-cmd --permanent --zone=public --add-port=4568/tcp"
ssh root@$ip_standby "firewall-cmd --permanent --zone=public --add-port=4444/tcp"
ssh root@$ip_standby "firewall-cmd --permanent --zone=public --add-port=4567/udp"
ssh root@$ip_standby "firewall-cmd --permanent --zone=public --add-port=5038/udp"
ssh root@$ip_standby 'firewall-cmd --permanent --zone=public --add-rich-rule "rule family='ipv4' source address='$ip_app' port port=5038 protocol=tcp accept"'
ssh root@$ip_standby "firewall-cmd --reload"
ssh root@$ip_app "firewall-cmd --permanent --zone=public --add-port=3306/tcp"
ssh root@$ip_app "firewall-cmd --permanent --zone=public --add-port=4567/tcp"
ssh root@$ip_app "firewall-cmd --permanent --zone=public --add-port=4568/tcp"
ssh root@$ip_app "firewall-cmd --permanent --zone=public --add-port=4444/tcp"
ssh root@$ip_app "firewall-cmd --permanent --zone=public --add-port=4567/udp"
ssh root@$ip_app "firewall-cmd --reload"
echo -e "************************************************************"
echo -e "* Configuring Permanent Firewall *"
echo -e "* Creating Firewall Services in VitalPBX in Server 1 *"
echo -e "************************************************************"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_services (name, protocol, port) VALUES ('MariaDB Client', 'tcp', '3306')"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_services (name, protocol, port) VALUES ('MariaDB Galera Traffic', 'tcp', '4567-4568')"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_services (name, protocol, port) VALUES ('MariaDB Galera SST', 'tcp', '4444')"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_services (name, protocol, port) VALUES ('HA2224', 'tcp', '2224')"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_services (name, protocol, port) VALUES ('HA3121', 'tcp', '3121')"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_services (name, protocol, port) VALUES ('HA5403', 'tcp', '5403')"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_services (name, protocol, port) VALUES ('HA5404-5405', 'udp', '5404-5405')"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_services (name, protocol, port) VALUES ('HA21064', 'tcp', '21064')"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_services (name, protocol, port) VALUES ('HA9929', 'both', '9929')"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_services (name, protocol, port) VALUES ('Asterisk AMI', 'both', '5038')"
echo -e "************************************************************"
echo -e "* Configuring Permanent Firewall *"
echo -e "* Creating Firewall Rules in VitalPBX in Server 1 *"
echo -e "************************************************************"
last_index=$(mysql -uroot ombutel -e "SELECT MAX(\`index\`) AS Consecutive FROM ombu_firewall_rules" | awk 'NR==2')
last_index=$last_index+1
service_id=$(mysql -uroot ombutel -e "select firewall_service_id from ombu_firewall_services where name = 'MariaDB Client'" | awk 'NR==2')
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_master', 'accept', $last_index)"
last_index=$last_index+1
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_standby', 'accept', $last_index)"
last_index=$last_index+1
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_app', 'accept', $last_index)"
last_index=$last_index+1
service_id=$(mysql -uroot ombutel -e "select firewall_service_id from ombu_firewall_services where name = 'MariaDB Galera Traffic'" | awk 'NR==2')
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_master', 'accept', $last_index)"
last_index=$last_index+1
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_standby', 'accept', $last_index)"
last_index=$last_index+1
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_app', 'accept', $last_index)"
last_index=$last_index+1
service_id=$(mysql -uroot ombutel -e "select firewall_service_id from ombu_firewall_services where name = 'MariaDB Galera SST'" | awk 'NR==2')
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_master', 'accept', $last_index)"
last_index=$last_index+1
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_standby', 'accept', $last_index)"
last_index=$last_index+1
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_app', 'accept', $last_index)"
last_index=$last_index+1
service_id=$(mysql -uroot ombutel -e "select firewall_service_id from ombu_firewall_services where name = 'HA2224'" | awk 'NR==2')
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_master', 'accept', $last_index)"
last_index=$last_index+1
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_standby', 'accept', $last_index)"
last_index=$last_index+1
service_id=$(mysql -uroot ombutel -e "select firewall_service_id from ombu_firewall_services where name = 'HA3121'" | awk 'NR==2')
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_master', 'accept', $last_index)"
last_index=$last_index+1
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_standby', 'accept', $last_index)"
last_index=$last_index+1
service_id=$(mysql -uroot ombutel -e "select firewall_service_id from ombu_firewall_services where name = 'HA5403'" | awk 'NR==2')
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_master', 'accept', $last_index)"
last_index=$last_index+1
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_standby', 'accept', $last_index)"
last_index=$last_index+1
service_id=$(mysql -uroot ombutel -e "select firewall_service_id from ombu_firewall_services where name = 'HA5404-5405'" | awk 'NR==2')
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_master', 'accept', $last_index)"
last_index=$last_index+1
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_standby', 'accept', $last_index)"
last_index=$last_index+1
service_id=$(mysql -uroot ombutel -e "select firewall_service_id from ombu_firewall_services where name = 'HA21064'" | awk 'NR==2')
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_master', 'accept', $last_index)"
last_index=$last_index+1
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_standby', 'accept', $last_index)"
last_index=$last_index+1
service_id=$(mysql -uroot ombutel -e "select firewall_service_id from ombu_firewall_services where name = 'HA9929'" | awk 'NR==2')
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_master', 'accept', $last_index)"
last_index=$last_index+1
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_standby', 'accept', $last_index)"
last_index=$last_index+1
service_id=$(mysql -uroot ombutel -e "select firewall_service_id from ombu_firewall_services where name = 'Asterisk AMI'" | awk 'NR==2')
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_floating', 'accept', $last_index)"
last_index=$last_index+1
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_rules (firewall_service_id, source, action, \`index\`) VALUES ($service_id, '$ip_app', 'accept', $last_index)"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_whitelist (host, description, \`default\`) VALUES ('$ip_master', 'Server 1 IP', 'no')"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_whitelist (host, description, \`default\`) VALUES ('$ip_standby', 'Server 2 IP', 'no')"
mysql -uroot ombutel -e "INSERT INTO ombu_firewall_whitelist (host, description, \`default\`) VALUES ('$ip_app', 'Server App IP', 'no')"
echo -e "*** Done Step 2 ***"
echo -e "2" > stepfirewall.txt
create_ami_user:
echo -e "************************************************************"
echo -e "* Creating AMI User *"
echo -e "************************************************************"
cat > /etc/asterisk/vitalpbx/manager__50-astboard-user.conf << EOF
[astboard]
secret = astboard
deny = 0.0.0.0/0.0.0.0
permit= 0.0.0.0/0.0.0.0
read = all
write = all
writetimeout = 5000
eventfilter=!Event: RTCP*
eventfilter=!Event: VarSet
eventfilter=!Event: Cdr
eventfilter=!Event: DTMF
eventfilter=!Event: AGIExec
eventfilter=!Event: ExtensionStatus
eventfilter=!Event: ChannelUpdate
eventfilter=!Event: ChallengeSent
eventfilter=!Event: SuccessfulAuth
eventfilter=!Event: NewExten
EOF
chown apache:root /etc/asterisk/vitalpbx/manager__50-astboard-user.conf
echo -e "*** Done Step 3 ***"
echo -e "3" > stepfirewall.txt
vitalpbx_cluster_ok:
echo -e "************************************************************"
echo -e "* VitalPBX Update Firewall OK *"
echo -e "************************************************************"
role