#!/bin/sh
#
#   Copyright (c) International Business Machines  Corp., 2003, 2005
#
#   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   : ftp
#
#  PURPOSE: Ftp into a remote hosts successfully as a vaild user (other than root) 
#
#  HISTORY:
#     03/03  Jerone Young (jeroney@us.ibm.com) 
#     04/03  Dustin Kirkland (k1rkland@us.ibm.com)
#     09/05  Kris Wilson (krisw@us.ibm.com) Check if vsftpd.conf was found.
#
#  NOTE:
#	This version is intended for EAL certification, it will need modification 
#	to conform with LTP standards in the offical LTP tree.

RHOST="localhost"
TEST_USER="ftpuser1"
TEST_USER_PASSWD="eal"
TEST_USER_ENCRYPTED_PASSWD="42VmxaOByKwlA"
TEST_USER_HOMEDIR="/home/$TEST_USER"
tvar=${MACHTYPE%-*}
tvar=${tvar#*-}

if [ $tvar = "redhat" -o $tvar = "redhat-linux" ] 
then
ls /etc/vsftpd/vsftpd.conf
 if [ $? != 0 ]
      then { 
         echo "vsftpd.conf not found.  Possible cause: does not exist on WS."
         echo "Test ftp03 FAIL."
         EXIT_CODE=1
         exit 1
      } 
 fi
LOCAL_ENABLE=`cat /etc/vsftpd/vsftpd.conf | grep "^local_enable=" | awk -F= '{print $2}'`
else
LOCAL_ENABLE=`cat /etc/vsftpd.conf | grep "^local_enable=" | awk -F= '{print $2}'`
fi
if [ "$LOCAL_ENABLE" != "YES" ]; then
  LOCAL_ENABLE="NO"
fi 

EXIT_CODE=0

#-----------------------------------------------------------------------
# FUNCTION:  do_setup
#-----------------------------------------------------------------------

do_setup(){
	
	#erase user if he may exist , so we can have a clean env
        rm -rf /home/$TEST_USER
	userdel $TEST_USER
        sleep 1

	useradd -m -p $TEST_USER_ENCRYPTED_PASSWD $TEST_USER 
	if [ $? != 0 ] 
	then { 
		echo "Could not add test user $TEST_USER on system $RHOST."
		exit 1
	}
	fi

	#create users home diretory (SLES 8 does not do this, even when specified in adduser)
	USER_UID=`id -u $TEST_USER`
	USER_GID=`id -g $TEST_USER`
	mkdir $TEST_USER_HOMEDIR
	chown -R $USER_UID.$USER_GID $TEST_USER_HOMEDIR
	
}

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

do_cleanup(){
        rm -rf /home/$TEST_USER
	userdel $TEST_USER
}

#-----------------------------------------------------------------------
# FUNCTION:  do_test
#
# DESCRIPTION: The test user will ftp in and create a directory in his home directory on the remote host.
#              The directory is then checked on the remote hosts to see if it is owned
#	       by the test user. 
#-----------------------------------------------------------------------

do_test(){
	echo "TEST: Ftp into a remote host as a local user (other than root), LOCAL_ENABLE=$LOCAL_ENABLE"

if [ "$LOCAL_ENABLE" = "YES" ]; then {
	expect -c "
                   spawn ftp $RHOST
                   sleep 1
                   expect -re \": \"
                   send \"$TEST_USER\r\"
                   expect -re \"Password:\"
                   send \"$TEST_USER_PASSWD\r\"
                   expect {
                     # 530 - Login failed
                           \"530\" {send_user \"==> TEST \#$TEST : FAIL (ftp rejected login attempt)\n\";exit 1}
                     # 230 - Login successful
                           \"230\" {send_user \"==> TEST \#$TEST : PASS (ftp allowed login attempt)\n\";exit 0}
                   }
                   expect \"ftp> \"
                   send \"quit\r\"
	"
} else {
        expect -c "
                   spawn ftp $RHOST
                   sleep 1
                   expect -re \": \"
                   send \"$TEST_USER\r\"
                   expect -re \"Password:\"
                   send \"$TEST_USER_PASSWD\r\"
                   expect {
                     # 230 - Login successful
                           \"230\" {send_user \"==> TEST \#$TEST : FAIL (ftp allowed login attempt)\n\";exit 1} 
                     # 500 - Login failed
                           \"500\" {send_user \"==> TEST \#$TEST : PASS (ftp rejected login attempt)\n\";exit 0}
                     # 530 - Login failed
                           \"530\" {send_user \"==> TEST \#$TEST : PASS (ftp rejected login attempt)\n\";exit 0}
                   }
                   expect \"ftp> \"
                   send \"quit\r\"
        "
} fi


    if [ $? != 0 ]
        then {
        EXIT_CODE=1 
        }
    fi
}	

#----------------------------------------------------------------------
# FUNCTION: MAIN
# PURPOSE:  To invoke the functions to perform the tasks described in
#           the prologue.
#----------------------------------------------------------------------
do_setup
do_test
do_cleanup
exit $EXIT_CODE
