]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_dynu.sh
Add support for Google Domains DNS API.
[mirror_acme.sh.git] / dnsapi / dns_dynu.sh
CommitLineData
70442368 1#!/usr/bin/env sh
2
70442368 3#Client ID
e137792e 4#Dynu_ClientId="0b71cae7-a099-4f6b-8ddf-94571cdb760d"
70442368 5#
6#Secret
e137792e 7#Dynu_Secret="aCUEY4BDCV45KI8CSIC3sp2LKQ9"
70442368 8#
9#Token
10Dynu_Token=""
11#
12#Endpoint
68c5c366 13Dynu_EndPoint="https://api.dynu.com/v2"
695482de 14#
6d7f6750 15#Author: Dynu Systems, Inc.
16#Report Bugs here: https://github.com/shar0119/acme.sh
695482de 17#
70442368 18######## Public functions #####################
19
20#Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
21dns_dynu_add() {
22 fulldomain=$1
23 txtvalue=$2
24
25 if [ -z "$Dynu_ClientId" ] || [ -z "$Dynu_Secret" ]; then
26 Dynu_ClientId=""
27 Dynu_Secret=""
28 _err "Dynu client id and secret is not specified."
29 _err "Please create you API client id and secret and try again."
30 return 1
31 fi
32
33 #save the client id and secret to the account conf file.
34 _saveaccountconf Dynu_ClientId "$Dynu_ClientId"
35 _saveaccountconf Dynu_Secret "$Dynu_Secret"
36
37 if [ -z "$Dynu_Token" ]; then
9a61d629 38 _info "Getting Dynu token."
70442368 39 if ! _dynu_authentication; then
40 _err "Can not get token."
41 fi
42 fi
43
44 _debug "Detect root zone"
45 if ! _get_root "$fulldomain"; then
9a61d629 46 _err "Invalid domain."
70442368 47 return 1
b7b93491 48 fi
70442368 49
50 _debug _node "$_node"
51 _debug _domain_name "$_domain_name"
52
9a61d629 53 _info "Creating TXT record."
68c5c366 54 if ! _dynu_rest POST "dns/$dnsId/record" "{\"domainId\":\"$dnsId\",\"nodeName\":\"$_node\",\"recordType\":\"TXT\",\"textData\":\"$txtvalue\",\"state\":true,\"ttl\":90}"; then
70442368 55 return 1
56 fi
57
68c5c366 58 if ! _contains "$response" "200"; then
9a61d629 59 _err "Could not add TXT record."
70442368 60 return 1
61 fi
62
63 return 0
64}
65
9a61d629 66#Usage: rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
70442368 67dns_dynu_rm() {
68 fulldomain=$1
9a61d629 69 txtvalue=$2
70
71 if [ -z "$Dynu_ClientId" ] || [ -z "$Dynu_Secret" ]; then
72 Dynu_ClientId=""
73 Dynu_Secret=""
74 _err "Dynu client id and secret is not specified."
75 _err "Please create you API client id and secret and try again."
76 return 1
77 fi
78
79 #save the client id and secret to the account conf file.
80 _saveaccountconf Dynu_ClientId "$Dynu_ClientId"
81 _saveaccountconf Dynu_Secret "$Dynu_Secret"
82
83 if [ -z "$Dynu_Token" ]; then
84 _info "Getting Dynu token."
85 if ! _dynu_authentication; then
86 _err "Can not get token."
87 fi
88 fi
89
90 _debug "Detect root zone."
91 if ! _get_root "$fulldomain"; then
92 _err "Invalid domain."
93 return 1
94 fi
95
96 _debug _node "$_node"
97 _debug _domain_name "$_domain_name"
98
99 _info "Checking for TXT record."
100 if ! _get_recordid "$fulldomain" "$txtvalue"; then
101 _err "Could not get TXT record id."
102 return 1
103 fi
104
105 if [ "$_dns_record_id" = "" ]; then
106 _err "TXT record not found."
107 return 1
108 fi
109
110 _info "Removing TXT record."
111 if ! _delete_txt_record "$_dns_record_id"; then
112 _err "Could not remove TXT record $_dns_record_id."
113 fi
114
394b1002 115 return 0
70442368 116}
117
118######## Private functions below ##################################
119#_acme-challenge.www.domain.com
120#returns
121# _node=_acme-challenge.www
122# _domain_name=domain.com
123_get_root() {
124 domain=$1
eef4acd0 125 i=2
126 p=1
127 while true; do
128 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
129 _debug h "$h"
130 if [ -z "$h" ]; then
131 #not valid
132 return 1
133 fi
70442368 134
68c5c366 135 if ! _dynu_rest GET "dns/getroot/$h"; then
eef4acd0 136 return 1
137 fi
138
68c5c366
BE
139 if _contains "$response" "\"domainName\":\"$h\"" >/dev/null; then
140 dnsId=$(printf "%s" "$response" | tr -d "{}" | cut -d , -f 2 | cut -d : -f 2)
eef4acd0 141 _domain_name=$h
142 _node=$(printf "%s" "$domain" | cut -d . -f 1-$p)
143 return 0
144 fi
145 p=$i
146 i=$(_math "$i" + 1)
147 done
148 return 1
70442368 149
70442368 150}
151
9a61d629 152_get_recordid() {
153 fulldomain=$1
154 txtvalue=$2
155
68c5c366 156 if ! _dynu_rest GET "dns/$dnsId/record"; then
9a61d629 157 return 1
158 fi
159
160 if ! _contains "$response" "$txtvalue"; then
161 _dns_record_id=0
162 return 0
163 fi
164
68c5c366 165 _dns_record_id=$(printf "%s" "$response" | sed -e 's/[^{]*\({[^}]*}\)[^{]*/\1\n/g' | grep "\"textData\":\"$txtvalue\"" | sed -e 's/.*"id":\([^,]*\).*/\1/')
9a61d629 166 return 0
167}
168
169_delete_txt_record() {
170 _dns_record_id=$1
171
68c5c366 172 if ! _dynu_rest DELETE "dns/$dnsId/record/$_dns_record_id"; then
9a61d629 173 return 1
174 fi
175
68c5c366 176 if ! _contains "$response" "200"; then
9a61d629 177 return 1
178 fi
179
180 return 0
181}
182
70442368 183_dynu_rest() {
184 m=$1
185 ep="$2"
186 data="$3"
187 _debug "$ep"
188
189 export _H1="Authorization: Bearer $Dynu_Token"
190 export _H2="Content-Type: application/json"
191
68c5c366 192 if [ "$data" ] || [ "$m" = "DELETE" ]; then
70442368 193 _debug data "$data"
194 response="$(_post "$data" "$Dynu_EndPoint/$ep" "" "$m")"
195 else
9a61d629 196 _info "Getting $Dynu_EndPoint/$ep"
70442368 197 response="$(_get "$Dynu_EndPoint/$ep")"
198 fi
199
200 if [ "$?" != "0" ]; then
201 _err "error $ep"
202 return 1
203 fi
204 _debug2 response "$response"
205 return 0
206}
207
208_dynu_authentication() {
cd8fcbf9 209 realm="$(printf "%s" "$Dynu_ClientId:$Dynu_Secret" | _base64)"
b7b93491 210
d0300d44 211 export _H1="Authorization: Basic $realm"
70442368 212 export _H2="Content-Type: application/json"
213
214 response="$(_get "$Dynu_EndPoint/oauth2/token")"
215 if [ "$?" != "0" ]; then
216 _err "Authentication failed."
217 return 1
218 fi
f8027658 219 if _contains "$response" "Authentication Exception"; then
220 _err "Authentication failed."
221 return 1
222 fi
68c5c366
BE
223 if _contains "$response" "access_token"; then
224 Dynu_Token=$(printf "%s" "$response" | tr -d "{}" | cut -d , -f 1 | cut -d : -f 2 | cut -d '"' -f 2)
70442368 225 fi
226 if _contains "$Dynu_Token" "null"; then
227 Dynu_Token=""
228 fi
b7b93491 229
70442368 230 _debug2 response "$response"
231 return 0
232}