2016-12-30 19:06:44 +00:00
|
|
|
#! /bin/bash
|
2016-06-14 11:16:11 +00:00
|
|
|
|
2021-02-14 20:19:58 +00:00
|
|
|
# shellcheck disable=SC2094
|
2020-10-21 16:16:32 +00:00
|
|
|
# ? 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.)
|
|
|
|
|
2022-02-21 10:56:57 +00:00
|
|
|
# shellcheck source=../scripts/helpers/index.sh
|
|
|
|
source /usr/local/bin/helpers/index.sh
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2022-04-20 00:29:28 +00:00
|
|
|
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'
|
2016-12-30 19:06:44 +00:00
|
|
|
|
2021-02-17 11:12:51 +00:00
|
|
|
function __usage
|
2020-10-21 16:16:32 +00:00
|
|
|
{
|
2021-02-17 11:12:51 +00:00
|
|
|
echo -e "\e[35mDELMAILUSER\e[31m(\e[93m8\e[31m)
|
|
|
|
|
|
|
|
\e[38;5;214mNAME\e[39m
|
|
|
|
delmailuser - delete a user and related data
|
|
|
|
|
|
|
|
\e[38;5;214mSYNOPSIS\e[39m
|
2021-03-31 14:45:16 +00:00
|
|
|
./setup.sh email del [ OPTIONS ] { <MAIL ADDRESS> [<MAIL ADDRESS>\e[31m...\e[39m] \e[31m|\e[39m help }
|
2021-02-17 11:12:51 +00:00
|
|
|
|
|
|
|
\e[38;5;214mDESCRIPTION\e[39m
|
|
|
|
Delete a mail user, aliases, quotas and mail data.
|
|
|
|
|
|
|
|
\e[38;5;214mOPTIONS\e[39m
|
|
|
|
-y
|
|
|
|
Indicate that \e[1mall mail data\e[22m is to be deleted without another prompt.
|
|
|
|
|
|
|
|
-h
|
|
|
|
Show this help dialogue.
|
|
|
|
|
|
|
|
\e[38;5;214mEXAMPLES\e[39m
|
2021-03-24 19:42:00 +00:00
|
|
|
\e[37m./setup.sh email del woohoo@some-domain.org\e[39m
|
|
|
|
Delete the mail user, quotas and aliases, but ask
|
|
|
|
again whether mailbox data should be deleted.
|
2021-02-17 11:12:51 +00:00
|
|
|
|
2021-03-31 14:45:16 +00:00
|
|
|
\e[37m./setup.sh email del -y test@domain.com test@domain.com\e[39m
|
2021-03-24 19:42:00 +00:00
|
|
|
Delete all mail data for the users 'test' and do not
|
|
|
|
prompt to ask if all mail data should be deleted.
|
2021-02-17 11:12:51 +00:00
|
|
|
|
|
|
|
\e[38;5;214mEXIT STATUS\e[39m
|
|
|
|
Exit status is 0 if command was successful, and 1 if there was an error.
|
|
|
|
"
|
2016-12-30 19:06:44 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 11:12:51 +00:00
|
|
|
if [[ ${1} == 'help' ]]
|
|
|
|
then
|
|
|
|
__usage
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
while getopts ":yYh" OPT
|
2020-10-21 16:16:32 +00:00
|
|
|
do
|
2022-03-26 08:30:09 +00:00
|
|
|
case "${OPT}" in
|
|
|
|
( 'y' | 'Y' )
|
2021-02-14 20:19:58 +00:00
|
|
|
MAILDEL=true
|
2020-10-21 16:16:32 +00:00
|
|
|
;;
|
2021-02-17 11:12:51 +00:00
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
( 'h' )
|
2021-02-17 11:12:51 +00:00
|
|
|
__usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
( * )
|
2021-02-17 11:12:51 +00:00
|
|
|
__usage
|
2022-03-26 08:30:09 +00:00
|
|
|
_exit_with_error "The option '${OPT}' is unknown"
|
2020-10-21 16:16:32 +00:00
|
|
|
;;
|
2021-02-17 11:12:51 +00:00
|
|
|
|
2018-03-14 19:00:38 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND-1))
|
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
[[ -z ${*} ]] && { __usage ; _exit_with_error 'No user specified' ; }
|
2020-10-21 16:16:32 +00:00
|
|
|
[[ -s ${DATABASE} ]] || exit 0
|
2016-12-30 19:06:44 +00:00
|
|
|
|
2021-02-14 20:19:58 +00:00
|
|
|
if ! ${MAILDEL}
|
|
|
|
then
|
2022-03-26 08:30:09 +00:00
|
|
|
read -r -p "Do you want to delete the mailbox as well (removing all mails)? [Y/n] " MAILDEL_CHOSEN
|
2021-02-17 11:12:51 +00:00
|
|
|
if [[ ${MAILDEL_CHOSEN} =~ (y|Y|yes|Yes) ]] || [[ -z ${MAILDEL_CHOSEN} ]]
|
|
|
|
then
|
|
|
|
MAILDEL=true
|
|
|
|
fi
|
2021-02-14 20:19:58 +00:00
|
|
|
fi
|
|
|
|
|
2022-02-21 10:56:57 +00:00
|
|
|
_create_lock # Protect config file with lock to avoid race conditions
|
2021-09-13 08:09:01 +00:00
|
|
|
|
2021-06-15 12:03:41 +00:00
|
|
|
for EMAIL in "${@}"
|
|
|
|
do
|
|
|
|
ERROR=false
|
2019-08-01 17:39:25 +00:00
|
|
|
|
2021-06-15 12:03:41 +00:00
|
|
|
USER="${EMAIL%@*}"
|
|
|
|
DOMAIN="${EMAIL#*@}"
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2021-06-15 12:03:41 +00:00
|
|
|
# ${EMAIL} must not contain /s and other syntactic characters
|
|
|
|
UNESCAPED_EMAIL="${EMAIL}"
|
2022-02-21 10:56:57 +00:00
|
|
|
EMAIL=$(_escape "${EMAIL}")
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2021-06-15 12:03:41 +00:00
|
|
|
if [[ -f ${DATABASE} ]]
|
|
|
|
then
|
2022-01-07 23:25:09 +00:00
|
|
|
if ! sedfile --strict -i "/^${EMAIL}|/d" "${DATABASE}"
|
2020-10-21 16:16:32 +00:00
|
|
|
then
|
2022-03-26 08:30:09 +00:00
|
|
|
_log 'error' "'${UNESCAPED_EMAIL}' couldn't be deleted in '${DATABASE}'"
|
2021-06-15 12:03:41 +00:00
|
|
|
ERROR=true
|
2020-10-21 16:16:32 +00:00
|
|
|
fi
|
2021-06-15 12:03:41 +00:00
|
|
|
fi
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2021-06-15 12:03:41 +00:00
|
|
|
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}"
|
2020-10-21 16:16:32 +00:00
|
|
|
then
|
2022-03-26 08:30:09 +00:00
|
|
|
_log 'info' "'${UNESCAPED_EMAIL}' and potential aliases deleted"
|
2021-06-15 12:03:41 +00:00
|
|
|
else
|
2022-03-26 08:30:09 +00:00
|
|
|
_log 'error' "Aliases for '${UNESCAPED_EMAIL}' couldn't be deleted in '${ALIAS_DATABASE}'"
|
2021-06-15 12:03:41 +00:00
|
|
|
ERROR=true
|
2020-10-21 16:16:32 +00:00
|
|
|
fi
|
2021-06-15 12:03:41 +00:00
|
|
|
fi
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2021-06-15 12:03:41 +00:00
|
|
|
# remove quota directives
|
|
|
|
if [[ -f ${QUOTA_DATABASE} ]]
|
|
|
|
then
|
2022-01-07 23:25:09 +00:00
|
|
|
if ! sedfile --strict -i -e "/^${EMAIL}:.*$/d" "${QUOTA_DATABASE}"
|
2020-10-21 16:16:32 +00:00
|
|
|
then
|
2022-03-26 08:30:09 +00:00
|
|
|
_log 'warn' "Quota for '${UNESCAPED_EMAIL}' couldn't be deleted in '${QUOTA_DATABASE}'"
|
2020-04-24 12:55:32 +00:00
|
|
|
fi
|
2021-06-15 12:03:41 +00:00
|
|
|
fi
|
2020-04-24 12:55:32 +00:00
|
|
|
|
2021-06-15 12:03:41 +00:00
|
|
|
if ! ${MAILDEL}
|
|
|
|
then
|
|
|
|
echo "Leaving the mailbox untouched.
|
2021-03-24 19:42:00 +00:00
|
|
|
If you want to delete it at a later point,
|
|
|
|
use 'sudo docker exec mailserver rm -R /var/mail/${DOMAIN}/${USER}'"
|
2021-06-15 12:03:41 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2021-06-15 12:03:41 +00:00
|
|
|
if [[ -e "/var/mail/${DOMAIN}/${USER}" ]]
|
|
|
|
then
|
|
|
|
if rm -R "/var/mail/${DOMAIN}/${USER}"
|
2020-10-21 16:16:32 +00:00
|
|
|
then
|
2022-03-26 08:30:09 +00:00
|
|
|
_log 'info' 'Mailbox deleted'
|
2021-02-14 20:19:58 +00:00
|
|
|
else
|
2022-03-26 08:30:09 +00:00
|
|
|
_log 'error' 'Mailbox could not be deleted'
|
2021-02-17 11:12:51 +00:00
|
|
|
ERROR=true
|
2020-10-21 16:16:32 +00:00
|
|
|
fi
|
2022-03-26 08:30:09 +00:00
|
|
|
rmdir "/var/mail/${DOMAIN}" &>/dev/null
|
2021-06-15 12:03:41 +00:00
|
|
|
else
|
2022-03-26 08:30:09 +00:00
|
|
|
log 'error' "Mailbox directory '/var/mail/${DOMAIN}/${USER}' did not exist"
|
2021-08-28 23:16:34 +00:00
|
|
|
ERROR=true
|
2021-06-15 12:03:41 +00:00
|
|
|
fi
|
2019-08-01 17:39:25 +00:00
|
|
|
|
2022-03-21 14:01:07 +00:00
|
|
|
${ERROR} && _exit_with_error 'See the messages above.'
|
2021-06-15 12:03:41 +00:00
|
|
|
done
|
2022-03-26 08:30:09 +00:00
|
|
|
|
2021-02-17 11:12:51 +00:00
|
|
|
exit 0
|