home · contact · privacy
edd53194624ccd343e1d685f1dacd037c3350830
[config] / all_new_2018 / setup_mail.sh
1 #/bin/sh
2 set -e
3
4 if [ $# -lt 2 ]; then
5     echo "Give arguments of mail domain and DKIM selector."
6     echo "Also, if hosting mail for entire domain, give third argument 'domainwide'."
7     false
8 fi
9 mail_domain="$1"
10 dkim_selector="$2"
11 domainwide="$3"
12
13 # Set up DKIM key if necessary.
14 mkdir -p /etc/dkimkeys/
15 add_dkim_record=0
16 if [ ! -f "/etc/dkimkeys/${dkim_selector}.private" ]; then
17     add_dkim_record=1
18     set +e
19     dpkg -s opendkim-tools &> /dev/null
20     preinstalled="$?"
21     set -e
22     if [ ! "${preinstalled}" -eq "0" ]; then
23         apt install -y opendkim-tools
24     fi
25     opendkim-genkey -s "${dkim_selector}"
26     mv "${dkim_selector}.private" /etc/dkimkeys/
27     if [ ! "${preinstalled}" -eq "0" ]; then
28         apt -y --purge autoremove opendkim-tools
29     fi
30 fi
31
32 # Link and adapt mail-server-specific /etc/ files.
33 ./hardlink_etc.sh mail
34 sed -i "s/REPLACE_Domain_ECALPER/${mail_domain}/g" /etc/opendkim.conf
35 sed -i "s/REPLACE_Selector_ECALPER/${dkim_selector}/g" /etc/opendkim.conf
36 sed -i "s/REPLACE_myhostname_ECALPER/$(hostname -f)/g" /etc/postfix/main.cf
37 if [ "${domainwide}" = "domainwide" ]; then
38     sed -i 's/REPLACE_mydomain_if_domainwide_ECALPER/$mydomain/g' /etc/postfix/main.cf
39 else
40     sed -i 's/REPLACE_mydomain_if_domainwide_ECALPER//g' /etc/postfix/main.cf
41 fi
42 # Since we re-set the iptables rules, we need to reload them.
43 iptables-restore /etc/iptables/rules.v4
44
45 # Some useful debconf selections.
46 echo "postfix postfix/main_mailer_type string 'Internet Site'" | debconf-set-selections
47 echo "ssl_cert = </etc/letsencrypt/live/$(hostname -f)/fullchain.pem" > /etc/dovecot/conf.d/99-ssl-certs.conf
48 echo "ssl_key = </etc/letsencrypt/live/$(hostname -f)/privkey.pem" >> /etc/dovecot/conf.d/99-ssl-certs.conf
49
50 # The second line should not be necessary due to the first line, but for
51 # some reason the installation forgets to set up /etc/mailname early
52 # enough to not (when running newaliases) stumble over its absence.
53 echo "postfix postfix/mailname string ${mail_domain}" | debconf-set-selections
54 echo "${mail_domain}" > /etc/mailname
55
56 # Everything should now be ready for installations.
57 apt install -y -o Dpkg::Options::=--force-confold postfix dovecot-imapd opendkim
58 echo "TODO: Ensure MX entry for your system in your DNS configuration."
59 echo "TODO: Ensure a proper SPF entry for this system in your DNS configuration; something like 'v=spf1 mx -all' mapped to your host."
60 if [ "${add_dkim_record}" -eq "1" ]; then
61     echo "TODO: Add the following DKIM entry to your DNS configuration (possibly with slightly changed host entry – if your mail domain includes a subdomain, append that with a dot):"
62     cat "${dkim_selector}.txt"
63 fi
64 echo "TODO: passwd plom"