:
#	@(#) fixmog 23.5 91/10/29 
#
#	Copyright (C) 1990-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.
#
# Script to correct owner, group and mode inconsistencies found by
# /tcb/bin/integrity -e. This script should only be run by root.
# The script returns zero on success and one if an error is detected.
# If a file is of the wrong type, it is not changed.
#
# Usage: fixmog [-i] [-v]
#
# The -v option causes fixmog to display a verbose description of what
# it changes. The -i option asks for confirmation before each change.
#

IFS=" 	
"
PATH=/bin:/usr/bin:/etc
export PATH IFS

COMMANDNAME=`basename $0`
INTEGRITY=/tcb/bin/integrity
VERBOSE=n
INTERACTIVE=n
SECURITY="/etc/default/security"
REQUIRED="YES"

umask 077

# Make sure we are running as root.
if [ -x /usr/bin/id ]
then
	eval `id  |  sed 's/[^a-z0-9=].*//'`
	if [ "${uid:=0}" -ne 0 ]
	then
		echo "$COMMANDNAME: must be root to run this program"
		exit 1
	fi
fi

while getopts iv c
do
	case $c in
		 i)	 INTERACTIVE=y;;
		 v)	 VERBOSE=y;;
		\?)	 echo "Usage: $COMMANDNAME [-i] [-v]"
			 exit 1;;
	esac
done

# Make sure the integrity program exists and is executable.
if [ ! -x $INTEGRITY ]
then
	if /bin/grep "^TCBFILES=OFF" "$SECURITY" >/dev/null 2>&1
	then
		exit 0
	else
		echo "$COMMANDNAME: $INTEGRITY is missing"
		exit 1
	fi
fi

$INTEGRITY -e | 
awk '
BEGIN {
	file = ""
}
function change() {
	if (file != "") {
		if (owner != "")
			dochange("chown", oldowner, owner, "owner of")
		if (group != "")
			dochange("chgrp", oldgroup, group, "group of")
		if (mode != 8)
			dochange("chmod", oldmode, mode, "mode of")
	}
	reset()
}
function dochange(command, from, to, operation) {
	if ("'$INTERACTIVE'" == "y") {
		reply = "n"
		printf "Change %s %s from %s to %s (y/n)? ", operation, file, from, to
		getline reply <"/dev/tty"
	}
	else
		reply = "y"

	if (reply == "y")
		if (system (sprintf ("%s %s %s", command, to, file)) == 0) {
			if ("'$VERBOSE'" == "y" && "'$INTERACTIVE'" != "y")
				printf "Changed %s %s from %s to %s\n", operation, file, from, to
		}
		else
			printf "Unable to change %s %s from %s to %s\n", operation, file, from, to
}
function reset() {
	file = ""
	owner = ""
	oldowner=""
	group = ""
	oldgroup=""
	mode = 8
	oldmode=8
}
/ is wrong\.$/ {
	change()
	file = $1
}
/^	Owner is/ {
	owner = substr($NF, 1, length($NF) - 1)
	if (index($3, ",") == length($3)) {
		oldowner = substr($3, 1, length($3) - 1)
	}
	else {
		oldowner = substr($4, 1, length($4) - 1)
	}
}
/^	Group is/ {
	group = substr($NF, 1, length($NF) - 1)
	if (index($3, ",") == length($3)) {
		oldgroup = substr($3, 1, length($3) - 1)
	}
	else {
		oldgroup = substr($4, 1, length($4) - 1)
	}
}
/^	Mode is/ {
	mode = substr($6, 1, length($6) - 1)
	oldmode = substr($3, 1, length($3) - 1)
}
/^	Type is/ {
	printf "%s is the wrong type of file:\n", file
	printf "%s\n", substr($0, 1, length($0) - 1)
	reset()
}
END {
	change()
}'
exit 0
