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.

198 lines
4.6 KiB

1 month ago
1 month ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_dnsimple_info='DNSimple.com
  4. Site: DNSimple.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_dnsimple
  6. Options:
  7. DNSimple_OAUTH_TOKEN OAuth Token
  8. Issues: github.com/pho3nixf1re/acme.sh/issues
  9. '
  10. DNSimple_API="https://api.dnsimple.com/v2"
  11. ######## Public functions #####################
  12. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. dns_dnsimple_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. if [ -z "$DNSimple_OAUTH_TOKEN" ]; then
  17. DNSimple_OAUTH_TOKEN=""
  18. _err "You have not set the dnsimple oauth token yet."
  19. _err "Please visit https://dnsimple.com/user to generate it."
  20. return 1
  21. fi
  22. # save the oauth token for later
  23. _saveaccountconf DNSimple_OAUTH_TOKEN "$DNSimple_OAUTH_TOKEN"
  24. if ! _get_account_id; then
  25. _err "failed to retrive account id"
  26. return 1
  27. fi
  28. if ! _get_root "$fulldomain"; then
  29. _err "invalid domain"
  30. return 1
  31. fi
  32. _get_records "$_account_id" "$_domain" "$_sub_domain"
  33. _info "Adding record"
  34. if _dnsimple_rest POST "$_account_id/zones/$_domain/records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  35. if printf -- "%s" "$response" | grep "\"name\":\"$_sub_domain\"" >/dev/null; then
  36. _info "Added"
  37. return 0
  38. else
  39. _err "Unexpected response while adding text record."
  40. return 1
  41. fi
  42. fi
  43. _err "Add txt record error."
  44. }
  45. # fulldomain
  46. dns_dnsimple_rm() {
  47. fulldomain=$1
  48. if ! _get_account_id; then
  49. _err "failed to retrive account id"
  50. return 1
  51. fi
  52. if ! _get_root "$fulldomain"; then
  53. _err "invalid domain"
  54. return 1
  55. fi
  56. _get_records "$_account_id" "$_domain" "$_sub_domain"
  57. _extract_record_id "$_records" "$_sub_domain"
  58. if [ "$_record_id" ]; then
  59. echo "$_record_id" | while read -r item; do
  60. if _dnsimple_rest DELETE "$_account_id/zones/$_domain/records/$item"; then
  61. _info "removed record" "$item"
  62. return 0
  63. else
  64. _err "failed to remove record" "$item"
  65. return 1
  66. fi
  67. done
  68. fi
  69. }
  70. #################### Private functions bellow ##################################
  71. # _acme-challenge.www.domain.com
  72. # returns
  73. # _sub_domain=_acme-challenge.www
  74. # _domain=domain.com
  75. _get_root() {
  76. domain=$1
  77. i=2
  78. previous=1
  79. while true; do
  80. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  81. if [ -z "$h" ]; then
  82. # not valid
  83. return 1
  84. fi
  85. if ! _dnsimple_rest GET "$_account_id/zones/$h"; then
  86. return 1
  87. fi
  88. if _contains "$response" 'not found'; then
  89. _debug "$h not found"
  90. else
  91. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$previous")
  92. _domain="$h"
  93. _debug _domain "$_domain"
  94. _debug _sub_domain "$_sub_domain"
  95. return 0
  96. fi
  97. previous="$i"
  98. i=$(_math "$i" + 1)
  99. done
  100. return 1
  101. }
  102. # returns _account_id
  103. _get_account_id() {
  104. _debug "retrive account id"
  105. if ! _dnsimple_rest GET "whoami"; then
  106. return 1
  107. fi
  108. if _contains "$response" "\"account\":null"; then
  109. _err "no account associated with this token"
  110. return 1
  111. fi
  112. if _contains "$response" "timeout"; then
  113. _err "timeout retrieving account id"
  114. return 1
  115. fi
  116. _account_id=$(printf "%s" "$response" | _egrep_o "\"id\":[^,]*,\"email\":" | cut -d: -f2 | cut -d, -f1)
  117. _debug _account_id "$_account_id"
  118. return 0
  119. }
  120. # returns
  121. # _records
  122. # _records_count
  123. _get_records() {
  124. account_id=$1
  125. domain=$2
  126. sub_domain=$3
  127. _debug "fetching txt records"
  128. _dnsimple_rest GET "$account_id/zones/$domain/records?per_page=5000&sort=id:desc"
  129. if ! _contains "$response" "\"id\":"; then
  130. _err "failed to retrieve records"
  131. return 1
  132. fi
  133. _records_count=$(printf "%s" "$response" | _egrep_o "\"name\":\"$sub_domain\"" | wc -l | _egrep_o "[0-9]+")
  134. _records=$response
  135. _debug _records_count "$_records_count"
  136. }
  137. # returns _record_id
  138. _extract_record_id() {
  139. _record_id=$(printf "%s" "$_records" | _egrep_o "\"id\":[^,]*,\"zone_id\":\"[^,]*\",\"parent_id\":null,\"name\":\"$_sub_domain\"" | cut -d: -f2 | cut -d, -f1)
  140. _debug "_record_id" "$_record_id"
  141. }
  142. # returns response
  143. _dnsimple_rest() {
  144. method=$1
  145. path="$2"
  146. data="$3"
  147. request_url="$DNSimple_API/$path"
  148. _debug "$path"
  149. export _H1="Accept: application/json"
  150. export _H2="Authorization: Bearer $DNSimple_OAUTH_TOKEN"
  151. if [ "$data" ] || [ "$method" = "DELETE" ]; then
  152. _H1="Content-Type: application/json"
  153. _debug data "$data"
  154. response="$(_post "$data" "$request_url" "" "$method")"
  155. else
  156. response="$(_get "$request_url" "" "" "$method")"
  157. fi
  158. if [ "$?" != "0" ]; then
  159. _err "error $request_url"
  160. return 1
  161. fi
  162. _debug2 response "$response"
  163. return 0
  164. }