---

# ansible_slackware_bootstrap
#
# File: slackware-mailconfigure.yaml
# Version 1.0
# Copyright Joey Kelly (joeykelly.net)
# March 9, 2016
# License: GPL version 2
#
# https://github.com/mmlj4/ansible_slackware_bootstrap


# now we need to configure our mail stack

- hosts: '{{ hosts }}'
  remote_user: root
  tasks:

    # let's set up our aliases file
    - copy: src=etc/aliases dest=/etc/aliases  owner=root group=root mode=0644
      register: aliases
    - command: /usr/bin/newaliases
      when: aliases.changed == True

    # amavisd-new
    - copy: src=etc/amavisd.conf dest=/etc/amavisd.conf  owner=root group=root mode=0644

    # dovecot
    # make our directory
    - file: path=/etc/dovecot/conf.d owner=root group=root mode=0775 state=directory
    # and copy up our files
    - copy: src=etc/dovecot/dovecot.conf                        dest=/etc/dovecot/dovecot.conf                        owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/10-auth.conf                 dest=/etc/dovecot/conf.d/10-auth.conf                 owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/10-director.conf             dest=/etc/dovecot/conf.d/10-director.conf             owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/10-logging.conf              dest=/etc/dovecot/conf.d/10-logging.conf              owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/10-mail.conf                 dest=/etc/dovecot/conf.d/10-mail.conf                 owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/10-master.conf               dest=/etc/dovecot/conf.d/10-master.conf               owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/10-ssl.conf                  dest=/etc/dovecot/conf.d/10-ssl.conf                  owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/15-lda.conf                  dest=/etc/dovecot/conf.d/15-lda.conf                  owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/15-mailboxes.conf            dest=/etc/dovecot/conf.d/15-mailboxes.conf            owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/20-imap.conf                 dest=/etc/dovecot/conf.d/20-imap.conf                 owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/20-lmtp.conf                 dest=/etc/dovecot/conf.d/20-lmtp.conf                 owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/20-pop3.conf                 dest=/etc/dovecot/conf.d/20-pop3.conf                 owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/90-acl.conf                  dest=/etc/dovecot/conf.d/90-acl.conf                  owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/90-plugin.conf               dest=/etc/dovecot/conf.d/90-plugin.conf               owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/90-quota.conf                dest=/etc/dovecot/conf.d/90-quota.conf                owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/auth-checkpassword.conf.ext  dest=/etc/dovecot/conf.d/auth-checkpassword.conf.ext  owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/auth-deny.conf.ext           dest=/etc/dovecot/conf.d/auth-deny.conf.ext           owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/auth-dict.conf.ext           dest=/etc/dovecot/conf.d/auth-dict.conf.ext           owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/auth-ldap.conf.ext           dest=/etc/dovecot/conf.d/auth-ldap.conf.ext           owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/auth-master.conf.ext         dest=/etc/dovecot/conf.d/auth-master.conf.ext         owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/auth-passwdfile.conf.ext     dest=/etc/dovecot/conf.d/auth-passwdfile.conf.ext     owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/auth-sql.conf.ext            dest=/etc/dovecot/conf.d/auth-sql.conf.ext            owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/auth-static.conf.ext         dest=/etc/dovecot/conf.d/auth-static.conf.ext         owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/auth-system.conf.ext         dest=/etc/dovecot/conf.d/auth-system.conf.ext         owner=root group=root mode=0644
    - copy: src=etc/dovecot/conf.d/auth-vpopmail.conf.ext       dest=/etc/dovecot/conf.d/auth-vpopmail.conf.ext       owner=root group=root mode=0644

    # postfix
    - copy: src=etc/postfix/main.cf dest=/etc/postfix/main.cf       owner=root group=root mode=0644
    - copy: src=etc/postfix/master.cf dest=/etc/postfix/master.cf   owner=root group=root mode=0644

    # SSL
    # NOTE: I am choosing dovecot/CentOS directory structure, which will keep us from clobbering any existing files
    # make our directories
    - file: path=/etc/ssl/dovecot           owner=root group=root mode=0775 state=directory
    - file: path=/etc/ssl/dovecot/certs     owner=root group=root mode=0775 state=directory
    - file: path=/etc/ssl/dovecot/private   owner=root group=root mode=0775 state=directory
    # and copy up our files
    - copy: src=etc/ssl/dovecot/dovecot-openssl.cnf   dest=/etc/ssl/dovecot/dovecot-openssl.cnf   owner=root group=root mode=0644
      register: sslconfig
    - copy: src=etc/ssl/dovecot/mkcert.sh             dest=/etc/ssl/dovecot/mkcert.sh             owner=root group=root mode=0744
    # if we haven't alreaady generated a self-signed SSL cert, let's do it now
    - stat: path=/etc/ssl/dovecot/certs/dovecot.pem
      register: pem
    - debug: msg="No self-signed SSl cert found, will try to generate one"
      when: pem.stat.exists == False
    - command: /etc/ssl/dovecot/mkcert.sh
      when: pem.stat.exists == False
      ignore_errors: True
    # we have to regenerate a cert if the config file has changed
    - file: path=/etc/ssl/dovecot/certs/dovecot.pem     state=absent
      when: sslconfig.changed == True
    - file: path=/etc/ssl/dovecot/private/dovecot.pem   state=absent
      when: sslconfig.changed == True
    - command: /etc/ssl/dovecot/mkcert.sh
      when: sslconfig.changed == True

    # let's update clamav's database while we're here
    - command: /usr/bin/freshclam
    # ditto spamassassin
    - command: /usr/bin/sa-update
    # not sure why this bombs, fix later
      ignore_errors: True

    # need to update our various postfix databases
    - command: /usr/sbin/postmap /etc/postfix/virtual
    - command: /usr/bin/touch /etc/postfix/relay_recipients
    - command: /usr/sbin/postmap /etc/postfix/relay_recipients

    # now we need to have our shiny new SBO services start at boot
    - command: chmod +x /etc/rc.d/rc.clamav
    - command: chmod +x /etc/rc.d/rc.amavisd-new
    - command: chmod +x /etc/rc.d/rc.dovecot
    - command: chmod +x /etc/rc.d/rc.postgrey
    - command: chmod +x /etc/rc.d/rc.postfix
    - shell: perl -e '$document = "here we start our SBO services"; print "\n# $document\n" unless `grep "$document" /etc/rc.d/rc.local`' >> /etc/rc.d/rc.local
    - shell: perl -e '$service = "clamav";       print "/etc/rc.d/rc.$service start\n" unless `grep $service /etc/rc.d/rc.local | grep -v "^#"`' >> /etc/rc.d/rc.local
    - shell: perl -e '$service = "amavisd-new";  print "/etc/rc.d/rc.$service start\n" unless `grep $service /etc/rc.d/rc.local | grep -v "^#"`' >> /etc/rc.d/rc.local
    - shell: perl -e '$service = "dovecot";      print "/etc/rc.d/rc.$service start\n" unless `grep $service /etc/rc.d/rc.local | grep -v "^#"`' >> /etc/rc.d/rc.local
    - shell: perl -e '$service = "postgrey";     print "/etc/rc.d/rc.$service start\n" unless `grep $service /etc/rc.d/rc.local | grep -v "^#"`' >> /etc/rc.d/rc.local
    - shell: perl -e '$service = "postfix";      print "/etc/rc.d/rc.$service start\n" unless `grep $service /etc/rc.d/rc.local | grep -v "^#"`' >> /etc/rc.d/rc.local

