Delmailuser (Closes: #878)

* delmailuser:
  + added multiple address deletion
  + added alias deletion
  + added maildir deletion (upon confirmation)
  + introduced optional "assume yes" argument
* updated addalias,delalias,delmailuser,updatemailuser and added modified tests
* added config check and repair to start-mailserver for old postfix-virtual.cf files
This commit is contained in:
17Halbe 2018-03-14 20:00:38 +01:00 committed by Johan Smits
parent 50a76ba91e
commit 570237232c
7 changed files with 61 additions and 30 deletions

View file

@ -6,7 +6,7 @@ EMAIL="$1"
RECIPIENT="$2"
usage() {
echo "Usage: addalias <user@domain> <recipient@other>"
echo "Usage: addalias <alias@domain> <recipient@other>"
}
errex() {
@ -24,7 +24,7 @@ grep -qi "^$(escape $EMAIL)[a-zA-Z@.\ ]*$(escape $RECIPIENT)" $DATABASE 2>/dev/n
errex "Alias \"$EMAIL $RECIPIENT\" already exists"
if grep -qi "^$(escape $EMAIL)" $DATABASE 2>/dev/null; then
sed -i "/$EMAIL/s/$/ $RECIPIENT,/" $DATABASE
sed -i "/$EMAIL/s/$/,$RECIPIENT/" $DATABASE
else
echo "$EMAIL $RECIPIENT," >> $DATABASE
echo "$EMAIL $RECIPIENT" >> $DATABASE
fi

View file

@ -6,7 +6,7 @@ EMAIL="$1"
RECIPIENT="$2"
usage() {
echo "Usage: delalias <user@domain> <recipient@other>"
echo "Usage: delalias <alias@domain> <recipient@other>"
}
errex() {
@ -26,5 +26,5 @@ CNT=$(grep "^$EMAIL" $DATABASE | wc -w | awk '{print $1}')
if [[ $CNT -eq 2 ]]; then
sed -i "/^$EMAIL/d" $DATABASE
else
sed -i "/^$EMAIL/s/ $RECIPIENT,//g" $DATABASE
sed -i "/^$EMAIL/s/,$RECIPIENT//g" $DATABASE
fi

View file

@ -1,15 +1,15 @@
#! /bin/bash
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
USER="$1"
ALIAS_DATABASE="/tmp/docker-mailserver/postfix-virtual.cf"
usage() {
echo "Usage: delmailuser <user@domain>"
echo "Usage: delmailuser <-y> <user@domain> <user2@anotherdomain> ..."
echo " -y: don't prompt for confirmations"
}
errex() {
echo "$@" 1>&2
echo -e "$@" 1>&2
exit 1
}
@ -17,8 +17,41 @@ escape() {
echo "${1//./\\.}"
}
[ -z "$USER" ] && { usage; errex "No user specifed"; }
while getopts ":y" OPT; do
case $OPT in
y)
MAILDEL="y"
;;
\?)
usage; errex "Invalid option: -$OPTARG"
;;
esac
done
shift $((OPTIND-1))
[ -z "$@" ] && { usage; errex "No user specifed"; }
[ -s "$DATABASE" ] || exit 0
# XXX $USER must not contain /s and other syntactic characters
sed -i "/^$(escape "$USER")|/d" $DATABASE
for USER in "$@"; do
#very simple plausibility check
[[ "$USER" != *"@"*"."* ]] && errex "No valid address: $USER"
MAILARR=(${USER//@/ })
# XXX $USER must not contain /s and other syntactic characters
USER=$(escape "$USER")
sed -i "/^"$USER"|/d" $DATABASE
[ $? != 0 ] && errex "$USER couldn't be deleted in $DATABASE. $?"
# Delete all aliases where the user is the only recipient( " $USER$" )
# Delete user only for all aliases that deliver to multiple recipients ( ",$USER" "$USER," )
sed -i -e "/ "$USER"$/d" \
-e "s/,"$USER"//g" \
-e "s/"$USER",//g" $ALIAS_DATABASE
[ $? = 0 ] && echo "$USER and potential aliases deleted." || errex "Aliases for $USER couldn't be deleted in $ALIAS_DATABASE. $?"
if [ "$MAILDEL" != "y" ]; then
read -p "Do you want to delete the maildir as well(all mails will be removed)?(y/n) " MAILDEL
echo
fi
[ "$MAILDEL" != "y" ] && errex "Leaving the maildir untouched. If you want to delete it at a later point use \"sudo docker exec mail rm -R /var/mail/${MAILARR[1]}/${MAILARR[0]}\""
rm -r -f /var/mail/${MAILARR[1]}/${MAILARR[0]}
[ $? = 0 ] && echo "Maildir deleted." || errex "Maildir couldn't be deleted: $?"
done

View file

@ -3,7 +3,7 @@
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-accounts.cf}
USER="$1"
PASSWD="$2"
PASSWD=$(doveadm pw -s SHA512-CRYPT -u "$USER" -p "$2")
usage() {
echo "Usage: updatemailuser <user@domain.tld> [password]"
@ -22,6 +22,5 @@ escape() {
grep -qi "^$(escape "$USER")|" $DATABASE 2>/dev/null ||
errex "User \"$USER\" does not exist"
delmailuser "$USER"
addmailuser "$USER" "$PASSWD"
sed -i "s ^"$USER"|.* "$USER"|"$PASSWD" " $DATABASE
#sed -i "s/^"$(escape "$USER")"|*/"$USER"|"$(doveadm pw -s SHA512-CRYPT -u "$USER" -p "$PASSWD")"/" $DATABASE

@ -1 +1 @@
Subproject commit b42a6eb65dc1e059c8bf3661e51f3ce308469e19
Subproject commit e3e84ded29d88b2945c0782dbb43237c561a54a9

View file

@ -700,6 +700,8 @@ function _setup_postfix_aliases() {
echo -n > /etc/postfix/virtual
echo -n > /etc/postfix/regexp
if [ -f /tmp/docker-mailserver/postfix-virtual.cf ]; then
# fixing old virtual user file
[[ $(grep ",$" /tmp/docker-mailserver/postfix-virtual.cf) ]] && sed -i -e "s/, /,/g" -e "s/,$//g" /tmp/docker-mailserver/postfix-virtual.cf
# Copying virtual file
cp -f /tmp/docker-mailserver/postfix-virtual.cf /etc/postfix/virtual
while read from to

View file

@ -1056,7 +1056,7 @@ load 'test_helper/bats-assert/load'
}
@test "checking accounts: user3 should have been removed from /tmp/docker-mailserver/postfix-accounts.cf but not auser3" {
docker exec mail /bin/sh -c "delmailuser user3@domain.tld"
docker exec mail /bin/sh -c "delmailuser -y user3@domain.tld"
run docker exec mail /bin/sh -c "grep '^user3@domain\.tld' -i /tmp/docker-mailserver/postfix-accounts.cf"
assert_failure
@ -1082,7 +1082,7 @@ load 'test_helper/bats-assert/load'
status="1"
fi
docker exec mail /bin/sh -c "delmailuser auser3@domain.tld"
docker exec mail /bin/sh -c "delmailuser -y auser3@domain.tld"
assert_success
}
@ -1096,7 +1096,7 @@ load 'test_helper/bats-assert/load'
@test "checking accounts: no error is generated when deleting a user if /tmp/docker-mailserver/postfix-accounts.cf is missing" {
run docker run --rm \
-v "$(pwd)/test/config/without-accounts/":/tmp/docker-mailserver/ \
`docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c 'delmailuser user3@domain.tld'
`docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c 'delmailuser -y user3@domain.tld'
assert_success
[ -z "$output" ]
}
@ -1177,18 +1177,15 @@ load 'test_helper/bats-assert/load'
initialpass=$(cat ./config/postfix-accounts.cf | grep lorem@impsum.org | awk -F '|' '{print $2}')
run ./setup.sh -c mail email update lorem@impsum.org consectetur
updatepass=$(cat ./config/postfix-accounts.cf | grep lorem@impsum.org | awk -F '|' '{print $2}')
if [ initialpass != changepass ]; then
status="0"
else
status="1"
fi
assert_success
[ "$initialpass" != "$changepass" ]
}
@test "checking setup.sh: setup.sh email del" {
run ./setup.sh -c mail email del lorem@impsum.org
run ./setup.sh -c mail email del -y lorem@impsum.org
assert_success
run value=$(cat ./config/postfix-accounts.cf | grep lorem@impsum.org)
[ -z "$value" ]
run docker exec mail ls /var/mail/impsum.org/lorem
assert_failure
run grep lorem@impsum.org ./config/postfix-accounts.cf
assert_failure
}
@test "checking setup.sh: setup.sh email restrict" {
@ -1223,7 +1220,7 @@ load 'test_helper/bats-assert/load'
./setup.sh -c mail alias add test1@example.org test1@forward.com
./setup.sh -c mail alias add test1@example.org test2@forward.com
run /bin/sh -c 'cat ./config/postfix-virtual.cf | grep "test1@example.org test1@forward.com, test2@forward.com," | wc -l | grep 1'
run /bin/sh -c 'cat ./config/postfix-virtual.cf | grep "test1@example.org test1@forward.com,test2@forward.com" | wc -l | grep 1'
assert_success
}
@test "checking setup.sh: setup.sh alias del" {