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.

216 lines
5.1 KiB

  1. #!/usr/bin/env sh
  2. # DNSimple domain api
  3. # https://github.com/pho3nixf1re/acme.sh/issues
  4. #
  5. # This is your oauth token which can be acquired on the account page. Please
  6. # note that this must be an _account_ token and not a _user_ token.
  7. # https://dnsimple.com/a/<your account id>/account/access_tokens
  8. # DNSimple_OAUTH_TOKEN="sdfsdfsdfljlbjkljlkjsdfoiwje"
  9. DNSimple_API="https://api.dnsimple.com/v2"
  10. ######## Public functions #####################
  11. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  12. dns_dnsimple_add() {
  13. fulldomain=$1
  14. txtvalue=$2
  15. if [ -z "$DNSimple_OAUTH_TOKEN" ]; then
  16. DNSimple_OAUTH_TOKEN=""
  17. _err "You have not set the dnsimple oauth token yet."
  18. _err "Please visit https://dnsimple.com/user to generate it."
  19. return 1
  20. fi
  21. # save the oauth token for later
  22. _saveaccountconf DNSimple_OAUTH_TOKEN "$DNSimple_OAUTH_TOKEN"
  23. if ! _get_account_id; then
  24. _err "failed to retrive account id"
  25. return 1
  26. fi
  27. if ! _get_root "$fulldomain"; then
  28. _err "invalid domain"
  29. return 1
  30. fi
  31. _get_records "$_account_id" "$_domain" "$_sub_domain"
  32. # if [ "$_records_count" = "0" ]; then
  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. # else
  45. # _info "Updating record"
  46. # _extract_record_id "$_records" "$_sub_domain"
  47. # if _dnsimple_rest \
  48. # PATCH \
  49. # "$_account_id/zones/$_domain/records/$_record_id" \
  50. # "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  51. # _info "Updated!"
  52. # return 0
  53. # fi
  54. # _err "Update error"
  55. # return 1
  56. # fi
  57. }
  58. # fulldomain
  59. dns_dnsimple_rm() {
  60. fulldomain=$1
  61. if ! _get_account_id; then
  62. _err "failed to retrive account id"
  63. return 1
  64. fi
  65. if ! _get_root "$fulldomain"; then
  66. _err "invalid domain"
  67. return 1
  68. fi
  69. _get_records "$_account_id" "$_domain" "$_sub_domain"
  70. _extract_record_id "$_records" "$_sub_domain"
  71. if [ "$_record_id" ]; then
  72. echo "$_record_id" | while read -r item
  73. do
  74. if _dnsimple_rest DELETE "$_account_id/zones/$_domain/records/$item"; then
  75. _info "removed record" "$item"
  76. return 0
  77. else
  78. _err "failed to remove record" "$item"
  79. return 1
  80. fi
  81. done
  82. fi
  83. }
  84. #################### Private functions bellow ##################################
  85. # _acme-challenge.www.domain.com
  86. # returns
  87. # _sub_domain=_acme-challenge.www
  88. # _domain=domain.com
  89. _get_root() {
  90. domain=$1
  91. i=2
  92. previous=1
  93. while true; do
  94. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  95. if [ -z "$h" ]; then
  96. # not valid
  97. return 1
  98. fi
  99. if ! _dnsimple_rest GET "$_account_id/zones/$h"; then
  100. return 1
  101. fi
  102. if _contains "$response" 'not found'; then
  103. _debug "$h not found"
  104. else
  105. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$previous)
  106. _domain="$h"
  107. _debug _domain "$_domain"
  108. _debug _sub_domain "$_sub_domain"
  109. return 0
  110. fi
  111. previous="$i"
  112. i=$(_math "$i" + 1)
  113. done
  114. return 1
  115. }
  116. # returns _account_id
  117. _get_account_id() {
  118. _debug "retrive account id"
  119. if ! _dnsimple_rest GET "whoami"; then
  120. return 1
  121. fi
  122. if _contains "$response" "\"account\":null"; then
  123. _err "no account associated with this token"
  124. return 1
  125. fi
  126. if _contains "$response" "timeout"; then
  127. _err "timeout retrieving account id"
  128. return 1
  129. fi
  130. _account_id=$(printf "%s" "$response" | _egrep_o "\"id\":[^,]*,\"email\":" | cut -d: -f2 | cut -d, -f1)
  131. _debug _account_id "$_account_id"
  132. return 0
  133. }
  134. # returns
  135. # _records
  136. # _records_count
  137. _get_records() {
  138. account_id=$1
  139. domain=$2
  140. sub_domain=$3
  141. _debug "fetching txt records"
  142. _dnsimple_rest GET "$account_id/zones/$domain/records?per_page=100"
  143. if ! _contains "$response" "\"id\":"; then
  144. _err "failed to retrieve records"
  145. return 1
  146. fi
  147. _records_count=$(printf "%s" "$response" | _egrep_o "\"name\":\"$sub_domain\"" | wc -l | _egrep_o "[0-9]+")
  148. _records=$response
  149. _debug _records_count "$_records_count"
  150. }
  151. # returns _record_id
  152. _extract_record_id() {
  153. _record_id=$(printf "%s" "$_records" | _egrep_o "\"id\":[^,]*,\"zone_id\":\"[^,]*\",\"parent_id\":null,\"name\":\"$_sub_domain\"" | cut -d: -f2 | cut -d, -f1)
  154. _debug "_record_id" "$_record_id"
  155. }
  156. # returns response
  157. _dnsimple_rest() {
  158. method=$1
  159. path="$2"
  160. data="$3"
  161. request_url="$DNSimple_API/$path"
  162. _debug "$path"
  163. export _H1="Accept: application/json"
  164. export _H2="Authorization: Bearer $DNSimple_OAUTH_TOKEN"
  165. if [ "$data" ] || [ "$method" = "DELETE" ]; then
  166. _H1="Content-Type: application/json"
  167. _debug data "$data"
  168. response="$(_post "$data" "$request_url" "" "$method")"
  169. else
  170. response="$(_get "$request_url" "" "" "$method")"
  171. fi
  172. if [ "$?" != "0" ]; then
  173. _err "error $request_url"
  174. return 1
  175. fi
  176. _debug2 response "$response"
  177. return 0
  178. }