changes for buster linode
[distro-setup] / mail-setup
1 #!/bin/bash
2 set -x
3
4 # Copyright (C) 2019 Ian Kelling
5
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9
10 # http://www.apache.org/licenses/LICENSE-2.0
11
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # TODO: copy dkim keys from within this file. its now done in conflink.
19 # TODO: fix dkim key to b chmod 640, group Debian-exim
20
21 set -eE -o pipefail
22 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
23
24 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
25 if [[ ! $SUDO_USER ]]; then
26 echo "$0: error: requires running as nonroot or sudo"
27 exit 1
28 fi
29 u=$SUDO_USER
30
31
32 usage() {
33 cat <<EOF
34 Usage: ${0##*/}
35 Setup exim4 & dovecot & related things
36
37 The minimal assumption we have is that /etc/mailpass exists
38
39
40 -h|--help Print help and exit.
41 EOF
42 exit $1
43 }
44
45
46
47 ####### instructions for icedove #####
48 # Incoming mail server: mail.iankelling.org, port 143, username iank, connection security starttls, authentication method normal password
49 # we could also just use 127.0.0.1 with no ssl, but todo: disable that in dovecot, so mail is secure from local programs.
50 #
51 # hamburger -> preferences -> preferences -> advanced tab -> config editor button -> security.ssl.enable_ocsp_must_staple = false
52 # background: ovecot does not yet have ocsp stapling support
53 # reference: https://community.letsencrypt.org/t/simple-guide-using-lets-encrypt-ssl-certs-with-dovecot/2921
54 #
55 # for phone, same thing but username alerts, pass in ivy-pass.
56 #######
57
58
59 ####### begin perstent password instructions ######
60 # # exim passwords:
61 # # for hosts which have all private files I just use the same user
62 # # for other hosts, each one get\'s their own password.
63 # # for generating secure pass, and storing for server too:
64 # # user=USUALLY_SAME_AS_HOSTNAME
65 # user=li
66 # f=$(mktemp)
67 # apg -m 50 -x 70 -n 1 -a 1 -M CLN >$f
68 # s sed -i "/^$user:/d" /p/c/filesystem/etc/exim4/passwd
69 # echo "$user:$(mkpasswd -m sha-512 -s <$f)" >>/p/c/filesystem/etc/exim4/passwd
70 # # todo: port is no longer used in mailpass, remove it.
71 # echo "mail.iankelling.org 587 $user:$(<$f)" >> /p/c/machine_specific/$user/filesystem/etc/mailpass
72 # # then run this script, or part of it which uses /etc/mailpass
73
74 # # dovecot password, i just need 1 as I\'m the only user
75 # mkdir /p/c/filesystem/etc/dovecot
76 # echo "iank:$(doveadm pw -s ssha256)::::::" >>/p/c/filesystem/etc/dovecot/users
77 # conflink
78
79
80
81 # # for ad-hoc testing of some random new host sending mail:
82 # user=li # client host username & hostname
83 # f=$(mktemp)
84 # apg -m 50 -x 70 -n 1 -a 1 -M CLN >$f
85 # s sed -i "/^$user:/d" /etc/exim4/passwd
86 # echo "$user:$(mkpasswd -m sha-512 -s <$f)" | s tee -a /etc/exim4/passwd
87 # echo "mail.iankelling.org:$user:$(<$f)" | ssh root@$user dd of=/etc/exim4/passwd.client
88 ####### end perstent password instructions ######
89
90
91 ####### begin persistent dkim/dns instructions #########
92 # # Remove 1 level of comments in this section, set the domain var
93 # # for the domain you are setting up, then run this and copy dns settings
94 # # into dns.
95 # domain=iankelling.org
96 # c /p/c/filesystem/etc/exim4
97 # # this has several bugs addressed in comments, but it was helpful
98 # # https://debian-administration.org/article/718/DKIM-signing_outgoing_mail_with_exim4
99
100 # openssl genrsa -out $domain-private.pem 2048 -outform PEM
101 # openssl rsa -in $domain-private.pem -out $domain.pem -pubout -outform PEM
102 # # selector is needed for having multiple keys for one domain.
103 # # I dun do that, so just use a static one: li
104 # echo "txt record name: li._domainkey.$domain"
105 # # Debadmin page does not have v=, fastmail does, and this
106 # # says it\'s recommended in 3.6.1, default is DKIM1 anyways.
107 # # https://www.ietf.org/rfc/rfc6376.txt
108 # # Join and print all but first and last line.
109 # # last line: swap hold & pattern, remove newlines, print.
110 # # lines 2+: append to hold space
111 # echo "txt record contents:"
112 # echo "v=DKIM1; k=rsa; p=$(sed -n '${x;s/\n//gp};2,$H' $domain.pem)"
113 # chmod 644 $domain.pem
114 # chmod 640 $domain-private.pem
115 # # in conflink, we chown these to group debian
116 # conflink
117 # # selector was also put into /etc/exim4/conf.d/main/000_localmacros,
118 # # via the mail-setup scripts
119
120 # # 2017-02 dmarc policies:
121 # # host -t txt _dmarc.gmail.com
122 # # yahoo: p=reject, hotmail: p=none, gmail: p=none, fastmail none for legacy reasons
123 # # there were articles claiming gmail would be changing
124 # # to p=reject, in early 2017, which didn\'t happen. I see no sources on them. It\'s
125 # # expected to cause problems
126 # # with a few old mailing lists, copying theirs for now.
127 #
128 # echo "dmarc dns, name: _dmarc value: v=DMARC1; p=none; rua=mailto:mailauth-reports@$domain"
129
130 # # 2017-02 spf policies:
131 # # host -t txt lists.fedoraproject.org
132 # # google ~all, hotmail ~all, yahoo: ?all, fastmail ?all, outlook ~all
133 # # i include fastmail\'s settings, per their instructions,
134 # # and follow their policy. In mail in a box, or similar instructions,
135 # # I\'ve seen recommended to not use a restrictive policy.
136 # echo "spf dns: name is empty, value: v=spf1 a include:spf.messagingengine.com ?all"
137
138 # # to check if dns has updated, you do
139 # host -a mesmtp._domainkey.$domain
140
141 # # mx records,
142 # # setting it to iankelling.org would work the same, but this
143 # # is more flexible, I could change where mail.iankelling.org pointed.
144 # cat <<'EOF'
145 # mx records, 2 records each, for * and empty domain
146 # pri 10 mail.iankelling.org
147 # pri 20 in1-smtp.messagingengine.com
148 # pri 30 in2-smtp.messagingengine.com
149 # EOF
150 ####### end persistent dkim instructions #########
151
152
153
154 e() { printf "%s\n" "$*"; }
155 pi() { # package install
156 local f
157 if dpkg -s -- "$@" &> /dev/null; then
158 return 0;
159 fi;
160 while fuser /var/lib/dpkg/lock &>/dev/null; do sleep 1; done
161 f=/var/cache/apt/pkgcache.bin;
162 if [[ ! -r $f ]] || (( $(( $(date +%s) - $(stat -c %Y $f ) )) > 60*60*12 )); then
163 apt-get update
164 fi
165 DEBIAN_FRONTEND=noninteractive apt-get -y install --purge --auto-remove "$@"
166 }
167
168 postmaster=alerts
169 mxhost=mail.iankelling.org
170 mxport=587
171 forward=$u@$mxhost
172
173 # old setup. left as comment for example
174 # mxhost=mail.messagingengine.com
175 # mxport=587
176 # forward=ian@iankelling.org
177
178 smarthost="$mxhost::$mxport" # exim
179
180 # light version of exim does not have sasl auth support.
181 pi exim4-daemon-heavy spamassassin spf-tools-perl
182
183 # trisquel 8 = openvpn, debian stretch = openvpn-client
184 vpn_ser=openvpn-client
185 if [[ ! -e /lib/systemd/system/openvpn-client@.service ]]; then
186 vpn_ser=openvpn
187 fi
188
189 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
190 # afaik, these will get ignored because they are routing to my own
191 # machine, but rm them is safer
192 rm -f $(eval echo ~$u)/.forward /root/.forward
193 else
194 # this can\'t be a symlink and has permission restrictions
195 # it might work in /etc/aliases, but this seems more proper.
196 install -m 644 {-o,-g}$u <(e $forward) $(eval echo ~$u)/.forward
197 fi
198
199
200 # light version of exim does not have sasl auth support.
201 pi exim4-daemon-heavy spamassassin spf-tools-perl openvpn dnsmasq
202
203 if [[ -e /p/c/filesystem ]]; then
204 # allow failure of these commands when our internet is down, they are likely not needed,
205 # we check that a valid cert is there already.
206 # to put the hostname in the known hosts
207 if ! ssh -o StrictHostKeyChecking=no root@li.iankelling.org :; then
208 # This just causes failure if our cert is going to expire in the next 30 days.
209 # Certs I generate last 10 years.
210 openssl x509 -checkend $(( 60 * 60 * 24 * 30 )) -noout -in /etc/openvpn/mail.crt
211 else
212 # note, man openvpn implies we could just call mail-route on vpn startup/shutdown with
213 # systemd, buuut it can remake the tun device unexpectedly, i got this in the log
214 # after my internet was down for a bit:
215 # NOTE: Pulled options changed on restart, will need to close and reopen TUN/TAP device.
216 /a/exe/vpn-mk-client-cert -b mail -n mail -s /b/ds/mail-route li.iankelling.org
217 fi
218 fi
219
220
221 cat >/etc/systemd/system/mailclean.timer <<'EOF'
222 [Unit]
223 Description=Run mailclean daily
224
225 [Timer]
226 OnCalendar=monthly
227
228 [Install]
229 WantedBy=timers.target
230 EOF
231
232 cat >/etc/systemd/system/mailclean.service <<EOF
233 [Unit]
234 Description=Delete and archive old mail files
235 After=multi-user.target
236
237 [Service]
238 User=$u
239 Type=oneshot
240 ExecStart=/a/bin/log-quiet/sysd-mail-once mailclean /a/bin/distro-setup/mailclean
241 EOF
242
243 systemctl daemon-reload
244
245
246 source /a/bin/bash_unpublished/source-state
247 mkdir -p /etc/exim4/conf.d/{main,transport,auth,router}
248
249 cat >/etc/exim4/rcpt_local_acl <<'EOF'
250 # Only hosts we control send to @mail.iankelling.org, so make sure
251 # they are all authed.
252 # Note, if we wanted authed senders for all domains,
253 # we could make this condition in acl_check_mail
254 deny
255 message = ian trusted domain recepient but no auth
256 !authenticated = *
257 domains = mail.iankelling.org
258 EOF
259 cat >/etc/exim4/data_local_acl <<'EOF'
260 # Except for the "condition =", this was
261 # a comment in the check_data acl. The comment about this not
262 # being suitable is mostly bs. The only thing related I found was to
263 # add the condition =, cuz spamassassin has problems with big
264 # messages and spammers don't bother with big messages,
265 # but I've increased the size from 10k
266 # suggested in official docs, and 100k in the wiki example because
267 # those docs are rather old and I see a 110k spam message
268 # pretty quickly looking through my spam folder.
269 warn
270 condition = ${if < {$message_size}{2000K}}
271 spam = Debian-exim:true
272 add_header = X-Spam_score: $spam_score\n\
273 X-Spam_score_int: $spam_score_int\n\
274 X-Spam_bar: $spam_bar\n\
275 X-Spam_report: $spam_report
276
277 #accept
278 # spf = pass:fail:softfail:none:neutral:permerror:temperror
279 # dmarc_status = reject:quarantine
280 # add_header = Reply-to: dmarctest@iankelling.org
281
282 EOF
283 cat >/etc/exim4/conf.d/auth/29_exim4-config_auth <<'EOF'
284 # from 30_exim4-config_examples
285
286 plain_server:
287 driver = plaintext
288 public_name = PLAIN
289 server_condition = "${if crypteq{$auth3}{${extract{1}{:}{${lookup{$auth2}lsearch{CONFDIR/passwd}{$value}{*:*}}}}}{1}{0}}"
290 server_set_id = $auth2
291 server_prompts = :
292 .ifndef AUTH_SERVER_ALLOW_NOTLS_PASSWORDS
293 server_advertise_condition = ${if eq{$tls_in_cipher}{}{}{*}}
294 .endif
295 EOF
296
297 cat >/etc/exim4/conf.d/router/900_exim4-config_local_user <<'EOF'
298 ### router/900_exim4-config_local_user
299 #################################
300
301 # This router matches local user mailboxes. If the router fails, the error
302 # message is "Unknown user".
303
304 local_user:
305 debug_print = "R: local_user for $local_part@$domain"
306 driver = accept
307 domains = +local_domains
308 # ian: commented this, in conjunction with a dovecot lmtp
309 # change so I get mail for all users.
310 # check_local_user
311 local_parts = ! root
312 transport = LOCAL_DELIVERY
313 cannot_route_message = Unknown user
314 EOF
315 cat >/etc/exim4/conf.d/transport/30_exim4-config_dovecot_lmtp <<'EOF'
316 dovecot_lmtp:
317 driver = lmtp
318 socket = /var/run/dovecot/lmtp
319 #maximum number of deliveries per batch, default 1
320 batch_max = 200
321 EOF
322
323 cat >/etc/exim4/host_local_deny_exceptions <<'EOF'
324 mail.fsf.org
325 *.posteo.de
326 EOF
327
328 cat >/etc/exim4/conf.d/router/190_exim4-config_fsfsmarthost <<'EOF'
329 # smarthost for fsf mail
330 # ian: copied from /etc/exim4/conf.d/router/200_exim4-config_primary, and added senders = and
331 # replaced DCsmarthost with mail.fsf.org
332 fsfsmarthost:
333 debug_print = "R: smarthost for $local_part@$domain"
334 driver = manualroute
335 domains = ! +local_domains
336 senders = *@fsf.org
337 transport = remote_smtp_smarthost
338 route_list = * mail.fsf.org byname
339 host_find_failed = ignore
340 same_domain_copy_routing = yes
341 no_more
342 EOF
343
344
345 #### begin mail cert setup ###
346 f=/usr/local/bin/mail-cert-cron
347 cat >$f <<'EOF'
348 #!/bin/bash
349 set -eE -o pipefail
350 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
351
352 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
353
354 f=/a/bin/bash_unpublished/source-state
355 if [[ -e $f ]]; then
356 source $f
357 fi
358 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
359 local_mx=mail.iankelling.org
360 rsync_common="rsync -ogtL --chown=root:Debian-exim --chmod=640 root@li.iankelling.org:/etc/letsencrypt/live/$local_mx/"
361 ${rsync_common}fullchain.pem /etc/exim4/exim.crt
362 ret=$?
363 ${rsync_common}privkey.pem /etc/exim4/exim.key
364 new_ret=$?
365 if [[ $ret != $new_ret ]]; then
366 echo "$0: error: differing rsync returns, $ret, $new_ret"
367 exit 1
368 fi
369 fi
370 if [[ $new_ret != 0 ]]; then
371 if ! openssl x509 -checkend $(( 60 * 60 * 24 * 3 )) -noout -in /etc/exim4/exim.crt; then
372 echo "$0: error!: cert rsync failed and it will expire in less than 3 days"
373 exit 1
374 fi
375 fi
376 exit 0
377 EOF
378 chmod 755 $f
379
380 cat >/etc/systemd/system/mailcert.service <<'EOF'
381 [Unit]
382 Description=Mail cert rsync
383 After=multi-user.target
384
385 [Service]
386 Type=oneshot
387 ExecStart=/a/bin/log-quiet/sysd-mail-once mailcert /usr/local/bin/mail-cert-cron
388 EOF
389
390 cat >/etc/systemd/system/mailcert.timer <<'EOF'
391 [Unit]
392 Description=Run mail-cert once a day
393
394 [Timer]
395 OnCalendar=daily
396
397 [Install]
398 WantedBy=timers.target
399 EOF
400 systemctl daemon-reload
401 systemctl start mailcert
402 systemctl restart mailcert.timer
403 systemctl enable mailcert.timer
404
405 ##### end mailcert setup #####
406
407 # comon stuff
408 cat >/etc/exim4/update-exim4.conf.conf <<'EOF'
409 # default stuff, i havent checked if its needed
410 dc_minimaldns='false'
411 dc_relay_nets=''
412 CFILEMODE='644'
413 dc_use_split_config='true'
414 dc_local_interfaces=''
415 dc_mailname_in_oh='true'
416 EOF
417
418
419 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
420
421 # mail.iankelling.org so local imap clients can connect with tls and
422 # when they happen to not be local.
423 sed -ri -f - /etc/hosts <<'EOF'
424 /^127\.0\.1\.1.* mail\.iankelling\.org\b/{p;d}
425 /^127\.0\.1\.1 /s/ *$/ mail.iankelling.org/
426 EOF
427 /a/exe/cedit mail /etc/dnsmasq-servers.conf <<'EOF' || [[ $? == 1 ]]
428 server=/mail.iankelling.org/127.0.1.1
429 EOF
430 systemctl reload dnsmasq
431
432 # I used to use debconf-set-selections + dpkg-reconfigure,
433 # which then updates this file
434 # but the process is slower than updating it directly and then I want to set other things in
435 # update-exim4.conf.conf, so there's no point.
436 # The file is documented in man update-exim4.conf,
437 # except the man page is not perfect, read the bash script to be sure about things.
438
439 # The debconf questions output is additional documentation that is not
440 # easily accessible, but super long, along with the initial default comment in this
441 # file, so I've saved that into ./mail-notes.conf.
442
443 cat >>/etc/exim4/update-exim4.conf.conf <<EOF
444 # note: some things we don't set that are here by default because they are unused.
445
446 dc_eximconfig_configtype='internet'
447
448 # man page: is used to build the local_domains list, together with “localhost”
449 # iank.bid is for testing
450 # mail.iankelling.org is for machines i own
451 dc_other_hostnames='*.iankelling.org;iankelling.org;*iank.bid;iank.bid;*zroe.org;zroe.org;*.b8.nz;b8.nz'
452
453 # from man page:
454 # Is a list of domains for which we accept mail from anywhere on the Internet but which are not delivered locally, e.g.
455 # because this machine serves as secondary MX for these domains. Sets MAIN_RELAY_TO_DOMAINS.
456 # todo: we should not accept from anywhere, only the mx for fsf.
457 dc_relay_domains='*.fsf.org;fsf.org'
458 EOF
459
460
461 # the debconf output about mailname is as follows:
462 # The 'mail name' is the domain name used to 'qualify' mail addresses without a domain
463 # name.
464 # This name will also be used by other programs. It should be the single, fully
465 # qualified domain name (FQDN).
466 # Thus, if a mail address on the local host is foo@example.org, the correct value for
467 # this option would be example.org.
468 # This name won\'t appear on From: lines of outgoing messages if rewriting is enabled.
469
470 echo mail.iankelling.org > /etc/mailname
471
472 # MAIN_HARDCODE_PRIMARY_HOSTNAME might mess up the
473 # smarthost config type, not sure. all other settings
474 # would be unused in that config type.
475 rm -f /etc/exim4/conf.d/main/000_localmacros # old filename
476 cat >/etc/exim4/conf.d/main/000_local <<EOF
477 # enable 587 in addition to the default 25, so that
478 # i can send mail where port 25 is firewalled by isp
479 daemon_smtp_ports = 25 : 587
480 # i don't have ipv6 setup for my vpn tunnel yet.
481 disable_ipv6 = true
482
483 MAIN_TLS_ENABLE = true
484
485 DKIM_CANON = relaxed
486 DKIM_SELECTOR = li
487
488 # from comments in
489 # https://debian-administration.org/article/718/DKIM-signing_outgoing_mail_with_exim4
490
491 # The file is based on the outgoing domain-name in the from-header.
492 DKIM_DOMAIN = \${lc:\${domain:\$h_from:}}
493 # sign if key exists
494 DKIM_PRIVATE_KEY= \${if exists{/etc/exim4/\${dkim_domain}-private.pem} {/etc/exim4/\${dkim_domain}-private.pem}}
495
496
497 # failing message on mail-tester.com:
498 # We check if there is a server (A Record) behind your hostname kd.
499 # You may want to publish a DNS record (A type) for the hostname kd or use a different hostname in your mail software
500 # https://serverfault.com/questions/46545/how-do-i-change-exim4s-primary-hostname-on-a-debian-box
501 # and this one seemed appropriate from grepping config.
502 # I originally set this to li.iankelling.org, but then ended up with errors when li tried to send
503 # mail to kd, so this should basically be a name that no host has as their
504 # canonical hostname since the actual host sits behind a nat and changes.
505 # Seems logical for this to be the same as mailname.
506 MAIN_HARDCODE_PRIMARY_HOSTNAME = mail.iankelling.org
507
508 # normally empty, I set this so I can set the envelope address
509 # when doing mail redelivery to invoke filters
510 MAIN_TRUSTED_GROUPS = $u
511
512 LOCAL_DELIVERY = dovecot_lmtp
513
514 # options exim has to avoid having to alter the default config files
515 CHECK_RCPT_LOCAL_ACL_FILE = /etc/exim4/rcpt_local_acl
516 CHECK_DATA_LOCAL_ACL_FILE = /etc/exim4/data_local_acl
517
518 # debian exim config added this in 2016 or so?
519 # it's part of the smtp spec, to limit lines to 998 chars
520 # but a fair amount of legit mail does not adhere to it. I don't think
521 # this should be default, like it says in
522 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=828801
523 # todo: the bug for introducing this was about headers, but
524 # the fix maybe is for all lines? one says gmail rejects, the
525 # other says gmail does not reject. figure out and open a new bug.
526 IGNORE_SMTP_LINE_LENGTH_LIMIT = true
527
528 # most of the ones that gmail seems to use.
529 # Exim has horrible default of signing unincluded
530 # list- headers since they got mentioned in an
531 # rfc, but this messes up mailing lists, like gnu/debian which want to
532 # keep your dkim signature intact but add list- headers.
533 DKIM_SIGN_HEADERS = mime-version:in-reply-to:references:from:date:subject:to
534
535 # recommended if dns is expected to work
536 CHECK_RCPT_VERIFY_SENDER = true
537 # seems like a good idea
538 CHECK_DATA_VERIFY_HEADER_SENDER = true
539 CHECK_RCPT_SPF = true
540 CHECK_RCPT_REVERSE_DNS = true
541 CHECK_MAIL_HELO_ISSUED = true
542
543 MAIN_LOG_SELECTOR = +all
544
545 # testing dmarc
546 #dmarc_tld_file = /etc/public_suffix_list.dat
547 EOF
548
549 f=/etc/cron.daily/refresh-dmarc-tld-file
550 cat >$f <<'EOF'
551 #!/bin/bash
552 cd /etc
553 wget -q -N https://publicsuffix.org/list/public_suffix_list.dat
554 EOF
555 chmod 755 $f
556
557 sed -i --follow-symlinks -f - /etc/aliases <<EOF
558 \$a root: $postmaster
559 /^root:/d
560 EOF
561
562 ####### begin dovecot setup ########
563 # based on a little google and package search, just the dovecot
564 # packages we need instead of dovecot-common.
565 #
566 # dovecot-lmtpd is for exim to deliver to dovecot instead of maildir
567 # directly. The reason to do this is to use dovecot\'s sieve, which
568 # has extensions that allow it to be almost equivalent to exim\'s
569 # filter capabilities, some ways probably better, some worse, and
570 # sieve has the benefit of being supported in postfix and
571 # proprietary/weird environments, so there is more examples on the
572 # internet. I was torn about whether to do this or not, meh.
573 pi dovecot-core dovecot-imapd dovecot-sieve dovecot-lmtpd
574
575 for f in /p/c/subdir_files/sieve/*sieve /a/c/subdir_files/sieve/*sieve; do
576 sudo -u $u /a/exe/lnf -T $f $(eval echo ~$u)/sieve/${f##*/}
577 done
578 # if we changed 90-sieve.conf and removed the active part of the
579 # sieve option, we wouldn\'t need this, but I\'d rather not modify a
580 # default config if not needed. This won\'t work as a symlink in /a/c
581 # unfortunately.
582 sudo -u $u /a/exe/lnf -T sieve/main.sieve $(eval echo ~$u)/.dovecot.sieve
583
584 # we set this later in local.conf
585 sed -ri -f - /etc/dovecot/conf.d/10-mail.conf <<'EOF'
586 /^\s*mail_location\s*=/d
587 EOF
588
589 cat >/etc/dovecot/conf.d/20-lmtp.conf <<EOF
590 protocol lmtp {
591 #per https://wiki2.dovecot.org/Pigeonhole/Sieve/Configuration
592 mail_plugins = \$mail_plugins sieve
593 # default was
594 #mail_plugins = \$mail_plugins
595
596 # For a normal setup with exim, we need something like this, which
597 # removes the domain part
598 # auth_username_format = %Ln
599 #
600 # or else # Exim says something like
601 # "LMTP error after RCPT ... 550 ... User doesn't exist someuser@somedomain"
602 # Dovecot verbose log says something like
603 # "auth-worker(9048): passwd(someuser@somedomain): unknown user"
604 # reference: http://wiki.dovecot.org/LMTP/Exim
605 #
606 # However, I use this to direct all mail to the same inbox.
607 # A normal way to do this, which I did at first is to have
608 # a router in exim almost at the end, eg 950,
609 #local_catchall:
610 # debug_print = "R: catchall for \$local_part@\$domain"
611 # driver = redirect
612 # domains = +local_domains
613 # data = $u
614 # based on
615 # http://blog.alteholz.eu/2015/04/exim4-and-catchall-email-address/
616 # with superflous options removed.
617 # However, this causes the envelope to be rewritten,
618 # which makes filtering into mailboxes a little less robust or more complicated,
619 # so I've done it this way instead. it also requires
620 # modifying the local router in exim.
621 auth_username_format = $u
622 }
623
624 EOF
625
626
627 cat >/etc/dovecot/local.conf <<EOF
628 # so I can use a different login that my shell login for mail. this is
629 # worth doing solely for the reason that if this login is compromised,
630 # it won't also compromise my shell password.
631 !include conf.d/auth-passwdfile.conf.ext
632
633 # settings derived from wiki and 10-ssl.conf
634 ssl = required
635 ssl_cert = </etc/exim4/exim.crt
636 ssl_key = </etc/exim4/exim.key
637 # https://github.com/certbot/certbot/raw/master/certbot-apache/certbot_apache/options-ssl-apache.conf
638 # in my cert cronjob, I check if that has changed upstream.
639 ssl_cipher_list = ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS
640
641 # ian: added this, more secure, per google etc
642 ssl_prefer_server_ciphers = yes
643
644
645 mail_location = maildir:/m/%u:LAYOUT=fs:INBOX=/m/%u/INBOX
646 mail_uid = $u
647 mail_gid = $u
648
649 # for debugging info, uncomment these.
650 # logs go to syslog and to /var/log/mail.log
651 # auth_verbose=yes
652 #mail_debug=yes
653 EOF
654 ####### end dovecot setup ########
655
656 # https://selivan.github.io/2017/12/30/systemd-serice-always-restart.html
657 d=/etc/systemd/system/openvpn@mail
658 mkdir -p $d
659 cat >$d/override.conf <<'EOF'
660 [Service]
661 Restart=always
662 # time to sleep before restarting a service
663 RestartSec=1
664
665 [Unit]
666 # StartLimitIntervalSec in recent systemd versions
667 StartLimitInterval=0
668 EOF
669
670
671
672 systemctl enable mailclean.timer
673 systemctl start mailclean.timer
674 systemctl restart $vpn_ser@mail
675 systemctl enable $vpn_ser@mail
676 systemctl enable dovecot
677 systemctl restart dovecot
678
679 else # $HOSTNAME != $MAIL_HOST
680 # remove mail. 2 lines to properly remove whitespace
681 sed -ri -f - /etc/hosts <<'EOF'
682 s#^(127\.0\.1\.1 .*) +mail\.iankelling\.org$#\1#
683 s#^(127\.0\.1\.1 .*)mail\.iankelling\.org +(.*)#\1\2#
684 EOF
685
686 echo | /a/exe/cedit mail /etc/dnsmasq-servers.conf || [[ $? == 1 ]]
687 systemctl reload dnsmasq
688
689 systemctl disable mailclean.timer &>/dev/null ||:
690 systemctl stop mailclean.timer &>/dev/null ||:
691 systemctl disable $vpn_ser@mail
692 systemctl stop $vpn_ser@mail
693 systemctl disable dovecot ||:
694 systemctl stop dovecot ||:
695 #
696 #
697 # would only exist because I wrote it i the previous condition,
698 # it\'s not part of exim
699 rm -f /etc/exim4/conf.d/main/000_localmacros
700 cat >>/etc/exim4/update-exim4.conf.conf <<EOF
701 dc_eximconfig_configtype='smarthost'
702 dc_smarthost='$smarthost'
703 # The manpage incorrectly states this will do header rewriting, but
704 # that only happens if we have dc_hide_mailname is set.
705 dc_readhost='iankelling.org'
706 # Only used in case of bounces.
707 dc_localdelivery='maildir_home'
708 EOF
709
710 hostname -f > /etc/mailname
711
712 # We set this to alerts on MAIL_HOST, but using a user that doesn't exist elsewhere
713 # is no good.
714 sed -i --follow-symlinks -f - /etc/aliases <<EOF
715 \$a root:
716 /^root:/d
717 EOF
718
719 fi # end $HOSTNAME != $MAIL_HOST
720
721 systemctl reload exim4
722
723 # i have the spool directory be common to distro multi-boot, so
724 # we need the uid to be the same. 608 cuz it's kind of in the middle
725 # of the free system uids.
726 IFS=:; read _ _ uid _ < <(getent passwd Debian-exim ||:) ||:; unset IFS
727 IFS=:; read _ _ gid _ < <(getent group Debian-exim ||:) ||:; unset IFS
728 if [[ ! $uid ]]; then
729 # from /var/lib/dpkg/info/exim4-base.postinst, plus uid and gid options
730 adduser --uid 608 --system --group --quiet --home /var/spool/exim4 \
731 --no-create-home --disabled-login --force-badname Debian-exim
732 elif [[ $uid != 608 ]]; then
733 systemctl stop exim4 ||:
734 usermod -u 608 Debian-exim
735 groupmod -g 608 Debian-exim
736 usermod -g 608 Debian-exim
737 find / /nocow -xdev -uid $uid -exec chown -h 608 {} +
738 find / /nocow -xdev -gid $gid -exec chgrp -h 608 {} +
739 fi
740
741
742
743 ##### begin spamassassin config
744 systemctl enable spamassassin
745 # per readme.debian
746 sed -i '/^\s*CRON\s*=/d' /etc/default/spamassassin
747 e CRON=1 >>/etc/default/spamassassin
748 # just noticed this in the config file, seems like a good idea.
749 sed -i '/^\s*NICE\s*=/d' /etc/default/spamassassin
750 e 'NICE="--nicelevel 15"' >>/etc/default/spamassassin
751 systemctl start spamassassin
752 systemctl reload spamassassin
753
754 cat >/etc/systemd/system/spamddnsfix.service <<'EOF'
755 [Unit]
756 Description=spamd dns bug fix cronjob
757
758 [Service]
759 Type=oneshot
760 ExecStart=/a/bin/distro-setup/spamd-dns-fix
761 EOF
762 # 2017-09, debian closed the bug on this saying upstream had fixed it.
763 # remove this when i\'m using the newer package, ie, debian 10, or maybe
764 # ubuntu 18.04.
765 cat >/etc/systemd/system/spamddnsfix.timer <<'EOF'
766 [Unit]
767 Description=run spamd bug fix script every 10 minutes
768
769 [Timer]
770 OnActiveSec=60
771 # the script looks back 9 minutes into the journal,
772 # it takes a second to run,
773 # so lets run every 9 minutes and 10 seconds.
774 OnUnitActiveSec=550
775
776 [Install]
777 WantedBy=timers.target
778 EOF
779 systemctl daemon-reload
780 systemctl restart spamddnsfix.timer
781 systemctl enable spamddnsfix.timer
782 #
783 ##### end spamassassin config
784
785
786
787
788
789 # https://blog.dhampir.no/content/make-exim4-on-debian-respect-forward-and-etcaliases-when-using-a-smarthost
790 # i only need .forwards, so just doing that one.
791 cd /etc/exim4/conf.d/router
792 b=userforward_higher_priority
793 # replace the router name so it is unique
794 sed -r s/^\\S+:/$b:/ 600_exim4-config_userforward >175_$b
795
796 # begin setup passwd.client
797 f=/etc/exim4/passwd.client
798 rm -f /etc/exim4/passwd.client
799 install -m 640 -g Debian-exim /dev/null $f
800 while read -r domain _ pass; do
801 # reference: exim4_passwd_client(5)
802 printf "%s:%s\n" "$domain" "$pass" >>$f
803 done </etc/mailpass
804 # end setup passwd.client
805
806 # by default, only 10 days of logs are kept. increase that.
807 sed -ri 's/^(\s*rotate\s).*/\11000/' /etc/logrotate.d/exim4-base
808
809 systemctl restart exim4
810
811
812
813 # /etc/alias setup is debian specific, and exim postinst script sets up
814 # an /etc/alias from root to the postmaster, based on the question
815 # exim4-config exim4/dc_postmaster, as long as there exists an entry for
816 # root, or there was no preexisting aliases file. postfix won\'t set up
817 # a root to $postmaster alias if it\'s already installed. Easiest to
818 # just set it ourselves.
819
820 # debconf question for postmaster:
821 # Mail for the 'postmaster', 'root', and other system accounts needs to be redirected
822 # to the user account of the actual system administrator.
823 # If this value is left empty, such mail will be saved in /var/mail/mail, which is not
824 # recommended.
825 # Note that postmaster\'s mail should be read on the system to which it is directed,
826 # rather than being forwarded elsewhere, so (at least one of) the users listed here
827 # should not redirect their mail off this machine. A 'real-' prefix can be used to
828 # force local delivery.
829 # Multiple user names need to be separated by spaces.
830 # Root and postmaster mail recipient:
831
832 # local mail that bounces goes to /Maildir or /root/Maildir
833 dirs=(/m/md/bounces/{cur,tmp,new})
834 mkdir -p ${dirs[@]}
835 chown -R $u:Debian-exim /m/md/bounces
836 chmod 775 ${dirs[@]}
837 usermod -a -G Debian-exim $u
838 for d in /Maildir /root/Maildir; do
839 if [[ ! -L $d ]]; then
840 rm -rf $d
841 fi
842 ln -sf -T /m/md/bounces $d
843 done
844
845 sudo -u $u ln -sf -T /m/.mu /home/$u/.mu
846
847 # put spool dir in directory that spans multiple distros.
848 # based on http://www.postfix.org/qmgr.8.html and my notes in gnus
849 #
850 # todo: I\'m suspicious of uids for Debian-exim being the same across
851 # distros. It would be good to test this.
852 dir=/nocow/exim4
853 sdir=/var/spool/exim4
854 # we only do this if our system has $dir
855
856 # this used to do a symlink, but, in the boot logs, /nocow would get mounted succesfully,
857 # about 2 seconds later, exim starts, and immediately puts into paniclog:
858 # honVi-0000u3-82 Failed to create directory "/var/spool/exim4/input": No such file or directory
859 # so, im trying a bind mount to get rid of that.
860 if [[ -e /nocow ]]; then
861 if ! grep -Fx "/nocow/exim4 /var/spool/exim4 none bind 0 0" /etc/fstab; then
862 echo "/nocow/exim4 /var/spool/exim4 none bind 0 0" >> /etc/fstab
863 fi
864 if ! mountpoint -q $sdir; then
865 systemctl stop exim4
866 if [[ -L $sdir ]]; then
867 rm $sdir
868 fi
869 if [[ ! -e $dir && -d $sdir ]]; then
870 mv $sdir $dir
871 fi
872 if [[ ! -d $sdir ]]; then
873 mkdir $sdir
874 chmod 000 $sdir # only want it to be used when its mounted
875 fi
876 mount $sdir
877 fi
878 fi
879
880
881
882 systemctl restart exim4
883 systemctl enable exim4
884
885
886 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
887 f=/usr/local/bin/send-test-forward
888 cat >$f <<'EOFOUTER'
889 #!/bin/bash
890 /usr/sbin/exim -t <<EOF
891 From: ian@iankelling.org
892 To: iank@posteo.de
893 Subject: primary_test $(date +%s) $(date +%Y-%m-%dT%H:%M:%S%z)
894
895 eom
896 EOF
897 EOFOUTER
898 chmod +x $f
899
900 cat >/etc/cron.d/mailtest <<EOF
901 SHELL=/bin/bash
902 # running as user just because no need to run as root
903 */10 * * * * $u $f 2>&1 | /usr/local/bin/log-once send-test-forward
904 */10 * * * * $u /usr/local/bin/mailtest-check 2>&1 | /usr/local/bin/log-once -1 mailtest-check
905 */10 * * * * root chmod -R g+rw /m/md/bounces 2>&1 | /usr/local/bin/log-once -1 bounces-chmod
906 EOF
907 cp /a/bin/distro-setup/filesystem/usr/local/bin/mailtest-check /usr/local/bin
908 else
909 rm -f /etc/cron.d/mailtest
910 fi
911
912 # MAIL_HOST also does radicale, and easier to start and stop it here
913 # for when MAIL_HOST changes, so radicale gets the synced files and
914 # does not stop us from remounting /o.
915 if dpkg -s radicale &>/dev/null; then
916 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
917 systemctl restart radicale
918 systemctl enable radicale
919 if [[ -e /etc/logrotate.d/radicale.disabled ]]; then
920 mv /etc/logrotate.d/radicale{.disabled,}
921 fi
922 else
923 systemctl stop radicale
924 systemctl disable radicale
925 # weekly logrotate tries to restart radicale even if it's a disabled service in flidas.
926 if [[ -e /etc/logrotate.d/radicale ]]; then
927 mv /etc/logrotate.d/radicale{,.disabled}
928 fi
929 fi
930 fi
931 exit 0
932 :