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.

100 lines
2.8 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_mydevil_info='MyDevil.net
  4. MyDevil.net already supports automatic Lets Encrypt certificates,
  5. except for wildcard domains.
  6. This script depends on devil command that MyDevil.net provides,
  7. which means that it works only on server side.
  8. Site: MyDevil.net
  9. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_mydevil
  10. Issues: github.com/acmesh-official/acme.sh/issues/2079
  11. Author: Marcin Konicki <https://ahwayakchih.neoni.net>
  12. '
  13. ######## Public functions #####################
  14. #Usage: dns_mydevil_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  15. dns_mydevil_add() {
  16. fulldomain=$1
  17. txtvalue=$2
  18. domain=""
  19. if ! _exists "devil"; then
  20. _err "Could not find 'devil' command."
  21. return 1
  22. fi
  23. _info "Using mydevil"
  24. domain=$(mydevil_get_domain "$fulldomain")
  25. if [ -z "$domain" ]; then
  26. _err "Invalid domain name: could not find root domain of $fulldomain."
  27. return 1
  28. fi
  29. # No need to check if record name exists, `devil` always adds new record.
  30. # In worst case scenario, we end up with multiple identical records.
  31. _info "Adding $fulldomain record for domain $domain"
  32. if devil dns add "$domain" "$fulldomain" TXT "$txtvalue"; then
  33. _info "Successfully added TXT record, ready for validation."
  34. return 0
  35. else
  36. _err "Unable to add DNS record."
  37. return 1
  38. fi
  39. }
  40. #Usage: fulldomain txtvalue
  41. #Remove the txt record after validation.
  42. dns_mydevil_rm() {
  43. fulldomain=$1
  44. txtvalue=$2
  45. domain=""
  46. if ! _exists "devil"; then
  47. _err "Could not find 'devil' command."
  48. return 1
  49. fi
  50. _info "Using mydevil"
  51. domain=$(mydevil_get_domain "$fulldomain")
  52. if [ -z "$domain" ]; then
  53. _err "Invalid domain name: could not find root domain of $fulldomain."
  54. return 1
  55. fi
  56. # catch one or more numbers
  57. num='[0-9][0-9]*'
  58. # catch one or more whitespace
  59. w=$(printf '[\t ][\t ]*')
  60. # catch anything, except newline
  61. any='.*'
  62. # filter to make sure we do not delete other records
  63. validRecords="^${num}${w}${fulldomain}${w}TXT${w}${any}${txtvalue}$"
  64. for id in $(devil dns list "$domain" | tail -n+2 | grep "${validRecords}" | cut -w -s -f 1); do
  65. _info "Removing record $id from domain $domain"
  66. echo "y" | devil dns del "$domain" "$id" || _err "Could not remove DNS record."
  67. done
  68. }
  69. #################### Private functions below ##################################
  70. # Usage: domain=$(mydevil_get_domain "_acme-challenge.www.domain.com" || _err "Invalid domain name")
  71. # echo $domain
  72. mydevil_get_domain() {
  73. fulldomain=$1
  74. domain=""
  75. for domain in $(devil dns list | cut -w -s -f 1 | tail -n+2); do
  76. _debug "Checking domain: $domain"
  77. if _endswith "$fulldomain" "$domain"; then
  78. _debug "Fulldomain '$fulldomain' matches '$domain'"
  79. printf -- "%s" "$domain"
  80. return 0
  81. fi
  82. done
  83. return 1
  84. }