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.

211 lines
5.0 KiB

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