Skip to content

Commit

Permalink
- Fixed references to chain being removed from INPUT and FORWARD whe…
Browse files Browse the repository at this point in the history
…n updating.

 - Now only attempt to remove references to chain from INPUT and FORWARD if they exist.
 - Silenced some (expected) `stderr` messages from `iptables`
  • Loading branch information
wallyhall committed Aug 15, 2021
1 parent 5c0dcee commit 32835de
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions spamhaus-drop
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,22 @@ set_mode() {
MODE="$1"
}

delete_chain_reference() {
$IPTABLES -L "$1" | tail -n +3 | grep -e "^$CHAIN " > /dev/null && $IPTABLES -D "$1" -j "$CHAIN"
}

delete_chain() {
if $IPTABLES -D INPUT -j "$CHAIN" && $IPTABLES -D FORWARD -j "$CHAIN" && $IPTABLES -F "$CHAIN" && $IPTABLES -X "$CHAIN"
then
echo "'$CHAIN' chain removed from iptables."
else
echo "'$CHAIN' chain NOT removed, please report this issue to https://github.com/wallyhall/spamhaus-drop/"
fi
if $IPTABLES -L "$CHAIN" -n &> /dev/null; then
delete_chain_reference INPUT
delete_chain_reference FORWARD
if $IPTABLES -F "$CHAIN" && $IPTABLES -X "$CHAIN"; then
echo "'$CHAIN' chain removed from iptables."
else
echo "'$CHAIN' chain NOT removed, please report this issue to https://github.com/wallyhall/spamhaus-drop/"
fi
else
echo "'$CHAIN' does not exist, nothing to delete."
fi
}

download_rules() {
Expand Down Expand Up @@ -103,10 +112,8 @@ update_iptables() {
fi

# check to see if the chain already exists
if $IPTABLES -L "$CHAIN" -n; then
if $IPTABLES -L "$CHAIN" -n &> /dev/null; then
# flush the old rules
$IPTABLES -D INPUT -j "$CHAIN"
$IPTABLES -D FORWARD -j "$CHAIN"
$IPTABLES -F "$CHAIN"

echo "Flushed old rules. Applying updated Spamhaus list...."
Expand Down

1 comment on commit 32835de

@enoch85
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Now it works as expected. :D

Please sign in to comment.