mmv

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

mmv (5573B)


      1 #!/bin/sh
      2 
      3 domain=$1
      4 maildom="mail.$domain"
      5 failed="false"
      6 
      7 replace() { \
      8 sed "s/<domain>/$domain/g;s/<maildom>/$maildom/g" $1
      9 }
     10 
     11 success() { \
     12     echo "\033[1;32m========================================================================"
     13     echo "$@"
     14     echo "========================================================================\033[0m"
     15 }
     16 
     17 failure() { \
     18     echo "\033[1;31m========================================================================"
     19     echo "$@"
     20     echo "========================================================================\033[0m"
     21     failed="true"
     22 }
     23 
     24 # first use?
     25 if [ ! -f /etc/mail/maindom ]; then
     26 
     27 #
     28 # register domain
     29 #
     30 
     31 echo "$domain" > /etc/mail/maindom &&
     32 echo "$domain" >> /etc/mail/domains &&
     33 success "Setting up email for the first time"
     34 
     35 #
     36 # install required software
     37 #
     38 
     39 pkg_add -u &&
     40 pkg_add -I opensmtpd-extras opensmtpd-filter-rspamd dovecot \
     41     dovecot-pigeonhole rspamd-- redis tor &&
     42 
     43 success "Installed required software" ||
     44 failure "Failed to install required software"
     45 
     46 #
     47 # certs
     48 #
     49 
     50 replace files/acme-client.conf >> /etc/acme-client.conf &&
     51 
     52 replace files/httpd.conf >> /etc/httpd.conf &&
     53 
     54 rcctl enable httpd &&
     55 rcctl start httpd &&
     56 
     57 acme-client -v $maildom &&
     58 
     59 replace files/daily.local >> /etc/daily.local &&
     60 
     61 success "Created and signed tls certificates (letsencrypt)" ||
     62 failure "Failed to sign tls certificates (letsencrypt)"
     63 
     64 #
     65 # vmail user & authentication
     66 #
     67 
     68 touch /etc/mail/credentials &&
     69 chmod 0440 /etc/mail/credentials &&
     70 chown _smtpd:_dovecot /etc/mail/credentials &&
     71 useradd -c "Virtual Mail Account" -d /var/vmail -s /sbin/nologin \
     72     -u 2000 -g =uid -L staff vmail &&
     73 mkdir -p /var/vmail &&
     74 chown vmail:vmail /var/vmail &&
     75 
     76 replace files/virtuals >> /etc/mail/virtuals &&
     77 
     78 success "Created vmail user & authentication file" ||
     79 failure "Failed to createvmail user & authentication file"
     80 
     81 #
     82 # smtpd
     83 #
     84 
     85 replace files/smtpd.conf > /etc/mail/smtpd.conf &&
     86 
     87 cp files/madduser /usr/local/bin/ &&
     88 cp files/mdeluser /usr/local/bin/ &&
     89 cp files/mpasswd /usr/local/bin/ &&
     90 
     91 rcctl restart smtpd &&
     92 
     93 success "Configured OpenSMTPD" ||
     94 failure "Failed to configure OpenSMTPD"
     95 
     96 #
     97 # dovecot
     98 #
     99 
    100 echo "dovecot:\\
    101         :openfiles-cur=1024:\\
    102         :openfiles-max=2048:\\
    103         :tc=daemon:
    104 " >> /etc/login.conf &&
    105 
    106 replace files/local.conf > /etc/dovecot/local.conf &&
    107 
    108 sed "s/^ssl_cert/#ssl_cert/;s/^ssl_key/#ssl_key/" \
    109 	/etc/dovecot/conf.d/10-ssl.conf > tempfile &&
    110 mv tempfile /etc/dovecot/conf.d/10-ssl.conf &&
    111 
    112 # setup training rspamd from email moving in and out of the Junk folder
    113 
    114 mkdir -p /usr/local/lib/dovecot/sieve &&
    115 cp files/report-ham.sieve /usr/local/lib/dovecot/sieve &&
    116 cp files/report-spam.sieve /usr/local/lib/dovecot/sieve &&
    117 sievec /usr/local/lib/dovecot/sieve/report-ham.sieve &&
    118 sievec /usr/local/lib/dovecot/sieve/report-spam.sieve &&
    119 
    120 cp files/sa-learn-ham.sh /usr/local/lib/dovecot/sieve/ &&
    121 cp files/sa-learn-spam.sh /usr/local/lib/dovecot/sieve/ &&
    122 chmod 0755 /usr/local/lib/dovecot/sieve/sa-learn-ham.sh &&
    123 chmod 0755 /usr/local/lib/dovecot/sieve/sa-learn-spam.sh &&
    124 
    125 rcctl enable dovecot &&
    126 rcctl start dovecot &&
    127 
    128 success "Configured Dovecot" ||
    129 failure "Failed to configure Dovecot"
    130 
    131 #
    132 # rspamd
    133 #
    134 
    135 mkdir -p /etc/mail/dkim &&
    136 openssl genrsa -out /etc/mail/dkim/$domain.key 1024 &&
    137 openssl rsa -in /etc/mail/dkim/$domain.key \
    138 	    -pubout -out /etc/mail/dkim/public.key &&
    139 chmod 0440 /etc/mail/dkim/$domain.key &&
    140 chown root:_rspamd /etc/mail/dkim/$domain.key &&
    141 
    142 replace files/dkim_signing.conf > /etc/rspamd/local.d/dkim_signing.conf &&
    143 
    144 rcctl enable redis rspamd &&
    145 rcctl start redis rspamd &&
    146 rcctl restart smtpd &&
    147 
    148 success "Configured rspamd" ||
    149 failure "Failed to configure rspamd"
    150 
    151 #
    152 # tor hidden service
    153 #
    154 
    155 patch /etc/tor/torrc files/torrc.diff &&
    156 
    157 rcctl enable tor &&
    158 rcctl start tor &&
    159 
    160 success "Configured tor, see address from /etc/mail/hidden-service/hostname" ||
    161 failure "Failed to configure tor"
    162 
    163 #
    164 # dns
    165 #
    166 
    167 pub_key=$(grep -v -e "---" /etc/mail/dkim/public.key | tr -d '\n' ) &&
    168 mkdir -p /etc/mail/dns/$domain &&
    169 echo "mail._domainkey.$domain. IN TXT \"v=DKIM1;k=rsa;p=$pub_key\"" > /etc/mail/dns/$domain/dkim &&
    170 echo "$domain. IN TXT \"v=spf1 mx -all\"" > /etc/mail/dns/$domain/spf &&
    171 echo "_dmarc.$domain. IN TXT \"v=DMARC1;p=quarantine;pct=100;ruf=mailto:postmaster@$domain\"" > /etc/mail/dns/$domain/dmarc &&
    172 echo "$domain. IN MX 0 $maildom." > /etc/mail/dns/$domain/mx &&
    173 success "Wrote relevant dns records in /etc/mail/dns/$domain/" ||
    174 failure "Failed to write relevant dns records in /etc/mail/dns/$domain/"
    175 
    176 #
    177 # setup admin account
    178 #
    179 
    180 # TODO: does .forward work with virtual users?
    181 
    182 success \
    183 "The creation of an admin account is required for this setup! Email to
    184 it can be forwarded to an email address written in:
    185 /var/vmail/$domain/admin/.forward
    186 Choose a password for the \"admin\" user." &&
    187 
    188 madduser admin@$domain
    189 
    190 else
    191 
    192 #
    193 # register domain
    194 #
    195 
    196 echo "$domain" >> /etc/mail/domains
    197 success "Adding domain $domain to existing setup"
    198 
    199 #
    200 # dns
    201 #
    202 
    203 maindom="$(cat /etc/mail/maindom)"
    204 
    205 mkdir /etc/mail/dns/$domain
    206 echo "$domain. IN MX 0 mail.$maindom." > /etc/mail/dns/$domain/mx &&
    207 echo "$domain. IN TXT \"v=spf1 mx -all\"" > /etc/mail/dns/$domain/spf &&
    208 echo "_dmarc.$domain. IN TXT \"v=DMARC1;p=quarantine;pct=100;ruf=mailto:postmaster@$maindom\"" > /etc/mail/dns/$domain/dmarc &&
    209 success "Wrote relevant dns records in /etc/mail/dns/$domain/" ||
    210 failure "Failed to write relevant dns records in /etc/mail/dns/$domain/"
    211 
    212 fi
    213 
    214 #
    215 # final status
    216 #
    217 
    218 [ $failed = "false" ] &&
    219 success "Done!" ||
    220 failure "Installation incomplete, check the errors and correct them!"