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.

147 lines
3.4 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 SSL_CERT_DIR=/etc/ssl/certs
  13. UHTTPD_LISTEN_HTTP=
  14. STATE_DIR='/etc/acme'
  15. ACCOUNT_EMAIL=
  16. DEBUG=0
  17. . /lib/functions.sh
  18. check_cron()
  19. {
  20. [ -f "/etc/crontabs/root" ] && grep -q '/etc/init.d/acme' /etc/crontabs/root && return
  21. echo "0 0 * * * /etc/init.d/acme start" >> /etc/crontabs/root
  22. /etc/init.d/cron start
  23. }
  24. pre_checks()
  25. {
  26. echo "Running pre checks."
  27. check_cron
  28. if [ -e /etc/init.d/uhttpd ]; then
  29. UHTTPD_LISTEN_HTTP=$(uci get uhttpd.main.listen_http)
  30. uci set uhttpd.main.listen_http=''
  31. uci commit uhttpd
  32. /etc/init.d/uhttpd reload || return 1
  33. fi
  34. iptables -I input_rule -p tcp --dport 80 -j ACCEPT || return 1
  35. ip6tables -I input_rule -p tcp --dport 80 -j ACCEPT || return 1
  36. return 0
  37. }
  38. post_checks()
  39. {
  40. echo "Running post checks (cleanup)."
  41. iptables -D input_rule -p tcp --dport 80 -j ACCEPT
  42. ip6tables -D input_rule -p tcp --dport 80 -j ACCEPT
  43. if [ -e /etc/init.d/uhttpd ]; then
  44. uci set uhttpd.main.listen_http="$UHTTPD_LISTEN_HTTP"
  45. uci commit uhttpd
  46. /etc/init.d/uhttpd reload
  47. fi
  48. }
  49. err_out()
  50. {
  51. post_checks
  52. exit 1
  53. }
  54. int_out()
  55. {
  56. post_checks
  57. trap - INT
  58. kill -INT $$
  59. }
  60. issue_cert()
  61. {
  62. local section="$1"
  63. local acme_args=
  64. local enabled
  65. local use_staging
  66. local update_uhttpd
  67. local keylength
  68. local domains
  69. local main_domain
  70. config_get_bool enabled "$section" enabled 0
  71. config_get_bool use_staging "$section" use_staging
  72. config_get_bool update_uhttpd "$section" update_uhttpd
  73. config_get domains "$section" domains
  74. config_get keylength "$section" keylength
  75. [ "$enabled" -eq "1" ] || return
  76. [ "$DEBUG" -eq "1" ] && acme_args="$acme_args --debug"
  77. set -- $domains
  78. main_domain=$1
  79. if [ -e "$STATE_DIR/$main_domain" ]; then
  80. $ACME --home "$STATE_DIR" --renew -d "$main_domain" $acme_args || return 1
  81. return 0
  82. fi
  83. acme_args="$acme_args $(for d in $domains; do echo -n "-d $d "; done)"
  84. acme_args="$acme_args --standalone"
  85. acme_args="$acme_args --keylength $keylength"
  86. [ -n "$ACCOUNT_EMAIL" ] && acme_args="$acme_args --accountemail $ACCOUNT_EMAIL"
  87. [ "$use_staging" -eq "1" ] && acme_args="$acme_args --staging"
  88. if ! $ACME --home "$STATE_DIR" --issue $acme_args; then
  89. echo "Issuing cert for $main_domain failed. It may be necessary to remove $STATE_DIR/$main_domain to recover." >&2
  90. return 1
  91. fi
  92. if [ "$update_uhttpd" -eq "1" ]; then
  93. uci set uhttpd.main.key="$STATE_DIR/${main_domain}/${main_domain}.key"
  94. uci set uhttpd.main.cert="$STATE_DIR/${main_domain}/fullchain.cer"
  95. # commit and reload is in post_checks
  96. fi
  97. }
  98. load_vars()
  99. {
  100. local section="$1"
  101. STATE_DIR=$(config_get "$section" state_dir)
  102. ACCOUNT_EMAIL=$(config_get "$section" account_email)
  103. DEBUG=$(config_get "$section" debug)
  104. }
  105. if [ -n "$CHECK_CRON" ]; then
  106. check_cron
  107. exit 0
  108. fi
  109. config_load acme
  110. config_foreach load_vars acme
  111. pre_checks || exit 1
  112. trap err_out HUP TERM
  113. trap int_out INT
  114. config_foreach issue_cert cert
  115. post_checks
  116. exit 0