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