]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_dnsimple.sh
Merge pull request #4658 from Justman10000/master
[mirror_acme.sh.git] / dnsapi / dns_dnsimple.sh
CommitLineData
1994c682
MT
1#!/usr/bin/env sh
2
3# DNSimple domain api
326ac485 4# https://github.com/pho3nixf1re/acme.sh/issues
1994c682
MT
5#
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"
10
11DNSimple_API="https://api.dnsimple.com/v2"
12
13######## Public functions #####################
14
15# Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
16dns_dnsimple_add() {
17 fulldomain=$1
18 txtvalue=$2
19
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."
24 return 1
25 fi
26
27 # save the oauth token for later
28 _saveaccountconf DNSimple_OAUTH_TOKEN "$DNSimple_OAUTH_TOKEN"
29
1994c682
MT
30 if ! _get_account_id; then
31 _err "failed to retrive account id"
32 return 1
33 fi
1994c682
MT
34
35 if ! _get_root "$fulldomain"; then
36 _err "invalid domain"
37 return 1
38 fi
1994c682 39
2f4111a2 40 _get_records "$_account_id" "$_domain" "$_sub_domain"
1994c682 41
30283282
C
42 _info "Adding record"
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
45 _info "Added"
46 return 0
47 else
48 _err "Unexpected response while adding text record."
49 return 1
1994c682 50 fi
30283282
C
51 fi
52 _err "Add txt record error."
1994c682
MT
53}
54
55# fulldomain
56dns_dnsimple_rm() {
57 fulldomain=$1
58
f9b419d1
MT
59 if ! _get_account_id; then
60 _err "failed to retrive account id"
61 return 1
62 fi
63
64 if ! _get_root "$fulldomain"; then
65 _err "invalid domain"
66 return 1
67 fi
68
2f4111a2 69 _get_records "$_account_id" "$_domain" "$_sub_domain"
f9b419d1 70
7588fc09 71 _extract_record_id "$_records" "$_sub_domain"
f9b419d1 72 if [ "$_record_id" ]; then
30283282 73 echo "$_record_id" | while read -r item; do
7588fc09
C
74 if _dnsimple_rest DELETE "$_account_id/zones/$_domain/records/$item"; then
75 _info "removed record" "$item"
76 return 0
77 else
78 _err "failed to remove record" "$item"
79 return 1
80 fi
81 done
f9b419d1 82 fi
1994c682
MT
83}
84
85#################### Private functions bellow ##################################
86# _acme-challenge.www.domain.com
87# returns
88# _sub_domain=_acme-challenge.www
89# _domain=domain.com
90_get_root() {
91 domain=$1
92 i=2
f9b419d1 93 previous=1
1994c682
MT
94 while true; do
95 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
96 if [ -z "$h" ]; then
97 # not valid
98 return 1
99 fi
100
101 if ! _dnsimple_rest GET "$_account_id/zones/$h"; then
102 return 1
103 fi
104
105 if _contains "$response" 'not found'; then
106 _debug "$h not found"
107 else
f9b419d1 108 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$previous)
1994c682 109 _domain="$h"
f9b419d1
MT
110
111 _debug _domain "$_domain"
112 _debug _sub_domain "$_sub_domain"
113
1994c682
MT
114 return 0
115 fi
f9b419d1
MT
116
117 previous="$i"
1994c682
MT
118 i=$(_math "$i" + 1)
119 done
120 return 1
121}
122
f9b419d1 123# returns _account_id
1994c682 124_get_account_id() {
f9b419d1 125 _debug "retrive account id"
1994c682
MT
126 if ! _dnsimple_rest GET "whoami"; then
127 return 1
128 fi
129
130 if _contains "$response" "\"account\":null"; then
131 _err "no account associated with this token"
132 return 1
133 fi
134
135 if _contains "$response" "timeout"; then
f9b419d1 136 _err "timeout retrieving account id"
1994c682
MT
137 return 1
138 fi
139
140 _account_id=$(printf "%s" "$response" | _egrep_o "\"id\":[^,]*,\"email\":" | cut -d: -f2 | cut -d, -f1)
f9b419d1
MT
141 _debug _account_id "$_account_id"
142
1994c682
MT
143 return 0
144}
145
f9b419d1
MT
146# returns
147# _records
148# _records_count
149_get_records() {
150 account_id=$1
151 domain=$2
152 sub_domain=$3
153
154 _debug "fetching txt records"
c1ec2afe 155 _dnsimple_rest GET "$account_id/zones/$domain/records?per_page=5000&sort=id:desc"
f9b419d1
MT
156
157 if ! _contains "$response" "\"id\":"; then
158 _err "failed to retrieve records"
159 return 1
160 fi
161
162 _records_count=$(printf "%s" "$response" | _egrep_o "\"name\":\"$sub_domain\"" | wc -l | _egrep_o "[0-9]+")
163 _records=$response
164 _debug _records_count "$_records_count"
165}
166
167# returns _record_id
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"
171}
172
173# returns response
1994c682
MT
174_dnsimple_rest() {
175 method=$1
176 path="$2"
177 data="$3"
178 request_url="$DNSimple_API/$path"
179 _debug "$path"
180
2f4111a2
MT
181 export _H1="Accept: application/json"
182 export _H2="Authorization: Bearer $DNSimple_OAUTH_TOKEN"
183
53323871 184 if [ "$data" ] || [ "$method" = "DELETE" ]; then
1994c682
MT
185 _H1="Content-Type: application/json"
186 _debug data "$data"
187 response="$(_post "$data" "$request_url" "" "$method")"
188 else
53323871 189 response="$(_get "$request_url" "" "" "$method")"
1994c682
MT
190 fi
191
192 if [ "$?" != "0" ]; then
193 _err "error $request_url"
194 return 1
195 fi
196 _debug2 response "$response"
197 return 0
198}