acme.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #!/bin/bash
  2. set -o pipefail
  3. exec 5>&1
  4. # Do not attempt to write to slave
  5. if [[ ! -z ${REDIS_SLAVEOF_IP} ]]; then
  6. export REDIS_CMDLINE="redis-cli -h ${REDIS_SLAVEOF_IP} -p ${REDIS_SLAVEOF_PORT}"
  7. else
  8. export REDIS_CMDLINE="redis-cli -h redis -p 6379"
  9. fi
  10. until [[ $(${REDIS_CMDLINE} PING) == "PONG" ]]; do
  11. echo "Waiting for Redis..."
  12. sleep 2
  13. done
  14. source /srv/functions.sh
  15. # Thanks to https://github.com/cvmiller -> https://github.com/cvmiller/expand6
  16. source /srv/expand6.sh
  17. # Skipping IP check when we like to live dangerously
  18. if [[ "${SKIP_IP_CHECK}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  19. SKIP_IP_CHECK=y
  20. fi
  21. # Skipping HTTP check when we like to live dangerously
  22. if [[ "${SKIP_HTTP_VERIFICATION}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  23. SKIP_HTTP_VERIFICATION=y
  24. fi
  25. # Request certificate for MAILCOW_HOSTNAME only
  26. if [[ "${ONLY_MAILCOW_HOSTNAME}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  27. ONLY_MAILCOW_HOSTNAME=y
  28. fi
  29. # Request individual certificate for every domain
  30. if [[ "${ENABLE_SSL_SNI}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  31. ENABLE_SSL_SNI=y
  32. fi
  33. if [[ "${SKIP_LETS_ENCRYPT}" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
  34. log_f "SKIP_LETS_ENCRYPT=y, skipping Let's Encrypt..."
  35. sleep 365d
  36. exec $(readlink -f "$0")
  37. fi
  38. log_f "Waiting for Docker API..."
  39. until ping dockerapi -c1 > /dev/null; do
  40. sleep 1
  41. done
  42. log_f "Docker API OK"
  43. log_f "Waiting for Postfix..."
  44. until ping postfix -c1 > /dev/null; do
  45. sleep 1
  46. done
  47. log_f "Postfix OK"
  48. log_f "Waiting for Dovecot..."
  49. until ping dovecot -c1 > /dev/null; do
  50. sleep 1
  51. done
  52. log_f "Dovecot OK"
  53. ACME_BASE=/var/lib/acme
  54. SSL_EXAMPLE=/var/lib/ssl-example
  55. mkdir -p ${ACME_BASE}/acme
  56. # Migrate
  57. [[ -f ${ACME_BASE}/acme/private/privkey.pem ]] && mv ${ACME_BASE}/acme/private/privkey.pem ${ACME_BASE}/acme/key.pem
  58. [[ -f ${ACME_BASE}/acme/private/account.key ]] && mv ${ACME_BASE}/acme/private/account.key ${ACME_BASE}/acme/account.pem
  59. if [[ -f ${ACME_BASE}/acme/key.pem && -f ${ACME_BASE}/acme/cert.pem ]]; then
  60. if verify_hash_match ${ACME_BASE}/acme/cert.pem ${ACME_BASE}/acme/key.pem; then
  61. log_f "Migrating to SNI folder structure..."
  62. CERT_DOMAIN=($(openssl x509 -noout -text -in ${ACME_BASE}/acme/cert.pem | grep "Subject:" | sed -e 's/\(Subject:\)\|\(CN = \)\|\(CN=\)//g' | sed -e 's/^[[:space:]]*//'))
  63. CERT_DOMAINS=(${CERT_DOMAIN} $(openssl x509 -noout -text -in ${ACME_BASE}/acme/cert.pem | grep "DNS:" | sed -e 's/\(DNS:\)\|,//g' | sed "s/${CERT_DOMAIN}//" | sed -e 's/^[[:space:]]*//'))
  64. mkdir -p ${ACME_BASE}/${CERT_DOMAIN}
  65. mv ${ACME_BASE}/acme/cert.pem ${ACME_BASE}/${CERT_DOMAIN}/cert.pem
  66. # key is only copied, not moved, because it is used by all other requests too
  67. cp ${ACME_BASE}/acme/key.pem ${ACME_BASE}/${CERT_DOMAIN}/key.pem
  68. chmod 600 ${ACME_BASE}/${CERT_DOMAIN}/key.pem
  69. echo -n ${CERT_DOMAINS[*]} > ${ACME_BASE}/${CERT_DOMAIN}/domains
  70. mv ${ACME_BASE}/acme/acme.csr ${ACME_BASE}/${CERT_DOMAIN}/acme.csr
  71. log_f "OK" no_date
  72. fi
  73. fi
  74. [[ ! -f ${ACME_BASE}/dhparams.pem ]] && cp ${SSL_EXAMPLE}/dhparams.pem ${ACME_BASE}/dhparams.pem
  75. if [[ -f ${ACME_BASE}/cert.pem ]] && [[ -f ${ACME_BASE}/key.pem ]] && [[ $(stat -c%s ${ACME_BASE}/cert.pem) != 0 ]]; then
  76. ISSUER=$(openssl x509 -in ${ACME_BASE}/cert.pem -noout -issuer)
  77. if [[ ${ISSUER} != *"Let's Encrypt"* && ${ISSUER} != *"mailcow"* && ${ISSUER} != *"Fake LE Intermediate"* ]]; then
  78. log_f "Found certificate with issuer other than mailcow snake-oil CA and Let's Encrypt, skipping ACME client..."
  79. sleep 3650d
  80. exec $(readlink -f "$0")
  81. fi
  82. else
  83. if [[ -f ${ACME_BASE}/${MAILCOW_HOSTNAME}/cert.pem ]] && [[ -f ${ACME_BASE}/${MAILCOW_HOSTNAME}/key.pem ]] && verify_hash_match ${ACME_BASE}/${MAILCOW_HOSTNAME}/cert.pem ${ACME_BASE}/${MAILCOW_HOSTNAME}/key.pem; then
  84. log_f "Restoring previous acme certificate and restarting script..."
  85. cp ${ACME_BASE}/${MAILCOW_HOSTNAME}/cert.pem ${ACME_BASE}/cert.pem
  86. cp ${ACME_BASE}/${MAILCOW_HOSTNAME}/key.pem ${ACME_BASE}/key.pem
  87. # Restarting with env var set to trigger a restart,
  88. exec env TRIGGER_RESTART=1 $(readlink -f "$0")
  89. else
  90. log_f "Restoring mailcow snake-oil certificates and restarting script..."
  91. cp ${SSL_EXAMPLE}/cert.pem ${ACME_BASE}/cert.pem
  92. cp ${SSL_EXAMPLE}/key.pem ${ACME_BASE}/key.pem
  93. exec env TRIGGER_RESTART=1 $(readlink -f "$0")
  94. fi
  95. fi
  96. chmod 600 ${ACME_BASE}/key.pem
  97. log_f "Waiting for database..."
  98. while ! mysqladmin status --socket=/var/run/mysqld/mysqld.sock -u${DBUSER} -p${DBPASS} --silent > /dev/null; do
  99. sleep 2
  100. done
  101. log_f "Database OK"
  102. log_f "Waiting for Nginx..."
  103. until $(curl --output /dev/null --silent --head --fail http://nginx:8081); do
  104. sleep 2
  105. done
  106. log_f "Nginx OK"
  107. log_f "Waiting for resolver..."
  108. until dig letsencrypt.org +time=3 +tries=1 @unbound > /dev/null; do
  109. sleep 2
  110. done
  111. log_f "Resolver OK"
  112. # Waiting for domain table
  113. log_f "Waiting for domain table..."
  114. while [[ -z ${DOMAIN_TABLE} ]]; do
  115. curl --silent http://nginx/ >/dev/null 2>&1
  116. DOMAIN_TABLE=$(mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "SHOW TABLES LIKE 'domain'" -Bs)
  117. [[ -z ${DOMAIN_TABLE} ]] && sleep 10
  118. done
  119. log_f "OK" no_date
  120. log_f "Initializing, please wait..."
  121. while true; do
  122. POSTFIX_CERT_SERIAL="$(echo | openssl s_client -connect postfix:25 -starttls smtp 2>/dev/null | openssl x509 -inform pem -noout -serial | cut -d "=" -f 2)"
  123. DOVECOT_CERT_SERIAL="$(echo | openssl s_client -connect dovecot:143 -starttls imap 2>/dev/null | openssl x509 -inform pem -noout -serial | cut -d "=" -f 2)"
  124. POSTFIX_CERT_SERIAL_NEW="$(echo | openssl s_client -connect postfix:25 -starttls smtp 2>/dev/null | openssl x509 -inform pem -noout -serial | cut -d "=" -f 2)"
  125. DOVECOT_CERT_SERIAL_NEW="$(echo | openssl s_client -connect dovecot:143 -starttls imap 2>/dev/null | openssl x509 -inform pem -noout -serial | cut -d "=" -f 2)"
  126. # Re-using previous acme-mailcow account and domain keys
  127. if [[ ! -f ${ACME_BASE}/acme/key.pem ]]; then
  128. log_f "Generating missing domain private rsa key..."
  129. openssl genrsa 4096 > ${ACME_BASE}/acme/key.pem
  130. else
  131. log_f "Using existing domain rsa key ${ACME_BASE}/acme/key.pem"
  132. fi
  133. if [[ ! -f ${ACME_BASE}/acme/account.pem ]]; then
  134. log_f "Generating missing Lets Encrypt account key..."
  135. if [[ ! -z ${ACME_CONTACT} ]]; then
  136. if ! verify_email "${ACME_CONTACT}"; then
  137. log_f "Invalid email address, will not start registration!"
  138. sleep 365d
  139. exec $(readlink -f "$0")
  140. else
  141. ACME_CONTACT_PARAMETER="--contact mailto:${ACME_CONTACT}"
  142. log_f "Valid email address, using ${ACME_CONTACT} for registration"
  143. fi
  144. else
  145. ACME_CONTACT_PARAMETER=""
  146. fi
  147. openssl genrsa 4096 > ${ACME_BASE}/acme/account.pem
  148. else
  149. log_f "Using existing Lets Encrypt account key ${ACME_BASE}/acme/account.pem"
  150. fi
  151. chmod 600 ${ACME_BASE}/acme/key.pem
  152. chmod 600 ${ACME_BASE}/acme/account.pem
  153. unset EXISTING_CERTS
  154. declare -a EXISTING_CERTS
  155. for cert_dir in ${ACME_BASE}/*/ ; do
  156. if [[ ! -f ${cert_dir}domains ]] || [[ ! -f ${cert_dir}cert.pem ]] || [[ ! -f ${cert_dir}key.pem ]]; then
  157. continue
  158. fi
  159. EXISTING_CERTS+=("$(basename ${cert_dir})")
  160. done
  161. # Cleaning up and init validation arrays
  162. unset SQL_DOMAIN_ARR
  163. unset VALIDATED_CONFIG_DOMAINS
  164. unset ADDITIONAL_VALIDATED_SAN
  165. unset ADDITIONAL_WC_ARR
  166. unset ADDITIONAL_SAN_ARR
  167. unset CERT_ERRORS
  168. unset CERT_CHANGED
  169. unset CERT_AMOUNT_CHANGED
  170. unset VALIDATED_CERTIFICATES
  171. CERT_ERRORS=0
  172. CERT_CHANGED=0
  173. CERT_AMOUNT_CHANGED=0
  174. declare -a SQL_DOMAIN_ARR
  175. declare -a VALIDATED_CONFIG_DOMAINS
  176. declare -a ADDITIONAL_VALIDATED_SAN
  177. declare -a ADDITIONAL_WC_ARR
  178. declare -a ADDITIONAL_SAN_ARR
  179. declare -a VALIDATED_CERTIFICATES
  180. IFS=',' read -r -a TMP_ARR <<< "${ADDITIONAL_SAN}"
  181. for i in "${TMP_ARR[@]}" ; do
  182. if [[ "$i" =~ \.\*$ ]]; then
  183. ADDITIONAL_WC_ARR+=(${i::-2})
  184. else
  185. ADDITIONAL_SAN_ARR+=($i)
  186. fi
  187. done
  188. ADDITIONAL_WC_ARR+=('autodiscover' 'autoconfig')
  189. if [[ ${SKIP_IP_CHECK} != "y" ]]; then
  190. # Start IP detection
  191. log_f "Detecting IP addresses..."
  192. IPV4=$(get_ipv4)
  193. IPV6=$(get_ipv6)
  194. log_f "OK: ${IPV4}, ${IPV6:-"0000:0000:0000:0000:0000:0000:0000:0000"}"
  195. fi
  196. #########################################
  197. # IP and webroot challenge verification #
  198. SQL_DOMAINS=$(mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "SELECT domain FROM domain WHERE backupmx=0 and active=1" -Bs)
  199. if [[ ! $? -eq 0 ]]; then
  200. log_f "Failed to read SQL domains, retrying in 1 minute..."
  201. sleep 1m
  202. exec $(readlink -f "$0")
  203. fi
  204. while read domains; do
  205. if [[ -z "${domains}" ]]; then
  206. # ignore empty lines
  207. continue
  208. fi
  209. SQL_DOMAIN_ARR+=("${domains}")
  210. done <<< "${SQL_DOMAINS}"
  211. if [[ ${ONLY_MAILCOW_HOSTNAME} != "y" ]]; then
  212. for SQL_DOMAIN in "${SQL_DOMAIN_ARR[@]}"; do
  213. unset VALIDATED_CONFIG_DOMAINS_SUBDOMAINS
  214. declare -a VALIDATED_CONFIG_DOMAINS_SUBDOMAINS
  215. for SUBDOMAIN in "${ADDITIONAL_WC_ARR[@]}"; do
  216. if [[ "${SUBDOMAIN}.${SQL_DOMAIN}" != "${MAILCOW_HOSTNAME}" ]]; then
  217. if check_domain "${SUBDOMAIN}.${SQL_DOMAIN}"; then
  218. VALIDATED_CONFIG_DOMAINS_SUBDOMAINS+=("${SUBDOMAIN}.${SQL_DOMAIN}")
  219. fi
  220. fi
  221. done
  222. VALIDATED_CONFIG_DOMAINS+=("${VALIDATED_CONFIG_DOMAINS_SUBDOMAINS[*]}")
  223. done
  224. fi
  225. if check_domain ${MAILCOW_HOSTNAME}; then
  226. VALIDATED_MAILCOW_HOSTNAME="${MAILCOW_HOSTNAME}"
  227. fi
  228. if [[ ${ONLY_MAILCOW_HOSTNAME} != "y" ]]; then
  229. for SAN in "${ADDITIONAL_SAN_ARR[@]}"; do
  230. # Skip on CAA errors for SAN
  231. SAN_PARENT_DOMAIN=$(echo ${SAN} | cut -d. -f2-)
  232. SAN_CAAS=( $(dig CAA ${SAN_PARENT_DOMAIN} +short | sed -n 's/\d issue "\(.*\)"/\1/p') )
  233. if [[ ! -z ${SAN_CAAS} ]]; then
  234. if [[ ${SAN_CAAS[@]} =~ "letsencrypt.org" ]]; then
  235. log_f "Validated CAA for parent domain ${SAN_PARENT_DOMAIN} of ${SAN}"
  236. else
  237. log_f "Skipping ACME validation for ${SAN}: Lets Encrypt disallowed for ${SAN} by CAA record"
  238. continue
  239. fi
  240. fi
  241. if [[ ${SAN} == ${MAILCOW_HOSTNAME} ]]; then
  242. continue
  243. fi
  244. if check_domain ${SAN}; then
  245. ADDITIONAL_VALIDATED_SAN+=("${SAN}")
  246. fi
  247. done
  248. fi
  249. # Unique domains for server certificate
  250. if [[ ${ENABLE_SSL_SNI} == "y" ]]; then
  251. # create certificate for server name and fqdn SANs only
  252. SERVER_SAN_VALIDATED=(${VALIDATED_MAILCOW_HOSTNAME} $(echo ${ADDITIONAL_VALIDATED_SAN[*]} | xargs -n1 | sort -u | xargs))
  253. else
  254. # create certificate for all domains, including all subdomains from other domains [*]
  255. SERVER_SAN_VALIDATED=(${VALIDATED_MAILCOW_HOSTNAME} $(echo ${VALIDATED_CONFIG_DOMAINS[*]} ${ADDITIONAL_VALIDATED_SAN[*]} | xargs -n1 | sort -u | xargs))
  256. fi
  257. if [[ ! -z ${SERVER_SAN_VALIDATED[*]} ]]; then
  258. CERT_NAME=${SERVER_SAN_VALIDATED[0]}
  259. VALIDATED_CERTIFICATES+=("${CERT_NAME}")
  260. # obtain server certificate if required
  261. ACME_CONTACT_PARAMETER=${ACME_CONTACT_PARAMETER} DOMAINS=${SERVER_SAN_VALIDATED[@]} /srv/obtain-certificate.sh rsa
  262. RETURN="$?"
  263. if [[ "$RETURN" == "0" ]]; then # 0 = cert created successfully
  264. CERT_AMOUNT_CHANGED=1
  265. CERT_CHANGED=1
  266. elif [[ "$RETURN" == "1" ]]; then # 1 = cert renewed successfully
  267. CERT_CHANGED=1
  268. elif [[ "$RETURN" == "2" ]]; then # 2 = cert not due for renewal
  269. :
  270. else
  271. CERT_ERRORS=1
  272. fi
  273. # copy hostname certificate to default/server certificate
  274. # do not a key when cert is missing, this can lead to a mismatch of cert/key
  275. if [[ -f ${ACME_BASE}/${CERT_NAME}/cert.pem ]]; then
  276. cp ${ACME_BASE}/${CERT_NAME}/cert.pem ${ACME_BASE}/cert.pem
  277. cp ${ACME_BASE}/${CERT_NAME}/key.pem ${ACME_BASE}/key.pem
  278. fi
  279. fi
  280. # individual certificates for SNI [@]
  281. if [[ ${ENABLE_SSL_SNI} == "y" ]]; then
  282. for VALIDATED_DOMAINS in "${VALIDATED_CONFIG_DOMAINS[@]}"; do
  283. VALIDATED_DOMAINS_ARR=(${VALIDATED_DOMAINS})
  284. unset VALIDATED_DOMAINS_SORTED
  285. declare -a VALIDATED_DOMAINS_SORTED
  286. VALIDATED_DOMAINS_SORTED=(${VALIDATED_DOMAINS_ARR[0]} $(echo ${VALIDATED_DOMAINS_ARR[@]:1} | xargs -n1 | sort -u | xargs))
  287. # remove all domain names that are already inside the server certificate (SERVER_SAN_VALIDATED)
  288. for domain in "${SERVER_SAN_VALIDATED[@]}"; do
  289. for i in "${!VALIDATED_DOMAINS_SORTED[@]}"; do
  290. if [[ ${VALIDATED_DOMAINS_SORTED[i]} = $domain ]]; then
  291. unset 'VALIDATED_DOMAINS_SORTED[i]'
  292. fi
  293. done
  294. done
  295. if [[ ! -z ${VALIDATED_DOMAINS_SORTED[*]} ]]; then
  296. CERT_NAME=${VALIDATED_DOMAINS_SORTED[0]}
  297. VALIDATED_CERTIFICATES+=("${CERT_NAME}")
  298. # obtain certificate if required
  299. DOMAINS=${VALIDATED_DOMAINS_SORTED[@]} /srv/obtain-certificate.sh rsa
  300. RETURN="$?"
  301. if [[ "$RETURN" == "0" ]]; then # 0 = cert created successfully
  302. CERT_AMOUNT_CHANGED=1
  303. CERT_CHANGED=1
  304. elif [[ "$RETURN" == "1" ]]; then # 1 = cert renewed successfully
  305. CERT_CHANGED=1
  306. elif [[ "$RETURN" == "2" ]]; then # 2 = cert not due for renewal
  307. :
  308. else
  309. CERT_ERRORS=1
  310. fi
  311. fi
  312. done
  313. fi
  314. if [[ -z ${VALIDATED_CERTIFICATES[*]} ]]; then
  315. log_f "Cannot validate any hostnames, skipping Let's Encrypt for 1 hour."
  316. log_f "Use SKIP_LETS_ENCRYPT=y in mailcow.conf to skip it permanently."
  317. ${REDIS_CMDLINE} SET ACME_FAIL_TIME "$(date +%s)"
  318. sleep 1h
  319. exec $(readlink -f "$0")
  320. fi
  321. # find orphaned certificates if no errors occurred
  322. if [[ "${CERT_ERRORS}" == "0" ]]; then
  323. for EXISTING_CERT in "${EXISTING_CERTS[@]}"; do
  324. if [[ ! "`printf '_%s_\n' "${VALIDATED_CERTIFICATES[@]}"`" == *"_${EXISTING_CERT}_"* ]]; then
  325. DATE=$(date +%Y-%m-%d_%H_%M_%S)
  326. log_f "Found orphaned certificate: ${EXISTING_CERT} - archiving it at ${ACME_BASE}/backups/${EXISTING_CERT}/"
  327. BACKUP_DIR=${ACME_BASE}/backups/${EXISTING_CERT}/${DATE}
  328. # archive rsa cert and any other files
  329. mkdir -p ${ACME_BASE}/backups/${EXISTING_CERT}
  330. mv ${ACME_BASE}/${EXISTING_CERT} ${BACKUP_DIR}
  331. CERT_CHANGED=1
  332. CERT_AMOUNT_CHANGED=1
  333. fi
  334. done
  335. fi
  336. # reload on new or changed certificates
  337. if [[ "${CERT_CHANGED}" == "1" ]]; then
  338. rm -f "${ACME_BASE}/force_renew" 2> /dev/null
  339. RELOAD_LOOP_C=1
  340. while [[ "${POSTFIX_CERT_SERIAL}" == "${POSTFIX_CERT_SERIAL_NEW}" ]] || [[ "${DOVECOT_CERT_SERIAL}" == "${DOVECOT_CERT_SERIAL_NEW}" ]] || [[ ${#POSTFIX_CERT_SERIAL_NEW} -ne 36 ]] || [[ ${#DOVECOT_CERT_SERIAL_NEW} -ne 36 ]]; do
  341. log_f "Reloading or restarting services... (${RELOAD_LOOP_C})"
  342. RELOAD_LOOP_C=$((RELOAD_LOOP_C + 1))
  343. CERT_AMOUNT_CHANGED=${CERT_AMOUNT_CHANGED} /srv/reload-configurations.sh
  344. log_f "Waiting for containers to settle..."
  345. sleep 10
  346. until nc -z dovecot 143; do
  347. sleep 1
  348. done
  349. until nc -z postfix 25; do
  350. sleep 1
  351. done
  352. POSTFIX_CERT_SERIAL_NEW="$(echo | openssl s_client -connect postfix:25 -starttls smtp 2>/dev/null | openssl x509 -inform pem -noout -serial | cut -d "=" -f 2)"
  353. DOVECOT_CERT_SERIAL_NEW="$(echo | openssl s_client -connect dovecot:143 -starttls imap 2>/dev/null | openssl x509 -inform pem -noout -serial | cut -d "=" -f 2)"
  354. if [[ ${RELOAD_LOOP_C} -gt 3 ]]; then
  355. log_f "Some services do return old end dates, something went wrong!"
  356. ${REDIS_CMDLINE} SET ACME_FAIL_TIME "$(date +%s)"
  357. break;
  358. fi
  359. done
  360. fi
  361. case "$CERT_ERRORS" in
  362. 0) # all successful
  363. if [[ "${CERT_CHANGED}" == "1" ]]; then
  364. if [[ "${CERT_AMOUNT_CHANGED}" == "1" ]]; then
  365. log_f "Certificates successfully requested and renewed where required, sleeping one day"
  366. else
  367. log_f "Certificates were successfully renewed where required, sleeping for another day."
  368. fi
  369. else
  370. log_f "Certificates were successfully validated, no changes or renewals required, sleeping for another day."
  371. fi
  372. sleep 1d
  373. ;;
  374. *) # non-zero
  375. log_f "Some errors occurred, retrying in 30 minutes..."
  376. ${REDIS_CMDLINE} SET ACME_FAIL_TIME "$(date +%s)"
  377. sleep 30m
  378. exec $(readlink -f "$0")
  379. ;;
  380. esac
  381. done