docker-mailserver/target/bin/delmailuser

162 lines
4.1 KiB
Bash
Executable file

#! /bin/bash
# shellcheck disable=SC2094
# ? This is done to ignore the message "Make sure not to read and write
# ? the same file in the same pipeline", which is a result of ${DATABASE}
# ? being used below. (This disables the message file-wide.)
# shellcheck source=../scripts/helpers/index.sh
source /usr/local/bin/helpers/index.sh
DATABASE='/tmp/docker-mailserver/postfix-accounts.cf'
ALIAS_DATABASE='/tmp/docker-mailserver/postfix-virtual.cf'
QUOTA_DATABASE='/tmp/docker-mailserver/dovecot-quotas.cf'
MAILDEL='false'
function __usage
{
printf '%s' "${PURPLE}DELMAILUSER${RED}(${YELLOW}8${RED})
${ORANGE}NAME${RESET}
delmailuser - delete a user and related data
${ORANGE}SYNOPSIS${RESET}
./setup.sh email del [ OPTIONS ] { <MAIL ADDRESS> [<MAIL ADDRESS>${RED}...${RESET}] ${RED}|${RESET} help }
${ORANGE}DESCRIPTION${RESET}
Delete a mail user, aliases, quotas and mail data.
${ORANGE}OPTIONS${RESET}
-y
Indicate that ${LWHITE}all mail data${RESET} is to be deleted without another prompt.
-h
Show this help dialogue.
${ORANGE}EXAMPLES${RESET}
${LWHITE}./setup.sh email del woohoo@some-domain.org${RESET}
Delete the mail user, quotas and aliases, but ask
again whether mailbox data should be deleted.
${LWHITE}./setup.sh email del -y test@domain.com test@domain.com${RESET}
Delete all mail data for the users 'test' and do not
prompt to ask if all mail data should be deleted.
${ORANGE}EXIT STATUS${RESET}
Exit status is 0 if command was successful, and 1 if there was an error.
"
}
if [[ ${1} == 'help' ]]
then
__usage
exit 0
fi
while getopts ":yYh" OPT
do
case "${OPT}" in
( 'y' | 'Y' )
MAILDEL=true
;;
( 'h' )
__usage
exit 0
;;
( * )
__usage
_exit_with_error "The option '${OPT}' is unknown"
;;
esac
done
shift $((OPTIND-1))
[[ -z ${*} ]] && { __usage ; _exit_with_error 'No user specified' ; }
[[ -s ${DATABASE} ]] || exit 0
if ! ${MAILDEL}
then
read -r -p "Do you want to delete the mailbox as well (removing all mails)? [Y/n] " MAILDEL_CHOSEN
if [[ ${MAILDEL_CHOSEN} =~ (y|Y|yes|Yes) ]] || [[ -z ${MAILDEL_CHOSEN} ]]
then
MAILDEL=true
fi
fi
_create_lock # Protect config file with lock to avoid race conditions
for EMAIL in "${@}"
do
ERROR=false
USER="${EMAIL%@*}"
DOMAIN="${EMAIL#*@}"
# ${EMAIL} must not contain /s and other syntactic characters
UNESCAPED_EMAIL="${EMAIL}"
EMAIL=$(_escape "${EMAIL}")
if [[ -f ${DATABASE} ]]
then
if ! sedfile --strict -i "/^${EMAIL}|/d" "${DATABASE}"
then
_log 'error' "'${UNESCAPED_EMAIL}' couldn't be deleted in '${DATABASE}'"
ERROR=true
fi
fi
if [[ -f ${ALIAS_DATABASE} ]]
then
# delete all aliases where the user is the only recipient( " ${EMAIL}" )
# delete user only for all aliases that deliver to multiple recipients ( ",${EMAIL}" "${EMAIL,}" )
if sed -i \
-e "/ ${EMAIL}$/d" -e "s/,${EMAIL}//g" -e "s/${EMAIL},//g" \
"${ALIAS_DATABASE}"
then
_log 'info' "'${UNESCAPED_EMAIL}' and potential aliases deleted"
else
_log 'error' "Aliases for '${UNESCAPED_EMAIL}' couldn't be deleted in '${ALIAS_DATABASE}'"
ERROR=true
fi
fi
# remove quota directives
if [[ -f ${QUOTA_DATABASE} ]]
then
if ! sedfile --strict -i -e "/^${EMAIL}:.*$/d" "${QUOTA_DATABASE}"
then
_log 'warn' "Quota for '${UNESCAPED_EMAIL}' couldn't be deleted in '${QUOTA_DATABASE}'"
fi
fi
if ! ${MAILDEL}
then
echo "Leaving the mailbox untouched.
If you want to delete it at a later point,
use 'sudo docker exec mailserver rm -R /var/mail/${DOMAIN}/${USER}'"
exit 0
fi
if [[ -e "/var/mail/${DOMAIN}/${USER}" ]]
then
if rm -R "/var/mail/${DOMAIN}/${USER}"
then
_log 'info' 'Mailbox deleted'
else
_log 'error' 'Mailbox could not be deleted'
ERROR=true
fi
rmdir "/var/mail/${DOMAIN}" &>/dev/null
else
log 'error' "Mailbox directory '/var/mail/${DOMAIN}/${USER}' did not exist"
ERROR=true
fi
${ERROR} && _exit_with_error 'See the messages above.'
done
exit 0