home · contact · privacy
Fix systemd timers.
[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 cp "${config_tree_prefix}/other_files/fetchmailrc" /home/plom/.fetchmailrc
43 chown plom:plom /home/plom/.fetchmailrc
44 chmod 0700 /home/plom/.fetchmailrc
45 cp "${config_tree_prefix}/other_files/pingmailrc" /home/plom/.pingmailrc
46 chown plom:plom /home/plom/.pingmailrc
47 su -lc "cd && git clone https://plomlompom.com/repos/clone/pingmail" plom
48 systemctl daemon-reload
49 systemctl enable --now fetchmail_old_account.timer
50 systemctl enable --now pingmail.timer
51
52 # To allow IMAPS access.
53 echo "ssl_cert = </etc/letsencrypt/live/$(hostname -f)/fullchain.pem" > /etc/dovecot/conf.d/99-ssl-certs.conf
54 echo "ssl_key = </etc/letsencrypt/live/$(hostname -f)/privkey.pem" >> /etc/dovecot/conf.d/99-ssl-certs.conf
55 password=$(pwgen -s 100 1)
56 #echo 'mail_privileged_group = mail' >> /etc/dovecot/conf.d/99-mail.conf
57 echo "plom:${password}" | chpasswd
58
59 # Get old mail data, shutdown old postfix server.
60 if [ "${old_server}" != "" ]; then
61   cp "${config_tree_prefix}/setup_scripts/prepare_to_meet_server.sh" /home/plom/
62   #chown plom:plom /home/plom/prepare_to_meet_server.sh
63   su -lc "./prepare_to_meet_server.sh ${old_server}" plom
64   read -p'Hit Enter when you are done.' ignore
65   rm /home/plom/prepare_to_meet_server.sh
66   su -lc "scp plom@${old_server}:.dovecot.sieve ~" plom
67   su -lc "scp plom@${old_server}:.fetchmailrc ~" plom
68   su -lc "scp plom@${old_server}:.pingmailrc ~" plom
69   su -lc "ssh plom@${old_server} \"su -lc 'service postfix stop'\"" plom
70   cp "${config_tree_prefix}/setup_scripts/mirror_dir.sh" /home/plom/
71   su -lc "./mirror_dir.sh ${old_server} /home/plom/mail" plom
72   rm /home/plom/mirror_dir.sh
73   touch /var/mail/plom
74   chown plom:mail /var/mail/plom
75   chmod 0600 /var/mail/plom
76   su -lc "scp plom@${old_server}:/var/mail/plom /var/mail/plom" plom
77 fi
78
79 service opendkim restart
80 service postfix restart
81 service dovecot restart
82
83 echo "To put into DNS:"
84 cat "/etc/dkimkeys/${selector}.txt"
85 echo "If subdomain, append .subdomain to _domainkeys!"
86 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"
87 echo "Also ensure SPF record of 'v=spf1 mx -all' as TXT entry at @ or subdomain"
88 echo "Also ensure reverse DNS lookup for our IP points to $(hostname -f)"
89 echo "Also ensure MX record of priority 10 for @ or subdomain pointing to $(hostname -f)"
90 echo "IMAPS password for user plom is: ${password}"
91
92 # todo just for proper mail /sending/:
93 # * 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?
94 # * figure out /etc/postfix/main.cf
95 # - 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")
96 # - 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.")
97 # - 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.")
98 # - relay_domains ("What destination domains (and subdomains thereof) this system will relay mail to")
99 # - mydestination ("The list of domains that are delivered via the $local_transport mail delivery transport.")
100 # * figure out /etc/postfix/master.cf (configures what postfix daemons run in what way?)
101 # * how to check IP safety
102 # https://talosintelligence.com/reputation_center/lookup?search=$IP
103 # http://www.anti-abuse.org/multi-rbl-check-results/?host=
104 # https://www.dnsbl.info/dnsbl-database-check.php
105 # note that none of these catch the IPs that gmx etc. reject
106
107 # other stuff:
108 # * 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)
109 # * more /etc/postfix/main.cf
110 # - smtpd_recipient_restrictions
111 # - smtpd_helo_restrictions
112 # - smtpd_client_restrictions
113 #
114 # for using SMTP server remotely:
115 # - SASL mechanism, may use dovecot for that (smtpd_sasl_type, smtpd_sasl_path)
116 #   - see https://wiki2.dovecot.org/HowTo/PostfixAndDovecotSASL
117 # - SASL is not a protocol but an abstraction layer to some auth mechanism
118 # - SSL works on transport layer
119 # - wild guess: TLS/SSL is used to authenticate /the server/ to the client, while SASL is used to identify /the client/ to the server
120 #   - then it should be possible to do SASL without TLS/STARTTLS first? (experiment)
121 #   - the telnet test should offer AUTH then without doing STARTTLS first