This commit is contained in:
Georg Lauterbach 2021-03-24 20:42:00 +01:00 committed by GitHub
parent c214cba981
commit 4afebda64d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 74 deletions

View file

@ -312,7 +312,7 @@ function _main
;; ;;
* ) * )
echo "Invalid option: -${OPTARG}" >&2 echo "Invalid option: -${OPT}" >&2
;; ;;
esac esac

View file

@ -47,8 +47,11 @@ touch "${DATABASE}"
( (
flock -e 200 flock -e 200
grep -qi "^$(escape "${USER}")|" "${DATABASE}" 2>/dev/null && if grep -qi "^$(escape "${USER}")|" "${DATABASE}" 2>/dev/null
errex "User \"${USER}\" already exists" then
echo "User '${USER}' already exists."
exit 1
fi
if [[ -z ${PASSWD} ]] if [[ -z ${PASSWD} ]]
then then

View file

@ -21,7 +21,7 @@ function __usage
delmailuser - delete a user and related data delmailuser - delete a user and related data
\e[38;5;214mSYNOPSIS\e[39m \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 \e[38;5;214mDESCRIPTION\e[39m
Delete a mail user, aliases, quotas and mail data. Delete a mail user, aliases, quotas and mail data.
@ -34,13 +34,13 @@ function __usage
Show this help dialogue. Show this help dialogue.
\e[38;5;214mEXAMPLES\e[39m \e[38;5;214mEXAMPLES\e[39m
./setup.sh email del -y test@domain.com another-test@domain.com \e[37m./setup.sh email del woohoo@some-domain.org\e[39m
Delete all mail data for the users 'test' and 'another-test' Delete the mail user, quotas and aliases, but ask
and do not prompt to ask if all mail data should be deleted. again whether mailbox data should be deleted.
./setup.sh email del woohoo@some-domain.org \e[37m./setup.sh email del -y test@domain.com\e[39m
Delete the mail user, quotas and aliases, but ask again whether Delete all mail data for the users 'test' and do not
mailbox data should be deleted. prompt to ask if all mail data should be deleted.
\e[38;5;214mEXIT STATUS\e[39m \e[38;5;214mEXIT STATUS\e[39m
Exit status is 0 if command was successful, and 1 if there was an error. Exit status is 0 if command was successful, and 1 if there was an error.
@ -67,15 +67,14 @@ do
* ) * )
__usage __usage
echo "The option ${OPT} is unknown." >&2 errex "The option ${OPT} is unknown."
exit 1
;; ;;
esac esac
done done
shift $((OPTIND-1)) shift $((OPTIND-1))
[[ -z ${*} ]] && { usage ; errex "No user specifed" ; } [[ -z ${*} ]] && { __usage ; errex "No user specifed" ; }
[[ -s ${DATABASE} ]] || exit 0 [[ -s ${DATABASE} ]] || exit 0
if ! ${MAILDEL} if ! ${MAILDEL}
@ -89,41 +88,40 @@ fi
( (
flock -e 200 flock -e 200
ERROR=false ERROR=false
EMAIL="${*}"
for USER in "${@}"
do
# very simple plausibility check # very simple plausibility check
[[ ${USER} != *'@'*'.'* ]] && errex "No valid address: ${USER}" [[ ${EMAIL} != *@*.* ]] && errex "No valid address: ${EMAIL}"
declare -a MAILARR USER="${EMAIL%@*}"
MAILARR[0]="${USER%@*}" DOMAIN="${EMAIL#*@}"
MAILARR[1]="${USER#*@}"
# ${USER} must not contain /s and other syntactic characters # ${EMAIL} must not contain /s and other syntactic characters
UNESCAPED_USER="${USER}" UNESCAPED_EMAIL="${EMAIL}"
USER=$(escape "${USER}") EMAIL=$(escape "${EMAIL}")
if [[ -f ${DATABASE} ]] if [[ -f ${DATABASE} ]]
then then
if ! sed -i "/^${USER}|/d" "${DATABASE}" if ! sed -i "/^${EMAIL}|/d" "${DATABASE}"
then then
echo "${UNESCAPED_USER} couldn't be deleted in ${DATABASE}." >&2 echo "${UNESCAPED_EMAIL} couldn't be deleted in ${DATABASE}." >&2
ERROR=true ERROR=true
fi fi
fi fi
if [[ -f ${ALIAS_DATABASE} ]] if [[ -f ${ALIAS_DATABASE} ]]
then then
# delete all aliases where the user is the only recipient( " ${USER$}" ) # delete all aliases where the user is the only recipient( " ${EMAIL}" )
# delete user only for all aliases that deliver to multiple recipients ( ",${USER}" "${USER,}" ) # delete user only for all aliases that deliver to multiple recipients ( ",${EMAIL}" "${EMAIL,}" )
if sed -i \ if sed -i \
-e "/ ${USER}$/d" -e "s/,${USER}//g" -e "s/${USER},//g" \ -e "/ ${EMAIL}$/d" -e "s/,${EMAIL}//g" -e "s/${EMAIL},//g" \
"${ALIAS_DATABASE}" "${ALIAS_DATABASE}"
then then
echo "${UNESCAPED_USER} and potential aliases deleted." echo "${UNESCAPED_EMAIL} and potential aliases deleted."
else else
echo "Aliases for ${UNESCAPED_USER} couldn't be deleted in ${ALIAS_DATABASE}." >&2 echo "Aliases for ${UNESCAPED_EMAIL} couldn't be deleted in ${ALIAS_DATABASE}." >&2
ERROR=true ERROR=true
fi fi
fi fi
@ -131,21 +129,23 @@ fi
# remove quota directives # remove quota directives
if [[ -f ${QUOTA_DATABASE} ]] if [[ -f ${QUOTA_DATABASE} ]]
then then
if ! sed -i -e "/^${USER}:.*$/d" "${QUOTA_DATABASE}" if ! sed -i -e "/^${EMAIL}:.*$/d" "${QUOTA_DATABASE}"
then then
echo "Quota for ${UNESCAPED_USER} couldn't be deleted in ${QUOTA_DATABASE}." >&2 echo "Quota for ${UNESCAPED_EMAIL} couldn't be deleted in ${QUOTA_DATABASE}." >&2
fi fi
fi fi
if ! ${MAILDEL} if ! ${MAILDEL}
then 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]}'" 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 exit 0
fi fi
if [[ -e "/var/mail/${MAILARR[1]}/${MAILARR[0]}" ]] if [[ -e "/var/mail/${DOMAIN}/${USER}" ]]
then then
if rm -R "/var/mail/${MAILARR[1]}/${MAILARR[0]}" if rm -R "/var/mail/${DOMAIN}/${USER}"
then then
echo "Mailbox deleted." echo "Mailbox deleted."
else else
@ -153,12 +153,11 @@ fi
ERROR=true ERROR=true
fi fi
else else
echo "Mailbox directory '/var/mail/${MAILARR[1]}/${MAILARR[0]}' did not exist." >&2 echo "Mailbox directory '/var/mail/${DOMAIN}/${USER}' did not exist." >&2
ERROR=true ERROR=true
fi fi
done
${ERROR} && errex 'Errors encountered.' ${ERROR} && errex 'See the messages above.'
) 200< "${DATABASE}" ) 200< "${DATABASE}"