certs-daemon 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env bash
  2. if [ `id -gn` != "root" ]; then
  3. echo "Run as root!"
  4. exit 1
  5. fi
  6. snapctl is-connected certs
  7. if [ $? -eq 0 ]; then
  8. echo "See log (journal -exf --grep=snap.${SNAP_NAME}*) for result"
  9. DOMAIN=`snapctl get domain`
  10. if [ $? -ne 0 ]; then
  11. logger "Missing domain, use 'snap set ${SNAP_NAME} domain=<domain>'"
  12. exit 0
  13. fi
  14. source "${SNAP}/helper/init"
  15. readarray -d '' DOMAIN_DIR < <(find "${CERTS_DIR}" -type d -name "${DOMAIN}" -print0)
  16. if [ "${#DOMAIN_DIR[@]}" -ne 1 ]; then
  17. logger "Not a unique match for domain ${DOMAIN}"
  18. exit 1
  19. fi
  20. DOMAIN_DIR="${DOMAIN_DIR[0]}"
  21. LAST_EDIT=`stat "${DOMAIN_DIR}/.time" --format="%Y" 2> /dev/null || echo 0`
  22. CURR_EDIT=`stat "${SSL_DIR}/.time" --format="%Y" 2> /dev/null || echo 0`
  23. if [ "${LAST_EDIT}" -le "${CURR_EDIT}" ]; then
  24. logger "No new certificate for ${DOMAIN}: `expr ${CURR_EDIT} - ${LAST_EDIT}`"
  25. exit 0
  26. fi
  27. readarray -d '' CERTS < <(find "${DOMAIN_DIR}" -type f -name "*.gpg" -print0)
  28. gpg_start_agent
  29. for CERT in "${CERTS[@]}"; do
  30. DEST="${SSL_DIR}/`basename "${CERT}" ".gpg"`"
  31. CURR=""
  32. if [ -f "${DEST}" ]; then
  33. CURR=`cat "${DEST}"`
  34. fi
  35. gpg --batch --yes --output "${DEST}" --decrypt "${CERT}"
  36. if [ -n "${CURR}" ]; then
  37. DIFF=`echo "${CURR}" | diff "${DEST}" -`
  38. if [ $? -ne 0 ]; then
  39. echo "${ORIG}" > "${DEST}.backup"
  40. fi
  41. fi
  42. done
  43. cp -f "${DOMAIN_DIR}/.time" "${SSL_DIR}/.time"
  44. logger "Replaced certificate ${DOMAIN} for snap ${SNAP_NAME}"
  45. snapctl restart "${SNAP_NAME}.server"
  46. gpg_close_agent
  47. fi