You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
6.5 KiB

  1. #!/bin/sh
  2. # Wrapper for acme.sh to work on openwrt.
  3. #
  4. # This program is free software; you can redistribute it and/or modify it under
  5. # the terms of the GNU General Public License as published by the Free Software
  6. # Foundation; either version 3 of the License, or (at your option) any later
  7. # version.
  8. #
  9. # Author: Toke Høiland-Jørgensen <toke@toke.dk>
  10. CHECK_CRON=$1
  11. ACME=/usr/lib/acme/acme.sh
  12. export CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
  13. export NO_TIMESTAMP=1
  14. UHTTPD_LISTEN_HTTP=
  15. STATE_DIR='/etc/acme'
  16. ACCOUNT_EMAIL=
  17. DEBUG=0
  18. . /lib/functions.sh
  19. check_cron()
  20. {
  21. [ -f "/etc/crontabs/root" ] && grep -q '/etc/init.d/acme' /etc/crontabs/root && return
  22. echo "0 0 * * * /etc/init.d/acme start" >> /etc/crontabs/root
  23. /etc/init.d/cron start
  24. }
  25. log()
  26. {
  27. logger -t acme -s -p daemon.info "$@"
  28. }
  29. err()
  30. {
  31. logger -t acme -s -p daemon.err "$@"
  32. }
  33. debug()
  34. {
  35. [ "$DEBUG" -eq "1" ] && logger -t acme -s -p daemon.debug "$@"
  36. }
  37. get_listeners()
  38. {
  39. netstat -nptl 2>/dev/null | awk 'match($4, /:80$/){split($7, parts, "/"); print parts[2];}' | uniq | tr "\n" " "
  40. }
  41. pre_checks()
  42. {
  43. main_domain="$1"
  44. log "Running pre checks for $main_domain."
  45. listeners="$(get_listeners)"
  46. debug "port80 listens: $listeners"
  47. case "$listeners" in
  48. "uhttpd")
  49. debug "Found uhttpd listening on port 80; trying to disable."
  50. UHTTPD_LISTEN_HTTP=$(uci get uhttpd.main.listen_http)
  51. if [ -z "$UHTTPD_LISTEN_HTTP" ]; then
  52. err "$main_domain: Unable to find uhttpd listen config."
  53. err "Manually disable uhttpd or set webroot to continue."
  54. return 1
  55. fi
  56. uci set uhttpd.main.listen_http=''
  57. uci commit uhttpd || return 1
  58. if ! /etc/init.d/uhttpd reload ; then
  59. uci set uhttpd.main.listen_http="$UHTTPD_LISTEN_HTTP"
  60. uci commit uhttpd
  61. return 1
  62. fi
  63. ;;
  64. "")
  65. debug "Nothing listening on port 80."
  66. ;;
  67. *)
  68. err "$main_domain: Cannot run in standalone mode; another daemon is listening on port 80."
  69. err "Disable other daemon or set webroot to continue."
  70. return 1
  71. ;;
  72. esac
  73. iptables -I input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" || return 1
  74. ip6tables -I input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" || return 1
  75. debug "v4 input_rule: $(iptables -nvL input_rule)"
  76. debug "v6 input_rule: $(ip6tables -nvL input_rule)"
  77. return 0
  78. }
  79. post_checks()
  80. {
  81. log "Running post checks (cleanup)."
  82. # The comment ensures we only touch our own rules. If no rules exist, that
  83. # is fine, so hide any errors
  84. iptables -D input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" 2>/dev/null
  85. ip6tables -D input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" 2>/dev/null
  86. if [ -e /etc/init.d/uhttpd ] && [ -n "$UHTTPD_LISTEN_HTTP" ]; then
  87. uci set uhttpd.main.listen_http="$UHTTPD_LISTEN_HTTP"
  88. uci commit uhttpd
  89. /etc/init.d/uhttpd reload
  90. UHTTPD_LISTEN_HTTP=
  91. fi
  92. }
  93. err_out()
  94. {
  95. post_checks
  96. exit 1
  97. }
  98. int_out()
  99. {
  100. post_checks
  101. trap - INT
  102. kill -INT $$
  103. }
  104. is_staging()
  105. {
  106. local main_domain="$1"
  107. grep -q "acme-staging" "$STATE_DIR/$main_domain/${main_domain}.conf"
  108. return $?
  109. }
  110. issue_cert()
  111. {
  112. local section="$1"
  113. local acme_args=
  114. local enabled
  115. local use_staging
  116. local update_uhttpd
  117. local keylength
  118. local domains
  119. local main_domain
  120. local moved_staging=0
  121. local failed_dir
  122. local webroot
  123. config_get_bool enabled "$section" enabled 0
  124. config_get_bool use_staging "$section" use_staging
  125. config_get_bool update_uhttpd "$section" update_uhttpd
  126. config_get domains "$section" domains
  127. config_get keylength "$section" keylength
  128. config_get webroot "$section" webroot
  129. [ "$enabled" -eq "1" ] || return
  130. [ "$DEBUG" -eq "1" ] && acme_args="$acme_args --debug"
  131. set -- $domains
  132. main_domain=$1
  133. [ -n "$webroot" ] || pre_checks "$main_domain" || return 1
  134. log "Running ACME for $main_domain"
  135. if [ -e "$STATE_DIR/$main_domain" ]; then
  136. if [ "$use_staging" -eq "0" ] && is_staging "$main_domain"; then
  137. log "Found previous cert issued using staging server. Moving it out of the way."
  138. mv "$STATE_DIR/$main_domain" "$STATE_DIR/$main_domain.staging"
  139. moved_staging=1
  140. else
  141. log "Found previous cert config. Issuing renew."
  142. $ACME --home "$STATE_DIR" --renew -d "$main_domain" $acme_args || return 1
  143. return 0
  144. fi
  145. fi
  146. acme_args="$acme_args $(for d in $domains; do echo -n "-d $d "; done)"
  147. acme_args="$acme_args --keylength $keylength"
  148. [ -n "$ACCOUNT_EMAIL" ] && acme_args="$acme_args --accountemail $ACCOUNT_EMAIL"
  149. [ "$use_staging" -eq "1" ] && acme_args="$acme_args --staging"
  150. if [ -z "$webroot" ]; then
  151. log "Using standalone mode"
  152. acme_args="$acme_args --standalone"
  153. else
  154. if [ ! -d "$webroot" ]; then
  155. err "$main_domain: Webroot dir '$webroot' does not exist!"
  156. return 1
  157. fi
  158. log "Using webroot dir: $webroot"
  159. acme_args="$acme_args --webroot $webroot"
  160. fi
  161. if ! $ACME --home "$STATE_DIR" --issue $acme_args; then
  162. failed_dir="$STATE_DIR/${main_domain}.failed-$(date +%s)"
  163. err "Issuing cert for $main_domain failed. Moving state to $failed_dir"
  164. [ -d "$STATE_DIR/$main_domain" ] && mv "$STATE_DIR/$main_domain" "$failed_dir"
  165. if [ "$moved_staging" -eq "1" ]; then
  166. err "Restoring staging certificate"
  167. mv "$STATE_DIR/${main_domain}.staging" "$STATE_DIR/${main_domain}"
  168. fi
  169. return 1
  170. fi
  171. if [ "$update_uhttpd" -eq "1" ]; then
  172. uci set uhttpd.main.key="$STATE_DIR/${main_domain}/${main_domain}.key"
  173. uci set uhttpd.main.cert="$STATE_DIR/${main_domain}/fullchain.cer"
  174. # commit and reload is in post_checks
  175. fi
  176. post_checks
  177. }
  178. load_vars()
  179. {
  180. local section="$1"
  181. STATE_DIR=$(config_get "$section" state_dir)
  182. ACCOUNT_EMAIL=$(config_get "$section" account_email)
  183. DEBUG=$(config_get "$section" debug)
  184. }
  185. check_cron
  186. [ -n "$CHECK_CRON" ] && exit 0
  187. config_load acme
  188. config_foreach load_vars acme
  189. if [ -z "$STATE_DIR" ] || [ -z "$ACCOUNT_EMAIL" ]; then
  190. err "state_dir and account_email must be set"
  191. exit 1
  192. fi
  193. [ -d "$STATE_DIR" ] || mkdir -p "$STATE_DIR"
  194. trap err_out HUP TERM
  195. trap int_out INT
  196. config_foreach issue_cert cert
  197. exit 0