#!/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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
#
#
#  FILE             : rpcinfo
#
#  TEST DESCRIPTION : Basic test for the `rpcinfo` 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/20/01 Robbie Williamson (robbiew@us.ibm.com)
#      -Written
#    11/20/08 Aime Le Rouzic (aime.lerouzic@bull.net)
#      - Adapt to rpcbind
#
# CMD      FLAG      ARGS
# rpcinfo   -p
# rpcinfo   -p       rem_host
# rpcinfo   -b
# rpcinfo   -d       prog_num   ver_num
# rpcinfo   -t       rem_host   prog_num
# rpcinfo   -t       rem_host   prog_num     ver_num
# rpcinfo   -t       rem_host   prog_name
# rpcinfo   -t       rem_host   prog_name    ver_num
# rpcinfo   -u       rem_host   prog_num
# rpcinfo   -u       rem_host   prog_num     ver_num
# rpcinfo   -u       rem_host   prog_name
# rpcinfo   -u       rem_host   prog_name    ver_num
# rpcinfo
# rpcinfo   -bogus_flag
# rpcinfo   -u         rem_host
# rpcinfo   -u         rem_host   bogus_prog   ver_num
# rpcinfo   -u         rem_host   prog_num     bogus_num
#***********************************************************************
#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}

$trace_logic
this_file=${0##*/}
TC=rpcinfo01
TCtmp=${TCtmp:=`pwd`}
TCbin=${TCbin:=`pwd`}
TCsrc=${TCsrc:=$TCbin}
LUSER=${LUSER:=root}
RHOST=${RHOST:=`hostname`}
CLEANUP=${CLEANUP:="ON"}
PID=0
export TCID=$TC
export TST_TOTAL=1
export TST_COUNT=1

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

echo "Checking for portmap or rpcbind daemon on `hostname` and $RHOST"
ps -ewf | grep portmap | grep -v grep > /dev/null
if [ $? -eq 0 ]
then
	PORTMAPPER="portmap"
else
	ps -ewf | grep rpcbind | grep -v grep > /dev/null
	if [ $? -eq 0 ]
	then
		PORTMAPPER="rpcbind"
	else
		echo "The portmap or rpcbind daemon is NOT running"
	fi
fi

rsh -n $RHOST "ps -ewf" | grep portmap | grep -v grep > /dev/null
[ $? -eq 0 ] || rsh -n $RHOST "ps -ewf" | grep rpcbind | grep -v grep > /dev/null
[ $? -eq 0 ] || end_testcase "The portmap or rpcbind daemon is NOT running on $RHOST"

echo "Test rpcinfo with default options"
echo "rpcinfo -p"
rpcinfo -p $RHOST | grep portmapper
[ $? -eq 0 ] || end_testcase "rpcinfo -p failed"

echo "Test rpcinfo with options"


# Create file with 1 tcp and 1 udp line. Use for variable assignments.
rpcinfo -p $RHOST | grep tcp | sed -n 2p > $TCtmp/rpc_out
rpcinfo -p $RHOST | grep udp | sed -n 2p >> $TCtmp/rpc_out

wc -l $TCtmp/rpc_out | grep "2" > /dev/null
[ $? -eq 0 ] || end_testcase "Not enough programs registered on $RHOST for test"

# Using "rpc_out" file created above, assign variables for rpcinfo options.
TPNUM=`grep tcp $TCtmp/rpc_out | awk '{print $1}'`
TVNUM=`grep tcp $TCtmp/rpc_out | awk '{print $2}'`
TCPNAME=`grep tcp $TCtmp/rpc_out | awk '{print $5}'`
UPNUM=`grep udp $TCtmp/rpc_out | awk '{print $1}'`
UVNUM=`grep udp $TCtmp/rpc_out | awk '{print $2}'`
UDPNAME=`grep udp $TCtmp/rpc_out | awk '{print $5}'`

# Go through the matrix of remaining rpcinfo options

# -b option
echo "rpcinfo -b <prog num> <ver num>"
rpcinfo -b $UPNUM $UVNUM &
sleep 5
[ $? -eq 0 ] || end_testcase "rpcinfo -b <prog num> <ver num> - failed"
PID=$!
kill -9 $PID > /dev/null 2>&1
PID=0

# -d option
ps -ewf | grep rusersd | grep -v grep
if [ $? = 1 ]; then
  /usr/sbin/rpc.rusersd
  if [ $? = 0 ]; then
    rpcinfo -p | grep 100002
    echo "rpcinfo -d <prog num> <ver num>"
    rpcinfo -d 100002 3
    rpcinfo -p | grep 100002
    rpcinfo -p | grep 100002 | wc -l | grep "1" > /dev/null
    [ $? = 0 ] || end_testcase "rpcinfo -d <prog num> <ver num> - failed"
    RUSERSD_PID=`ps -ewf | grep rusersd | grep -v grep | awk '{print $2}'`
    kill -9 $RUSERSD_PID > /dev/null 2>&1
  else
    echo "Could not start rusersd daemon...skipping -d option test"
  fi
else
  echo "rusersd is currently running, so may be used...skipping -d option test"
fi

# -t options
echo "rpcinfo -t <hostname> <tcp prog num>"
rpcinfo -t $RHOST $TPNUM
[ $? -eq 0 ] || end_testcase "rpcinfo -t <hostname> <tcp prog num> - failed"

echo "rpcinfo -t <hostname> <tcp prog num> <ver num>"
rpcinfo -t $RHOST $TPNUM $TVNUM
[ $? -eq 0 ] || end_testcase "rpcinfo -t <hostname> <tcp prog num> <ver num> - failed"

echo "rpcinfo -t <hostname> <tcp prog name>"
rpcinfo -t $RHOST $TCPNAME
[ $? -eq 0 ] || end_testcase "rpcinfo -t <hostname> <tcp prog name> -  failed"

echo "rpcinfo -t <hostname> <tcp prog name> <ver num>"
rpcinfo -t $RHOST $TCPNAME $TVNUM
[ $? -eq 0 ] || end_testcase "rpcinfo -t <hostname> <tcp prog name> <ver num> - failed"

# -u options
echo "rpcinfo -u <hostname> <udp prog num>"
rpcinfo -u $RHOST 100000
[ $? -eq 0 ] || end_testcase "rpcinfo -u <hostname> <udp prog num> - failed"

echo "rpcinfo -u <hostname> <udp prog num> <ver num>"
rpcinfo -u $RHOST 100000 2
[ $? -eq 0 ] || end_testcase "rpcinfo -u <hostname> <udp prog num> <ver num> - failed"

echo "rpcinfo -u <hostname> <udp prog name>"
rpcinfo -u $RHOST portmapper
[ $? -eq 0 ] || end_testcase "rpcinfo -u <hostname> <udp prog name> - failed"

echo "rpcinfo -u <hostname> <udp prog name> <ver num>"
rpcinfo -u $RHOST portmapper 2
[ $? -eq 0 ] || end_testcase "rpcinfo -u <hostname> <udp prog name> <ver num> - failed"

echo "Test rpcinfo with missing or bad options"

# Check the following: noflag, bogushost, bogusflag, no prog for -u or -t,
# bogusprog, 99999 as versnum.

echo "rpcinfo <no flags>"
case $PORTMAPPER in
	portmap)
		rpcinfo
		[ $? -eq 1 ] || end_testcase "rpcinfo <no flags> should fail"
		;;
	rpcbind)
		rpcinfo
		[ $? -eq 0 ]|| end_testcase "rpcinfo <no flags> should not fail"
		;;
	*)
		echo " portmap or rpcbind not running"
		exit 1
		;;
