#!/bin/sh
#* ----------------------------------------------------------------------
#*               Proprietary and Confidential Information
#*
#* This material contains, and is part of a computer software program
#* which is, proprietary and confidential information owned by COMPUTER
#* ASSOCIATES INTERNATIONAL, INC.  The program, including this material,
#* may not be duplicated, disclosed or reproduced in whole or in part for
#* any purpose without the express written authorization of COMPUTER
#* ASSOCIATES. All authorized reproductions must be marked with this
#* legend.
#*
#*                          Copyright (c) 1992-2005
#*             Computer Associates International, Incorporated
#*                          All rights reserved
#*
#* ----------------------------------------------------------------------
#*  SCCS Module Control Stamp
#* ----------------------------------------------------------------------
#
#* "@(#)cadirpermchk	1.4  09/06/05 15:10:26"
#* "Copyright (C) 1992-2005 Computer Associates International, Inc.  All rights reserved."
#
# UNIINS.518 - magka04
# Add debug processing to facilitate debugging directory permission
# problems encountered during installs on client systems.
[ "$DEBUG_DIRPERMCHK" = "ON" ] && set -x

traverse_dirs()
{

dir_arg="$1"
directory_name="$dir_arg"


	[ "`echo "$directory_name" | cut -c1`" = "/" ] && dir_fullpath=1

	if [ "$dir_fullpath" != "1" -a -f "$dir_arg" ]
	then

		return 2
	fi


	dir_chg=0
	rcd=0	
	while [ 1 ]
	do

		if [ "$dir_fullpath" = "0" -a ! -d "$directory_name" -a ! -d "`dirname \"$directory_name\"`" ];then

			rcd=5; return $rcd
		fi


		if [ -d "$directory_name" ];then

			if [ "$dir_chg" = "1" -a "$CAUSER" != "root" ];then
				
				echo "test -x \"$directory_name\"" | su "$CAUSER" >/dev/null 2>&1
				if [ "$?" = "0" ];then

					#echo "$directory_name is executable by $CAUSER"
					rcd=0; return $rcd
				else
                                        # UNIINS.518 - magka04
                                        # Additional debug to display directory
                                        # permissions for all directories in
                                        # current test path.
                                        [ "$DEBUG_DIRPERMCHK" = "ON" ] && {
                                             d="$directory_name"
                                             while [ "$d" != "/" ]
                                             do
                                               ls -ld $d
                                               d="`dirname \"$d\"`"
                                             done
				             ls -ld /
                                        }

					rcd=6; return $rcd
				fi
			fi
					

			echo "touch \"$directory_name\"/.cachkdirperm" | su "$CAUSER" >/dev/null 2>&1
			if [ "$?" = "0" ];then

				#echo "$directory_name is writable"

				rm -f "$directory_name"/.cachkdirperm >/dev/null 2>&1
				rcd=0; return $rcd
			else
                           # UNIINS.518 - magka04
                           # Additional debug to display directory
                           # permissions for all directories in
                           # current test path.
                           [ "$DEBUG_DIRPERMCHK" = "ON" ] && {
                                d="$directory_name"
                                while [ "$d" != "/" ]
                                do
                                  ls -ld $d
                                  d="`dirname \"$d\"`"
                                done
				ls -ld /
                           }
                                # UNIINS.518 - magka04
                                # Change return code to facilitate debugging
                                # installs.  This will help to differentiate
                                # between this failure and the other rcd=5
                                # failure earlier in this loop.
				
				rcd=7; return $rcd
			fi
			
		fi


		[ "$directory_name" = "/" ] && {
			rcd=0; return $rcd
		}

		directory_name="`dirname \"$directory_name\"`"
		dir_chg=1
		
	done

	return 0	
}


Display_Help()
{
	
	echo "" >/dev/stderr
	echo "Usage: $0 [user] [directory-name]" >/dev/stderr
	echo "" >/dev/stderr
	echo "       This program is used to check permissions of the directories of root/non-root users" >/dev/stderr
	echo "       This program is intended to be exectued by 'root' user only" >/dev/stderr
	echo "" >/dev/stderr
	exit 3
}

CAUSER="$1"
CADIR="$2"

[ "$1" = "-?" ] && Display_Help

[ "$#" -lt 2 ] && {

        #echo "Usage: $0 [user] [directory-name]"
        exit 4
}


rc1=0

userid=`id | cut -d'(' -f2 |cut -d')' -f1`

if [ "$userid" = "root" ];then

	unset BASH_ENV
	( su ${CAUSER} -c "exit 0" ) >/dev/null 2>&1 || rc1=5

else
	# This program can only be executed by root
	exit 3
fi

[ "$rc1" -ne 0 ] && exit $rc1

traverse_dirs "$CADIR" && {

	#echo "$CADIR ($directory_name) exists and is writable"
	exit 0

} || {

	#echo "$CADIR ($directory_name) is not writable"
	exit $rcd
}

exit 0

