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