4 # https://github.com/pho3nixf1re/acme.sh/issues
6 # This is your oauth token which can be acquired on the account page. Please
7 # note that this must be an _account_ token and not a _user_ token.
8 # https://dnsimple.com/a/<your account id>/account/access_tokens
9 # DNSimple_OAUTH_TOKEN="sdfsdfsdfljlbjkljlkjsdfoiwje"
11 DNSimple_API
="https://api.dnsimple.com/v2"
13 ######## Public functions #####################
15 # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
20 if [ -z "$DNSimple_OAUTH_TOKEN" ]; then
21 DNSimple_OAUTH_TOKEN
=""
22 _err
"You have not set the dnsimple oauth token yet."
23 _err
"Please visit https://dnsimple.com/user to generate it."
27 # save the oauth token for later
28 _saveaccountconf DNSimple_OAUTH_TOKEN
"$DNSimple_OAUTH_TOKEN"
30 if ! _get_account_id
; then
31 _err
"failed to retrive account id"
35 if ! _get_root
"$fulldomain"; then
40 _get_records
"$_account_id" "$_domain" "$_sub_domain"
43 if _dnsimple_rest POST
"$_account_id/zones/$_domain/records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
44 if printf -- "%s" "$response" |
grep "\"name\":\"$_sub_domain\"" >/dev
/null
; then
48 _err
"Unexpected response while adding text record."
52 _err
"Add txt record error."
59 if ! _get_account_id
; then
60 _err
"failed to retrive account id"
64 if ! _get_root
"$fulldomain"; then
69 _get_records
"$_account_id" "$_domain" "$_sub_domain"
71 _extract_record_id
"$_records" "$_sub_domain"
72 if [ "$_record_id" ]; then
73 echo "$_record_id" |
while read -r item
; do
74 if _dnsimple_rest DELETE
"$_account_id/zones/$_domain/records/$item"; then
75 _info
"removed record" "$item"
78 _err
"failed to remove record" "$item"
85 #################### Private functions bellow ##################################
86 # _acme-challenge.www.domain.com
88 # _sub_domain=_acme-challenge.www
95 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
101 if ! _dnsimple_rest GET
"$_account_id/zones/$h"; then
105 if _contains
"$response" 'not found'; then
106 _debug
"$h not found"
108 _sub_domain
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$previous)
111 _debug _domain
"$_domain"
112 _debug _sub_domain
"$_sub_domain"
123 # returns _account_id
125 _debug
"retrive account id"
126 if ! _dnsimple_rest GET
"whoami"; then
130 if _contains
"$response" "\"account\":null"; then
131 _err
"no account associated with this token"
135 if _contains
"$response" "timeout"; then
136 _err
"timeout retrieving account id"
140 _account_id
=$
(printf "%s" "$response" | _egrep_o
"\"id\":[^,]*,\"email\":" | cut
-d: -f2 | cut
-d, -f1)
141 _debug _account_id
"$_account_id"
154 _debug
"fetching txt records"
155 _dnsimple_rest GET
"$account_id/zones/$domain/records?per_page=5000&sort=id:desc"
157 if ! _contains
"$response" "\"id\":"; then
158 _err
"failed to retrieve records"
162 _records_count
=$
(printf "%s" "$response" | _egrep_o
"\"name\":\"$sub_domain\"" |
wc -l | _egrep_o
"[0-9]+")
164 _debug _records_count
"$_records_count"
168 _extract_record_id
() {
169 _record_id
=$
(printf "%s" "$_records" | _egrep_o
"\"id\":[^,]*,\"zone_id\":\"[^,]*\",\"parent_id\":null,\"name\":\"$_sub_domain\"" | cut
-d: -f2 | cut
-d, -f1)
170 _debug
"_record_id" "$_record_id"
178 request_url
="$DNSimple_API/$path"
181 export _H1
="Accept: application/json"
182 export _H2
="Authorization: Bearer $DNSimple_OAUTH_TOKEN"
184 if [ "$data" ] ||
[ "$method" = "DELETE" ]; then
185 _H1
="Content-Type: application/json"
187 response
="$(_post "$data" "$request_url" "" "$method")"
189 response
="$(_get "$request_url" "" "" "$method")"
192 if [ "$?" != "0" ]; then
193 _err
"error $request_url"
196 _debug2 response
"$response"