2019-09-27 00:13:30 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
error() {
|
|
|
|
>&2 echo "${PROGNAME}: $1"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
debug() {
|
|
|
|
if [ "${VERBOSE}" = "1" ] && [ "${QUIET}" != "1" ]; then
|
|
|
|
>&2 echo "${PROGNAME}: $1"
|
|
|
|
fi
|
|
|
|
}
|
2019-09-27 14:03:39 +02:00
|
|
|
daemon_found_and_running() {
|
|
|
|
test -n "$(pidof haproxy)" && test -n "${haproxy_bin}"
|
|
|
|
}
|
|
|
|
found_renewed_lineage() {
|
|
|
|
test -f "${RENEWED_LINEAGE}/fullchain.pem" && test -f "${RENEWED_LINEAGE}/privkey.pem"
|
|
|
|
}
|
|
|
|
config_check() {
|
2020-06-14 12:30:34 +02:00
|
|
|
${haproxy_bin} -c -f "${haproxy_config_file}" > /dev/null 2>&1
|
2019-09-27 14:03:39 +02:00
|
|
|
}
|
|
|
|
concat_files() {
|
|
|
|
# shellcheck disable=SC2174
|
|
|
|
mkdir --mode=700 --parents "${haproxy_cert_dir}"
|
|
|
|
chown root: "${haproxy_cert_dir}"
|
2019-09-27 00:13:30 +02:00
|
|
|
|
2019-09-27 14:03:39 +02:00
|
|
|
debug "Concatenating certificate files to ${haproxy_cert_file}"
|
|
|
|
cat "${RENEWED_LINEAGE}/fullchain.pem" "${RENEWED_LINEAGE}/privkey.pem" > "${haproxy_cert_file}"
|
|
|
|
chmod 600 "${haproxy_cert_file}"
|
|
|
|
chown root: "${haproxy_cert_file}"
|
|
|
|
}
|
|
|
|
cert_and_key_mismatch() {
|
|
|
|
haproxy_cert_md5=$(openssl x509 -noout -modulus -in "${haproxy_cert_file}" | openssl md5)
|
|
|
|
haproxy_key_md5=$(openssl rsa -noout -modulus -in "${haproxy_cert_file}" | openssl md5)
|
2019-09-27 00:13:30 +02:00
|
|
|
|
2019-09-27 14:03:39 +02:00
|
|
|
test "${haproxy_cert_md5}" != "${haproxy_key_md5}"
|
|
|
|
}
|
2020-06-14 12:30:34 +02:00
|
|
|
detect_haproxy_cert_dir() {
|
|
|
|
# get last field or line wich defines the crt directory
|
2020-07-21 10:45:34 +02:00
|
|
|
config_cert_dir=$(grep -r -o -E -h '^\s*bind .* crt /etc/\S+' "${haproxy_config_file}" | head -1 | awk '{ print $(NF)}')
|
2020-06-14 12:30:34 +02:00
|
|
|
if [ -n "${config_cert_dir}" ]; then
|
|
|
|
debug "Cert directory is configured with ${config_cert_dir}"
|
|
|
|
echo "${config_cert_dir}"
|
|
|
|
elif [ -d "/etc/haproxy/ssl" ]; then
|
|
|
|
debug "No configured cert directory found, but /etc/haproxy/ssl exists"
|
|
|
|
echo "/etc/haproxy/ssl"
|
|
|
|
elif [ -d "/etc/ssl/haproxy" ]; then
|
|
|
|
debug "No configured cert directory found, but /etc/ssl/haproxy exists"
|
|
|
|
echo "/etc/ssl/haproxy"
|
|
|
|
else
|
|
|
|
error "Cert directory not found."
|
|
|
|
fi
|
|
|
|
}
|
2019-09-27 14:03:39 +02:00
|
|
|
main() {
|
|
|
|
if [ -z "${RENEWED_LINEAGE}" ]; then
|
|
|
|
error "This script must be called only by certbot!"
|
|
|
|
fi
|
2019-09-27 10:15:33 +02:00
|
|
|
|
2019-09-27 14:03:39 +02:00
|
|
|
if daemon_found_and_running; then
|
2020-12-03 17:26:16 +01:00
|
|
|
readonly haproxy_config_file="/etc/haproxy/haproxy.cfg"
|
|
|
|
readonly haproxy_cert_dir=$(detect_haproxy_cert_dir)
|
|
|
|
|
2019-09-27 14:03:39 +02:00
|
|
|
if found_renewed_lineage; then
|
|
|
|
haproxy_cert_file="${haproxy_cert_dir}/$(basename "${RENEWED_LINEAGE}").pem"
|
|
|
|
failed_cert_file="/root/$(basename "${RENEWED_LINEAGE}").failed.pem"
|
2019-09-27 09:31:22 +02:00
|
|
|
|
2019-09-27 14:03:39 +02:00
|
|
|
concat_files
|
2019-09-27 09:47:51 +02:00
|
|
|
|
2019-09-27 14:03:39 +02:00
|
|
|
if cert_and_key_mismatch; then
|
|
|
|
mv "${haproxy_cert_file}" "${failed_cert_file}"
|
|
|
|
error "Key and cert don't match, we moved the file to ${failed_cert_file} for inspection"
|
|
|
|
fi
|
2019-09-27 09:47:51 +02:00
|
|
|
|
2019-09-27 14:03:39 +02:00
|
|
|
if config_check; then
|
|
|
|
debug "HAProxy detected... reloading"
|
2019-10-25 18:21:23 +02:00
|
|
|
systemctl reload haproxy
|
2019-09-27 14:03:39 +02:00
|
|
|
else
|
|
|
|
error "HAProxy config is broken, you must fix it !"
|
|
|
|
fi
|
2019-09-27 00:13:30 +02:00
|
|
|
else
|
2019-09-27 14:03:39 +02:00
|
|
|
error "Couldn't find ${RENEWED_LINEAGE}/fullchain.pem or ${RENEWED_LINEAGE}/privkey.pem"
|
2019-09-27 00:13:30 +02:00
|
|
|
fi
|
|
|
|
else
|
2019-09-27 14:03:39 +02:00
|
|
|
debug "HAProxy is not running or missing. Skip."
|
2019-09-27 00:13:30 +02:00
|
|
|
fi
|
2019-09-27 14:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
readonly PROGNAME=$(basename "$0")
|
|
|
|
readonly VERBOSE=${VERBOSE:-"0"}
|
|
|
|
readonly QUIET=${QUIET:-"0"}
|
|
|
|
|
|
|
|
readonly haproxy_bin=$(command -v haproxy)
|
|
|
|
|
|
|
|
main
|