#!/bin/sh
#
#     Shorewall Packet Filtering Firewall Control Program - V1.0 - 4/15/2001
#
#     This program is under GPL [http://www.gnu.org/copyleft/gpl.htm]         
#
#     (c) 1999,2000,2001 - Tom Eastep (teastep@evergo.net)
#
#
#       This file should be placed in /sbin/seawall.
#
#       Shorewall documentation is available at http://shorewall.sourceforge.net
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of Version 2 of the GNU General Public License 
#       as published by the Free Software Foundation.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
#       
#       If your external interface uses a dynamic IP or if you have local 
#       interfaces, you must have the interface(s) started before you start 
#       the firewall.
#
#       If an error occurs while starting or restarting the firewall, the
#       firewall is automatically stopped.
#
#       The firewall uses a configuration file /etc/shorewall.conf - a sample
#       file is included with the firewall.
#
#       Commands are:
#
#          shorewall start                         Starts the firewall 
#          shorewall restart                       Restarts the firewall
#          shorewall stop                          Stops the firewall
#          shorewall monitor [ refresh-interval ]  Repeatedly Displays firewall status
#                                                  plus the last 5 "interesting"
#                                                  packets (requires that you
#                                                  set log="Yes" in your
#                                                  configuration file).
#          shorewall status                        Displays firewall status              
#          shorewall reset                         Resets iptables packet and
#                                                  byte counts
#          shorewall clear                         Open the floodgates by
#                                                  removing all iptables rules
#                                                  and setting the three permanent
#                                                  chain policies to ACCEPT
#

#
# Display a chain if it exists
#
showfirstchain() # $1 = name of chain
{
    awk 'BEGIN {prnt=0;}; /^$/ { next; };/^Chain/ {if ( prnt == 1 ) exit; };/Chain '$1'/{ prnt=1; }; { if (prnt == 1)  print; }' /tmp/chains-$$
}

