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.

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