From cb2ecacd563c62decd33bf42317ba9c9fcdee14f Mon Sep 17 00:00:00 2001 From: Georg Lauterbach <44545919+aendeavor@users.noreply.github.com> Date: Wed, 17 Feb 2021 12:12:51 +0100 Subject: [PATCH] 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 --- setup.sh | 6 ++-- target/bin/delmailuser | 82 +++++++++++++++++++++++++++++++++++------- 2 files changed, 73 insertions(+), 15 deletions(-) diff --git a/setup.sh b/setup.sh index b19c5620..9cbcef5b 100755 --- a/setup.sh +++ b/setup.sh @@ -127,16 +127,18 @@ SUBCOMMANDS: ${0} email add [] ${0} email update [] - ${0} email del + ${0} email del [OPTIONS] ${0} email restrict [] ${0} email list alias: + ${0} alias add ${0} alias del ${0} alias list quota: + ${0} quota set [] ${0} quota del @@ -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 } diff --git a/target/bin/delmailuser b/target/bin/delmailuser index 7b5b346f..5a83dada 100755 --- a/target/bin/delmailuser +++ b/target/bin/delmailuser @@ -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] []" + 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 ] { [\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