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.

208 lines
6.1 KiB

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