2017-06-12 10:45:12 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
ACME_BASE=/var/lib/acme
|
2017-06-20 20:06:54 +02:00
|
|
|
SSL_EXAMPLE=/var/lib/ssl-example
|
2017-06-12 10:45:12 +02:00
|
|
|
mkdir -p ${ACME_BASE}/acme/private
|
|
|
|
|
|
|
|
restart_containers(){
|
|
|
|
for container in $*; do
|
|
|
|
curl -X POST \
|
|
|
|
--unix-socket /var/run/docker.sock \
|
|
|
|
"http/containers/${container}/restart"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2017-06-20 20:06:54 +02:00
|
|
|
if [[ -f ${ACME_BASE}/cert.pem ]]; then
|
2017-06-23 08:33:07 +02:00
|
|
|
ISSUER=$(openssl x509 -in ${ACME_BASE}/cert.pem -noout -issuer)
|
|
|
|
if [[ ${ISSUER} != *"Let's Encrypt"* && ${ISSUER} != *"mailcow"* ]]; then
|
|
|
|
echo "Found certificate with issuer other than mailcow snake-oil CA and Let's Encrypt, skipping ACME client..."
|
2017-06-20 20:06:54 +02:00
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
declare -a SAN_ARRAY_NOW
|
|
|
|
SAN_NAMES=$(openssl x509 -noout -text -in ${ACME_BASE}/cert.pem | awk '/X509v3 Subject Alternative Name/ {getline;gsub(/ /, "", $0); print}' | tr -d "DNS:")
|
|
|
|
if [[ ! -z ${SAN_NAMES} ]]; then
|
|
|
|
IFS=',' read -a SAN_ARRAY_NOW <<< ${SAN_NAMES}
|
2017-06-23 08:33:07 +02:00
|
|
|
echo "Found Let's Encrypt or mailcow snake-oil CA issued certificate with SANs: ${SAN_ARRAY_NOW[*]}"
|
2017-06-20 20:06:54 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
2017-06-23 08:33:07 +02:00
|
|
|
ISSUER="mailcow"
|
2017-06-20 20:06:54 +02:00
|
|
|
if [[ -f ${ACME_BASE}/acme/fullchain.pem ]] && [[ -f ${ACME_BASE}/acme/private/privkey.pem ]]; then
|
|
|
|
cp ${ACME_BASE}/acme/fullchain.pem ${ACME_BASE}/cert.pem
|
|
|
|
cp ${ACME_BASE}/acme/private/privkey.pem ${ACME_BASE}/key.pem
|
|
|
|
else
|
|
|
|
cp ${SSL_EXAMPLE}/cert.pem ${ACME_BASE}/cert.pem
|
|
|
|
cp ${SSL_EXAMPLE}/key.pem ${ACME_BASE}/key.pem
|
|
|
|
fi
|
2017-06-17 10:08:12 +02:00
|
|
|
fi
|
|
|
|
|
2017-06-12 10:45:12 +02:00
|
|
|
while true; do
|
2017-06-23 08:33:07 +02:00
|
|
|
if [[ "${SKIP_LETS_ENCRYPT}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
|
|
|
|
echo "SKIP_LETS_ENCRYPT=y, skipping Let's Encrypt..."
|
|
|
|
exit 0
|
|
|
|
fi
|
2017-06-13 23:37:48 +02:00
|
|
|
declare -a SQL_DOMAIN_ARR
|
2017-06-20 20:06:54 +02:00
|
|
|
declare -a VALIDATED_CONFIG_DOMAINS
|
2017-06-14 07:24:32 +02:00
|
|
|
declare -a ADDITIONAL_VALIDATED_SAN
|
|
|
|
IFS=' ' read -r -a ADDITIONAL_SAN_ARR <<< "${ADDITIONAL_SAN}"
|
|
|
|
IPV4=$(curl -4s https://mailcow.email/ip.php)
|
2017-06-13 23:37:48 +02:00
|
|
|
|
|
|
|
while read line; do
|
|
|
|
SQL_DOMAIN_ARR+=("${line}")
|
|
|
|
done < <(mysql -h mysql-mailcow -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "SELECT domain FROM domain" -Bs)
|
|
|
|
|
|
|
|
for SQL_DOMAIN in "${SQL_DOMAIN_ARR[@]}"; do
|
2017-06-22 20:34:54 +02:00
|
|
|
A_CONFIG=$(dig A autoconfig.${SQL_DOMAIN} +short | tail -n 1)
|
2017-06-13 23:37:48 +02:00
|
|
|
if [[ ! -z ${A_CONFIG} ]]; then
|
|
|
|
echo "Found A record for autoconfig.${SQL_DOMAIN}: ${A_CONFIG}"
|
|
|
|
if [[ ${IPV4} == ${A_CONFIG} ]]; then
|
|
|
|
echo "Confirmed A record autoconfig.${SQL_DOMAIN}"
|
2017-06-20 20:06:54 +02:00
|
|
|
VALIDATED_CONFIG_DOMAINS+=("autoconfig.${SQL_DOMAIN}")
|
2017-06-13 23:37:48 +02:00
|
|
|
else
|
|
|
|
echo "Cannot match Your IP against hostname autoconfig.${SQL_DOMAIN}"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "No A record for autoconfig.${SQL_DOMAIN} found"
|
2017-06-12 10:45:12 +02:00
|
|
|
fi
|
|
|
|
|
2017-06-22 20:34:54 +02:00
|
|
|
A_DISCOVER=$(dig A autodiscover.${SQL_DOMAIN} +short | tail -n 1)
|
2017-06-13 23:37:48 +02:00
|
|
|
if [[ ! -z ${A_DISCOVER} ]]; then
|
|
|
|
echo "Found A record for autodiscover.${SQL_DOMAIN}: ${A_CONFIG}"
|
|
|
|
if [[ ${IPV4} == ${A_DISCOVER} ]]; then
|
|
|
|
echo "Confirmed A record autodiscover.${SQL_DOMAIN}"
|
2017-06-20 20:06:54 +02:00
|
|
|
VALIDATED_CONFIG_DOMAINS+=("autodiscover.${SQL_DOMAIN}")
|
2017-06-13 23:37:48 +02:00
|
|
|
else
|
|
|
|
echo "Cannot match Your IP against hostname autodiscover.${SQL_DOMAIN}"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "No A record for autodiscover.${SQL_DOMAIN} found"
|
2017-06-12 10:45:12 +02:00
|
|
|
fi
|
2017-06-13 23:37:48 +02:00
|
|
|
done
|
2017-06-12 10:45:12 +02:00
|
|
|
|
2017-06-14 07:24:32 +02:00
|
|
|
for SAN in "${ADDITIONAL_SAN_ARR[@]}"; do
|
2017-06-22 20:34:54 +02:00
|
|
|
A_SAN=$(dig A ${SAN} +short | tail -n 1)
|
2017-06-14 07:24:32 +02:00
|
|
|
if [[ ! -z ${A_SAN} ]]; then
|
|
|
|
echo "Found A record for ${SAN}: ${A_SAN}"
|
|
|
|
if [[ ${IPV4} == ${A_SAN} ]]; then
|
|
|
|
echo "Confirmed A record ${SAN}"
|
|
|
|
ADDITIONAL_VALIDATED_SAN+=("${SAN}")
|
|
|
|
else
|
|
|
|
echo "Cannot match Your IP against hostname ${SAN}"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "No A record for ${SAN} found"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2017-06-20 20:06:54 +02:00
|
|
|
ORPHANED_SAN=($(echo ${SAN_ARRAY_NOW[*]} ${VALIDATED_CONFIG_DOMAINS[*]} ${ADDITIONAL_VALIDATED_SAN[*]} ${MAILCOW_HOSTNAME} | tr ' ' '\n' | sort | uniq -u ))
|
|
|
|
if [[ ! -z ${ORPHANED_SAN[*]} ]]; then
|
|
|
|
DATE=$(date +%Y-%m-%d_%H_%M_%S)
|
2017-06-23 08:40:05 +02:00
|
|
|
echo "Found orphaned SAN(s) ${ORPHANED_SAN[*]} in certificate, moving old files to ${ACME_BASE}/acme/private/${DATE}.bak/"
|
2017-06-23 08:33:07 +02:00
|
|
|
mkdir -p ${ACME_BASE}/acme/private/${DATE}.bak/
|
|
|
|
[[ -f ${ACME_BASE}/acme/private/account.key ]] && mv ${ACME_BASE}/acme/private/account.key ${ACME_BASE}/acme/private/${DATE}.bak/
|
|
|
|
mv ${ACME_BASE}/acme/fullchain.pem ${ACME_BASE}/acme/private/${DATE}.bak/
|
2017-06-23 08:40:05 +02:00
|
|
|
mv ${ACME_BASE}/acme/cert.pem ${ACME_BASE}/acme/private/${DATE}.bak/
|
2017-06-23 08:33:07 +02:00
|
|
|
cp ${ACME_BASE}/acme/private/privkey.pem ${ACME_BASE}/acme/private/${DATE}.bak/ # Keep key for TLSA 3 1 1 records
|
2017-06-20 20:06:54 +02:00
|
|
|
fi
|
|
|
|
|
2017-06-12 10:45:12 +02:00
|
|
|
acme-client \
|
2017-06-13 23:37:48 +02:00
|
|
|
-v -e -b -N -n \
|
2017-06-12 10:45:12 +02:00
|
|
|
-f ${ACME_BASE}/acme/private/account.key \
|
|
|
|
-k ${ACME_BASE}/acme/private/privkey.pem \
|
|
|
|
-c ${ACME_BASE}/acme \
|
2017-06-20 20:06:54 +02:00
|
|
|
${MAILCOW_HOSTNAME} ${VALIDATED_CONFIG_DOMAINS[*]} ${ADDITIONAL_VALIDATED_SAN[*]}
|
2017-06-12 10:45:12 +02:00
|
|
|
|
|
|
|
case "$?" in
|
|
|
|
0) # new certs
|
|
|
|
# cp the new certificates and keys
|
|
|
|
cp ${ACME_BASE}/acme/fullchain.pem ${ACME_BASE}/cert.pem
|
|
|
|
cp ${ACME_BASE}/acme/private/privkey.pem ${ACME_BASE}/key.pem
|
|
|
|
|
|
|
|
# restart docker containers
|
|
|
|
restart_containers ${CONTAINERS_RESTART}
|
|
|
|
;;
|
|
|
|
1) # failure
|
2017-06-23 14:22:54 +02:00
|
|
|
[[ -f ${ACME_BASE}/acme/private/${DATE}.bak/fullchain.pem ]] && cp ${ACME_BASE}/acme/private/${DATE}.bak/fullchain.pem ${ACME_BASE}/cert.pem
|
|
|
|
[[ -f ${ACME_BASE}/acme/private/${DATE}.bak/privkey.pem ]] && cp ${ACME_BASE}/acme/private/${DATE}.bak/privkey.pem ${ACME_BASE}/key.pem
|
2017-06-12 10:45:12 +02:00
|
|
|
exit 1;;
|
|
|
|
2) # no change
|
|
|
|
;;
|
|
|
|
*) # unspecified
|
2017-06-23 14:22:54 +02:00
|
|
|
[[ -f ${ACME_BASE}/acme/private/${DATE}.bak/fullchain.pem ]] && cp ${ACME_BASE}/acme/private/${DATE}.bak/fullchain.pem ${ACME_BASE}/cert.pem
|
|
|
|
[[ -f ${ACME_BASE}/acme/private/${DATE}.bak/privkey.pem ]] && cp ${ACME_BASE}/acme/private/${DATE}.bak/privkey.pem ${ACME_BASE}/key.pem
|
2017-06-12 10:45:12 +02:00
|
|
|
exit 1;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
echo "ACME certificate validation done. Sleeping for another day."
|
|
|
|
sleep 86400
|
|
|
|
|
|
|
|
done
|