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