run.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. # We export both ca variables in an attempts to keep backwards
  13. # compatibility with older versions of curl that was linked against
  14. # openssl
  15. export SSL_CERT_DIR=/etc/ssl/certs
  16. export CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
  17. export NO_TIMESTAMP=1
  18. UHTTPD_LISTEN_HTTP=
  19. STATE_DIR='/etc/acme'
  20. ACCOUNT_EMAIL=
  21. DEBUG=0
  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. debug()
  30. {
  31. [ "$DEBUG" -eq "1" ] && echo "$@" >&2
  32. }
  33. pre_checks()
  34. {
  35. echo "Running pre checks."
  36. check_cron
  37. [ -d "$STATE_DIR" ] || mkdir -p "$STATE_DIR"
  38. if [ -e /etc/init.d/uhttpd ]; then
  39. UHTTPD_LISTEN_HTTP=$(uci get uhttpd.main.listen_http)
  40. uci set uhttpd.main.listen_http=''
  41. uci commit uhttpd
  42. /etc/init.d/uhttpd reload || return 1
  43. fi
  44. iptables -I input_rule -p tcp --dport 80 -j ACCEPT || return 1
  45. ip6tables -I input_rule -p tcp --dport 80 -j ACCEPT || return 1
  46. debug "v4 input_rule: $(iptables -nvL input_rule)"
  47. debug "v6 input_rule: $(ip6tables -nvL input_rule)"
  48. debug "port80 listens: $(netstat -ntpl | grep :80)"
  49. return 0
  50. }
  51. post_checks()
  52. {
  53. echo "Running post checks (cleanup)."
  54. iptables -D input_rule -p tcp --dport 80 -j ACCEPT
  55. ip6tables -D input_rule -p tcp --dport 80 -j ACCEPT
  56. if [ -e /etc/init.d/uhttpd ]; then
  57. uci set uhttpd.main.listen_http="$UHTTPD_LISTEN_HTTP"
  58. uci commit uhttpd
  59. /etc/init.d/uhttpd reload
  60. fi
  61. }
  62. err_out()
  63. {
  64. post_checks
  65. exit 1
  66. }
  67. int_out()
  68. {
  69. post_checks
  70. trap - INT
  71. kill -INT $$
  72. }
  73. is_staging()
  74. {
  75. local main_domain="$1"
  76. grep -q "acme-staging" "$STATE_DIR/$main_domain/${main_domain}.conf"
  77. return $?
  78. }
  79. issue_cert()
  80. {
  81. local section="$1"
  82. local acme_args=
  83. local enabled
  84. local use_staging
  85. local update_uhttpd
  86. local keylength
  87. local domains
  88. local main_domain
  89. local moved_staging=0
  90. local failed_dir
  91. config_get_bool enabled "$section" enabled 0
  92. config_get_bool use_staging "$section" use_staging
  93. config_get_bool update_uhttpd "$section" update_uhttpd
  94. config_get domains "$section" domains
  95. config_get keylength "$section" keylength
  96. [ "$enabled" -eq "1" ] || return
  97. [ "$DEBUG" -eq "1" ] && acme_args="$acme_args --debug"
  98. set -- $domains
  99. main_domain=$1
  100. if [ -e "$STATE_DIR/$main_domain" ]; then
  101. if [ "$use_staging" -eq "0" ] && is_staging "$main_domain"; then
  102. echo "Found previous cert issued using staging server. Moving it out of the way."
  103. mv "$STATE_DIR/$main_domain" "$STATE_DIR/$main_domain.staging"
  104. moved_staging=1
  105. else
  106. echo "Found previous cert config. Issuing renew."
  107. $ACME --home "$STATE_DIR" --renew -d "$main_domain" $acme_args || return 1
  108. return 0
  109. fi
  110. fi
  111. acme_args="$acme_args $(for d in $domains; do echo -n "-d $d "; done)"
  112. acme_args="$acme_args --standalone"
  113. acme_args="$acme_args --keylength $keylength"
  114. [ -n "$ACCOUNT_EMAIL" ] && acme_args="$acme_args --accountemail $ACCOUNT_EMAIL"
  115. [ "$use_staging" -eq "1" ] && acme_args="$acme_args --staging"
  116. if ! $ACME --home "$STATE_DIR" --issue $acme_args; then
  117. failed_dir="$STATE_DIR/${main_domain}.failed-$(date +%s)"
  118. echo "Issuing cert for $main_domain failed. Moving state to $failed_dir" >&2
  119. [ -d "$STATE_DIR/$main_domain" ] && mv "$STATE_DIR/$main_domain" "$failed_dir"
  120. if [ "$moved_staging" -eq "1" ]; then
  121. echo "Restoring staging certificate" >&2
  122. mv "$STATE_DIR/${main_domain}.staging" "$STATE_DIR/${main_domain}"
  123. fi
  124. return 1
  125. fi
  126. if [ "$update_uhttpd" -eq "1" ]; then
  127. uci set uhttpd.main.key="$STATE_DIR/${main_domain}/${main_domain}.key"
  128. uci set uhttpd.main.cert="$STATE_DIR/${main_domain}/fullchain.cer"
  129. # commit and reload is in post_checks
  130. fi
  131. }
  132. load_vars()
  133. {
  134. local section="$1"
  135. STATE_DIR=$(config_get "$section" state_dir)
  136. ACCOUNT_EMAIL=$(config_get "$section" account_email)
  137. DEBUG=$(config_get "$section" debug)
  138. }
  139. if [ -n "$CHECK_CRON" ]; then
  140. check_cron
  141. exit 0
  142. fi
  143. config_load acme
  144. config_foreach load_vars acme
  145. pre_checks || exit 1
  146. trap err_out HUP TERM
  147. trap int_out INT
  148. config_foreach issue_cert cert
  149. post_checks
  150. exit 0