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.

99 lines
2.7 KiB

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