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.

212 lines
6.3 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_rackspace_info='RackSpace.com
  4. Site: RackSpace.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_rackspace
  6. Options:
  7. RACKSPACE_Apikey API Key
  8. RACKSPACE_Username Username
  9. Issues: github.com/acmesh-official/acme.sh/issues/2091
  10. '
  11. RACKSPACE_Endpoint="https://dns.api.rackspacecloud.com/v1.0"
  12. # 20210923 - RS changed the fields in the API response; fix sed
  13. # 20190213 - The name & id fields swapped in the API response; fix sed
  14. # 20190101 - Duplicating file for new pull request to dev branch
  15. # Original - tcocca:rackspace_dnsapi https://github.com/acmesh-official/acme.sh/pull/1297
  16. ######## Public functions #####################
  17. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  18. dns_rackspace_add() {
  19. fulldomain="$1"
  20. _debug fulldomain="$fulldomain"
  21. txtvalue="$2"
  22. _debug txtvalue="$txtvalue"
  23. _rackspace_check_auth || return 1
  24. _rackspace_check_rootzone || return 1
  25. _info "Creating TXT record."
  26. if ! _rackspace_rest POST "$RACKSPACE_Tenant/domains/$_domain_id/records" "{\"records\":[{\"name\":\"$fulldomain\",\"type\":\"TXT\",\"data\":\"$txtvalue\",\"ttl\":300}]}"; then
  27. return 1
  28. fi
  29. _debug2 response "$response"
  30. if ! _contains "$response" "$txtvalue" >/dev/null; then
  31. _err "Could not add TXT record."
  32. return 1
  33. fi
  34. return 0
  35. }
  36. #fulldomain txtvalue
  37. dns_rackspace_rm() {
  38. fulldomain=$1
  39. _debug fulldomain="$fulldomain"
  40. txtvalue=$2
  41. _debug txtvalue="$txtvalue"
  42. _rackspace_check_auth || return 1
  43. _rackspace_check_rootzone || return 1
  44. _info "Checking for TXT record."
  45. if ! _get_recordid "$_domain_id" "$fulldomain" "$txtvalue"; then
  46. _err "Could not get TXT record id."
  47. return 1
  48. fi
  49. if [ "$_dns_record_id" = "" ]; then
  50. _err "TXT record not found."
  51. return 1
  52. fi
  53. _info "Removing TXT record."
  54. if ! _delete_txt_record "$_domain_id" "$_dns_record_id"; then
  55. _err "Could not remove TXT record $_dns_record_id."
  56. fi
  57. return 0
  58. }
  59. #################### Private functions below ##################################
  60. #_acme-challenge.www.domain.com
  61. #returns
  62. # _sub_domain=_acme-challenge.www
  63. # _domain=domain.com
  64. # _domain_id=sdjkglgdfewsdfg
  65. _get_root_zone() {
  66. domain="$1"
  67. i=2
  68. p=1
  69. while true; do
  70. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  71. _debug h "$h"
  72. if [ -z "$h" ]; then
  73. #not valid
  74. return 1
  75. fi
  76. if ! _rackspace_rest GET "$RACKSPACE_Tenant/domains/search?name=$h"; then
  77. return 1
  78. fi
  79. _debug2 response "$response"
  80. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  81. # Response looks like:
  82. # {"id":"12345","accountId":"1111111","name": "example.com","ttl":3600,"emailAddress": ... <and so on>
  83. _domain_id=$(echo "$response" | sed -n "s/^.*\"id\":\"\([^,]*\)\",\"accountId\":\"[0-9]*\",\"name\":\"$h\",.*/\1/p")
  84. _debug2 domain_id "$_domain_id"
  85. if [ -n "$_domain_id" ]; then
  86. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  87. _domain=$h
  88. return 0
  89. fi
  90. return 1
  91. fi
  92. p=$i
  93. i=$(_math "$i" + 1)
  94. done
  95. return 1
  96. }
  97. _get_recordid() {
  98. domainid="$1"
  99. fulldomain="$2"
  100. txtvalue="$3"
  101. if ! _rackspace_rest GET "$RACKSPACE_Tenant/domains/$domainid/records?name=$fulldomain&type=TXT"; then
  102. return 1
  103. fi
  104. _debug response "$response"
  105. if ! _contains "$response" "$txtvalue"; then
  106. _dns_record_id=0
  107. return 0
  108. fi
  109. _dns_record_id=$(echo "$response" | tr '{' "\n" | grep "\"data\":\"$txtvalue\"" | sed -n 's/^.*"id":"\([^"]*\)".*/\1/p')
  110. _debug _dns_record_id "$_dns_record_id"
  111. return 0
  112. }
  113. _delete_txt_record() {
  114. domainid="$1"
  115. _dns_record_id="$2"
  116. if ! _rackspace_rest DELETE "$RACKSPACE_Tenant/domains/$domainid/records?id=$_dns_record_id"; then
  117. return 1
  118. fi
  119. _debug response "$response"
  120. if ! _contains "$response" "RUNNING"; then
  121. return 1
  122. fi
  123. return 0
  124. }
  125. _rackspace_rest() {
  126. m="$1"
  127. ep="$2"
  128. data="$3"
  129. _debug ep "$ep"
  130. export _H1="Accept: application/json"
  131. export _H2="X-Auth-Token: $RACKSPACE_Token"
  132. export _H3="X-Project-Id: $RACKSPACE_Tenant"
  133. export _H4="Content-Type: application/json"
  134. if [ "$m" != "GET" ]; then
  135. _debug data "$data"
  136. response="$(_post "$data" "$RACKSPACE_Endpoint/$ep" "" "$m")"
  137. retcode=$?
  138. else
  139. _info "Getting $RACKSPACE_Endpoint/$ep"
  140. response="$(_get "$RACKSPACE_Endpoint/$ep")"
  141. retcode=$?
  142. fi
  143. if [ "$retcode" != "0" ]; then
  144. _err "error $ep"
  145. return 1
  146. fi
  147. _debug2 response "$response"
  148. return 0
  149. }
  150. _rackspace_authorization() {
  151. export _H1="Content-Type: application/json"
  152. data="{\"auth\":{\"RAX-KSKEY:apiKeyCredentials\":{\"username\":\"$RACKSPACE_Username\",\"apiKey\":\"$RACKSPACE_Apikey\"}}}"
  153. _debug data "$data"
  154. response="$(_post "$data" "https://identity.api.rackspacecloud.com/v2.0/tokens" "" "POST")"
  155. retcode=$?
  156. _debug2 response "$response"
  157. if [ "$retcode" != "0" ]; then
  158. _err "Authentication failed."
  159. return 1
  160. fi
  161. if _contains "$response" "token"; then
  162. RACKSPACE_Token="$(echo "$response" | _normalizeJson | sed -n 's/^.*"token":{.*,"id":"\([^"]*\)",".*/\1/p')"
  163. RACKSPACE_Tenant="$(echo "$response" | _normalizeJson | sed -n 's/^.*"token":{.*,"id":"\([^"]*\)"}.*/\1/p')"
  164. _debug RACKSPACE_Token "$RACKSPACE_Token"
  165. _debug RACKSPACE_Tenant "$RACKSPACE_Tenant"
  166. fi
  167. return 0
  168. }
  169. _rackspace_check_auth() {
  170. # retrieve the rackspace creds
  171. RACKSPACE_Username="${RACKSPACE_Username:-$(_readaccountconf_mutable RACKSPACE_Username)}"
  172. RACKSPACE_Apikey="${RACKSPACE_Apikey:-$(_readaccountconf_mutable RACKSPACE_Apikey)}"
  173. # check their vals for null
  174. if [ -z "$RACKSPACE_Username" ] || [ -z "$RACKSPACE_Apikey" ]; then
  175. RACKSPACE_Username=""
  176. RACKSPACE_Apikey=""
  177. _err "You didn't specify a Rackspace username and api key."
  178. _err "Please set those values and try again."
  179. return 1
  180. fi
  181. # save the username and api key to the account conf file.
  182. _saveaccountconf_mutable RACKSPACE_Username "$RACKSPACE_Username"
  183. _saveaccountconf_mutable RACKSPACE_Apikey "$RACKSPACE_Apikey"
  184. if [ -z "$RACKSPACE_Token" ]; then
  185. _info "Getting authorization token."
  186. if ! _rackspace_authorization; then
  187. _err "Can not get token."
  188. fi
  189. fi
  190. }
  191. _rackspace_check_rootzone() {
  192. _debug "First detect the root zone"
  193. if ! _get_root_zone "$fulldomain"; then
  194. _err "invalid domain"
  195. return 1
  196. fi
  197. _debug _domain_id "$_domain_id"
  198. _debug _sub_domain "$_sub_domain"
  199. _debug _domain "$_domain"
  200. }