#!/bin/sh
#
#   Copyright (c) International Business Machines  Corp., 2000
#
#   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; either version 2 of the License, or
#   (at your option) any later version.
#
#   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 for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this pronram;  if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
#
#  FILE   : finger6
#
#  PURPOSE: To test the basic functionality of the `finger6` command.
#
#  SETUP: The home directory of root on the machine exported as "RHOST"
#         MUST have a ".rhosts" file with the hostname of the machine
#         where the test is executed. Also, both machines MUST have
#         the fingerd daemon active (see inetd or xinetd config file). 
#
#  HISTORY:
#    03/01 Robbie Williamson (robbiew@us.ibm.com)
#      -Ported
#
#
#-----------------------------------------------------------------------
# Uncomment line below for debugging information
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

RUSER=root
RHOST=${RHOST:-`hostname`}
LHOST=`hostname`
SLEEPTIME=${SLEEPTIME:-1}
LUSER=`id | tr -s '()' '  ' | awk '{print $2}'`

this_file=${0##*/}
#-----------------------------------------------------------------------
#
# FUNCTION:  do_setup
#
#-----------------------------------------------------------------------

do_setup ()
{
   $trace_logic

   # Start fingerd locally and remotely.

   [ "$LUSER" = "root" ] || end_testcase "MUST BE root to run the test!"

   FINGER=`netstat -an | grep "::79" |awk '{print $6}'`
   if [ $FINGER -ne "LISTEN" ]; then
      end_testcase "Please start fingerd for IPv6 locally."
   fi

   FINGER=`rsh -n $RHOST netstat -a |grep "::79"|awk '{print $6}'`
   if [ $FINGER -ne "LISTEN" ]; then
      end_testcase "Please start fingerd for IPv6 on remote host."
   fi
}

#-----------------------------------------------------------------------
# FUNCTION: do_test1
# PURPOSE: Run finger with no flags 
# INPUT:    None.
# OUTPUT:   Error messages are logged for all discrepancies found. 
#-----------------------------------------------------------------------

do_test1()
{
   $trace_logic

   echo "finger: do_test1 started"

   finger @$LHOST 
   [ $? = 0 ] || end_testcase "finger @$LHOST"
   finger @$RHOST 
   [ $? = 0 ] || end_testcase "finger @$RHOST"
	
   # Testing finger command with user argument

   finger $LUSER 
   [ $? = 0 ] || end_testcase "finger $LUSER"
   finger $LUSER@$LHOST 
   [ $? = 0 ] || end_testcase "finger $LUSER@$LHOST"
   finger $RUSER@$RHOST
   [ $? = 0 ] || end_testcase "finger @$RUSER"
}

#-----------------------------------------------------------------------
# FUNCTION: do_test2
# PURPOSE:  To run the "finger User@host" commands and verify that the
#           "finger" command is functioning properly.
# INPUT:    None.
# OUTPUT:   Error messages are logged for all discrepancies found.
#-----------------------------------------------------------------------

do_test2()
{
   $trace_logic

   echo "do_test2 $TC finger -bad flag"
   BADUSER="uuuu"
   BADHOST="xxxx"

   echo "finger -badflag default "
   finger -x
   [ $? = 0 ] && end_testcase "finger -x should fail"

   echo "finger -bad user "
   finger $BADUSER
   [ $? = 0 ] || end_testcase "finger $BADUSER "
   finger @$BADHOST
   [ $? = 0 ] || end_testcase "finger @$BADHOST"
   finger $BADUSER@$BADHOST
   [ $? = 0 ] || end_testcase "finger $BADUSER@$BADHOST"
}

#-----------------------------------------------------------------------
# FUNCTION: do_test3
# PURPOSE:  To run the "finger -flag User@Host" commands and verify that the
#           "finger" command is functioning properly.
# INPUT:    None.
# OUTPUT:   Error messages are logged for all discrepancies found. 
#-----------------------------------------------------------------------

do_test3()
{
   $trace_logic

   echo "do_test3 $TC finger -f flag"

   echo "finger -l : gives a long listing"
   finger -l 
   [ $? = 0 ] || end_testcase "finger -l"
   finger -l $LUSER 
   [ $? = 0 ] || end_testcase "finger -l $LUSER "
   finger -l @$LHOST 
   [ $? = 0 ] || end_testcase "finger -l @$LHOST"
   finger -l $LUSER@$LHOST 
   [ $? = 0 ] || end_testcase "finger -l $LUSER@$LHOST"
   finger -l @$RHOST 
   [ $? = 0 ] || end_testcase "finger -l @$RHOST"
   finger -l $RUSER@$RHOST 
   [ $? = 0 ] || end_testcase "finger -l $RUSER@$RHOST"

   echo "finger -m : assures that user is a user ID discretionary"
   finger -m 
   [ $? = 0 ] || end_testcase "finger -m"
   finger -m $LUSER 
   [ $? = 0 ] || end_testcase "finger -m $LUSER "
   finger -m @$LHOST 
   [ $? = 0 ] || end_testcase "finger -m @$LHOST"
   finger -m $LUSER@$LHOST 
   [ $? = 0 ] || end_testcase "finger -m $LUSER@$LHOST"
   finger -m @$RHOST 
   [ $? = 0 ] || end_testcase "finger -m @$RHOST"
   finger -m $RUSER@$RHOST 
   [ $? = 0 ] || end_testcase "finger -m $RUSER@$RHOST"

   echo "finger -p:suppresses printing of .plan files on long and brief long formats"
   finger -p 
   [ $? = 0 ] || end_testcase "finger -p"
   finger -p $LUSER 
   [ $? = 0 ] || end_testcase "finger -p $LUSER "
   finger -p @$LHOST 
   [ $? = 0 ] || end_testcase "finger -p @$LHOST"
   finger -p $LUSER@$LHOST 
   [ $? = 0 ] || end_testcase "finger -p $LUSER@$LHOST"
   finger -p @$RHOST 
   [ $? = 0 ] || end_testcase "finger -p @$RHOST"
   finger -p $RUSER@$RHOST 
   [ $? = 0 ] || end_testcase "finger -p $RUSER@$RHOST"

   echo "finger -s : gives a short format list"
   finger -s 
   [ $? = 0 ] || end_testcase "finger -s"
   finger -s $LUSER 
   [ $? = 0 ] || end_testcase "finger -s $LUSER "
   finger -s @$LHOST 
   [ $? = 0 ] || end_testcase "finger -s @$LHOST"
   finger -s $LUSER@$LHOST 
   [ $? = 0 ] || end_testcase "finger -s $LUSER@$LHOST"
   finger -s @$RHOST 
   [ $? = 0 ] || end_testcase "finger -s @$RHOST"
   finger -s $RUSER@$RHOST 
   [ $? = 0 ] || end_testcase "finger -s $RUSER@$RHOST"

}

#=============================================================================
# FUNCTION NAME:        end_testcase
#
# FUNCTION DESCRIPTION: Clean up
#
# PARAMETERS:           string, IF AND ONLY IF the testcase fails
#
# RETURNS:              None.
#=============================================================================

end_testcase()
{
   $trace_logic
   echo "$this_file: doing $0."

   [ $# = 0 ] && { echo "Test Successful"; exit 0; }
   echo "test failed: $@"
   exit 1
}

#-----------------------------------------------------------------------
# FUNCTION: MAIN
# PURPOSE:  To invoke the functions to perform the tasks outlined in the
#           prologue.
# INPUT:    None.
# OUTPUT:   None.
#-----------------------------------------------------------------------
do_setup
do_test1
sleep $SLEEPTIME
do_test2
sleep $SLEEPTIME
do_test3
sleep $SLEEPTIME
end_testcase
