dns_dnsimple.sh 4.7 KB

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