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.

200 lines
5.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_gd_info='GoDaddy.com
  4. Site: GoDaddy.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_gd
  6. Options:
  7. GD_Key API Key
  8. GD_Secret API Secret
  9. '
  10. GD_Api="https://api.godaddy.com/v1"
  11. ######## Public functions #####################
  12. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. dns_gd_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. GD_Key="${GD_Key:-$(_readaccountconf_mutable GD_Key)}"
  17. GD_Secret="${GD_Secret:-$(_readaccountconf_mutable GD_Secret)}"
  18. if [ -z "$GD_Key" ] || [ -z "$GD_Secret" ]; then
  19. GD_Key=""
  20. GD_Secret=""
  21. _err "You didn't specify godaddy api key and secret yet."
  22. _err "Please create your key and try again."
  23. return 1
  24. fi
  25. #save the api key and email to the account conf file.
  26. _saveaccountconf_mutable GD_Key "$GD_Key"
  27. _saveaccountconf_mutable GD_Secret "$GD_Secret"
  28. _debug "First detect the root zone"
  29. if ! _get_root "$fulldomain"; then
  30. _err "invalid domain"
  31. return 1
  32. fi
  33. _debug _sub_domain "$_sub_domain"
  34. _debug _domain "$_domain"
  35. _debug "Getting existing records"
  36. if ! _gd_rest GET "domains/$_domain/records/TXT/$_sub_domain"; then
  37. return 1
  38. fi
  39. if _contains "$response" "$txtvalue"; then
  40. _info "This record already exists, skipping"
  41. return 0
  42. fi
  43. _add_data="{\"data\":\"$txtvalue\"}"
  44. for t in $(echo "$response" | tr '{' "\n" | grep "\"name\":\"$_sub_domain\"" | tr ',' "\n" | grep '"data"' | cut -d : -f 2); do
  45. _debug2 t "$t"
  46. # ignore empty (previously removed) records, to prevent useless _acme-challenge TXT entries
  47. if [ "$t" ] && [ "$t" != '""' ]; then
  48. _add_data="$_add_data,{\"data\":$t}"
  49. fi
  50. done
  51. _debug2 _add_data "$_add_data"
  52. _info "Adding record"
  53. if _gd_rest PUT "domains/$_domain/records/TXT/$_sub_domain" "[$_add_data]"; then
  54. _debug "Checking updated records of '${fulldomain}'"
  55. if ! _gd_rest GET "domains/$_domain/records/TXT/$_sub_domain"; then
  56. _err "Validating TXT record for '${fulldomain}' with rest error [$?]." "$response"
  57. return 1
  58. fi
  59. if ! _contains "$response" "$txtvalue"; then
  60. _err "TXT record '${txtvalue}' for '${fulldomain}', value wasn't set!"
  61. return 1
  62. fi
  63. else
  64. _err "Add txt record error, value '${txtvalue}' for '${fulldomain}' was not set."
  65. return 1
  66. fi
  67. _sleep 10
  68. _info "Added TXT record '${txtvalue}' for '${fulldomain}'."
  69. return 0
  70. }
  71. #fulldomain
  72. dns_gd_rm() {
  73. fulldomain=$1
  74. txtvalue=$2
  75. GD_Key="${GD_Key:-$(_readaccountconf_mutable GD_Key)}"
  76. GD_Secret="${GD_Secret:-$(_readaccountconf_mutable GD_Secret)}"
  77. _debug "First detect the root zone"
  78. if ! _get_root "$fulldomain"; then
  79. _err "invalid domain"
  80. return 1
  81. fi
  82. _debug _sub_domain "$_sub_domain"
  83. _debug _domain "$_domain"
  84. _debug "Getting existing records"
  85. if ! _gd_rest GET "domains/$_domain/records/TXT/$_sub_domain"; then
  86. return 1
  87. fi
  88. if ! _contains "$response" "$txtvalue"; then
  89. _info "The record does not exist, skip"
  90. return 0
  91. fi
  92. _add_data=""
  93. for t in $(echo "$response" | tr '{' "\n" | grep "\"name\":\"$_sub_domain\"" | tr ',' "\n" | grep '"data"' | cut -d : -f 2); do
  94. _debug2 t "$t"
  95. if [ "$t" ] && [ "$t" != "\"$txtvalue\"" ]; then
  96. if [ "$_add_data" ]; then
  97. _add_data="$_add_data,{\"data\":$t}"
  98. else
  99. _add_data="{\"data\":$t}"
  100. fi
  101. fi
  102. done
  103. if [ -z "$_add_data" ]; then
  104. # delete empty record
  105. _debug "Delete last record for '${fulldomain}'"
  106. if ! _gd_rest DELETE "domains/$_domain/records/TXT/$_sub_domain"; then
  107. _err "Cannot delete empty TXT record for '$fulldomain'"
  108. return 1
  109. fi
  110. else
  111. # remove specific TXT value, keeping other entries
  112. _debug2 _add_data "$_add_data"
  113. if ! _gd_rest PUT "domains/$_domain/records/TXT/$_sub_domain" "[$_add_data]"; then
  114. _err "Cannot update TXT record for '$fulldomain'"
  115. return 1
  116. fi
  117. fi
  118. }
  119. #################### Private functions below ##################################
  120. #_acme-challenge.www.domain.com
  121. #returns
  122. # _sub_domain=_acme-challenge.www
  123. # _domain=domain.com
  124. _get_root() {
  125. domain=$1
  126. i=2
  127. p=1
  128. while true; do
  129. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  130. if [ -z "$h" ]; then
  131. #not valid
  132. return 1
  133. fi
  134. if ! _gd_rest GET "domains/$h"; then
  135. return 1
  136. fi
  137. if _contains "$response" '"code":"NOT_FOUND"'; then
  138. _debug "$h not found"
  139. else
  140. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  141. _domain="$h"
  142. return 0
  143. fi
  144. p="$i"
  145. i=$(_math "$i" + 1)
  146. done
  147. return 1
  148. }
  149. _gd_rest() {
  150. m=$1
  151. ep="$2"
  152. data="$3"
  153. _debug "$ep"
  154. export _H1="Authorization: sso-key $GD_Key:$GD_Secret"
  155. export _H2="Content-Type: application/json"
  156. if [ "$data" ] || [ "$m" = "DELETE" ]; then
  157. _debug "data ($m): " "$data"
  158. response="$(_post "$data" "$GD_Api/$ep" "" "$m")"
  159. else
  160. response="$(_get "$GD_Api/$ep")"
  161. fi
  162. if [ "$?" != "0" ]; then
  163. _err "error on rest call ($m): $ep"
  164. return 1
  165. fi
  166. _debug2 response "$response"
  167. if _contains "$response" "UNABLE_TO_AUTHENTICATE"; then
  168. _err "It seems that your api key or secret is not correct."
  169. return 1
  170. fi
  171. return 0
  172. }