fixup mail route going down, and switch mail with no internet
[distro-setup] / mail-setup
index c0035c6b0d7dad89f08ec1036793616a8951530b..a0bba6db2b78082d78f54ceffc983b53f482e0b2 100755 (executable)
@@ -1,4 +1,6 @@
 #!/bin/bash
+set -x
+
 # Copyright (C) 2016 Ian Kelling
 
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,6 +27,15 @@ Setup exim4 / postfix / dovecot
 
 The minimal assumption we have is that /etc/mailpass exists
 
+
+I've had problems with postfix on debian:
+on stretch, a startup ordering issue caused all mail to fail.
+postfix changed defaults to only use ipv6 dns, causing all my mail to fail.
+I haven't gotten around to getting a non-debian exim
+setup.
+
+
+
 -h|--help  Print help and exit.
 EOF
     exit $1
@@ -55,7 +66,7 @@ u=$SUDO_USER
 # 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
-# echo "mail.iankelling.org:$user:$(<$f)" >> /p/c/machine_specific/$user/filesystem/etc/mailpass
+# echo "mail.iankelling.org $user $(<$f)" >> /p/c/machine_specific/$user/filesystem/etc/mailpass
 # # then run this script, or part of it which uses /etc/mailpass
 
 # # dovecot password, i just need 1 as I\'m the only user
@@ -108,13 +119,14 @@ u=$SUDO_USER
 # # host -t txt _dmarc.gmail.com
 # # yahoo: p=reject, hotmail: p=none, gmail: p=none, fastmail none for legacy reasons
 # # there were articles claiming gmail would be  changing
-# # to p=reject, in early 2017, which didn't happen. I see no sources on them. It's
+# # to p=reject, in early 2017, which didn\'t happen. I see no sources on them. It\'s
 # # expected to cause problems
 # # with a few old mailing lists, copying theirs for now.
 #
 # echo "dmarc dns, name: _dmarc value: v=DMARC1; p=none; rua=mailto:mailauth-reports@$domain"
 
 # # 2017-02 spf policies:
+# # host -t txt lists.fedoraproject.org
 # # google ~all, hotmail -all, yahoo: ?all, fastmail ?all
 # # i include fastmail\'s settings, per their instructions,
 # # and follow their policy. In mail in a box, or similar instructions,
@@ -253,7 +265,7 @@ smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
 inet_protocols = ipv4
 EOF
     # msg_size_limit: I ran into a log file not sending cuz of size. double from 10 to 20 meg limit
-    # inet_protocols: without this, I've had postfix try an ipv6 lookup then gives
+    # inet_protocols: without this, I\'ve had postfix try an ipv6 lookup then gives
     # up and fail forever. snippet from syslog: type=AAAA: Host not found, try again
 
 
@@ -278,34 +290,26 @@ else # begin exim. has debian specific stuff for now
     fi
 
     if [[ -e /p/c/filesystem ]]; then
-        /a/exe/vpn-mk-client-cert -b mail -n mail li
+        # allow failure of these commands when our internet is down, they are likely not needed,
+        # we check that a valid cert is there already.
+        # to put the hostname in the known hosts
+        if ! ssh -o StrictHostKeyChecking=no root@li.iankelling.org :; then
+            openssl x509 -checkend $(( 60 * 60 * 24 * 3 )) -noout -in /etc/openvpn/mail.crt
+        else
+            # note, man openvpn implies we could just call mail-route on vpn startup/shutdown with
+            # systemd, buuut it can remake the tun device unexpectedly, i got this in the log
+            # after my internet was down for a bit:
+            # NOTE: Pulled options changed on restart, will need to close and reopen TUN/TAP device.
+            /a/exe/vpn-mk-client-cert -b mail -n mail -s /b/ds/mail-route li.iankelling.org
+        fi
     fi
 
-    cat >/etc/systemd/system/mailroute.service <<EOF
-[Unit]
-# this unit is configured to start and stop whenever $vpn_ser@mail.service
-# does
-Description=Routing for email vpn
-After=network.target
-BindsTo=$vpn_ser@mail.service
-After=$vpn_ser@mail.service
-
-[Service]
-Type=oneshot
-ExecStart=/a/bin/distro-setup/mail-route start
-ExecStop=/a/bin/distro-setup/mail-route stop
-RemainAfterExit=yes
-
-[Install]
-RequiredBy=$vpn_ser@mail.service
-EOF
-
     cat >/etc/systemd/system/offlineimapsync.timer <<'EOF'
 [Unit]
-Description=Run offlineimap-sync once every 5 mins
+Description=Run offlineimap-sync once every min
 
 [Timer]
