Browse Source

Add Fail2Ban unban support to firewall ban removal script (#5139)

* Add Fail2Ban unban support to firewall ban removal script

This update enhances the v-delete-firewall-ban script by integrating Fail2Ban unban functionality.

Implemented logic to iterate through all Fail2Ban jails and unban the corresponding IP if the jail uses a hestia-* action matching the firewall chain.

This ensures that IPs removed from Hestia’s firewall are also properly unbanned from Fail2Ban jails, keeping both systems synchronized.

* chmod 0755 bin/v-*

---------

Co-authored-by: hanshenrik <hans@loltek.net>
sahsanu 3 months ago
parent
commit
de30145e69
2 changed files with 13 additions and 1 deletions
  1. 13 1
      bin/v-delete-firewall-ban
  2. 0 0
      bin/v-delete-user-backup-restic

+ 13 - 1
bin/v-delete-firewall-ban

@@ -14,8 +14,9 @@
 ipv4_cidr=$1
 chain=$(echo $2 | tr '[:lower:]' '[:upper:]')
 
-# Defining absolute path for iptables and modprobe
+# Defining absolute path for iptables and fail2ban-client
 iptables="/sbin/iptables"
+f2bcli="/usr/bin/fail2ban-client"
 
 # Includes
 # shellcheck source=/etc/hestiacp/hestia.conf
@@ -59,6 +60,11 @@ if [ "$chain" == "ALL" ]; then
 		sed -i "/IP='$sip' CHAIN='$CHAIN'/d" $conf
 		b=$($iptables -L fail2ban-$CHAIN --line-number -n | grep -w $ipv4_cidr | awk '{print $1}')
 		$iptables -D fail2ban-$CHAIN $b 2> /dev/null
+		for f2bjail in $("$f2bcli" status | grep -i 'Jail list:' | cut -d':' -f2- | sed -E 's/\s*//g' | tr ',' ' '); do
+			if "$f2bcli" get "$f2bjail" actions | grep -Eq "^hestia-$CHAIN" 2> /dev/null; then
+				"$f2bcli" set "$f2bjail" unbanip "$sip" &> /dev/null
+			fi
+		done
 	done
 else
 	# Checking ip in banlist
@@ -72,6 +78,12 @@ else
 	sed -i "/IP='$sip' CHAIN='$chain'/d" $conf
 	b=$($iptables -L fail2ban-$chain --line-number -n | grep -w $ipv4_cidr | awk '{print $1}')
 	$iptables -D fail2ban-$chain $b 2> /dev/null
+	for f2bjail in $("$f2bcli" status | grep -i 'Jail list:' | cut -d':' -f2- | sed -E 's/\s*//g' | tr ',' ' '); do
+		if "$f2bcli" get "$f2bjail" actions | grep -Eq "^hestia-$chain" 2> /dev/null; then
+			"$f2bcli" set "$f2bjail" unbanip "$sip" &> /dev/null
+		fi
+	done
+
 fi
 
 # Changing permissions

+ 0 - 0
bin/v-delete-user-backup-restic