home · contact · privacy
f19d36ea9fd0e71f63016570e4da41934adf7107
[config] / all_new_2018 / setup_mail.sh
1 #/bin/sh
2 set -e
3
4 dkim_selector=$1
5 if [ ! -n "${dkim_selector}" ]; then
6     echo "Give DKIM selector argument."
7     false
8 fi
9
10 # Set up DKIM key if necessary.
11 mkdir -p /etc/dkimkeys/
12 add_dkim_record=0
13 if [ ! -f "/etc/dkimkeys/${dkim_selector}.private" ]; then
14     add_dkim_record=1
15     set +e
16     dpkg -s opendkim-tools &> /dev/null
17     preinstalled="$?"
18     set -e
19     if [ ! "${preinstalled}" -eq "0" ]; then
20         apt install -y opendkim-tools
21     fi
22     opendkim-genkey -s "${dkim_selector}"
23     mv "${dkim_selector}.private" /etc/dkimkeys/
24     if [ ! "${preinstalled}" -eq "0" ]; then
25         apt -y --purge autoremove opendkim-tools
26     fi
27 fi
28
29 # Link and adapt mail-server-specific /etc/ files.
30 ./hardlink_etc.sh mail
31 sed -i "s/REPLACE_Domain_ECALPER/$(hostname -f)/g" /etc/opendkim.conf
32 sed -i "s/REPLACE_Selector_ECALPER/${dkim_selector}/g" /etc/opendkim.conf
33
34 # Some useful debconf selections.
35 echo "postfix postfix/main_mailer_type string 'Internet Site'" | debconf-set-selections
36 echo "ssl_cert = </etc/letsencrypt/live/$(hostname -f)/fullchain.pem" > /etc/dovecot/conf.d/99-ssl-certs.conf
37 echo "ssl_key = </etc/letsencrypt/live/$(hostname -f)/privkey.pem" >> /etc/dovecot/conf.d/99-ssl-certs.conf
38
39 # The second line should not be necessary due to the first line, but for
40 # some reason the installation forgets to set up /etc/mailname early
41 # enough to not (when running newaliases) stumble over its absence.
42 echo "postfix postfix/mailname string $(hostname -f)" | debconf-set-selections
43 echo "$(hostname -f)" > /etc/mailname
44
45 # Everything should now be ready for installations.
46 apt install -y postfix dovecot-imapd opendkim
47 echo "TODO: Ensure MX entry for your system in your DNS configuration."
48 echo "TODO: Ensure a proper SPF entry for this system in your DNS configuration; something like 'v=spf1 a mx -all' mapped to your subdomain."
49 if [ "${add_dkim_record}" -eq "1" ]; then
50     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):"
51     cat "${dkim_selector}.txt"
52 fi