#!/bin/bash
#*********************************************************************
#   Copyright (c) International Business Machines  Corp., 2003, 2004
#
#   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   : su
#
#  PURPOSE: Tests the basic functionality of `su`.
#
#  SETUP: The program `/usr/bin/expect' MUST be installed.
#
#  HISTORY:
#    03/03    Dustin Kirkland (dkirklan@us.ibm.com)
#    03/03    Jerone Young    (jeroney@us.ibm.com)
#    10/01/04 Kris Wilson     Port to Red Hat
#
#*********************************************************************

echo "This script contains bashism that needs to be fixed!"

if [ -z ${TCbin} ]
then
   export TCbin=$PWD
fi

export TEST_USER1="su_usr1"

tvar=${MACHTYPE%-*}
tvar=${tvar#*-}

# need to export tvar for su01_s1
export tvar
printf "Machine type is: $tvar\n\n"

if [ "$tvar" = "redhat" -o "$tvar" = "redhat-linux" ]
# Need to also set group for TEST_USER2
then
export TEST_USER1_GROUP="wheel"
export TEST_USER2_GROUP="wheel"
else
export TEST_USER1_GROUP="trusted"
export TEST_USER2_GROUP="trusted"
fi
export TEST_USER1_PASSWD="eal"
export TEST_USER1_ENCRYPTED_PASSWD="42VmxaOByKwlA"
export TEST_USER1_NEW_PASSWD="a_very_good_and_long_password"
export TEST_USER1_HOMEDIR="/home/$TEST_USER1"

export TEST_USER2="su_usr2"
# Group needs to be trusted for Red Hat.
#export TEST_USER2_GROUP="trusted"
export TEST_USER2_PASSWD="eal"
export TEST_USER2_ENCRYPTED_PASSWD="42VmxaOByKwlA"
export TEST_USER2_HOMEDIR="/home/$TEST_USER2"

#This is for enviroment test
export TEST_LINE="YOU_HAVE_THE_VARIABLE"
export TEST_ENV_FILE="/tmp/TEST_ENV_FILE_ROOT"
export TEST_ENV_FILE_USER="/tmp/TEST_ENV_FILE_USER"
export TEST_ENV_FILE2="/tmp/TEST_ENV_FILE_ROOT2"
#-----------------------------------------------------------------------
# FUNCTION:  do_setup
#-----------------------------------------------------------------------

do_setup(){

#REMOVE ANY TEMPOARY FILES THAT MAY STILL BE AROUND
rm -f $TEST_ENV_FILE_USER > /dev/null 2>&1
rm -f $TEST_ENV_FILE2 > /dev/null 2>&1
rm -f $TEST_ENV_FILE > /dev/null 2>&1


#Create 1st test user
    #erase user if he may exist , so we can have a clean en
        rm -rf /home/$TEST_USER1
        getent passwd $TEST_USER1 > /dev/null 2>&1 && userdel $TEST_USER1
	sleep 1

        useradd -m -g users $TEST_USER1
        if [ $? != 0 ]
        then {
                echo "Could not add test user $TEST_USER1."
                exit 1
        }
        fi

		usermod -G users,$TEST_USER1_GROUP $TEST_USER1

	#create users home directory (SLES 8 does not do this, even when specified in adduser)
# Only do this if not RH; RH creates the directory.
	if [ "$tvar" != "redhat" -a "$tvar" != "redhat-linux" ]
	then {
          USER_UID=`id -u $TEST_USER1`
          USER_GID=`id -g $TEST_USER1`
          mkdir -p $TEST_USER1_HOMEDIR
          chown -R $USER_UID.$USER_GID $TEST_USER1_HOMEDIR
	}
	fi

        usermod -p $TEST_USER1_ENCRYPTED_PASSWD $TEST_USER1 > /dev/null 2>&1
        if [ $? != 0 ]
        then {
                echo "Could not set password for test user $TEST_USER1"
                exit 1
        }
        fi

#Create 2nd test user
	#erase user if he may exist , so we can have a clean en
        rm -rf /home/$TEST_USER2
        getent passwd $TEST_USER2 > /dev/null 2>&1 && userdel $TEST_USER2
	sleep 1

        useradd -m -g users $TEST_USER2

        if [ $? != 0 ]
        then {
                echo "Could not add test user $TEST_USER2."
                exit 1
        }
        fi

		usermod -G users,$TEST_USER2_GROUP $TEST_USER2

	#create users home diretory (SLES 8 does not do this, even when specified in adduser)
# Only do this if not RH; RH creates the directory.
        if [ "$tvar" != "redhat" -a "$tvar" != "redhat-linux" ]
        then {
          USER_UID=`id -u $TEST_USER2`
          USER_GID=`id -g $TEST_USER2`
          mkdir -p $TEST_USER2_HOMEDIR
          chown -R $USER_UID.$USER_GID $TEST_USER2_HOMEDIR
	}
	fi

        usermod -p $TEST_USER2_ENCRYPTED_PASSWD $TEST_USER2 > /dev/null 2>&1
        if [ $? != 0 ]
        then {
                echo "Could not set password for test user $TEST_USER2"
                exit 1
        }
        fi
}


#-----------------------------------------------------------------------
# FUNCTION:  do_cleanup
#-----------------------------------------------------------------------

do_cleanup() {
        rm -rf /home/$TEST_USER1
        rm -rf /home/$TEST_USER2
	userdel $TEST_USER1
	userdel $TEST_USER2
	#REMOVE ANY TEMPOARY FILES THAT MAY STILL BE AROUND
	rm -f $TEST_ENV_FILE_USER > /dev/null 2>&1
	rm -f $TEST_ENV_FILE2 > /dev/null 2>&1
	rm -f $TEST_ENV_FILE > /dev/null 2>&1
}

#-----------------------------------------------------------------------
# FUNCTION:  MAIN
#-----------------------------------------------------------------------
do_setup
/bin/su $TEST_USER1 -c ${TCbin}/su01_s1
EXIT_CODE=$?
do_cleanup
exit $EXIT_CODE