mmv

mailserver bootstrap script
git clone git://git.yotsev.xyz/mmv.git
Log | Files | Refs | README | LICENSE

setup.sh (3778B)


      1 #!/bin/sh
      2 
      3 domain=$1
      4 maildom="mail.$domain"
      5 
      6 replace() { \
      7 sed "s/<domain>/$domain/g;s/<maildom>/$maildom/g" $1
      8 }
      9 
     10 success() { \
     11     echo "\033[1;32m==================================================="
     12     echo "$@"
     13     echo "===================================================\033[0m"
     14 }
     15 
     16 failure() { \
     17     echo "\033[1;31m==================================================="
     18     echo "$@"
     19     echo "\033[0m==================================================="
     20 }
     21 
     22 #
     23 # install required software
     24 #
     25 
     26 pkg_add opensmtpd-extras opensmtpd-filter-rspamd dovecot dovecot-pigeonhole rspamd redis sieve &&
     27 
     28 success "Installed required software" &&
     29 
     30 #
     31 # certs
     32 #
     33 
     34 replace files/acme-client.conf >> /etc/acme-client.conf &&
     35 
     36 replace files/httpd.conf >> /etc/httpd.conf &&
     37 
     38 rcctl enable httpd &&
     39 rcctl start httpd &&
     40 
     41 acme-client -v $maildom &&
     42 
     43 replace files/daily.local >> /etc/daily.local &&
     44 
     45 success "Created and signed tls certificates (letencrypt)" &&
     46 
     47 #
     48 # vmail user & authentication
     49 #
     50 
     51 touch /etc/mail/credentials &&
     52 chmod 0440 /etc/mail/credentials &&
     53 chown _smtpd:_dovecot /etc/mail/credentials &&
     54 useradd -c "Virtual Mail Account" -d /var/vmail -s /sbin/nologin \
     55     -u 2000 -g =uid -L staff vmail &&
     56 mkdir -p /var/vmail &&
     57 chown vmail:vmail /var/vmail &&
     58 
     59 replace files/virtuals >> /etc/mail/virtuals &&
     60 replace files/newuser > ./newuser &&
     61 chmod +x ./newuser &&
     62 
     63 success "Created vmail user & authentication file" &&
     64 
     65 #
     66 # smtpd
     67 #
     68 
     69 replace files/smtpd.conf > /etc/mail/smtpd.conf &&
     70 
     71 success "Configured OpenSMTPD" &&
     72 
     73 #
     74 # dovecot
     75 #
     76 
     77 echo "dovecot:\\
     78         :openfiles-cur=1024:\\
     79         :openfiles-max=2048:\\
     80         :tc=daemon:
     81 " >> /etc/login.conf &&
     82 
     83 replace files/local.conf > /etc/dovecot/local.conf &&
     84 
     85 sed "s/^ssl_cert/#ssl_cert/;s/^ssl_key/#ssl_key/" \
     86 	/etc/dovecot/conf.d/10-ssl.conf > tempfile &&
     87 mv tempfile /etc/dovecot/conf.d/10-ssl.conf &&
     88 
     89 # setup training rspamd from email moving in and out of the Junk folder
     90 
     91 mkdir -p /usr/local/lib/dovecot/sieve &&
     92 cp files/report-ham.sieve /usr/local/lib/dovecot/sieve &&
     93 cp files/report-spam.sieve /usr/local/lib/dovecot/sieve &&
     94 sievec /usr/local/lib/dovecot/sieve/report-ham.sieve &&
     95 sievec /usr/local/lib/dovecot/sieve/report-spam.sieve &&
     96 
     97 cp files/sa-learn-ham.sh /usr/local/lib/dovecot/sieve/ &&
     98 cp files/sa-learn-spam.sh /usr/local/lib/dovecot/sieve/ &&
     99 chmod 0755 /usr/local/lib/dovecot/sieve/sa-learn-ham.sh &&
    100 chmod 0755 /usr/local/lib/dovecot/sieve/sa-learn-spam.sh &&
    101 
    102 rcctl enable dovecot &&
    103 rcctl start dovecot &&
    104 
    105 success "Configured Dovecot" &&
    106 
    107 #
    108 # rspamd
    109 #
    110 
    111 mkdir -p /etc/mail/dkim &&
    112 openssl genrsa -out /etc/mail/dkim/$domain.key 1024 &&
    113 openssl rsa -in /etc/mail/dkim/$domain.key \
    114 	    -pubout -out /etc/mail/dkim/public.key &&
    115 chmod 0440 /etc/mail/dkim/$domain.key &&
    116 chown root:_rspamd /etc/mail/dkim/$domain.key &&
    117 
    118 replace files/dkim_signing.conf > /etc/rspamd/local.d/dkim_signing.conf &&
    119 
    120 rcctl enable redis rspamd &&
    121 rcctl start redis rspamd &&
    122 rcctl restart smtpd &&
    123 
    124 success "Configured rspamd" &&
    125 
    126 #
    127 # dns
    128 #
    129 
    130 pub_key=$(grep -v -e "---" /etc/mail/dkim/public.key | tr -d '\n' ) &&
    131 
    132 mkdir -p dns &&
    133 echo "mail._domainkey.$domain. IN TXT \"v=DKIM1;k=rsa;p=$pub_key\"" > ./dns/dkim-record &&
    134 echo "$domain. IN TXT \"v=spf1 mx -all\"" > ./dns/spf-record &&
    135 echo "_dmarc.$domain. IN TXT \"v=DMARC1;p=none;pct=100;rua=mailto:postmaster@$domain\"" > ./dns/dmarc-record &&
    136 
    137 success "Wrote relevant dns records in ./dns/" &&
    138 # TODO: does .forward work with virtual users?
    139 success \
    140 "The creation of an admin account is required for this setup! Email to
    141 it can be forwarded to an email address written in:
    142 /var/vmail/$domain/admin/.forward
    143 New users can be similarly added by running ./newuser. Please use the
    144 username \"admin\" and a password of your choosing" &&
    145 
    146 ./newuser &&
    147 rcctl restart smtpd