From 356e16ed633b7f2388e1ed5691d9602f632b128f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Diemer?= <stephane.diemer@ubicast.eu>
Date: Tue, 16 May 2017 17:34:26 +0200
Subject: [PATCH] Added postfix relay auth support (refs #21500).

---
 2.Common_services/1.Postfix/0_setup.py | 20 ++++++++++++++++++++
 global-conf.sh                         |  2 ++
 2 files changed, 22 insertions(+)

diff --git a/2.Common_services/1.Postfix/0_setup.py b/2.Common_services/1.Postfix/0_setup.py
index 068002b6..bdad62b6 100644
--- a/2.Common_services/1.Postfix/0_setup.py
+++ b/2.Common_services/1.Postfix/0_setup.py
@@ -32,3 +32,23 @@ def setup(interactive=True):
         'newaliases',
     ]
     utils.run_commands(cmds)
+    # Setup authentication if any
+    user = utils.get_conf('EMAIL_SMTP_USER')
+    if user:
+        utils.log('Enabling authentication for SMTP relay.')
+        pwd = utils.get_conf('EMAIL_SMTP_PWD', '')
+        with open('/etc/postfix/sasl-passwords', 'w') as fo:
+            fo.write('%s %s:%s\n' % (server, user, pwd))
+        auth_conf = '''
+# SMTP relay authentication
+smtp_sasl_auth_enable = yes
+smtp_sasl_password_maps = hash:/etc/postfix/sasl-passwords
+smtp_sasl_security_options = noanonymous
+'''
+        with open('/etc/postfix/main.cf', 'a') as fo:
+            fo.write(auth_conf)
+        cmds = [
+            'postmap hash:/etc/postfix/sasl-passwords',
+            'service postfix restart',
+        ]
+        utils.run_commands(cmds)
diff --git a/global-conf.sh b/global-conf.sh
index 39dbec6e..06e10d88 100644
--- a/global-conf.sh
+++ b/global-conf.sh
@@ -35,6 +35,8 @@ SHELL_ADMIN_PWD=
 
 # -- Emails --
 EMAIL_SMTP_SERVER=
+EMAIL_SMTP_USER=
+EMAIL_SMTP_PWD=
 EMAIL_SENDER='support@ubicast.eu'
 # separate emails with comas in EMAIL_ADMINS
 EMAIL_ADMINS='sysadmin@ubicast.eu'
-- 
GitLab