home · contact · privacy
Fix mail server setup script.
[config] / all_new_2018 / setup_scripts / 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 config_tree_prefix="${HOME}/config/all_new_2018"
14 setup_scripts_dir="${config_tree_prefix}/setup_scripts"
15 cd "${setup_scripts_dir}"
16
17 # Set up DKIM key if necessary.
18 mkdir -p /etc/dkimkeys/
19 add_dkim_record=0
20 if [ ! -f "/etc/dkimkeys/${dkim_selector}.private" ]; then
21     add_dkim_record=1
22     set +e
23     dpkg -s opendkim-tools &> /dev/null
24     preinstalled="$?"
25     set -e
26     if [ ! "${preinstalled}" -eq "0" ]; then
27         apt install -y opendkim-tools
28     fi
29     opendkim-genkey -s "${dkim_selector}"
30     mv "${dkim_selector}.private" /etc/dkimkeys/
31     if [ ! "${preinstalled}" -eq "0" ]; then
32         apt -y --purge autoremove opendkim-tools
33     fi
34 fi
35
36 # Link and adapt mail-server-specific /etc/ files.
37 ./hardlink_etc.sh mail
38 sed -i "s/REPLACE_maildomain_ECALPER/${mail_domain}/g" /etc/mailutils.conf
39 sed -i "s/REPLACE_Domain_ECALPER/${mail_domain}/g" /etc/opendkim.conf
40 sed -i "s/REPLACE_Selector_ECALPER/${dkim_selector}/g" /etc/opendkim.conf
41 sed -i "s/REPLACE_myhostname_ECALPER/$(hostname -f)/g" /etc/postfix/main.cf
42 if [ "${domainwide}" = "domainwide" ]; then
43     sed -i 's/REPLACE_mydomain_if_domainwide_ECALPER/$mydomain/g' /etc/postfix/main.cf
44 else
45     sed -i 's/REPLACE_mydomain_if_domainwide_ECALPER//g' /etc/postfix/main.cf
46 fi
47 # Since we re-set the iptables rules, we need to reload them.
48 iptables-restore /etc/iptables/rules.v4
49
50 # Some useful debconf selections.
51 echo "postfix postfix/main_mailer_type string 'Internet Site'" | debconf-set-selections
52 echo "ssl_cert = </etc/letsencrypt/live/$(hostname -f)/fullchain.pem" > /etc/dovecot/conf.d/99-ssl-certs.conf
53 echo "ssl_key = </etc/letsencrypt/live/$(hostname -f)/privkey.pem" >> /etc/dovecot/conf.d/99-ssl-certs.conf
54
55 # The second line should not be necessary due to the first line, but for
56 # some reason the installation forgets to set up /etc/mailname early
57 # enough to not (when running newaliases) stumble over its absence.
58 echo "postfix postfix/mailname string ${mail_domain}" | debconf-set-selections
59 echo "${mail_domain}" > /etc/mailname
60
61 # Everything should now be ready for installations. Note that we don't
62 # strictly need dovecot-lmtpd, as postfix will deliver mail to /var/mail/USER
63 # in any case, to be found by dovecot; we use it as a transport mechanism to
64 # allow for sophisticated stuff like dovecot-side sieve filtering (installed
65 # with dovecot-sieve).
66 apt install -y -o Dpkg::Options::=--force-confold postfix dovecot-imapd dovecot-lmtpd dovecot-sieve opendkim
67 cp "${config_tree_prefix}/user_files/dovecot.sieve" /home/plom/.dovecot.sieve
68 chown plom:plom /home/plom/.dovecot.sieve
69 cp "${config_tree_prefix}/user_files/pingmailrc" /home/plom/.pingmailrc
70 chown plom:plom /home/plom/.pingmailrc
71
72 # In addition to our postfix server receiving mails, we funnel mails from a
73 # POP3 account into dovecot via fetchmail. It might make sense to adapt the
74 # ~/.dovecot.sieve to move mails targeted to the fetched mail account to their
75 # own mbox.
76 cp "${config_tree_prefix}/user_files/fetchmailrc" /home/plom/.fetchmailrc
77 chown plom:plom /home/plom/.fetchmailrc
78 chmod 0700 /home/plom/.fetchmailrc
79 set +e
80 apt install -y fetchmail
81 systemctl daemon-reload
82 systemctl start fetchmail.timer
83 set -e
84
85 # Final advice to user.
86 echo "TODO: Ensure MX entry for your system in your DNS configuration."
87 echo "TODO: Ensure a proper SPF entry for this system in your DNS configuration; something like 'v=spf1 mx -all' mapped to your host."
88 if [ "${add_dkim_record}" -eq "1" ]; then
89     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):"
90     cat "${dkim_selector}.txt"
91 fi
92 echo "TODO: passwd plom"
93 echo "TODO: adapt /home/plom/.dovecot.sieve /home/plom/.fetchmailrc /home/plom/.pingmailrc"