#!/bin/sh

#  
# Copyright (C) 2000-2002 Wojciech Purczynski <cliph@isec.pl>
#
# 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.
#

VERBOSE=""
FILES=""
DIRS=""
RPMS=""
GREP="ELF.*executable"

while [ -n "$1" ]; do
	case "$1" in
	-v)
		shift
		VERBOSE="-v"
		;;
	-f)
		shift
		FILES=$*
		shift $#
		;;
	-d)
		shift
		DIRS=$*
		shift $#
		;;
	-s)
		shift
		DIRS="/bin /sbin /lib /usr/bin /usr/sbin /usr/libexec /usr/lib /usr/games /usr/X11R6/bin /usr/X11R6/lib /usr/local/bin /usr/local/sbin /usr/local/lib /usr/local/games /home/httpd/cgi-bin"
		;;
	-r)
		shift
		if [ "$1" == "-a" ]; then
			RPMS="-a"
			shift
		else
			RPMS="-- $*"
			shift $#
		fi
		;;
	-g)
		shift
		GREP="$1"
		shift
		;;
	*)
		echo
		echo "Script removes read/write attributes from executables."
		echo "Copyright (c) 2000-2002 by Wojciech Purczynski <cliph@isec.pl>"
		echo
		echo "Usage:"
		echo "$0 [-v] [-f files | -d dirs | -s | -r packages...]"
		echo "	-v"
		echo "		Verbose mode."
		echo "	-f files..."
		echo "		Fix attributes of specified executables."
		echo "	-d dirs..."
		echo "		Fix attributes of exececutable found in specified"
		echo "		directory."
		echo "	-s"
		echo "		Fix attributes of executables found in standard"
		echo "		system directories."
		echo "	-r packages..."
		echo "		Fix attributes of executables from RPM packages."
		echo "		If package is '-a' then fix attributes of executables"
		echo "		from all installed packages."
		echo "	-h"
		echo "		Displays this help."
		echo
		exit 0
		;;
	esac
done

if [ -n "$FILES" ]; then
	for F in $FILES; do
		file -- "$F"|grep -e "$GREP"|while read fl rst; do
			fl=$(echo "$fl<<<"|sed -e "s/:<<<\$//")
			chmod $VERBOSE ugo-rw -- "$fl"
		done
	done
fi

if [ -n "$DIRS" ]; then
	find $DIRS -type f|file -f -|grep -e "$GREP"|while read fl rst; do
		fl=$(echo "$fl<<<"|sed -e "s/:<<<\$//")
		chmod $VERBOSE ugo-rw -- "$fl"
	done
fi

if [ -n "$RPMS" ]; then
	rpm -ql $RPMS|file -f -|grep -e "$GREP"|while read fl rst; do
		fl=$(echo "$fl<<<"|sed -e "s/:<<<\$//")
		chmod $VERBOSE ugo-rw -- "$fl"
	done
fi
