From df23ae84db758100c308055709ebb656ff675ac8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Diemer?= <stephane.diemer@ubicast.eu>
Date: Wed, 9 Aug 2017 11:17:00 +0200
Subject: [PATCH] Configure sender address in postfix (refs #22099).

---
 2.Common_services/1.Postfix/0_setup.py | 34 +++++++++++++++++++-------
 2.Common_services/1.Postfix/main.cf    |  1 +
 2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/2.Common_services/1.Postfix/0_setup.py b/2.Common_services/1.Postfix/0_setup.py
index bdad62b6..3d3d13f9 100644
--- a/2.Common_services/1.Postfix/0_setup.py
+++ b/2.Common_services/1.Postfix/0_setup.py
@@ -12,25 +12,41 @@ def setup(interactive=True):
     else:
         raise Exception('Failed to get hostname.')
     # Install and configure postfix
-    server = utils.get_conf('EMAIL_SMTP_SERVER', '')
-    if not server:
-        # with relayless cases emails are not always delivered to google mailing lists unless mailname is ubicast.eu and DNS spf records are set
-        mailname = 'ubicast.eu'
-    else:
-        mailname = utils.get_conf('MS_SERVER_NAME', hostname)
     dir_path = utils.get_dir(__file__)
+    server = utils.get_conf('EMAIL_SMTP_SERVER', '')
     cmds = [
         'DEBIAN_FRONTEND=noninteractive apt-get install -y postfix mailutils',
-        'echo "Replacing /etc/postfix/main.cf"',
         dict(line='write', template='%s/main.cf' % dir_path, target='/etc/postfix/main.cf', params=(
             ('{{ hostname }}', hostname),
             ('{{ smtp }}', server),
         )),
+    ]
+    # Configure mail aliases
+    if not server:
+        # with relayless cases emails are not always delivered to google mailing lists unless mailname is ubicast.eu and DNS spf records are set
+        mailname = 'ubicast.eu'
+    else:
+        mailname = utils.get_conf('MS_SERVER_NAME')
+        if not mailname or mailname == 'mediaserver':
+            mailname = hostname
+    cmds.extend([
         'echo "%s" > /etc/mailname' % mailname,
         'rgrep "root:" /etc/aliases || echo "root: sysadmin@ubicast.eu" >> /etc/aliases',
-        'service postfix restart',
         'newaliases',
-    ]
+    ])
+    # Configure mail sender
+    sender = utils.get_conf('EMAIL_SENDER', '')
+    sender_domain = sender.split('@')[-1]
+    if sender_domain:
+        if sender_domain == 'ubicast.eu' and utils.get_conf('MS_SERVER_NAME', '') not in ('', 'mediaserver'):
+            sender_domain = utils.get_conf('MS_SERVER_NAME')
+        cmds.extend([
+            'rm -f /etc/postfix/generic',
+            'echo "root@localhost %s@%s" >> /etc/postfix/generic' % (hostname, sender_domain),
+            'echo "root@%s %s@%s" >> /etc/postfix/generic' % (hostname, hostname, sender_domain),
+            'postmap hash:/etc/postfix/generic',
+        ])
+    cmds.append('service postfix restart')
     utils.run_commands(cmds)
     # Setup authentication if any
     user = utils.get_conf('EMAIL_SMTP_USER')
diff --git a/2.Common_services/1.Postfix/main.cf b/2.Common_services/1.Postfix/main.cf
index c921849a..8504dbfe 100644
--- a/2.Common_services/1.Postfix/main.cf
+++ b/2.Common_services/1.Postfix/main.cf
@@ -40,3 +40,4 @@ inet_protocols = ipv4
 default_transport = smtp
 relay_transport = smtp
 disable_vrfy_command = yes
+smtp_generic_maps = hash:/etc/postfix/generic
-- 
GitLab