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.

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