#!/bin/sh

# Copyright (C) 2006 Daniel O'Connor <darius@dons.net.au>. All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# Use in conjunction with a pf.conf like so..
# table <abusive> persist
# ...
# block in quick from <abusive>
# ...
# pass in on $ext_if proto tcp from any to ($ext_if) \
#   port $tcp_services flags S/SA keep state \
#   (max-src-conn-rate 10/10, overload <abusive> flush global)
# ...

PFCTL=/sbin/pfctl

if [ $# -ne 2 ]; then
    echo "Bad usage" 1>2
    echo "	$0 table expirytime"
    echo " expirytime is in seconds"
    exit 1
fi

table=$1
expiry=$2
now=`date +%s`

parsepf () {
    $PFCTL -v -t $table -T show 2>&1 | (
	while [ 1 ]; do
	    oldIFS=$IFS
	    IFS=""
	    read line
	    rtn=$?
	    IFS=$oldIFS
	    if [ $rtn -ne 0 ]; then
		break;
	    fi

	    if [ -z "$ip" ]; then
		ip=`echo "$line" | sed -nEe 's/^[[:space:]]+([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)/\1/p'`
		continue
	    else
		datestr=`echo "$line" | sed -nEe 's/^[[:space:]]+Cleared:[[:space:]]+(.*)/\1/p'`
		if [ -z "$datestr" ]; then
		    continue
		fi
	    fi

	    date=`date -j -f %c "$datestr" +%s`
	    if [ $? -ne 0 ]; then
		echo "Unable to parse date '$datestr'"
		exit 1
	    fi

	    if [ $(($date + $expiry)) -lt $now ]; then
		echo -n "exp_$ip "
	    else
		echo -n "fresh_$ip "
	    fi
	    unset ip
	done
    )
}

iplist=`parsepf`
expirylist=""
freshlist=""
for ip in $iplist; do
    if [ "${ip##exp_}" != "${ip}" ]; then
	expirylist="$expirylist ${ip##exp_}"
    else
	freshlist="$freshlist ${ip##fresh_}"
    fi
done
explen=$((`echo $expirylist | wc -w`))
freshlen=$((`echo $freshlist | wc -w`))

(
    for ip in $expirylist; do
        echo $ip
    done
) | $PFCTL -t $table -T del -f - -q

echo "Expired $explen, kept $freshlen IPs in $table"

