Mainly add external monitoring of mail server
[distro-setup] / mail-setup
index ee8482d6495f7b53b00ef9a44c6509166d14e292..68478bec55a07860a6c86d82ec3cefaae97704c9 100755 (executable)
@@ -11,9 +11,7 @@ m() { printf "$pre %s\n"  "$*"; "$@"; }
 e() { printf "$pre %s\n"  "$*"; }
 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $0: $*" >&2; }
 
-
-# TODO: copy dkim keys from within this file. its now done in conflink.
-# TODO: fix dkim key to b chmod 640, group Debian-exim
+shopt -s nullglob
 
 if [[ -s /usr/local/lib/err ]]; then
   source /usr/local/lib/err
@@ -37,9 +35,6 @@ usage() {
 Usage: ${0##*/}
 Setup exim4 & dovecot & related things
 
-The minimal assumption we have is that /etc/mailpass exists
-
-
 -h|--help  Print help and exit.
 EOF
   exit $1
@@ -55,7 +50,9 @@ EOF
 # background: ovecot does not yet have ocsp stapling support
 # reference: https://community.letsencrypt.org/t/simple-guide-using-lets-encrypt-ssl-certs-with-dovecot/2921
 #
-# for phone, same thing but username alerts, pass in ivy-pass.
+# for phone, k9mail, same thing but username alerts, pass in ivy-pass.
+# also, l2.b8.nz for secondary alerts
+# fetching mail settings: folder poll frequency 10 minutes
 #######
 
 
@@ -64,30 +61,19 @@ EOF
 # # for hosts which have all private files I just use the same user
 # # for other hosts, each one get\'s their own password.
 # # for generating secure pass, and storing for server too:
-# # user=USUALLY_SAME_AS_HOSTNAME
-# user=li
 # f=$(mktemp)
+# I use $HOSTNAME as username
 # apg -m 50 -x 70 -n 1 -a 1 -M CLN >$f
-# s sed -i "/^$user:/d" /p/c/filesystem/etc/exim4/passwd
-# echo "$user:$(mkpasswd -m sha-512 -s <$f)" >>/p/c/filesystem/etc/exim4/passwd
-# # todo: port is no longer used in mailpass, remove it.
-# echo "mail.iankelling.org 587 $user:$(<$f)" >> /p/c/machine_specific/$user/filesystem/etc/mailpass
-# # then run this script, or part of it which uses /etc/mailpass
+# s sed -i "/^$HOSTNAME:/d" /p/c/filesystem/etc/exim4/passwd
+# echo "$HOSTNAME:$(mkpasswd -m sha-512 -s <$f)" >>/p/c/filesystem/etc/exim4/passwd
+# reference: exim4_passwd_client(5)
+# echo "mail.iankelling.org:$HOSTNAME:$(<$f)" > /p/c/machine_specific/$HOSTNAME/filesystem/etc/exim4/passwd.client
+# # then run this script
 
 # # dovecot password, i just need 1 as I\'m the only user
 # mkdir /p/c/filesystem/etc/dovecot
 # echo "iank:$(doveadm pw -s ssha256)::::::" >>/p/c/filesystem/etc/dovecot/users
-# conflink
-
 
-
-# # for ad-hoc testing of some random new host sending mail:
-# user=li # client host username & hostname
-# f=$(mktemp)
-# apg -m 50 -x 70 -n 1 -a 1 -M CLN >$f
-# s sed -i "/^$user:/d" /etc/exim4/passwd
-# echo "$user:$(mkpasswd -m sha-512 -s <$f)" | s tee -a /etc/exim4/passwd
-# echo "mail.iankelling.org:$user:$(<$f)" | ssh root@$user dd of=/etc/exim4/passwd.client
 ####### end perstent password instructions ######
 
 
@@ -113,12 +99,7 @@ EOF
 # # lines 2+: append to hold space
 # echo "txt record contents:"
 # echo "v=DKIM1; k=rsa; p=$(sed -n '${x;s/\n//gp};2,$H' $domain.pem)"
-# chmod 644 $domain.pem
-# chmod 640 $domain-private.pem
-# # in conflink, we chown these to group debian
-# conflink
 # # selector was also put into /etc/exim4/conf.d/main/000_local,
-# # via the mail-setup scripts
 
 # # 2017-02 dmarc policies:
 # # host -t txt _dmarc.gmail.com
@@ -152,7 +133,7 @@ EOF
 
 # * functions  constants
 e() { printf "%s\n" "$*"; }
-pi() { # package install
+pi() { # package install without starting daemons
   local f
   if dpkg -s -- "$@" &> /dev/null; then
     return 0;
@@ -162,7 +143,18 @@ pi() { # package install
   if [[ ! -r $f ]] || (( $(( $(date +%s) - $(stat -c %Y $f ) )) > 60*60*12 )); then
     m apt-get update
   fi
-  DEBIAN_FRONTEND=noninteractive m apt-get -y install --purge --auto-remove "$@"
+  f=/usr/sbin/policy-rc.d
+  dd of=$f 2>/dev/null <<EOF
+#!/bin/sh
+exit 101
+EOF
+  chmod +x $f
+  ret=
+  DEBIAN_FRONTEND=noninteractive m apt-get -y install --purge --auto-remove "$@" || ret=$?
+  rm $f
+  if [[ $ret ]]; then
+    err-exit $ret "failed apt-get install above"
+  fi
 }
 
 postmaster=alerts
@@ -189,16 +181,20 @@ fi
 
 uhome=$(eval echo ~$u)
 ### * user forward file
-if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
-  # afaik, these will get ignored because they are routing to my own
-  # machine, but rm them is safer
-  rm -fv $uhome/.forward /root/.forward
-else
-  # this can\'t be a symlink and has permission restrictions
-  # it might work in /etc/aliases, but this seems more proper.
-  e setting $uhome/.forward to $forward
-  install -m 644 {-o,-g}$u <(e $forward) $uhome/.forward
-fi
+
+case $HOSTNAME in
+  $MAIL_HOST|l2)
+    # afaik, these will get ignored on MAIL_HOST because they are routing to my own
+    # machine, but rm them is safer
+    rm -fv $uhome/.forward /root/.forward
+    ;;
+  *)
+    # this can\'t be a symlink and has permission restrictions
+    # it might work in /etc/aliases, but this seems more proper.
+    e setting $uhome/.forward to $forward
+    install -m 644 {-o,-g}$u <(e $forward) $uhome/.forward
+    ;;
+esac
 
 # * Mail clean cronjob
 
@@ -367,6 +363,9 @@ source /a/bin/bash_unpublished/source-state
 # local mail that bounces goes to /Maildir or /root/Maildir
 dirs=(/m/md/bounces/{cur,tmp,new})
 m mkdir -p ${dirs[@]}
+m chown iank:iank /m /m/md
+m ln -sfT /m/md /m/iank
+m chmod 700 /m /m/md
 m chown -R $u:Debian-exim /m/md/bounces
 m chmod 775 ${dirs[@]}
 m usermod -a -G Debian-exim $u
@@ -378,16 +377,6 @@ for d in /Maildir /root/Maildir; do
 done
 
 
-### begin setup passwd.client
-f=/etc/exim4/passwd.client
-rm -fv /etc/exim4/passwd.client
-m install -m 640 -g Debian-exim /dev/null $f
-while read -r domain _ pass; do
-  # reference: exim4_passwd_client(5)
-  printf "%s:%s\n" "$domain" "$pass" >>$f
-done </etc/mailpass
-###  end setup passwd.client
-
 # by default, only 10 days of logs are kept. increase that.
 m sed -ri 's/^(\s*rotate\s).*/\11000/' /etc/logrotate.d/exim4-base
 
@@ -422,6 +411,32 @@ MAIN_LOG_SELECTOR = +all
 # when doing mail redelivery to invoke filters. Also allows
 # me exiqgrep and stuff.
 MAIN_TRUSTED_GROUPS = $u
+
+# default is 10. when exim has been down for a bit, fsf mailserver
+# will do a big send in one connection, then exim decides to put
+# the messages in the queue instead of delivering them, to avoid
+# spawning too many delivery processes. Pretty sure my system
+# can handle a lot more, but lets go with this.
+smtp_accept_queue_per_connection = 100
+
+
+DKIM_CANON = relaxed
+DKIM_SELECTOR = li
+
+# from comments in
+# https://debian-administration.org/article/718/DKIM-signing_outgoing_mail_with_exim4
+
+# The file is based on the outgoing domain-name in the from-header.
+DKIM_DOMAIN = \${lc:\${domain:\$h_from:}}
+# sign if key exists
+DKIM_PRIVATE_KEY= \${if exists{/etc/exim4/\${dkim_domain}-private.pem} {/etc/exim4/\${dkim_domain}-private.pem}}
+
+# most of the ones that gmail seems to use.
+# Exim has horrible default of signing unincluded
+# list- headers since they got mentioned in an
+# rfc, but this messes up mailing lists, like gnu/debian which want to
+# keep your dkim signature intact but add list- headers.
+DKIM_SIGN_HEADERS = mime-version:in-reply-to:references:from:date:subject:to
 EOF
 
 rm -fv /etc/exim4/rcpt_local_acl # old path
@@ -532,10 +547,9 @@ dc_local_interfaces=''
 dc_mailname_in_oh='true'
 EOF
 
-# * if MAIL_HOST
-if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
-  # ** dovecot
-  ####### begin dovecot setup ########
+
+# ** dovecot
+dovecot-setup() {
   # based on a little google and package search, just the dovecot
   # packages we need instead of dovecot-common.
   #
@@ -548,14 +562,23 @@ if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
   # internet. I was torn about whether to do this or not, meh.
   pi dovecot-core dovecot-imapd dovecot-sieve dovecot-lmtpd
 
+  for f in /p/c{/machine_specific/$HOSTNAME,}/filesystem/etc/dovecot/users; do
+    e $f
+    if [[ -e $f ]]; then
+      m sudo rsync -ahhi --chown=root:dovecot --chmod=0640 $f /etc/dovecot/
+      break
+    fi
+  done
   for f in /p/c/subdir_files/sieve/*sieve /a/c/subdir_files/sieve/*sieve; do
     m sudo -u $u /a/exe/lnf -T $f $uhome/sieve/${f##*/}
   done
-  # if we changed 90-sieve.conf and removed the active part of the
+  # If we changed 90-sieve.conf and removed the active part of the
   # sieve option, we wouldn\'t need this, but I\'d rather not modify a
   # default config if not needed. This won\'t work as a symlink in /a/c
   # unfortunately.
-  m sudo -u $u /a/exe/lnf -T sieve/main.sieve $uhome/.dovecot.sieve
+  if [[ -e $uhome/sieve/personal.sieve ]]; then
+    m sudo -u $u /a/exe/lnf -T sieve/main.sieve $uhome/.dovecot.sieve
+  fi
 
   # we set this later in local.conf
   sed -ri -f - /etc/dovecot/conf.d/10-mail.conf <<'EOF'
@@ -617,7 +640,7 @@ ssl_cipher_list = ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDH
 # ian: added this, more secure, per google etc
 ssl_prefer_server_ciphers = yes
 
-
+# ian: %u is used for alerts user vs iank
 mail_location = maildir:/m/%u:LAYOUT=fs:INBOX=/m/%u/INBOX
 mail_uid = $u
 mail_gid = $u
@@ -627,40 +650,53 @@ mail_gid = $u
 # auth_verbose=yes
 #mail_debug=yes
 EOF
-  ####### end dovecot setup ########
+  ####### end dovecot-setup ########
+}
+
+
+
+# * if MAIL_HOST
+case $HOSTNAME in
+  $MAIL_HOST)
+    dovecot-setup
+
+    # ** exim
+
+    sudo rsync -ahhi --chown=root:Debian-exim --chmod=0640 \
+         /p/c/filesystem/etc/exim4/passwd /p/c/filesystem/etc/exim4/*.pem /etc/exim4/
 
-  # ** exim
 
-  # mail.iankelling.org so local imap clients can connect with tls and
-  # when they happen to not be local.
-  sed -ri -f - /etc/hosts <<'EOF'
+    # mail.iankelling.org so local imap clients can connect with tls and
+    # when they happen to not be local.
+    sed -ri -f - /etc/hosts <<'EOF'
 /^127\.0\.1\.1.* mail\.iankelling\.org\b/{p;d}
 /^127\.0\.1\.1 /s/ *$/ mail.iankelling.org/
 EOF
-  /a/exe/cedit mail /etc/dnsmasq-servers.conf <<'EOF' || [[ $? == 1 ]]
+    /a/exe/cedit mail /etc/dnsmasq-servers.conf <<'EOF' || [[ $? == 1 ]]
 server=/mail.iankelling.org/127.0.1.1
 EOF
-  if systemctl is-active dnsmasq >/dev/null; then
-    m systemctl reload dnsmasq
-  fi
+    if systemctl is-active dnsmasq >/dev/null; then
+      m systemctl reload dnsmasq
+      m nscd -i hosts
+    fi
 
-  # I used to use debconf-set-selections + dpkg-reconfigure,
-  # which then updates this file
-  # but the process is slower than updating it directly and then I want to set other things in
-  # update-exim4.conf.conf, so there's no point.
-  # The file is documented in man update-exim4.conf,
-  # except the man page is not perfect, read the bash script to be sure about things.
+    # I used to use debconf-set-selections + dpkg-reconfigure,
+    # which then updates this file
+    # but the process is slower than updating it directly and then I want to set other things in
+    # update-exim4.conf.conf, so there's no point.
+    # The file is documented in man update-exim4.conf,
+    # except the man page is not perfect, read the bash script to be sure about things.
 
-  # The debconf questions output is additional documentation that is not
-  # easily accessible, but super long, along with the initial default comment in this
-  # file, so I've saved that into ./mail-notes.conf.
+    # The debconf questions output is additional documentation that is not
+    # easily accessible, but super long, along with the initial default comment in this
+    # file, so I've saved that into ./mail-notes.conf.
 
-  cat >>/etc/exim4/update-exim4.conf.conf <<EOF
+    cat >>/etc/exim4/update-exim4.conf.conf <<EOF
 # note: some things we don't set that are here by default because they are unused.
 
 dc_eximconfig_configtype='internet'
 
-# man page: is used to build the local_domains list, together with “localhost”
+# man page: is used to build the local_domains list, together with "localhost"
 # iank.bid is for testing
 # mail.iankelling.org is for machines i own
 dc_other_hostnames='*.iankelling.org;iankelling.org;*iank.bid;iank.bid;*zroe.org;zroe.org;*.b8.nz;b8.nz'
@@ -670,38 +706,29 @@ dc_other_hostnames='*.iankelling.org;iankelling.org;*iank.bid;iank.bid;*zroe.org
 # because this machine serves as secondary MX for these domains. Sets MAIN_RELAY_TO_DOMAINS.
 # todo: we should not accept from anywhere, only the mx for fsf.
 dc_relay_domains='*.fsf.org;fsf.org'
+dc_localdelivery='dovecot_lmtp'
 EOF
 
 
-  # the debconf output about mailname is as follows:
-  # The 'mail name' is the domain name used to 'qualify' mail addresses without a domain
-  # name.
-  # This name will also be used by other programs. It should be the single, fully
-  # qualified domain name (FQDN).
-  # Thus, if a mail address on the local host is foo@example.org, the correct value for
-  # this option would be example.org.
-  # This name won\'t appear on From: lines of outgoing messages if rewriting is enabled.
+    # the debconf output about mailname is as follows:
+    # The 'mail name' is the domain name used to 'qualify' mail addresses without a domain
+    # name.
+    # This name will also be used by other programs. It should be the single, fully
+    # qualified domain name (FQDN).
+    # Thus, if a mail address on the local host is foo@example.org, the correct value for
+    # this option would be example.org.
+    # This name won\'t appear on From: lines of outgoing messages if rewriting is enabled.
 
-  echo mail.iankelling.org > /etc/mailname
+    echo mail.iankelling.org > /etc/mailname
 
-  # MAIN_HARDCODE_PRIMARY_HOSTNAME might mess up the
-  # smarthost config type, not sure. all other settings
-  # would be unused in that config type.
-  cat >>/etc/exim4/conf.d/main/000_local <<EOF
+    # MAIN_HARDCODE_PRIMARY_HOSTNAME might mess up the
+    # smarthost config type, not sure. all other settings
+    # would be unused in that config type.
+    cat >>/etc/exim4/conf.d/main/000_local <<EOF
 # enable 587 in addition to the default 25, so that
 # i can send mail where port 25 is firewalled by isp
 daemon_smtp_ports = 25 : 587
 
-DKIM_CANON = relaxed
-DKIM_SELECTOR = li
-
-# from comments in
-# https://debian-administration.org/article/718/DKIM-signing_outgoing_mail_with_exim4
-
-# The file is based on the outgoing domain-name in the from-header.
-DKIM_DOMAIN = \${lc:\${domain:\$h_from:}}
-# sign if key exists
-DKIM_PRIVATE_KEY= \${if exists{/etc/exim4/\${dkim_domain}-private.pem} {/etc/exim4/\${dkim_domain}-private.pem}}
 
 
 # failing message on mail-tester.com:
@@ -715,18 +742,10 @@ DKIM_PRIVATE_KEY= \${if exists{/etc/exim4/\${dkim_domain}-private.pem} {/etc/exi
 # Seems logical for this to be the same as mailname.
 MAIN_HARDCODE_PRIMARY_HOSTNAME = mail.iankelling.org
 
-LOCAL_DELIVERY = dovecot_lmtp
-
 # options exim has to avoid having to alter the default config files
 CHECK_RCPT_LOCAL_ACL_FILE = /etc/exim4/conf.d/rcpt_local_acl
 CHECK_DATA_LOCAL_ACL_FILE = /etc/exim4/conf.d/data_local_acl
 
-# most of the ones that gmail seems to use.
-# Exim has horrible default of signing unincluded
-# list- headers since they got mentioned in an
-# rfc, but this messes up mailing lists, like gnu/debian which want to
-# keep your dkim signature intact but add list- headers.
-DKIM_SIGN_HEADERS = mime-version:in-reply-to:references:from:date:subject:to
 
 # recommended if dns is expected to work
 CHECK_RCPT_VERIFY_SENDER = true
@@ -740,24 +759,24 @@ CHECK_MAIL_HELO_ISSUED = true
 #dmarc_tld_file = /etc/public_suffix_list.dat
 EOF
 
-  f=/etc/cron.daily/refresh-dmarc-tld-file
-  cat >$f <<'EOF'
+    f=/etc/cron.daily/refresh-dmarc-tld-file
+    cat >$f <<'EOF'
 #!/bin/bash
 cd /etc
 wget -q -N https://publicsuffix.org/list/public_suffix_list.dat
 EOF
-  m chmod 755 $f
+    m chmod 755 $f
 
-  sed -i --follow-symlinks -f - /etc/aliases <<EOF
+    sed -i --follow-symlinks -f - /etc/aliases <<EOF
 \$a root: $postmaster
 /^root:/d
 EOF
 
 
-  # https://selivan.github.io/2017/12/30/systemd-serice-always-restart.html
-  d=/etc/systemd/system/openvpn@mail.service.d
-  m mkdir -p $d
-  cat >$d/override.conf <<'EOF'
+    # https://selivan.github.io/2017/12/30/systemd-serice-always-restart.html
+    d=/etc/systemd/system/openvpn@mail.service.d
+    m mkdir -p $d
+    cat >$d/override.conf <<'EOF'
 [Service]
 Restart=always
 # time to sleep before restarting a service
@@ -767,63 +786,93 @@ RestartSec=1
 # StartLimitIntervalSec in recent systemd versions
 StartLimitInterval=0
 EOF
-  if ! systemctl cat openvpn@mail.service|grep -xF StartLimitInterval=0 &>/dev/null; then
-    # needed for the above config to go into effect
-    m systemctl daemon-reexec
-  fi
-
+    if ! systemctl cat openvpn@mail.service|grep -xF StartLimitInterval=0 &>/dev/null; then
+      # needed for the above config to go into effect
+      m systemctl daemon-reexec
+    fi
 
-  m systemctl enable mailclean.timer
-  m systemctl start mailclean.timer
-  m systemctl restart $vpn_ser@mail
-  m systemctl enable $vpn_ser@mail
-  m systemctl enable dovecot
-  m systemctl restart dovecot
 
+    m systemctl enable mailclean.timer
+    m systemctl start mailclean.timer
+    m systemctl restart $vpn_ser@mail
+    m systemctl enable $vpn_ser@mail
+    m systemctl enable dovecot
+    m systemctl restart dovecot
+    ;;
   # * not MAIL_HOST
-else # $HOSTNAME != $MAIL_HOST
-  # remove mail. 2 lines to properly remove whitespace
-  sed -ri -f - /etc/hosts <<'EOF'
+  *) # $HOSTNAME != $MAIL_HOST
+    # remove mail. 2 lines to properly remove whitespace
+    sed -ri -f - /etc/hosts <<'EOF'
 s#^(127\.0\.1\.1 .*) +mail\.iankelling\.org$#\1#
 s#^(127\.0\.1\.1 .*)mail\.iankelling\.org +(.*)#\1\2#
 EOF
 
-  echo | /a/exe/cedit mail /etc/dnsmasq-servers.conf || [[ $? == 1 ]]
-  if systemctl is-active dnsmasq >/dev/null; then
-    m systemctl reload dnsmasq
-  fi
+    echo | /a/exe/cedit mail /etc/dnsmasq-servers.conf || [[ $? == 1 ]]
+    if systemctl is-active dnsmasq >/dev/null; then
+      m nscd -i hosts
+      m systemctl reload dnsmasq
+    fi
 
-  m systemctl disable mailclean.timer &>/dev/null ||:
-  m systemctl stop mailclean.timer &>/dev/null ||:
-  m systemctl disable $vpn_ser@mail
-  m systemctl stop $vpn_ser@mail
-  m systemctl disable dovecot ||:
-  m systemctl stop dovecot ||:
-  #
-  #
-  # would only exist because I wrote it i the previous condition,
-  # it\'s not part of exim
-  rm -fv /etc/exim4/conf.d/main/000_localmacros
-  cat >>/etc/exim4/update-exim4.conf.conf <<EOF
+    m systemctl disable mailclean.timer &>/dev/null ||:
+    m systemctl stop mailclean.timer &>/dev/null ||:
+    m systemctl disable $vpn_ser@mail
+    m systemctl stop $vpn_ser@mail
+    #
+    #
+    # would only exist because I wrote it i the previous condition,
+    # it\'s not part of exim
+    rm -fv /etc/exim4/conf.d/main/000_localmacros
+    cat >>/etc/exim4/update-exim4.conf.conf <<EOF
 dc_eximconfig_configtype='smarthost'
 dc_smarthost='$smarthost'
 # The manpage incorrectly states this will do header rewriting, but
 # that only happens if we have dc_hide_mailname is set.
 dc_readhost='iankelling.org'
-# Only used in case of bounces.
-dc_localdelivery='maildir_home'
 EOF
 
-  hostname -f >/etc/mailname
+    hostname -f >/etc/mailname
 
-  # This ends up at alerts mailbox on MAIL_HOST, but using a user that doesn't exist elsewhere
-  # is no good.
-  sed -i --follow-symlinks -f - /etc/aliases <<EOF
-\$a root: root@mail.iankelling.org
+    ;;&
+  ## we use this host to monitor MAIL_HOST
+  l2)
+    dovecot-setup
+    m systemctl enable dovecot
+    m systemctl restart dovecot
+    cat >>/etc/exim4/update-exim4.conf.conf <<EOF
+# man page: is used to build the local_domains list, together with "localhost"
+# mail.iankelling.org is for machines i own
+dc_other_hostnames='l2.b8.nz'
+dc_localdelivery='dovecot_lmtp'
+EOF
+    # This ends up at alerts mailbox on MAIL_HOST, but using a user that doesn't exist elsewhere
+    # is no good.
+    sed -i --follow-symlinks -f - /etc/aliases <<EOF
+\$a root: iank
 /^root:/d
 EOF
+    ;;
+  *)
+
+    f=/p/c/filesystem/etc/exim4/passwd.client
+    if [[ ! -e $f ]]; then
+      f=/p/c/machine_specific/$HOSTNAME/filesystem/etc/exim4/passwd.client
+    fi
+    sudo rsync -ahhi --chown=root:Debian-exim --chmod=0640 $f /etc/exim4/
 
-fi # end $HOSTNAME != $MAIL_HOST
+    # This ends up at alerts mailbox on MAIL_HOST, but using a user that doesn't exist elsewhere
+    # is no good.
+    sed -i --follow-symlinks -f - /etc/aliases <<EOF
+\$a root: root@mail.iankelling.org
+/^root:/d
+EOF
+    cat >>/etc/exim4/update-exim4.conf.conf <<EOF
+# Only used in case of bounces.
+dc_localdelivery='maildir_home'
+EOF
+    m systemctl disable dovecot ||:
+    m systemctl stop dovecot ||:
+    ;;
+esac # end $HOSTNAME != $MAIL_HOST
 
 # * spool dir setup
 
@@ -867,12 +916,12 @@ fi
 # i have the spool directory be common to distro multi-boot, so
 # we need the uid to be the same. 608 cuz it's kind of in the middle
 # of the free system uids.
-IFS=:; read _ _ uid _ < <(getent passwd Debian-exim ||:) ||:; unset IFS
-IFS=:; read _ _ gid _ < <(getent group Debian-exim ||:) ||:; unset IFS
+IFS=:; read -r _ _ uid _ < <(getent passwd Debian-exim ||:) ||:; unset IFS
+IFS=:; read -r _ _ gid _ < <(getent group Debian-exim ||:) ||:; unset IFS
 if [[ ! $uid ]]; then
   # from /var/lib/dpkg/info/exim4-base.postinst, plus uid and gid options
   m adduser --uid 608 --system --group --quiet --home /var/spool/exim4 \
-         --no-create-home --disabled-login --force-badname Debian-exim
+    --no-create-home --disabled-login --force-badname Debian-exim
 elif [[ $uid != 608 ]]; then
   m systemctl stop exim4 ||:
   m usermod -u 608 Debian-exim
@@ -895,56 +944,51 @@ fi
 
 
 # * mail monitoring / testing
-if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
-  fname=send-test-forward
-  f=/usr/local/bin/$fname
-  cat >$f <<'EOFOUTER'
-#!/bin/bash
-/usr/sbin/exim -t <<EOF
-From: ian@iankelling.org
-To: iank@posteo.de
-Subject: primary_test $(date +%s) $(date +%Y-%m-%dT%H:%M:%S%z)
-
-eom
-EOF
-EOFOUTER
-  m chmod +x $f
 
-  cat >/etc/cron.d/mailtest <<EOF
+case $HOSTNAME in
+  $MAIL_HOST|l2)
+    # note: cronjob "ian" also does some important monitoring
+    cat >/etc/cron.d/mailtest <<EOF
 SHELL=/bin/bash
 PATH=/usr/bin:/bin:/usr/local/bin
-# running as user just because no need to run as root
-*/10 * * * *   $u $fname |& log-once send-test-forward
-*/10 * * * *   $u mailtest-check |& log-once -1 mailtest-check
+*/5 * * * *   $u send-test-forward |& log-once send-test-forward
 */10 * * * *   root chmod -R g+rw /m/md/bounces |& log-once -1 bounces-chmod
 EOF
