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.

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