#!/bin/bash
#
# $Id$
#
# /etc/init.d/sfw6
#
# starts/stops/statuses sfw6, IBCL's Simple FireWall for IPv6
#
#
# hints for update-rc.d on Debian like distros and chkconfig on SuSE like distros
#
### BEGIN INIT INFO
# Provides:          sfw6
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Should-Start:      $network iptables ipmasq arno-iptables-firewall iptables-persistent ferm fail2ban
# Should-Stop:       $network iptables ipmasq arno-iptables-firewall iptables-persistent ferm fail2ban
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start or stop the Simple IPv6 FireWall sfw6.
# Description:       Start or stop the Simple IPv6 FireWall sfw6.
### END INIT INFO
#
#
# hints for chkconfig on RedHat like distros
#
# chkconfig: 345 90 10
# description: Start or stop the Simple IPv6 FireWall sfw6.
# processname: 
#

# Defaults

START_SFW="no"
ALLOWED_TCPPORTS=""
ALLOWED_UDPPORTS=""
SWALLOWED_TCPPORTS=""
SWALLOWED_UDPPORTS=""
LOG_LIMIT="1/s"
LOG_DROPS="no"
VERBOSITY=0

SFWCONF="/etc/sfw6/sfw6.conf"

. /usr/lib/sfw6/parse_sfw6_conf.sh

if [ -z "$VERBOSITY" ] ; then
  VERBOSITY=0
fi

if [ "$2" = "-v" ] ; then
  VERBOSITY=$((VERBOSITY + 1))
fi

export IPTOPTIONS=" -w "
if [ -f /etc/debian_version ] ; then
  OLDDEB=$(egrep "^[2-7]" /etc/debian_version)
  if [ -n "$OLDDEB" ] ; then
    echo "Detected old debian without support for -w, clearing IPTOPTIONS and sleeping a bit"
    IPTOPTIONS=""
    sleep 3
  fi
fi
#echo "DEBUG: IPTOPTIONS=${IPTOPTIONS}"

