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