
#!/bin/bash
# File	: cgi-bin/set-own-pin_htaccess.sh
# By	: MaartenDeBoer.nl, 190130, 200609, 200630
# Subject	: CGI script to process a htaccess set using PIN
#(0.2),191028	: Added NAME2HTACCESSFILE
#(0.3),200609	: Mod's
#(0.4),200609	: Remove echo's
#(0.5),200630	: Mod. some at mailx
#(0.6),200630	: Renamed from preset
#(0.7),201013	: Added mailto
#PGM=`/bin/basename $0|cut -d\. -f1`
PGM="set-own-pin_htaccess"
VER="0.7"
LOG="/var/log/${PGM}.log"
HOSTNAME=`/bin/hostname|cut -d\. -f1`
TMP="/tmp/${PGM}.$$"

APPLDIR="/home/maarten/e-bladmuziek"
DATAFILE="${APPLDIR}/data/name2mail2instrument.csv"
HTPWDDIR="${APPLDIR}/htpwd"

FFACE="Verdana"
FSIZE=10
HEADER="e-bladmuziek"
CUSTOMCOLOR=red
CUSTOMER="ViJoS - Bussum"

USERNAME=""
MAILADDRESS=""

# MAIN
echo "`date` ${PGM} v${VER} started." >> ${LOG}
touch ${TMP} ${TMP}.mailto

echo "Content-type: text/html"
echo "" 

echo "    <TITLE>${HEADER}</TITLE>"
echo "  <BODY>"
echo "  <BODY aLink=${LINKCOLOR} link=${LINKCOLOR} vLink=${LINKCOLOR}>"
echo "  <FONT face=${FFACE}><H2>${HEADER}</H2></FONT>"
echo "  <CENTER> <FONT face=${FFACE} color=${CUSTOMCOLOR}><H1><B>${CUSTOMER}</B></H1></FONT> </CENTER>"
echo "  <DIV align=right><FONT face=${FFACE}<H3><I>Elektronische opslag van bladmuziek</I></H3> </FONT> </DIV>"
echo "  <HR>" 
# Broodkruimels
echo "   <FONT face=${FFACE} size=${FSIZE}> <A HREF=\"/ \">Begin</A> > <A HREF=\"/toegang/\"> Toegang beheer </A> > Zet PIN </FONT> "
echo "  <HR>" 

if [ ${REQUEST_METHOD}="POST" ]; then
  read POST_STRING
#  echo "POST_STRING=${POST_STRING} <BR> <BR>"

  USERNAME=`echo "$POST_STRING" | sed -n 's/^.*USERNAME=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"|sed "s/\+/ /g"`
  PIN_CODE=`echo "$POST_STRING" | sed -n 's/^.*PIN_CODE=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"|sed "s/\+/ /g"`

  echo "<DIV align=left><FONT face=${FFACE} size=${FSIZE}> <B>Zetten PIN</B> <BR>"


# Get the HTACCESSFILE from the username
# And SET the passwd (PIN) in the file with own (PIN_CODE).
# Can be more then one! So, a loop
# When MAILNAME="", then choise = Preset PIN

  if [ "${PIN_CODE}" != "" ] && [ "${USERNAME}" != "" ] ; then
# When setting own ping (${2}), then not gen PIN
    echo "<DIV align=left><FONT face=${FFACE} size=${FSIZE}>   van ${USERNAME} <BR> "
    PINCODE=`echo ${PIN_CODE} | grep "^[0-9][0-9][0-9][0-9]"|head -c${1:-4};echo;`     
    grep "${USERNAME};" ${DATAFILE}|grep -v ^#|while read LINE
    do
      MAILADDRESS=`echo ${LINE}|awk -F\; '{print $2}'|sort -u|head -1|cut -d\< -f2|cut -d\> -f1`
      echo "${MAILADDRESS}" > ${TMP}.mailto
      BAND=`echo ${LINE}|awk -F\; '{print $3}'|sort -u|head -1|cut -d\/ -f1`
      INSTRUMENT=`echo ${LINE}|awk -F\; '{print $3}'|sort -u|head -1|cut -d\/ -f2`
      HTPWDFILE="${HTPWDDIR}/htpasswd.${BAND}.${INSTRUMENT}"
      if [ "${HTPWDFILE}" != "" ]; then
        if [ ! -f ${HTPWDFILE} ]; then
# No HTPWDFILE. So, create
# MIND: HTPWDFILE-DIR NEED to have group-write-perm for web-server-users (www-data)
          htpasswd -bc ${HTPWDFILE} ${USERNAME} ${PINCODE}
          EC1=${?}
        else
          htpasswd -b ${HTPWDFILE} ${USERNAME} ${PINCODE}
          EC2=${?}
        fi
        echo " voor ${BAND}/${INSTRUMENT} <BR>"
#echo "${HTPWDFILE}=${EC} <BR>"
      fi  # "${HTPWDFILE}" != ""
    done  # grep
    echo "<DIV align=left><FONT face=${FFACE} size=${FSIZE}>de PIN-code </FONT>"

    MAILADDRESS=`cat ${TMP}.mailto`
    echo "" > ${TMP}
    echo "Bij e-bladmuziek is de PIN-code van gebruiker ${USERNAME} gezet." >> ${TMP}
    echo "Als dit niet correct is. Dan deze zelf weer resetten, AUB." >> ${TMP}
    echo "" >> ${TMP}
    echo "Verzonden door ${PGM} v${VER} @ ${HOSTNAME} " >> ${TMP}
    echo "" >> ${TMP}

    cat ${TMP} | mailx -s "e-bladmuziek: PIN is gezet." ${MAILADDRESS}

    echo "  Inform mail (with ${USERNAME}) send to ${MAILADDRESS}" >> ${LOG}
# echo " <BR> Sending mail to :${MAILADDRESS} <BR>"

  fi  # "${PIN_CODE}" != "" ] && [ "${USERNAME}" != "" 

  echo "<DIV align=left><FONT face=${FFACE} size=${FSIZE}>is gezet.</FONT>"

else
  echo "Geen POST <BR>"
fi  # ${REQUEST_METHOD}="POST"

rm ${TMP} ${TMP}.mailto
echo "`date` ${PGM} v${VER} finished." >> ${LOG}
exit 0

