diff --git a/.gitignore b/.gitignore index fc55b270..bc5326d5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ docker-compose.yml .idea test/config/empty/ +test/config/without-accounts/ +test/config/without-virtual/ test/config/postfix-accounts.cf test/config/letsencrypt/mail.my-domain.com/combined.pem test/onedir diff --git a/target/bin/addmailuser b/target/bin/addmailuser index 6aeac44f..81ba0af0 100755 --- a/target/bin/addmailuser +++ b/target/bin/addmailuser @@ -9,7 +9,7 @@ function usage { if [ ! -z "$1" ]; then USER=$1 - if [ ! -z "$(grep $USER -i $DATABASE)" ]; then + if [ -e "$DATABASE" ] && [ ! -z "$(grep $USER -i $DATABASE)" ]; then echo "User already exists" exit 1 fi diff --git a/target/bin/delmailuser b/target/bin/delmailuser index dc471b43..85bba7b6 100755 --- a/target/bin/delmailuser +++ b/target/bin/delmailuser @@ -9,8 +9,10 @@ function usage { if [ ! -z "$1" ]; then USER=$1 - ENTRIES=$(grep "$USER" -vi $DATABASE) - echo "$ENTRIES" > $DATABASE + if [ -f "$DATABASE" ]; then + ENTRIES=$(grep "$USER" -vi $DATABASE) + echo "$ENTRIES" > $DATABASE + fi else usage fi diff --git a/target/bin/generate-dkim-config b/target/bin/generate-dkim-config index f2f8eeed..ffcff2b2 100755 --- a/target/bin/generate-dkim-config +++ b/target/bin/generate-dkim-config @@ -3,27 +3,37 @@ touch /tmp/vhost.tmp # Getting domains from mail accounts -while IFS=$'|' read login pass -do - domain=$(echo ${login} | cut -d @ -f2) - echo ${domain} >> /tmp/vhost.tmp -done < /tmp/docker-mailserver/postfix-accounts.cf +if [ -f /tmp/docker-mailserver/postfix-accounts.cf ]; then + while IFS=$'|' read login pass + do + domain=$(echo ${login} | cut -d @ -f2) + echo ${domain} >> /tmp/vhost.tmp + done < /tmp/docker-mailserver/postfix-accounts.cf +fi # Getting domains from mail aliases -while read from to -do - # Setting variables for better readability - uname=$(echo ${from} | cut -d @ -f1) - domain=$(echo ${from} | cut -d @ -f2) - # if they are equal it means the line looks like: "user1 other@domain.tld" - test "$uname" != "$domain" && echo ${domain} >> /tmp/vhost.tmp -done < /tmp/docker-mailserver/postfix-virtual.cf +if [ -f /tmp/docker-mailserver/postfix-virtual.cf ]; then + while read from to + do + # Setting variables for better readability + uname=$(echo ${from} | cut -d @ -f1) + domain=$(echo ${from} | cut -d @ -f2) + # if they are equal it means the line looks like: "user1 other@domain.tld" + test "$uname" != "$domain" && echo ${domain} >> /tmp/vhost.tmp + done < /tmp/docker-mailserver/postfix-virtual.cf +fi # Keeping unique entries if [ -f /tmp/vhost.tmp ]; then cat /tmp/vhost.tmp | sort | uniq > /tmp/vhost && rm /tmp/vhost.tmp fi +# Exit if no entries found +if [ ! -f /tmp/vhost ]; then + echo "No entries found, no keys to make" + exit 0 +fi + grep -vE '^(\s*$|#)' /tmp/vhost | while read domainname; do mkdir -p /tmp/docker-mailserver/opendkim/keys/$domainname @@ -56,9 +66,8 @@ grep -vE '^(\s*$|#)' /tmp/vhost | while read domainname; do done # Creates TrustedHosts if missing -if [ ! -f "/tmp/docker-mailserver/opendkim/TrustedHosts" ]; then +if [ -d "/tmp/docker-mailserver/opendkim" ] && [ ! -f "/tmp/docker-mailserver/opendkim/TrustedHosts" ]; then echo "Creating DKIM TrustedHosts"; echo "127.0.0.1" > /tmp/docker-mailserver/opendkim/TrustedHosts echo "localhost" >> /tmp/docker-mailserver/opendkim/TrustedHosts fi - diff --git a/test/tests.bats b/test/tests.bats index c5e2b5c1..1041a98a 100644 --- a/test/tests.bats +++ b/test/tests.bats @@ -328,6 +328,62 @@ [ "$output" -eq 4 ] } +@test "checking opendkim: generator creates keys, tables and TrustedHosts without postfix-accounts.cf" { + rm -rf "$(pwd)/test/config/without-accounts" && mkdir -p "$(pwd)/test/config/without-accounts" + run docker run --rm \ + -v "$(pwd)/test/config/without-accounts/":/tmp/docker-mailserver/ \ + -v "$(pwd)/test/config/postfix-virtual.cf":/tmp/docker-mailserver/postfix-virtual.cf \ + `docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c 'generate-dkim-config | wc -l' + [ "$status" -eq 0 ] + [ "$output" -eq 5 ] + # Check keys for localhost.localdomain + run docker run --rm \ + -v "$(pwd)/test/config/without-accounts/opendkim":/etc/opendkim \ + `docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c 'ls -1 /etc/opendkim/keys/localhost.localdomain/ | wc -l' + [ "$status" -eq 0 ] + [ "$output" -eq 2 ] + # Check keys for otherdomain.tld + # run docker run --rm \ + # -v "$(pwd)/test/config/without-accounts/opendkim":/etc/opendkim \ + # `docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c 'ls -1 /etc/opendkim/keys/otherdomain.tld | wc -l' + # [ "$status" -eq 0 ] + # [ "$output" -eq 0 ] + # Check presence of tables and TrustedHosts + run docker run --rm \ + -v "$(pwd)/test/config/without-accounts/opendkim":/etc/opendkim \ + `docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c "ls -1 etc/opendkim | grep -E 'KeyTable|SigningTable|TrustedHosts|keys'|wc -l" + [ "$status" -eq 0 ] + [ "$output" -eq 4 ] +} + +@test "checking opendkim: generator creates keys, tables and TrustedHosts without postfix-virtual.cf" { + rm -rf "$(pwd)/test/config/without-virtual" && mkdir -p "$(pwd)/test/config/without-virtual" + run docker run --rm \ + -v "$(pwd)/test/config/without-virtual/":/tmp/docker-mailserver/ \ + -v "$(pwd)/test/config/postfix-accounts.cf":/tmp/docker-mailserver/postfix-accounts.cf \ + `docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c 'generate-dkim-config | wc -l' + [ "$status" -eq 0 ] + [ "$output" -eq 5 ] + # Check keys for localhost.localdomain + run docker run --rm \ + -v "$(pwd)/test/config/without-virtual/opendkim":/etc/opendkim \ + `docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c 'ls -1 /etc/opendkim/keys/localhost.localdomain/ | wc -l' + [ "$status" -eq 0 ] + [ "$output" -eq 2 ] + # Check keys for otherdomain.tld + run docker run --rm \ + -v "$(pwd)/test/config/without-virtual/opendkim":/etc/opendkim \ + `docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c 'ls -1 /etc/opendkim/keys/otherdomain.tld | wc -l' + [ "$status" -eq 0 ] + [ "$output" -eq 2 ] + # Check presence of tables and TrustedHosts + run docker run --rm \ + -v "$(pwd)/test/config/without-virtual/opendkim":/etc/opendkim \ + `docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c "ls -1 etc/opendkim | grep -E 'KeyTable|SigningTable|TrustedHosts|keys'|wc -l" + [ "$status" -eq 0 ] + [ "$output" -eq 4 ] +} + # # opendmarc # @@ -535,9 +591,29 @@ [ -z "$output" ] } +@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' + [ "$status" -eq 0 ] + [ -z "$output" ] +} + +@test "checking accounts: user3 should have been added to /tmp/docker-mailserver/postfix-accounts.cf even when that file does not exist" { + run docker run --rm \ + -v "$(pwd)/test/config/without-accounts/":/tmp/docker-mailserver/ \ + `docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c 'addmailuser user3@domain.tld mypassword' + run docker run --rm \ + -v "$(pwd)/test/config/without-accounts/":/tmp/docker-mailserver/ \ + `docker inspect --format '{{ .Config.Image }}' mail` /bin/sh -c 'grep user3@domain.tld -i /tmp/docker-mailserver/postfix-accounts.cf' + [ "$status" -eq 0 ] + [ ! -z "$output" ] +} + # # PERMIT_DOCKER mynetworks # + @test "checking PERMIT_DOCKER: can get container ip" { run docker exec mail /bin/sh -c "ip addr show eth0 | grep 'inet ' | sed 's/[^0-9\.\/]*//g' | cut -d '/' -f 1 | egrep '[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}'" [ "$status" -eq 0 ]