From 633d5810e8e44a1fb618280968c432d186aa44ef Mon Sep 17 00:00:00 2001 From: Jack Twilley Date: Tue, 23 Aug 2016 15:36:11 -0700 Subject: [PATCH] Updated commands to handle missing files better. The functional tests now pass. --- target/bin/addmailuser | 2 +- target/bin/delmailuser | 6 +++-- target/bin/generate-dkim-config | 39 ++++++++++++++++++++------------- 3 files changed, 29 insertions(+), 18 deletions(-) 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 -