esac

echo "rpcinfo -p <bad hostname>"
rpcinfo -p bogushost
[ $? -eq 1 ] || end_testcase "rpcinfo <bad hostname> should fail"

echo "rpcinfo <bad flag>"
rpcinfo -bogusflag
[ $? -eq 1 ] || end_testcase "rpcinfo <bad flag> should fail"

echo "rpcinfo -t <hostname> <no prognum>"
rpcinfo -t $RHOST
[ $? -eq 1 ] || end_testcase "rpcinfo -t <hostname> <no prognum> should fail"

echo "rpcinfo -u <hostname> <no prognum>"
rpcinfo -u $RHOST
[ $? -eq 1 ] || end_testcase "rpcinfo -u <hostname> <no prognum> should fail"

echo "rpcinfo -u <hostname> <bad prog name>"
rpcinfo -u $RHOST bogusprog
[ $? -eq 1 ] || end_testcase "rpcinfo -u <hostname> <invalid program name> should fail"

echo "rpcinfo -u <hostname> <bad prog num>"
rpcinfo -u $RHOST 11579
[ $? -eq 1 ] || end_testcase "rpcinfo -u <hostname> 11579 should fail"

echo "rpcinfo -u <hostname> <prog num> <bad ver num>"
rpcinfo -u $RHOST 100000 5
[ $? -eq 1 ] || end_testcase "rpcinfo -u <hostname> <prog num> <bad ver num> should fail"
}

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

    if [ "$PID" != 0 ]; then
      kill -9 $PID
    fi
    rm -f $TCtmp/rpc_out
}

#=============================================================================
# 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 ] && { tst_resm TPASS "Test Successful"; exit 0; }
   tst_resm TFAIL "Test Failed: $@"
   exit 1
}

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

do_test
end_testcase