#!/bin/sh
# $Id$
#

#
# This is an example script for filtering overly aggressive SMTP
# clients using Linux's iptables packet filter.  To use this script,
# you must first add a chain called bad_smtp_clients by running the
# following two commands:
#
#     iptables -N bad_smtp_clients
#     iptables -A INPUT -p tcp --dport 25 --syn -j bad_smtp_clients
#
# (If your system does not save iptable rules automatically, you
# should ensure these commands are run automatically whenever your
# system reboots.)
# 
# Once you have configured iptables to have a bad_smtp_clients chain,
# simply copy this script to /etc/avenger/smtp-filter, and add the
# following line to /etc/avenger/asmtpd.conf:
#
#     SMTPFilter /etc/avenger/smtp-filter
#

#
# To port this script to another OS, replace the bodies of the
# filter_add, filter_del, and filter_clear functions with appropriate
# commands to add to, delete from, and clear the list of filtered
# client IP addresses.  See the asmtpd.conf(5) man page for more
# information.
#

IPTABLES=/sbin/iptables
CHAIN=bad_smtp_clients

filter_add () {
    exec $IPTABLES -A $CHAIN -s "$1" -j DROP
}

filter_del () {
    exec $IPTABLES -D $CHAIN -s "$1" -j DROP
}

filter_clear () {
    exec $IPTABLES -F $CHAIN
}


usage () {
    echo "usage: $0 add <ip-addr>" >&2
    echo "       $0 del <ip-addr>" >&2
    echo "       $0 clear" >&2
    exit 2
}

case $1 in
    add)
	test "$#" -ne 2 && usage
	filter_add "$2"
	;;
    del)
	test "$#" -ne 2 && usage
	filter_del "$2"
	;;
    clear)
	test "$#" -ne 1 && usage
	filter_clear
	;;
    *)
	usage
	;;
esac
