Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for #1808 #1864

Merged
merged 5 commits into from Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.sh
Expand Up @@ -312,7 +312,7 @@ function _main
;;

* )
echo "Invalid option: -${OPTARG}" >&2
echo "Invalid option: -${OPT}" >&2
;;

esac
Expand Down
7 changes: 5 additions & 2 deletions target/bin/addmailuser
Expand Up @@ -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
Expand Down
119 changes: 59 additions & 60 deletions target/bin/delmailuser
Expand Up @@ -21,7 +21,7 @@ function __usage
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 }
./setup.sh email del [ OPTIONS ] { <MAIL ADDRESS> \e[31m|\e[39m help }

\e[38;5;214mDESCRIPTION\e[39m
Delete a mail user, aliases, quotas and mail data.
Expand All @@ -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.
Expand All @@ -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}
Expand All @@ -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
echo "${UNESCAPED_EMAIL} couldn't be deleted in ${DATABASE}." >&2
ERROR=true
fi
fi

if [[ -f ${ALIAS_DATABASE} ]]
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
# 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
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} ]]
# remove quota directives
if [[ -f ${QUOTA_DATABASE} ]]
then
if ! sed -i -e "/^${EMAIL}:.*$/d" "${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
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 mail rm -R /var/mail/${MAILARR[1]}/${MAILARR[0]}'"
exit 0
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/${MAILARR[1]}/${MAILARR[0]}" ]]
if [[ -e "/var/mail/${DOMAIN}/${USER}" ]]
then
if rm -R "/var/mail/${DOMAIN}/${USER}"
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
echo "Mailbox deleted."
else
echo "Mailbox directory '/var/mail/${MAILARR[1]}/${MAILARR[0]}' did not exist." >&2
echo "Mailbox couldn't be deleted." >&2
ERROR=true
fi
done
else
echo "Mailbox directory '/var/mail/${DOMAIN}/${USER}' did not exist." >&2
ERROR=true
fi

${ERROR} && errex 'Errors encountered.'
${ERROR} && errex 'See the messages above.'

) 200< "${DATABASE}"

Expand Down