Skip to content

Commit 6cb207d

Browse files
committed
[Firewall] Extend fw cli script to accept ipset names
1 parent a5c8fd4 commit 6cb207d

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

bin/v-add-firewall-rule

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,21 @@ sort_fw_rules() {
4747
#----------------------------------------------------------#
4848

4949
check_args '3' "$#" 'ACTION IP PORT [PROTOCOL] [COMMENT] [RULE]'
50-
is_format_valid 'action' 'protocol' 'port_ext' 'ip'
50+
is_format_valid 'action' 'protocol' 'port_ext'
5151
is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
5252
get_next_fw_rule
5353
is_format_valid 'rule'
5454
is_object_new '../../data/firewall/rules' 'RULE' "$rule"
5555
if [ ! -z "$comment" ]; then
5656
is_format_valid 'comment'
5757
fi
58+
if [[ "$ip" =~ ^ipset: ]]; then
59+
ipset_name="${ip#ipset:}"
60+
v-list-firewall-ipset plain | grep "^$ipset_name\s" >/dev/null
61+
check_result $? 'ipset object not found' $E_NOTEXIST
62+
else
63+
is_format_valid 'ip'
64+
fi
5865

5966
# Perform verification if read-only mode is enabled
6067
check_hestia_demo_mode

bin/v-change-firewall-rule

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,21 @@ sort_fw_rules() {
4040
#----------------------------------------------------------#
4141

4242
check_args '5' "$#" 'RULE ACTION IP PORT [PROTOCOL] [COMMENT]'
43-
is_format_valid 'rule' 'action' 'protocol' 'port_ext' 'ip'
43+
is_format_valid 'rule' 'action' 'protocol' 'port_ext'
4444
if [ ! -z "$comment" ]; then
4545
is_format_valid 'comment'
4646
fi
4747
is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
4848
is_object_valid '../../data/firewall/rules' 'RULE' "$rule"
4949

50+
if [[ "$ip" =~ ^ipset: ]]; then
51+
ipset_name="${ip#ipset:}"
52+
v-list-firewall-ipset plain | grep "^$ipset_name\s" >/dev/null
53+
check_result $? 'ipset object not found' $E_NOTEXIST
54+
else
55+
is_format_valid 'ip'
56+
fi
57+
5058
# Perform verification if read-only mode is enabled
5159
check_hestia_demo_mode
5260

bin/v-update-firewall

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ if [[ "$sshport" =~ ^[0-9]+$ ]] && [ "$sshport" -ne "22" ]; then
5656
sed -i "s/PORT='22'/PORT=\'$sshport\'/" $rules
5757
fi
5858

59+
# Load ipset lists before adding Hestia iptables rules
60+
$BIN/v-update-firewall-ipset
61+
5962
# Creating temporary file
6063
tmp=$(mktemp)
6164

@@ -83,10 +86,17 @@ for line in $(sort -r -n -k 2 -t \' $rules); do
8386
if [ "$SUSPENDED" = 'no' ]; then
8487
proto="-p $PROTOCOL"
8588
port="--dport $PORT"
86-
ip="-s $IP"
8789
state=""
8890
action="-j $ACTION"
8991

92+
if [[ "$IP" =~ ^ipset: ]]; then
93+
ipset_name="${IP#ipset:}"
94+
$(v-list-firewall-ipset plain | grep "^$ipset_name\s" >/dev/null) || log_event $E_NOTEXIST "ipset object ($ipset_name) not found"
95+
ip="-m set --match-set '${ipset_name}' src"
96+
else
97+
ip="-s $IP"
98+
fi
99+
90100
# Adding multiport module
91101
if [[ "$PORT" =~ ,|-|: ]] ; then
92102
port="-m multiport --dports ${PORT//-/:}"

0 commit comments

Comments
 (0)