
#!/bin/sh
# File	: create_mss01_access.sh
# By	: Maarten.deBoer@Atos.net, 210113
# Subject	: Script to generate access to nlnafvmss01
PGM=`basename $0|cut -d\. -f1`
VER="0.1"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
SSH="/usr/bin/ssh -n"
MAILTO="maarten.deboer@atos.net"

USERLIST="${HOME}/etc/${PGM}.list"
DONELIST="${HOME}/data/${PGM}.done"
FILER="nlnaf25"
VFILER="nlnafvmss01"
NEWPWD="Welkom2MSS!"
GROUP="Administrators"

echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
if [ ! -f ${USERLIST} ]; then
  echo "  NO USERLIST (${USERLIST}) found. Exiting ..."|tee -a ${LOG}
  exit 3
fi

touch ${TMP}

${SSH} ${FILER} "vfiler run ${VFILER} options security.passwd.firstlogin.enable off"
${SSH} ${FILER} "vfiler run ${VFILER} options security.passwd.rules.minimum 10"

cat ${USERLIST}| grep -v ^#|while read LINE REST
do
  UNAME=`echo ${LINE}|awk -F\; '{print $1}'`
  MNAME=`echo ${LINE}|awk -F\; '{print $2}'`
  CHECK=`grep ^${UNAME} ${DONELIST}`
  if [ "${CHECK}" = "" ]; then
# UNAME not found. So, continue
    echo "   ${UNAME}|${MNAME}|"
# 
    ANSW=`${SSH} ${FILER} "vfiler run ${VFILER} useradmin user list ${UNAME}" 2>&1 |grep "User does not exist"`
    if [ "${ANSW}" != "" ]; then
      echo "   ${UNAME} : User does not exist. Will be created."|tee -a ${LOG}
      ${SSH} ${FILER} "vfiler run ${VFILER} useradmin user add ${UNAME} -g ${GROUP} -c ${MNAME} -p ${NEWPWD}"
    fi  # ANSW

    STATUS=`${SSH} ${FILER} "vfiler run ${VFILER} useradmin user list -x ${UNAME}"|grep Status|awk -F\: '{print $2}'|grep enabled`
    NEWPWD="W2MSS!`date +%H%M%S`"
    if [ "${STATUS}" = "" ]; then
      echo "    User ${UNAME} NOT enabled. So, removed & added again ..."|tee -a ${LOG}
      ${SSH} ${FILER} "vfiler run ${VFILER} useradmin user delete ${UNAME}"
      ${SSH} ${FILER} "vfiler run ${VFILER} useradmin user add ${UNAME} -g ${GROUP} -c ${MNAME} -p ${NEWPWD}"
    echo
      echo "    User ${UNAME} enabled. So, will set (new)PWD again ..."|tee -a ${LOG}
      ${SSH} ${FILER} "vfiler run ${VFILER} useradmin user modify ${UNAME} -p ${NEWPWD}"
    fi  # STATUS

    STATUS=`${SSH} ${FILER} "vfiler run ${VFILER} useradmin user list -x ${UNAME}"|grep Status|awk -F\: '{print $2}'|grep enabled`
    if [ "${STATUS}" != "" ]; then
      echo "     User ${UNAME} enabled. Sending a mail to ${MNAME} ..."|tee -a ${LOG}
      echo "Hello ${UNAME}" >> ${TMP}
      echo "You have access to the 5 (mss-)shares at 161.89.52.117 with username ${UNAME}" >> ${TMP}
      echo "and password will follow in seperate mail" >> ${TMP}
      echo "" >> ${TMP}
      echo "This mail is (auto-)generated at `hostname`" >> ${TMP}
      echo "Questions can be send to ${MAILTO}" >> ${TMP}
      echo "" >> ${TMP}
      cat ${TMP} | mailx -s "Created access to MSS01-shares [${PGM} v${VER}]" ${MAILTO} ${MNAME}
      echo "${NEWPWD}" | mailx -s " " ${MNAME}
      cp /dev/null ${TMP}
      echo "${UNAME};${MNAME};`date`" >> ${DONELIST}
    fi  # STATUS
  else
    echo "  ${UNAME} (${MNAME}) already done."
  fi  # CHECK
# FOr generating diff NEWPWD's (based on H:M:S)
  sleep 1
done  # cat ${USERLIST}


rm ${TMP}
echo "`date` ${PGM} v${VER} finished."|tee -a ${LOG}
exit 0

