home · contact · privacy
Add play server setup.
[config] / buster / setup_scripts / setup_mail.sh
1 #!/bin/sh
2 set -e
3
4 # Check we have the necessary arguments.
5 if [ "$#" -lt 1 ]; then
6     echo 'Need mail for letsencrypt, mail domain, and optionally old server IP.'
7     false
8 fi
9 mail="$1"
10 mail_domain="$2"
11 old_server="$3"
12
13 read -p'You sure you entered the correct mail domain? (not the server domain, but what comes after the @ in your mail addresses) If not, abort here!' ignore
14
15 config_tree_prefix="${HOME}/config/buster"
16 echo "postfix postfix/main_mailer_type string 'Internet Site'" | debconf-set-selections
17 echo "postfix postfix/mailname string ${mail_domain}" | debconf-set-selections
18 ./install_for_target.sh mail
19 ./copy_dirtree.sh "${config_tree_prefix}/etc_files" "" mail
20 nft -f /etc/nftables.conf
21
22 # Rebuild aliases DB from /etc/aliases
23 newaliases
24
25 # Update config files without overwriting defaults.
26 cat "${config_tree_prefix}/other_files/append_postfix_main.cf" >> /etc/postfix/main.cf
27 cat "${config_tree_prefix}/other_files/append_postfix_master.cf" >> /etc/postfix/master.cf
28 cat "${config_tree_prefix}/other_files/append_opendkim.conf" >> /etc/opendkim.conf
29
30 # Set up letsencrypt certificate.  We need this for STARTTLS on port
31 # 25/SMTP (some mail servers refuse delivering mails here if no
32 # STARTTLS available) and transport-layer TLS on port 465 (for
33 # user-to-server SMTPS)
34 # TODO: Is it auto-renewed?
35 certbot certonly --standalone --agree-tos --no-eff-email -m "${mail}" -d "$(hostname -f)"
36
37 # For if FQDN != mail domain name.
38 sed -i "s/REPLACE_maildomain_ECALPER/${mail_domain}/g" /etc/mailutils.conf
39 sed -i "s/REPLACE_maildomain_ECALPER/${mail_domain}/g" /etc/postfix/main.cf
40
41 # OpenDKIM setup.
42 selector=$(hostname)$(date +%Y%m%d)
43 opendkim-genkey -d "${mail_domain}" -D /etc/dkimkeys -s "${selector}"
44 sed -i "s/REPLACE_maildomain_ECALPER/${mail_domain}/g" /etc/opendkim.conf
45 sed -i "s/REPLACE_selector_ECALPER/${selector}/g" /etc/opendkim.conf
46
47 # Dovecot sieve filtering via LMTP.  Without this, mail only gets
48 # delivered to /var/mail/…, with it /var/mail/… remains the fallback
49 # inbox, but all else is sieve-filtered to ~/mail/.
50 cp "${config_tree_prefix}/other_files/dovecot.sieve" /home/plom/.dovecot.sieve
51 chown plom:plom /home/plom/.dovecot.sieve
52
53 # In addition to our postfix server receiving mails, we funnel mails from a
54 # POP3 account into dovecot via fetchmail. It might make sense to adapt the
55 # ~/.dovecot.sieve to move mails targeted to the fetched mail account to their
56 # own mbox.
57 cp "${config_tree_prefix}/other_files/fetchmailrc" /home/plom/.fetchmailrc
58 chown plom:plom /home/plom/.fetchmailrc
59 chmod 0700 /home/plom/.fetchmailrc
60
61 # Pingmail setup.
62 cp "${config_tree_prefix}/other_files/pingmailrc" /home/plom/.pingmailrc
63 chown plom:plom /home/plom/.pingmailrc
64 su -lc "cd && git clone https://plomlompom.com/repos/clone/pingmail" plom
65
66 # To allow IMAPS access.
67 echo "ssl_cert = </etc/letsencrypt/live/$(hostname -f)/fullchain.pem" > /etc/dovecot/conf.d/99-ssl-certs.conf
68 echo "ssl_key = </etc/letsencrypt/live/$(hostname -f)/privkey.pem" >> /etc/dovecot/conf.d/99-ssl-certs.conf
69 password=$(pwgen -s 100 1)
70 echo "plom:${password}" | chpasswd
71
72 # Get old mail data, shutdown old postfix server.
73 if [ "${old_server}" != "" ]; then
74   cp "${config_tree_prefix}/setup_scripts/prepare_to_meet_server.sh" /home/plom/
75   su -lc "./prepare_to_meet_server.sh ${old_server}" plom
76   read -p'Hit Enter when you are done.' ignore
77   rm /home/plom/prepare_to_meet_server.sh
78   su -lc "scp plom@${old_server}:.dovecot.sieve ~" plom
79   su -lc "scp plom@${old_server}:.fetchmailrc ~" plom
80   su -lc "scp plom@${old_server}:.pingmailrc ~" plom
81   su -lc "ssh -t plom@${old_server} \"su -lc 'service postfix stop'\"" plom
82   su -lc "ssh plom@${old_server} \"su -lc 'systemctl disable fetchmail_old_account.timer'\"" plom
83   su -lc "ssh plom@${old_server} \"su -lc 'service fetchmail_old_account stop'\"" plom
84   #su -lc "ssh -t plom@${old_server} \"su -lc 'service fetchmail stop'\"" plom
85   cp "${config_tree_prefix}/setup_scripts/mirror_dir.sh" /home/plom/
86   su -lc "./mirror_dir.sh ${old_server} /home/plom/mail" plom
87   rm /home/plom/mirror_dir.sh
88   touch /var/mail/plom
89   chown plom:mail /var/mail/plom
90   chmod 0600 /var/mail/plom
91   su -lc "scp plom@${old_server}:/var/mail/plom /var/mail/plom" plom
92 fi
93
94 # Start everything anew to ensure new configurations.
95 service opendkim restart
96 service postfix restart
97 service dovecot restart
98
99 # Pingmail and fetchmail have some systemd timers waiting. To let systemd
100 # know about them, do this.
101 systemctl daemon-reload
102 systemctl enable --now fetchmail_old_account.timer
103 systemctl enable --now pingmail.timer
104
105 # Final advice to user.
106 echo "To put into DNS:"
107 cat "/etc/dkimkeys/${selector}.txt"
108 echo "If subdomain, append .subdomain to _domainkeys!"
109 echo "Also ensure DMARC record of 'v=DMARC1; p=none; rua=mailto:plom+dmarc@plomlompom.com;' as TXT entry at _dmarc or, if subdomain, _dmarc.subdomain"
110 echo "Also ensure SPF record of 'v=spf1 mx -all' as TXT entry at @ or subdomain"
111 echo "Also ensure reverse DNS lookup for our IP points to $(hostname -f)"
112 echo "Also ensure MX record of priority 10 for @ or subdomain pointing to $(hostname -f)"
113 echo "IMAPS password for user plom is: ${password}"
114 echo "Also don't forget borgbackup migration …"
115
116 # todo just for proper mail /sending/:
117 # * how to check IP safety
118 # https://talosintelligence.com/reputation_center/lookup?search=$IP
119 # http://www.anti-abuse.org/multi-rbl-check-results/?host=
120 # https://www.dnsbl.info/dnsbl-database-check.php
121 # note that none of these catch the IPs that gmx etc. reject