From 84adebc17ba162b83d40cb5f55045f302cee4549 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@robotics.uk.to>
Date: Sun, 23 Oct 2016 14:13:38 +0100
Subject: [PATCH] Change exim relay when changing network

---
 src/freedombone-base-email   | 21 +++++++++++++++++----
 src/freedombone-controlpanel |  3 +++
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/freedombone-base-email b/src/freedombone-base-email
index 558bf97c3..19ff1d11a 100755
--- a/src/freedombone-base-email
+++ b/src/freedombone-base-email
@@ -804,7 +804,9 @@ function install_email {
     echo "dc_readhost=''" >> /etc/exim4/update-exim4.conf.conf
     echo "dc_relay_domains=''" >> /etc/exim4/update-exim4.conf.conf
     echo "dc_minimaldns='false'" >> /etc/exim4/update-exim4.conf.conf
-    RELAY_NETS='192.168.1.0/24'
+    IPv4_address=$(get_ipv4_address)
+    IPv4_address_base=$(echo "$IPv4_address" | awk -F '.' '{print $1"."$2"."$3}')
+    RELAY_NETS="${IPv4_address_base}.0/24"
     if [ $LOCAL_NETWORK_STATIC_IP_ADDRESS ]; then
         RELAY_NETS=$(echo $LOCAL_NETWORK_STATIC_IP_ADDRESS | awk -F '.' '{print $1 "." $2 "." $3 ".0/24"}')
     fi
@@ -832,9 +834,9 @@ function install_email {
 
     sed -i '/login_saslauthd_server/,/.endif/ s/# *//' /etc/exim4/exim4.conf.template
     if ! grep -q "MAIN_TLS_ENABLE = true" /etc/exim4/exim4.conf.template; then
-        sed -i "/.ifdef MAIN_HARDCODE_PRIMARY_HOSTNAME/i\MAIN_HARDCODE_PRIMARY_HOSTNAME = $DEFAULT_DOMAIN_NAME\nMAIN_TLS_ENABLE = true" /etc/exim4/exim4.conf.template
+        sed -i "/.ifdef MAIN_HARDCODE_PRIMARY_HOSTNAME/i\MAIN_HARDCODE_PRIMARY_HOSTNAME =\nMAIN_TLS_ENABLE = true" /etc/exim4/exim4.conf.template
     else
-        sed -i "s|MAIN_HARDCODE_PRIMARY_HOSTNAME =.*|MAIN_HARDCODE_PRIMARY_HOSTNAME = $DEFAULT_DOMAIN_NAME|g" /etc/exim4/exim4.conf.template
+        sed -i "s|MAIN_HARDCODE_PRIMARY_HOSTNAME =.*|MAIN_HARDCODE_PRIMARY_HOSTNAME =|g" /etc/exim4/exim4.conf.template
     fi
     sed -i "s|SMTPLISTENEROPTIONS=''|SMTPLISTENEROPTIONS='-oX 465:25:587 -oP /var/run/exim4/exim.pid'|g" /etc/default/exim4
     if ! grep -q "tls_on_connect_ports=465" /etc/exim4/exim4.conf.template; then
@@ -847,6 +849,9 @@ function install_email {
     adduser $MY_USERNAME sasl
     addgroup Debian-exim sasl
     systemctl restart exim4
+    if [ ! -d /etc/skel/log ]; then
+        mkdir -m 700 /etc/skel/log
+    fi
     if [ ! -d /etc/skel/Maildir ]; then
         mkdir -m 700 /etc/skel/.mutt
         mkdir -m 700 /etc/skel/Maildir
@@ -897,6 +902,14 @@ function install_email {
     mark_completed $FUNCNAME
 }
 
+function email_change_relay {
+    curr_ip_address="$1"
+    email_relay_base=$(echo "$curr_ip_address" | awk -F '.' '{print $1"."$2"."$3}')
+    RELAY_NETS="${email_relay_base}.0/24"
+    sed -i "s|dc_relay_nets=.*|dc_relay_nets='$RELAY_NETS'|g" /etc/exim4/update-exim4.conf.conf
+    dpkg-reconfigure --frontend noninteractive exim4-config
+}
+
 function create_procmail {
     if [ ! -d /etc/exim4 ]; then
         return
@@ -1094,7 +1107,7 @@ function spam_filtering {
     # this must be accessible by root
     chown -R $MY_USERNAME:root /home/$MY_USERNAME/.spamassassin
 
-    systemctl restart spamassassin
+    systemctl start spamassassin
     systemctl restart exim4
     systemctl restart cron
 
diff --git a/src/freedombone-controlpanel b/src/freedombone-controlpanel
index 024382935..965b6f2fb 100755
--- a/src/freedombone-controlpanel
+++ b/src/freedombone-controlpanel
@@ -1344,6 +1344,9 @@ Enter a static local IP address for this system.\n\nIt will typically be ${IPv4_
             write_config_param "NETWORK_IS_STATIC" "1"
             write_config_param "STATIC_IP_ADDRESS" "$NEW_STATIC_IP"
             write_config_param "ROUTER_IP_ADDRESS" "$NEW_STATIC_GATEWAY"
+
+            email_change_relay "$NEW_STATIC_IP"
+
             if [[ $(config_param_exists "WIFI_INTERFACE") == "0" ]]; then
                 # wired network
                 echo '# This file describes the network interfaces available on your system' > /etc/network/interfaces
-- 
GitLab