#!/bin/sh

#################################################################################################
#       For starting and stopping a firewall with iptables					#
#												#					
#	(Tested on SuSE 7.1/ 7.2)								#
#												#
#	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.config
. /etc/rc.status

 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`
     
     if [ ! -d /etc/firewall/ ]; then
	echo ""
	echo "This script expects the firewall-startfile in the directory '/etc/firewall'!"
	echo "Please do a 'mkdir /etc/firewall' and copy your iptables-script into that dir."
	echo ""
	exit
     fi
 
     if [ -f /usr/sbin/iptables ]; then
	iptables -V > /tmp/version
      else
	echo "iptables not installed. Please install in /usr/sbin/ !"
      exit 
     fi
 
 version=`cat /tmp/version`
 
  case "$1" in
 
   start)
      if [ -f /var/lock/subsys/firewall ]; then
	echo "$prog is already running"
      else
	echo -n $"Starting $prog ($version)"
	/etc/firewall/$yourhost.fw 	# <--- adapt this to the name of your script
		
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/firewall
	rc_status -v
	rm /tmp/version
     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
	rm /tmp/version
      else
	echo "$prog is not running"
     fi
	;;
 
   restart|reload)
	$0 stop && $0 start
	rc_status
	;;
 
   status)
     if [ -f /var/lock/subsys/firewall ]; then
        echo $"$prog ($version) is running..."
	RETVAL=0
      else
        echo $"$prog is stopped"
    RETVAL=1
     fi
	;;

   *)
	echo $"Usage: $0 {start|stop|restart|status}"
     exit 1
   esac
 rc_exit
exit $RETVAL
