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