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.

392 lines
10 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. UPDATE_HAPROXY=0
  22. NFT_HANDLE=
  23. USER_CLEANUP=
  24. . /lib/functions.sh
  25. check_cron() {
  26. [ -f "/etc/crontabs/root" ] && grep -q '/etc/init.d/acme' /etc/crontabs/root && return
  27. echo "0 0 * * * /etc/init.d/acme start" >> /etc/crontabs/root
  28. /etc/init.d/cron start
  29. }
  30. log() {
  31. logger -t acme -s -p daemon.info -- "$@"
  32. }
  33. err() {
  34. logger -t acme -s -p daemon.err -- "$@"
  35. }
  36. debug() {
  37. [ "$DEBUG" -eq "1" ] && logger -t acme -s -p daemon.debug -- "$@"
  38. }
  39. get_listeners() {
  40. local proto rq sq listen remote state program
  41. netstat -nptl 2> /dev/null | while read -r proto rq sq listen remote state program; do
  42. case "$proto#$listen#$program" in
  43. tcp#*:80#[0-9]*/*) echo -n "${program%% *} " ;;
  44. esac
  45. done
  46. }
  47. run_acme() {
  48. debug "Running acme.sh as '$ACME $*'"
  49. $ACME "$@"
  50. }
  51. pre_checks() {
  52. main_domain="$1"
  53. log "Running pre checks for $main_domain."
  54. listeners="$(get_listeners)"
  55. debug "port80 listens: $listeners"
  56. for listener in $(get_listeners); do
  57. pid="${listener%/*}"
  58. cmd="$(basename $(readlink /proc/$pid/exe))"
  59. case "$cmd" in
  60. uhttpd)
  61. if [ -n "$UHTTPD_LISTEN_HTTP" ]; then
  62. debug "Already handled uhttpd; skipping"
  63. continue
  64. fi
  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. if [ "$NGINX_WEBSERVER" -eq "1" ]; then
  82. debug "Already handled nginx; skipping"
  83. continue
  84. fi
  85. debug "Found nginx listening on port 80; trying to disable."
  86. NGINX_WEBSERVER=1
  87. local tries=0
  88. while grep -sq "$cmd" "/proc/$pid/cmdline" && kill -0 "$pid"; do
  89. /etc/init.d/nginx stop
  90. if [ $tries -gt 10 ]; then
  91. debug "Can't stop nginx. Terminating script."
  92. return 1
  93. fi
  94. debug "Waiting for nginx to stop..."
  95. tries=$((tries + 1))
  96. sleep 1
  97. done
  98. ;;
  99. "")
  100. debug "Nothing listening on port 80."
  101. ;;
  102. *)
  103. err "$main_domain: Cannot run in standalone mode; another daemon is listening on port 80."
  104. err "Disable other daemon or set webroot to continue."
  105. return 1
  106. ;;
  107. esac
  108. done
  109. NFT_HANDLE=$(nft -a -e insert rule inet fw4 input tcp dport 80 counter accept comment ACME | grep -o 'handle [0-9]\+')
  110. ret=$?
  111. [ "$ret" -eq "0" ] || return 1
  112. debug "added nft rule: $NFT_HANDLE"
  113. return 0
  114. }
  115. post_checks() {
  116. log "Running post checks (cleanup)."
  117. # $NFT_HANDLE contains the string 'handle XX' so pass it unquoted to nft
  118. [ -n "$NFT_HANDLE" ] && nft delete rule inet fw4 input $NFT_HANDLE
  119. if [ -e /etc/init.d/uhttpd ] && { [ -n "$UHTTPD_LISTEN_HTTP" ] || [ "$UPDATE_UHTTPD" -eq 1 ]; }; then
  120. if [ -n "$UHTTPD_LISTEN_HTTP" ]; then
  121. uci set uhttpd.main.listen_http="$UHTTPD_LISTEN_HTTP"
  122. UHTTPD_LISTEN_HTTP=
  123. fi
  124. uci commit uhttpd
  125. /etc/init.d/uhttpd restart
  126. fi
  127. if [ -e /etc/init.d/nginx ] && { [ "$NGINX_WEBSERVER" -eq 1 ] || [ "$UPDATE_NGINX" -eq 1 ]; }; then
  128. NGINX_WEBSERVER=0
  129. /etc/init.d/nginx restart
  130. fi
  131. if [ -e /etc/init.d/haproxy ] && [ "$UPDATE_HAPROXY" -eq 1 ] ; then
  132. /etc/init.d/haproxy restart
  133. fi
  134. if [ -n "$USER_CLEANUP" ] && [ -f "$USER_CLEANUP" ]; then
  135. log "Running user-provided cleanup script from $USER_CLEANUP."
  136. "$USER_CLEANUP" || return 1
  137. fi
  138. }
  139. err_out() {
  140. post_checks
  141. exit 1
  142. }
  143. int_out() {
  144. post_checks
  145. trap - INT
  146. kill -INT $$
  147. }
  148. is_staging() {
  149. local main_domain
  150. local domain_dir
  151. main_domain="$1"
  152. domain_dir="$2"
  153. grep -q "acme-staging" "${domain_dir}/${main_domain}.conf"
  154. return $?
  155. }
  156. issue_cert() {
  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 update_haproxy
  164. local keylength
  165. local keylength_ecc=0
  166. local domains
  167. local main_domain
  168. local moved_staging=0
  169. local failed_dir
  170. local webroot
  171. local dns
  172. local user_setup
  173. local user_cleanup
  174. local ret
  175. local domain_dir
  176. local acme_server
  177. local days
  178. config_get_bool enabled "$section" enabled 0
  179. config_get_bool use_staging "$section" use_staging
  180. config_get_bool update_uhttpd "$section" update_uhttpd
  181. config_get_bool update_nginx "$section" update_nginx
  182. config_get_bool update_haproxy "$section" update_haproxy
  183. config_get calias "$section" calias
  184. config_get dalias "$section" dalias
  185. config_get domains "$section" domains
  186. config_get keylength "$section" keylength
  187. config_get webroot "$section" webroot
  188. config_get dns "$section" dns
  189. config_get user_setup "$section" user_setup
  190. config_get user_cleanup "$section" user_cleanup
  191. config_get acme_server "$section" acme_server
  192. config_get days "$section" days
  193. UPDATE_NGINX=$update_nginx
  194. UPDATE_UHTTPD=$update_uhttpd
  195. UPDATE_HAPROXY=$update_haproxy
  196. USER_CLEANUP=$user_cleanup
  197. [ "$enabled" -eq "1" ] || return
  198. [ "$DEBUG" -eq "1" ] && acme_args="$acme_args --debug"
  199. set -- $domains
  200. main_domain=$1
  201. if [ -n "$user_setup" ] && [ -f "$user_setup" ]; then
  202. log "Running user-provided setup script from $user_setup."
  203. "$user_setup" "$main_domain" || return 1
  204. else
  205. [ -n "$webroot" ] || [ -n "$dns" ] || pre_checks "$main_domain" || return 1
  206. fi
  207. if echo "$keylength" | grep -q "^ec-"; then
  208. domain_dir="$STATE_DIR/${main_domain}_ecc"
  209. keylength_ecc=1
  210. else
  211. domain_dir="$STATE_DIR/${main_domain}"
  212. fi
  213. log "Running ACME for $main_domain"
  214. handle_credentials() {
  215. local credential="$1"
  216. eval export $credential
  217. }
  218. config_list_foreach "$section" credentials handle_credentials
  219. if [ -e "$domain_dir" ]; then
  220. if [ "$use_staging" -eq "0" ] && is_staging "$main_domain" "$domain_dir"; then
  221. log "Found previous cert issued using staging server. Moving it out of the way."
  222. mv "$domain_dir" "${domain_dir}.staging"
  223. moved_staging=1
  224. else
  225. log "Found previous cert config. Issuing renew."
  226. [ "$keylength_ecc" -eq "1" ] && acme_args="$acme_args --ecc"
  227. run_acme --home "$STATE_DIR" --renew -d "$main_domain" $acme_args && ret=0 || ret=1
  228. post_checks
  229. return $ret
  230. fi
  231. fi
  232. acme_args="$acme_args $(for d in $domains; do echo -n "-d $d "; done)"
  233. acme_args="$acme_args --keylength $keylength"
  234. [ -n "$ACCOUNT_EMAIL" ] && acme_args="$acme_args --accountemail $ACCOUNT_EMAIL"
  235. if [ -n "$acme_server" ]; then
  236. log "Using custom ACME server URL"
  237. acme_args="$acme_args --server $acme_server"
  238. else
  239. # default to letsencrypt because the upstream default may change
  240. if [ "$use_staging" -eq "1" ]; then
  241. acme_args="$acme_args --server letsencrypt_test"
  242. else
  243. acme_args="$acme_args --server letsencrypt"
  244. fi
  245. fi
  246. if [ -n "$days" ]; then
  247. log "Renewing after $days days"
  248. acme_args="$acme_args --days $days"
  249. fi
  250. if [ -n "$dns" ]; then
  251. log "Using dns mode"
  252. acme_args="$acme_args --dns $dns"
  253. if [ -n "$dalias" ]; then
  254. log "Using domain alias for dns mode"
  255. acme_args="$acme_args --domain-alias $dalias"
  256. if [ -n "$calias" ]; then
  257. err "Both domain and challenge aliases are defined. Ignoring the challenge alias."
  258. fi
  259. elif [ -n "$calias" ]; then
  260. log "Using challenge alias for dns mode"
  261. acme_args="$acme_args --challenge-alias $calias"
  262. fi
  263. elif [ -z "$webroot" ]; then
  264. log "Using standalone mode"
  265. acme_args="$acme_args --standalone --listen-v6"
  266. else
  267. if [ ! -d "$webroot" ]; then
  268. err "$main_domain: Webroot dir '$webroot' does not exist!"
  269. post_checks
  270. return 1
  271. fi
  272. log "Using webroot dir: $webroot"
  273. acme_args="$acme_args --webroot $webroot"
  274. fi
  275. if ! run_acme --home "$STATE_DIR" --issue $acme_args; then
  276. failed_dir="${domain_dir}.failed-$(date +%s)"
  277. err "Issuing cert for $main_domain failed. Moving state to $failed_dir"
  278. [ -d "$domain_dir" ] && mv "$domain_dir" "$failed_dir"
  279. if [ "$moved_staging" -eq "1" ]; then
  280. err "Restoring staging certificate"
  281. mv "${domain_dir}.staging" "${domain_dir}"
  282. fi
  283. post_checks
  284. return 1
  285. fi
  286. if [ -e /etc/init.d/uhttpd ] && [ "$update_uhttpd" -eq "1" ]; then
  287. uci set uhttpd.main.key="${domain_dir}/${main_domain}.key"
  288. uci set uhttpd.main.cert="${domain_dir}/fullchain.cer"
  289. # commit and reload is in post_checks
  290. fi
  291. local nginx_updated
  292. nginx_updated=0
  293. if command -v nginx-util 2> /dev/null && [ "$update_nginx" -eq "1" ]; then
  294. nginx_updated=1
  295. for domain in $domains; do
  296. nginx-util add_ssl "${domain}" acme "${domain_dir}/fullchain.cer" \
  297. "${domain_dir}/${main_domain}.key" || nginx_updated=0
  298. done
  299. # reload is in post_checks
  300. fi
  301. if [ "$nginx_updated" -eq "0" ] && [ -w /etc/nginx/nginx.conf ] && [ "$update_nginx" -eq "1" ]; then
  302. sed -i "s#ssl_certificate\ .*#ssl_certificate ${domain_dir}/fullchain.cer;#g" /etc/nginx/nginx.conf
  303. sed -i "s#ssl_certificate_key\ .*#ssl_certificate_key ${domain_dir}/${main_domain}.key;#g" /etc/nginx/nginx.conf
  304. # commit and reload is in post_checks
  305. fi
  306. if [ -e /etc/init.d/haproxy ] && [ -w /etc/haproxy.cfg ] && [ "$update_haproxy" -eq "1" ]; then
  307. cat "${domain_dir}/${main_domain}.key" "${domain_dir}/fullchain.cer" > "${domain_dir}/${main_domain}-haproxy.pem"
  308. sed -i "s#bind :::443 v4v6 ssl crt .* alpn#bind :::443 v4v6 ssl crt ${domain_dir}/${main_domain}-haproxy.pem alpn#g" /etc/haproxy.cfg
  309. # commit and reload is in post_checks
  310. fi
  311. post_checks
  312. }
  313. load_vars() {
  314. local section="$1"
  315. STATE_DIR=$(config_get "$section" state_dir)
  316. ACCOUNT_EMAIL=$(config_get "$section" account_email)
  317. DEBUG=$(config_get "$section" debug)
  318. }
  319. check_cron
  320. [ -n "$CHECK_CRON" ] && exit 0
  321. [ -e "/var/run/acme_boot" ] && rm -f "/var/run/acme_boot" && exit 0
  322. config_load acme
  323. config_foreach load_vars acme
  324. if [ -z "$STATE_DIR" ] || [ -z "$ACCOUNT_EMAIL" ]; then
  325. err "state_dir and account_email must be set"
  326. exit 1
  327. fi
  328. [ -d "$STATE_DIR" ] || mkdir -p "$STATE_DIR"
  329. trap err_out HUP TERM
  330. trap int_out INT
  331. config_foreach issue_cert cert
  332. exit 0