#!/bin/sh
# Shell script to handle requires from External login authority.

# init vars
name=""
password=""
value=""
output=/opt/tarantella/var/tmp/ttaext_$$.tmp
failmsg="Login incorrect"
successmsg="Last login"
trap "rm -f $output ; exit" 1 2 3 15

#function to input into telnet
tpipe()
{
  sleep 1
  echo -e "$password" 
  sleep 1 #increase this value if telnet response is slow
}

#loop around stdin and handle strings
while [ "$string" != "exit" ]; do
read string  # read input for stdin

 case $string in
      authenticate=* )
         value=`echo $string | awk 'BEGIN {FS="="}{print $2}'`
         name=`echo $value | awk 'BEGIN {FS=","}{print $1}'`
         password=`echo $value | awk 'BEGIN {FS=","}{print $2}'`

         #feed telnet with values
         tpipe|telnet -l $name yorks.tarantella.com >$output 2>&1 
         status=$?

         #look for success or failure
         value=`grep "$successmsg" "$output"` 

         if [  "$value" != "" ]; then
            echo "OK"
         else
            echo "DENY"
         fi
      ;;
      exit )
        break
      ;;
 esac

# cleanup
rm -f $output

done
