import os from optparse import make_option from django.core.management.base import BaseCommand from orchestra.utils.system import run, check_root class Command(BaseCommand): def __init__(self, *args, **kwargs): super(Command, self).__init__(*args, **kwargs) self.option_list = BaseCommand.option_list + ( make_option('--db_name', dest='db_name', default='orchestra', help='Specifies the database to create.'), make_option('--db_user', dest='db_user', default='orchestra', help='Specifies the database to create.'), make_option('--db_password', dest='db_password', default='orchestra', help='Specifies the database to create.'), make_option('--db_host', dest='db_host', default='localhost', help='Specifies the database to create.'), make_option('--vmail_username', dest='vmail_username', default='vmail', help='Specifies username in the operating system (default=vmail).'), make_option('--vmail_uid', dest='vmail_uid', default='5000', help='UID of user (default=5000).'), make_option('--vmail_groupname', dest='vmail_groupname', default='vmail', help='Specifies the groupname in the operating system (default=vmail).'), make_option('--vmail_gid', dest='vmail_gid', default='5000', help='GID of user (default=5000).'), make_option('--vmail_home', dest='vmail_home', default='/var/vmail', help='$HOME of user (default=/var/vmail).'), make_option('--dovecot_dir', dest='dovecot_dir', default='/etc/dovecot', help='Dovecot root directory (default=/etc/dovecot).'), make_option('--postfix_dir', dest='postfix_dir', default='/etc/postfix', help='Postfix root directory (default=/etc/postfix).'), make_option('--amavis_dir', dest='amavis_dir', default='/etc/amavis', help='Amavis root directory (default=/etc/amavis).'), make_option('--noinput', action='store_false', dest='interactive', default=True, help='Tells Django to NOT prompt the user for input of any kind. ' 'You must use --username with --noinput, and must contain the ' 'cleeryd process owner, which is the user how will perform tincd updates'), ) option_list = BaseCommand.option_list help = 'Setup Postfix.' @check_root def handle(self, *args, **options): # Configure firmware generation context = { 'db_name': options.get('db_name'), 'db_user': options.get('db_user'), 'db_password': options.get('db_password'), 'db_host': options.get('db_host'), 'vmail_username': options.get('vmail_username'), 'vmail_uid': options.get('vmail_uid'), 'vmail_groupname': options.get('vmail_groupname'), 'vmail_gid': options.get('vmail_gid'), 'vmail_home': options.get('vmail_home'), 'dovecot_dir': options.get('dovecot_dir'), 'postfix_dir': options.get('postfix_dir'), 'amavis_dir': options.get('amavis_dir'), } file_name = '%(postfix_dir)s/pgsql-email2email.cf' % context run("#Processing %s" % file_name) pgsql_email2email = """user = %(db_user)s password = %(db_password)s hosts = %(db_host)s dbname = %(db_name)s query = SELECT mails_mailbox.emailname || '@' || names_domain.name as email FROM mails_mailbox INNER JOIN names_domain ON (mails_mailbox.domain_id = names_domain.id) WHERE mails_mailbox.emailname = '%%u' AND names_domain.name = '%%d' """ f = open(file_name, 'w') f.write(pgsql_email2email % context) f.close() run("chown root:postfix %s" % file_name) run("chmod 640 %s" % file_name) file_name = '%(postfix_dir)s/pgsql-virtual-alias-maps.cf' % context run("#Processing %s" % file_name) virtual_alias_maps = """user = %(db_user)s password = %(db_password)s hosts = %(db_host)s dbname = %(db_name)s query = SELECT mails_mailalias.destination FROM mails_mailalias INNER JOIN names_domain ON (mails_mailalias.domain_id = names_domain.id) WHERE mails_mailalias.emailname = '%%u' AND names_domain.name='%%d' """ f = open(file_name, 'w') f.write(virtual_alias_maps % context) f.close() run("chown root:postfix %s" % file_name) run("chmod 640 %s" % file_name) file_name = '%(postfix_dir)s/pgsql-virtual-mailbox-domains.cf' % context run("#Processing %s" % file_name) virtual_mailbox_domains = """user = %(db_user)s password = %(db_password)s hosts = %(db_host)s dbname = %(db_name)s query = SELECT 1 FROM names_domain WHERE names_domain.name='%%s' """ f = open(file_name, 'w') f.write(virtual_mailbox_domains % context) f.close() run("chown root:postfix %s" % file_name) run("chmod 640 %s" % file_name) file_name = '%(postfix_dir)s/pgsql-virtual-mailbox-maps.cf' % context run("#Processing %s" % file_name) virtual_mailbox_maps = """user = %(db_user)s password = %(db_password)s hosts = %(db_host)s dbname = %(db_name)s query = SELECT 1 FROM mails_mailbox INNER JOIN names_domain ON (mails_mailbox.domain_id = names_domain.id) WHERE mails_mailbox.emailname='%%u' AND names_domain.name='%%d' """ f = open(file_name, 'w') f.write(virtual_mailbox_maps % context) f.close() run("chown root:postfix %s" % file_name) run("chmod 640 %s" % file_name) #Dovecot vmail_usename = run("id -u %(vmail_username)s" % context) vmail_groupname = run("id -g %(vmail_groupname)s" % context) if vmail_groupname != context["vmail_gid"]: run("groupadd -g %(vmail_gid)s %(vmail_groupname)s" % context) run("chown -R %(vmail_username)s:%(vmail_groupname)s %(vmail_home)s" % context) if vmail_usename != context["vmail_uid"]: run("useradd -g %(vmail_groupname)s -u %(vmail_uid)s %(vmail_username)s -d %(vmail_home)s -m" % context) run("chmod u+w %(vmail_home)s" % context) run("chown -R %(vmail_username)s:%(vmail_groupname)s %(vmail_home)s" % context) run("chmod u+w %(vmail_home)s" % context) file_name = "%(dovecot_dir)s/conf.d/10-auth.conf" % context run("""sed -i "s/auth_mechanisms = plain$/auth_mechanisms = plain login/g" %s """ % file_name) run("""sed -i "s/\#\!include auth-sql.conf.ext/\!include auth-sql.conf.ext/" %s """ % file_name) file_name = "%(dovecot_dir)s/conf.d/auth-sql.conf.ext" % context run("#Processing %s" % file_name) auth_sql_conf_ext = """passdb { driver = sql args = %(dovecot_dir)s/dovecot-sql.conf.ext } userdb { driver = static args = uid=%(vmail_username)s gid=%(vmail_groupname)s home=%(vmail_home)s/%%d/%%n/Maildir allow_all_users=yes } """ f = open(file_name, 'w') f.write(auth_sql_conf_ext % context) f.close() file_name = "%(dovecot_dir)s/conf.d/10-mail.conf" % context run("#Processing %s" % file_name) mail_conf = """mail_location = maildir:%(vmail_home)s/%%d/%%n/Maildir namespace inbox { separator = . inbox = yes } """ f = open(file_name, 'w') f.write(mail_conf % context) f.close() file_name = "%(dovecot_dir)s/conf.d/10-master.conf" % context run("""sed -i "s/service auth {/service auth {\\n\\tunix_listener \/var\/spool\/postfix\/private\/auth {\\n\\t\\tmode = 0660\\n\\t\\tuser = postfix\\n\\t\\tgroup = postfix\\n\\t}\\n/g" %s """ % file_name) file_name = "%(dovecot_dir)s/conf.d/10-ssl.conf" % context run("#Processing %s" % file_name) ssl_conf = """ssl_cert =