ipv6 tunnel
[distro-setup] / mail-setup
index 31841b9ef71f6293a95e0784411fbbef44eebd48..11f8dffe089dc81613f83513cbcd5e2562df351d 100755 (executable)
@@ -15,12 +15,13 @@ set -x
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# todo: make quick backups of maildir, or deliver to multiple hosts.
+# TODO: copy dkim keys from within this file. its now done in conflink.
+# TODO: fix dkim key to b chmod 640, group Debian-exim
 
 set -eE -o pipefail
 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 
-[[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
+[[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
 if [[ ! $SUDO_USER ]]; then
   echo "$0: error: requires running as nonroot or sudo"
   exit 1
@@ -161,7 +162,7 @@ pi() { # package install
   if [[ ! -r $f ]] || (( $(( $(date +%s) - $(stat -c %Y $f ) )) > 60*60*12 )); then
     apt-get update
   fi
-  apt-get -y install --purge --auto-remove "$@"
+  DEBIAN_FRONTEND=noninteractive apt-get -y install --purge --auto-remove "$@"
 }
 
 postmaster=alerts
@@ -176,13 +177,16 @@ forward=$u@$mxhost
 
 smarthost="$mxhost::$mxport" # exim
 
+# light version of exim does not have sasl auth support.
+pi exim4-daemon-heavy spamassassin spf-tools-perl
+
 # trisquel 8 = openvpn, debian stretch = openvpn-client
 vpn_ser=openvpn-client
 if [[ ! -e /lib/systemd/system/openvpn-client@.service ]]; then
   vpn_ser=openvpn
 fi
 
-if [[ $HOSTNAME == $MAIL_HOST ]]; then
+if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
   # afaik, these will get ignored because they are routing to my own
   # machine, but rm them is safer
   rm -f $(eval echo ~$u)/.forward /root/.forward
@@ -193,7 +197,8 @@ else
 fi
 
 
-pi openvpn
+# light version of exim does not have sasl auth support.
+pi exim4-daemon-heavy spamassassin spf-tools-perl openvpn dnsmasq
 
 if [[ -e /p/c/filesystem ]]; then
   # allow failure of these commands when our internet is down, they are likely not needed,
@@ -317,6 +322,7 @@ EOF
 
 cat >/etc/exim4/host_local_deny_exceptions <<'EOF'
 mail.fsf.org
+*.posteo.de
 EOF
 
 cat >/etc/exim4/conf.d/router/190_exim4-config_fsfsmarthost <<'EOF'
@@ -339,16 +345,17 @@ EOF
 #### begin mail cert setup ###
 f=/usr/local/bin/mail-cert-cron
 cat >$f <<'EOF'
+#!/bin/bash
 set -eE -o pipefail
 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 
-[[ $EUID == 0 ]] || exec sudo "$BASH_SOURCE" "$@"
+[[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
 
 f=/a/bin/bash_unpublished/source-state
 if [[ -e $f ]]; then
     source $f
 fi
-if [[ $HOSTNAME == $MAIL_HOST ]]; then
+if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
     local_mx=mail.iankelling.org
     rsync_common="rsync -ogtL --chown=root:Debian-exim --chmod=640 root@li.iankelling.org:/etc/letsencrypt/live/$local_mx/"
     ${rsync_common}fullchain.pem /etc/exim4/exim.crt
@@ -409,7 +416,7 @@ dc_mailname_in_oh='true'
 EOF
 
 
-if [[ $HOSTNAME == $MAIL_HOST ]]; then
+if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
 
   # mail.iankelling.org so local imap clients can connect with tls and
   # when they happen to not be local.
@@ -470,8 +477,6 @@ 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
-# i don't have ipv6 setup for my vpn tunnel yet.
-disable_ipv6 = true
 
 MAIN_TLS_ENABLE = true
 
@@ -543,10 +548,14 @@ EOF
   cat >$f <<'EOF'
 #!/bin/bash
 cd /etc
-wget -nv -N https://publicsuffix.org/list/public_suffix_list.dat
+wget -q -N https://publicsuffix.org/list/public_suffix_list.dat
 EOF
   chmod 755 $f
 
+  sed -i --follow-symlinks -f - /etc/aliases <<EOF
+\$a root: $postmaster
+/^root:/d
+EOF
 
   ####### begin dovecot setup ########
   # based on a little google and package search, just the dovecot
@@ -561,6 +570,9 @@ EOF
   # 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/subdir_files/sieve/*sieve /a/c/subdir_files/sieve/*sieve; do
+    sudo -u $u /a/exe/lnf -T $f $(eval echo ~$u)/sieve/${f##*/}
+  done
   # 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
@@ -610,7 +622,7 @@ protocol lmtp {
 EOF
 
 
-  cat >/etc/dovecot/local.conf <<'EOF'
+  cat >/etc/dovecot/local.conf <<EOF
 # so I can use a different login that my shell login for mail.  this is
 # worth doing solely for the reason that if this login is compromised,
 # it won't also compromise my shell password.
@@ -629,8 +641,8 @@ ssl_prefer_server_ciphers = yes
 
 
 mail_location = maildir:/m/%u:LAYOUT=fs:INBOX=/m/%u/INBOX
-mail_uid = iank
-mail_gid = iank
+mail_uid = $u
+mail_gid = $u
 
 # for debugging info, uncomment these.
 # logs go to syslog and to /var/log/mail.log
@@ -653,6 +665,8 @@ RestartSec=1
 StartLimitInterval=0
 EOF
 
+
+
   systemctl enable mailclean.timer
   systemctl start mailclean.timer
   systemctl restart $vpn_ser@mail
@@ -682,14 +696,24 @@ EOF
   # it\'s not part of exim
   rm -f /etc/exim4/conf.d/main/000_localmacros
   cat >>/etc/exim4/update-exim4.conf.conf <<EOF
-dc_eximconfig_configtype='satellite'
+dc_eximconfig_configtype='smarthost'
 dc_smarthost='$smarthost'
-# i dunno if this would ever get used.
+# 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
 
+  # We set this to alerts 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:/d
+EOF
+
 fi # end $HOSTNAME != $MAIL_HOST
 
 systemctl reload exim4
@@ -713,10 +737,6 @@ elif [[ $uid != 608 ]]; then
 fi
 
 
-# light version of exim does not have sasl auth support.
-pi exim4-daemon-heavy spamassassin spf-tools-perl
-
-
 
 ##### begin spamassassin config
 systemctl enable spamassassin
@@ -807,24 +827,20 @@ systemctl restart exim4
 # Multiple user names need to be separated by spaces.
 # Root and postmaster mail recipient:
 
-if [[ $postmaster != root ]]; then
-  sed -i --follow-symlinks -f - /etc/aliases <<EOF
-\$a root: $postmaster
-/^root:/d
-EOF
-  newaliases
-fi
+# local mail that bounces goes to /Maildir or /root/Maildir
+dirs=(/m/md/bounces/{cur,tmp,new})
+mkdir -p ${dirs[@]}
+chown -R $u:Debian-exim /m/md/bounces
+chmod 775 ${dirs[@]}
+usermod -a -G Debian-exim $u
+for d in /Maildir /root/Maildir; do
+  if [[ ! -L $d ]]; then
+    rm -rf $d
+  fi
+  ln -sf -T /m/md/bounces $d
+done
 
-# local mail that bounces to alerts goes to /Maildir
-mkdir -p /m/md/INBOX
-chmod 770 /m/md/INBOX
-if id -g 1000 &>/dev/null; then
-  chgrp 1000 /m/md/INBOX
-  usermod -a -G 1000 Debian-exim
-else
-  chgrp Debian-exim /m/md/INBOX
-fi
-ln -s /m/md/INBOX /Maildir
+sudo -u $u ln -sf -T /m/.mu /home/$u/.mu
 
 # put spool dir in directory that spans multiple distros.
 # based on http://www.postfix.org/qmgr.8.html and my notes in gnus
@@ -865,10 +881,26 @@ systemctl restart exim4
 systemctl enable exim4
 
 
-if [[ $HOSTNAME == $MAIL_HOST ]]; then
-  cat >/etc/cron.d/mailtest <<'EOF'
-*/10 * * * *   iank echo body_test | mail -s "primary_test $(date +%s) $(date +%Y-%m-%dT%H:%M:%S%z)" iank@posteo.de
-2/10 * * * *   root /usr/local/bin/mailtest-check
+if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
+  f=/usr/local/bin/send-test-forward
+  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
+  chmod +x $f
+
+  cat >/etc/cron.d/mailtest <<EOF
+SHELL=/bin/bash
+# running as user just because no need to run as root
+*/10 * * * *   $u $f 2>&1 | /usr/local/bin/log-once send-test-forward
+*/10 * * * *   $u /usr/local/bin/mailtest-check 2>&1 | /usr/local/bin/log-once -1 mailtest-check
+*/10 * * * *   root chmod -R g+rw /m/md/bounces 2>&1 | /usr/local/bin/log-once -1 bounces-chmod
 EOF
   cp /a/bin/distro-setup/filesystem/usr/local/bin/mailtest-check /usr/local/bin
 else
@@ -879,7 +911,7 @@ fi
 # 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
+  if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
     systemctl restart radicale
     systemctl enable radicale
     if [[ -e /etc/logrotate.d/radicale.disabled ]]; then