diff --git a/setup.sh b/setup.sh index cc59c576..1b9a93fc 100755 --- a/setup.sh +++ b/setup.sh @@ -312,7 +312,7 @@ function _main ;; * ) - echo "Invalid option: -${OPTARG}" >&2 + echo "Invalid option: -${OPT}" >&2 ;; esac diff --git a/target/bin/addmailuser b/target/bin/addmailuser index e2b33d64..657f56b8 100755 --- a/target/bin/addmailuser +++ b/target/bin/addmailuser @@ -47,8 +47,11 @@ touch "${DATABASE}" ( flock -e 200 - grep -qi "^$(escape "${USER}")|" "${DATABASE}" 2>/dev/null && - errex "User \"${USER}\" already exists" + if grep -qi "^$(escape "${USER}")|" "${DATABASE}" 2>/dev/null + then + echo "User '${USER}' already exists." + exit 1 + fi if [[ -z ${PASSWD} ]] then diff --git a/target/bin/delmailuser b/target/bin/delmailuser index 5a83dada..c4b48729 100755 --- a/target/bin/delmailuser +++ b/target/bin/delmailuser @@ -21,7 +21,7 @@ function __usage 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 } + ./setup.sh email del [ OPTIONS ] { \e[31m|\e[39m help } \e[38;5;214mDESCRIPTION\e[39m Delete a mail user, aliases, quotas and mail data. @@ -34,13 +34,13 @@ function __usage 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. + \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. - ./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[37m./setup.sh email del -y test@domain.com\e[39m + Delete all mail data for the users 'test' and do not + prompt to ask if all mail 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. @@ -67,15 +67,14 @@ do * ) __usage - echo "The option ${OPT} is unknown." >&2 - exit 1 + errex "The option ${OPT} is unknown." ;; esac done shift $((OPTIND-1)) -[[ -z ${*} ]] && { usage ; errex "No user specifed" ; } +[[ -z ${*} ]] && { __usage ; errex "No user specifed" ; } [[ -s ${DATABASE} ]] || exit 0 if ! ${MAILDEL} @@ -89,76 +88,76 @@ fi ( flock -e 200 + ERROR=false + EMAIL="${*}" - for USER in "${@}" - do - # very simple plausibility check - [[ ${USER} != *'@'*'.'* ]] && errex "No valid address: ${USER}" + # very simple plausibility check + [[ ${EMAIL} != *@*.* ]] && errex "No valid address: ${EMAIL}" - declare -a MAILARR - MAILARR[0]="${USER%@*}" - MAILARR[1]="${USER#*@}" + USER="${EMAIL%@*}" + DOMAIN="${EMAIL#*@}" - # ${USER} must not contain /s and other syntactic characters - UNESCAPED_USER="${USER}" - USER=$(escape "${USER}") + # ${EMAIL} must not contain /s and other syntactic characters + UNESCAPED_EMAIL="${EMAIL}" + EMAIL=$(escape "${EMAIL}") - if [[ -f ${DATABASE} ]] + if [[ -f ${DATABASE} ]] + then + if ! sed -i "/^${EMAIL}|/d" "${DATABASE}" then - if ! sed -i "/^${USER}|/d" "${DATABASE}" - then - echo "${UNESCAPED_USER} couldn't be deleted in ${DATABASE}." >&2 - ERROR=true - fi - fi - - if [[ -f ${ALIAS_DATABASE} ]] - then - # delete all aliases where the user is the only recipient( " ${USER$}" ) - # delete user only for all aliases that deliver to multiple recipients ( ",${USER}" "${USER,}" ) - if sed -i \ - -e "/ ${USER}$/d" -e "s/,${USER}//g" -e "s/${USER},//g" \ - "${ALIAS_DATABASE}" - then - echo "${UNESCAPED_USER} and potential aliases deleted." - else - echo "Aliases for ${UNESCAPED_USER} couldn't be deleted in ${ALIAS_DATABASE}." >&2 - ERROR=true - fi - fi - - # remove quota directives - if [[ -f ${QUOTA_DATABASE} ]] - then - if ! sed -i -e "/^${USER}:.*$/d" "${QUOTA_DATABASE}" - then - echo "Quota for ${UNESCAPED_USER} couldn't be deleted in ${QUOTA_DATABASE}." >&2 - fi - fi - - if ! ${MAILDEL} - then - echo "Leaving the mailbox untouched. If you want to delete it at a later point use 'sudo docker exec mail rm -R /var/mail/${MAILARR[1]}/${MAILARR[0]}'" - exit 0 - fi - - if [[ -e "/var/mail/${MAILARR[1]}/${MAILARR[0]}" ]] - then - if rm -R "/var/mail/${MAILARR[1]}/${MAILARR[0]}" - then - echo "Mailbox deleted." - else - echo "Mailbox couldn't be deleted." >&2 - ERROR=true - fi - else - echo "Mailbox directory '/var/mail/${MAILARR[1]}/${MAILARR[0]}' did not exist." >&2 + echo "${UNESCAPED_EMAIL} couldn't be deleted in ${DATABASE}." >&2 ERROR=true fi - done + fi - ${ERROR} && errex 'Errors encountered.' + 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 + echo "${UNESCAPED_EMAIL} and potential aliases deleted." + else + echo "Aliases for ${UNESCAPED_EMAIL} couldn't be deleted in ${ALIAS_DATABASE}." >&2 + ERROR=true + fi + fi + + # remove quota directives + if [[ -f ${QUOTA_DATABASE} ]] + then + if ! sed -i -e "/^${EMAIL}:.*$/d" "${QUOTA_DATABASE}" + then + echo "Quota for ${UNESCAPED_EMAIL} couldn't be deleted in ${QUOTA_DATABASE}." >&2 + 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 + echo "Mailbox deleted." + else + echo "Mailbox couldn't be deleted." >&2 + ERROR=true + fi + else + echo "Mailbox directory '/var/mail/${DOMAIN}/${USER}' did not exist." >&2 + ERROR=true + fi + + ${ERROR} && errex 'See the messages above.' ) 200< "${DATABASE}"