#!/usr/bin/env python3 # -*- coding: utf-8 -*- import utils def setup(interactive=True): # Get hostname utils.log('Getting system hostname.') code, hostname = utils.exec_cmd('hostname') if code == 0: utils.log('Hostname is %s.' % hostname) else: raise Exception('Failed to get hostname.') # Install and configure postfix dir_path = utils.get_dir(__file__) server = utils.get_conf('EMAIL_SMTP_SERVER', '') cmds = [ 'DEBIAN_FRONTEND=noninteractive apt-get install -y postfix mailutils', 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', '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') if sender_domain == 'ubicast.eu' and utils.get_conf('CM_SERVER_NAME', '') not in ('', 'campusmanager'): sender_domain = utils.get_conf('CM_SERVER_NAME') if sender_domain == 'ubicast.eu' and utils.get_conf('MONITOR_SERVER_NAME', '') not in ('', 'monitor'): sender_domain = utils.get_conf('MONITOR_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), 'echo "@%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') 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)