2019-08-07 00:24:56 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2020-09-05 14:19:12 +00:00
|
|
|
# version 0.1.1
|
|
|
|
#
|
|
|
|
# Provides varous helpers.
|
|
|
|
|
|
|
|
|
|
|
|
# ? IP and CIDR -------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
function _mask_ip_digit()
|
|
|
|
{
|
|
|
|
if [[ ${1} -ge 8 ]]
|
|
|
|
then
|
2020-06-30 20:43:22 +00:00
|
|
|
MASK=255
|
2020-09-05 14:19:12 +00:00
|
|
|
elif [[ ${1} -le 0 ]]
|
|
|
|
then
|
|
|
|
MASK=0
|
2020-06-30 20:43:22 +00:00
|
|
|
else
|
2020-09-05 14:19:12 +00:00
|
|
|
VALUES=(0 128 192 224 240 248 252 254 255)
|
2020-09-06 10:27:40 +00:00
|
|
|
MASK=${VALUES[${1}]}
|
2020-06-30 20:43:22 +00:00
|
|
|
fi
|
2020-09-05 14:19:12 +00:00
|
|
|
|
|
|
|
local DVAL=${2}
|
|
|
|
((DVAL&=MASK))
|
|
|
|
|
2020-09-06 10:27:40 +00:00
|
|
|
echo "${DVAL}"
|
2019-08-07 00:24:56 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 14:19:12 +00: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.
|
|
|
|
function _sanitize_ipv4_to_subnet_cidr()
|
|
|
|
{
|
|
|
|
local DIGIT_PREFIX_LENGTH="${1#*/}"
|
|
|
|
|
2020-09-26 13:11:52 +00:00
|
|
|
declare -a MASKED_DIGITS DIGITS
|
2020-09-06 10:27:40 +00:00
|
|
|
IFS='.' ; read -r -a DIGITS < <(echo "${1%%/*}") ; unset IFS
|
2020-09-05 14:19:12 +00:00
|
|
|
|
|
|
|
for ((i = 0 ; i < 4 ; i++))
|
|
|
|
do
|
2020-09-06 10:27:40 +00:00
|
|
|
MASKED_DIGITS[i]=$(_mask_ip_digit "${DIGIT_PREFIX_LENGTH}" "${DIGITS[i]}")
|
2020-09-05 14:19:12 +00:00
|
|
|
DIGIT_PREFIX_LENGTH=$((DIGIT_PREFIX_LENGTH - 8))
|
2020-06-30 20:43:22 +00:00
|
|
|
done
|
2019-08-07 00:24:56 +00:00
|
|
|
|
2020-09-05 14:19:12 +00:00
|
|
|
echo "${MASKED_DIGITS[0]}.${MASKED_DIGITS[1]}.${MASKED_DIGITS[2]}.${MASKED_DIGITS[3]}/${1#*/}"
|
2020-06-30 20:43:22 +00:00
|
|
|
}
|
2020-09-05 14:19:12 +00:00
|
|
|
export -f _sanitize_ipv4_to_subnet_cidr
|
|
|
|
|
2020-06-30 20:43:22 +00:00
|
|
|
|
2020-09-05 14:19:12 +00:00
|
|
|
# ? ACME certs --------------------------------------------
|
2020-07-19 21:57:16 +00:00
|
|
|
|
2020-09-05 14:19:12 +00:00
|
|
|
|
|
|
|
function _extract_certs_from_acme()
|
|
|
|
{
|
|
|
|
local KEY
|
|
|
|
# shellcheck disable=SC2002
|
2020-07-19 21:57:16 +00: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']
|
|
|
|
for cert in certs:
|
|
|
|
if 'domain' in cert and 'key' in cert:
|
2020-09-06 10:27:40 +00:00
|
|
|
if 'main' in cert['domain'] and cert['domain']['main'] == '${1}' or 'sans' in cert['domain'] and '${1}' in cert['domain']['sans']:
|
2020-07-19 21:57:16 +00:00
|
|
|
print cert['key']
|
2020-07-20 09:28:23 +00:00
|
|
|
break
|
2020-07-19 21:57:16 +00:00
|
|
|
")
|
2020-09-05 14:19:12 +00:00
|
|
|
|
|
|
|
local CERT
|
|
|
|
# shellcheck disable=SC2002
|
2020-07-19 21:57:16 +00: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']
|
|
|
|
for cert in certs:
|
|
|
|
if 'domain' in cert and 'certificate' in cert:
|
2020-09-06 10:27:40 +00:00
|
|
|
if 'main' in cert['domain'] and cert['domain']['main'] == '${1}' or 'sans' in cert['domain'] and '${1}' in cert['domain']['sans']:
|
2020-07-19 21:57:16 +00:00
|
|
|
print cert['certificate']
|
2020-07-20 09:28:23 +00:00
|
|
|
break
|
2020-07-19 21:57:16 +00:00
|
|
|
")
|
2020-06-30 20:43:22 +00:00
|
|
|
|
2020-09-05 14:19:12 +00:00
|
|
|
if [[ -n "${KEY}${CERT}" ]]
|
|
|
|
then
|
|
|
|
mkdir -p "/etc/letsencrypt/live/${HOSTNAME}/"
|
|
|
|
|
2020-09-06 10:27:40 +00: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
|
|
|
|
echo "Cert found in /etc/letsencrypt/acme.json for ${1}"
|
2020-09-05 14:19:12 +00:00
|
|
|
|
2020-06-30 20:43:22 +00:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
2020-09-05 14:19:12 +00:00
|
|
|
export -f _extract_certs_from_acme
|
|
|
|
|
|
|
|
|
|
|
|
# ? Notification ------------------------------------------
|
|
|
|
|
2020-08-24 18:46:50 +00:00
|
|
|
|
2020-08-24 20:08:11 +00:00
|
|
|
declare -A DEFAULT_VARS
|
2020-09-09 15:19:48 +00:00
|
|
|
DEFAULT_VARS["DMS_DEBUG"]="${DMS_DEBUG:=0}"
|
2020-08-24 20:08:11 +00:00
|
|
|
|
2020-09-05 14:53:36 +00:00
|
|
|
function _notify()
|
2020-09-05 14:19:12 +00:00
|
|
|
{
|
2020-08-24 20:08:11 +00:00
|
|
|
c_red="\e[0;31m"
|
|
|
|
c_green="\e[0;32m"
|
|
|
|
c_brown="\e[0;33m"
|
|
|
|
c_blue="\e[0;34m"
|
|
|
|
c_bold="\033[1m"
|
|
|
|
c_reset="\e[0m"
|
|
|
|
|
2020-09-06 10:27:40 +00:00
|
|
|
notification_type=${1}
|
|
|
|
notification_msg=${2}
|
|
|
|
notification_format=${3}
|
2020-08-24 20:08:11 +00:00
|
|
|
msg=""
|
|
|
|
|
|
|
|
case "${notification_type}" in
|
2020-09-05 14:19:12 +00:00
|
|
|
'taskgrp' ) msg="${c_bold}${notification_msg}${c_reset}" ;;
|
|
|
|
'task' )
|
2020-09-09 15:19:48 +00:00
|
|
|
if [[ ${DEFAULT_VARS["DMS_DEBUG"]} -eq 1 ]]
|
2020-09-05 14:19:12 +00:00
|
|
|
then
|
2020-08-24 20:08:11 +00:00
|
|
|
msg=" ${notification_msg}${c_reset}"
|
|
|
|
fi
|
|
|
|
;;
|
2020-09-05 14:19:12 +00:00
|
|
|
'inf' )
|
2020-09-09 15:19:48 +00:00
|
|
|
if [[ ${DEFAULT_VARS["DMS_DEBUG"]} -eq 1 ]]
|
2020-09-05 14:19:12 +00:00
|
|
|
then
|
2020-08-24 20:08:11 +00:00
|
|
|
msg="${c_green} * ${notification_msg}${c_reset}"
|
|
|
|
fi
|
|
|
|
;;
|
2020-09-05 14:19:12 +00:00
|
|
|
'started' ) msg="${c_green} ${notification_msg}${c_reset}" ;;
|
|
|
|
'warn' ) msg="${c_brown} Warning ${notification_msg}${c_reset}" ;;
|
|
|
|
'err' ) msg="${c_blue} Error ${notification_msg}${c_reset}" ;;
|
|
|
|
'fatal' ) msg="${c_red} Fatal Error: ${notification_msg}${c_reset}" ;;
|
|
|
|
* ) msg="" ;;
|
2020-08-24 20:08:11 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
case "${notification_format}" in
|
2020-09-05 14:19:12 +00:00
|
|
|
'n' ) options="-ne" ;;
|
|
|
|
* ) options="-e" ;;
|
2020-08-24 20:08:11 +00:00
|
|
|
esac
|
|
|
|
|
2020-09-08 17:49:19 +00:00
|
|
|
[[ -n "${msg}" ]] && echo "${options}" "${msg}"
|
2020-08-24 20:08:11 +00:00
|
|
|
}
|
2020-09-05 14:53:36 +00:00
|
|
|
export -f _notify
|
2020-09-05 14:19:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ? Relay Host Map ----------------------------------------
|
|
|
|
|
2020-08-24 20:08:11 +00: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-09-05 14:19:12 +00:00
|
|
|
function _populate_relayhost_map()
|
|
|
|
{
|
2020-08-24 20:08:11 +00:00
|
|
|
echo -n > /etc/postfix/relayhost_map
|
|
|
|
chown root:root /etc/postfix/relayhost_map
|
|
|
|
chmod 0600 /etc/postfix/relayhost_map
|
|
|
|
|
2020-09-05 14:19:12 +00:00
|
|
|
if [[ -f /tmp/docker-mailserver/postfix-relaymap.cf ]]
|
|
|
|
then
|
2020-09-05 14:53:36 +00:00
|
|
|
_notify 'inf' "Adding relay mappings from postfix-relaymap.cf"
|
2020-09-05 14:19:12 +00: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 20:08:11 +00:00
|
|
|
fi
|
2020-09-05 14:19:12 +00:00
|
|
|
|
2020-08-24 20:53:54 +00:00
|
|
|
{
|
2020-09-05 14:19:12 +00:00
|
|
|
# note: won't detect domains when lhs has spaces (but who does that?!)
|
2020-08-24 20:53:54 +00:00
|
|
|
sed -n '/^\s*[^#[:space:]]/ s/^[^@|]*@\([^|]\+\)|.*$/\1/p' /tmp/docker-mailserver/postfix-accounts.cf
|
2020-09-05 14:19:12 +00: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
|
|
|
|
} | while read -r domain
|
|
|
|
do
|
|
|
|
# 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
|
|
|
|
then
|
2020-09-05 14:53:36 +00:00
|
|
|
_notify 'inf' "Adding relay mapping for ${domain}"
|
2020-09-06 10:27:40 +00:00
|
|
|
echo "@${domain} [${RELAY_HOST}]:${RELAY_PORT}" >> /etc/postfix/relayhost_map
|
2020-08-24 20:08:11 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
2020-09-05 14:19:12 +00:00
|
|
|
export -f _populate_relayhost_map
|
|
|
|
|
|
|
|
|
|
|
|
# ? File checksums ----------------------------------------
|
|
|
|
|
2020-08-24 20:08:11 +00:00
|
|
|
|
2020-09-05 14:19:12 +00:00
|
|
|
# file storing the checksums of the monitored files.
|
|
|
|
# shellcheck disable=SC2034
|
2020-08-24 18:46:50 +00:00
|
|
|
CHKSUM_FILE=/tmp/docker-mailserver-config-chksum
|
|
|
|
|
|
|
|
# Compute checksums of monitored files.
|
2020-09-05 14:19:12 +00:00
|
|
|
function _monitored_files_checksums()
|
|
|
|
{
|
2020-08-24 18:46:50 +00:00
|
|
|
(
|
2020-09-05 14:19:12 +00:00
|
|
|
cd /tmp/docker-mailserver || exit 1
|
2020-08-24 18:46:50 +00:00
|
|
|
exec sha512sum 2>/dev/null -- \
|
2020-09-05 14:19:12 +00:00
|
|
|
postfix-accounts.cf \
|
|
|
|
postfix-virtual.cf \
|
|
|
|
postfix-aliases.cf \
|
|
|
|
dovecot-quotas.cf \
|
|
|
|
/etc/letsencrypt/acme.json \
|
2020-09-06 10:27:40 +00:00
|
|
|
"/etc/letsencrypt/live/${HOSTNAME}/key.pem" \
|
|
|
|
"/etc/letsencrypt/live/${HOSTNAME}/fullchain.pem"
|
2020-08-24 18:46:50 +00:00
|
|
|
)
|
|
|
|
}
|
2020-09-05 14:19:12 +00:00
|
|
|
export -f _monitored_files_checksums
|