]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_dnsimple.sh
fix: fix shellcheck
[mirror_acme.sh.git] / dnsapi / dns_dnsimple.sh
1 #!/usr/bin/env sh
2
3 # DNSimple domain api
4 # https://github.com/pho3nixf1re/acme.sh/issues
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
11 DNSimple_API="https://api.dnsimple.com/v2"
12
13 ######## Public functions #####################
14
15 # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
16 dns_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
30 if ! _get_account_id; then
31 _err "failed to retrive account id"
32 return 1
33 fi
34
35 if ! _get_root "$fulldomain"; then
36 _err "invalid domain"
37 return 1
38 fi
39
40 _get_records "$_account_id" "$_domain" "$_sub_domain"
41
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
50 fi
51 fi
52 _err "Add txt record error."
53 }
54
55 # fulldomain
56 dns_dnsimple_rm() {
57 fulldomain=$1
58
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
69 _get_records "$_account_id" "$_domain" "$_sub_domain"
70
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"
76 return 0
77 else
78 _err "failed to remove record" "$item"
79 return 1
80 fi
81 done
82 fi
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
93 previous=1
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
108 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$previous)
109 _domain="$h"
110
111 _debug _domain "$_domain"
112 _debug _sub_domain "$_sub_domain"
113
114 return 0
115 fi
116
117 previous="$i"
118 i=$(_math "$i" + 1)
119 done
120 return 1
121 }
122
123 # returns _account_id
124 _get_account_id() {
125 _debug "retrive account id"
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
136 _err "timeout retrieving account id"
137 return 1
138 fi
139
140 _account_id=$(printf "%s" "$response" | _egrep_o "\"id\":[^,]*,\"email\":" | cut -d: -f2 | cut -d, -f1)
141 _debug _account_id "$_account_id"
142
143 return 0
144 }
145
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"
155 _dnsimple_rest GET "$account_id/zones/$domain/records?per_page=5000&sort=id:desc"
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
174 _dnsimple_rest() {
175 method=$1
176 path="$2"
177 data="$3"
178 request_url="$DNSimple_API/$path"
179 _debug "$path"
180
181 export _H1="Accept: application/json"
182 export _H2="Authorization: Bearer $DNSimple_OAUTH_TOKEN"
183
184 if [ "$data" ] || [ "$method" = "DELETE" ]; then
185 _H1="Content-Type: application/json"
186 _debug data "$data"
187 response="$(_post "$data" "$request_url" "" "$method")"
188 else
189 response="$(_get "$request_url" "" "" "$method")"
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 }