-  m cp /a/bin/distro-setup/filesystem/usr/local/bin/mailtest-check /usr/local/bin
-else
-  rm -fv /etc/cron.d/mailtest
-fi
+    ;;&
+  $MAIL_HOST)
+    test_from=ian@iankelling.org
+    test_to=iank@posteo.de
+
+    cat >>/etc/cron.d/mailtest <<EOF
+*/5 * * * *   $u mailtest-check |& log-once -1 mailtest-check
+2   * * * *   $u check-remote-mailqs |& log-once check-remote-mailqs
+EOF
+    m sudo rsync -ahhi --chown=root:root --chmod=0755 \
+      /b/ds/mailtest-check /b/ds/check-remote-mailqs /usr/local/bin/
+    ;;&
+  l2)
+    test_from=iank@l2.b8.nz
+    test_to=testignore@iankelling.org
+    ;;&
+  $MAIL_HOST|l2)
+    cat >/usr/local/bin/send-test-forward <<EOFOUTER
+#!/bin/bash
+/usr/sbin/exim -t <<EOF
+From: $test_from
+To: $test_to
+Subject: primary_test \$(date +%s) \$(date +%Y-%m-%dT%H:%M:%S%z)
 
+eom
+EOF
+EOFOUTER
+    m chmod +x /usr/local/bin/send-test-forward
+    ;;
+  *)
+    rm -fv /etc/cron.d/mailtest
+    ;;
+esac
 
-# * Radicale
 
-# MAIL_HOST also does radicale, and easier to start and stop it here
-# for when MAIL_HOST changes, so radicale gets the synced files and
-# does not stop us from remounting /o.
-if dpkg -s radicale &>/dev/null; then
-  if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
-    m systemctl restart radicale
-    m systemctl enable radicale
-    if [[ -e /etc/logrotate.d/radicale.disabled ]]; then
-      m mv /etc/logrotate.d/radicale{.disabled,}
-    fi
-  else
-    m systemctl stop radicale
-    m systemctl disable radicale
-    # weekly logrotate tries to restart radicale even if it's a disabled service in flidas.
-    if [[ -e /etc/logrotate.d/radicale ]]; then
-      m mv /etc/logrotate.d/radicale{,.disabled}
-    fi
-  fi
-fi
 
 # * misc
 m sudo -u $u ln -sf -T /m/.mu /home/$u/.mu