Cover v07 i13
Article
Listing 1
Listing 2
Listing 3
Listing 4
Listing 5
Listing 6
Listing 7


Listing 3

#! /bin/bash -
# FILENAME: gethtaccess
# DESCRIPTION: Scans a directory tree searching for htaccess files and parses
# them to retrieve httpd protection information for relevant subdirectories.
# Protection data are written to stdout or to file suitable for # # use with
# mkhtaccess utility. Base directory may be specified on the # command line.
# Output format is:
#
# dir USER=user[,user,...] GROUP=group[,group,...]# ALLOW=net[,net,...] 	
#        DENY=net[,net,...]
#
# Commands (preceded by #) are allowed as whole lines or appended # at end 
# of line.
# Usernames and groups must be recognized by httpd, i.e. included in global or
# local htpasswd and htgroup files.
#
# USAGE: gethtaccess [-H] [-f file] [-h file] [-D dir] [-v]
# OPTIONS:
#	-H - Prints help message
#	-f file - Appends output to file
#	-h file - Name of htaccess files to search for
#	-D dir - Scans directory tree starting from dir
#	-v - Produces verbose output
#
# LABEN S.p.A. - 05-jan-1998
#
# HISTORY:
# 0.0	Luca Salvadori <lsalvadori.laben.it> - 05-jan-1998
# 	- Functions and behaviour
#
#
########################### S U B R O U T I N E S ##########################

# Program information
AUTHOR="Luca Salvadori <salvadori.l@laben.it>"
VERSION="0.0"
DATE="05-jan-1998"
# Initializing global variables
# Default networks to grant access to
DEF_FILE=mkhtaccess.dat             # Default output file
DEF_HTACCESS=.htaccess              # Default input file(s)
DEF_DIR=~/public_html/              # Default directory to scan
VERBOSE=0                           # Brief output is default
FILE_SELECTED=0                     # Defoult Output is to stdout
# Options string
OPTS=":-H -f: -h: -D: -v"
# Default options
DEFOPT="-D $DEF_DIR -h $DEF_HTACCESS"
# End of global variables initialization

function helpmsg() {
	echo "" 1>&2
	echo " `basename $0` Version $VERSION - $DATE" 1>&2
	echo " Author: $AUTHOR" 1>&2
	echo "" 1>&2
	echo " USAGE: `basename $0` [-H] [-f file] [-h file] [-D dir] [-v]" 1>&2
	echo " OPTIONS:" 1>&2
	echo "	-H - Prints help message" 1>&2
	echo "	-f file - Uses "file" for output" 1>&2
	echo "	-h file - Name of htaccess files to search for" 1>&2
	echo "	-D dir - Scans this directory's protections" 1>&2
	echo "	-v - Produces verbose output" 1>&2
	echo "" 1>&2
}

##################### E N D  O F  S U B R O U T I N E S ####################


########################## M A I N  P R O G R A M ##########################

# Parsing input parameters and assigning default if needed
options=`echo $*`
[ "$options" = "" ] && { $0 $DEFOPT ; exit }

# Parsing options and setting defaults if needed
while [ $OPTIND -le $# ]
do
	getopts "$OPTS" option
	case $option in
		h)
			HTACCESS=$OPTARG
			;;
		H)
			helpmsg
			exit 0
			;;
		f)
			FILE=$OPTARG
			FILE_SELECTED=1
			;;
		D)
			DIR=$OPTARG
			;;
		v)
			VERBOSE=1
			;;
		"?")
			echo "`basename $0` - ERROR: Option -$OPTARG requires an argument or is unknown." 1>&2
			echo "Run `basename $0` -H for help." 1>&2
			exit 3
			;;
		*)
			echo "`basename $0` - ERROR: Unknown option -$OPTARG." 1>&2
			echo "Run `basename $0` -H for help." 1>&2
			exit 2
			;;
	esac
	case $OPTARG in
		-*)
			echo "`basename $0` - ERROR: Option -$option requires an argument." 1>&2
			echo "Invalid argument $OPTARG." 1>&2
			echo "Run `basename $0` -H for help." 1>&2
			exit 3
			;;
	esac
done
# Setting defaults for unselected options
HTACCESS=${HTACCESS:-$DEF_HTACCESS}
FILE=${FILE:-$DEF_FILE}
DIR=${DIR:-$DEF_DIR}
# Perform sanity checks, just in case
[ ! -d $DIR ] && { echo "`basename $0` - ERROR: Directory $DIR does not exist." 1>&2 ; echo "Run `basename $0` -H for help." 1>&2 ; exit 7 }

# Here begins the real stuff

# Some nice output for verbose mode
[ $VERBOSE -eq 1 ] && echo "`basename $0` - Verbose output
-------------------------------------------------------------
Invoked with following defaults:
- Input file: $HTACCESS
- Base Directory: $DIR
-------------------------------------------------------------"


# Now scan directories
for file in `find $DIR -name $HTACCESS`
do
	# Unset local variables
	unset _DIR _USER _GROUP _ALLOW _DENY
	# Parse and evaluate line
	_DIR=`echo $file | sed "s/$HTACCESS//"`
	line=$_DIR
	# Output in verbose mode
	[ $VERBOSE -eq 1 ] && echo "Directory: $_DIR"
	# Get allowed networks
	_ALLOW=`cat $file | grep "^allow" | tr -s " " | sed "s/^allow from //"`
	_ALLOW=`echo $_ALLOW | tr -s " " ","`
	# Display results if required
	if [ $_ALLOW ] 
	then
		[ $VERBOSE -eq 1 ] && echo "Allowed networks: $_ALLOW"
		line=$line" ALLOW=$_ALLOW"
	fi
	# Get denied networks
	_DENY=`cat $file | grep "^deny" | tr -s " " | sed "s/^deny from //"`
	_DENY=`echo $_DENY | tr -s " " ","`
	# Display results if required
	if [ $_DENY ] 
	then
		[ $VERBOSE -eq 1 ] && echo "Denied networks: $_DENY"
		line=$line" DENY=$_DENY"
	fi
	# Get allowed users
	_USER=`cat $file | grep "^require user" | tr -s " " | sed "s/^require user //"`
	_USER=`echo $_USER | tr -s " " ","`
	# Display results if required
	if [ $_USER ] 
	then
		[ $VERBOSE -eq 1 ] && echo "Allowed users: $_USER"
		line=$line" USER=$_USER"
	fi
	# Set allowed groups
	_GROUP=`cat $file | grep "^require group" | tr -s " " | sed "s/^require group //"`
	_GROUP=`echo $_GROUP | tr -s " " ","`
	# Display results if required
	if [ $_GROUP ] 
	then
		[ $VERBOSE -eq 1 ] && echo "Allowed groups: $_GROUP"
		line=$line" GROUP=$_GROUP"
	fi
	[ $VERBOSE -eq 1 ] && { 
		echo "-------------------------------------------------"
	}
	# Append output line to output file (if any)
	if [ $FILE_SELECTED -eq 1 ]
	then
		echo $line >> $FILE
	else
		[ $VERBOSE -ne 1 ] && echo $line
	fi
done

# Happily exit
exit 0

################### E N D  O F  M A I N  P R O G R A M ###################