home · contact · privacy
Refactor mail setup.
[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 cat "${config_tree_prefix}/other_files/append_postfix_main.cf" >> /etc/postfix/main.cf
18 cat "${config_tree_prefix}/other_files/append_postfix_master.cf" >> /etc/postfix/main.cf
19 cat "${config_tree_prefix}/other_files/append_opendkim.conf" >> /etc/opendkim.conf
20
21 # Set up letsencrypt certificate.  We need this for STARTTLS on port
22 # 25/SMTP (some mail servers refuse delivering mails here if no
23 # STARTTLS available) and transport-layer TLS on port 465 (for
24 # user-to-server SMTPS)
25 # TODO: Is it auto-renewed?
26 certbot certonly --standalone --agree-tos --no-eff-email -m "${mail}" -d "$(hostname -f)"
27
28 # OpenDKIM setup.
29 selector=$(hostname)$(date +%Y%m%d)
30 #opendkim-genkey -D /etc/dkimkeys -s "${selector}"
31 opendkim-genkey -d "$(hostname -f)" -D /etc/dkimkeys -s "${selector}"
32 sed -i "s/REPLACE_hostname_ECALPER/$(hostname -f)/g" /etc/opendkim.conf
33 sed -i "s/REPLACE_selector_ECALPER/${selector}/g" /etc/opendkim.conf
34
35 # Dovecot sieve filtering via LMTP.  Without this, mail only gets
36 # delivered to /var/mail/….
37 cp "${config_tree_prefix}/other_files/dovecot.sieve" /home/plom/.dovecot.sieve
38 chown plom:plom /home/plom/.dovecot.sieve
39
40 # To allow IMAPS access.
41 echo "ssl_cert = </etc/letsencrypt/live/$(hostname -f)/fullchain.pem" > /etc/dovecot/conf.d/99-ssl-certs.conf
42 echo "ssl_key = </etc/letsencrypt/live/$(hostname -f)/privkey.pem" >> /etc/dovecot/conf.d/99-ssl-certs.conf
43 password=$(pwgen -s 100 1)
44 #echo 'mail_privileged_group = mail' >> /etc/dovecot/conf.d/99-mail.conf
45 echo "plom:${password}" | chpasswd
46
47 service opendkim restart
48 service postfix restart
49 service dovecot restart
50
51 echo "To put into DNS:"
52 cat "/etc/dkimkeys/${selector}.txt"
53 echo "If subdomain, append .subdomain to _domainkeys!"
54 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"
55 echo "Also ensure SPF record of 'v=spf1 mx -all' at @ or subdomain"
56 echo "Also ensure reverse DNS lookup for our IP points to $(hostname -f)"
57 echo "Also ensure MX record of priority 10 for @ or subdomain pointing to $(hostname -f)"
58 echo "IMAPS password for user plom is: ${password}"
59
60 # todo just for proper mail /sending/:
61 # * 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?
62 # * figure out /etc/postfix/main.cf
63 # - 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")
64 # - 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.")
65 # - 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.")
66 # - relay_domains ("What destination domains (and subdomains thereof) this system will relay mail to")
67 # - mydestination ("The list of domains that are delivered via the $local_transport mail delivery transport.")
68 # * figure out /etc/postfix/master.cf (configures what postfix daemons run in what way?)
69 # * how to check IP safety
70 # https://talosintelligence.com/reputation_center/lookup?search=$IP
71 # http://www.anti-abuse.org/multi-rbl-check-results/?host=
72 # https://www.dnsbl.info/dnsbl-database-check.php
73 # note that none of these catch the IPs that gmx etc. reject
74
75 # other stuff:
76 # * 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)
77 # * more /etc/postfix/main.cf
78 # - smtpd_recipient_restrictions
79 # - smtpd_helo_restrictions
80 # - smtpd_client_restrictions
81 #
82 # for using SMTP server remotely:
83 # - SASL mechanism, may use dovecot for that (smtpd_sasl_type, smtpd_sasl_path)
84 #   - see https://wiki2.dovecot.org/HowTo/PostfixAndDovecotSASL
85 # - SASL is not a protocol but an abstraction layer to some auth mechanism
86 # - SSL works on transport layer
87 # - wild guess: TLS/SSL is used to authenticate /the server/ to the client, while SASL is used to identify /the client/ to the server
88 #   - then it should be possible to do SASL without TLS/STARTTLS first? (experiment)
89 #   - the telnet test should offer AUTH then without doing STARTTLS first