Skip to content

Commit

Permalink
Rewrite MTA Setup
Browse files Browse the repository at this point in the history
Signed-off-by: Ethan Dye <mrtops03@gmail.com>
  • Loading branch information
ecdye committed Jun 1, 2020
1 parent cd48fd4 commit d53a15e
Showing 1 changed file with 52 additions and 32 deletions.
84 changes: 52 additions & 32 deletions functions/packages.bash
Original file line number Diff line number Diff line change
Expand Up @@ -58,49 +58,69 @@ firemotd_setup() {
if cond_redirect FireMotD -S; then echo "OK"; else echo "FAILED"; fi
}

exim_setup() {
echo -n "$(timestamp) [openHABian] Installing MTA required packages... "
if cond_redirect apt-get install --yes exim4 dnsutils mailutils; then
echo "OK"
create_mta_config
else
echo "FAILED"
fi
}

create_mta_config() {
TMP="$(mktemp /tmp/openhabian.XXXXX)"
HOSTNAME=$(/bin/hostname)
INTERFACES="$(/usr/bin/dig +short "$HOSTNAME" | /usr/bin/tr '\n' ';');127.0.0.1;::1"
EXIM_CLIENTCONFIG=/etc/exim4/passwd.client

introtext1="We will guide you through the install of exim4 as the mail transfer agent on your system and configure it to relay mails through a public service such as Google gmail.\\nPlease
enter the data shown in the next window when being asked for. You will be able to repeat the whole installation if required by selecting the openHABian menu for MTA again."
introtext2="Mail server type: mail sent by smarthost; received via SMTP or fetchmail\\nSystem mail name: FQDN (your full hostname including the domain part)\\nIPs should be allowed by the se
rver: $INTERFACES\\nOther destinations for which mail is accepted: $HOSTNAME\\nMachines to relay mail for: Leave empty\\nIP address or host name of outgoing smarthost: smtp.gmail.com::587\\nHide
local mail name in outgoing mail: No\\nKeep number of DNS-queries minimal: No\\nDelivery method: Select: Maildir format in home directory\\nMinimize number of DNS queries: No\\nSplit configurat
ion into small files: Yes\\n"
local hostName
local interfaces
local temp
local eximConfig
local smartHost
local relayUser
local relayPass
local introtext1
local introtext2

hostName="$(hostname)"
interfaces="$(dig +short "$hostName" | tr '\n' ';');127.0.0.1;::1"
temp="$(mktemp /tmp/openhabian.XXXXX)"
eximConfig="/etc/exim4/passwd.client"
introtext1="We will guide you through the install of exim4 as the mail transfer agent on your system and configure it to relay mails through a public service such as Google gmail.\\n\\nPlease enter the data shown in the next window when being asked for. You will be able to repeat the whole installation if required by selecting the openHABian menu for MTA again."
introtext2="Mail server type: mail sent by smarthost (received via SMTP or fetchmail)\\n\\nSystem mail name: FQDN (your full hostname including the domain part)\\n\\nIPs that should be allowed by the server: $interfaces\\n\\nOther destinations for which mail is accepted: $hostName\\n\\nMachines to relay mail for: Leave empty\\n\\nIP address or host name of outgoing smarthost: smtp.gmail.com::587\\n\\nHide local mail name in outgoing mail: No\\n\\nKeep number of DNS-queries minimal: No\\n\\nDelivery method: Select: Maildir format in home directory\\n\\nMinimize number of DNS queries: No\\n\\nSplit configuration into small files: Yes"

if [ -n "$INTERACTIVE" ]; then
if ! (whiptail --title "Mail Transfer Agent installation" --yes-button "Start" --no-button "don't change MTA" --yesno "$introtext1" 12 78); then echo "CANCELED"; return 0; fi
if ! (whiptail --title "Mail Transfer Agent installation" --msgbox "$introtext2" 18 78); then echo "CANCELED"; return 0; fi
if ! (whiptail --title "Mail Transfer Agent installation" --yes-button "Begin" --no-button "Cancel" --yesno "$introtext1" 13 80); then echo "CANCELED"; return 0; fi
if ! (whiptail --title "Mail Transfer Agent installation" --yes-button "Continue" --no-button "Cancel" --yesno "$introtext2" 27 80); then echo "CANCELED"; return 0; fi
else
echo "Ignoring MTA config creation in unattended install mode."
echo "$(timestamp) [openHABian] Ignoring MTA config creation in unattended install mode."
return 0
fi

dpkg-reconfigure exim4-config
if ! SMARTHOST=$(whiptail --title "Enter public mail service smarthost to relay your mails to" --inputbox "\\nEnter the list of smarthost(s) to use your account for" 9 78 "*.google.com" 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi
if ! RELAYUSER=$(whiptail --title "Enter your public service mail user" --inputbox "\\nEnter the mail username of the public service to relay all outgoing mail to ${SMARTHOST}" 9 78 "yourname@gmail.com" 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi
if ! RELAYPASS=$(whiptail --title "Enter your public service mail password" --passwordbox "\\nEnter the password used to relay mail as ${RELAYUSER}@${SMARTHOST}" 9 78 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi

grep '^#' "${EXIM_CLIENTCONFIG}" > "$TMP"
echo "${SMARTHOST}:${RELAYUSER}:${RELAYPASS}" >> "$TMP"
cp "$TMP" "${EXIM_CLIENTCONFIG}"
/bin/chmod o-rwx "${EXIM_CLIENTCONFIG}"
{
echo "openhab: ${RELAYUSER}";echo "openhabian: ${RELAYUSER}";
echo "root: ${RELAYUSER}";echo "backup: ${RELAYUSER}";
} >> /etc/email-addresses
rm -f "$TMP"
}
echo "$(timestamp) [openHABian] Reconfiguring exim4-config... "
if cond_redirect dpkg-reconfigure exim4-config; then echo "OK"; else echo "FAILED"; return 1; fi

exim_setup() {
if apt-get install --yes exim4 dnsutils mailutils &>/dev/null; then
if ! smartHost=$(whiptail --title "Enter public mail service smarthost to relay your mails to" --inputbox "\\nEnter the list of smarthost(s) to use your account for" 9 80 "*.google.com" 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi
if ! relayUser=$(whiptail --title "Enter your public service mail user" --inputbox "\\nEnter the mail username of the public service to relay all outgoing mail to $smartHost" 9 80 "yourname@gmail.com" 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi
if ! relayPass=$(whiptail --title "Enter your public service mail password" --passwordbox "\\nEnter the password used to relay mail as ${relayUser}@${smartHost}" 9 80 3>&1 1>&2 2>&3); then echo "CANCELED"; return 0; fi

echo "$(timestamp) [openHABian] Creating MTA config... "
if {
grep '^#' "$eximConfig" > "$temp"
echo "${smartHost}:${relayUser}:${relayPass}" >> "$temp"
cp "$temp" "$eximConfig"
chmod o-rwx "$eximConfig"
}; then
echo "OK"
create_mta_config
else
echo "FAILED"
rm -f "$temp"
return 1
fi
rm -f "$temp"

echo "$(timestamp) [openHABian] Adding to $relayUser email to system accounts... "
if {
echo "openhab: $relayUser"; echo "openhabian: $relayUser"
echo "root: $relayUser"; echo "backup: $relayUser"
} >> /etc/email-addresses; then echo "OK"; else echo "FAILED"; return 1; fi
}

etckeeper_setup() {
Expand Down

0 comments on commit d53a15e

Please sign in to comment.