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.

154 lines
3.8 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_rackcorp_info='RackCorp.com
  4. Site: RackCorp.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_rackcorp
  6. Options:
  7. RACKCORP_APIUUID API UUID. See Portal: ADMINISTRATION -> API
  8. RACKCORP_APISECRET API Secret
  9. Issues: github.com/acmesh-official/acme.sh/issues/3351
  10. Author: Stephen Dendtler <sdendtler@rackcorp.com>
  11. '
  12. RACKCORP_API_ENDPOINT="https://api.rackcorp.net/api/rest/v2.4/json.php"
  13. ######## Public functions #####################
  14. dns_rackcorp_add() {
  15. fulldomain="$1"
  16. txtvalue="$2"
  17. _debug fulldomain="$fulldomain"
  18. _debug txtvalue="$txtvalue"
  19. if ! _rackcorp_validate; then
  20. return 1
  21. fi
  22. _debug "Searching for root zone"
  23. if ! _get_root "$fulldomain"; then
  24. return 1
  25. fi
  26. _debug _lookup "$_lookup"
  27. _debug _domain "$_domain"
  28. _info "Creating TXT record."
  29. if ! _rackcorp_api dns.record.create "\"name\":\"$_domain\",\"type\":\"TXT\",\"lookup\":\"$_lookup\",\"data\":\"$txtvalue\",\"ttl\":300"; then
  30. return 1
  31. fi
  32. return 0
  33. }
  34. #Usage: fulldomain txtvalue
  35. #Remove the txt record after validation.
  36. dns_rackcorp_rm() {
  37. fulldomain=$1
  38. txtvalue=$2
  39. _debug fulldomain="$fulldomain"
  40. _debug txtvalue="$txtvalue"
  41. if ! _rackcorp_validate; then
  42. return 1
  43. fi
  44. _debug "Searching for root zone"
  45. if ! _get_root "$fulldomain"; then
  46. return 1
  47. fi
  48. _debug _lookup "$_lookup"
  49. _debug _domain "$_domain"
  50. _info "Creating TXT record."
  51. if ! _rackcorp_api dns.record.delete "\"name\":\"$_domain\",\"type\":\"TXT\",\"lookup\":\"$_lookup\",\"data\":\"$txtvalue\""; then
  52. return 1
  53. fi
  54. return 0
  55. }
  56. #################### Private functions below ##################################
  57. #_acme-challenge.domain.com
  58. #returns
  59. # _lookup=_acme-challenge
  60. # _domain=domain.com
  61. _get_root() {
  62. domain=$1
  63. i=1
  64. p=1
  65. if ! _rackcorp_api dns.domain.getall "\"name\":\"$domain\""; then
  66. return 1
  67. fi
  68. while true; do
  69. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  70. _debug searchhost "$h"
  71. if [ -z "$h" ]; then
  72. _err "Could not find domain for record $domain in RackCorp using the provided credentials"
  73. #not valid
  74. return 1
  75. fi
  76. _rackcorp_api dns.domain.getall "\"exactName\":\"$h\""
  77. if _contains "$response" "\"matches\":1"; then
  78. if _contains "$response" "\"name\":\"$h\""; then
  79. _lookup=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  80. _domain="$h"
  81. return 0
  82. fi
  83. fi
  84. p=$i
  85. i=$(_math "$i" + 1)
  86. done
  87. return 1
  88. }
  89. _rackcorp_validate() {
  90. RACKCORP_APIUUID="${RACKCORP_APIUUID:-$(_readaccountconf_mutable RACKCORP_APIUUID)}"
  91. if [ -z "$RACKCORP_APIUUID" ]; then
  92. RACKCORP_APIUUID=""
  93. _err "You require a RackCorp API UUID (export RACKCORP_APIUUID=\"<api uuid>\")"
  94. _err "Please login to the portal and create an API key and try again."
  95. return 1
  96. fi
  97. _saveaccountconf_mutable RACKCORP_APIUUID "$RACKCORP_APIUUID"
  98. RACKCORP_APISECRET="${RACKCORP_APISECRET:-$(_readaccountconf_mutable RACKCORP_APISECRET)}"
  99. if [ -z "$RACKCORP_APISECRET" ]; then
  100. RACKCORP_APISECRET=""
  101. _err "You require a RackCorp API secret (export RACKCORP_APISECRET=\"<api secret>\")"
  102. _err "Please login to the portal and create an API key and try again."
  103. return 1
  104. fi
  105. _saveaccountconf_mutable RACKCORP_APISECRET "$RACKCORP_APISECRET"
  106. return 0
  107. }
  108. _rackcorp_api() {
  109. _rackcorpcmd=$1
  110. _rackcorpinputdata=$2
  111. _debug cmd "$_rackcorpcmd $_rackcorpinputdata"
  112. export _H1="Accept: application/json"
  113. response="$(_post "{\"APIUUID\":\"$RACKCORP_APIUUID\",\"APISECRET\":\"$RACKCORP_APISECRET\",\"cmd\":\"$_rackcorpcmd\",$_rackcorpinputdata}" "$RACKCORP_API_ENDPOINT" "" "POST")"
  114. if [ "$?" != "0" ]; then
  115. _err "error $response"
  116. return 1
  117. fi
  118. _debug2 response "$response"
  119. if _contains "$response" "\"code\":\"OK\""; then
  120. _debug code "OK"
  121. else
  122. _debug code "FAILED"
  123. response=""
  124. return 1
  125. fi
  126. return 0
  127. }