home · contact · privacy
Add mail server setup scripting.
[config] / buster / setup_scripts / setup_mail.sh
1 #!/bin/sh
2 set -e
3
4 if [ "$#" -ne 1 ]; then
5     echo 'Need mail for letsencrypt.'
6     false
7 fi
8 mail="$1"
9
10 config_tree_prefix="${HOME}/config/buster"
11 echo "postfix postfix/main_mailer_type string 'Internet Site'" | debconf-set-selections
12 echo "postfix postfix/mailname string $(hostname -f)" | debconf-set-selections
13 ./install_for_target.sh mail
14 ./copy_dirtree.sh "${config_tree_prefix}/etc_files" "" mail
15 nft -f /etc/nftables.conf
16
17 # Set up letsencrypt certificate.
18 # TODO: Is it auto-renewed?
19 # TODO: Find out if/why this works despite firewall?
20 certbot certonly --standalone --agree-tos --no-eff-email -m "${mail}" -d "$(hostname -f)"
21
22 # generate opendkim selector
23 selector=$(hostname)$(date +%Y%m%d)
24 #opendkim-genkey -D /etc/dkimkeys -s "${selector}"
25 opendkim-genkey -d "$(hostname -f)" -D /etc/dkimkeys -s "${selector}"
26
27 # customize /etc/opendkim.conf
28 echo '' >> /etc/opendkim.conf
29 echo '# plomlompom customizations' >> /etc/opendkim.conf
30 echo "Domain $(hostname -f)" >> /etc/opendkim.conf
31 echo "KeyFile /etc/dkimkeys/${selector}.private" >> /etc/opendkim.conf
32 echo "Selector ${selector}" >> /etc/opendkim.conf
33 echo 'Socket inet:8892@localhost' >> /etc/opendkim.conf
34
35 # customize /etc/postfix/main.cf
36 echo '' >> /etc/postfix/main.cf
37 echo '# opendkim milter' >> /etc/postfix/main.cf
38 echo 'non_smtpd_milters = inet:localhost:8892' >> /etc/postfix/main.cf
39 echo '' >> /etc/postfix/main.cf
40 echo '# TLS certs'
41 echo 'smtpd_tls_cert_file=/etc/letsencrypt/live/${myhostname}/fullchain.pem' >> /etc/postfix/main.cf
42 echo 'smtpd_tls_key_file=/etc/letsencrypt/live/${myhostname}/privkey.pem' >> /etc/postfix/main.cf
43
44 # TODO: consider <https://wiki.debian.org/opendkim#Postfix_integration>
45
46 # Dovecot sieve filtering via LMTP
47 echo '' >> /etc/postfix/main.cf
48 echo '# transport mail to dovecot; not strictly needed, as even without this' >> /etc/postfix/main.cf
49 echo '# postfix will throw mail to /var/mail/USER to be found by dovecot for' >> /etc/postfix/main.cf
50 echo "# serving via IMAP etc.; but using dovecot's LMTP server for delivery" >> /etc/postfix/main.cf
51 echo '# allows us to do stuff like dovecot-side sieve filtering.' >> /etc/postfix/main.cf
52 echo 'mailbox_transport = lmtp:inet:127.0.0.1:2424' >> /etc/postfix/main.cf
53 cp "${config_tree_prefix}/other_files/dovecot.sieve" /home/plom/.dovecot.sieve
54 chown plom:plom /home/plom/.dovecot.sieve
55
56 # To allow IMAPS access
57 echo "ssl_cert = </etc/letsencrypt/live/$(hostname -f)/fullchain.pem" > /etc/dovecot/conf.d/99-ssl-certs.conf
58 echo "ssl_key = </etc/letsencrypt/live/$(hostname -f)/privkey.pem" >> /etc/dovecot/conf.d/99-ssl-certs.conf
59 password=$(pwgen -s 100 1)
60 #echo 'mail_privileged_group = mail' >> /etc/dovecot/conf.d/99-mail.conf
61 echo "plom:${password}" | chpasswd
62
63 # To use Dovecot SASL for SMTP access.
64 echo '' >> /etc/postfix/main.cf
65 echo '# use dovecot SASL for SMTP access' >> /etc/postfix/main.cf
66 echo 'smtpd_sasl_type = dovecot' >> /etc/postfix/main.cf
67 echo 'smtpd_sasl_path = private/auth' >> /etc/postfix/main.cf
68 echo 'smtpd_sasl_auth_enable = yes' >> /etc/postfix/main.cf
69
70 service opendkim restart
71 service postfix restart
72 service dovecot restart
73
74 echo "To put into DNS:"
75 cat "/etc/dkimkeys/${selector}.txt"
76 echo "If subdomain, append .subdomain to _domainkeys!"
77 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"
78 echo "Also ensure SPF record of 'v=spf1 mx -all' at @ or subdomain"
79 echo "Also ensure reverse DNS lookup for our IP points to $(hostname -f)"
80 echo "Also ensure MX record of priority 10 for @ or subdomain pointing to $(hostname -f)"
81 echo "IMAPS password for user plom is: ${password}"
82
83 # todo just for proper mail /sending/:
84 # * figure out /etc/mailname ("used by the Mail Transfer Agent (i.e. mail server) to know its own hostname", "will contain the portion after the username and @ (at) sign for email addresses of users on the machine", "Postfix sets myorigin=/etc/mailname. Myorigin is appended (with an @) to any bare name in any address field. During (re)configure, if /etc/mailname exists, it is used as the default value for myorigin in debconf dialogs. Changing it from the default results in postinst rewriting /etc/mailname to the new value.") – figure out if it is actually used?
85 # * figure out /etc/postfix/main.cf
86 # - myorigin ("specifies the domain that appears in mail that is posted on this machine. The default is to use the local machine name, $myhostname, which defaults to the name of the machine. Unless you are running a really small site, you probably want to change that into $mydomain, which defaults to the parent domain of the machine name")
87 # - myhostname ("The internet hostname of this mail system. The default is to use the fully-qualified domain name (FQDN) from gethostname(), or to use the non-FQDN result from gethostname() and append ".$mydomain". $myhostname is used as a default value for many other configuration parameters.")
88 # - mydomain ("The internet domain name of this mail system. The default is to use $myhostname minus the first component, or "localdomain" (Postfix 2.3 and later). $mydomain is used as a default value for many other configuration parameters.")
89 # - relay_domains ("What destination domains (and subdomains thereof) this system will relay mail to")
90 # - mydestination ("The list of domains that are delivered via the $local_transport mail delivery transport.")
91 # * figure out /etc/postfix/master.cf (configures what postfix daemons run in what way?)
92 # * how to check IP safety
93 # https://talosintelligence.com/reputation_center/lookup?search=$IP
94 # http://www.anti-abuse.org/multi-rbl-check-results/?host=
95 # https://www.dnsbl.info/dnsbl-database-check.php
96 # note that none of these catch the IPs that gmx etc. reject
97
98 # other stuff:
99 # * figure out /etc/aliases (maps whom what is sent to – just re-use old core.plomlompom.com one? – also, run newaliases to generate /etc/aliases.db)
100 # * more /etc/postfix/main.cf
101 # - smtpd_recipient_restrictions
102 # - smtpd_helo_restrictions
103 # - smtpd_client_restrictions
104 #
105 # for using SMTP server remotely:
106 # - SASL mechanism, may use dovecot for that (smtpd_sasl_type, smtpd_sasl_path)
107 #   - see https://wiki2.dovecot.org/HowTo/PostfixAndDovecotSASL
108 # - SASL is not a protocol but an abstraction layer to some auth mechanism
109 # - SSL works on transport layer
110 # - wild guess: TLS/SSL is used to authenticate /the server/ to the client, while SASL is used to identify /the client/ to the server
111 #   - then it should be possible to do SASL without TLS/STARTTLS first? (experiment)
112 #   - the telnet test should offer AUTH then without doing STARTTLS first
113 #
114 # for receiving mails: make sure firewall opens SMTP port 25, and for STARTTLS to work need certificate installed (set in postfix/main.cf) – some providers only deliver via STARTTLS, i.e. GMail – this all only delivers mails to /var/mail/…, dovecot should do more then