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.

351 lines
8.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. {
  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 calias "$section" calias
  180. config_get dalias "$section" dalias
  181. config_get domains "$section" domains
  182. config_get keylength "$section" keylength
  183. config_get webroot "$section" webroot
  184. config_get dns "$section" dns
  185. config_get user_setup "$section" user_setup
  186. config_get user_cleanup "$section" user_cleanup
  187. UPDATE_NGINX=$update_nginx
  188. UPDATE_UHTTPD=$update_uhttpd
  189. USER_CLEANUP=$user_cleanup
  190. [ "$enabled" -eq "1" ] || return
  191. [ "$DEBUG" -eq "1" ] && acme_args="$acme_args --debug"
  192. set -- $domains
  193. main_domain=$1
  194. if [ -n "$user_setup" ] && [ -f "$user_setup" ]; then
  195. log "Running user-provided setup script from $user_setup."
  196. "$user_setup" "$main_domain" || return 1
  197. else
  198. [ -n "$webroot" ] || [ -n "$dns" ] || pre_checks "$main_domain" || return 1
  199. fi
  200. if echo $keylength | grep -q "^ec-"; then
  201. domain_dir="$STATE_DIR/${main_domain}_ecc"
  202. keylength_ecc=1
  203. else
  204. domain_dir="$STATE_DIR/${main_domain}"
  205. fi
  206. log "Running ACME for $main_domain"
  207. handle_credentials() {
  208. local credential="$1"
  209. eval export $credential
  210. }
  211. config_list_foreach "$section" credentials handle_credentials
  212. if [ -e "$domain_dir" ]; then
  213. if [ "$use_staging" -eq "0" ] && is_staging "$main_domain" "$domain_dir"; then
  214. log "Found previous cert issued using staging server. Moving it out of the way."
  215. mv "$domain_dir" "${domain_dir}.staging"
  216. moved_staging=1
  217. else
  218. log "Found previous cert config. Issuing renew."
  219. [ "$keylength_ecc" -eq "1" ] && acme_args="$acme_args --ecc"
  220. run_acme --home "$STATE_DIR" --renew -d "$main_domain" $acme_args && ret=0 || ret=1
  221. post_checks
  222. return $ret
  223. fi
  224. fi
  225. acme_args="$acme_args $(for d in $domains; do echo -n "-d $d "; done)"
  226. acme_args="$acme_args --keylength $keylength"
  227. [ -n "$ACCOUNT_EMAIL" ] && acme_args="$acme_args --accountemail $ACCOUNT_EMAIL"
  228. [ "$use_staging" -eq "1" ] && acme_args="$acme_args --staging"
  229. if [ -n "$dns" ]; then
  230. log "Using dns mode"
  231. acme_args="$acme_args --dns $dns"
  232. if [ -n "$dalias" ]; then
  233. log "Using domain alias for dns mode"
  234. acme_args="$acme_args --domain-alias $dalias"
  235. if [ -n "$calias" ]; then
  236. err "Both domain and challenge aliases are defined. Ignoring the challenge alias."
  237. fi
  238. elif [ -n "$calias" ]; then
  239. log "Using challenge alias for dns mode"
  240. acme_args="$acme_args --challenge-alias $calias"
  241. fi
  242. elif [ -z "$webroot" ]; then
  243. log "Using standalone mode"
  244. acme_args="$acme_args --standalone --listen-v6"
  245. else
  246. if [ ! -d "$webroot" ]; then
  247. err "$main_domain: Webroot dir '$webroot' does not exist!"
  248. post_checks
  249. return 1
  250. fi
  251. log "Using webroot dir: $webroot"
  252. acme_args="$acme_args --webroot $webroot"
  253. fi
  254. if ! run_acme --home "$STATE_DIR" --issue $acme_args; then
  255. failed_dir="${domain_dir}.failed-$(date +%s)"
  256. err "Issuing cert for $main_domain failed. Moving state to $failed_dir"
  257. [ -d "$domain_dir" ] && mv "$domain_dir" "$failed_dir"
  258. if [ "$moved_staging" -eq "1" ]; then
  259. err "Restoring staging certificate"
  260. mv "${domain_dir}.staging" "${domain_dir}"
  261. fi
  262. post_checks
  263. return 1
  264. fi
  265. if [ -e /etc/init.d/uhttpd ] && [ "$update_uhttpd" -eq "1" ]; then
  266. uci set uhttpd.main.key="${domain_dir}/${main_domain}.key"
  267. uci set uhttpd.main.cert="${domain_dir}/fullchain.cer"
  268. # commit and reload is in post_checks
  269. fi
  270. if [ -e /etc/init.d/nginx ] && [ "$update_nginx" -eq "1" ]; then
  271. sed -i "s#ssl_certificate\ .*#ssl_certificate ${domain_dir}/fullchain.cer;#g" /etc/nginx/nginx.conf
  272. sed -i "s#ssl_certificate_key\ .*#ssl_certificate_key ${domain_dir}/${main_domain}.key;#g" /etc/nginx/nginx.conf
  273. # commit and reload is in post_checks
  274. fi
  275. post_checks
  276. }
  277. load_vars()
  278. {
  279. local section="$1"
  280. STATE_DIR=$(config_get "$section" state_dir)
  281. ACCOUNT_EMAIL=$(config_get "$section" account_email)
  282. DEBUG=$(config_get "$section" debug)
  283. }
  284. check_cron
  285. [ -n "$CHECK_CRON" ] && exit 0
  286. [ -e "/var/run/acme_boot" ] && rm -f "/var/run/acme_boot" && exit 0
  287. config_load acme
  288. config_foreach load_vars acme
  289. if [ -z "$STATE_DIR" ] || [ -z "$ACCOUNT_EMAIL" ]; then
  290. err "state_dir and account_email must be set"
  291. exit 1
  292. fi
  293. [ -d "$STATE_DIR" ] || mkdir -p "$STATE_DIR"
  294. trap err_out HUP TERM
  295. trap int_out INT
  296. config_foreach issue_cert cert
  297. exit 0