#!/bin/sh
#
# Script by Marc Mengel to obtain a new Kerberos ticket in
# "portal mode" without logging out and back in again.
#

# get uid
eval `id | sed -e 's/(.*//'`

# figure ticket cache
if [ "x$KRB5CCNAME" = x ]
then
    krb5file=/tmp/krb5cc_$uid
else
    krb5file=`echo $KRB5CCNAME | sed -e s,FILE:,,`
fi

# Send the cryptocard response and some commands to the "remote" end
# of the telnet (which is on localhost).  The sleep command holds the
# input side open while the work goes on.  The block after the telnet
# command picks out the challenge and prompts the user for the response.
(
   read line
   echo $line
   sleep 1000 &
   pid=$!
   echo cp '`echo $KRB5CCNAME | sed -e s,FILE:,,`' "$krb5file"
   echo "kdestroy"
   echo "echo xyzzy $pid xyzzy"
   echo "exit"
   wait $pid
) | (
    /usr/krb5/bin/telnet -X KERBEROS_V5 -X KERBEROS_V4 -N localhost
) |
    while read line
    do
        set : $line
        case $2 in
        Press)
	    printf "$line\n"
	    printf "Enter the displayed response: "
	    ;;
        xyzzy)
	    kill $3
	    ;;
        esac
    done
