#!/bin/bash

# Iptables initialization script.
# Copyright (c) Belgarat@mail.klfree.net
# Use is subject to Klfree Debian License terms
#
# This script just invokes the iptables management
# script, that provides translations for individual
# network devices

### BEGIN INIT INFO
# Provides:          iptables
# Required-Start:    $localfs
# Required-Stop:     
# Should-Start:      
# Should-Stop:       
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Sets up iptables
# Description:       Loads definitions of iptables. Loads it before the
#		     interfaces are activated, so there's no window
#		     when an attack can be made.
### END INIT INFO

start_iptables() {
    /usr/local/sbin/setup-iptables start
}

stop_iptables() {
    for t in nat filter mangle ; do
	iptables -t $t -F
	iptables -t $t -X
    done
    if [ -x "/usr/sbin/ipset" ] ; then
	ipset -X
    fi
}


case $1 in
    start | create)
    start_iptables
    ;;
    
    stop)
    stop_iptables || true
    ;;

    restart)
    stop_iptables
    start_iptables
    ;;
        
    *)
    echo "Usage: iptables (start|stop|restart)"
    echo ""
    echo "start - loads default iptable rules and all services"
    echo "stop - flushes all tables"
    echo "restart - flushes and then restores iptables"
    exit 1
    ;;
esac
