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.

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