#!/bin/sh
#
#   Copyright (C) International Business Machines  Corp., 2003
#
#   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   : faillog01
#
#  PURPOSE: Test functionality of the /var/log/faillog file; verify that this
#           effectively reflects failed logins
#
#  HISTORY:
#     04/03  Dustin Kirkland (k1rkland@us.ibm.com)
#
#  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="fl_user"
TEST_USER_PASSWD="ltp_test_pass"
TEST_USER_ENCRYPTED_PASSWD="\$1\$1yzzszzz\$7P9AphbzAN43pTktT/kpp/"
TEST_USER_HOMEDIR="/home/$TEST_USER"
TEST=0

#-----------------------------------------------------------------------
# FUNCTION:  create_user
#-----------------------------------------------------------------------

create_user(){

        echo "Creating test user $TEST_USER..."
	
	#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 to system $RHOST."
		exit 1
	}
	fi

}

#-----------------------------------------------------------------------
# FUNCTION:  delete_user
#-----------------------------------------------------------------------

delete_user(){
        echo "Deleting test user $TEST_USER..."
        rm -rf /home/$TEST_USER
	userdel $TEST_USER
        if [ $? != 0 ]
        then
                echo "Not able to delete test user $TEST_USER."
                exit 1
        fi
}

#-----------------------------------------------------------------------
# FUNCTION:  verify_faillog_no_failures
#-----------------------------------------------------------------------

verify_faillog_no_failures(){

        echo "Verifying faillog no failures for test user $TEST_USER..."

        RESULT=`faillog -u $TEST_USER | grep "^$TEST_USER " | awk -F" " '{ print $2 }'`
        echo $RESULT
        if [ "$RESULT" != "0" ]
        then {
            echo "faillog does not report 0 failures for test user $TEST_USER."
            echo "==> TEST #$TEST : FAIL (Verifying faillog no failures for test user)"
            exit 1
        }
        fi
        echo "==> TEST #$TEST : PASS (Verifying faillog no failures for test user)"
        TEST=$(($TEST+1))
}


#-----------------------------------------------------------------------
# FUNCTION:  verify_faillog_failures
#-----------------------------------------------------------------------

verify_faillog_failures(){

        echo "Verifying faillog failures for test user $TEST_USER..."

        RESULT=`faillog -u $TEST_USER | grep "^$TEST_USER " | awk -F" " '{ print $2 }'`
        echo $RESULT
        if [ "$RESULT" != "3" ]
        then {
            echo "faillog does not report failures for test user $TEST_USER."
            echo "==> TEST #$TEST : FAIL (Verifying faillog failures for test user)"
            exit 1
        }
        fi
        echo "==> TEST #$TEST : PASS (Verifying faillog failures for test user)"
        TEST=$(($TEST+1))
}


#-----------------------------------------------------------------------
# FUNCTION:  login_user
#-----------------------------------------------------------------------

login_user(){
        echo "Logging in test user $TEST_USER over ssh..."
        expect -c "
                   spawn ssh $TEST_USER@localhost
                   expect \"Password:\"
                   sleep 1
                   exp_send \"$TEST_USER_PASSWD\r\"
                   expect \"> \"
                   sleep 1
                   exp_send \"exit\r\"
                   expect success
                   exit 0;
                  "
}

#-----------------------------------------------------------------------
# FUNCTION:  login_user_incorrectly
#-----------------------------------------------------------------------

login_user_incorrectly(){
        echo "Logging in test user $TEST_USER INCORRECTLY over ssh..."
        expect -c "
                   spawn ssh $TEST_USER@localhost
                   expect \"Password:\"
                   sleep 1
                   exp_send \"wrong $TEST_USER_PASSWD\r\"
                   expect \"Password:\"
                   sleep 1
                   exp_send \"wrong $TEST_USER_PASSWD\r\"
                   expect \"Password:\"
                   sleep 1
                   exp_send \"wrong $TEST_USER_PASSWD\r\"
                   expect \"> \"
                   sleep 1
                   exit 0;
                  "
}


#----------------------------------------------------------------------
# FUNCTION: MAIN
# PURPOSE:  To invoke the functions to perform the tasks described in
#           the prologue.
#----------------------------------------------------------------------
create_user
echo "-> Test #$TEST : Verifying faillog no failures for test user"
verify_faillog_no_failures
echo "-> Test #$TEST : Login user incorrectly, verify faillog updated"
login_user_incorrectly
verify_faillog_failures
echo "-> Test #$TEST : Login user correctly, verify faillog updated"
login_user
verify_faillog_no_failures
delete_user
