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.

320 lines
8.9 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. NGINX_WEBSERVER=0
  19. UPDATE_NGINX=0
  20. UPDATE_UHTTPD=0
  21. . /lib/functions.sh
  22. check_cron()
  23. {
  24. [ -f "/etc/crontabs/root" ] && grep -q '/etc/init.d/acme' /etc/crontabs/root && return
  25. echo "0 0 * * * /etc/init.d/acme start" >> /etc/crontabs/root
  26. /etc/init.d/cron start
  27. }
  28. log()
  29. {
  30. logger -t acme -s -p daemon.info "$@"
  31. }
  32. err()
  33. {
  34. logger -t acme -s -p daemon.err "$@"
  35. }
  36. debug()
  37. {
  38. [ "$DEBUG" -eq "1" ] && logger -t acme -s -p daemon.debug "$@"
  39. }
  40. get_listeners() {
  41. local proto rq sq listen remote state program
  42. netstat -nptl 2>/dev/null | while read proto rq sq listen remote state program; do
  43. case "$proto#$listen#$program" in
  44. tcp#*:80#[0-9]*/*) echo -n "${program%% *} " ;;
  45. esac
  46. done
  47. }
  48. run_acme()
  49. {
  50. debug "Running acme.sh as '$ACME $@'"
  51. $ACME "$@"
  52. }
  53. pre_checks()
  54. {
  55. main_domain="$1"
  56. log "Running pre checks for $main_domain."
  57. listeners="$(get_listeners)"
  58. debug "port80 listens: $listeners"
  59. for listener in $(get_listeners); do
  60. pid="${listener%/*}"
  61. cmd="${listener#*/}"
  62. case "$cmd" in
  63. uhttpd)
  64. debug "Found uhttpd listening on port 80; trying to disable."
  65. UHTTPD_LISTEN_HTTP=$(uci get uhttpd.main.listen_http)
  66. if [ -z "$UHTTPD_LISTEN_HTTP" ]; then
  67. err "$main_domain: Unable to find uhttpd listen config."
  68. err "Manually disable uhttpd or set webroot to continue."
  69. return 1
  70. fi
  71. uci set uhttpd.main.listen_http=''
  72. uci commit uhttpd || return 1
  73. if ! /etc/init.d/uhttpd reload ; then
  74. uci set uhttpd.main.listen_http="$UHTTPD_LISTEN_HTTP"
  75. uci commit uhttpd
  76. return 1
  77. fi
  78. ;;
  79. nginx*)
  80. debug "Found nginx listening on port 80; trying to disable."
  81. NGINX_WEBSERVER=1
  82. local tries=0
  83. while grep -sq "$cmd" "/proc/$pid/cmdline" && kill -0 "$pid"; do
  84. /etc/init.d/nginx stop
  85. if [ $tries -gt 10 ]; then
  86. debug "Can't stop nginx. Terminating script."
  87. return 1
  88. fi
  89. debug "Waiting for nginx to stop..."
  90. tries=$((tries + 1))
  91. sleep 1
  92. done
  93. ;;
  94. "")
  95. debug "Nothing listening on port 80."
  96. ;;
  97. *)
  98. err "$main_domain: Cannot run in standalone mode; another daemon is listening on port 80."
  99. err "Disable other daemon or set webroot to continue."
  100. return 1
  101. ;;
  102. esac
  103. done
  104. iptables -I input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" || return 1
  105. ip6tables -I input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" || return 1
  106. debug "v4 input_rule: $(iptables -nvL input_rule)"
  107. debug "v6 input_rule: $(ip6tables -nvL input_rule)"
  108. return 0
  109. }
  110. post_checks()
  111. {
  112. log "Running post checks (cleanup)."
  113. # The comment ensures we only touch our own rules. If no rules exist, that
  114. # is fine, so hide any errors
  115. iptables -D input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" 2>/dev/null
  116. ip6tables -D input_rule -p tcp --dport 80 -j ACCEPT -m comment --comment "ACME" 2>/dev/null
  117. if [ -e /etc/init.d/uhttpd ] && ( [ -n "$UHTTPD_LISTEN_HTTP" ] || [ "$UPDATE_UHTTPD" -eq 1 ] ); then
  118. if [ -n "$UHTTPD_LISTEN_HTTP" ]; then
  119. uci set uhttpd.main.listen_http="$UHTTPD_LISTEN_HTTP"
  120. UHTTPD_LISTEN_HTTP=
  121. fi
  122. uci commit uhttpd
  123. /etc/init.d/uhttpd reload
  124. fi
  125. if [ -e /etc/init.d/nginx ] && ( [ "$NGINX_WEBSERVER" -eq 1 ] || [ "$UPDATE_NGINX" -eq 1 ] ); then
  126. NGINX_WEBSERVER=0
  127. /etc/init.d/nginx restart
  128. fi
  129. }
  130. err_out()
  131. {
  132. post_checks
  133. exit 1
  134. }
  135. int_out()
  136. {
  137. post_checks
  138. trap - INT
  139. kill -INT $$
  140. }
  141. is_staging()
  142. {
  143. local main_domain
  144. local domain_dir
  145. main_domain="$1"
  146. domain_dir="$2"
  147. grep -q "acme-staging" "${domain_dir}/${main_domain}.conf"
  148. return $?
  149. }
  150. issue_cert()
  151. {
  152. local section="$1"
  153. local acme_args=
  154. local enabled
  155. local use_staging
  156. local update_uhttpd
  157. local update_nginx
  158. local keylength
  159. local domains
  160. local main_domain
  161. local moved_staging=0
  162. local failed_dir
  163. local webroot
  164. local dns
  165. local ret
  166. local domain_dir
  167. config_get_bool enabled "$section" enabled 0
  168. config_get_bool use_staging "$section" use_staging
  169. config_get_bool update_uhttpd "$section" update_uhttpd
  170. config_get_bool update_nginx "$section" update_nginx
  171. config_get domains "$section" domains
  172. config_get keylength "$section" keylength
  173. config_get webroot "$section" webroot
  174. config_get dns "$section" dns
  175. UPDATE_NGINX=$update_nginx
  176. UPDATE_UHTTPD=$update_uhttpd
  177. [ "$enabled" -eq "1" ] || return
  178. [ "$DEBUG" -eq "1" ] && acme_args="$acme_args --debug"
  179. set -- $domains
  180. main_domain=$1
  181. [ -n "$webroot" ] || [ -n "$dns" ] || pre_checks "$main_domain" || return 1
  182. if echo $keylength | grep -q "^ec-"; then
  183. domain_dir="$STATE_DIR/${main_domain}_ecc"
  184. else
  185. domain_dir="$STATE_DIR/${main_domain}"
  186. fi
  187. log "Running ACME for $main_domain"
  188. handle_credentials() {
  189. local credential="$1"
  190. eval export $credential
  191. }
  192. config_list_foreach "$section" credentials handle_credentials
  193. if [ -e "$domain_dir" ]; then
  194. if [ "$use_staging" -eq "0" ] && is_staging "$main_domain" "$domain_dir"; then
  195. log "Found previous cert issued using staging server. Moving it out of the way."
  196. mv "$domain_dir" "${domain_dir}.staging"
  197. moved_staging=1
  198. else
  199. log "Found previous cert config. Issuing renew."
  200. run_acme --home "$STATE_DIR" --renew -d "$main_domain" $acme_args && ret=0 || ret=1
  201. post_checks
  202. return $ret
  203. fi
  204. fi
  205. acme_args="$acme_args $(for d in $domains; do echo -n "-d $d "; done)"
  206. acme_args="$acme_args --keylength $keylength"
  207. [ -n "$ACCOUNT_EMAIL" ] && acme_args="$acme_args --accountemail $ACCOUNT_EMAIL"
  208. [ "$use_staging" -eq "1" ] && acme_args="$acme_args --staging"
  209. if [ -n "$dns" ]; then
  210. log "Using dns mode"
  211. acme_args="$acme_args --dns $dns"
  212. elif [ -z "$webroot" ]; then
  213. log "Using standalone mode"
  214. acme_args="$acme_args --standalone --listen-v6"
  215. else
  216. if [ ! -d "$webroot" ]; then
  217. err "$main_domain: Webroot dir '$webroot' does not exist!"
  218. post_checks
  219. return 1
  220. fi
  221. log "Using webroot dir: $webroot"
  222. acme_args="$acme_args --webroot $webroot"
  223. fi
  224. if ! run_acme --home "$STATE_DIR" --issue $acme_args; then
  225. failed_dir="${domain_dir}.failed-$(date +%s)"
  226. err "Issuing cert for $main_domain failed. Moving state to $failed_dir"
  227. [ -d "$domain_dir" ] && mv "$domain_dir" "$failed_dir"
  228. if [ "$moved_staging" -eq "1" ]; then
  229. err "Restoring staging certificate"
  230. mv "${domain_dir}.staging" "${domain_dir}"
  231. fi
  232. post_checks
  233. return 1
  234. fi
  235. if [ -e /etc/init.d/uhttpd ] && [ "$update_uhttpd" -eq "1" ]; then
  236. uci set uhttpd.main.key="${domain_dir}/${main_domain}.key"
  237. uci set uhttpd.main.cert="${domain_dir}/fullchain.cer"
  238. # commit and reload is in post_checks
  239. fi
  240. if [ -e /etc/init.d/nginx ] && [ "$update_nginx" -eq "1" ]; then
  241. sed -i "s#ssl_certificate\ .*#ssl_certificate ${domain_dir}/fullchain.cer;#g" /etc/nginx/nginx.conf
  242. sed -i "s#ssl_certificate_key\ .*#ssl_certificate_key ${domain_dir}/${main_domain}.key;#g" /etc/nginx/nginx.conf
  243. # commit and reload is in post_checks
  244. fi
  245. post_checks
  246. }
  247. load_vars()
  248. {
  249. local section="$1"
  250. STATE_DIR=$(config_get "$section" state_dir)
  251. ACCOUNT_EMAIL=$(config_get "$section" account_email)
  252. DEBUG=$(config_get "$section" debug)
  253. }
  254. check_cron
  255. [ -n "$CHECK_CRON" ] && exit 0
  256. [ -e "/var/run/acme_boot" ] && rm -f "/var/run/acme_boot" && exit 0
  257. config_load acme
  258. config_foreach load_vars acme
  259. if [ -z "$STATE_DIR" ] || [ -z "$ACCOUNT_EMAIL" ]; then
  260. err "state_dir and account_email must be set"
  261. exit 1
  262. fi
  263. [ -d "$STATE_DIR" ] || mkdir -p "$STATE_DIR"
  264. trap err_out HUP TERM
  265. trap int_out INT
  266. config_foreach issue_cert cert
  267. exit 0