expire-sites/generate-expire-config

65 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
#
# CONFIGURATION
#
LANG=C
DISABLED_PAGE=/~sysadmin/expired/index.php
WHITELIST="ctdalek"
#
# Temporary files
#
EXPIRED_USERS=$(mktemp "/tmp/expired_users.XXXX")
#
# Identify expired accounts
#
# Get the current term
YEAR=$(date '+%Y')
MONTH=$(date '+%-m')
TERM_START_MONTH=$(($MONTH - (($MONTH - 1) % 4)))
# NOTE: This is configured to give 1 months grace period
if [ ${TERM_START_MONTH} -eq 1 ]; then
if [ ${MONTH} -eq 1 ]; then
TERM_FILTER="(!(term=F$((${YEAR} - 1))))(!(term=W${YEAR}))"
else
TERM_FILTER="(!(term=W${YEAR}))"
fi
elif [ ${TERM_START_MONTH} -eq 5 ]; then
if [ ${MONTH} -eq 5 ]; then
TERM_FILTER="(!(term=W${YEAR}))(!(term=S${YEAR}))"
else
TERM_FILTER="(!(term=S${YEAR}))"
fi
elif [ ${TERM_START_MONTH} -eq 9 ]; then
if [ ${MONTH} -eq 9 ]; then
TERM_FILTER="(!(term=S${YEAR}))(!(term=F${YEAR}))"
else
TERM_FILTER="(!(term=F${YEAR}))"
fi
else
echo "Invalid term start month: ${TERM_START_MONTH}" >&2
exit 1
fi
# Find expired accounts
ldapsearch -xb "ou=People,dc=csclub,dc=uwaterloo,dc=ca" "(&${TERM_FILTER}(objectClass=member))" | grep 'uid:' | awk '{print $2}' | sort -u > ${EXPIRED_USERS}
#
# Generate Apache config
#
for uid in $(cat ${EXPIRED_USERS}); do
if [ -d /users/${uid}/www ] && [[ ! ${WHITELIST} =~ ${uid} ]]; then
echo "<Directory /users/${uid}>"
echo " AllowOverride None"
echo " Redirect 503 /"
echo " ErrorDocument 503 /~sysadmin/expired/index.html"
echo "</Directory>"
echo ""
fi
done
rm -f ${EXPIRED_USERS}