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