Ver Fonte

[Firewall] Extend fw cli script to accept ipset names

Robert Zollner há 5 anos atrás
pai
commit
6cb207db7b
3 ficheiros alterados com 28 adições e 3 exclusões
  1. 8 1
      bin/v-add-firewall-rule
  2. 9 1
      bin/v-change-firewall-rule
  3. 11 1
      bin/v-update-firewall

+ 8 - 1
bin/v-add-firewall-rule

@@ -47,7 +47,7 @@ sort_fw_rules() {
 #----------------------------------------------------------#
 #----------------------------------------------------------#
 
 
 check_args '3' "$#" 'ACTION IP PORT [PROTOCOL] [COMMENT] [RULE]'
 check_args '3' "$#" 'ACTION IP PORT [PROTOCOL] [COMMENT] [RULE]'
-is_format_valid 'action' 'protocol' 'port_ext' 'ip'
+is_format_valid 'action' 'protocol' 'port_ext'
 is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
 is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
 get_next_fw_rule
 get_next_fw_rule
 is_format_valid 'rule'
 is_format_valid 'rule'
@@ -55,6 +55,13 @@ is_object_new '../../data/firewall/rules' 'RULE' "$rule"
 if [ ! -z "$comment" ]; then
 if [ ! -z "$comment" ]; then
     is_format_valid 'comment'
     is_format_valid 'comment'
 fi
 fi
+if [[ "$ip" =~ ^ipset: ]]; then
+    ipset_name="${ip#ipset:}"
+    v-list-firewall-ipset plain | grep "^$ipset_name\s" >/dev/null
+    check_result $? 'ipset object not found' $E_NOTEXIST
+else
+    is_format_valid 'ip'
+fi
 
 
 # Perform verification if read-only mode is enabled
 # Perform verification if read-only mode is enabled
 check_hestia_demo_mode
 check_hestia_demo_mode

+ 9 - 1
bin/v-change-firewall-rule

@@ -40,13 +40,21 @@ sort_fw_rules() {
 #----------------------------------------------------------#
 #----------------------------------------------------------#
 
 
 check_args '5' "$#" 'RULE ACTION IP  PORT [PROTOCOL] [COMMENT]'
 check_args '5' "$#" 'RULE ACTION IP  PORT [PROTOCOL] [COMMENT]'
-is_format_valid 'rule' 'action' 'protocol' 'port_ext' 'ip'
+is_format_valid 'rule' 'action' 'protocol' 'port_ext'
 if [ ! -z "$comment" ]; then
 if [ ! -z "$comment" ]; then
     is_format_valid 'comment'
     is_format_valid 'comment'
 fi
 fi
 is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
 is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'
 is_object_valid '../../data/firewall/rules' 'RULE' "$rule"
 is_object_valid '../../data/firewall/rules' 'RULE' "$rule"
 
 
+if [[ "$ip" =~ ^ipset: ]]; then
+    ipset_name="${ip#ipset:}"
+    v-list-firewall-ipset plain | grep "^$ipset_name\s" >/dev/null
+    check_result $? 'ipset object not found' $E_NOTEXIST
+else
+    is_format_valid 'ip'
+fi
+
 # Perform verification if read-only mode is enabled
 # Perform verification if read-only mode is enabled
 check_hestia_demo_mode
 check_hestia_demo_mode
 
 

+ 11 - 1
bin/v-update-firewall

@@ -56,6 +56,9 @@ if [[ "$sshport" =~ ^[0-9]+$ ]] && [ "$sshport" -ne "22"  ]; then
     sed -i "s/PORT='22'/PORT=\'$sshport\'/" $rules
     sed -i "s/PORT='22'/PORT=\'$sshport\'/" $rules
 fi
 fi
 
 
+# Load ipset lists before adding Hestia iptables rules
+$BIN/v-update-firewall-ipset
+
 # Creating temporary file
 # Creating temporary file
 tmp=$(mktemp)
 tmp=$(mktemp)
 
 
@@ -83,10 +86,17 @@ for line in $(sort -r -n -k 2 -t \' $rules); do
     if [ "$SUSPENDED" = 'no' ]; then
     if [ "$SUSPENDED" = 'no' ]; then
         proto="-p $PROTOCOL"
         proto="-p $PROTOCOL"
         port="--dport $PORT"
         port="--dport $PORT"
-        ip="-s $IP"
         state=""
         state=""
         action="-j $ACTION"
         action="-j $ACTION"
 
 
+        if [[ "$IP" =~ ^ipset: ]]; then
+            ipset_name="${IP#ipset:}"
+            $(v-list-firewall-ipset plain | grep "^$ipset_name\s" >/dev/null) || log_event $E_NOTEXIST "ipset object ($ipset_name) not found"
+            ip="-m set --match-set '${ipset_name}' src"
+        else
+            ip="-s $IP"
+        fi
+
         # Adding multiport module
         # Adding multiport module
         if [[ "$PORT" =~ ,|-|: ]] ; then
         if [[ "$PORT" =~ ,|-|: ]] ; then
             port="-m multiport --dports ${PORT//-/:}"
             port="-m multiport --dports ${PORT//-/:}"