-OnCalendar=*:0/5
+OnCalendar=*:0/1
 
 [Install]
 WantedBy=timers.target
@@ -322,7 +326,6 @@ Type=oneshot
 ExecStart=/a/bin/log-quiet/sysd-mail-once offlineimap-sync /a/bin/distro-setup/offlineimap-sync
 EOF
     systemctl daemon-reload
-    systemctl enable mailroute
 
     # wording of question from dpkg-reconfigure exim4-config
     # 1. internet site; mail is sent and received directly using SMTP
@@ -341,6 +344,12 @@ EOF
 
     # most of these settings get translated into settings
     # in /etc/exim4/update-exim4.conf.conf
+    # how /etc/exim4/update-exim4.conf.conf translates into actual exim settings is
+    # documented in man update-exim4.conf, which outputs to the config that
+    # exim actually reads. except the man page is not perfect, for example,
+    # it doesn't document that it sets
+    # DCconfig_${dc_eximconfig_configtype}" "1"
+    # which is a line from update-exim4.conf, which is a relatively short bash script.
     # mailname setting sets /etc/mailname
 
     debconf-set-selections <<EOF
@@ -352,6 +361,70 @@ EOF
     mkdir -p $exim_main_dir
 
 
+
+    #### begin mail cert setup ###
+    f=/usr/local/bin/mail-cert-cron
+    cat >$f <<'EOF'
+set -eE -o pipefail
+trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
+
+[[ $EUID == 0 ]] || exec sudo "$BASH_SOURCE" "$@"
+
+f=/a/bin/bash_unpublished/source-semi-priv
+if [[ -e $f ]]; then
+    source $f
+fi
+if [[ $HOSTNAME == $MAIL_HOST ]]; then
+    local_mx=mail.iankelling.org
+    rsync_common="rsync -ogtL --chown=root:Debian-exim --chmod=640 root@li:/etc/letsencrypt/live/$local_mx/"
+    ${rsync_common}fullchain.pem /etc/exim4/exim.crt
+    ret=$?
+    ${rsync_common}privkey.pem /etc/exim4/exim.key
+    new_ret=$?
+    if [[ $ret != $new_ret ]]; then
+       echo "$0: error: differing rsync returns, $ret, $new_ret"
+       exit 1
+    fi
+fi
+if [[ $new_ret != 0 ]]; then
+    if ! openssl x509 -checkend $(( 60 * 60 * 24 * 3 )) -noout -in /etc/exim4/exim.crt; then
+        echo "$0: error!: cert rsync failed and it will expire in less than 3 days"
+        exit 1
+    fi
+fi
+exit 0
+EOF
+    chmod 755 $f
+
+    cat >/etc/systemd/system/mailcert.service <<'EOF'
+[Unit]
+Description=Mail cert rsync
+After=multi-user.target
+
+[Service]
+Type=oneshot
+ExecStart=/a/bin/log-quiet/sysd-mail-once mailcert /usr/local/bin/mail-cert-cron
+EOF
+
+    cat >/etc/systemd/system/mailcert.timer <<'EOF'
+[Unit]
+Description=Run mail-cert once a day
+
+[Timer]
+OnCalendar=daily
+
+[Install]
+WantedBy=timers.target
+EOF
+    systemctl daemon-reload
+    systemctl start mailcert
+    systemctl restart mailcert.timer
+    systemctl enable mailcert.timer
+
+    ##### end mailcert setup #####
+
+
+
     if [[ $HOSTNAME == $MAIL_HOST ]]; then
 
         debconf-set-selections <<EOF
@@ -467,6 +540,9 @@ EOF
         # smarthost config type, not sure. all other settings
         # would be unused in that config type.
         cat >$exim_main_dir/000_localmacros <<EOF
+# i don't have ipv6 setup for my tunnel yet.
+disable_ipv6 = true
+
 MAIN_TLS_ENABLE = true
 
 DKIM_CANON = relaxed
@@ -547,12 +623,12 @@ EOF
 /^\s*mail_location\s*=/d
 EOF
 
