Issue 538 (#541)

* adapted setup.sh to handle email aliases
* added needed scripts for alias management
* added integration tests
This commit is contained in:
Thomas A. Kilian 2017-04-17 18:27:28 +02:00 committed by Thomas VIAL
parent 7753d59d72
commit a144f3811c
6 changed files with 122 additions and 0 deletions

1
.gitignore vendored
View file

@ -7,3 +7,4 @@ test/config/without-virtual/
test/config/postfix-accounts.cf test/config/postfix-accounts.cf
test/config/letsencrypt/mail.my-domain.com/combined.pem test/config/letsencrypt/mail.my-domain.com/combined.pem
test/onedir test/onedir
config/opendkim/

View file

@ -47,6 +47,11 @@ SUBCOMMANDS:
$0 email del <email> $0 email del <email>
$0 email list $0 email list
alias:
$0 alias add <email> <recipient>
$0 alias del <email> <recipient>
$0 alias list
config: config:
$0 config dkim $0 config dkim
@ -133,6 +138,27 @@ case $1 in
esac esac
;; ;;
alias)
shift
case $1 in
add)
shift
_docker_image addalias $@
;;
del)
shift
_docker_image delalias $@
;;
list)
shift
_docker_image listalias $@
;;
*)
_usage
;;
esac
;;
config) config)
shift shift
case $1 in case $1 in

30
target/bin/addalias Executable file
View file

@ -0,0 +1,30 @@
#! /bin/bash
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-virtual.cf}
EMAIL="$1"
RECIPIENT="$2"
usage() {
echo "Usage: addalias <user@domain> <recipient@other>"
}
errex() {
echo "$@" 1>&2
exit 1
}
escape() {
echo "${1//./\\.}"
}
[ -z "$EMAIL" ] && { usage; errex "no email specified"; }
grep -qi "^$(escape $EMAIL)[a-zA-Z@.\ ]*$(escape $RECIPIENT)" $DATABASE 2>/dev/null &&
errex "Alias \"$EMAIL $RECIPIENT\" already exists"
if grep -qi "^$(escape $EMAIL)" $DATABASE 2>/dev/null; then
sed -i "/$EMAIL/s/$/ $RECIPIENT,/" $DATABASE
else
echo "$EMAIL $RECIPIENT," >> $DATABASE
fi

30
target/bin/delalias Executable file
View file

@ -0,0 +1,30 @@
#! /bin/bash
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-virtual.cf}
EMAIL="$1"
RECIPIENT="$2"
usage() {
echo "Usage: delalias <user@domain> <recipient@other>"
}
errex() {
echo "$@" 1>&2
exit 1
}
escape() {
echo "${1//./\\.}"
}
[ -z "$EMAIL" ] || [ -z "$RECIPIENT" ] && { usage; errex "No email specifed"; }
[ -s "$DATABASE" ] || exit 0
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
fi

13
target/bin/listalias Executable file
View file

@ -0,0 +1,13 @@
#! /bin/bash
DATABASE=${DATABASE:-/tmp/docker-mailserver/postfix-virtual.cf}
errex() {
echo "$@" 1>&2
exit 1
}
[ -f $DATABASE ] || errex "No postfix-virtual.cf file"
[ -s $DATABASE ] || errex "Empty postfix-virtual.cf - no aliases have been added"
cat $DATABASE

View file

@ -936,6 +936,28 @@ load 'test_helper/bats-assert/load'
[ -z "$value" ] [ -z "$value" ]
} }
# alias
@test "checking setup.sh: setup.sh alias list" {
echo "test@example.org test@forward.com" > ./config/postfix-virtual.cf
run ./setup.sh -c mail alias list
assert_success
}
@test "checking setup.sh: setup.sh alias add" {
echo "" > ./config/postfix-virtual.cf
./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'
assert_success
}
@test "checking setup.sh: setup.sh alias del" {
echo 'test1@example.org test1@forward.com, test2@forward.com,' > ./config/postfix-virtual.cf
./setup.sh -c mail alias del test1@example.org test1@forward.com
./setup.sh -c mail alias del test1@example.org test2@forward.com
run cat ./config/postfix-virtual.cf | wc -l | grep 0
assert_success
}
# config # config
@test "checking setup.sh: setup.sh config dkim" { @test "checking setup.sh: setup.sh config dkim" {
run ./setup.sh -c mail config dkim run ./setup.sh -c mail config dkim