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.

212 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. _dnsimple_rest PATCH "$_account_id/zones/$_domain/records/$_record_id" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":120}"
  48. if [ "$?" = "0" ]; then
  49. _info "Updated!"
  50. #todo: check if the record takes effect
  51. return 0
  52. fi
  53. _err "Update error"
  54. return 1
  55. fi
  56. }
  57. # fulldomain
  58. dns_dnsimple_rm() {
  59. fulldomain=$1
  60. if ! _get_account_id; then
  61. _err "failed to retrive account id"
  62. return 1
  63. fi
  64. if ! _get_root "$fulldomain"; then
  65. _err "invalid domain"
  66. return 1
  67. fi
  68. _get_records $_account_id $_domain $_sub_domain
  69. _extract_record_id $_records $_sub_domain
  70. if [ "$_record_id" ]; then
  71. _dnsimple_rest DELETE "$_account_id/zones/$_domain/records/$_record_id"
  72. if [ "$?" = "0" ]; then
  73. _info "removed record" "$_record_id"
  74. return 0
  75. fi
  76. fi
  77. _err "failed to remove record" "$_record_id"
  78. return 1
  79. }
  80. #################### Private functions bellow ##################################
  81. # _acme-challenge.www.domain.com
  82. # returns
  83. # _sub_domain=_acme-challenge.www
  84. # _domain=domain.com
  85. _get_root() {
  86. domain=$1
  87. i=2
  88. previous=1
  89. while true; do
  90. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  91. if [ -z "$h" ]; then
  92. # not valid
  93. return 1
  94. fi
  95. if ! _dnsimple_rest GET "$_account_id/zones/$h"; then
  96. return 1
  97. fi
  98. if _contains "$response" 'not found'; then
  99. _debug "$h not found"
  100. else
  101. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$previous)
  102. _domain="$h"
  103. _debug _domain "$_domain"
  104. _debug _sub_domain "$_sub_domain"
  105. return 0
  106. fi
  107. previous="$i"
  108. i=$(_math "$i" + 1)
  109. done
  110. return 1
  111. }
  112. # returns _account_id
  113. _get_account_id() {
  114. _debug "retrive account id"
  115. if ! _dnsimple_rest GET "whoami"; then
  116. return 1
  117. fi
  118. if _contains "$response" "\"account\":null"; then
  119. _err "no account associated with this token"
  120. return 1
  121. fi
  122. if _contains "$response" "timeout"; then
  123. _err "timeout retrieving account id"
  124. return 1
  125. fi
  126. _account_id=$(printf "%s" "$response" | _egrep_o "\"id\":[^,]*,\"email\":" | cut -d: -f2 | cut -d, -f1)
  127. _debug _account_id "$_account_id"
  128. return 0
  129. }
  130. # returns
  131. # _records
  132. # _records_count
  133. _get_records() {
  134. account_id=$1
  135. domain=$2
  136. sub_domain=$3
  137. _debug "fetching txt records"
  138. _dnsimple_rest GET "$account_id/zones/$domain/records?per_page=100"
  139. if ! _contains "$response" "\"id\":"; then
  140. _err "failed to retrieve records"
  141. return 1
  142. fi
  143. _records_count=$(printf "%s" "$response" | _egrep_o "\"name\":\"$sub_domain\"" | wc -l | _egrep_o "[0-9]+")
  144. _records=$response
  145. _debug _records_count "$_records_count"
  146. }
  147. # returns _record_id
  148. _extract_record_id() {
  149. _record_id=$(printf "%s" "$_records" | _egrep_o "\"id\":[^,]*,\"zone_id\":\"[^,]*\",\"parent_id\":null,\"name\":\"$_sub_domain\"" | cut -d: -f2 | cut -d, -f1)
  150. _debug "_record_id" "$_record_id"
  151. }
  152. # returns response
  153. _dnsimple_rest() {
  154. method=$1
  155. path="$2"
  156. data="$3"
  157. request_url="$DNSimple_API/$path"
  158. _debug "$path"
  159. _H1="Accept: application/json"
  160. _H2="Authorization: Bearer $DNSimple_OAUTH_TOKEN"
  161. if [ "$data" ]; then
  162. _H1="Content-Type: application/json"
  163. _debug data "$data"
  164. response="$(_post "$data" "$request_url" "" "$method")"
  165. else
  166. response="$(_request "$request_url" "" "" "$method")"
  167. fi
  168. if [ "$?" != "0" ]; then
  169. _err "error $request_url"
  170. return 1
  171. fi
  172. _debug2 response "$response"
  173. return 0
  174. }