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


Listing 1

#! /bin/bash -
# FILENAME: chkhtaccess
# DESCRIPTION: Checks for htaccess files in an httpd directory tree.
# Directories containing no htaccess file are displayed on stdout and
# appended to a specified file to be used by mkhtaccess utility.
#
# USAGE: chkhtaccess [-H] [-f file] [-h file] [ -D dir] [-v]
# OPTIONS:
#	-H - Prints help message
#	-f file - Output is appended to "file"
#	-h file - Name of htaccess files to search for
#	-D dir - Starts on this directory
#	-v - Produces verbose output
#
# LABEN S.p.A. - 08-jan-1998
#
# HISTORY:
# 0.0	Luca Salvadori <lsalvadori.laben.it> - 08-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="08-jan-1998"
# Initializing global variables
DEF_FILE=mkhtaccess.dat               # Default input file
DEF_HTACCESS=.htaccess                # Default output file(s)
DEF_DIR=~/public_html/                # Default directory to protect
FILE_SELECTED=0                       # Initialize index to false (0)
VERBOSE=0                             # Brief output is default
TEMP=/tmp                             # Scratch directory
OUTPUT="stdout"                       # Default output
# Options string
OPTS=":-H -f: -h: -D: -v"
# Default options
DEFOPT="-h $DEF_HTACCESS -D $DEF_DIR"
# 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 - Output is appended to "file"" 1>&2
	echo "	-h file - Name of htaccess files to search for" 1>&2
	echo "	-D dir - Starts on this directory" 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
			OUTPUT=$FILE
			;;
		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
[ -f $TEMPLATE ] || { echo "`basename $0` - ERROR: Template file $TEMPLATE does not exist." 1>&2 ; echo "Run `basename $0` -H for help." 1>&2 ; exit 5 }
[ ! -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:
- HTTPD access file: $HTACCESS
- Base Directory: $DIR
- Output sent to: $OUTPUT
-------------------------------------------------------------"

# Now loop over directories
for dir in `find $DIR -type d`
do
	if [ ! -f $dir/$HTACCESS ]
	then
		if [ $VERBOSE -eq 1 ]
		then
			echo "Directory $dir has no $HTACCESS file"
		else
			if [ $FILE_SELECTED -eq 1 ]
			then
				echo $dir >> $FILE
			else
				echo $dir
			fi
		fi
	fi
done

exit 0

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