2020-10-21 18:16:32 +02:00
|
|
|
#! /bin/bash
|
2019-08-07 02:24:56 +02:00
|
|
|
|
2021-01-16 10:16:05 +01:00
|
|
|
DMS_DEBUG="${DMS_DEBUG:=0}"
|
2021-09-13 04:09:01 -04:00
|
|
|
SCRIPT_NAME="$(basename "$0")" # This becomes the sourcing script name (Example: check-for-changes.sh)
|
|
|
|
LOCK_ID="$(uuid)" # Used inside of lock files to identify them and prevent removal by other instances of docker-mailserver
|
2020-09-05 16:19:12 +02:00
|
|
|
|
2021-06-08 03:20:20 +02:00
|
|
|
# ? --------------------------------------------- BIN HELPER
|
2021-01-16 10:16:05 +01:00
|
|
|
|
|
|
|
function errex
|
|
|
|
{
|
2021-02-23 20:03:01 +01:00
|
|
|
echo -e "Error :: ${*}\nAborting." >&2
|
2021-01-16 10:16:05 +01:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
function escape
|
|
|
|
{
|
|
|
|
echo "${1//./\\.}"
|
|
|
|
}
|
|
|
|
|
2021-06-15 08:03:41 -04:00
|
|
|
function create_lock
|
|
|
|
{
|
2021-09-13 04:09:01 -04:00
|
|
|
LOCK_FILE="/tmp/docker-mailserver/${SCRIPT_NAME}.lock"
|
|
|
|
while [[ -e "${LOCK_FILE}" ]]
|
|
|
|
do
|
|
|
|
_notify 'warn' "Lock file ${LOCK_FILE} exists. Another ${SCRIPT_NAME} execution is happening. Trying again shortly..."
|
|
|
|
# Handle stale lock files left behind on crashes
|
|
|
|
# or premature/non-graceful exits of containers while they're making changes
|
|
|
|
if [[ -n "$(find "${LOCK_FILE}" -mmin +1 2>/dev/null)" ]]
|
|
|
|
then
|
|
|
|
_notify 'warn' "Lock file older than 1 minute. Removing stale lock file."
|
|
|
|
rm -f "${LOCK_FILE}"
|
|
|
|
_notify 'inf' "Removed stale lock ${LOCK_FILE}."
|
|
|
|
fi
|
|
|
|
sleep 5
|
|
|
|
done
|
|
|
|
trap remove_lock EXIT
|
|
|
|
echo "${LOCK_ID}" > "${LOCK_FILE}"
|
2021-06-15 08:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function remove_lock
|
|
|
|
{
|
2021-09-13 04:09:01 -04:00
|
|
|
LOCK_FILE="${LOCK_FILE:-"/tmp/docker-mailserver/${SCRIPT_NAME}.lock"}"
|
|
|
|
[[ -z "${LOCK_ID}" ]] && errex "Cannot remove ${LOCK_FILE} as there is no LOCK_ID set"
|
|
|
|
if [[ -e "${LOCK_FILE}" && $(grep -c "${LOCK_ID}" "${LOCK_FILE}") -gt 0 ]] # Ensure we don't delete a lock that's not ours
|
|
|
|
then
|
|
|
|
rm -f "${LOCK_FILE}"
|
|
|
|
_notify 'inf' "Removed lock ${LOCK_FILE}."
|
|
|
|
fi
|
2021-06-15 08:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
# ? ––––––––––––––––––––––––––––––––––––––––––––– IP & CIDR
|
2020-09-05 16:19:12 +02:00
|
|
|
|
2020-10-02 15:45:57 +02:00
|
|
|
function _mask_ip_digit
|
2020-09-05 16:19:12 +02:00
|
|
|
{
|
|
|
|
if [[ ${1} -ge 8 ]]
|
|
|
|
then
|
2020-06-30 22:43:22 +02:00
|
|
|
MASK=255
|
2020-09-05 16:19:12 +02:00
|
|
|
elif [[ ${1} -le 0 ]]
|
|
|
|
then
|
|
|
|
MASK=0
|
2020-06-30 22:43:22 +02:00
|
|
|
else
|
2020-09-05 16:19:12 +02:00
|
|
|
VALUES=(0 128 192 224 240 248 252 254 255)
|
2020-09-06 12:27:40 +02:00
|
|
|
MASK=${VALUES[${1}]}
|
2020-06-30 22:43:22 +02:00
|
|
|
fi
|
2020-09-05 16:19:12 +02:00
|
|
|
|
|
|
|
local DVAL=${2}
|
|
|
|
((DVAL&=MASK))
|
|
|
|
|
2020-09-06 12:27:40 +02:00
|
|
|
echo "${DVAL}"
|
2019-08-07 02:24:56 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 16:19:12 +02:00
|
|
|
# Transforms a specific IP with CIDR suffix
|
|
|
|
# like 1.2.3.4/16 to subnet with cidr suffix
|
|
|
|
# like 1.2.0.0/16.
|
|
|
|
# Assumes correct IP and subnet are provided.
|
2020-10-02 15:45:57 +02:00
|
|
|
function _sanitize_ipv4_to_subnet_cidr
|
2020-09-05 16:19:12 +02:00
|
|
|
{
|
|
|
|
local DIGIT_PREFIX_LENGTH="${1#*/}"
|
|
|
|
|
2020-09-26 15:11:52 +02:00
|
|
|
declare -a MASKED_DIGITS DIGITS
|
2020-09-06 12:27:40 +02:00
|
|
|
IFS='.' ; read -r -a DIGITS < <(echo "${1%%/*}") ; unset IFS
|
2020-09-05 16:19:12 +02:00
|
|
|
|
|
|
|
for ((i = 0 ; i < 4 ; i++))
|
|
|
|
do
|
2020-09-06 12:27:40 +02:00
|
|
|
MASKED_DIGITS[i]=$(_mask_ip_digit "${DIGIT_PREFIX_LENGTH}" "${DIGITS[i]}")
|
2020-09-05 16:19:12 +02:00
|
|
|
DIGIT_PREFIX_LENGTH=$((DIGIT_PREFIX_LENGTH - 8))
|
2020-06-30 22:43:22 +02:00
|
|
|
done
|
2019-08-07 02:24:56 +02:00
|
|
|
|
2020-09-05 16:19:12 +02:00
|
|
|
echo "${MASKED_DIGITS[0]}.${MASKED_DIGITS[1]}.${MASKED_DIGITS[2]}.${MASKED_DIGITS[3]}/${1#*/}"
|
2020-06-30 22:43:22 +02:00
|
|
|
}
|
2020-09-05 16:19:12 +02:00
|
|
|
export -f _sanitize_ipv4_to_subnet_cidr
|
|
|
|
|
2021-06-08 03:20:20 +02:00
|
|
|
# ? --------------------------------------------- ACME
|
2020-09-05 16:19:12 +02:00
|
|
|
|
2020-10-02 15:45:57 +02:00
|
|
|
function _extract_certs_from_acme
|
2020-09-05 16:19:12 +02:00
|
|
|
{
|
|
|
|
local KEY
|
|
|
|
# shellcheck disable=SC2002
|
2020-07-19 23:57:16 +02:00
|
|
|
KEY=$(cat /etc/letsencrypt/acme.json | python -c "
|
|
|
|
import sys,json
|
|
|
|
acme = json.load(sys.stdin)
|
|
|
|
for key, value in acme.items():
|
|
|
|
certs = value['Certificates']
|
2020-12-06 20:29:12 +01:00
|
|
|
if certs is not None:
|
|
|
|
for cert in certs:
|
|
|
|
if 'domain' in cert and 'key' in cert:
|
|
|
|
if 'main' in cert['domain'] and cert['domain']['main'] == '${1}' or 'sans' in cert['domain'] and '${1}' in cert['domain']['sans']:
|
|
|
|
print cert['key']
|
|
|
|
break
|
2020-07-19 23:57:16 +02:00
|
|
|
")
|
2020-09-05 16:19:12 +02:00
|
|
|
|
|
|
|
local CERT
|
|
|
|
# shellcheck disable=SC2002
|
2020-07-19 23:57:16 +02:00
|
|
|
CERT=$(cat /etc/letsencrypt/acme.json | python -c "
|
|
|
|
import sys,json
|
|
|
|
acme = json.load(sys.stdin)
|
|
|
|
for key, value in acme.items():
|
|
|
|
certs = value['Certificates']
|
2020-12-06 20:29:12 +01:00
|
|
|
if certs is not None:
|
|
|
|
for cert in certs:
|
|
|
|
if 'domain' in cert and 'certificate' in cert:
|
|
|
|
if 'main' in cert['domain'] and cert['domain']['main'] == '${1}' or 'sans' in cert['domain'] and '${1}' in cert['domain']['sans']:
|
|
|
|
print cert['certificate']
|
|
|
|
break
|
2020-07-19 23:57:16 +02:00
|
|
|
")
|
2020-06-30 22:43:22 +02:00
|
|
|
|
2020-09-05 16:19:12 +02:00
|
|
|
if [[ -n "${KEY}${CERT}" ]]
|
|
|
|
then
|
|
|
|
mkdir -p "/etc/letsencrypt/live/${HOSTNAME}/"
|
|
|
|
|
2020-09-06 12:27:40 +02:00
|
|
|
echo "${KEY}" | base64 -d >/etc/letsencrypt/live/"${HOSTNAME}"/key.pem || exit 1
|
|
|
|
echo "${CERT}" | base64 -d >/etc/letsencrypt/live/"${HOSTNAME}"/fullchain.pem || exit 1
|
2021-01-22 10:03:31 +01:00
|
|
|
_notify 'inf' "Cert found in /etc/letsencrypt/acme.json for ${1}"
|
2020-09-05 16:19:12 +02:00
|
|
|
|
2020-06-30 22:43:22 +02:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
2020-09-05 16:19:12 +02:00
|
|
|
export -f _extract_certs_from_acme
|
|
|
|
|
2021-06-08 03:20:20 +02:00
|
|
|
# ? --------------------------------------------- Notifications
|
2020-08-24 22:08:11 +02:00
|
|
|
|
2020-10-02 15:45:57 +02:00
|
|
|
function _notify
|
2020-09-05 16:19:12 +02:00
|
|
|
{
|
2021-02-24 17:28:59 +01:00
|
|
|
{ [[ -z ${1:-} ]] || [[ -z ${2:-} ]] ; } && return 0
|
|
|
|
|
|
|
|
local RESET LGREEN LYELLOW LRED RED LBLUE LGREY LMAGENTA
|
|
|
|
|
|
|
|
RESET='\e[0m' ; LGREEN='\e[92m' ; LYELLOW='\e[93m'
|
|
|
|
LRED='\e[31m' ; RED='\e[91m' ; LBLUE='\e[34m'
|
|
|
|
LGREY='\e[37m' ; LMAGENTA='\e[95m'
|
|
|
|
|
|
|
|
case "${1}" in
|
|
|
|
'tasklog' ) echo "-e${3:-}" "[ ${LGREEN}TASKLOG${RESET} ] ${2}" ;;
|
|
|
|
'warn' ) echo "-e${3:-}" "[ ${LYELLOW}WARNING${RESET} ] ${2}" ;;
|
|
|
|
'err' ) echo "-e${3:-}" "[ ${LRED}ERROR${RESET} ] ${2}" ;;
|
|
|
|
'fatal' ) echo "-e${3:-}" "[ ${RED}FATAL${RESET} ] ${2}" ;;
|
|
|
|
'inf' ) [[ ${DMS_DEBUG} -eq 1 ]] && echo "-e${3:-}" "[[ ${LBLUE}INF${RESET} ]] ${2}" ;;
|
|
|
|
'task' ) [[ ${DMS_DEBUG} -eq 1 ]] && echo "-e${3:-}" "[[ ${LGREY}TASKS${RESET} ]] ${2}" ;;
|
|
|
|
* ) echo "-e${3:-}" "[ ${LMAGENTA}UNKNOWN${RESET} ] ${2}" ;;
|
2020-08-24 22:08:11 +02:00
|
|
|
esac
|
2021-02-24 17:28:59 +01:00
|
|
|
|
|
|
|
return 0
|
2020-08-24 22:08:11 +02:00
|
|
|
}
|
2020-09-05 16:53:36 +02:00
|
|
|
export -f _notify
|
2020-09-05 16:19:12 +02:00
|
|
|
|
2021-06-08 03:20:20 +02:00
|
|
|
# ? --------------------------------------------- Relay Host Map
|
2020-08-24 22:08:11 +02:00
|
|
|
|
|
|
|
# setup /etc/postfix/relayhost_map
|
|
|
|
# --
|
|
|
|
# @domain1.com [smtp.mailgun.org]:587
|
|
|
|
# @domain2.com [smtp.mailgun.org]:587
|
|
|
|
# @domain3.com [smtp.mailgun.org]:587
|
2020-10-02 15:45:57 +02:00
|
|
|
function _populate_relayhost_map
|
2020-09-05 16:19:12 +02:00
|
|
|
{
|
2020-10-06 14:45:55 +02:00
|
|
|
: >/etc/postfix/relayhost_map
|
2020-08-24 22:08:11 +02:00
|
|
|
chown root:root /etc/postfix/relayhost_map
|
|
|
|
chmod 0600 /etc/postfix/relayhost_map
|
|
|
|
|
2020-09-05 16:19:12 +02:00
|
|
|
if [[ -f /tmp/docker-mailserver/postfix-relaymap.cf ]]
|
|
|
|
then
|
2020-09-05 16:53:36 +02:00
|
|
|
_notify 'inf' "Adding relay mappings from postfix-relaymap.cf"
|
2020-09-05 16:19:12 +02:00
|
|
|
# keep lines which are not a comment *and* have a destination.
|
|
|
|
sed -n '/^\s*[^#[:space:]]\S*\s\+\S/p' /tmp/docker-mailserver/postfix-relaymap.cf >> /etc/postfix/relayhost_map
|
2020-08-24 22:08:11 +02:00
|
|
|
fi
|
2020-09-05 16:19:12 +02:00
|
|
|
|
2020-08-24 22:53:54 +02:00
|
|
|
{
|
2020-09-05 16:19:12 +02:00
|
|
|
# note: won't detect domains when lhs has spaces (but who does that?!)
|
2020-08-24 22:53:54 +02:00
|
|
|
sed -n '/^\s*[^#[:space:]]/ s/^[^@|]*@\([^|]\+\)|.*$/\1/p' /tmp/docker-mailserver/postfix-accounts.cf
|
2020-09-05 16:19:12 +02:00
|
|
|
|
|
|
|
[ -f /tmp/docker-mailserver/postfix-virtual.cf ] && sed -n '/^\s*[^#[:space:]]/ s/^\s*[^@[:space:]]*@\(\S\+\)\s.*/\1/p' /tmp/docker-mailserver/postfix-virtual.cf
|
2021-02-23 20:03:01 +01:00
|
|
|
} | while read -r DOMAIN
|
2020-09-05 16:19:12 +02:00
|
|
|
do
|
2021-02-23 20:03:01 +01:00
|
|
|
# DOMAIN not already present *and* not ignored
|
|
|
|
if ! grep -q -e "^@${DOMAIN}\b" /etc/postfix/relayhost_map && ! grep -qs -e "^\s*@${DOMAIN}\s*$" /tmp/docker-mailserver/postfix-relaymap.cf
|
2020-09-05 16:19:12 +02:00
|
|
|
then
|
2021-02-23 20:03:01 +01:00
|
|
|
_notify 'inf' "Adding relay mapping for ${DOMAIN}"
|
2021-06-07 08:58:34 -04:00
|
|
|
# shellcheck disable=SC2153
|
2021-02-23 20:03:01 +01:00
|
|
|
echo "@${DOMAIN} [${RELAY_HOST}]:${RELAY_PORT}" >> /etc/postfix/relayhost_map
|
2020-08-24 22:08:11 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
2020-09-05 16:19:12 +02:00
|
|
|
export -f _populate_relayhost_map
|
|
|
|
|
2021-06-08 03:20:20 +02:00
|
|
|
# ? --------------------------------------------- File Checksums
|
2020-08-24 22:08:11 +02:00
|
|
|
|
2020-09-05 16:19:12 +02:00
|
|
|
# file storing the checksums of the monitored files.
|
|
|
|
# shellcheck disable=SC2034
|
2020-08-24 20:46:50 +02:00
|
|
|
CHKSUM_FILE=/tmp/docker-mailserver-config-chksum
|
|
|
|
|
|
|
|
# Compute checksums of monitored files.
|
2020-10-02 15:45:57 +02:00
|
|
|
function _monitored_files_checksums
|
2020-09-05 16:19:12 +02:00
|
|
|
{
|
2020-08-24 20:46:50 +02:00
|
|
|
(
|
2020-09-05 16:19:12 +02:00
|
|
|
cd /tmp/docker-mailserver || exit 1
|
2020-08-24 20:46:50 +02:00
|
|
|
exec sha512sum 2>/dev/null -- \
|
2020-09-05 16:19:12 +02:00
|
|
|
postfix-accounts.cf \
|
|
|
|
postfix-virtual.cf \
|
|
|
|
postfix-aliases.cf \
|
|
|
|
dovecot-quotas.cf \
|
|
|
|
/etc/letsencrypt/acme.json \
|
2020-09-06 12:27:40 +02:00
|
|
|
"/etc/letsencrypt/live/${HOSTNAME}/key.pem" \
|
2020-11-16 15:49:35 +01:00
|
|
|
"/etc/letsencrypt/live/${HOSTNAME}/privkey.pem" \
|
2020-09-06 12:27:40 +02:00
|
|
|
"/etc/letsencrypt/live/${HOSTNAME}/fullchain.pem"
|
2020-08-24 20:46:50 +02:00
|
|
|
)
|
|
|
|
}
|
2020-09-05 16:19:12 +02:00
|
|
|
export -f _monitored_files_checksums
|
2021-06-19 22:24:06 +02:00
|
|
|
|
2021-09-11 10:20:16 -04:00
|
|
|
# ? --------------------------------------------- General
|
|
|
|
|
|
|
|
function _obtain_hostname_and_domainname
|
|
|
|
{
|
|
|
|
if [[ -n "${OVERRIDE_HOSTNAME}" ]]
|
|
|
|
then
|
|
|
|
export HOSTNAME="${OVERRIDE_HOSTNAME}"
|
|
|
|
export DOMAINNAME="${DOMAINNAME:-${HOSTNAME#*.}}"
|
|
|
|
# Handle situations where the hostname is name.tld and hostname -d ends up just showing "tld"
|
|
|
|
if [[ ! "${DOMAINNAME}" =~ .*\..* ]]
|
|
|
|
then
|
|
|
|
DOMAINNAME="${HOSTNAME}"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# These hostname commands will fail with "hostname: Name or service not known"
|
|
|
|
# if the hostname is not valid (important for tests)
|
|
|
|
HOSTNAME="$(hostname -f)"
|
|
|
|
DOMAINNAME="${DOMAINNAME:-$(hostname -d)}"
|
|
|
|
if [[ ! "${DOMAINNAME}" =~ .*\..* ]]
|
|
|
|
then
|
|
|
|
DOMAINNAME="${HOSTNAME}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-06-19 22:24:06 +02:00
|
|
|
function _shutdown
|
|
|
|
{
|
|
|
|
_notify 'err' "Shutting down.."
|
|
|
|
kill 1
|
|
|
|
}
|