#!/bin/sh

#################################################################################################
#       For starting and stopping a firewall with iptables					#
#												#					
#	(Tested on SuSE 8.1, should work on 8.0 too)						#
#												#
#	Andreas Hochsteger <e9625392@student.tuwien.ac.at>					#
#	based on the firewall scripts of Olaf.Zenker@t-systems.de				#
#################################################################################################

### BEGIN INIT INFO
# Provides: firewall
# Required-Start: $network
# Required-Stop:  
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    Start Firewall
### END INIT INFO

. /etc/rc.status
. /etc/sysconfig/firewall

base=${0##*/}
link=${base#*[SK][0-9][0-9]}

test $link = $base && START_FIREWALL=yes
test "$START_FIREWALL" = yes || exit 0

return=$rc_done
PATH="/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin"
prog="Firewall"
kernel=`uname -r`     
yourhost=`uname -n`
fw_script=/etc/firewall/$yourhost.fw	# <--- adapt this to the name of your script

if [ ! -f $fw_script ]; then
	echo ""
	echo "This script could not find the iptables-script '$fw_script'!"
	echo "Please do a 'mkdir /etc/firewall' and copy your iptables-script into that dir."
	echo ""
	exit 1
fi

if [ ! -f /usr/sbin/iptables ]; then
	echo "iptables not installed. Please install in /usr/sbin/ !"
	exit 1
fi

version=`iptables -V`

case "$1" in
    start)
	if [ -f /var/lock/subsys/firewall ]; then
	    echo "$prog is already running"
	else
	    echo -n $"Starting $prog ($version)"
	    $fw_script
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/firewall
	    rc_status -v
	fi
	;;
    stop)
	if [ -f /var/lock/subsys/firewall ]; then
	    echo -n $"Stopping $prog"
	    iptables -L -n | awk '/Chain/ {printf "iptables -F %s\n",$2;}'|/bin/sh && \
	    iptables -X
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/firewall
	    rc_status -v
	else
	    echo "$prog is not running"
	fi
	;;
    restart)
	$0 stop && $0 start
	rc_status
	;;
    reload)
	if [ -f /var/lock/subsys/firewall ]; then
	    echo -n $"Reloading firewall rules"
	    $fw_script && rc_failed 0 || rc_failed 2
	    rc_status -v
	else
	    echo "$prog is not running"
	fi
	;;
    status)
	echo -n "Checking for $prog ($version)"
	if [ -f /var/lock/subsys/firewall ]; then
	    rc_failed 0
	else
	    rc_failed 3
	fi
	rc_status -v
	;;
    *)
	echo $"Usage: $0 {start|stop|restart|reload|status}"
	exit 1
esac

rc_exit
