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.

156 lines
3.8 KiB

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