#!/bin/bash # apm--Apache Password Manager-allows-A the administrator to easily # add, update, or delete accounts and passwords for a subdirectory # of a typical Apache configuration (where the config file is called # .htaccess). echo "Content-type: text/html" echo "" echo "Apache Password Manager Utility" basedir=$(pwd) myname="$(basename $0)" footer="$basedir/apm-footer.html" htaccess="$basedir/.htaccess" htpasswd="`which htpasswd` -b" # It's highly suggested you include the following code for security purposes: # # if [ "$REMOTE_USER" != "admin" -a -s $htpasswd ] ; then # echo "Error: You must be user admin to use APM." # exit 0 # fi # Now get the password filename from the .htaccess file. if [ ! -r "$htaccess" ] ; then echo "Error: cannot read $htaccess file." exit 1 fi passwdfile="$(grep "AuthUserFile" $htaccess | cut -d\ -f2)" if [ ! -r $passwdfile ] ; then echo "Error: can't read password file: can't make updates." exit 1 elif [ ! -w $passwdfile ] ; then echo "Error: can't write to password file: can't update." exit 1 fi echo "

" echo "Apache Password Manager

" action="$(echo $QUERY_STRING | cut -c3)" user="$(echo $QUERY_STRING|cut -d\& -f2|cut -d= -f2|tr '[:upper:]' '[:lower:]')" case "$action" in A ) echo "

Adding New User $user

" if [ ! -z "$(grep -E "^${user}:" $passwdfile)" ] ; then echo "Error: user $user already appears in the file." else pass="$(echo $QUERY_STRING|cut -d\& -f3|cut -d= -f2)" if [ ! -z "$(echo $pass|tr -d '[[:upper:][:lower:][:digit:]]')" ]; then echo "Error: passwords can only contain a-z A-Z 0-9 ($pass)" else $htpasswd $passwdfile "$user" "$pass" echo "Added!
" fi fi ;; U ) echo "

Updating Password for user $user

" if [ -z "$(grep -E "^${user}:" $passwdfile)" ] ; then echo "Error: user $user isn't in the password file?" echo "searched for "^${user}:" in $passwdfile" else pass="$(echo $QUERY_STRING|cut -d\& -f3|cut -d= -f2)" if [ ! -z "$(echo $pass|tr -d '[[:upper:][:lower:][:digit:]]')" ]; then echo "Error: passwords can only contain a-z A-Z 0-9 ($pass)" else grep -vE "^${user}:" $passwdfile | tee $passwdfile > /dev/null $htpasswd $passwdfile "$user" "$pass" echo "Updated!
" fi fi ;; D ) echo "

Deleting User $user

" if [ -z "$(grep -E "^${user}:" $passwdfile)" ] ; then echo "Error: user $user isn't in the password file?" elif [ "$user" = "admin" ] ; then echo "Error: you can't delete the 'admin' account." else grep -vE "^${user}:" $passwdfile | tee $passwdfile >/dev/null echo "Deleted!
" fi ;; esac # Always list the current users in the password file... echo "

" echo "" oldIFS=$IFS ; IFS=":" # Change word split delimiter... while read acct pw ; do echo "" done < $passwdfile echo "
List " echo "of all current users
$acct" echo "[delete]
" IFS=$oldIFS # ...and restore it. # Build selectstring with all accounts included... optionstring="$(cut -d: -f1 $passwdfile | sed 's/^/