#!/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 program;  if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
#
#  FILE             : rusers
#
#  TEST DESCRIPTION : Basic test for the `rusers` 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.
#
#  HISTORY:
#    04/18/01 Robbie Williamson (robbiew@us.ibm.com)
#      -Written
#
# CMD      FLAG      ARGS       
# rusers                       
# rusers             RHOST    
# rusers    -a               
# rusers    -a       RHOST  
# rusers    -l             
# rusers    -l       RHOST
# rusers             bogus_host  
# rusers   -bogus_flag          
#***********************************************************************
#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}

$trace_logic
this_file=${0##*/}

TC=rusers
TCtmp=${TCtmp:=`pwd`}
TCbin=${TCbin:=`pwd`}
TCsrc=${TCsrc:=$TCbin}
LUSER=${LUSER:=root}
RHOST=${RHOST:=`hostname`}
CLEANUP=${CLEANUP:="ON"}
PID=0

#=============================================================================
# FUNCTION NAME:        do_test
#
# FUNCTION DESCRIPTION: Perform the test
#
# PARAMETERS:           None.
#
# RETURNS:              None.
#=============================================================================
do_test()
{
$trace_logic

echo "Checking for rusersd on $RHOST"

rpcinfo -u $RHOST rusersd > /dev/null 2>&1
if [ $? -ne 0 ]; then
  echo "Attempting to start rusersd on $RHOST"
  rsh -n -l root $RHOST "/usr/sbin/rpc.rusersd &"
  [ $? -eq 0 ] || end_testcase "rusersd is inactive on $RHOST" 
  PID=`rsh -n $RHOST ps -ewf | grep rusersd | awk '{print $2 }'`
  echo "ruserd started on $RHOST"
fi

#RHOST=`echo $RHOST | cut -d. -f1`   //The use is depriciated as it fails when RHOST is set to an IP address
#                                    //Pointed out by "Ambar Seksena" <ambar.seksena@calsoftinc.com> 

echo "Test rusers with defaults...please be patient"
# rusers with no options broadcasts over the net and reports 
# responses as it receives them. Time-out for responses is approx. 2 minutes.

rusers > /dev/null 
[ $? -eq 0 ] || end_testcase "rusers with defaults - failed"

echo "Test rusers with options set...please be patient"
# Go through matrix of rusers options:

rusers $RHOST | grep $RHOST > /dev/null 
[ $? -eq 0 ] || end_testcase "rusers $RHOST - failed"

rusers -a $RHOST | grep $RHOST > /dev/null 
[ $? -eq 0 ] || end_testcase "rusers -a $RHOST - failed"

rusers -l > /dev/null 
[ $? -eq 0 ] || end_testcase "rusers -l - failed" 

rusers -l $RHOST | grep $RHOST > /dev/null
[ $? -eq 0 ] || end_testcase "rusers -l $RHOST - failed"   

echo "Test rusers with bad options"

rusers bogushost > /dev/null 2>&1
[ $? -eq 1 ] || end_testcase "rusers <invalid hostname> should fail"

rusers -bogusflag > /dev/null 2>&1
[ $? -eq 1 ] || end_testcase "rusers -<invalid flag> should fail"
}

#=============================================================================
# FUNCTION NAME:        do_cleanup
#
# FUNCTION DESCRIPTION: Clean up
#
# PARAMETERS:           None.
#
# RETURNS:              None.
#=============================================================================
do_cleanup()
{
    $trace_logic

    if [ "$PID" != 0 ]; then
        # Kill rusersd on remote machine
        rsh -n $RHOST kill -9 $PID
        echo "rusersd daemon stopped on $RHOST"
    fi
}

#=============================================================================
# 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."
   if [ "$CLEANUP" = "ON" ]; then
     do_cleanup
   fi

   [ $# = 0 ] && { echo "Test Successfull"; exit 0; }
   echo "Test Failed: $@"
   exit 1
}

#=============================================================================
# MAIN PROCEDURE
#=============================================================================

do_test
end_testcase
