
#-----------------------------------------------------------------------
# Copyright (C) 2000-2001, Jean-Sebastien Morisset <jsmoriss@mvlan.net>
#-----------------------------------------------------------------------
# $Id: mark_hostports.sh,v 1.3 2001/08/28 21:37:23 jsmoriss Exp $
#-----------------------------------------------------------------------
#  $RCSfile: mark_hostports.sh,v $
#   $Author: jsmoriss $
# $Revision: 1.3 $
#     $Date: 2001/08/28 21:37:23 $
#   $Locker:  $ 
#     $Name:  $
#-----------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by 
# the Free Software Foundation <http://www.gnu.org/copyleft/gpl.txt>.
# 
# 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 (LICENSE file in archive) for more details.
#
#-----------------------------------------------------------------------
# FUNCTION
#-----------------------------------------------------------------------

Mark_Hostports () {
	#
	# This function expects each [markvalue] to have an accompanying host and port number(s).
	#
	# Syntax:
	#   Mark_Hostports [local|remote] {prot} {markvalue, hosts, and ports}
	#
	# Examples:
	#   Mark_Hostports local tcp 10 any/0 www
	#   Mark_Hostports local tcp 20 first.friend.com 1234:1240 1666, 20 second.friend.com 1234
	#
	
	lcase_loc_arg="`echo $1|To_Lower`"
	lcase_prot_arg="`echo $2|To_Lower`"
	shift 2	

	# Make sure there's at least one markvalue, hostname, and port.
	if [ "$3" ]
	then
		hostports_arg="`echo $*|sed -e 's/,/ *separator* /g'`"
		mark_field=""
		host_field=""
		port_field=""

		for field in $hostports_arg
		do
			if [ "$field" = "*separator*" ]
			then
				mark_field=""
				host_field=""
				port_field=""
			elif [ ! "$mark_field" ]
			then
				mark_field="$field"
				host_field=""
				port_field=""
			elif [ ! "$host_field" ]
			then
				host_field="$field"
				port_field=""
			else
				port_field="$field"
				
				# Display the port number only if it's numerical.
				[ "`echo $port_field|grep '^[0-9][0-9]*'`" ] \
					&& showport="Port# ${port_field}" || showport="$port_field"

				# If this is a rule for a cluster, we need access *through* the Interface.
				# This means the INCHAIN/OUTCHAIN variables must be reversed
				if [ "$CLUSTER_NAME" ]
				then
					inchain="$OUTCHAIN"
					outchain="$INCHAIN"
				else
					inchain="$INCHAIN"
					outchain="$OUTCHAIN"
				fi

				case $lcase_loc_arg in
					local)
						echo "Inserting Mark #$mark_field $INTOPT $IPADDR ${showport}/$lcase_prot_arg <- $host_field $LOG_MSG"
						ipchains -I $inchain  -m $mark_field -p $lcase_prot_arg -s $host_field -d $IPADDR $port_field $LOG

						# there shouldn't be any reason to mark outgoing packets
						#ipchains -I $outchain -m $mark_field -p $lcase_prot_arg -s $IPADDR $port_field -d $host_field $LOG
						;;
					remote)
						echo "Inserting Mark #$mark_field $INTOPT $IPADDR/$lcase_prot_arg <- $host_field ${showport} $LOG_MSG"
						ipchains -I $inchain  -m $mark_field -p $lcase_prot_arg -s $host_field $port_field -d $IPADDR $LOG

						# there shouldn't be any reason to mark outgoing packets
						#ipchains -I $outchain -m $mark_field -p $lcase_prot_arg -s $IPADDR -d $host_field $port_field $LOG
				esac
			fi
		done
	fi
	unset lcase_loc_arg lcase_prot_arg
	unset hostports_arg field mark_field host_field port_field
}