-        cat >/etc/dovecot/conf.d/20-lmtp.conf <<'EOF'
+        cat >/etc/dovecot/conf.d/20-lmtp.conf <<EOF
 protocol lmtp {
 #per https://wiki2.dovecot.org/Pigeonhole/Sieve/Configuration
-  mail_plugins = $mail_plugins sieve
+  mail_plugins = \$mail_plugins sieve
 # default was
-  #mail_plugins = $mail_plugins
+  #mail_plugins = \$mail_plugins
 
 # For a normal setup with exim, we need something like this, which
 # removes the domain part
@@ -568,10 +644,10 @@ protocol lmtp {
 # A normal way to do this, which I did at first is to have
 # a router in exim almost at the end, eg 950,
 #local_catchall:
-#  debug_print = "R: catchall for $local_part@$domain"
+#  debug_print = "R: catchall for \$local_part@\$domain"
 #  driver = redirect
 #  domains = +local_domains
-#  data = ian
+#  data = $u
 # based on
 # http://blog.alteholz.eu/2015/04/exim4-and-catchall-email-address/
 # with superflous options removed.
@@ -579,7 +655,7 @@ protocol lmtp {
 # which makes filtering into mailboxes a little less robust or more complicated,
 # so I've done it this way instead. it also requires
 # modifying the local router in exim.
-  auth_username_format = ian
+  auth_username_format = $u
 }
 
 EOF
@@ -646,6 +722,25 @@ EOF
         rm -f /etc/exim4/update-exim4.conf.conf
         dpkg-reconfigure -u -fnoninteractive exim4-config
     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
+    if [[ ! $uid ]]; then
+        # from /var/lib/dpkg/info/exim4-base.postinst, plus uid and gid options
+       adduser --uid 608 --gid 608 --system --group --quiet --home /var/spool/exim4 \
+               --no-create-home --disabled-login --force-badname Debian-exim
+    elif [[ $uid != 608 ]]; then
+        systemctl stop exim4 ||:
+        usermod -u 608 Debian-exim
+        groupmod -g 608 Debian-exim
+        usermod -g 608 Debian-exim
+        find / /nocow -xdev -uid $uid -exec chown -h 608 {} +
+        find / /nocow -xdev -gid $gid -exec chgrp -h 608 {} +
+    fi
+
     # light version of exim does not have sasl auth support.
     apt-get -y install --purge --auto-remove exim4-daemon-heavy spamassassin
 
@@ -672,7 +767,7 @@ Type=oneshot
 ExecStart=/a/bin/distro-setup/spamd-dns-fix
 EOF
     # 2017-09, debian closed the bug on this saying upstream had fixed it.
-    # remove this when i'm using the newer package, ie, debian 10, or maybe
+    # remove this when i\'m using the newer package, ie, debian 10, or maybe
     # ubuntu 18.04.
     cat >/etc/systemd/system/spamddnsfix.timer <<'EOF'
 [Unit]
@@ -695,53 +790,6 @@ EOF
     #####   end spamassassin config
 
 
-    f=/usr/local/bin/mail-cert-cron
-    cat >$f <<'EOF'
-set -eE -o pipefail
-trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
-
-[[ $EUID == 0 ]] || exec sudo "$BASH_SOURCE" "$@"
-
-f=/a/bin/bash_unpublished/source-semi-priv
-if [[ -e $f ]]; then
-    source $f
-fi
-if [[ $HOSTNAME == $MAIL_HOST ]]; then
-    local_mx=mail.iankelling.org
-    rsync_common="rsync -ogtL --chown=root:Debian-exim --chmod=640 root@li:/etc/letsencrypt/live/$local_mx/"
-    ${rsync_common}fullchain.pem /etc/exim4/exim.crt
-    ${rsync_common}privkey.pem /etc/exim4/exim.key
-fi
-EOF
-    chmod 755 $f
-
-    cat >/etc/systemd/system/mailcert.service <<'EOF'
-[Unit]
-Description=Mail cert rsync
-After=multi-user.target
-
-[Service]
-Type=oneshot
-ExecStart=/a/bin/log-quiet/sysd-mail-once mailcert /usr/local/bin/mail-cert-cron
-EOF
-
-    cat >/etc/systemd/system/mailcert.timer <<'EOF'
-[Unit]
-Description=Run mail-cert once a day
-
-[Timer]
-OnCalendar=daily
-
-[Install]
-WantedBy=timers.target
-EOF
-    systemctl daemon-reload
-    systemctl start mailcert
-    systemctl restart mailcert.timer
-    systemctl enable mailcert.timer
-
-
-
 
 
 
@@ -814,6 +862,28 @@ dovecot_lmtp:
         batch_max = 200
 EOF
 
+    cat >/etc/exim4/conf.d/router/190_exim4-config_fsfsmarthost <<'EOF'
+# smarthost for fsf mail
+# ian: copied from /etc/exim4/conf.d/router/200_exim4-config_primary, and added senders = and
+# replaced DCsmarthost with mail.fsf.org
+fsfsmarthost:
+  debug_print = "R: smarthost for $local_part@$domain"
+  driver = manualroute
+  domains = ! +local_domains
+  senders = *@fsf.org
+  transport = remote_smtp_smarthost
+  route_list = * mail.fsf.org byname
+  host_find_failed = ignore
+  same_domain_copy_routing = yes
+  no_more
+EOF
+
+    # https://blog.dhampir.no/content/make-exim4-on-debian-respect-forward-and-etcaliases-when-using-a-smarthost
+    # i only need .forwards, so just doing that one.
+    cd /etc/exim4/conf.d/router
+    b=userforward_higher_priority
+    # replace the router name so it is unique
+    sed -r s/^\\S+:/$b:/ 600_exim4-config_userforward >175_$b
 
     # begin setup passwd.client
     f=/etc/exim4/passwd.client
@@ -825,21 +895,18 @@ EOF
     done
     # end setup passwd.client
 
-    # https://blog.dhampir.no/content/make-exim4-on-debian-respect-forward-and-etcaliases-when-using-a-smarthost
-    # i only need .forwards, so just doing that one.
-    cd /etc/exim4/conf.d/router
-    b=userforward_higher_priority
-    # replace the router name so it is unique
-    sed -r s/^\\S+:/$b:/ 600_exim4-config_userforward >175_$b
+    # by default, only 10 days of logs are kept. increase that.
+    sed -ri 's/^(\s*rotate\s).*/\11000/' /etc/logrotate.d/exim4-base
+
     systemctl restart exim4
 
-        fi  # end if exim4
+        fi  #### end if exim4
 
          # /etc/alias setup is debian specific, and
          # exim config sets up an /etc/alias from root to the postmaster, which i
          # config to ian, as long as there exists an entry for root, or there was
          # no preexisting aliases file. based on the postinst file. postfix
-         # won't set up a root to $postmaster alias if it's already installed.
+         # won\'t set up a root to $postmaster alias if it\'s already installed.
          # Since postfix is not the greatest, just set it ourselves.
          if [[ $postmaster != root ]]; then
              sed -i --follow-symlinks -f - /etc/aliases <<EOF
@@ -849,39 +916,47 @@ EOF
              newaliases
          fi
 
-# put spool dir in directory that spans multiple distros.
-# based on http://www.postfix.org/qmgr.8.html and my notes in gnus
-#
-# todo: I'm suspicious of uids for Debian-exim being the same across
-# distros. It would be good to test this.
-dir=/nocow/$type
-sdir=/var/spool/$type
-# we only do this if our system has $dir
-if [[ -e $dir && $(readlink -f $sdir) != $dir ]]; then
-    systemctl stop $type
-    if [[ ! -e $dir && -d $sdir ]]; then
-        mv $sdir $dir
-    fi
-    /a/exe/lnf -T $dir $sdir
-fi
-
-systemctl restart $type
-systemctl enable $type
+         # put spool dir in directory that spans multiple distros.
+         # based on http://www.postfix.org/qmgr.8.html and my notes in gnus
+         #
+         # todo: I\'m suspicious of uids for Debian-exim being the same across
+         # distros. It would be good to test this.
+         dir=/nocow/$type
+         sdir=/var/spool/$type
+         # we only do this if our system has $dir
+         if [[ -e /nocow && $(readlink -f $sdir) != $dir ]]; then
+             systemctl stop $type
+             if [[ ! -e $dir && -d $sdir ]]; then
+                 mv $sdir $dir
+             fi
+             /a/exe/lnf -T $dir $sdir
+         fi
 
-# 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
-        systemctl restart radicale
-        systemctl enable radicale
-    else
-        systemctl stop radicale
-        systemctl disable radicale
-    fi
-fi
+         systemctl restart $type
+         systemctl enable $type
+
+         # 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
+                 systemctl restart radicale
+                 systemctl enable radicale
+                 if [[ -e /etc/logrotate.d/radicale.disabled ]]; then
+                     mv /etc/logrotate.d/radicale{.disabled,}
+                 fi
+             else
+                 systemctl stop radicale
+                 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
+            mv /etc/logrotate.d/radicale{,.disabled}
+        fi
+        fi
+        fi
+         exit 0
 
-# if I wanted the from address to be renamed and sent to a different address,
-# echo "sdx@localhost development@localhost" | sudo dd of=/etc/postfix/recipient_canonical
-# sudo postmap hash:/etc/postfix/recipient_canonical
-# sudo service postfix reload
+         # if I wanted the from address to be renamed and sent to a different address,
+         # echo "sdx@localhost development@localhost" | sudo dd of=/etc/postfix/recipient_canonical
+         # sudo postmap hash:/etc/postfix/recipient_canonical
+         # sudo service postfix reload