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