mirror of
https://github.com/docker-mailserver/docker-mailserver.git
synced 2025-06-22 08:53:39 +02:00
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:
parent
f66c70faa8
commit
cb2ecacd56
2 changed files with 73 additions and 15 deletions
6
setup.sh
6
setup.sh
|
@ -127,16 +127,18 @@ SUBCOMMANDS:
|
||||||
|
|
||||||
${0} email add <email> [<password>]
|
${0} email add <email> [<password>]
|
||||||
${0} email update <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 restrict <add|del|list> <send|receive> [<email>]
|
||||||
${0} email list
|
${0} email list
|
||||||
|
|
||||||
alias:
|
alias:
|
||||||
|
|
||||||
${0} alias add <email> <recipient>
|
${0} alias add <email> <recipient>
|
||||||
${0} alias del <email> <recipient>
|
${0} alias del <email> <recipient>
|
||||||
${0} alias list
|
${0} alias list
|
||||||
|
|
||||||
quota:
|
quota:
|
||||||
|
|
||||||
${0} quota set <email> [<quota>]
|
${0} quota set <email> [<quota>]
|
||||||
${0} quota del <email>
|
${0} quota del <email>
|
||||||
|
|
||||||
|
@ -185,7 +187,7 @@ function _docker_image
|
||||||
fi
|
fi
|
||||||
|
|
||||||
${CRI} run --rm \
|
${CRI} run --rm \
|
||||||
-v "${CONFIG_PATH}":/tmp/docker-mailserver"${USING_SELINUX}" \
|
-v "${CONFIG_PATH}:/tmp/docker-mailserver${USING_SELINUX}" \
|
||||||
"${USE_TTY}" "${IMAGE_NAME}" "${@}"
|
"${USE_TTY}" "${IMAGE_NAME}" "${@}"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,21 +13,64 @@ ALIAS_DATABASE="/tmp/docker-mailserver/postfix-virtual.cf"
|
||||||
QUOTA_DATABASE="/tmp/docker-mailserver/dovecot-quotas.cf"
|
QUOTA_DATABASE="/tmp/docker-mailserver/dovecot-quotas.cf"
|
||||||
MAILDEL=false
|
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
|
do
|
||||||
case ${OPT} in
|
case ${OPT} in
|
||||||
y | Y )
|
y | Y )
|
||||||
MAILDEL=true
|
MAILDEL=true
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
h )
|
||||||
|
__usage
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
|
||||||
* )
|
* )
|
||||||
usage
|
__usage
|
||||||
|
echo "The option ${OPT} is unknown." >&2
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
|
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
shift $((OPTIND-1))
|
shift $((OPTIND-1))
|
||||||
|
@ -38,11 +81,15 @@ shift $((OPTIND-1))
|
||||||
if ! ${MAILDEL}
|
if ! ${MAILDEL}
|
||||||
then
|
then
|
||||||
read -r -p "Do you want to delete the mailbox as well (removing all mails) ? [Y/n] " MAILDEL_CHOSEN
|
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
|
fi
|
||||||
|
|
||||||
(
|
(
|
||||||
flock -e 200
|
flock -e 200
|
||||||
|
ERROR=false
|
||||||
|
|
||||||
for USER in "${@}"
|
for USER in "${@}"
|
||||||
do
|
do
|
||||||
|
@ -54,13 +101,15 @@ fi
|
||||||
MAILARR[1]="${USER#*@}"
|
MAILARR[1]="${USER#*@}"
|
||||||
|
|
||||||
# ${USER} must not contain /s and other syntactic characters
|
# ${USER} must not contain /s and other syntactic characters
|
||||||
|
UNESCAPED_USER="${USER}"
|
||||||
USER=$(escape "${USER}")
|
USER=$(escape "${USER}")
|
||||||
|
|
||||||
if [[ -f ${DATABASE} ]]
|
if [[ -f ${DATABASE} ]]
|
||||||
then
|
then
|
||||||
if ! sed -i "/^${USER}|/d" "${DATABASE}"
|
if ! sed -i "/^${USER}|/d" "${DATABASE}"
|
||||||
then
|
then
|
||||||
errex "${USER} couldn't be deleted in ${DATABASE}. ${?}"
|
echo "${UNESCAPED_USER} couldn't be deleted in ${DATABASE}." >&2
|
||||||
|
ERROR=true
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -72,9 +121,10 @@ fi
|
||||||
-e "/ ${USER}$/d" -e "s/,${USER}//g" -e "s/${USER},//g" \
|
-e "/ ${USER}$/d" -e "s/,${USER}//g" -e "s/${USER},//g" \
|
||||||
"${ALIAS_DATABASE}"
|
"${ALIAS_DATABASE}"
|
||||||
then
|
then
|
||||||
echo "${USER} and potential aliases deleted."
|
echo "${UNESCAPED_USER} and potential aliases deleted."
|
||||||
else
|
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
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -83,7 +133,7 @@ fi
|
||||||
then
|
then
|
||||||
if ! sed -i -e "/^${USER}:.*$/d" "${QUOTA_DATABASE}"
|
if ! sed -i -e "/^${USER}:.*$/d" "${QUOTA_DATABASE}"
|
||||||
then
|
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
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -93,17 +143,23 @@ fi
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ -d "/var/mail/${MAILARR[1]}/${MAILARR[0]}" ]]
|
if [[ -e "/var/mail/${MAILARR[1]}/${MAILARR[0]}" ]]
|
||||||
then
|
then
|
||||||
if rm -r -f "/var/mail/${MAILARR[1]}/${MAILARR[0]}"
|
if rm -R "/var/mail/${MAILARR[1]}/${MAILARR[0]}"
|
||||||
then
|
then
|
||||||
echo "Mailbox deleted."
|
echo "Mailbox deleted."
|
||||||
else
|
else
|
||||||
errex "Mailbox couldn't be deleted."
|
echo "Mailbox couldn't be deleted." >&2
|
||||||
|
ERROR=true
|
||||||
fi
|
fi
|
||||||
else
|
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
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
${ERROR} && errex 'Errors encountered.'
|
||||||
|
|
||||||
) 200< "${DATABASE}"
|
) 200< "${DATABASE}"
|
||||||
|
|
||||||
|
exit 0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue