#!/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   : acls 
#
#  PURPOSE: Tests extended acls 
#
#  HISTORY:
#    03/03 Jerone Young  (jyoung5@us.ibm.com)
#    09/08 Jacky Malcles changing the setup to have this TC started by runltp
#*********************************************************************
#
################################################################
#
# Make sure that uid=root is running this script.
# Make sure that loop device is built into the kernel
# Make sure that ACL(Access Control List) and Extended Attribute are
#     built into the kernel
#
#
################################################################

TEST_USER1="acltest1"
TEST_USER1_GROUP="users"
TEST_USER1_PASSWD="ltp_test_pass1"
TEST_USER1_HOMEDIR="tacl/mount-ext3/$TEST_USER1"

FILE_ACL="tacl/mount-ext3/test_file"
FILE_ACL_LINK="tacl/mount-ext3/test_file_link"
TCbin=`pwd`

iam=`whoami`
if [ "z$iam" = "z$TEST_USER1" ]
then
	echo ""
else
if [ $(id -ru) != 0 ]
then
        echo "FAILED: Must have root access to execute this script"
        exit 1
fi

if [ ! -e tacl ]
then
        mkdir -m 777 tacl
else
        echo "FAILED: Directory tacl are exist"
        exit 1
fi

#       The  following  commands  can  be  used as an example of using the loop
#       device.

dd if=/dev/zero of=tacl/blkext3 bs=1k count=10240
chmod 777 tacl/blkext3
#
losetup /dev/loop0 tacl/blkext3 2>&1 > /dev/null
if [ $? != 0 ]
then
        echo ""
        echo "FAILED:  [ losetup ] Must have loop device support by kernel"
        printf "\t to execute this script\n"
        exit 1
fi

mount | grep ext2
if [ $? != 0 ]
then
        mkfs -t ext3 /dev/loop0 #> /dev/null 2>&1
        mkdir  -m 777 tacl/mount-ext3
        mount -t ext3 -o defaults,acl,user_xattr /dev/loop0 tacl/mount-ext3
        if [ $? != 0 ]
        then
                echo ""
                echo "FAILED: [ mount ] Make sure that ACL (Access Control List)"
                printf "\t and Extended Attribute are built into the kernel\n"
                printf "\t Can not mount ext3 file system with acl and user_xattr options\n"
                exit 1
        fi
else
        mkfs -t ext2 /dev/loop0
        mkdir  -m 777 tacl/mount-ext3
        mount -t ext2 -o defaults,acl,user_xattr /dev/loop0 tacl/mount-ext3
        if [ $? != 0 ]
        then
                echo ""
                echo "FAILED: [ mount ] Make sure that ACL (Access Control List)"
                printf "\t and Extended Attribute are built into the kernel\n"
                printf "\t Can not mount ext2 file system with acl and user_xattr options\n"
                exit 1
        fi
fi
fi


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

do_setup(){

	rm -f $FILE_ACL
	rm -f $FILE_ACL_LINK

	if [ "$TEST_USER1_HOMEDIR" = "" ]
	then {
                echo "Could not find user $TEST_USER1's home directory."
                exit 1
        }
        fi

	rm -rf $TEST_USER1_HOMEDIR 
	userdel $TEST_USER1 > /dev/null 2>&1
	sleep 1
	useradd -d `pwd`/$TEST_USER1_HOMEDIR -m -g $TEST_USER1_GROUP $TEST_USER1 -s /bin/sh

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

}

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

do_cleanup() {
	if [ "$TEST_USER1_HOMEDIR" = "" ]
	then {
                echo "Could not remove home directory of the user $TEST_USER1."
                exit 1
        }
        fi
        rm -rf $TEST_USER1_HOMEDIR
	userdel $TEST_USER1
	rm -f $FILE_ACL > /dev/null 2>&1
	rm -f $FILE_ACL_LINK > /dev/null 2>&1
	umount -d tacl/mount-ext3
	rm -rf tacl

}

#-----------------------------------------------------------------------
# FUNCTION:  MAIN
#-----------------------------------------------------------------------

iam=`whoami`
EXIT_CODE=0
if [ "z$iam" = "z$TEST_USER1" ]
then
	echo ""
	echo "TESTING ACLS FOR 2ND USER $iam"

	#setup files for extended attributes
	setfacl -m u:$TEST_USER1:r $FILE_ACL_LINK
	setfacl -m u:$TEST_USER1:r $FILE_ACL

	echo "Trying extended acls for files"
	${TCbin}/acl_file_test $FILE_ACL
	if [ $? != 0 ]
	then 
		EXIT_CODE=1
		echo "Extended acls for files (FAILED)"
	else
		echo "Extended acls for files (PASSED)"
	fi

	echo "Trying extended acls for file links"
	${TCbin}/acl_link_test $FILE_ACL_LINK
	if [ $? != 0 ]
        then
                EXIT_CODE=1
		echo "Extended acls for links (FAILED)"
	else 
		echo "Extended acls for links (PASSED)"	
        fi
else
	do_setup
	echo ""
	echo "TESTING ACLS FOR USER $iam"

	touch $FILE_ACL
	touch $FILE_ACL_LINK

	#setup files for extended attributes
	setfacl -mu:root:r $FILE_ACL_LINK
	setfacl -mu:root:r $FILE_ACL
	
	echo "Trying extended acls for files"
	${TCbin}/acl_file_test $FILE_ACL
	if [ $? != 0 ]
    	then
      	 	EXIT_CODE=1
		echo "Extended acls for files (FAILED)"
	else
		echo "Extended acls for files (PASSED)"
    	fi

	echo "Trying extended acls for file links"	
	${TCbin}/acl_link_test $FILE_ACL_LINK
	if [ $? != 0 ]
	then
		EXIT_CODE=1
		echo "Extended acls for links (FAILED)"
	else
		echo "Extended acls for links (PASSED)"
	fi

	chown $TEST_USER1 $FILE_ACL
	chown $TEST_USER1 $FILE_ACL_LINK

	su $TEST_USER1 -c "$0"
	echo ""
	do_cleanup
fi	

exit $EXIT_CODE
