:
#	@(#) tcbck.sh 23.7 91/11/23 
#
#	Copyright (C) 1989-1991 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	The information in this file is provided for the exclusive use of
#	the licensees of The Santa Cruz Operation, Inc.  Such users have the
#	right to use, modify, and incorporate this code into other products
#	for purposes authorized by the license agreement provided they include
#	this notice and the associated copyright notice with any such product.
#	The information in this file is provided "AS IS" without warranty.
#
# Begin SCO_C2TCB

removelock() {
# remove -o files
	if [ -f "$1-o" ]
	then
		if [ -f "$1" ]
		then
			/bin/rm -f "$1-o"
		else
			/bin/mv "$1-o" "$1"
		fi
	fi
# remove -t files
	if [ -f "$1-t" ]
	then
		if [ -f "$1" ]
		then
			/bin/rm -f "$1-t"
		else
			fn=`pwd`"/$1"
			echo "$0: file $fn missing, saved $fn-t as $fn" >&2
			/bin/mv "$1-t" "$1"
		fi
	fi

}

checkfiles() {
	missingfiles="NO"
	while read file 
  	do
		if [ ! -s "$file" ]
		then
			echo "$0: file $file is missing or zero length"
			missingfiles="YES"
		fi
  	done 
	[ "X$missingfiles" = "XNO" ] 
} << EOF
/etc/auth/system/default 
/etc/auth/system/authorize 
/etc/auth/system/devassign
/tcb/files/auth/r/root 
/etc/group
/etc/passwd
EOF

# check if tcb files are required
if /bin/grep "^TCBFILES=OFF" /etc/default/security >/dev/null 2>&1
then
 	required="NO"
else
 	required="YES"
fi

# clean up -t and -o files

swaredir="/etc/auth/system /etc/auth/subsystems"

for d in $swaredir 
do
	[ -d  "$d" ] && {
		cd $d
		for f in `/bin/ls *?-[to] 2> /dev/null`
		do
			removelock `echo "$f" | /bin/sed "s/-[to]$//"`
		done
	}
done

PRPW="/tcb/files/auth"
if [ -d "$PRPW" ]
then
	cd "$PRPW"

	for f in `/bin/ls */*?-[to] 2> /dev/null`
	do
		removelock `echo "$f" | /bin/sed "s/-[to]$//"`
	done
fi

cd /etc
removelock "passwd"
removelock "shadow"
removelock "group"
cd /etc/default
removelock "login"
removelock "passwd"

# remove passwd and group map files
/bin/rm -f /etc/auth/system/pw_id_map
/bin/rm -f /etc/auth/system/gr_id_map

# remove the temporary ps file
/bin/rm -f /etc/ps/ps_data

# ensure files essential for successful operation are present
missingfiles="NO"
[ "X$required" = "XYES" ] && { checkfiles || missingfiles="YES"; }

# check map files can be rebuilt
fcb="/etc/auth/system/files"
if [ -s "$fcb" ]
then
	if [ -x /tcb/bin/cps ]
	then
		/tcb/bin/cps / > /dev/null 2>&1
		if [ $? -eq 1 -a "X$required" = "XYES" ] 
		then 
		echo "$0: either slash (/) is missing from $fcb or there"
		echo "	are malformed entries in /etc/passwd or /etc/group"
		fi
	else
		if [ "X$required" = "XYES" ]
		then
		 	echo "$0: /tcb/bin/cps is not executable"
		fi
	fi
else
	if [ "X$required" = "XYES" ]
	then
		echo "$0: file $fcb is missing or zero length"
		missingfiles="YES"
	fi
fi

[ "X$required" = "XYES" -a "X$missingfiles" = "XYES" ] && exit 1

exit 0

# End SCO_C2TCB

