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.

163 lines
4.1 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. _debug "Retrive account ID"
  23. if ! _get_account_id; then
  24. _err "failed to retrive account id"
  25. return 1
  26. fi
  27. _debug _account_id "$_account_id"
  28. if ! _get_root "$fulldomain"; then
  29. _err "invalid domain"
  30. return 1
  31. fi
  32. _debug _domain "$_domain"
  33. _debug _sub_domain "$_sub_domain"
  34. _debug "Getting txt records"
  35. _dnsimple_rest GET "$_account_id/zones/$_domain/records?per_page=100"
  36. if ! _contains "$response" "\"id\":"; then
  37. _err "Error"
  38. return 1
  39. fi
  40. count=$(printf "%s" "$response" | _egrep_o "\"name\":\"$_sub_domain\"" | wc -l | _egrep_o "[0-9]+")
  41. _debug count "$count"
  42. if [ "$count" = "0" ]; then
  43. _info "Adding record"
  44. if _dnsimple_rest POST "$_account_id/zones/$_domain/records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  45. if printf -- "%s" "$response" | grep "\"name\":\"$_sub_domain\"" >/dev/null; then
  46. _info "Added"
  47. return 0
  48. else
  49. _err "Add txt record error."
  50. return 1
  51. fi
  52. fi
  53. _err "Add txt record error."
  54. else
  55. _info "Updating record"
  56. record_id=$(printf "%s" "$response" | _egrep_o "\"id\":[^,]*,\"zone_id\":\"[^,]*\",\"parent_id\":null,\"name\":\"$_sub_domain\"" | cut -d: -f2 | cut -d, -f1)
  57. _debug "record_id" "$record_id"
  58. _dnsimple_rest PATCH "$_account_id/zones/$_domain/records/$record_id" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":120}"
  59. if [ "$?" = "0" ]; then
  60. _info "Updated!"
  61. #todo: check if the record takes effect
  62. return 0
  63. fi
  64. _err "Update error"
  65. return 1
  66. fi
  67. }
  68. # fulldomain
  69. dns_dnsimple_rm() {
  70. fulldomain=$1
  71. }
  72. #################### Private functions bellow ##################################
  73. # _acme-challenge.www.domain.com
  74. # returns
  75. # _sub_domain=_acme-challenge.www
  76. # _domain=domain.com
  77. _get_root() {
  78. domain=$1
  79. i=2
  80. p=1
  81. while true; do
  82. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  83. if [ -z "$h" ]; then
  84. # not valid
  85. return 1
  86. fi
  87. if ! _dnsimple_rest GET "$_account_id/zones/$h"; then
  88. return 1
  89. fi
  90. if _contains "$response" 'not found'; then
  91. _debug "$h not found"
  92. else
  93. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  94. _domain="$h"
  95. return 0
  96. fi
  97. p="$i"
  98. i=$(_math "$i" + 1)
  99. done
  100. return 1
  101. }
  102. _get_account_id() {
  103. if ! _dnsimple_rest GET "whoami"; then
  104. return 1
  105. fi
  106. if _contains "$response" "\"account\":null"; then
  107. _err "no account associated with this token"
  108. return 1
  109. fi
  110. if _contains "$response" "timeout"; then
  111. _err "timeout retrieving account_id"
  112. return 1
  113. fi
  114. _account_id=$(printf "%s" "$response" | _egrep_o "\"id\":[^,]*,\"email\":" | cut -d: -f2 | cut -d, -f1)
  115. return 0
  116. }
  117. _dnsimple_rest() {
  118. method=$1
  119. path="$2"
  120. data="$3"
  121. request_url="$DNSimple_API/$path"
  122. _debug "$path"
  123. _H1="Accept: application/json"
  124. _H2="Authorization: Bearer $DNSimple_OAUTH_TOKEN"
  125. if [ "$data" ]; then
  126. _H1="Content-Type: application/json"
  127. _debug data "$data"
  128. response="$(_post "$data" "$request_url" "" "$method")"
  129. else
  130. response="$(_get "$request_url")"
  131. fi
  132. if [ "$?" != "0" ]; then
  133. _err "error $request_url"
  134. return 1
  135. fi
  136. _debug2 response "$response"
  137. return 0
  138. }