showchain() # $1 = name of chain
{
    if [ "$firstchain" = "Yes" ]; then
	showfirstchain $1
	firstchain=
    else
	awk 'BEGIN {prnt=0;}; /^$|^ pkts/ { next; };/^Chain/ {if ( prnt == 1 ) exit; };/Chain '$1'/{ prnt=1; }; { if (prnt == 1)  print; }' /tmp/chains-$$
    fi
}
#################################################################################
# Display IPTABLES rules -- we used to store them in a variable but ash         #
# dies when trying to display large sets of rules                               #
#################################################################################
display_chains()
{
    if [ "$haveawk" = "Yes" ]; then
	#
	# Send the output to a temporary file since ash craps if we try to store
	# the output in a variable.
	#
	iptables -L -n -v > /tmp/chains-$$

	clear
	echo -e "$banner `date`\\n"
        echo -e "Common Chains\\n"
	firstchain="Yes"
	showchain INPUT
	showchain OUTPUT
	showchain FORWARD
	showchain common
	showchain icmp-def
	showchain rfc1918
	
	timed_read

	for zone in $zones; do
	    if [ -n "`mygrep "^Chain ${zone}2fw" /tmp/chains-$$`" ] ; then
		clear
		echo -e "$banner `date`\\n"
		firstchain=Yes
		eval display=\$${zone}_display
		echo -e "$display Chains\\n"
		for zone1 in fw $zones; do
		    showchain ${zone}2$zone1
		    [ "$zone" != "$zone1" ] && showchain ${zone1}2${zone}
		done
		
		timed_read
	    fi
	done

	qt rm -f /tmp/chains-$$
    else
	iptables -L -n -v
	timed_read
    fi
}
#################################################################################
# Delay $timeout seconds -- if we're running on a recent bash2 then allow       # 
# <enter> to terminate the delay                                                #
#################################################################################
timed_read () 
{
    read -t $timeout foo 2> /dev/null

    test $? -eq 2 && sleep $timeout
}
#################################################################################
# Display the last 20 packets logged                                            #
#################################################################################
packet_log()
{
    if [ -n "$realtail" ]; then
	grep "Shorewall:" /var/log/messages | sed s/" $host kernel: Shorewall:"/" "/ | tail -n20
    else
	grep "Shorewall:" /var/log/messages | sed s/" $host kernel: Shorewall:"/" "/ | tail 
    fi     
}
#################################################################################
# Monitor the Firewall                                                          #
#################################################################################
monitor_firewall() # $1 = timeout -- if negative, prompt each time that
                   #                 an 'interesting' packet count changes
{

    host=`echo $HOSTNAME | sed 's/\..*$//'`
    oldrejects=`iptables -L -v -n | grep 'LOG'`
    
    if [ $1 -lt 0 ]; then
	let "timeout=- $1"
	pause="Yes"
    else
	pause="No"
	timeout=$1
    fi

    qt which awk && { haveawk=Yes; determine_zones; } || haveawk=

    while true; do
	display_chains
	
	clear
	echo -e "$banner `date`\\n"

	echo -e "Denied/Rejected Packet Log\\n"

	rejects=`iptables -L -v -n | grep 'LOG'`

	if [ "$rejects" != "$oldrejects" ]; then
	    oldrejects="$rejects"
	    echo -e '\a'
	    packet_log
	    
	    if [ "$pause" = "Yes" ]; then
		echo -en '\nEnter any character to continue: '
		read foo
	    else
		timed_read 
	    fi
	else
	    if [ "$pause" != "Yes" ]; then
		echo
		packet_log
	    fi
		
	    timed_read
	fi
	
	clear
	echo -e "$banner `date`\\n"

	echo -e "NAT Status\\n"
	iptables -t nat -L -n -v
	echo -e "\\nCurrent Tracked Connections\\n"
	cat /proc/net/ip_conntrack
		
	timed_read
    done
}
#################################################################################
# Give Usage Information							#
#################################################################################
usage() # $1 = exit status
{
    echo "Usage: $0 {log|show <chain>|start|stop|reset|restart|status|clear|monitor [refresh interval]}"
    exit $1
}
#################################################################################
# Execution begins here								#
#################################################################################
if [ $# -gt 0 ] && [ "$1" = "debug" ]; then
    debugging=debug
    shift
fi

if [ $# -eq 0 ] || [ $# -gt 2 ]; then
    usage 1
fi

if [ ! -f /etc/shorewall/firewall ]; then
    echo "ERROR: Shorewall is not properly installed"
    if [ -L /etc/shorewall/firewall ]; then
	echo "       /etc/shorewall/firewall is a symbolic link to a"
	echo "       non-existant file"
    else
	echo "       The file /etc/shorewall/firewall does not exist"
    fi
    
    exit 2    
fi

[ -f /etc/shorewall/functions ] && . /etc/shorewall/functions || { echo "/etc/shorewall/functions does not exist!" ; exit 2; }

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin

if [ -f /etc/shorewall/version ]; then
    version=`cat /etc/shorewall/version`
else
    echo "ERROR: Shoreline Firewall is not properly installed"
    echo "       The file /etc/shorewall/version does not exist"
    exit 1
fi
    
banner="Shorewall-$version Status at $HOSTNAME -"
#
# See if we have a real version of "tail" -- use separate redirection so 
# that ash (aka /bin/sh on LRP doesn't crap 
#
if ( tail -n5 /var/log/messages > /dev/null 2> /dev/null ) ; then
    realtail="Yes"
else
    realtail=""
fi

case "$1" in
    start|stop|restart|reset|clear)
	[ $# -eq 2 ] && usage 1
	exec /etc/shorewall/firewall $debugging $1
	;;
    show)
	if [ "$2" = "nat" ]; then
	    echo -e "Shorewall-$version NAT at $HOSTNAME - `date`\\n"
	    iptables -t nat -L -n -v
	elif [ "$2" = "log" ]; then
	    echo -e "Shorewall-$version Log at $HOSTNAME - `date`\\n"
	    host=`echo $HOSTNAME | sed 's/\..*$//'`
	    packet_log
        else
	    echo -e "Shorewall-$version Chain $2 at $HOSTNAME - `date`\\n"
	    iptables -L $2 -n -v
        fi
	;;
    monitor)
	if [ $# -eq 2 ]; then
	    monitor_firewall $2
	else
	    monitor_firewall 30
	fi
        ;;
    status)
	clear
	echo -e "Shorewall-$version Status at $HOSTNAME - `date`\\n"
	host=`echo $HOSTNAME | sed 's/\..*$//'`
	iptables -L -n -v
	;;
    *)
	usage 1
	;;
esac 












