From 83d8b23b6a489affd36458245e550e18aca41b6f Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Sun, 16 Dec 2018 20:03:51 +0100
Subject: [PATCH] Improve mail server setup.

---
 .../mail/etc/systemd/system/pingmail.service  |  7 +++
 .../system/timers.target.wants/pingmail.timer |  8 +++
 all_new_2018/setup_scripts/setup_mail.sh      | 50 +++++++++----------
 3 files changed, 39 insertions(+), 26 deletions(-)
 create mode 100644 all_new_2018/linkable_etc_files/mail/etc/systemd/system/pingmail.service
 create mode 100644 all_new_2018/linkable_etc_files/mail/etc/systemd/system/timers.target.wants/pingmail.timer

diff --git a/all_new_2018/linkable_etc_files/mail/etc/systemd/system/pingmail.service b/all_new_2018/linkable_etc_files/mail/etc/systemd/system/pingmail.service
new file mode 100644
index 0000000..e332114
--- /dev/null
+++ b/all_new_2018/linkable_etc_files/mail/etc/systemd/system/pingmail.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Run pingmail check
+
+[Service]
+Type=oneshot
+User=plom
+ExecStart=/bin/sh -c '~/pingmail/pingmail check'
diff --git a/all_new_2018/linkable_etc_files/mail/etc/systemd/system/timers.target.wants/pingmail.timer b/all_new_2018/linkable_etc_files/mail/etc/systemd/system/timers.target.wants/pingmail.timer
new file mode 100644
index 0000000..dba0c9f
--- /dev/null
+++ b/all_new_2018/linkable_etc_files/mail/etc/systemd/system/timers.target.wants/pingmail.timer
@@ -0,0 +1,8 @@
+[Unit]
+Description=Run pingmail check once every hour
+
+[Timer]
+OnCalendar=*-*-* *:00:00
+
+[Install]
+WantedBy=timers.target
diff --git a/all_new_2018/setup_scripts/setup_mail.sh b/all_new_2018/setup_scripts/setup_mail.sh
index ab124b9..70a1342 100755
--- a/all_new_2018/setup_scripts/setup_mail.sh
+++ b/all_new_2018/setup_scripts/setup_mail.sh
@@ -1,6 +1,7 @@
 #/bin/sh
 set -e
 
+# Check we have the necessary arguments.
 if [ $# -lt 2 ]; then
     echo "Give arguments of mail domain and DKIM selector."
     echo "Also, if hosting mail for entire domain, give third argument 'domainwide'."
@@ -14,23 +15,19 @@ config_tree_prefix="${HOME}/config/all_new_2018"
 setup_scripts_dir="${config_tree_prefix}/setup_scripts"
 cd "${setup_scripts_dir}"
 
-# Set up DKIM key if necessary.
+# Set up DKIM key. Only keep opendkim-tools on system if pre-installed.
 mkdir -p /etc/dkimkeys/
-add_dkim_record=0
-if [ ! -f "/etc/dkimkeys/${dkim_selector}.private" ]; then
-    add_dkim_record=1
-    set +e
-    dpkg -s opendkim-tools &> /dev/null
-    preinstalled="$?"
-    set -e
-    if [ ! "${preinstalled}" -eq "0" ]; then
-        apt install -y opendkim-tools
-    fi
-    opendkim-genkey -s "${dkim_selector}"
-    mv "${dkim_selector}.private" /etc/dkimkeys/
-    if [ ! "${preinstalled}" -eq "0" ]; then
-        apt -y --purge autoremove opendkim-tools
-    fi
+set +e
+dpkg -s opendkim-tools &> /dev/null
+preinstalled="$?"
+set -e
+if [ ! "${preinstalled}" -eq "0" ]; then
+    apt install -y opendkim-tools
+fi
+opendkim-genkey -s "${dkim_selector}"
+mv "${dkim_selector}.private" /etc/dkimkeys/
+if [ ! "${preinstalled}" -eq "0" ]; then
+    apt -y --purge autoremove opendkim-tools
 fi
 
 # Link and adapt mail-server-specific /etc/ files.
@@ -66,8 +63,12 @@ echo "${mail_domain}" > /etc/mailname
 apt install -y -o Dpkg::Options::=--force-confold postfix dovecot-imapd dovecot-lmtpd dovecot-sieve opendkim
 cp "${config_tree_prefix}/user_files/dovecot.sieve" /home/plom/.dovecot.sieve
 chown plom:plom /home/plom/.dovecot.sieve
+
+# Pingmail setup.
+apt install -y mailutils
 cp "${config_tree_prefix}/user_files/pingmailrc" /home/plom/.pingmailrc
 chown plom:plom /home/plom/.pingmailrc
+su plom -c "cd && git clone https://plomlompom.com/repos/clone/pingmail.git"
 
 # In addition to our postfix server receiving mails, we funnel mails from a
 # POP3 account into dovecot via fetchmail. It might make sense to adapt the
@@ -76,18 +77,15 @@ chown plom:plom /home/plom/.pingmailrc
 cp "${config_tree_prefix}/user_files/fetchmailrc" /home/plom/.fetchmailrc
 chown plom:plom /home/plom/.fetchmailrc
 chmod 0700 /home/plom/.fetchmailrc
-set +e
-apt install -y fetchmail
+
+# Pingmail and fetchmail have some systemd timers waiting. To let systemd
+# know about them, do this.
 systemctl daemon-reload
-systemctl start fetchmail.timer
-set -e
 
 # Final advice to user.
 echo "TODO: Ensure MX entry for your system in your DNS configuration."
 echo "TODO: Ensure a proper SPF entry for this system in your DNS configuration; something like 'v=spf1 mx -all' mapped to your host."
-if [ "${add_dkim_record}" -eq "1" ]; then
-    echo "TODO: Add the following DKIM entry to your DNS configuration (possibly with slightly changed host entry – if your mail domain includes a subdomain, append that with a dot):"
-    cat "${dkim_selector}.txt"
-fi
-echo "TODO: passwd plom"
-echo "TODO: adapt /home/plom/.dovecot.sieve /home/plom/.fetchmailrc /home/plom/.pingmailrc"
+echo "TODO: passwd plom for IMAPS login"
+echo "TODO: adapt /home/plom/.dovecot.sieve /home/plom/.fetchmailrc /home/plom/.pingmailrc, then run: systemctl start pingmail.timer && systemctl start fetchmail.timer"
+echo "TODO: Add the following DKIM entry to your DNS configuration (possibly with slightly changed host entry – if your mail domain includes a subdomain, append that with a dot):"
+cat "${dkim_selector}.txt"
-- 
2.30.2