case "$1" in

    start)	echo "Starting SFW6 (Simple FireWall for IPv6)."

		DO_START_SFW=$(echo "$START_SFW" |egrep -i '^(yes|true)$')
		if [ -z "$DO_START_SFW" ] ; then
		  echo "WARNING: START_SFW set to '$START_SFW' rather than yes or true, quitting."
		  exit 0
		fi

		#echo "1" > /proc/sys/net/ipv4/ip_forward

		# - clear existing tables
		SFWININPUT=$(/sbin/ip6tables ${IPTOPTIONS} -L INPUT -n 2>/dev/null |egrep "^sfw6 ")
		if [ -n "$SFWININPUT" ] ; then
		  /sbin/ip6tables ${IPTOPTIONS} -D INPUT -j sfw6
		  /sbin/ip6tables ${IPTOPTIONS} -F sfw6
		  /sbin/ip6tables ${IPTOPTIONS} -X sfw6
		fi

		/sbin/ip6tables ${IPTOPTIONS} -N sfw6
		/sbin/ip6tables ${IPTOPTIONS} -A INPUT -j sfw6

		# check if we are allowed to load modules
		LOADEDMODULES=$(cat /proc/modules 2>/dev/null |grep -iv "Modules")
		NRMODULES=$(( $(echo "$LOADEDMODULES" |wc -l |tr -d ' ') - 1 ))
		test -z $NRMODULES && NRMODULES="0"
		if [ $NRMODULES -gt 0 ] ; then
		  test $VERBOSITY -gt 0 && echo "$NRMODULES modules loaded, runnig module checks."
		  # module ip6_tables (formerly named ipt_state)
		  if echo "$LOADEDMODULES" |egrep "ip6_tables" >/dev/null ; then
		    test $VERBOSITY -gt 0 && echo "ip6_tables module already loaded."
		  else
		    test $VERBOSITY -gt 0 && echo "ip6_tables module not loaded, calling modprobe."
		    modprobe ip6_tables
		  fi
		  #if echo "$LOADEDMODULES" |egrep "nf_conntrack_ipv6" >/dev/null ; then
		  #  test $VERBOSITY -gt 0 && echo "nf_conntrack_ipv6 module already loaded."
		  #else
		  #  test $VERBOSITY -gt 0 && echo "nf_conntrack_ipv6 module not loaded, calling modprobe."
		  #  modprobe nf_conntrack_ipv6
		  #fi
		  # the future might see a configurable list of modules to check for
		  #if echo "$LOADEDMODULES" |egrep "gibtsnet" >/dev/null ; then
		  #  test $VERBOSITY -gt 0 && echo "gibtsnet module already loaded."
		  #else
		  #  test $VERBOSITY -gt 0 && echo "gibtsnet module not loaded, calling modprobe."
		  #  modprobe gibtsnet
		  #fi
		else
		  test $VERBOSITY -gt 0 && echo "No modules loaded, probably running in container. Skipping module checks."
		fi
		# due to the large number of kernel level container technologies neither the lack of /proc/modules content
		# nor the inability to modprobe a module really proof a module problem.
		# therefore we leave failure and exiting to the -A statements below and in called scripts

		# block anything with RH0 headers, see http://www.sixxs.net/wiki/IPv6_Firewalling
		test $VERBOSITY -gt 0 && echo "Blocking RT0 packets"
		/sbin/ip6tables ${IPTOPTIONS} -A sfw6 -m rt --rt-type 0 -j DROP
		#test $VERBOSITY -gt 0 && echo "Blocking RT0 in FORWARD"
		#/sbin/ip6tables ${IPTOPTIONS} -A FORWARD -m rt --rt-type 0 -j DROP
		#test $VERBOSITY -gt 0 && echo "Blocking RT0 in OUTPUT"
		#/sbin/ip6tables ${IPTOPTIONS} -A OUTPUT -m rt --rt-type 0 -j DROP
		
		# - allowed_input

		# DROP configured BLACKLISTED_HOSTS
		if [ -n "$BLACKLISTED_HOSTS" ] ; then
		  for HOST in $BLACKLISTED_HOSTS ; do
		    test $VERBOSITY -gt 0 && echo "Adding DROP for blacklisted Host/Network $HOST"
		    /sbin/ip6tables ${IPTOPTIONS} -A sfw6 -s $HOST -j DROP || exit 11
 		  done
		fi

		# SPECIAL_RULES
		if [ -x "/etc/sfw6/special_rules.sh" ] ; then
		  test $VERBOSITY -gt 0 && echo "Calling /etc/sfw6/special_rules.sh"
		  /etc/sfw6/special_rules.sh $* || exit 21
		else
		  test $VERBOSITY -gt 0 && echo "No executable /etc/sfw6/special_rules.sh to call."
		fi

		# ACCEPT existing connections
		test $VERBOSITY -gt 1 && echo "ACCEPTing established connections"
 		/sbin/ip6tables ${IPTOPTIONS} -A sfw6 -m state --state ESTABLISHED,RELATED -j ACCEPT || echo "FAILED to ACCEPT established connections (modules missing?), continuing anyway"

		# ACCEPT myself
		test $VERBOSITY -gt 1 && echo "ACCEPTing -i lo"
		/sbin/ip6tables ${IPTOPTIONS} -A sfw6 -i lo -j ACCEPT || exit 22
		test $VERBOSITY -gt 1 && echo "ACCEPTing ::1"
		/sbin/ip6tables ${IPTOPTIONS} -A sfw6 -s ::1 -j ACCEPT || exit 23

		# ACCEPT configured TRUSTED_HOSTS
		if [ -n "$TRUSTED_HOSTS" ] ; then
		  for HOST in $TRUSTED_HOSTS ; do
		    test $VERBOSITY -gt 0 && echo "Adding ACCEPT for Host/Network $HOST"
		    /sbin/ip6tables ${IPTOPTIONS} -A sfw6 -s $HOST -j ACCEPT || exit 31
 		  done
		fi

		# ACCEPT configured ALLOWED_TCPPORTS
		if [ -n "$ALLOWED_TCPPORTS" ] ; then
		  for PORT in $ALLOWED_TCPPORTS ; do
		    test $VERBOSITY -gt 0 && echo "Adding ACCEPT for new connections to TCP Port $PORT"
		    /sbin/ip6tables ${IPTOPTIONS} -A sfw6 -p tcp --destination-port $PORT -j ACCEPT || exit 32
 		  done
		fi

		# ACCEPT configured ALLOWED_UDPPORTS
		if [ -n "$ALLOWED_UDPPORTS" ] ; then
		  for PORT in $ALLOWED_UDPPORTS ; do
		    test $VERBOSITY -gt 0 && echo "Adding ACCEPT for new connections to UDP Port $PORT"
		    /sbin/ip6tables ${IPTOPTIONS} -A sfw6 -p udp --destination-port $PORT -j ACCEPT || exit 33
 		  done
		fi

		# SWALLOW configured SWALLOWED_TCPPORTS
		if [ -n "$SWALLOWED_TCPPORTS" ] ; then
		  for PORT in $SWALLOWED_TCPPORTS ; do
		    test $VERBOSITY -gt 0 && echo "Adding swallowing of new connections to TCP Port $PORT"
		    /sbin/ip6tables ${IPTOPTIONS} -A sfw6 -p tcp --destination-port $PORT -j DROP || exit 34
 		  done
		fi

		# SWALLOW configured SWALLOWED_UDPPORTS
		if [ -n "$SWALLOWED_UDPPORTS" ] ; then
		  for PORT in $SWALLOWED_UDPPORTS ; do
		    test $VERBOSITY -gt 0 && echo "Adding swallowing of new connections to UDP Port $PORT"
		    /sbin/ip6tables ${IPTOPTIONS} -A sfw6 -p udp --destination-port $PORT -j DROP || exit 35
 		  done
		fi

		# ACCEPT some pings
		test $VERBOSITY -gt 0 && echo "Adding ACCEPT for ICMPv6."
		#ICMPTYPES=""
 		## RFC 4890
		#ICMPTYPES="$ICMPTYPES 1 2 3/0 3/1 4/0 4/1 4/2 128 129 130 131 132 133 141 142 143 148 149 151 152 153"
 		## Mobile prefix discovery
		#ICMPTYPES="$ICMPTYPES 144 145 146 147"
		#for ICMPTYPE in $ICMPTYPES
		#do
		#  /sbin/ip6tables ${IPTOPTIONS} -A sfw6 -p icmpv6 --icmpv6-type $ICMPTYPE -j ACCEPT || exit 51
		#done
		/sbin/ip6tables ${IPTOPTIONS} -A sfw6 -p icmpv6 -j ACCEPT || exit 51

		# - block rest_of_input

		DO_LOG_DROPS=$(echo "$LOG_DROPS" |egrep -i '^(yes|true)$')
		if [ -z "$DO_LOG_DROPS" ] ; then
		  test $VERBOSITY -gt 0 && echo "Not adding LOGing of rest, because LOG_DROPS is not set to 'yes' or 'true'"
       		else
		  test $VERBOSITY -gt 0 && echo "Adding LOGing of rest, with --limit $LOG_LIMIT"
		  /sbin/ip6tables ${IPTOPTIONS} -A sfw6 -m limit --limit $LOG_LIMIT -j LOG --log-prefix "DROPping unknown packet: "  || echo "FAILED to activate logging the rest."
       		fi

		test $VERBOSITY -gt 0 && echo "DROPping rest"
		/sbin/ip6tables ${IPTOPTIONS} -A sfw6 -j DROP || exit 53

		echo "Done Starting SFW6 (Simple FireWall for IPv6)."
		;;

    stop)	echo "Stopping SFW6 (Simple FireWall for IPv6)."
                
		SFWININPUT=$(/sbin/ip6tables ${IPTOPTIONS} -L INPUT -n 2>/dev/null |egrep "^sfw6 ")
		if [ -n "$SFWININPUT" ] ; then
		  /sbin/ip6tables ${IPTOPTIONS} -D INPUT -j sfw6
		  /sbin/ip6tables ${IPTOPTIONS} -F sfw6
		  /sbin/ip6tables ${IPTOPTIONS} -X sfw6
		fi

        	;;

    restart|reload)
		$0 stop $2 && $0 start $2
		;;

    status)	echo "Statussing SFW6 (Simple FireWall for IPv6)."
		/sbin/ip6tables ${IPTOPTIONS} -L -n
		;;

    configure) 
		# it's ok to do nothing here
		;;

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

esac

