#!/bin/sh
#set -x

#------------------------------------------------------------------------------
# tmp files.
#------------------------------------------------------------------------------
IFCONFIG=/tmp/setup_firewall-ifconfig.$$
ROUTE=/tmp/setup_firewall-route.$$
TMPROUTE=/tmp/setup_firewall-route.tmp.$$

#------------------------------------------------------------------------------
# save ifconfig and route-n output for later reuse
#------------------------------------------------------------------------------
/sbin/ifconfig > $IFCONFIG
/sbin/route -n > $ROUTE

#------------------------------------------------------------------------------
# All interfaces without aliases and without lo
#------------------------------------------------------------------------------
INTERFACES=`cat $IFCONFIG|grep Link|grep -v inet6|awk '{ print $1; }'|grep -v lo|grep -v :`  # EXPORTED

#------------------------------------------------------------------------------
# EXPORTED: external interface name
#------------------------------------------------------------------------------
INTF_EXTERNAL=`grep ^0.0.0.0 $ROUTE | head -1 | awk '{ print $8; }'`			# EXPORTED
if [ -z "$INTF_EXTERNAL" ]
then
	INTF_EXTERNAL="none"
fi
IP_INTF_EXTERNAL=`/sbin/ifconfig $INTF_EXTERNAL|grep addr:|sed -e s/.*addr:// -e 's/ .*//'`
NET_EXTERNAL=`grep $INTF_EXTERNAL $ROUTE | grep -v ^0.0.0.0 | awk '{ printf("%s/%s\n", $1, $3); }'| sort -u`	# EXPORTED


#------------------------------------------------------------------------------
# All interfaces without aliases and without lo
#------------------------------------------------------------------------------
if [ "$HAVE_DMZ" = "yes" ]
then
	INTF_INTERNAL=`cat $IFCONFIG|grep Link|grep -v inet6|awk '{ print $1 }'|grep -v lo|grep -v :| grep -v ipsec | grep -v $INTF_EXTERNAL | grep -v $INTF
_DMZ`
else
	INTF_INTERNAL=`cat $IFCONFIG|grep Link|grep -v inet6|awk '{ print $1 }'|grep -v lo|grep -v :| grep -v ipsec | grep -v $INTF_EXTERNAL`
fi


#------------------------------------------------------------------------------
# Create list of the ip addresses of all internal interfaces
#------------------------------------------------------------------------------
TMP=""
for intf in $INTF_INTERNAL
do
	TMP=`/sbin/ifconfig $intf|grep addr:|sed -e s/.*addr:// -e 's/ .*//'`
	IP_INTF_INTERNAL="$TMP $IP_INTF_INTERNAL"						# EXPORTED
done

#------------------------------------------------------------------------------
# Create list of all networks on all the internal interfaces
#------------------------------------------------------------------------------
TMP=""
for intf in $INTF_INTERNAL ipsec0
do
	TMP=`grep $intf $ROUTE | awk '{ printf("%s/%s\n", $1, $3); }'| sort -u`
	for net in $TMP
	do
		if [ "$net" = "$NET_EXTERNAL" ]
		then
			continue
    		fi
    		NET_INTERNAL="$net $NET_INTERNAL"
  	done
done

#------------------------------------------------------------------------------
# Remove our tmp file.
#------------------------------------------------------------------------------
rm -f $ROUTE $IFCONFIG

#------------------------------------------------------------------------------
# Display information.
#------------------------------------------------------------------------------
if [ "$INFOONLY" = "1" -o "$VERBOSE" = "1" ]
then
	# report findings
	echo All interfaces: $INTERFACES

	echo INTF_INTERNAL=$INTF_INTERNAL
	echo IP_INTF_INTERNAL=$IP_INTF_INTERNAL
	echo NET_INTERNAL=$NET_INTERNAL

	echo INTF_EXTERNAL=$INTF_EXTERNAL
	echo IP_INTF_EXTERNAL=$IP_INTF_EXTERNAL
	echo NET_EXTERNAL=$NET_EXTERNAL

	echo HAVE_DMZ=$HAVE_DMZ
	if [ "$HAVE_DMZ" = "yes" ]
	then
		echo INTF_DMZ=$INTF_DMZ
		echo IP_INTF_DMZ=$IP_INTF_DMZ
		echo NET_DMZ=$NET_DMZ
	fi
	echo IP_ALIASES=$IP_ALIASES
fi

