From 0cb738084cdf3fccace39d11848b2497276129a2 Mon Sep 17 00:00:00 2001 From: Juan Luis Baptiste Date: Wed, 9 May 2018 02:06:30 -0500 Subject: [PATCH] Don't add duplicate configuration entries. --- run.sh | 53 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/run.sh b/run.sh index e0c9462..310b84f 100644 --- a/run.sh +++ b/run.sh @@ -1,27 +1,48 @@ #!/bin/bash +[ "${DEBUG}" == "yes" ] && set -x + +function add_config_value() { + local key=${1} + local value=${2} + local config_file=${3:-/etc/postfix/main.cf} + [ "${key}" == "" ] && echo "ERROR: No key set !!" && exit 1 + [ "${value}" == "" ] && echo "ERROR: No value set !!" && exit 1 + + echo "Setting configuration option ${key} with value: ${value}" + sed -i -e "/^#\?\(\s*${key}\s*=\s*\).*/{s//\1${value}/;:a;n;:ba;q}" \ + -e "\$a${key}=${value}" \ + ${config_file} +} + [ -z "${SMTP_SERVER}" ] && echo "SMTP_SERVER is not set" && exit 1 [ -z "${SMTP_USERNAME}" ] && echo "SMTP_USERNAME is not set" && exit 1 [ -z "${SMTP_PASSWORD}" ] && echo "SMTP_PASSWORD is not set" && exit 1 [ -z "${SERVER_HOSTNAME}" ] && echo "SERVER_HOSTNAME is not set" && exit 1 #Get the domain from the server host name -DOMAIN=`echo $SERVER_HOSTNAME |awk -F. '{$1="";OFS="." ; print $0}' | sed 's/^.//'` +DOMAIN=`echo ${SERVER_HOSTNAME} |awk -F. '{$1="";OFS="." ; print $0}' | sed 's/^.//'` -#Comment default mydestination, we will set it bellow -sed -i -e '/mydestination/ s/^#*/#/' /etc/postfix/main.cf +# Set needed config options +add_config_value "myhostname" ${SERVER_HOSTNAME} +add_config_value "mydomain" ${DOMAIN} +add_config_value "mydestination" '$myhostname' +add_config_value "myorigin" '$mydomain' +add_config_value "relayhost" "[${SMTP_SERVER}]:587" +add_config_value "smtp_use_tls" "yes" +add_config_value "smtp_sasl_auth_enable" "yes" +add_config_value "smtp_sasl_password_maps" "hash:\/etc\/postfix\/sasl_passwd" +add_config_value "smtp_sasl_security_options" "noanonymous" -echo "myhostname=$SERVER_HOSTNAME" >> /etc/postfix/main.cf -echo "mydomain=$DOMAIN" >> /etc/postfix/main.cf -echo 'mydestination=$myhostname' >> /etc/postfix/main.cf -echo 'myorigin=$mydomain' >> /etc/postfix/main.cf -echo "relayhost = [$SMTP_SERVER]:587" >> /etc/postfix/main.cf -echo "smtp_use_tls=yes" >> /etc/postfix/main.cf -echo "smtp_sasl_auth_enable = yes" >> /etc/postfix/main.cf -echo "smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd" >> /etc/postfix/main.cf -echo "smtp_sasl_security_options = noanonymous" >> /etc/postfix/main.cf +# Create sasl_passwd file with auth credentials +if [ ! -f /etc/postfix/sasl_passwd ]; then + grep -q "${SMTP_SERVER}" /etc/postfix/sasl_passwd > /dev/null 2>&1 + if [ $? -gt 0 ]; then + echo "Adding SASL authentication configuration" + echo "[${SMTP_SERVER}]:587 ${SMTP_USERNAME}:${SMTP_PASSWORD}" >> /etc/postfix/sasl_passwd + postmap /etc/postfix/sasl_passwd + fi +fi -echo "[$SMTP_SERVER]:587 $SMTP_USERNAME:$SMTP_PASSWORD" >> /etc/postfix/sasl_passwd -postmap /etc/postfix/sasl_passwd - -supervisord \ No newline at end of file +#Start services +supervisord