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