#!/bin/bash
#Copyright 2003 William Stearns <wstearns@pobox.com>
#Released under the GPL.

Me='pasvmap'
MyVersion='0.3.2'

[ -r /etc/firebricks/firebricks.conf ] &&			. /etc/firebricks/firebricks.conf
[ -r /etc/firebricks/$Me.conf ] &&				. /etc/firebricks/$Me.conf
[ -r ${FBLibDir:-'/usr/lib/firebricks/'}/firebrickslib ] &&	. ${FBLibDir:-'/usr/lib/firebricks/'}/firebrickslib
if [ -z "$FBLibVer" ]; then
	echo 'It looks like firebrickslib was not loaded, why?  Exiting' >&2
	exit 1
fi

for OneTask in $Tasks ; do
	case "$OneTask" in
	link)
		$IptablesBin -N $Me >/dev/null 2>&1
		$IptablesBin $AppIn INPUT -i \! lo -p tcp --tcp-flags SYN,ACK,FIN,RST SYN,ACK -m state --state established	-j $Me
		$IptablesBin $AppIn FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST SYN,ACK -m state --state established		-j $Me
		$IptablesBin $AppIn OUTPUT -p tcp --tcp-flags SYN,ACK,FIN,RST SYN,ACK -m state --state established		-j $Me
		;;
	unlink)
		$IptablesBin -D INPUT -i \! lo -p tcp --tcp-flags SYN,ACK,FIN,RST SYN,ACK -m state --state established		-j $Me
		$IptablesBin -D FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST SYN,ACK -m state --state established			-j $Me
		$IptablesBin -D OUTPUT -p tcp --tcp-flags SYN,ACK,FIN,RST SYN,ACK -m state --state established			-j $Me
		$IptablesBin -X $Me >/dev/null 2>&1
		;;
	create)
		echo "Starting $Me" >&2
		FlushOrNewChain $Me
		if [ -r "$FBData/tcpservers" ]; then
			cat "$FBData/tcpservers" | while read IP Port ; do
				if [ -n "$Port" ]; then
					$IptablesBin -A $Me -p tcp -s "$IP" --sport "$Port"	-j RETURN
				else
					$IptablesBin -A $Me -p tcp -s "$IP"			-j RETURN
				fi
			done
		fi
		Actions='LOG' LogAs='FB-livetcpserver'	$Ipt -A $Me 				$Tail
		;;
	destroy)
		echo "Stopping $Me" >&2
		DestroyChain $Me
		;;
	renamechain)
		TempChain="$Me-$RANDOM"
		echo "Replacing existing rules in $Me with new rules" >&2
		$IptablesBin -E $Me $TempChain
		;;
	replacelinks)
		if [ -z "$TempChain" ]; then
			echo "No temporary chain to relink in $Me replacelinks, replace operation incomplete." >&2
		elif ! $IptablesBin -L $Me -n >/dev/null 2>&1 ; then
			echo "No $Me chain in $Me, replace operation incomplete." >&2
		elif ! $IptablesBin -L $TempChain -n >/dev/null 2>&1 ; then
			echo "No $TempChain chain in $Me, replace operation incomplete." >&2
		elif [ "`$IptablesBin -L INPUT -n --line-numbers | grep $TempChain | wc -l`" -ne 1 ]; then
			echo "Too few/many references to $TempChain in INPUT in $Me replacelinks, replace operation incomplete." >&2
		elif [ "`$IptablesBin -L FORWARD -n --line-numbers | grep $TempChain | wc -l`" -ne 1 ]; then
			echo "Too few/many references to $TempChain in FORWARD in $Me replacelinks, replace operation incomplete." >&2
		elif [ "`$IptablesBin -L OUTPUT -n --line-numbers | grep $TempChain | wc -l`" -ne 1 ]; then
			echo "Too few/many references to $TempChain in OUTPUT in $Me replacelinks, replace operation incomplete." >&2
		else
			$IptablesBin -R INPUT `$IptablesBin -L INPUT -n --line-numbers | grep $TempChain | awk '{print $1}'` -i \! lo -p tcp --tcp-flags SYN,ACK,FIN,RST SYN,ACK -m state --state established	-j $Me
			$IptablesBin -R FORWARD `$IptablesBin -L FORWARD -n --line-numbers | grep $TempChain | awk '{print $1}'` -p tcp --tcp-flags SYN,ACK,FIN,RST SYN,ACK -m state --state established	-j $Me
			$IptablesBin -R OUTPUT `$IptablesBin -L OUTPUT -n --line-numbers | grep $TempChain | awk '{print $1}'` -p tcp --tcp-flags SYN,ACK,FIN,RST SYN,ACK -m state --state established		-j $Me
			DestroyChain $TempChain
			unset TempChain
		fi
		;;
	status)
		if $IptablesBin -L $Me -n >/dev/null 2>&1 ; then
			echo "$Me created" >&2
		else
			echo "$Me destroyed" >&2
		fi
		;;
	version)
		echo "$Me $MyVersion, firebrickslib $FBLibVer" >&2
		;;
	help)
		DefaultHelp
		cat <<EOTEXT >&2
	The $Me module doesn\'t block any traffic, but simply passively
logs any tcp servers to syslog.  It does this by logging established
SYN/ACK packets; these must have come from a real server at the logged
source IP address and source port.
	Any "IP Port" pairs listed in $FBData/tcpservers are returned
before hitting the log rule; place any known servers here.
	The only reason why you might want to avoid this module is
because it may log heavily.  Otherwise it should be univerally safe.
EOTEXT
		;;
	*)
		echo "Unknown action $Action in $Me, no action taken." >&2
		;;
	esac
done
