$Id: LightyDigest,v 1.1 2007/12/11 16:19:46 martin Exp $
Wiki Index All Recent Edit Top
Wiki Index
All
Recent
Edit
Bottom
_ #!/bin/sh
_
_ export PATH="/bin:/usr/bin:/usr/sbin:$PATH"
_
_ # when input ctrl-c, remove lockfile and exit
_ trap '[ $lockstart -eq 1 ] && unlock $pfile && exit 0 || exit 0' INT
_
_ pfile="/etc/lighttpd/conf.d/lighttpd.user"
_ lockstart=0
_ remove=0
_
_ errmsg() {
_ echo "$1" > /dev/stderr
_ }
_
_ user_check() {
_ check_user=$1
_ grep "^${check_user}:" ${pfile} >& /dev/null
_ return $?
_ }
_
_ lock() {
_ lockfile="$1"
_
_ lockfile="${lockfile}.lock"
_
_ [ -f "${lockfile}" ] && {
_ errmsg "WARNING: lock file ${lockfile} is already exists"
_ errmsg " Wait minites for end of previous working ..."
_ }
_
_ while [ -f "${lockfile}" ]; do echo >& /dev/null ; done
_ touch ${lockfile}
_ lockstart=1
_ }
_
_ unlock() {
_ lockfile="$1"
_ lockfile="${lockfile}.lock"
_
_ [ -f "${lockfile}" ] && rm -f ${lockfile} && lockstart=0
_ }
_
_ usage() {
_ errmsg
_ errmsg "lighty-digest.sh: lighttpd htdigest password generation program"
_ errmsg "Scripted by JoungKyun.Kim <http://oops.org>"
_ errmsg
_ errmsg "Usage: $0 -[hd] -u user -p pass -r realm [-f password_file]"
_ errmsg "Options:"
_ errmsg " -h print this help messages"
_ errmsg " -u user username"
_ errmsg " -p pass password"
_ errmsg " -r realm realm name"
_ errmsg " -f filename password file [default: /etc/lighttpd/conf.d/lighttpd.user"
_ errmsg " -d remove user"
_ errmsg
_
_ [ $lockstart -eq 1 ] && rm -f ${pfile}.lock
_
_ exit 1
_ }
_
_ opts=$(getopt df:hp:r:u: $*)
_ [ $? != 0 ] && usage
_
_ set -- ${opts}
_ for i
_ do
_ case "$i" in
_ -d) remove=1; shift;;
_ -f) pfile="$2"; shift; shift;;
_ -p) pass="$2"; shift; shift;;
_ -r) realm="$2"; shift; shift;;
_ -u) user="$2"; shift; shift;;
_ --) shift; break;
_ esac
_ done
_
_ #echo $user
_ #echo $realm
_ #echo $pass
_ #echo $pfile
_ #echo $remove
_
_ [ -z "$user" ] && errmsg "ERROR: User is none!!" && usage
_ [ ${remove} -eq 0 -a -z "${realm}" ] && errmsg "ERROR: Realm is none!!" && usage
_
_ if [ -z "${pass}" -a ${remove} -eq 0 ]; then
_ echo -n "Input new password : "
_ read newpass
_ echo -n "Reinput password for confirm : "
_ read renewpass
_
_ if [ "${newpass}" != "${renewpass}" ]; then
_ errmsg "ERROR: Password is not match"
_ exit 1
_ fi
_
_ pass=${newpass}
_ fi
_
_ lock ${pfile}
_
_ if [ ${remove} -eq 0 ]; then
_ # User Add Mode
_ hash=$(echo -n "${user}:${realm}:${pass}" | md5sum | cut -b -32)
_ user_check ${user}
_ already=$?
_
_ [ -f "${pfile}" ] && cp -af ${pfile} ${pfile}.bak
_ if [ ${already} -eq 0 ]; then
_ # already exists
_ perl -pi -e "s/^${user}:.*$/${user}:${realm}:${hash}/g" ${pfile}
_ else
_ # add new user
_ echo "${user}:${realm}:${hash}" >> ${pfile}
_ fi
_ else
_ # User Remove Mode
_ tmp_htdigest="/tmp/lighttpd-htdiges.tmp.$$"
_ cp -af ${pfile} ${pfile}.bak
_ grep -v "^${user}:" ${pfile} > ${tmp_htdigest}
_ mv -f ${tmp_htdigest} ${pfile}
_ fi
_
_ unlock ${pfile}
_
_ exit 0


