improvements
[distro-setup] / mail-setup
1 #!/bin/bash
2 # * intro
3 # Copyright (C) 2019 Ian Kelling
4 # SPDX-License-Identifier: AGPL-3.0-or-later
5
6 # todo: sandbox / harden exim:
7 # 1. stop it from running as root. how?
8 # https://www.exim.org/exim-html-current/doc/html/spec_html/ch-security_considerations.html
9 # * avoid using .forward files, remove that router
10 # * set deliver_drop_privilege
11 # * set user to run as Debian-exim in systemd
12 # * set port to something like 2500, and forward 25 to 2500 with iptables. same for 587.
13 # https://superuser.com/questions/710253/allow-non-root-process-to-bind-to-port-80-and-443/1334552#1334552
14 # * consider whether other routers like postmaster need modification / removal.
15 # 2. restrict its filesystem access from within systemd
16
17 # todo: harden dovecot. need to do some research. one way is for it to only listen on a wireguard vpn interface, so only clients that are on the vpn can access it.
18 # todo: consider hardening cups listening on 0.0.0.0
19 # todo: stop/disable local apache, and rpc.mountd, and kdeconnect when not in use.
20 # todo: check that spamd and unbound only listen locally.
21
22 # todo: hosts should only allow external mail that is authed and
23 # destined for backup route. it is a minor issue since traffic is
24 # limited to the wghole network.
25
26 # todo: emailing info@amnimal.ninja produces a bounce, user doesn't exist
27 # instead of a simple rejection like it should.
28
29 # todo: auto restart of je on checkrestart
30
31 # todo: run mailping test after running, or otherwise
32 # clear out terminal alert
33
34 # todo: reinstall bk with bigger filesystem
35
36 # todo: on bk, dont send email if mailvpn is not up
37
38 # todo: mailtest-check should check on bk too
39
40 # todo: disable postgrey
41
42 # todo: in testforward-check, we should also look
43
44 # todo: test that bounces dont help create valid mailtest-check
45
46 # todo: move mail stuff in distro-end into this file
47
48 # todo: consider rotating dkim & publishing key so every past email I sent
49 # isnt necessarily signed
50
51 # todo: consider how to get clamav out of Debian-exim group
52 # so it cant read/write the whole mail spool, for better
53 # security.
54
55 # todo: create a cronjob to update or warn on expiring dnssec keys
56
57 # todo: we should test failed mail daily or so
58 # failed cronjob, failed sysd-log-once,
59 # a local bounce from a cronjob, a local bounce
60 # to a bad remote address, perhaps a local failure
61 # when the sending daemon is down.
62 # And send an alert email if no alerts have been sent
63 # in 2 or 3 days or something. todo, test cron mail on li.
64
65 # todo: look at mailinabox extra dns records, note these changelogs:
66 # - An MTA-STS policy for incoming mail is now published (in DNS and over HTTPS) when the primary hostname and email address domain both have a signed TLS certificate installed, allowing senders to know that an encrypted connection should be enforced.
67 # - The per-IP connection limit to the IMAP server has been doubled to allow more devices to connect at once, especially with multiple users behind a NAT.
68 #
69
70 # todo: mailtest-check failure on remote hosts is not going to alert me.
71 # sort that out.
72 # todo: test mail failure as well as success.
73 #
74 # todo: validate that mailtest-check is doing dnsbl checks.
75
76 # background: I want to run exim in a network namespace so it can send
77 # and receive through a vpn. This is needed so it can do ipv6, because
78 # outside the namespace if we dont have ipv6, to send ipv6 through the
79 # vpn, we have to send all our ipv6 through the vpn. I did this for a
80 # long time, it was fine, but it causes various pains, like increased
81 # latency, increased recaptcha because my ip is from a data center, just
82 # various issues I dont want on all the time. The problem with the
83 # namespace is that all kinds of programs want to invoke exim, but they
84 # wont be in the namespace. I could replace exim with a wrapper that
85 # jumps into the namespace, i tried that, it works fine. One remaining
86 # problem was that I would have needed to hook into exim upgrades to
87 # move exim and replace it with my wrapper script. Also, my script to
88 # join the namespace is not super reliable because it uses a pgrep.
89 # Instead, I should have created a systemd service for a process that
90 # will never die and just writes its pid somewhere convenient.
91 # That implementation
92 # is below here:
93 #
94 # sudoers:
95 # user ALL=(ALL) /usr/sbin/exim4
96 #
97 # move exim4 to eximian, use this script for exim4:
98 #
99 # #!/bin/bash
100 # if ip a show veth1-mail &>/dev/null; then
101 # /usr/sbin/eximian "$@"
102 # exit
103 # fi
104 # dosudo=false
105 # if [[ $USER && $USER != root ]]; then
106 # dosudo=true
107 # fi
108 # pid=$(pgrep -f "/usr/sbin/openvpn .* --config /etc/openvpn/.*mail.conf")
109 # if $dosudo; then
110 # sudo nsenter -t $pid -n -m sudo -u $USER /usr/sbin/eximian "$@"
111 # else
112 # nsenter -t $pid -n -m /usr/sbin/eximian "$@"
113 # fi
114 # ## end script
115 #
116 # an alternate solution: there is a small setguid program for
117 # network namespaces in my bookmarks.
118 #
119 # However, the solution I went with is: have 2 exim
120 # configs. A nonstandard location for the daemon that runs
121 # in the namespace. For all other invocations, it uses
122 # the default config location, which is altered to be
123 # in a smarthost config which sends mail to the deaemon.
124 #
125 # I have a bash function, enn to invoke exim like the daemon is running.
126 # and mailbash to just enter its network namespace.
127
128 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
129
130 shopt -s nullglob
131
132 if [[ -s /usr/local/lib/err ]]; then
133 source /usr/local/lib/err
134 elif [[ -s /a/bin/errhandle/err ]]; then
135 source /a/bin/errhandle/err
136 else
137 err "no err tracing script found"
138 fi
139 source /a/bin/distro-functions/src/identify-distros
140 source /a/bin/distro-functions/src/package-manager-abstractions
141
142 # has nextcloud_admin_pass in it
143 f=/p/c/machine_specific/$HOSTNAME/mail
144 if [[ -e $f ]]; then
145 # shellcheck source=/p/c/machine_specific/bk/mail
146 source $f
147 fi
148
149
150 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
151
152
153 u=$(id -nu 1000)
154
155
156 usage() {
157 cat <<EOF
158 Usage: ${0##*/} anything_here_to_debug
159 Setup exim4 & dovecot & related things
160
161 -h|--help Print help and exit.
162 EOF
163 exit $1
164 }
165
166 # debug output if we pass any arg
167 if (( $# )); then
168 set -x
169 fi
170
171
172 ####### instructions for icedove #####
173 # Incoming mail server: mail.iankelling.org, port 143, username iank, connection security starttls, authentication method normal password,
174 # then click advanced so it accepts it.
175 # we could also just use 127.0.0.1 with no ssl
176 #
177 # hamburger -> preferences -> preferences -> advanced tab -> config editor button -> security.ssl.enable_ocsp_must_staple = false
178 # background: dovecot does not yet have ocsp stapling support
179 # reference: https://community.letsencrypt.org/t/simple-guide-using-lets-encrypt-ssl-certs-with-dovecot/2921
180 #
181 # for phone, k9mail, same thing but username alerts, pass in ivy-pass.
182 # also, bk.b8.nz for secondary alerts, username is iank. same alerts pass.
183 # fetching mail settings: folder poll frequency 10 minutes
184 #######
185
186
187 # * perstent password instructions
188 # Note: for cert cron, we need to manually run first to accept known_hosts
189
190 # # exim passwords:
191 # # for hosts which have all private files I just use the same user
192 # # for other hosts, each one get\'s their own password.
193 # # for generating secure pass, and storing for server too:
194 # f=$(mktemp)
195 # host=tp
196 # apg -m 50 -x 70 -n 1 -a 1 -M CLN >$f
197 # s sed -i "/^$host:/d" /p/c/filesystem/etc/exim4/passwd
198 # echo "$host:$(mkpasswd -m sha-512 -s <$f)" >>/p/c/filesystem/etc/exim4/passwd
199 # #reference: exim4_passwd_client(5)
200 # dir=/p/c/machine_specific/$host/filesystem/etc/exim4
201 # mkdir -p $dir
202 # echo "mail.iankelling.org:$host:$(<$f)" > $dir/passwd.client
203 # # then run this script
204
205 # # dovecot password, i just need 1 as I\'m the only user
206 # mkdir /p/c/filesystem/etc/dovecot
207 # echo "iank:$(doveadm pw -s SHA512-CRYPT)::::::" >>/p/c/filesystem/etc/dovecot/users
208
209 ####### end perstent password instructions ######
210
211
212 # * dkim dns
213 # # Remove 1 level of comments in this section, set the domain var
214 # # for the domain you are setting up, then run this and copy dns settings
215 # # into dns.
216 # domain=iankelling.org
217 # c /p/c/filesystem/etc/exim4
218 # # this has several bugs addressed in comments, but it was helpful
219 # # https://debian-administration.org/article/718/DKIM-signing_outgoing_mail_with_exim4
220
221 # openssl genrsa -out $domain-private.pem 2048
222 # # Then, to get the public key strings to put in bind:
223
224 # # selector is needed for having multiple keys for one domain.
225 # # I dun do that, so just use a static one: li
226 # # Debadmin page does not have v=, fastmail does, and this
227 # # says it\'s recommended in 3.6.1, default is DKIM1 anyways.
228 # # https://www.ietf.org/rfc/rfc6376.txt
229 # # Join and print all but first and last line.
230 # # last line: swap hold & pattern, remove newlines, print.
231 # # lines 2+: append to hold space
232 # echo "bind txt record: remember to truncate $domain so its relative to the bind zone"
233 # cat <<EOF
234 # a._domainkey.$domain TXT (
235 # "v=DKIM1\059 k=rsa\059 p=$(openssl rsa -in $domain-private.pem -pubout |&sed -rn '${x;s/\n//g;s/^(.*)(.{240}$)/\1"\n"\2/p};3,$H')" )
236 # EOF
237 # # sed explanation: skip the first few lines, then put them into the hold space, then
238 # # on the last line, back to the patern space, remove the newlines, then add a newline
239 # # at the last char - 240, because bind txt records need strings <=255 chars,
240 # # other dkim stuff at the begining is is 25 chars, and the pubkey is 393, so this
241 # # leaves us a bit of extra room at the end and a bunch at the beginning.
242
243 # # selector was also put into /etc/exim4/conf.d/main/000_local,
244
245 # * dmarc dns
246
247 # # 2017-02 dmarc policies:
248 # # host -t txt _dmarc.gmail.com
249 # # yahoo: p=reject, hotmail: p=none, gmail: p=none, fastmail none for legacy reasons
250 # # there were articles claiming gmail would be changing
251 # # to p=reject, in early 2017, which didn\'t happen. I see no sources on them. It\'s
252 # # expected to cause problems
253 # # with a few old mailing lists, copying theirs for now.
254 #
255 # echo "dmarc dns, name: _dmarc value: v=DMARC1; p=none; rua=mailto:mailauth-reports@$domain"
256
257 # * other dns
258
259 # # 2017-02 spf policies:
260 # # host -t txt lists.fedoraproject.org
261 # # google ~all, hotmail ~all, yahoo: ?all, fastmail ?all, outlook ~all
262 # # i include fastmail\'s settings, per their instructions,
263 # # and follow their policy. In mail in a box, or similar instructions,
264 # # I\'ve seen recommended to not use a restrictive policy.
265
266 # # to check if dns has updated, you do
267 # host -a mesmtp._domainkey.$domain
268
269 # # mx records,
270 # # setting it to iankelling.org would work the same, but this
271 # # is more flexible, I could change where mail.iankelling.org pointed.
272 # cat <<'EOF'
273 # mx records, 2 records each, for * and empty domain
274 # pri 10 mail.iankelling.org
275 # EOF
276
277 # # dnssec
278 # from brc2, run dnsecgen then dsign, update named.local.conf, publish keys to registrar
279
280 # * functions & constants
281
282 pre="${0##*/}:"
283 m() { printf "$pre %s\n" "$*"; "$@"; }
284 e() { printf "$pre %s\n" "$*"; }
285 err() { printf "$pre %s\n" "$*" >&2; exit 1; }
286
287 reload=false
288 # This file is so if we fail in the middle and rerun, we dont lose state
289 if [[ -e /var/local/mail-setup-reload ]]; then
290 reload=true
291 fi
292 i() { # install file
293 local tmp tmpdir dest="$1"
294 local base="${dest##*/}"
295 mkdir -p ${dest%/*}
296 ir=false # i result
297 tmpdir=$(mktemp -d)
298 cat >$tmpdir/"$base"
299 tmp=$(rsync -ic $tmpdir/"$base" "$dest")
300 if [[ $tmp ]]; then
301 printf "%s\n" "$tmp"
302 ir=true
303 if [[ $dest == /etc/systemd/system/* ]]; then
304 touch /var/local/mail-setup-reload
305 reload=true
306 fi
307 fi
308 rm -rf $tmpdir
309 }
310 setini() {
311 key="$1" value="$2" section="$3"
312 file="/etc/radicale/config"
313 sed -ri "/ *\[$section\]/,/^ *\[[^]]+\]/{/^\s*${key}[[:space:]=]/d};/ *\[$section\]/a $key = $value" "$file"
314 }
315 soff () {
316 for service; do
317 # ignore services that dont exist
318 if systemctl cat $service &>/dev/null; then
319 m systemctl disable --now $service
320 fi
321 done
322 }
323 sre() {
324 for service; do
325 m systemctl restart $service
326 m systemctl enable $service;
327 done
328 }
329 mailhost() {
330 [[ $HOSTNAME == "$MAIL_HOST" ]]
331 }
332 e() { printf "%s\n" "$*"; }
333 reifactive() {
334 for service; do
335 if systemctl is-active $service >/dev/null; then
336 m systemctl restart $service
337 fi
338 done
339 }
340 stopifactive() {
341 for service; do
342 if systemctl is-active $service >/dev/null; then
343 m systemctl stop $service
344 fi
345 done
346 }
347
348 mxhost=mx.iankelling.org
349 mxport=587
350 forward=$u@$mxhost
351
352 # old setup. left as comment for example
353 # mxhost=mail.messagingengine.com
354 # mxport=587
355 # forward=ian@iankelling.org
356
357 smarthost="$mxhost::$mxport"
358 uhome=$(eval echo ~$u)
359
360 # Somehow on one machine, a file got written with 664 perms.
361 # just being defensive here.
362 umask 0022
363
364 source /a/bin/bash_unpublished/source-state
365 if [[ ! $MAIL_HOST ]]; then
366 err "\$MAIL_HOST not set"
367 fi
368
369 bhost_t=false
370 case $HOSTNAME in
371 $MAIL_HOST) : ;;
372 kd|frodo|x2|x3|kw|sy)
373 bhost_t=true
374 ;;
375 esac
376
377
378 # * Install universal packages
379
380
381 # installs epanicclean
382 /a/bin/ds/install-my-scripts
383
384 if [[ $(debian-codename-compat) == bionic ]]; then
385 cat >/etc/apt/preferences.d/spamassassin <<'EOF'
386 Package: spamassassin sa-compile spamc
387 Pin: release n=focal,o=Ubuntu
388 Pin-Priority: 500
389 EOF
390 fi
391
392 # light version of exim does not have sasl auth support.
393 pi-nostart exim4 exim4-daemon-heavy spamassassin openvpn unbound clamav-daemon wireguard
394
395 # note: pyzor debian readme says you need to run some initialization command
396 # but its outdated.
397 pi spf-tools-perl p0f postgrey pyzor razor jq moreutils certbot
398 # bad packages that sometimes get automatically installed
399 pu openresolv resolvconf
400
401 soff openvpn
402
403
404 if [[ $(debian-codename) == etiona ]]; then
405 # ip6tables stopped loading on boot. openvpn has reduced capability set,
406 # so running iptables as part of openvpn startup wont work. This should do it.
407 pi iptables-persistent
408 cat >/etc/iptables/rules.v6 <<'EOF'
409 *mangle
410 COMMIT
411 *nat
412 COMMIT
413 EOF
414 # load it now.
415 m ip6tables -S >/dev/null
416 fi
417
418 # our nostart pi fails to avoid enabling
419
420
421 # * user forward file
422 case $HOSTNAME in
423 $MAIL_HOST)
424 # afaik, these will get ignored on MAIL_HOST because they are routing to my own
425 # machine, but rm them is safer
426 rm -fv $uhome/.forward /root/.forward
427 ;;
428 *)
429 # this can\'t be a symlink and has permission restrictions
430 # it might work in /etc/aliases, but this seems more proper.
431 e setting $uhome/.forward to $forward
432 install -m 644 {-o,-g}$u <(e $forward) $uhome/.forward
433 ;;
434 esac
435
436 # * Mail clean cronjob
437
438 i /etc/systemd/system/mailclean.timer <<'EOF'
439 [Unit]
440 Description=Run mailclean daily
441
442 [Timer]
443 OnCalendar=monthly
444
445 [Install]
446 WantedBy=timers.target
447 EOF
448
449 i /etc/systemd/system/mailclean.service <<EOF
450 [Unit]
451 Description=Delete and archive old mail files
452 After=multi-user.target
453
454 [Service]
455 User=$u
456 Type=oneshot
457 ExecStart=/usr/local/bin/sysd-mail-once mailclean /a/bin/distro-setup/mailclean
458 EOF
459
460 # * postgrey
461
462
463 i /etc/default/postgrey <<'EOF'
464 POSTGREY_OPTS="--exim --unix=/var/run/postgrey/postgrey.sock --retry-window=4 --max-age=60"
465 EOF
466
467 # * clamav
468
469 m usermod -a -G Debian-exim clamav
470
471 i /etc/systemd/system/clamav-daemon.service.d/fix.conf <<EOF
472 [Service]
473 ExecStartPre=-/bin/mkdir /var/run/clamav
474 ExecStartPre=/bin/chown clamav /var/run/clamav
475 EOF
476
477 # * mail vpn config
478
479 # old.
480 #vpnser=mailvpn.service
481 # todo: this hangs if it cant resolv the endpoint. we
482 # want it to just retry in the background.
483 vpnser=wg-quick@wgmail.service
484
485 case $HOSTNAME in
486 $MAIL_HOST)
487 rsync -aiSAX --chown=root:root --chmod=g-s /p/c/filesystem/etc/wireguard/ /etc/wireguard
488 bindpaths="/etc/127.0.0.1-resolv:/run/systemd/resolve /etc/basic-nsswitch:/etc/resolved-nsswitch:norbind"
489 ;;&
490 bk)
491 bindpaths="/etc/10.173.8.1-resolv:/etc/127.0.0.1-resolv"
492 ;;&
493 *)
494 d=/p/c/machine_specific/$HOSTNAME/filesystem/etc/wireguard/
495 if [[ -d $d ]]; then
496 rsync -aiSAX --chown=root:root --chmod=g-s $d /etc/wireguard
497 fi
498 ;;
499 esac
500
501 i /etc/systemd/system/wg-quick@wgmail.service.d/override.conf <<EOF
502 [Unit]
503 Requires=mailnn.service
504 After=network.target mailnn.service
505 JoinsNamespaceOf=mailnn.service
506 BindsTo=mailnn.service
507
508 [Service]
509 PrivateNetwork=true
510 # i dont think we need any of these, but it doesnt hurt to stay consistent
511 BindPaths=$bindpaths
512 EOF
513
514 # https://selivan.github.io/2017/12/30/systemd-serice-always-restart.html
515 i /etc/systemd/system/mailvpn.service <<EOF
516 [Unit]
517 Description=OpenVPN tunnel for mail
518 After=syslog.target network-online.target mailnn.service
519 Wants=network-online.target
520 Documentation=man:openvpn(8)
521 Documentation=https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage
522 Documentation=https://community.openvpn.net/openvpn/wiki/HOWTO
523 # needed to continually restatr
524 JoinsNamespaceOf=mailnn.service
525 BindsTo=mailnn.service
526 StartLimitIntervalSec=0
527
528 [Service]
529 Type=notify
530 RuntimeDirectory=openvpn-client
531 RuntimeDirectoryMode=0710
532 WorkingDirectory=/etc/openvpn/client
533 ExecStart=/usr/sbin/openvpn --suppress-timestamps --nobind --config /etc/openvpn/client/mail.conf
534 #CapabilityBoundingSet=CAP_IPC_LOCK CAP_NET_ADMIN CAP_NET_RAW CAP_SETGID CAP_SETUID CAP_SYS_CHROOT CAP_DAC_OVERRIDE
535 LimitNPROC=10
536 # DeviceAllow=/dev/null rw
537 # DeviceAllow=/dev/net/tun rw
538 PrivateNetwork=true
539 # in the network namespace, we cant connect to systemd-resolved on 127.0.0.53,
540 # because of
541 # https://unix.stackexchange.com/questions/445782/how-to-allow-systemd-resolved-to-listen-to-an-interface-other-than-loopback
542 # there is a workaround there, but i dont think its really worth it,
543 # the mail server is fine with a static dns anyways.
544 # This thread is also interesting,
545 # https://github.com/slingamn/namespaced-openvpn/issues/7
546 # todo: the iptables rule at the bottom could be useful to prevent
547 # dns from leaking in my network namespaced vpn.
548 # I also like the idea of patching systemd-resolved so it
549 # will listen on other interfaces, but its not worth my time.
550 BindPaths=$bindpaths
551 Restart=always
552 # time to sleep before restarting a service
553 RestartSec=1
554
555 [Install]
556 WantedBy=multi-user.target
557 EOF
558
559 i /etc/systemd/system/mailnnroute.service <<'EOF'
560 [Unit]
561 Description=Network routing for mailnn
562 After=syslog.target network-online.target mailnn.service
563 Wants=network-online.target
564 JoinsNamespaceOf=mailnn.service
565 BindsTo=mailnn.service
566 StartLimitIntervalSec=0
567
568 [Service]
569 Type=simple
570 RemainAfterExit=true
571 PrivateNetwork=true
572 ExecStart=/usr/bin/flock -w 20 /tmp/newns.flock /a/bin/newns/newns -n 10.173.8 start mail
573 ExecStop=/usr/bin/flock -w 20 /tmp/newns.flock /a/bin/newns/newns stop mail
574 Restart=always
575 RestartSec=10
576
577
578 [Install]
579 WantedBy=multi-user.target
580 EOF
581
582 #
583 i /etc/systemd/system/mailnn.service <<'EOF'
584 [Unit]
585 Description=Network Namespace for mail vpn service that will live forever and cant fail
586 After=syslog.target network-online.target
587 Wants=network-online.target
588
589 [Service]
590 Type=simple
591 PrivateNetwork=true
592 ExecStart=/bin/sleep infinity
593
594 [Install]
595 WantedBy=multi-user.target
596 EOF
597
598 i /etc/systemd/system/mailbindwatchdog.service <<EOF
599 [Unit]
600 Description=Watchdog to restart services relying on systemd-resolved dir
601 After=syslog.target network-online.target
602 Wants=network-online.target
603 BindsTo=mailnn.service
604
605 [Service]
606 Type=simple
607 ExecStart=/usr/local/bin/mailbindwatchdog $vpnser ${nn_progs[@]} unbound.service radicale.service
608 Restart=always
609 # time to sleep before restarting a service
610 RestartSec=1
611
612 [Install]
613 WantedBy=multi-user.target
614 EOF
615
616
617
618 # old service name
619 rm -fv /etc/systemd/system/openvpn-client-mail@.service
620
621 # We use a local unbound because systemd-resolved wont accept our
622 # request, it will only listen to 127.0.0.53 in the main network
623 # namespace, and rejected feature requests to change that (although I
624 # could change the code and recompile), but anyways, that could answer
625 # with things specific to the lan that aren't applicable in this
626 # namespace, and since unbound is a recursive resolver, it means we just
627 # use our own ip against dnsbl rate limits.
628 #
629 # If we ever notice this change, chattr +i on it
630 # trust-ad is used in t10+, glibc 2.31
631
632 i /etc/127.0.0.1-resolv/stub-resolv.conf <<'EOF'
633 nameserver 127.0.0.1
634 options edns0 trust-ad
635 EOF
636
637 i /etc/127.0.0.53-resolv/stub-resolv.conf <<'EOF'
638 nameserver 127.0.0.53
639 options edns0 trust-ad
640 EOF
641
642
643 i /etc/10.173.8.1-resolv/stub-resolv.conf <<'EOF'
644 nameserver 10.173.8.1
645 options edns0 trust-ad
646 EOF
647
648 # this is just a bug fix for trisquel.
649 f=/etc/apparmor.d/usr.sbin.unbound
650 line="/usr/sbin/unbound flags=(attach_disconnected) {"
651 if ! grep -qFx "$line" $f; then
652 badline="/usr/sbin/unbound {"
653 if ! grep -qFx "$badline" $f; then
654 err expected line in $f not found
655 fi
656 sed -i "s,^$badline$,$line," $f
657 if systemctl is-active apparmor &>/dev/null; then
658 m systemctl reload apparmor
659 fi
660 fi
661
662 # note: anything added to nn_progs needs corresponding rm
663 # down below in the host switch
664 nn_progs=(exim4)
665 if mailhost; then
666 # Note dovecots lmtp doesnt need to be in the same nn to accept delivery.
667 # Its in the nn so remote clients can connect to it.
668 nn_progs+=(spamassassin dovecot)
669 fi
670
671 case $HOSTNAME in
672 $MAIL_HOST)
673 i /etc/systemd/system/unbound.service.d/nn.conf <<EOF
674 [Unit]
675 After=mailnn.service
676 JoinsNamespaceOf=mailnn.service
677 BindsTo=mailnn.service
678 StartLimitIntervalSec=0
679
680 [Service]
681 PrivateNetwork=true
682 # note the nsswitch bind is actually not needed for bk, but
683 # its the same file so it does no harm.
684 BindPaths=$bindpaths
685
686 Restart=always
687 RestartSec=1
688 EOF
689
690 # sooo, there are a few ways to get traffic from the mail network
691 # namespace to go over the wghole.
692 #
693 #1: unify the mail vpn and wghole
694 # into 1 network. this seems simple and logical, so I'm doing it.
695 # One general downside is tying things together, if I need to mess
696 # with one thing, it breaks the other. Oh well for now.
697 #
698 # 2. We can route 10.5.3.0/24 out of the mail nn and nat it into wghole.
699 #
700 # 3. We can setup the routing to happen on li, which seemed like I
701 # just needed to add 10.8.0.4/24 to AllowedIPs in at least the
702 # wghole clients, but I think that is kind of hacky and breaks ipv4
703 # routing within the mailvpn, it happened to work just because exim
704 # prefers ipv6 and that was also available in the mailvpn.
705 #
706 # 4. Put the hole interface into the mail network namespace. This
707 # doesn't work if the mail vpn is wg. For openvpn, it bypasses the
708 # vpn routing and establishes a direct connection. I only use the
709 # hole vpn for randomish things, it should be fine to join the mail
710 # nn for that. There should be some way to fix the routing issue
711 # by doing manual routing, but that doesn't seem like a good use of time.
712 # relevant:
713 # https://www.wireguard.com/netns/#
714 #
715 # for wireguard debugging
716 # echo module wireguard +p > /sys/kernel/debug/dynamic_debug/control
717 # dmesg -w
718
719 ;;&
720 $MAIL_HOST|bk)
721 for unit in ${nn_progs[@]}; do
722 i /etc/systemd/system/$unit.service.d/nn.conf <<EOF
723 [Unit]
724 # commented for old openvpn
725 Requires=$vpnser
726 After=network.target mailnn.service $vpnser
727 JoinsNamespaceOf=mailnn.service
728 BindsTo=mailnn.service
729 StartLimitIntervalSec=0
730
731 [Service]
732 PrivateNetwork=true
733 # note the nsswitch bind is actually not needed for bk, but
734 # its the same file so it does no harm.
735 BindPaths=$bindpaths
736
737 Restart=always
738 RestartSec=1
739 EOF
740 done
741 ;;
742 *)
743 for unit in exim4 spamassassin dovecot unbound; do
744 f=/etc/systemd/system/$unit.service.d/nn.conf
745 if [[ -s $f ]]; then
746 rm -fv $f
747 reload=true
748 fi
749 done
750 ;;
751 esac
752
753 # * spamassassin config
754 i /etc/sysctl.d/80-iank-mail.conf <<'EOF'
755 # see exim spec
756 net.netfilter.nf_conntrack_tcp_timeout_close_wait = 120
757 EOF
758 if $ir; then
759 m sysctl -p
760 fi
761
762 i /etc/spamassassin/mylocal.cf <<'EOF'
763 # this is mylocal.cf because the normal local.cf has a bunch of upstream stuff i dont want to mess with
764
765 # /usr/share/doc/exim4-base/README.Debian.gz:
766 # SpamAssassin's default report should not be used in a add_header
767 # statement since it contains empty lines. (This triggers e.g. Amavis'
768 # warning "BAD HEADER SECTION, Improper folded header field made up
769 # entirely of whitespace".) This is a safe, terse alternative:
770 clear_report_template
771 report (_SCORE_ / _REQD_ requ) _TESTSSCORES(,)_ autolearn=_AUTOLEARN
772 uridnsbl_skip_domain iankelling.org
773 uridnsbl_skip_domain amnimal.ninja
774 uridnsbl_skip_domain expertpathologyreview.com
775 uridnsbl_skip_domain zroe.org
776 EOF
777
778 # 2020-10-19 remove old file. remove this when all hosts updated
779 rm -fv /etc/systemd/system/spamddnsfix.{timer,service}
780
781 i /etc/default/spamassassin <<'EOF'
782 # defaults plus debugging flags for an issue im having
783 OPTIONS="--create-prefs --max-children 5 --helper-home-dir"
784 PIDFILE="/var/run/spamd.pid"
785 # my additions
786 NICE="--nicelevel 15"
787 CRON=1
788 EOF
789 ##### end spamassassin config
790
791
792 # * Update mail cert
793 if [[ -e /p/c/filesystem ]]; then
794 # note, man openvpn implies we could just call mail-route on vpn startup/shutdown with
795 # systemd, buuut it can remake the tun device unexpectedly, i got this in the log
796 # after my internet was down for a bit:
797 # NOTE: Pulled options changed on restart, will need to close and reopen TUN/TAP device.
798 m /a/exe/vpn-mk-client-cert -b mailclient -n mail li.iankelling.org
799 fi
800 case $HOSTNAME in
801 bk)
802 if [[ ! -e /etc/openvpn/client/mail.conf ]]; then
803 echo "$0: error: first, on a system with /p/c/filesystem, run mail-setup, or the vpn-mk-client-cert line above this err" 2>&2
804 exit 1
805 fi
806 ;;
807 esac
808
809 m rsync -aiSAX --chown=root:root --chmod=g-s /a/bin/ds/mail-cert-cron /usr/local/bin
810
811 i /etc/systemd/system/mailcert.service <<'EOF'
812 [Unit]
813 Description=Mail cert rsync
814 After=multi-user.target
815
816 [Service]
817 Type=oneshot
818 ExecStart=/usr/local/bin/sysd-mail-once mailcert /usr/local/bin/mail-cert-cron
819 EOF
820 i /etc/systemd/system/mailcert.timer <<'EOF'
821 [Unit]
822 Description=Run mail-cert once a day
823
824 [Timer]
825 OnCalendar=daily
826
827 [Install]
828 WantedBy=timers.target
829 EOF
830
831
832 wghost=${HOSTNAME}wg.b8.nz
833 if $bhost_t && [[ ! -e /etc/exim4/certs/$wghost/privkey.pem ]]; then
834 certbot -n --manual-public-ip-logging-ok --eff-email --agree-tos -m letsencrypt@iankelling.org \
835 certonly --manual --preferred-challenges=dns \
836 --manual-auth-hook /a/bin/ds/le-dns-challenge \
837 --manual-cleanup-hook /a/bin/ds/le-dns-challenge-cleanup \
838 --deploy-hook /a/bin/ds/le-exim-deploy -d $wghost
839 fi
840
841 # * common exim4 config
842
843
844 # Make all system users be aliases. preventative
845 # measure for things like cron mail for user without alias
846 awk 'BEGIN { FS = ":" } ; $6 !~ /^\/home/ { print $1 }' /etc/passwd| while read -r user; do
847 if [[ ! $user ]]; then
848 continue
849 fi
850 if ! grep -q "^$user:" /etc/aliases; then
851 echo "$user: root" |m tee -a /etc/aliases
852 fi
853 done
854
855 if ! grep -q "^ncsoft:" /etc/aliases; then
856 echo "ncsoft: graceq2323@gmail.com" |m tee -a /etc/aliases
857 fi
858
859
860
861 m gpasswd -a iank adm #needed for reading logs
862
863 ### make local bounces go to normal maildir
864 # local mail that bounces goes to /Maildir or /root/Maildir
865 dirs=(/m/md/bounces/{cur,tmp,new})
866 m mkdir -p ${dirs[@]}
867 m chown iank:iank /m /m/md
868 m ln -sfT /m/md /m/iank
869 m chmod 771 /m /m/md
870 m chown -R $u:Debian-exim /m/md/bounces
871 m chmod 775 ${dirs[@]}
872 m usermod -a -G Debian-exim $u
873 for d in /Maildir /root/Maildir; do
874 if [[ ! -L $d ]]; then
875 m rm -rf $d
876 fi
877 m ln -sf -T /m/md/bounces $d
878 done
879
880 # dkim, client passwd file
881 files=(/p/c/machine_specific/$HOSTNAME/filesystem/etc/exim4/*)
882 f=/p/c/filesystem/etc/exim4/passwd.client
883 if [[ -e $f ]]; then
884 files+=($f)
885 fi
886 if (( ${#files[@]} )); then
887 m rsync -ahhi --chown=root:Debian-exim --chmod=0640 \
888 ${files[@]} /etc/exim4
889 fi
890
891 # by default, only 10 days of logs are kept. increase that.
892 m sed -ri 's/^(\s*rotate\s).*/\11000/' /etc/logrotate.d/exim4-base
893
894
895 ## https://blog.dhampir.no/content/make-exim4-on-debian-respect-forward-and-etcaliases-when-using-a-smarthost
896 # i only need .forwards, so just doing that one.
897 cd /etc/exim4/conf.d/router
898 b=userforward_higher_priority
899 # replace the router name so it is unique
900 sed -r s/^\\S+:/$b:/ 600_exim4-config_userforward >175_$b
901
902 # todo, consider 'separate' in etc/exim4.conf, could it help on busy systems?
903
904 # alerts is basically the postmaster address
905 m sed -i --follow-symlinks -f - /etc/aliases <<EOF
906 \$a root: alerts@iankelling.org
907 /^root:/d
908 EOF
909
910 cat >/etc/exim4/conf.d/rewrite/34_iank_rewriting <<'EOF'
911 ncsoft@zroe.org graceq2323@gmail.com hE
912 EOF
913
914 # old name
915 rm -fv /etc/exim4/conf.d/retry/37_retry
916
917 cat >/etc/exim4/conf.d/retry/17_retry <<'EOF'
918 # Retry fast for my own domains
919 iankelling.org * F,1d,10m;F,14d,1h
920 amnimal.ninja * F,1d,10m;F,14d,1h
921 expertpathologyreview.com * F,1d,10m;F,14d,1h
922 je.b8.nz * F,1d,10m;F,14d,1h
923 zroe.org * F,1d,10m;F,14d,1h
924 eximbackup.b8.nz * F,1d,4m;F,14d,1h
925 EOF
926
927
928 rm -vf /etc/exim4/conf.d/main/000_localmacros # old filename
929 cat >/etc/exim4/conf.d/main/000_local <<EOF
930 MAIN_TLS_ENABLE = true
931
932 # require tls connections for all smarthosts
933 REMOTE_SMTP_SMARTHOST_HOSTS_REQUIRE_TLS = *
934
935 # debian exim config added this in 2016 or so?
936 # it's part of the smtp spec, to limit lines to 998 chars
937 # but a fair amount of legit mail does not adhere to it. I don't think
938 # this should be default, like it says in
939 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=828801
940 # todo: the bug for introducing this was about headers, but
941 # the fix maybe is for all lines? one says gmail rejects, the
942 # other says gmail does not reject. figure out and open a new bug.
943 IGNORE_SMTP_LINE_LENGTH_LIMIT = true
944
945 # more verbose logs
946 MAIN_LOG_SELECTOR = +all
947
948 # Based on spec, seems like a good idea to be nice.
949 smtp_return_error_details = true
950
951 # normally empty, I set this so I can set the envelope address
952 # when doing mail redelivery to invoke filters. Also allows
953 # me exiqgrep and stuff.
954 MAIN_TRUSTED_GROUPS = $u
955
956 # default is 10. when exim has been down for a bit, fsf mailserver
957 # will do a big send in one connection, then exim decides to put
958 # the messages in the queue instead of delivering them, to avoid
959 # spawning too many delivery processes. This is the same as the
960 # fsfs value. And the corresponding one for how many messages
961 # to send out in 1 connection remote_max_parallel = 256
962 smtp_accept_queue_per_connection = 500
963
964
965 DKIM_CANON = relaxed
966 DKIM_SELECTOR = li
967
968 # from comments in
969 # https://debian-administration.org/article/718/DKIM-signing_outgoing_mail_with_exim4
970 # and its best for this to align https://tools.ietf.org/html/rfc7489#page-8
971 # There could be some circumstance when the
972 # from: isnt our domain, but the envelope sender is
973 # and so still want to sign, but I cant think of any case.
974 DKIM_DOMAIN = \${lc:\${domain:\$rh_from:}}
975 # The file is based on the outgoing domain-name in the from-header.
976 # sign if key exists
977 DKIM_PRIVATE_KEY = \${if exists{/etc/exim4/\${dkim_domain}-private.pem} {/etc/exim4/\${dkim_domain}-private.pem}}
978
979 # most of the ones that gmail seems to use.
980 # Exim has horrible default of signing unincluded
981 # list- headers since they got mentioned in an
982 # rfc, but this messes up mailing lists, like gnu/debian which want to
983 # keep your dkim signature intact but add list- headers.
984 DKIM_SIGN_HEADERS = mime-version:in-reply-to:references:from:date:subject:to
985
986 domainlist local_hostnames = ! je.b8.nz : ! bk.b8.nz : *.b8.nz : b8.nz
987
988 hostlist iank_trusted = <; \\
989 # veth0
990 10.173.8.1 ; \\
991 # li li_ip6
992 72.14.176.105 ; 2600:3c00::f03c:91ff:fe6d:baf8 ; \\
993 # li_vpn_net li_vpn_net_ip6s
994 10.8.0.0/24; 2600:3c00:e000:280::/64 ; 2600:3c00:e002:3800::/56 ; \\
995 # bk bk_ip6
996 85.119.83.50 ; 2001:ba8:1f1:f0c9::2 ; \\
997 # je je_ipv6
998 85.119.82.128 ; 2001:ba8:1f1:f09d::2 ; \\
999 # fsf_mit_net fsf_mit_net_ip6 fsf_net fsf_net_ip6 fsf_office_net
1000 18.4.89.0/24 ; 2603:3005:71a:2e00::/64 ; 209.51.188.0/24 ; 2001:470:142::/48 ; 74.94.156.208/28
1001 EOF
1002
1003 rm -fv /etc/exim4/rcpt_local_acl # old path
1004
1005 i /etc/exim4/conf.d/local_deny_exceptions_acl <<'EOF'
1006 # This acl already exists in rcpt, this just makes it more widespread.
1007 # See the comment there for its rationale. The reason it needs to be
1008 # more widespread is that I've turned on sender verification, but cron
1009 # emails can fail sender verification since I may be in a network that
1010 # doesn't have my local dns.
1011 accept
1012 authenticated = *
1013
1014 # i setup a local programs smtp to mail.iankelling.org, this
1015 # skips sender verification for it.
1016 accept
1017 hosts = 10.173.8.1
1018 EOF
1019
1020 rm -fv /etc/exim4/data_local_acl # old path
1021 i /etc/exim4/conf.d/data_local_acl <<'EOF'
1022 # Except for the "condition =", this was
1023 # a comment in the check_data acl. The comment about this not
1024 # being suitable has been changed in newer exim versions. The only thing
1025 # related I found was to
1026 # add the condition =, cuz spamassassin has problems with big
1027 # messages and spammers don't bother with big messages,
1028 # but I've increased the size from 10k
1029 # suggested in official docs, and 100k in the wiki example because
1030 # those docs are rather old and I see a 110k spam message
1031 # pretty quickly looking through my spam folder.
1032
1033 warn
1034 !hosts = +iank_trusted
1035 remove_header = X-Spam_score: X-Spam_score_int : X-Spam_bar : X-Spam_report
1036
1037 warn
1038 !hosts = +iank_trusted
1039 condition = ${if < {$message_size}{5000K}}
1040 spam = Debian-exim:true
1041 add_header = X-Spam_score_int: $spam_score_int
1042 add_header = X-Spam_score: $spam_score
1043 add_header = X-Spam_bar: $spam_bar
1044 add_header = X-Spam_report: $spam_report
1045 add_header = X-Spam_action: $spam_action
1046
1047 warn
1048 condition = ${if def:malware_name}
1049 remove_header = Subject:
1050 add_header = Subject: [Clamav warning: $malware_name] $h_subject
1051 log_message = heuristic malware warning: $malware_name
1052
1053 #accept
1054 # spf = pass:fail:softfail:none:neutral:permerror:temperror
1055 # dmarc_status = reject:quarantine
1056 # add_header = Reply-to: dmarctest@iankelling.org
1057
1058 EOF
1059
1060 i /etc/exim4/conf.d/router/900_exim4-config_local_user <<EOF
1061 ### router/900_exim4-config_local_user
1062 #################################
1063
1064 # This router matches local user mailboxes. If the router fails, the error
1065 # message is "Unknown user".
1066 local_user:
1067 debug_print = "R: local_user for \$local_part@\$domain"
1068 driver = accept
1069 domains = +local_domains
1070 # ian: default file except where mentioned.
1071 # ian: commented this. I get all local parts. for bk, an rcpt
1072 # check handles checking with dovecot, and the only router
1073 # after this is root.
1074 # local_parts = ! root
1075 transport = LOCAL_DELIVERY
1076 cannot_route_message = Unknown user
1077 # ian: added for + addressing.
1078 local_part_suffix = +*
1079 local_part_suffix_optional
1080 EOF
1081 i /etc/exim4/conf.d/transport/30_exim4-config_dovecot_lmtp <<'EOF'
1082 dovecot_lmtp:
1083 driver = lmtp
1084 socket = /var/run/dovecot/lmtp
1085 #maximum number of deliveries per batch, default 1
1086 batch_max = 200
1087 envelope_to_add
1088 EOF
1089
1090 i /etc/exim4/conf.d/transport/30_remote_smtp_vpn <<'EOF'
1091 # same as debians 30_exim4-config_remote_smtp, but
1092 # with interface added at the end.
1093
1094 remote_smtp_vpn:
1095 debug_print = "T: remote_smtp_vpn for $local_part@$domain"
1096 driver = smtp
1097 .ifndef IGNORE_SMTP_LINE_LENGTH_LIMIT
1098 message_size_limit = ${if > {$max_received_linelength}{998} {1}{0}}
1099 .endif
1100 .ifdef REMOTE_SMTP_HOSTS_AVOID_TLS
1101 hosts_avoid_tls = REMOTE_SMTP_HOSTS_AVOID_TLS
1102 .endif
1103 .ifdef REMOTE_SMTP_HEADERS_REWRITE
1104 headers_rewrite = REMOTE_SMTP_HEADERS_REWRITE
1105 .endif
1106 .ifdef REMOTE_SMTP_RETURN_PATH
1107 return_path = REMOTE_SMTP_RETURN_PATH
1108 .endif
1109 .ifdef REMOTE_SMTP_HELO_DATA
1110 helo_data=REMOTE_SMTP_HELO_DATA
1111 .endif
1112 .ifdef DKIM_DOMAIN
1113 dkim_domain = DKIM_DOMAIN
1114 .endif
1115 .ifdef DKIM_SELECTOR
1116 dkim_selector = DKIM_SELECTOR
1117 .endif
1118 .ifdef DKIM_PRIVATE_KEY
1119 dkim_private_key = DKIM_PRIVATE_KEY
1120 .endif
1121 .ifdef DKIM_CANON
1122 dkim_canon = DKIM_CANON
1123 .endif
1124 .ifdef DKIM_STRICT
1125 dkim_strict = DKIM_STRICT
1126 .endif
1127 .ifdef DKIM_SIGN_HEADERS
1128 dkim_sign_headers = DKIM_SIGN_HEADERS
1129 .endif
1130 .ifdef TLS_DH_MIN_BITS
1131 tls_dh_min_bits = TLS_DH_MIN_BITS
1132 .endif
1133 .ifdef REMOTE_SMTP_TLS_CERTIFICATE
1134 tls_certificate = REMOTE_SMTP_TLS_CERTIFICATE
1135 .endif
1136 .ifdef REMOTE_SMTP_PRIVATEKEY
1137 tls_privatekey = REMOTE_SMTP_PRIVATEKEY
1138 .endif
1139 .ifdef REMOTE_SMTP_HOSTS_REQUIRE_TLS
1140 hosts_require_tls = REMOTE_SMTP_HOSTS_REQUIRE_TLS
1141 .endif
1142 .ifdef REMOTE_SMTP_TRANSPORTS_HEADERS_REMOVE
1143 headers_remove = REMOTE_SMTP_TRANSPORTS_HEADERS_REMOVE
1144 .endif
1145 interface = <; 10.8.0.4 ; 2600:3c00:e002:3800::4
1146 EOF
1147
1148 i /etc/exim4/conf.d/transport/30_smarthost_dkim <<'EOF'
1149 # ian: this is remote_smtp_smarthost plus the dkim parts from remote_smtp
1150
1151 smarthost_dkim:
1152 debug_print = "T: remote_smtp_smarthost for $local_part@$domain"
1153 driver = smtp
1154 multi_domain
1155 .ifndef IGNORE_SMTP_LINE_LENGTH_LIMIT
1156 message_size_limit = ${if > {$max_received_linelength}{998} {1}{0}}
1157 .endif
1158 hosts_try_auth = <; ${if exists{CONFDIR/passwd.client} \
1159 {\
1160 ${lookup{$host}nwildlsearch{CONFDIR/passwd.client}{$host_address}}\
1161 }\
1162 {} \
1163 }
1164 .ifdef REMOTE_SMTP_SMARTHOST_HOSTS_AVOID_TLS
1165 hosts_avoid_tls = REMOTE_SMTP_SMARTHOST_HOSTS_AVOID_TLS
1166 .endif
1167 .ifdef REMOTE_SMTP_SMARTHOST_HOSTS_REQUIRE_TLS
1168 hosts_require_tls = REMOTE_SMTP_SMARTHOST_HOSTS_REQUIRE_TLS
1169 .endif
1170 .ifdef REMOTE_SMTP_SMARTHOST_TLS_VERIFY_CERTIFICATES
1171 tls_verify_certificates = REMOTE_SMTP_SMARTHOST_TLS_VERIFY_CERTIFICATES
1172 .endif
1173 .ifdef REMOTE_SMTP_SMARTHOST_TLS_VERIFY_HOSTS
1174 tls_verify_hosts = REMOTE_SMTP_SMARTHOST_TLS_VERIFY_HOST
1175 .endif
1176 .ifdef REMOTE_SMTP_HEADERS_REWRITE
1177 headers_rewrite = REMOTE_SMTP_HEADERS_REWRITE
1178 .endif
1179 .ifdef REMOTE_SMTP_RETURN_PATH
1180 return_path = REMOTE_SMTP_RETURN_PATH
1181 .endif
1182 .ifdef REMOTE_SMTP_HELO_DATA
1183 helo_data=REMOTE_SMTP_HELO_DATA
1184 .endif
1185 .ifdef TLS_DH_MIN_BITS
1186 tls_dh_min_bits = TLS_DH_MIN_BITS
1187 .endif
1188 .ifdef REMOTE_SMTP_SMARTHOST_TLS_CERTIFICATE
1189 tls_certificate = REMOTE_SMTP_SMARTHOST_TLS_CERTIFICATE
1190 .endif
1191 .ifdef REMOTE_SMTP_SMARTHOST_PRIVATEKEY
1192 tls_privatekey = REMOTE_SMTP_SMARTHOST_PRIVATEKEY
1193 .endif
1194 .ifdef REMOTE_SMTP_TRANSPORTS_HEADERS_REMOVE
1195 headers_remove = REMOTE_SMTP_TRANSPORTS_HEADERS_REMOVE
1196 .endif
1197 .ifdef DKIM_DOMAIN
1198 dkim_domain = DKIM_DOMAIN
1199 .endif
1200 .ifdef DKIM_SELECTOR
1201 dkim_selector = DKIM_SELECTOR
1202 .endif
1203 .ifdef DKIM_PRIVATE_KEY
1204 dkim_private_key = DKIM_PRIVATE_KEY
1205 .endif
1206 .ifdef DKIM_CANON
1207 dkim_canon = DKIM_CANON
1208 .endif
1209 .ifdef DKIM_STRICT
1210 dkim_strict = DKIM_STRICT
1211 .endif
1212 .ifdef DKIM_SIGN_HEADERS
1213 dkim_sign_headers = DKIM_SIGN_HEADERS
1214 .endif
1215 EOF
1216
1217
1218 cat >/etc/exim4/update-exim4.conf.conf <<'EOF'
1219 # default stuff, i havent checked if its needed
1220 dc_minimaldns='false'
1221 dc_relay_nets=''
1222 CFILEMODE='644'
1223 dc_use_split_config='true'
1224 dc_mailname_in_oh='true'
1225 EOF
1226
1227
1228 # * radicale
1229 if mailhost; then
1230 if ! mountpoint /o; then
1231 echo "error /o is not a mountpoint" >&2
1232 exit 1
1233 fi
1234
1235 # davx/davdroid setup instructions at the bottom
1236
1237 # main docs:
1238 # http://radicale.org/user_documentation/
1239 # https://davdroid.bitfire.at/configuration/
1240
1241 # note on debugging: if radicale can't bind to the address,
1242 # in the log it just says "Starting Radicale". If you run
1243 # it in the foreground, it will give more info. Background
1244 # plus debug does not help.
1245 # sudo -u radicale radicale -D -f
1246
1247 # created password file with:
1248 # htpasswd -c /p/c/machine_specific/li/filesystem/etc/caldav-htpasswd
1249 # chmod 640 /p/c/machine_specific/li/filesystem/etc/caldav-htpasswd
1250 # # setup chgrp www-data in ./conflink
1251
1252 pi-nostart radicale
1253
1254 i /etc/systemd/system/radicale.service.d/override.conf <<EOF
1255 [Unit]
1256 # this unit is configured to start and stop whenever
1257 # $vpnser does
1258
1259 After=network.target network-online.target mailnn.service $vpnser
1260 BindsTo=$vpnser
1261
1262 Wants=network-online.target
1263 JoinsNamespaceOf=mailnn.service
1264 StartLimitIntervalSec=0
1265
1266 [Service]
1267 PrivateNetwork=true
1268 BindPaths=$bindpaths
1269 Restart=always
1270 # time to sleep before restarting a service
1271 RestartSec=1000
1272
1273 [Install]
1274 # for openvpn
1275 RequiredBy=$vpnser
1276 EOF
1277
1278
1279 # use persistent uid/gid
1280 IFS=:; read -r _ _ uid _ < <(getent passwd radicale ); unset IFS
1281 IFS=:; read -r _ _ gid _ < <(getent group radicale ); unset IFS
1282 if [[ $uid != 609 ]]; then
1283 m systemctl stop radicale ||:
1284 m usermod -u 609 radicale
1285 m groupmod -g 609 radicale
1286 m usermod -g 609 radicale
1287 fi
1288 m find /o/radicale -xdev -exec chown -h 609 {} +
1289 m find /o/radicale -xdev -exec chgrp -h 609 {} +
1290
1291
1292 # I moved /var/lib/radicale after it's initialization.
1293 # I did a sudo -u radicale git init in the collections subfolder
1294 # after it gets created, per the git docs.
1295 m /a/exe/lnf -T /o/radicale /var/lib/radicale
1296
1297 # from https://www.williamjbowman.com/blog/2015/07/24/setting-up-webdav-caldav-and-carddav-servers/
1298
1299 # more config is for li in distro-end
1300
1301 # coment in this file says this is needed for it to run on startup
1302 sed -ri 's/^\s*#+\s*(ENABLE_RADICALE\s*=\s*yes\s*)/\1/' /etc/default/radicale
1303
1304 # comments say default is 0.0.0.0:5232
1305 m setini hosts 10.8.0.4:5232 server
1306 # https://radicale.org/2.1.html
1307 m setini type http_x_remote_user auth
1308
1309
1310 # disable power management feature, set to 240 min sync interval,
1311 # so it shouldn't be bad.
1312
1313 # davdroid from f-druid.
1314 # login with url and user name
1315 # url https://cal.iankelling.org/ian
1316 # username ian
1317 # pass, see password manager for radicale
1318 #
1319 # add account dialog:
1320 #
1321 # set account name as ian@iankelling.org, per help text below the
1322 # field.
1323 #
1324 # switch to groups are per-contact categories,
1325 # per https://davdroid.bitfire.at/configuration/radicale/
1326 #
1327 #
1328 # After setting up account, I added one address book, named
1329 # ianaddr. calender was already created, named ian. checked boxes under
1330 # both. synced.
1331 #
1332 # To restore from old phone to new phone, I wiped all data out, then copied over the newly created files. I think
1333 #
1334 # ignorable background info:
1335 #
1336 # opentasks uses the calendar file.
1337 #
1338 # The address book I created got a uuid as a name for the file. Note
1339 # the .props file says if it's a calendar or addressbook.
1340 #
1341 # When debugging, tailed /var/log/radicale/radicale.log and apache log,
1342 # both show the requests happening. Without creating the address book,
1343 # after creating a contact, a sync would delete it.
1344 #
1345 # Address books correspond to .props files in the radicale dir.
1346 #
1347 # Some background is here,
1348 # https://davdroid.bitfire.at/faq/entry/cant-manage-groups-on-device/
1349 # which shows separate vcard option is from rfc 6350, the other is 2426,
1350 # radicale page says it implements the former not the latter,
1351 # which conflicts with the documentation of which to select, but whatever.
1352 # http://radicale.org/technical_choices/
1353 # https://davdroid.bitfire.at/faq/entry/cant-manage-groups-on-device/
1354 #
1355 # Note, url above says only cayanogenmod 13+ and omnirom can manage groups.
1356
1357 # Note, radicale had built-in git support to track changes, but they
1358 # removed it in 2.0.
1359
1360 fi
1361
1362 # * dovecot
1363
1364 # ** $MAIL_HOST|bk|je)
1365 case $HOSTNAME in
1366 $MAIL_HOST|bk|je)
1367 # based on a little google and package search, just the dovecot
1368 # packages we need instead of dovecot-common.
1369 #
1370 # dovecot-lmtpd is for exim to deliver to dovecot instead of maildir
1371 # directly. The reason to do this is to use dovecot\'s sieve, which
1372 # can generally do more than exims filters (a few things less) and
1373 # sieve has the benefit of being supported in postfix and
1374 # proprietary/weird environments, so there is more examples on the
1375 # internet.
1376 pi dovecot-core dovecot-imapd dovecot-sieve dovecot-lmtpd dovecot-sqlite sqlite3
1377
1378 for f in /p/c{/machine_specific/$HOSTNAME,}/filesystem/etc/dovecot/users; do
1379 if [[ -e $f ]]; then
1380 m rsync -ahhi --chown=root:dovecot --chmod=0640 $f /etc/dovecot/
1381 break
1382 fi
1383 done
1384 for f in /p/c/subdir_files/sieve/*sieve /a/c/subdir_files/sieve/*sieve; do
1385 m sudo -u $u /a/exe/lnf -T $f $uhome/sieve/${f##*/}
1386 done
1387
1388 # https://wiki.dovecot.org/SSL/DovecotConfiguration
1389 i /etc/dovecot/dhparam <<'EOF'
1390 -----BEGIN DH PARAMETERS-----
1391 MIIBCAKCAQEAoleil6SBxGqQKk7j0y2vV3Oklv6XupZKn7PkPv485QuFeFagifeS
1392 A+Jz6Wquqk5zhGyCu63Hp4wzGs4TyQqoLjkaWL6Ra/Bw3g3ofPEzMGEsV1Qdqde4
1393 jorwiwtr2i9E6TXQp0noT/7VFeHulIkayTeW8JulINdMHs+oLylv16McGCIrxbkM
1394 8D1PuO0TP/CNDs2QbRvJ1RjY3CeGpxMhrSHVgBCUMwnA2cvz3bYjI7UMYMMDPNrE
1395 PLrwsYzXGGCdJsO2vsmmqqgLsZiapYJlUNjfiyWLt7E2H6WzkNB3VIhIPfLqFDPK
1396 xioE3sYKdjOt+p6mlg3l8+OLtODEFPHDqwIBAg==
1397 -----END DH PARAMETERS-----
1398 EOF
1399 {
1400 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
1401 cat <<'EOF'
1402 ssl_cert = </etc/exim4/fullchain.pem
1403 ssl_key = </etc/exim4/privkey.pem
1404 EOF
1405 else
1406 cat <<'EOF'
1407 ssl_cert = </etc/exim4/exim.crt
1408 ssl_key = </etc/exim4/exim.key
1409 EOF
1410 fi
1411 cat <<EOF
1412 # https://ssl-config.mozilla.org
1413 ssl = required
1414 # this is the same as the certbot list, in my cert cronjob, I check if that has changed upstream.
1415 ssl_cipher_list = ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
1416 ssl_protocols = TLSv1.2
1417 ssl_prefer_server_ciphers = no
1418 ssl_dh_parameters_length = 2048
1419
1420 protocol lmtp {
1421 #per https://wiki2.dovecot.org/Pigeonhole/Sieve/Configuration
1422 # default is just \$mail_plugins
1423 mail_plugins = \$mail_plugins sieve
1424 }
1425 EOF
1426 if dpkg --compare-versions $(dpkg-query -f='${Version}\n' --show dovecot-core) ge 1:2.3; then
1427 cat <<EOF
1428 ssl_dh = </etc/dovecot/dhparam
1429 EOF
1430 fi
1431 } >/etc/dovecot/local.conf
1432
1433 ;;&
1434
1435 # ** $MAIL_HOST)
1436 $MAIL_HOST)
1437 # If we changed 90-sieve.conf and removed the active part of the
1438 # sieve option, we wouldn\'t need this, but I\'d rather not modify a
1439 # default config if not needed. This won\'t work as a symlink in /a/c
1440 # unfortunately.
1441 m sudo -u $u /a/exe/lnf -T sieve/main.sieve $uhome/.dovecot.sieve
1442
1443 if [[ ! -e $uhome/sieve/personal.sieve ]]; then
1444 m touch $uhome/sieve/personal{,end}{,test}.sieve
1445 fi
1446
1447 rm -fv /etc/dovecot/conf.d/20-lmtp.conf # file from prev version
1448 cat >>/etc/dovecot/local.conf <<EOF
1449 # simple password file based login
1450 !include conf.d/auth-passwdfile.conf.ext
1451
1452 # ian: %u is used for alerts user vs iank
1453 mail_location = maildir:/m/%u:LAYOUT=fs:INBOX=/m/%u/INBOX
1454 mail_uid = $u
1455 mail_gid = $u
1456
1457 protocol lmtp {
1458 # For a normal setup with exim, we need something like this, which
1459 # removes the domain part
1460 # auth_username_format = %Ln
1461 #
1462 # or else # Exim says something like
1463 # "LMTP error after RCPT ... 550 ... User doesn't exist someuser@somedomain"
1464 # Dovecot verbose log says something like
1465 # "auth-worker(9048): passwd(someuser@somedomain): unknown user"
1466 # reference: http://wiki.dovecot.org/LMTP/Exim
1467 #
1468 # However, I use this to direct all mail to the same inbox.
1469 # A normal way to do this, which I did at first is to have
1470 # a router in exim almost at the end, eg 950,
1471 #local_catchall:
1472 # debug_print = "R: catchall for \$local_part@\$domain"
1473 # driver = redirect
1474 # domains = +local_domains
1475 # data = $u
1476 # based on
1477 # http://blog.alteholz.eu/2015/04/exim4-and-catchall-email-address/
1478 # with superflous options removed.
1479 # However, this causes the envelope to be rewritten,
1480 # which makes filtering into mailboxes a little less robust or more complicated,
1481 # so I've done it this way instead. it also requires
1482 # modifying the local router in exim.
1483 auth_username_format = $u
1484 }
1485 EOF
1486 ;;&
1487 # ** bk|je)
1488 bk|je)
1489 chown -R mail.mail /m/md
1490
1491 f=/etc/dovecot/conf.d/10-auth.conf
1492 if [[ -e $f ]]; then
1493 mv $f $f-iank-disabled
1494 fi
1495
1496 cat >>/etc/dovecot/local.conf <<EOF
1497 !include /etc/dovecot/local.conf.ext
1498
1499 # for debugging info, uncomment these.
1500 # logs go to syslog and to /var/log/mail.log
1501 #auth_verbose=yes
1502 #mail_debug=yes
1503
1504
1505 protocol lmtp {
1506 # This downcases the localpart. default is case sensitive.
1507 # case sensitive local part will miss out on valid email when some person or system
1508 # mistakenly capitalizes things.
1509 auth_username_format = %Lu
1510 }
1511
1512 # make 147 only listen on localhost, plan to use for nextcloud.
1513 # copied from mailinabox
1514 service imap-login {
1515 inet_listener imap {
1516 address = 127.0.0.1
1517 }
1518 }
1519 # https://www.exim.org/exim-html-current/doc/html/spec_html/ch-the_dovecot_authenticator.html
1520 service auth {
1521 unix_listener auth-client {
1522 user = Debian-exim
1523 group = Debian-exim
1524 }
1525 }
1526
1527
1528 plugin {
1529 sieve_before = /etc/dovecot/sieve-spam.sieve
1530 # from mailinabox
1531 sieve = /m/sieve/%d/%n.sieve
1532 sieve_dir = /m/sieve/%d/%n
1533 }
1534
1535
1536 # all taken from mailinabox.
1537 mail_location = maildir:/m/md/%d/%n
1538 # meh, ok.
1539 mail_privileged_group = mail
1540 # By default Dovecot allows users to log in only with UID numbers 500 and above. mail is 8
1541 first_valid_uid = 1
1542
1543 # todo: test these changes in the universal config
1544 # mailboxes taken from mailinabox but removed
1545 # settings duplicate to defaults
1546 namespace inbox {
1547 mailbox INBOX {
1548 auto = subscribe
1549 }
1550 mailbox Spam {
1551 special_use = \Junk
1552 auto = subscribe
1553 }
1554 mailbox Drafts {
1555 auto = subscribe
1556 }
1557 mailbox Sent {
1558 auto = subscribe
1559 }
1560 mailbox Trash {
1561 auto = subscribe
1562 }
1563 mailbox Archive {
1564 special_use = \Archive
1565 auto = subscribe
1566 }
1567 }
1568 auth_mechanisms = plain login
1569 EOF
1570
1571 i /etc/dovecot/sieve-spam.sieve <<'EOF'
1572 require ["regex", "fileinto", "imap4flags"];
1573
1574 if allof (header :regex "X-Spam-Status" "^Yes") {
1575 fileinto "Spam";
1576 stop;
1577 }
1578 EOF
1579
1580 i /etc/dovecot/local.conf.ext <<'EOF'
1581 passdb {
1582 driver = sql
1583 args = /etc/dovecot/dovecot-sql.conf.ext
1584 }
1585 userdb {
1586 driver = sql
1587 args = /etc/dovecot/dovecot-sql.conf.ext
1588 }
1589
1590 EOF
1591
1592 i /etc/dovecot/dovecot-sql.conf.ext <<'EOF'
1593 # from mailinabox
1594 driver = sqlite
1595 connect = /m/rc/users.sqlite
1596 default_pass_scheme = SHA512-CRYPT
1597 password_query = SELECT email as user, password FROM users WHERE email='%u';
1598 user_query = SELECT email AS user, "mail" as uid, "mail" as gid, "/m/md/%d/%n" as home FROM users WHERE email='%u';
1599 iterate_query = SELECT email AS user FROM users;
1600 EOF
1601 m chmod 0600 /etc/dovecot/dovecot-sql.conf.ext # per Dovecot instructions
1602
1603 # db needs to be in a www-data writable directory
1604 db=/m/rc/users.sqlite
1605 if [[ ! -s $db ]]; then
1606 m mkdir -p /m/rc
1607 m sqlite3 $db <<'EOF'
1608 CREATE TABLE users (
1609 id INTEGER PRIMARY KEY AUTOINCREMENT,
1610 email TEXT NOT NULL UNIQUE,
1611 password TEXT NOT NULL,
1612 extra,
1613 privileges TEXT NOT NULL DEFAULT '');
1614 EOF
1615 fi
1616 # example of adding a user:
1617 # hash: doveadm pw -s SHA512-CRYPT -p passhere
1618 # sqlite3 /m/rc/users.sqlite <<'EOF'
1619 #insert into users (email, password) values ('testignore@bk.b8.nz', 'hash');
1620 #EOF
1621 # update users set password = 'hash' where email = 'testignore@bk.b8.nz';
1622
1623 # this should be at the end since it requires a valid dovecot config
1624 m sievec /etc/dovecot/sieve-spam.sieve
1625 ;;&
1626 # ** bk)
1627 bk)
1628 # roundcube uses this
1629 mkdir -p /m/sieve
1630 chown mail.mail /m/sieve
1631 m pi dovecot-managesieved
1632 ;;
1633 esac
1634
1635 # * thunderbird autoconfig setup
1636
1637 bkdomains=(expertpathologyreview.com amnimal.ninja)
1638 if [[ $HOSTNAME == bk ]]; then
1639 for domain in ${bkdomains[@]}; do
1640 m /a/exe/web-conf apache2 autoconfig.$domain
1641 dir=/var/www/autoconfig.$domain/html/mail
1642 m mkdir -p $dir
1643 # taken from mailinabox
1644 i $dir/config-v1.1.xml <<EOF
1645 <?xml version="1.0" encoding="UTF-8"?>
1646 <clientConfig version="1.1">
1647 <emailProvider id="$domain">
1648 <domain>$domain</domain>
1649
1650 <displayName>$domain Mail</displayName>
1651 <displayShortName>$domain</displayShortName>
1652
1653 <incomingServer type="imap">
1654 <hostname>mail2.iankelling.org</hostname>
1655 <port>993</port>
1656 <socketType>SSL</socketType>
1657 <username>%EMAILADDRESS%</username>
1658 <authentication>password-cleartext</authentication>
1659 </incomingServer>
1660
1661 <outgoingServer type="smtp">
1662 <hostname>mail2.iankelling.org</hostname>
1663 <port>587</port>
1664 <socketType>STARTTLS</socketType>
1665 <username>%EMAILADDRESS%</username>
1666 <authentication>password-cleartext</authentication>
1667 <addThisServer>true</addThisServer>
1668 <useGlobalPreferredServer>false</useGlobalPreferredServer>
1669 </outgoingServer>
1670
1671 <documentation url="https://$domain/">
1672 <descr lang="en">$domain website.</descr>
1673 </documentation>
1674 </emailProvider>
1675
1676 <webMail>
1677 <loginPage url="https://$domain/roundcube" />
1678 <loginPageInfo url="https://$domain/roundcube" >
1679 <username>%EMAILADDRESS%</username>
1680 <usernameField id="rcmloginuser" name="_user" />
1681 <passwordField id="rcmloginpwd" name="_pass" />
1682 <loginButton id="rcmloginsubmit" />
1683 </loginPageInfo>
1684 </webMail>
1685 <clientConfigUpdate url="https://autoconfig.$domain/mail/config-v1.1.xml" />
1686 </clientConfig>
1687 EOF
1688 done
1689 fi
1690
1691 # * roundcube setup
1692
1693 if [[ $HOSTNAME == bk ]]; then
1694
1695 # zip according to /installer
1696 # which requires adding a line to /usr/local/lib/roundcubemail/config/config.inc.php
1697 # $config['enable_installer'] = true;
1698 pi roundcube roundcube-sqlite3 php-zip apache2 php-fpm
1699
1700 ### begin composer install
1701 # https://getcomposer.org/doc/faqs/how-to-install-composer-programmatically.md
1702 # cd $(mktemp -d)
1703 # sum="$(wget -q -O - https://composer.github.io/installer.sig)"
1704 # m php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
1705 # if [[ $sum != $(php -r "echo hash_file('sha384', 'composer-setup.php');") ]]; then
1706 # echo 'ERROR: Invalid composer installer checksum' >&2
1707 # rm -fv composer-setup.php
1708 # exit 1
1709 # fi
1710 # m php composer-setup.php --quiet
1711 # rm -fv composer-setup.php
1712 # m mv composer.phar /usr/local/bin
1713
1714 # the above method gets composer2, carddav plugin at least doesnt work with that
1715 # yet, it was just released 10-24-2020.
1716 m cd /usr/local/bin
1717 m wget -nv -N https://getcomposer.org/composer-1.phar
1718 chmod +x composer-1.phar
1719 ### end composer install
1720
1721 rcdirs=(/usr/local/lib/rcexpertpath /usr/local/lib/rcninja)
1722 ncdirs=(/var/www/ncexpertpath /var/www/ncninja)
1723 # point debian cronjob to our local install, preventing daily cron error
1724
1725 # debian's cronjob will fail, remove both paths it uses just to be sure
1726 rm -fv /usr/share/roundcube/bin/cleandb.sh /etc/cron.d/roundcube-core
1727
1728 #### begin dl roundcube
1729 # note, im r2e subbed to https://github.com/roundcube/roundcubemail/releases.atom
1730 v=1.4.11; f=roundcubemail-$v-complete.tar.gz
1731 cd /a/opt
1732 if [[ -e $f ]]; then
1733 timestamp=$(stat -c %Y $f)
1734 else
1735 timestamp=0
1736 fi
1737 m wget -nv -N https://github.com/roundcube/roundcubemail/releases/download/$v/$f
1738 new_timestamp=$(stat -c %Y $f)
1739 for rcdir in ${rcdirs[@]}; do
1740 if [[ $timestamp != "$new_timestamp" || ! -e "$rcdir/config/secret" ]]; then
1741 m tar -C /usr/local/lib --no-same-owner -zxf $f
1742 m rm -rf $rcdir
1743 m mv /usr/local/lib/roundcubemail-$v $rcdir
1744 fi
1745 done
1746 #### end dl roundcube
1747
1748 for ((i=0; i < ${#bkdomains[@]}; i++)); do
1749 domain=${bkdomains[i]}
1750 rcdir=${rcdirs[i]}
1751 rcbase=${rcdir##*/}
1752 ncdir=${ncdirs[i]}
1753
1754 # copied from debians cronjob
1755 i /etc/cron.d/$rcbase <<EOF
1756 # Roundcube database cleaning: finally removes all records that are
1757 # marked as deleted.
1758 0 5 * * * www-data $rcdir/bin/cleandb.sh >/dev/null
1759 EOF
1760
1761 m /a/exe/web-conf - apache2 $domain <<EOF
1762 Alias /roundcube $rcdir
1763 ### begin roundcube settings
1764 # taken from /etc/apache2/conf-available/roundcube.conf version 1.4.8+dfsg.1-1~bpo10+1
1765 <Directory $rcdir/>
1766 Options +FollowSymLinks
1767 # This is needed to parse $rcdir/.htaccess.
1768 AllowOverride All
1769 Require all granted
1770 </Directory>
1771 # Protecting basic directories:
1772 <Directory $rcdir/config>
1773 Options -FollowSymLinks
1774 AllowOverride None
1775 </Directory>
1776 ### end roundcube settings
1777
1778
1779 ### begin nextcloud settings
1780 Alias /nextcloud "$ncdir/"
1781 <Directory $ncdir/>
1782 Require all granted
1783 AllowOverride All
1784 Options FollowSymLinks MultiViews
1785
1786 <IfModule mod_dav.c>
1787 Dav off
1788 </IfModule>
1789
1790 </Directory>
1791
1792 # based on install checker, links to
1793 # https://docs.nextcloud.com/server/19/admin_manual/issues/general_troubleshooting.html#service-discovery
1794 # their example was a bit wrong, I figured it out by adding
1795 # LogLevel warn rewrite:trace5
1796 # then watching the apache logs
1797
1798 RewriteEngine on
1799 RewriteRule ^/\.well-known/host-meta /nextcloud/public.php?service=host-meta [QSA,L]
1800 RewriteRule ^/\.well-known/host-meta\.json /nextcloud/public.php?service=host-meta-json [QSA,L]
1801 RewriteRule ^/\.well-known/webfinger /nextcloud/public.php?service=webfinger [QSA,L]
1802 RewriteRule ^/\.well-known/carddav /nextcloud/remote.php/dav/ [R=301,L]
1803 RewriteRule ^/\.well-known/caldav /nextcloud/remote.php/dav/ [R=301,L]
1804 ### end nextcloud settings
1805 EOF
1806 if [[ ! -e $rcdir/config/secret ]]; then
1807 base64 </dev/urandom | head -c24 >$rcdir/config/secret || [[ $? == 141 || ${PIPESTATUS[0]} == 32 ]]
1808 fi
1809 secret=$(cat $rcdir/config/secret)
1810
1811 rclogdir=/var/log/$rcbase
1812 rctmpdir=/var/tmp/$rcbase
1813 rcdb=/m/rc/$rcbase.sqlite
1814 # config from mailinabox
1815 i $rcdir/config/config.inc.php <<EOF
1816 <?php
1817 \$config = array();
1818 # debian creates this for us
1819 \$config['log_dir'] = '$rclogdir/';
1820 # debian also creates a temp dir, but it is under its install dir,
1821 # seems better to have our own.
1822 \$config['temp_dir'] = '$rctmpdir/';
1823 \$config['db_dsnw'] = 'sqlite:///$rcdb?mode=0640';
1824 \$config['default_host'] = 'ssl://localhost';
1825 \$config['default_port'] = 993;
1826 \$config['imap_conn_options'] = array(
1827 'ssl' => array(
1828 'verify_peer' => false,
1829 'verify_peer_name' => false,
1830 ),
1831 );
1832 \$config['imap_timeout'] = 15;
1833 \$config['smtp_server'] = 'tls://127.0.0.1';
1834 \$config['smtp_conn_options'] = array(
1835 'ssl' => array(
1836 'verify_peer' => false,
1837 'verify_peer_name' => false,
1838 ),
1839 );
1840 \$config['product_name'] = 'webmail';
1841 \$config['des_key'] = '$secret';
1842 \$config['plugins'] = array('archive', 'zipdownload', 'password', 'managesieve', 'jqueryui', 'carddav', 'html5_notifier');
1843 \$config['skin'] = 'elastic';
1844 \$config['login_autocomplete'] = 2;
1845 \$config['password_charset'] = 'UTF-8';
1846 \$config['junk_mbox'] = 'Spam';
1847 # disable builtin addressbook
1848 \$config['address_book_type'] = '';
1849 ?>
1850 EOF
1851
1852 m mkdir -p $rclogdir
1853 m chmod 750 $rclogdir
1854 m chown www-data:adm $rclogdir
1855 # note: subscribed to updates:
1856 # r2e add rcmcarddav https://github.com/blind-coder/rcmcarddav/commits/master.atom ian@iankelling.org
1857 # r2e add roundcube https://github.com/roundcube/roundcubemail/releases.atom ian@iankelling.org
1858 m mkdir -p $rctmpdir /m/rc
1859 m chown -R www-data.www-data $rctmpdir /m/rc
1860 m chmod 750 $rctmpdir
1861 # Ensure the log file monitored by fail2ban exists, or else fail2ban can't start.
1862 # todo: setup fail2ban
1863 # todo: check for other mailinabox things
1864 m sudo -u www-data touch $rclogdir/errors.log
1865
1866 #### begin carddav install
1867 # This is the official roundcube carddav repo.
1868 # Install doc suggests downloading with composer, but that
1869 # didnt work, it said some ldap package for roundcube was missing,
1870 # but I dont want to download some extra ldap thing.
1871 # https://github.com/blind-coder/rcmcarddav/blob/master/doc/INSTALL.md
1872 verf=$rcdir/plugins/carddav/myversion
1873 upgrade=false
1874 install=false
1875 v=4.0.0
1876 if [[ -e $verf ]]; then
1877 if [[ $(cat $verf) != "$v" ]]; then
1878 install=true
1879 upgrade=true
1880 fi
1881 else
1882 install=true
1883 fi
1884 if $install; then
1885 m rm -rf $rcdir/plugins/carddav
1886 tmpd=$(mktemp -d)
1887 m wget -nv -O $tmpd/t.tgz https://github.com/blind-coder/rcmcarddav/releases/download/v$v/carddav-v$v.tgz
1888 cd $rcdir/plugins
1889 tar xzf $tmpd/t.tgz
1890 rm -rf $tmpd
1891 m chown -R www-data:www-data $rcdir/plugins/carddav
1892 m cd $rcdir/plugins/carddav
1893 if $upgrade; then
1894 m sudo -u www-data composer-1.phar update --no-dev
1895 else
1896 m sudo -u www-data composer-1.phar install --no-dev
1897 fi
1898 m chown -R root:root $rcdir/plugins/carddav
1899 echo $v >$verf
1900 fi
1901
1902 # So, strangely, this worked in initial testing, but then
1903 # on first run it wouldn't show the existing contacts until
1904 # I went into the carddav settings and did "force immediate sync",
1905 # which seemed to fix things. Note, some of these settings
1906 # get initalized per/addressbook in the db, then need changing
1907 # there or through the settings menu.
1908
1909 # About categories, see https://www.davx5.com/tested-with/nextcloud
1910 # https://github.com/blind-coder/rcmcarddav/blob/master/doc/GROUPS.md
1911 i $rcdir/plugins/carddav/config.inc.php <<EOF;
1912 <?php
1913 \$prefs['_GLOBAL']['hide_preferences'] = false;
1914 \$prefs['davserver'] = array(
1915 # name in the UI is kind of dumb. This is just something short that seems to fit ok.
1916 'name' => 'Main',
1917 'username' => '%u', // login username
1918 'password' => '%p', // login password
1919 'url' => 'https://$domain/nextcloud/remote.php/dav/addressbooks/users/%u/contacts',
1920 'active' => true,
1921 'readonly' => false,
1922 'refresh_time' => '00:10:00',
1923 'fixed' => array('username','password'),
1924 'use_categories' => false,
1925 'hide' => false,
1926 );
1927 ?>
1928 EOF
1929 #### end carddav install
1930
1931 cd $rcdir/plugins
1932 if [[ ! -d html5_notifier ]]; then
1933 m git clone https://github.com/stremlau/html5_notifier
1934 fi
1935 cd $rcdir/plugins/html5_notifier
1936 m git pull --rebase
1937
1938 # todo: try out roundcube plugins: thunderbird labels
1939
1940 # Password changing plugin settings
1941 cat $rcdir/plugins/password/config.inc.php.dist - >$rcdir/plugins/password/config.inc.php <<'EOF'
1942 # following are from mailinabox
1943 $config['password_minimum_length'] = 8;
1944 $config['password_db_dsn'] = 'sqlite:////m/rc/users.sqlite';
1945 $config['password_query'] = 'UPDATE users SET password=%D WHERE email=%u';
1946 $config['password_dovecotpw'] = '/usr/bin/doveadm pw';
1947 $config['password_dovecotpw_method'] = 'SHA512-CRYPT';
1948 $config['password_dovecotpw_with_method'] = true;
1949 EOF
1950 # so PHP can use doveadm, for the password changing plugin
1951 m usermod -a -G dovecot www-data
1952 m usermod -a -G mail $u
1953
1954 # so php can update passwords
1955 m chown www-data:dovecot /m/rc/users.sqlite
1956 m chmod 664 /m/rc/users.sqlite
1957
1958 # Run Roundcube database migration script (database is created if it does not exist)
1959 m $rcdir/bin/updatedb.sh --dir $rcdir/SQL --package roundcube
1960 m chown www-data:www-data $rcdb
1961 m chmod 664 $rcdb
1962 done # end loop over domains and rcdirs
1963
1964 ### begin php setup for rc ###
1965 # Enable PHP modules.
1966 m phpenmod -v php mcrypt imap
1967 # dpkg says this is required
1968 m a2enmod proxy_fcgi setenvif
1969 fpm=$(dpkg-query -s php-fpm | sed -nr 's/^Depends:.* (php[^ ]*-fpm)( .*|$)/\1/p') # eg: php7.3-fpm
1970 phpver=$(dpkg-query -s php-fpm | sed -nr 's/^Depends:.* php([^ ]*)-fpm( .*|$)/\1/p')
1971 m a2enconf $fpm
1972 # 3 useless guides on php fpm fcgi debian 10 later, i figure out from reading
1973 # /etc/apache2/conf-enabled/php7.3-fpm.conf
1974 m a2dismod php$phpver
1975 # according to /install, we should set date.timezone,
1976 # but that is dumb, the system already has the right zone in
1977 # $rclogdir/errors.log
1978 # todo: consider other settings in
1979 # /a/opt/mailinabox/setup/nextcloud.sh
1980 i /etc/php/$phpver/cli/conf.d/30-local.ini <<'EOF'
1981 apc.enable_cli = 1
1982 EOF
1983
1984 i /etc/php/$phpver/fpm/conf.d/30-local.ini <<'EOF'
1985 date.timezone = "America/New_York"
1986 # for nextcloud
1987 upload_max_filesize = 2000M
1988 post_max_size = 2000M
1989 # install checker, nextcloud/settings/admin/overview
1990 memory_limit = 512M
1991 EOF
1992 m systemctl restart $fpm
1993 # dunno if reload/restart is needed
1994 m systemctl reload apache2
1995 # note bk backups are defined in crontab outside this file
1996 ### end php setup for rc ###
1997
1998 fi # end roundcube setup
1999
2000 # * nextcloud setup
2001
2002 if [[ $HOSTNAME == bk ]]; then
2003 # from install checker, nextcloud/settings/admin/overview and
2004 # https://docs.nextcloud.com/server/19/admin_manual/installation/source_installation.html
2005 # curl from the web installer requirement, but i switched to cli
2006 # it recommends php-file info, but that is part of php7.3-common, already got installed
2007 # with roundcube.
2008 m pi php-curl php-bz2 php-gmp php-bcmath php-imagick php-apcu
2009
2010 # https://docs.nextcloud.com/server/19/admin_manual/installation/source_installation.html
2011 cat >/etc/php/$phpver/fpm/pool.d/localwww.conf <<'EOF'
2012 [www]
2013 clear_env = no
2014 EOF
2015
2016 for ((i=0; i < ${#bkdomains[@]}; i++)); do
2017 domain=${bkdomains[i]}
2018 ncdir=${ncdirs[i]}
2019 ncbase=${ncdir##*/}
2020 m cd /var/www
2021 if [[ ! -e $ncdir/index.php ]]; then
2022 # if we wanted to only install a specific version, use something like
2023 # file=latest-22.zip
2024 file=latest.zip
2025 m wget -nv -N https://download.nextcloud.com/server/releases/$file
2026 m rm -rf nextcloud
2027 m unzip -q $file
2028 m rm -f $file
2029 m chown -R www-data.www-data nextcloud
2030 m mv nextcloud $ncdir
2031 m cd $ncdir
2032 m sudo -u www-data php occ maintenance:install --database sqlite --admin-user iank --admin-pass $nextcloud_admin_pass
2033 fi
2034 # note, strange this happend where updater did not increment the version var,
2035 # mine was stuck on 20. I manually updated it.
2036 m cd $ncdir/config
2037 if [[ ! -e config.php-orig ]]; then
2038 m cp -a config.php config.php-orig
2039 fi
2040 cat config.php-orig - >tmp.php <<EOF
2041 # https://docs.nextcloud.com/server/19/admin_manual/configuration_server/email_configuration.html
2042 \$CONFIG["mail_smtpmode"] = "sendmail";
2043 \$CONFIG["mail_smtphost"] = "127.0.0.1";
2044 \$CONFIG["mail_smtpport"] = 25;
2045 \$CONFIG["mail_smtptimeout"] = 10;
2046 \$CONFIG["mail_smtpsecure"] = "";
2047 \$CONFIG["mail_smtpauth"] = false;
2048 \$CONFIG["mail_smtpauthtype"] = "LOGIN";
2049 \$CONFIG["mail_smtpname"] = "";
2050 \$CONFIG["mail_smtppassword"] = "";
2051 \$CONFIG["mail_domain"] = "$domain";
2052
2053 # https://github.com/nextcloud/user_external#readme
2054 # plus mailinabox example
2055 #\$CONFIG['user_backends'] = array(array('class' => 'OC_User_IMAP','arguments' => array('127.0.0.1', 143, null),),);
2056
2057
2058 # based on installer check
2059 # https://docs.nextcloud.com/server/19/admin_manual/configuration_server/caching_configuration.html
2060 \$CONFIG['memcache.local'] = '\OC\Memcache\APCu';
2061
2062 \$CONFIG['overwrite.cli.url'] = 'https://$domain/nextcloud';
2063 \$CONFIG['htaccess.RewriteBase'] = '/nextcloud';
2064 \$CONFIG['trusted_domains'] = array (
2065 0 => '$domain',
2066 );
2067 #\$CONFIG[''] = '';
2068 fwrite(STDOUT, "<?php\n\\\$CONFIG = ");
2069 var_export(\$CONFIG);
2070 fwrite(STDOUT, ";\n");
2071 EOF
2072 m php tmp.php >config.php
2073 m rm tmp.php
2074 m sudo -u www-data php $ncdir/occ maintenance:update:htaccess
2075 list=$(sudo -u www-data php $ncdir/occ --output=json_pretty app:list)
2076 # user_external not compaible with nc 23
2077 for app in contacts calendar; do
2078 if [[ $(printf "%s\n" "$list"| jq ".enabled.$app") == null ]]; then
2079 m sudo -u www-data php $ncdir/occ app:install $app
2080 fi
2081 done
2082 i /etc/systemd/system/$ncbase.service <<EOF
2083 [Unit]
2084 Description=ncup $ncbase
2085 After=multi-user.target
2086
2087 [Service]
2088 Type=oneshot
2089 ExecStart=/usr/local/bin/ncup $ncbase
2090 User=www-data
2091 IOSchedulingClass=idle
2092 CPUSchedulingPolicy=idle
2093 EOF
2094 i /etc/systemd/system/$ncbase.timer <<EOF
2095 [Unit]
2096 Description=ncup $ncbase timer
2097
2098 [Timer]
2099 OnCalendar=Daily
2100
2101 [Install]
2102 WantedBy=timers.target
2103 EOF
2104 systemctl enable --now $ncbase.timer
2105 i /usr/local/bin/ncup <<'EOFOUTER'
2106 #!/bin/bash
2107 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
2108 shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
2109 set -eE -o pipefail
2110 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
2111
2112 ncbase=$1
2113 if ! php /var/www/$ncbase/updater/updater.phar -n; then
2114 echo failed nextcloud update for $ncbase >&2
2115 /sbin/exim -t <<EOF
2116 To: alerts@iankelling.org
2117 From: root@$(hostname -f)
2118 Subject: failed nextcloud update for $ncbase
2119
2120 For logs, run: jr -u $ncbase
2121 EOF
2122 fi
2123 EOFOUTER
2124
2125 mkdir -p /var/www/cron-errors
2126 chown www-data.www-data /var/www/cron-errors
2127 i /etc/cron.d/$ncbase <<EOF
2128 PATH=/sbin:/usr/sbin:/usr/bin:/bin:/usr/local/bin
2129 SHELL=/bin/bash
2130 # https://docs.nextcloud.com/server/20/admin_manual/configuration_server/background_jobs_configuration.html
2131 */5 * * * * www-data php -f $ncdir/cron.php --define apc.enable_cli=1 |& log-once nccron
2132 EOF
2133
2134 done
2135 fi
2136
2137
2138 # * exim host conditional config
2139
2140 # ** exim certs
2141
2142 all_dirs=(/p/c/filesystem)
2143 for x in /p/c/machine_specific/*.hosts /a/bin/ds/machine_specific/*.hosts; do
2144 if grep -qxF $HOSTNAME $x; then all_dirs+=( ${x%.hosts} ); fi
2145 done
2146 files=()
2147 for d in ${all_dirs[@]}; do
2148 f=$d/etc/exim4/passwd
2149 if [[ -e $f ]]; then
2150 files+=($f)
2151 fi
2152 tmp=($d/etc/exim4/*.pem)
2153 if (( ${#tmp[@]} )); then
2154 files+=(${tmp[@]})
2155 fi
2156 done
2157
2158 if (( ${#files[@]} )); then
2159 sudo rsync -ahhi --chown=root:Debian-exim --chmod=0640 ${files[@]} /etc/exim4/
2160 fi
2161
2162
2163 # ** auth
2164
2165 case $HOSTNAME in
2166 bk|je)
2167 # avoid accepting mail for invalid users
2168 # https://wiki.dovecot.org/LMTP/Exim
2169 cat >>/etc/exim4/conf.d/rcpt_local_acl <<'EOF'
2170 deny
2171 message = invalid recipient
2172 domains = +local_domains
2173 !verify = recipient/callout=no_cache
2174 EOF
2175 i /etc/exim4/conf.d/auth/29_exim4-config_auth <<'EOF'
2176 dovecot_plain:
2177 driver = dovecot
2178 public_name = PLAIN
2179 server_socket = /var/run/dovecot/auth-client
2180 server_set_id = $auth1
2181 EOF
2182 ;;
2183 esac
2184 if $bhost_t; then
2185 i /etc/exim4/conf.d/auth/29_exim4-config_auth <<'EOF'
2186 # from 30_exim4-config_examples
2187 plain_server:
2188 driver = plaintext
2189 public_name = PLAIN
2190 server_condition = "${if crypteq{$auth3}{${extract{1}{:}{${lookup{$auth2}lsearch{CONFDIR/passwd}{$value}{*:*}}}}}{1}{0}}"
2191 server_set_id = $auth2
2192 server_prompts = :
2193 .ifndef AUTH_SERVER_ALLOW_NOTLS_PASSWORDS
2194 server_advertise_condition = ${if eq{$tls_in_cipher}{}{}{*}}
2195 .endif
2196 EOF
2197 fi
2198
2199 # ** main daemon use non-default config file
2200 case $HOSTNAME in
2201 bk|$MAIL_HOST)
2202 # to see the default comments in /etc/default/exim4:
2203 # s update-exim4defaults --force --init
2204 # which will overwrite any existing file
2205 i /etc/default/exim4 <<'EOF'
2206 QUEUERUNNER='combined'
2207 QUEUEINTERVAL='30m'
2208 COMMONOPTIONS='-C /etc/exim4/my.conf'
2209 UPEX4OPTS='-o /etc/exim4/my.conf'
2210 #E4BCD_PANICLOG_NOISE='malware acl condition: clamd /var/run/clamav/clamd\.ctl : unable to connect to UNIX socket'
2211 EOF
2212 i /etc/exim4/trusted_configs <<'EOF'
2213 /etc/exim4/my.conf
2214 EOF
2215 ;;
2216 *)
2217 # default file
2218 i /etc/default/exim4 <<'EOF'
2219 QUEUERUNNER='combined'
2220 QUEUEINTERVAL='30m'
2221 EOF
2222 ;;
2223 esac
2224
2225 case $HOSTNAME in
2226 # ** $MAIL_HOST|bk|je)
2227 $MAIL_HOST|bk|je)
2228
2229 echo|i /etc/exim4/conf.d/router/870_backup_local
2230
2231 cat >>/etc/exim4/update-exim4.conf.conf <<EOF
2232 # note: some things we don't set that are here by default because they are unused.
2233 dc_local_interfaces=''
2234 dc_eximconfig_configtype='internet'
2235 dc_localdelivery='dovecot_lmtp'
2236 EOF
2237 cat >>/etc/exim4/conf.d/main/000_local <<EOF
2238 # recommended if dns is expected to work
2239 CHECK_RCPT_VERIFY_SENDER = true
2240 # default config comment says: If you enable this, you might reject legitimate mail,
2241 # but eggs has had this a long time, so that seems unlikely.
2242 CHECK_DATA_VERIFY_HEADER_SYNTAX = true
2243 CHECK_RCPT_SPF = true
2244 CHECK_RCPT_REVERSE_DNS = true
2245 CHECK_MAIL_HELO_ISSUED = true
2246
2247 # enable 587 in addition to the default 25, so that
2248 # i can send mail where port 25 is firewalled by isp
2249 daemon_smtp_ports = 25 : 587
2250 # default of 25, can get stuck when catching up on mail
2251 smtp_accept_max = 400
2252 smtp_accept_reserve = 100
2253 smtp_reserve_hosts = +iank_trusted
2254
2255 # options exim has to avoid having to alter the default config files
2256 CHECK_RCPT_LOCAL_ACL_FILE = /etc/exim4/conf.d/rcpt_local_acl
2257 CHECK_DATA_LOCAL_ACL_FILE = /etc/exim4/conf.d/data_local_acl
2258 LOCAL_DENY_EXCEPTIONS_LOCAL_ACL_FILE = /etc/exim4/conf.d/local_deny_exceptions_acl
2259 # testing dmarc
2260 #dmarc_tld_file = /etc/public_suffix_list.dat
2261 EOF
2262 ;;&
2263
2264 # ** $MAIL_HOST|bk)
2265 $MAIL_HOST|bk)
2266
2267 cat >>/etc/exim4/conf.d/main/000_local <<EOF
2268 # je.b8.nz will run out of memory with freshclam
2269 av_scanner = clamd:/var/run/clamav/clamd.ctl
2270 EOF
2271
2272 cat >> /etc/exim4/conf.d/data_local_acl <<'EOF'
2273 deny
2274 malware = */defer_ok
2275 !condition = ${if match {$malware_name}{\N^Heuristic\N}}
2276 message = This message was detected as possible malware ($malware_name).
2277 EOF
2278
2279 cat >/etc/exim4/conf.d/main/000_local-nn <<EOF
2280 # MAIN_HARDCODE_PRIMARY_HOSTNAME might mess up the
2281 # smarthost config type, not sure.
2282 # failing message on mail-tester.com:
2283 # We check if there is a server (A Record) behind your hostname kd.
2284 # You may want to publish a DNS record (A type) for the hostname kd or use a different hostname in your mail software
2285 # https://serverfault.com/questions/46545/how-do-i-change-exim4s-primary-hostname-on-a-debian-box
2286 # and this one seemed appropriate from grepping config.
2287 # I originally set this to li.iankelling.org, but then ended up with errors when li tried to send
2288 # mail to kd, so this should basically be a name that no host has as their
2289 # canonical hostname since the actual host sits behind a nat and changes.
2290 MAIN_HARDCODE_PRIMARY_HOSTNAME = mail.iankelling.org
2291 # I used this to avoid sender verification, didnt work but it still
2292 # makes sense based on the spec.
2293 hosts_treat_as_local = defaultnn.b8.nz
2294
2295 # Outside nn, we get the default cert location from a debian macro,
2296 # and the cert file is put in place by a certbot hook.
2297 MAIN_TLS_CERTIFICATE = /etc/exim4/fullchain.pem
2298 MAIN_TLS_PRIVATEKEY = /etc/exim4/privkey.pem
2299 EOF
2300
2301 i /etc/exim4/conf.d/router/190_exim4-config_fsfsmarthost <<'EOF'
2302 gnusmarthost:
2303 debug_print = "R: smarthost for $local_part@$domain"
2304 driver = manualroute
2305 domains = ! +local_domains
2306 # send most mail through eggs, helps fsfs sender reputation.
2307 # uncomment and optionally move to 188 file to send through my own servers again
2308 senders = *@gnu.org
2309 transport = smarthost_dkim
2310 route_list = * fencepost.gnu.org::587 byname
2311 host_find_failed = ignore
2312 same_domain_copy_routing = yes
2313 no_more
2314 EOF
2315
2316 /a/exe/cedit defaultnn /etc/hosts <<'EOF' || [[ $? == 1 ]]
2317 10.173.8.1 defaultnn.b8.nz
2318 EOF
2319 ;;&
2320 # ** $MAIL_HOST)
2321 $MAIL_HOST)
2322
2323 i /etc/exim4/conf.d/router/195_dnslookup_vpn <<'EOF'
2324 # copied from /etc/exim4/conf.d/router/200_exim4-config_primary, but
2325 # use vpn transport. lower priority so it overrides the default route.
2326 # Use this in case our vpn fails, we dont send anything without it.
2327 .ifdef DCconfig_internet
2328 dnslookup_vpn:
2329 debug_print = "R: dnslookup for $local_part@$domain"
2330 driver = dnslookup
2331 domains = ! +local_domains
2332 transport = remote_smtp_vpn
2333 same_domain_copy_routing = yes
2334 ignore_target_hosts = <; 0.0.0.0 ; 127.0.0.0/8 ; 192.168.0.0/16 ; 172.16.0.0/12 ; 10.0.0.0/8 ; 169.254.0.0/16 ; 255.255.255.255 ; ::/128 ; ::1/128 ; fc00::/7 ; fe80::/10 ; 100::/64
2335 no_more
2336 .endif
2337 EOF
2338
2339
2340 # note on backups: I used to do an automatic sshfs and restricted
2341 # permissions to a specific directory on the remote server, /bu/mnt,
2342 # which required using a dedicated user, but realized smtp will be
2343 # more reliable and less fuss. If I ever need that again, see the
2344 # history of this file, and bum in brc2.
2345
2346 i /etc/exim4/conf.d/router/890_backup_copy <<EOF
2347 ### router/900_exim4-config_local_user
2348 #################################
2349
2350 # todo, it would be nice to save sent email too,
2351 # but its not so important, they still exist in my head
2352
2353 backup_redir:
2354 driver = redirect
2355 domains = +local_domains
2356 # b is just an arbirary short string
2357 data = b@eximbackup.b8.nz
2358 # note, to test this, i could temporarily allow testignore.
2359 # alerts avoids potential mail loop. root is already
2360 # redirected earlier, so that is just being overly cautious.
2361 local_parts = ! root : ! testignore : ! alerts
2362 unseen = true
2363
2364 backup_copy:
2365 driver = manualroute
2366 domains = eximbackup.b8.nz
2367 transport = backup_remote
2368 ignore_target_hosts = ${HOSTNAME}wg.b8.nz
2369 # note changes here also require change in passwd.client
2370 route_list = * eximbackup.b8.nz
2371 same_domain_copy_routing = yes
2372 no_more
2373 EOF
2374
2375
2376 i /etc/exim4/conf.d/transport/30_backup_remote <<'EOF'
2377 backup_remote:
2378 driver = smtp
2379 multi_domain
2380 .ifndef IGNORE_SMTP_LINE_LENGTH_LIMIT
2381 message_size_limit = ${if > {$max_received_linelength}{998} {1}{0}}
2382 .endif
2383 hosts_require_auth = *
2384 hosts_try_auth = *
2385 return_path = alerts@iankelling.org
2386 envelope_to_add
2387 # manual return path because we dont want it to be the envelope sender
2388 # we got not the one we are using in this smtp transport
2389 headers_add = "Return-path: $sender_address"
2390 .ifdef REMOTE_SMTP_SMARTHOST_HOSTS_AVOID_TLS
2391 hosts_avoid_tls = REMOTE_SMTP_SMARTHOST_HOSTS_AVOID_TLS
2392 .endif
2393 .ifdef REMOTE_SMTP_SMARTHOST_HOSTS_REQUIRE_TLS
2394 hosts_require_tls = REMOTE_SMTP_SMARTHOST_HOSTS_REQUIRE_TLS
2395 .endif
2396 .ifdef REMOTE_SMTP_SMARTHOST_TLS_VERIFY_CERTIFICATES
2397 tls_verify_certificates = REMOTE_SMTP_SMARTHOST_TLS_VERIFY_CERTIFICATES
2398 .endif
2399 .ifdef REMOTE_SMTP_SMARTHOST_TLS_VERIFY_HOSTS
2400 tls_verify_hosts = REMOTE_SMTP_SMARTHOST_TLS_VERIFY_HOST
2401 .endif
2402 .ifdef REMOTE_SMTP_HEADERS_REWRITE
2403 headers_rewrite = REMOTE_SMTP_HEADERS_REWRITE
2404 .endif
2405 .ifdef REMOTE_SMTP_HELO_DATA
2406 helo_data=REMOTE_SMTP_HELO_DATA
2407 .endif
2408 .ifdef TLS_DH_MIN_BITS
2409 tls_dh_min_bits = TLS_DH_MIN_BITS
2410 .endif
2411 .ifdef REMOTE_SMTP_SMARTHOST_TLS_CERTIFICATE
2412 tls_certificate = REMOTE_SMTP_SMARTHOST_TLS_CERTIFICATE
2413 .endif
2414 .ifdef REMOTE_SMTP_SMARTHOST_PRIVATEKEY
2415 tls_privatekey = REMOTE_SMTP_SMARTHOST_PRIVATEKEY
2416 .endif
2417 .ifdef REMOTE_SMTP_TRANSPORTS_HEADERS_REMOVE
2418 headers_remove = REMOTE_SMTP_TRANSPORTS_HEADERS_REMOVE
2419 .endif
2420 EOF
2421
2422
2423 # this avoids some error. i cant remember what. todo:
2424 # test it out and document why/if its needed.
2425 i /etc/exim4/host_local_deny_exceptions <<'EOF'
2426 mail.fsf.org
2427 *.posteo.de
2428 EOF
2429
2430 # cron email from smarthost hosts will automatically be to
2431 # USER@FQDN. I redirect that to alerts@, on the smarthosts, but in
2432 # case that doesn't work, we still want to accept that mail, but not
2433 # from any host except the smarthosts. local_hostnames and this rule
2434 # is for that purpose.
2435 i /etc/exim4/conf.d/rcpt_local_acl <<'EOF'
2436 deny
2437 !authenticated = *
2438 domains = +local_hostnames
2439 message = no relay
2440 EOF
2441 echo|i /etc/exim4/conf.d/router/880_universal_forward
2442
2443 # for iank@fsf.org, i have mail.fsf.org forward it to fsf@iankelling.org.
2444 # and also have mail.iankelling.org whitelisted as a relay domain.
2445 # I could avoid that if I changed this to submit to 587 with a
2446 # password like a standard mua.
2447 i /etc/exim4/conf.d/router/188_exim4-config_smarthost <<'EOF'
2448 # ian: copied from /etc/exim4/conf.d/router/200_exim4-config_primary, and added senders = and
2449 # replaced DCsmarthost with hostname
2450 fsfsmarthost:
2451 debug_print = "R: smarthost for $local_part@$domain"
2452 driver = manualroute
2453 domains = ! +local_domains
2454 senders = *@fsf.org
2455 transport = remote_smtp_smarthost
2456 route_list = * mail.fsf.org::587 byname
2457 host_find_failed = ignore
2458 same_domain_copy_routing = yes
2459 no_more
2460
2461 posteosmarthost:
2462 debug_print = "R: smarthost for $local_part@$domain"
2463 driver = manualroute
2464 domains = ! +local_domains
2465 senders = *@posteo.net
2466 transport = remote_smtp_smarthost
2467 route_list = * posteo.de::587 byname
2468 host_find_failed = ignore
2469 same_domain_copy_routing = yes
2470 no_more
2471
2472 EOF
2473
2474 # Greping /etc/exim4, unqualified mails this would end up as
2475 # a return path, so it should go somewhere we will see.
2476 # The debconf output about mailname is as follows:
2477 # The 'mail name' is the domain name used to 'qualify' mail addresses without a domain
2478 # name.
2479 # This name will also be used by other programs. It should be the single, fully
2480 # qualified domain name (FQDN).
2481 # Thus, if a mail address on the local host is foo@example.org, the correct value for
2482 # this option would be example.org.
2483 # This name won\'t appear on From: lines of outgoing messages if rewriting is enabled.
2484 echo iankelling.org > /etc/mailname
2485
2486
2487 # mail.iankelling.org so local imap clients can connect with tls and
2488 # when they happen to not be local.
2489 # todo: this should be 10.8.0.4
2490
2491 /a/exe/cedit nn /etc/hosts <<'EOF' || [[ $? == 1 ]]
2492 # note: i put nn.b8.nz into bind for good measure
2493 10.173.8.2 nn.b8.nz mx.iankelling.org
2494 EOF
2495
2496 # note: systemd-resolved will consult /etc/hosts, dnsmasq wont. this assumes
2497 # weve configured this file in dnsmasq if we are using it.
2498 /a/exe/cedit mail /etc/dnsmasq-servers.conf <<'EOF' || [[ $? == 1 ]]
2499 server=/mx.iankelling.org/127.0.1.1
2500 EOF
2501 # I used to use debconf-set-selections + dpkg-reconfigure,
2502 # which then updates this file
2503 # but the process is slower than updating it directly and then I want to set other things in
2504 # update-exim4.conf.conf, so there's no point.
2505 # The file is documented in man update-exim4.conf,
2506 # except the man page is not perfect, read the bash script to be sure about things.
2507
2508 # The debconf questions output is additional documentation that is not
2509 # easily accessible, but super long, along with the initial default comment in this
2510 # file, so I've saved that into ./mail-notes.conf.
2511 cat >>/etc/exim4/update-exim4.conf.conf <<EOF
2512 # man page: is used to build the local_domains list, together with "localhost"
2513 # this is duplicated in a later router.
2514 dc_other_hostnames='iankelling.org;zroe.org;r2e.iankelling.org;!je.b8.nz;!bk.b8.nz;*.b8.nz;b8.nz'
2515 EOF
2516
2517
2518 # dmarc. not used currently
2519 f=/etc/cron.daily/refresh-dmarc-tld-file
2520 cat >$f <<'EOF'
2521 #!/bin/bash
2522 cd /etc
2523 wget -q -N https://publicsuffix.org/list/public_suffix_list.dat
2524 EOF
2525 m chmod 755 $f
2526
2527 ;;
2528 # ** bk
2529 ## we use this host to monitor MAIL_HOST and host a mail server for someone
2530 bk)
2531
2532 echo|i /etc/exim4/conf.d/rcpt_local_acl
2533 echo|i /etc/exim4/conf.d/router/880_universal_forward
2534
2535 echo amnimal.ninja > /etc/mailname
2536
2537 /a/exe/cedit nn /etc/hosts <<'EOF' || [[ $? == 1 ]]
2538 10.173.8.2 nn.b8.nz
2539 EOF
2540
2541 sed -r -f - /etc/init.d/exim4 <<'EOF' | i /etc/init.d/exim4in
2542 s,/etc/default/exim4,/etc/default/exim4in,g
2543 s,/run/exim4/exim.pid,/run/exim4/eximin.pid,g
2544 s,(^[ #]*Provides:).*,\1 exim4in,
2545 s,(^[ #]*NAME=).*,\1"exim4in",
2546 EOF
2547 chmod +x /etc/init.d/exim4in
2548 i /etc/systemd/system/exim4in.service.d/alwaysrestart.conf <<'EOF'
2549 [Unit]
2550 # needed to continually restart
2551 StartLimitIntervalSec=0
2552
2553 [Service]
2554 Restart=always
2555 # time to sleep before restarting a service
2556 RestartSec=1
2557 EOF
2558
2559 i /etc/default/exim4in <<'EOF'
2560 # defaults but no queue runner and alternate config dir
2561 QUEUERUNNER='no'
2562 COMMONOPTIONS='-oP /run/exim4/eximin.pid'
2563 UPEX4OPTS='-d /etc/myexim4'
2564 EOF
2565
2566 cat >>/etc/exim4/update-exim4.conf.conf <<EOF
2567 # man page: is used to build the local_domains list, together with "localhost"
2568 dc_other_hostnames='amnimal.ninja;expertpathologyreview.com'
2569 EOF
2570
2571 ;;
2572 # ** je
2573 je)
2574 echo je.b8.nz > /etc/mailname
2575 cat >>/etc/exim4/update-exim4.conf.conf <<EOF
2576 dc_other_hostnames='je.b8.nz'
2577 EOF
2578 echo|i /etc/exim4/conf.d/router/188_exim4-config_smarthost
2579 echo|i /etc/exim4/conf.d/router/190_exim4-config_fsfsmarthost
2580 echo|i /etc/exim4/conf.d/rcpt_local_acl
2581 echo|i /etc/exim4/conf.d/router/880_universal_forward
2582 ;;
2583 # ** not MAIL_HOST|bk|je
2584 *)
2585 # this one should be removed for all non mail hosts, but
2586 # bk and je never become mail_host
2587 echo|i /etc/exim4/conf.d/router/195_dnslookup_vpn
2588
2589 echo|i /etc/exim4/conf.d/router/188_exim4-config_smarthost
2590 echo|i /etc/exim4/conf.d/router/190_exim4-config_fsfsmarthost
2591 echo|i /etc/exim4/conf.d/rcpt_local_acl
2592 echo|i /etc/exim4/conf.d/router/890_backup_copy
2593 echo|i /etc/exim4/conf.d/main/000_local-nn
2594
2595
2596 if $bhost_t; then
2597 cat >>/etc/exim4/conf.d/main/000_local <<EOF
2598 MAIN_TLS_CERTIFICATE = /etc/exim4/certs/$wghost/fullchain.pem
2599 MAIN_TLS_PRIVATEKEY = /etc/exim4/certs/$wghost/privkey.pem
2600 # so we can maintiain the originals of the backups.
2601 # we wouldnt want this if we were dealing with any other
2602 # local deliveries, but we sent all others to the smarthost
2603 # which then strips the headers.
2604 envelope_to_remove = false
2605 return_path_remove = false
2606 EOF
2607 fi
2608
2609 # catches things like cronjob email
2610 i /etc/exim4/conf.d/router/880_universal_forward <<'EOF'
2611 universal_forward:
2612 driver = redirect
2613 domains = +local_domains
2614 data = alerts@iankelling.org
2615 EOF
2616
2617
2618 for unit in ${nn_progs[@]}; do
2619 f=/etc/systemd/system/$unit.service.d/nn.conf
2620 rm -fv $f
2621 done
2622
2623 # dont i dont care if defaultnn section gets left, it wont
2624 # get used.
2625 echo | /a/exe/cedit nn /etc/hosts || [[ $? == 1 ]]
2626 echo | /a/exe/cedit mail /etc/dnsmasq-servers.conf || [[ $? == 1 ]]
2627
2628 if $bhost_t; then
2629 i /etc/exim4/conf.d/transport/30_backup_maildir <<EOF
2630 # modified debian maildir transport
2631 backup_maildir:
2632 driver = appendfile
2633 directory = /bu/md
2634 delivery_date_add
2635 # note, no return path or envelope added
2636 maildir_format
2637 directory_mode = 0700
2638 mode = 0644
2639 mode_fail_narrower = false
2640 user = $u
2641 EOF
2642
2643 i /etc/exim4/conf.d/router/870_backup_local <<EOF
2644 ### router/900_exim4-config_local_user
2645 #################################
2646
2647 backup_local:
2648 debug_print = "R: local_user for \$local_part@\$domain"
2649 driver = accept
2650 domains = eximbackup.b8.nz
2651 transport = backup_maildir
2652 EOF
2653
2654 wgholeip=$(sed -rn 's/^ *Address *= *([^/]+).*/\1/p' /etc/wireguard/wghole.conf)
2655 cat >>/etc/exim4/update-exim4.conf.conf <<EOF
2656 dc_other_hostnames='eximbackup.b8.nz'
2657 dc_local_interfaces='127.0.0.1;::1;$wgholeip'
2658
2659 EOF
2660 else
2661 cat >>/etc/exim4/update-exim4.conf.conf <<EOF
2662 # Note: If theres like a temporary problem where mail gets sent to
2663 # one of these hosts, if exim isnt listening, it will be a temporary error
2664 # instead of a permanent 5xx.
2665 dc_local_interfaces='127.0.0.1;::1'
2666 EOF
2667 fi
2668 cat >>/etc/exim4/update-exim4.conf.conf <<EOF
2669 dc_eximconfig_configtype='smarthost'
2670 dc_smarthost='$smarthost'
2671 EOF
2672
2673 hostname -f |i /etc/mailname
2674 cat >>/etc/exim4/update-exim4.conf.conf <<EOF
2675 # The manpage incorrectly states this will do header rewriting, but
2676 # that only happens if we have dc_hide_mailname is set.
2677 dc_readhost='iankelling.org'
2678 # Only used in case of bounces.
2679 dc_localdelivery='maildir_home'
2680 EOF
2681 ;;
2682 esac
2683
2684
2685
2686
2687 # ** $MAILHOST|bk, things that belong at the end
2688 case $HOSTNAME in
2689 $MAIL_HOST|bk)
2690 # config for the non-nn exim
2691 m rsync -ra --delete /etc/exim4/ /etc/myexim4
2692 cat >>/etc/myexim4/update-exim4.conf.conf <<'EOF'
2693 dc_eximconfig_configtype='smarthost'
2694 dc_smarthost='nn.b8.nz'
2695 EOF
2696 ;;&
2697 bk)
2698
2699 # config for the non-nn exim
2700 cat >/etc/myexim4/conf.d/main/000_local-nn <<'EOF'
2701 MAIN_HARDCODE_PRIMARY_HOSTNAME = mail2.iankelling.org
2702 EOF
2703 ;;
2704 $MAIL_HOST)
2705 # for bk, we have a exim4in.service that will do this for us.
2706 m update-exim4.conf -d /etc/myexim4
2707 ;;
2708 esac
2709
2710 # * spool dir setup
2711
2712 # ** bind mount setup
2713 # put spool dir in directory that spans multiple distros.
2714 # based on http://www.postfix.org/qmgr.8.html and my notes in gnus
2715 #
2716 dir=/nocow/exim4
2717 sdir=/var/spool/exim4
2718 # we only do this if our system has $dir
2719
2720 # this used to do a symlink, but, in the boot logs, /nocow would get mounted succesfully,
2721 # about 2 seconds later, exim starts, and immediately puts into paniclog:
2722 # honVi-0000u3-82 Failed to create directory "/var/spool/exim4/input": No such file or directory
2723 # so, im trying a bind mount to get rid of that.
2724 if [[ -e /nocow ]]; then
2725 if ! grep -Fx "/nocow/exim4 /var/spool/exim4 none bind 0 0" /etc/fstab; then
2726 echo "/nocow/exim4 /var/spool/exim4 none bind 0 0" >>/etc/fstab
2727 fi
2728 i /etc/systemd/system/exim4.service.d/override.conf <<'EOF'
2729 [Unit]
2730 # without local-fs on exim, we get these kind of errors in paniclog on shutdown:
2731 # Failed to create spool file /var/spool/exim4//input//1jCLxz-0008V4-V9-D: Permission denied
2732 After=local-fs.target
2733 EOF
2734 if ! mountpoint -q $sdir; then
2735 stopifactive exim4 exim4in
2736 if [[ -L $sdir ]]; then
2737 m rm $sdir
2738 fi
2739 if [[ ! -e $dir && -d $sdir ]]; then
2740 m mv $sdir $dir
2741 fi
2742 if [[ ! -d $sdir ]]; then
2743 m mkdir $sdir
2744 m chmod 000 $sdir # only want it to be used when its mounted
2745 fi
2746 m mount $sdir
2747 fi
2748 fi
2749
2750
2751
2752 # ** exim/spool uid setup
2753 # i have the spool directory be common to distro multi-boot, so
2754 # we need the uid to be the same. 608 cuz it's kind of in the middle
2755 # of the free system uids.
2756 IFS=:; read -r _ _ uid _ < <(getent passwd Debian-exim ||:) ||:; unset IFS
2757 IFS=:; read -r _ _ gid _ < <(getent group Debian-exim ||:) ||:; unset IFS
2758 if [[ ! $uid ]]; then
2759 # from /var/lib/dpkg/info/exim4-base.postinst, plus uid and gid options
2760 m adduser --uid 608 --system --group --quiet --home /var/spool/exim4 \
2761 --no-create-home --disabled-login --force-badname Debian-exim
2762 elif [[ $uid != 608 ]]; then
2763 stopifactive exim4 exim4in
2764 m usermod -u 608 Debian-exim
2765 m groupmod -g 608 Debian-exim
2766 m usermod -g 608 Debian-exim
2767 m find / /nocow -path ./var/tmp -prune -o -xdev -uid $uid -execdir chown -h 608 {} +
2768 m find / /nocow -path ./var/tmp -prune -o -xdev -gid $gid -execdir chgrp -h 608 {} +
2769 fi
2770
2771 # * start / stop services
2772
2773 reifactive dnsmasq nscd
2774
2775 if $reload; then
2776 m systemctl daemon-reload
2777 fi
2778
2779 m systemctl --now enable epanicclean.timer
2780
2781 case $HOSTNAME in
2782 je)
2783 /a/exe/web-conf apache2 je.b8.nz
2784 ;;
2785 bk)
2786 /a/exe/web-conf apache2 mail2.iankelling.org
2787 ;;
2788 esac
2789
2790 m /a/bin/ds/mail-cert-cron -1
2791 sre mailcert.timer
2792
2793 case $HOSTNAME in
2794 bk)
2795 # todo, this should be done in distro-begin
2796 soff systemd-resolved
2797 ln -sf 127.0.0.1-resolv/stub-resolv.conf /etc/resolv.conf
2798 ;;&
2799 $MAIL_HOST|bk)
2800 m systemctl --now enable mailnn mailnnroute
2801 ;;&
2802 $MAIL_HOST)
2803 # we use dns to start wg
2804 if $reload; then
2805 sre unbound
2806 else
2807 m systemctl --now enable unbound
2808 fi
2809 ;;&
2810 $MAIL_HOST|bk)
2811 # If these have changes, id rather manually restart it, id rather
2812 # not restart and cause temporary errors
2813 if $reload; then
2814 sre $vpnser
2815 else
2816 m systemctl --now enable $vpnser
2817 fi
2818 if ! systemctl is-active clamav-daemon >/dev/null; then
2819 m systemctl --now enable clamav-daemon
2820 out=$(rsync -aiSAX --chown=root:root --chmod=g-s /a/bin/ds/filesystem/etc/systemd/system/epanicclean.{timer,service} /etc/systemd/system)
2821 if [[ $out ]]; then
2822 reload=true
2823 fi
2824
2825 # note, this will cause paniclog entries because it takes like 45
2826 # seconds for clamav to start, i use ./epanic-clean to remove
2827 # them.
2828 fi
2829 ;;&
2830 $MAIL_HOST|bk|je)
2831 # start spamassassin/dovecot before exim.
2832 sre dovecot spamassassin
2833 # need to wait a bit before restarting exim, else I
2834 # get a paniclog entry like: spam acl condition: all spamd servers failed
2835 sleep 3
2836 m systemctl --now enable mailclean.timer
2837 ;;&
2838 $MAIL_HOST)
2839 # < 2.1 (eg: in t9), uses a different data format which required manual
2840 # migration. dont start if we are running an old version.
2841 if dpkg --compare-versions $(dpkg -s radicale | awk '$1 == "Version:" { print $2 }') ge 2.1; then
2842 m systemctl --now enable radicale
2843 fi
2844 ;;&
2845 esac
2846
2847 # last use of $reload happens in previous block
2848 rm -f /var/local/mail-setup-reload
2849
2850
2851 case $HOSTNAME in
2852 $MAIL_HOST|bk|je) : ;;
2853 *)
2854 soff radicale mailclean.timer dovecot spamassassin $vpnser mailnn clamav-daemon
2855 ;;
2856 esac
2857
2858 sre exim4
2859
2860 case $HOSTNAME in
2861 $MAIL_HOST)
2862 m systemctl --now enable mailbindwatchdog
2863 ;;
2864 *)
2865 soff mailbindwatchdog
2866 ;;
2867 esac
2868
2869
2870 case $HOSTNAME in
2871 bk) sre exim4in ;;
2872 esac
2873
2874 # * mail monitoring / testing
2875
2876 # note, to test clamav, send an email with body that only contains
2877 # https://en.wikipedia.org/wiki/EICAR_test_file
2878 # which set malware_name to Eicar-Signature
2879 case $HOSTNAME in
2880 $MAIL_HOST|bk|je)
2881 # note: cronjob "ian" also does some important monitoring
2882 # todo: this will sometimes cause an alert because mailtest-check will run
2883 # before we have setup network namespace and spamassassin
2884 cat >/etc/cron.d/mailtest <<EOF
2885 SHELL=/bin/bash
2886 PATH=/usr/bin:/bin:/usr/local/bin
2887 MAILTO=alerts@iankelling.org
2888 */5 * * * * $u send-test-forward |& log-once send-test-forward
2889 */10 * * * * root chmod -R g+rw /m/md/bounces |& log-once -1 bounces-chmod
2890 # im seeing some intermittent failures on the slow check, do it all the time
2891 # for now. It looks like a dns failure.
2892 #5-59/5 * * * * root mailtest-check |& log-once -1 mailtest-check
2893 #0 * * * * root mailtest-check slow |& log-once -1 mailtest-slow
2894 */5 * * * * root timeout 290 mailtest-check slow |& log-once -12 mailtest-check
2895 EOF
2896 m sudo rsync -ahhi --chown=root:root --chmod=0755 \
2897 /b/ds/mailtest-check /b/ds/check-remote-mailqs /usr/local/bin/
2898 ;;&
2899 $MAIL_HOST)
2900 test_froms=(ian@iankelling.org z@zroe.org iank@gnu.org)
2901 test_to="testignore@expertpathologyreview.com, testignore@je.b8.nz, testignore@amnimal.ninja, jtuttle@gnu.org"
2902
2903 cat >>/etc/cron.d/mailtest <<EOF
2904 2 * * * * $u check-remote-mailqs |& log-once check-remote-mailqs
2905 EOF
2906 ;;&
2907 bk)
2908 test_froms=(testignore@expertpathologyreview.com testignore@amnimal.ninja)
2909 test_to="testignore@iankelling.org, testignore@zroe.org, testignore@je.b8.nz"
2910 ;;&
2911 je)
2912 test_froms=(testignore@je.b8.nz)
2913 test_to="testignore@iankelling.org, testignore@zroe.org, testignore@expertpathologyreview.com, testignore@amnimal.ninja"
2914 ;;&
2915 $MAIL_HOST|bk|je)
2916 cat >/usr/local/bin/send-test-forward <<'EOF'
2917 #!/bin/bash
2918 olds=(
2919 $(/sbin/exiqgrep -o 260 -i -r '^(testignore@(iankelling\.org|zroe\.org|expertpathologyreview\.com|amnimal\.ninja|je\.b8\.nz)|jtuttle@gnu\.org)$')
2920 )
2921 if (( ${#olds[@]} )); then
2922 /sbin/exim -Mrm "${olds[@]}" >/dev/null
2923 fi
2924 EOF
2925 for test_from in ${test_froms[@]}; do
2926 cat >>/usr/local/bin/send-test-forward <<EOFOUTER
2927 /usr/sbin/exim -f $test_from -t <<EOF
2928 From: $test_from
2929 To: $test_to
2930 Subject: test \$(date +%Y-%m-%dT%H:%M:%S%z) \$(date +%s)
2931
2932 /usr/local/bin/send-test-forward
2933 EOF
2934 EOFOUTER
2935 done
2936 m chmod +x /usr/local/bin/send-test-forward
2937 ;;
2938 *)
2939 rm -fv /etc/cron.d/mailtest
2940 ;;
2941 esac
2942
2943
2944
2945 # * misc
2946 m sudo -u $u mkdir -p /home/$u/.cache
2947 set -- /m/mucache /home/$u/.cache/mu /m/.mu /home/$u/.mu
2948 while (($#)); do
2949 target=$1
2950 f=$2
2951 shift 2
2952 if [[ ! -L $f ]]; then
2953 if [[ -e $f ]]; then
2954 rm -rf $f
2955 fi
2956 m sudo -u $u ln -sf -T $target $f
2957 fi
2958 done
2959
2960
2961 # /etc/alias setup is debian specific, and exim postinst script sets up
2962 # an /etc/alias from root to the postmaster, based on the question
2963 # exim4-config exim4/dc_postmaster, as long as there exists an entry for
2964 # root, or there was no preexisting aliases file. postfix won\'t set up
2965 # a root to $postmaster alias if it\'s already installed. Easiest to
2966 # just set it ourselves.
2967
2968 # debconf question for postmaster:
2969 # Mail for the 'postmaster', 'root', and other system accounts needs to be redirected
2970 # to the user account of the actual system administrator.
2971 # If this value is left empty, such mail will be saved in /var/mail/mail, which is not
2972 # recommended.
2973 # Note that postmaster\'s mail should be read on the system to which it is directed,
2974 # rather than being forwarded elsewhere, so (at least one of) the users listed here
2975 # should not redirect their mail off this machine. A 'real-' prefix can be used to
2976 # force local delivery.
2977 # Multiple user names need to be separated by spaces.
2978 # Root and postmaster mail recipient:
2979
2980 m exit 0
2981 :
2982
2983 # Local Variables:
2984 # eval: (outline-minor-mode)
2985 # outline-regexp: "\\( *\\)# [*]\\{1,8\\} "
2986 # End:
2987 # this is combined with defining outline-level in init.el