Rewrite of delmailuser to enable proper account deletion (again) (#1813)

* rewrite to fix docker-mailserver#1808 (again)
* exiting script correctly now
* over-engineered usage information
the usage is now displayed like a man page and the paging mechanism (i.e. the display of the information) is borrowed from batcat
* fix typos
This commit is contained in:
Georg Lauterbach 2021-02-17 12:12:51 +01:00 committed by GitHub
parent f66c70faa8
commit cb2ecacd56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 15 deletions

View file

@ -127,16 +127,18 @@ SUBCOMMANDS:
${0} email add <email> [<password>]
${0} email update <email> [<password>]
${0} email del <email>
${0} email del [OPTIONS] <email>
${0} email restrict <add|del|list> <send|receive> [<email>]
${0} email list
alias:
${0} alias add <email> <recipient>
${0} alias del <email> <recipient>
${0} alias list
quota:
${0} quota set <email> [<quota>]
${0} quota del <email>
@ -185,7 +187,7 @@ function _docker_image
fi
${CRI} run --rm \
-v "${CONFIG_PATH}":/tmp/docker-mailserver"${USING_SELINUX}" \
-v "${CONFIG_PATH}:/tmp/docker-mailserver${USING_SELINUX}" \
"${USE_TTY}" "${IMAGE_NAME}" "${@}"
fi
}

View file

@ -13,21 +13,64 @@ ALIAS_DATABASE="/tmp/docker-mailserver/postfix-virtual.cf"
QUOTA_DATABASE="/tmp/docker-mailserver/dovecot-quotas.cf"
MAILDEL=false
function usage
function __usage
{
echo "Usage: delmailuser [-y] <user@domain> [<user2@anotherdomain>]"
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
./setup.sh email del [ OPTIONS ] { <MAIL ADDRESS> [<MAIL ADDRESS 2>\e[31m...\e[39m] \e[31m|\e[39m help }
\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
./setup.sh email del -y test@domain.com another-test@domain.com
Delete all mail data for the users 'test' and 'another-test'
and do not prompt to ask if all mail data should be deleted.
./setup.sh email del woohoo@some-domain.org
Delete the mail user, quotas and aliases, but ask again whether
mailbox data should be deleted.
\e[38;5;214mEXIT STATUS\e[39m
Exit status is 0 if command was successful, and 1 if there was an error.
"
}
while getopts ":yY" OPT
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
__usage
echo "The option ${OPT} is unknown." >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
@ -38,11 +81,15 @@ shift $((OPTIND-1))
if ! ${MAILDEL}
then
read -r -p "Do you want to delete the mailbox as well (removing all mails) ? [Y/n] " MAILDEL_CHOSEN
[[ ${MAILDEL_CHOSEN} =~ (y|Y| ) ]] && MAILDEL=true
if [[ ${MAILDEL_CHOSEN} =~ (y|Y|yes|Yes) ]] || [[ -z ${MAILDEL_CHOSEN} ]]
then
MAILDEL=true
fi
fi
(
flock -e 200
ERROR=false
for USER in "${@}"
do
@ -54,13 +101,15 @@ fi
MAILARR[1]="${USER#*@}"
# ${USER} must not contain /s and other syntactic characters
UNESCAPED_USER="${USER}"
USER=$(escape "${USER}")
if [[ -f ${DATABASE} ]]
then
if ! sed -i "/^${USER}|/d" "${DATABASE}"
then
errex "${USER} couldn't be deleted in ${DATABASE}. ${?}"
echo "${UNESCAPED_USER} couldn't be deleted in ${DATABASE}." >&2
ERROR=true
fi
fi
@ -72,9 +121,10 @@ fi
-e "/ ${USER}$/d" -e "s/,${USER}//g" -e "s/${USER},//g" \
"${ALIAS_DATABASE}"
then
echo "${USER} and potential aliases deleted."
echo "${UNESCAPED_USER} and potential aliases deleted."
else
errex "Aliases for ${USER} couldn't be deleted in ${ALIAS_DATABASE}."
echo "Aliases for ${UNESCAPED_USER} couldn't be deleted in ${ALIAS_DATABASE}." >&2
ERROR=true
fi
fi
@ -83,7 +133,7 @@ fi
then
if ! sed -i -e "/^${USER}:.*$/d" "${QUOTA_DATABASE}"
then
errex "Quota for ${USER} couldn't be deleted in ${QUOTA_DATABASE}."
echo "Quota for ${UNESCAPED_USER} couldn't be deleted in ${QUOTA_DATABASE}." >&2
fi
fi
@ -93,17 +143,23 @@ fi
exit 0
fi
if [[ -d "/var/mail/${MAILARR[1]}/${MAILARR[0]}" ]]
if [[ -e "/var/mail/${MAILARR[1]}/${MAILARR[0]}" ]]
then
if rm -r -f "/var/mail/${MAILARR[1]}/${MAILARR[0]}"
if rm -R "/var/mail/${MAILARR[1]}/${MAILARR[0]}"
then
echo "Mailbox deleted."
else
errex "Mailbox couldn't be deleted."
echo "Mailbox couldn't be deleted." >&2
ERROR=true
fi
else
echo "Mailbox directory '/var/mail/${MAILARR[1]}/${MAILARR[0]}' did not exist."
echo "Mailbox directory '/var/mail/${MAILARR[1]}/${MAILARR[0]}' did not exist." >&2
ERROR=true
fi
done
${ERROR} && errex 'Errors encountered.'
) 200< "${DATABASE}"
exit 0