#! /bin/sh
#
# tacclient	Run a TACACS+ client
#
# Version:	$Id: bfdca679985eb1bac1d2bfeb35a029a05595b6fe $
#

usage() {
cat <<EOF
Usage: tacclient [options] server[:port] <command> [secret]
  <command>              One of auth-start, auth-continue, autz, acct
  -4                     Use IPv4 address of server
  -6                     Use IPv6 address of server.
  -d <raddb>             Set user dictionary directory.
  -D <dictdir>           Set main dictionary directory.
  -f <file>              Read packets from file, not stdin.
  -h                     Print usage help information.
  -x                     Debugging mode.
EOF
exit 1
}

OPTIONS=

TACCLIENT_PORT=4900

# Quick pass to check options
if [ $# -lt 2 ]
then
	usage
fi

TACCLIENT_CAST=
OPT_d="-d ./raddb"
OPT_D="-D share/dictionary"

# Parse new command-line options
while [ `echo "$1" | cut -c 1` = "-" ]
do
   case "$1" in
	-4) 
	    	TACCLIENT_CAST="<ipv4addr>"
		shift
		;;
	-6) 
	    	TACCLIENT_CAST="<ipv6addr>"
		shift
		;;
	-d) 
		OPT_d="-d $2"
		shift;shift
		;;
	-D) 
		OPT_D="-D $2"
		shift;shift
		;;
	-f) 
		OPTIONS="$OPTIONS -i $2"
		shift;shift
		;;
	-x)
		OPTIONS="$OPTIONS -x"
		shift
		;;

	*)
		usage
		;;
  esac
done

# Check that there are enough options left over.
if [ $# -lt 2 ]
then
	usage
fi

#
#  Parse packet type
#
PACKET_TYPE=
case "$2" in
	acct) 
		TACCLIENT_PACKET_TYPE="Accounting-Request"
		;;
	autz) 
		TACCLIENT_PACKET_TYPE="Authorization-Request"
		;;
	auth-start) 
		TACCLIENT_PACKET_TYPE="Authentication-Start"
		;;
	auth-continue) 
		TACCLIENT_PACKET_TYPE="Authentication-Continue"
		;;
	*)
		usage
		;;
esac

#
#  Parse server name AFTER packet type, which lets the above
#  code set the default port, but also lets the caller override
#  it with "server:port"
#
TACCLIENT_SERVER="$TACCLIENT_CAST$1"
TACCLIENT_SECRET=$3

#
#  These environment variables are used to fill in the configuration
#  options in radclient.conf
#
export TACCLIENT_SERVER TACCLIENT_PORT TACCLIENT_SECRET

exec ./build/make/jlibtool --mode=execute ./build/bin/local/unit_test_module $OPT_d $OPT_D $OPTIONS -p tacacs -i tacacs -n tacclient -X $@
