]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_dynu.sh
Merge pull request #726 from shar0119/patch-1
[mirror_acme.sh.git] / dnsapi / dns_dynu.sh
1 #!/usr/bin/env sh
2
3 #Client ID
4 #Dynu_ClientId="0b71cae7-a099-4f6b-8ddf-94571cdb760d"
5 #
6 #Secret
7 #Dynu_Secret="aCUEY4BDCV45KI8CSIC3sp2LKQ9"
8 #
9 #Token
10 Dynu_Token=""
11 #
12 #Endpoint
13 Dynu_EndPoint="https://api.dynu.com/v1"
14 #
15 #Author: Dynu Systems, Inc.
16 #Report Bugs here: https://github.com/shar0119/acme.sh
17 #
18 ######## Public functions #####################
19
20 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
21 dns_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
38 _info "Getting Dynu token."
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
46 _err "Invalid domain."
47 return 1
48 fi
49
50 _debug _node "$_node"
51 _debug _domain_name "$_domain_name"
52
53 _info "Creating TXT record."
54 if ! _dynu_rest POST "dns/record/add" "{\"domain_name\":\"$_domain_name\",\"node_name\":\"$_node\",\"record_type\":\"TXT\",\"text_data\":\"$txtvalue\",\"state\":true,\"ttl\":90}"; then
55 return 1
56 fi
57
58 if ! _contains "$response" "text_data"; then
59 _err "Could not add TXT record."
60 return 1
61 fi
62
63 return 0
64 }
65
66 #Usage: rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
67 dns_dynu_rm() {
68 fulldomain=$1
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
115 return 0
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
125 if ! _dynu_rest GET "dns/getroot/$domain"; then
126 return 1
127 fi
128
129 if ! _contains "$response" "domain_name"; then
130 _debug "Domain name not found."
131 return 1
132 fi
133
134 _domain_name=$(printf "%s" "$response" | tr -d "{}" | cut -d , -f 1 | cut -d : -f 2 | cut -d '"' -f 2)
135 _node=$(printf "%s" "$response" | tr -d "{}" | cut -d , -f 3 | cut -d : -f 2 | cut -d '"' -f 2)
136 return 0
137 }
138
139 _get_recordid() {
140 fulldomain=$1
141 txtvalue=$2
142
143 if ! _dynu_rest GET "dns/record/get?hostname=$fulldomain&rrtype=TXT"; then
144 return 1
145 fi
146
147 if ! _contains "$response" "$txtvalue"; then
148 _dns_record_id=0
149 return 0
150 fi
151
152 _dns_record_id=$(printf "%s" "$response" | _egrep_o "{[^}]*}" | grep "\"text_data\":\"$txtvalue\"" | grep -Po '"id":\K[0-9]+')
153
154 return 0
155 }
156
157 _delete_txt_record() {
158 _dns_record_id=$1
159
160 if ! _dynu_rest GET "dns/record/delete/$_dns_record_id"; then
161 return 1
162 fi
163
164 if ! _contains "$response" "true"; then
165 return 1
166 fi
167
168 return 0
169 }
170
171 _dynu_rest() {
172 m=$1
173 ep="$2"
174 data="$3"
175 _debug "$ep"
176
177 export _H1="Authorization: Bearer $Dynu_Token"
178 export _H2="Content-Type: application/json"
179
180 if [ "$data" ]; then
181 _debug data "$data"
182 response="$(_post "$data" "$Dynu_EndPoint/$ep" "" "$m")"
183 else
184 _info "Getting $Dynu_EndPoint/$ep"
185 response="$(_get "$Dynu_EndPoint/$ep")"
186 fi
187
188 if [ "$?" != "0" ]; then
189 _err "error $ep"
190 return 1
191 fi
192 _debug2 response "$response"
193 return 0
194 }
195
196 _dynu_authentication() {
197 realm="$(printf "%s" "$Dynu_ClientId:$Dynu_Secret" | _base64)"
198
199 export _H1="Authorization: Basic $realm"
200 export _H2="Content-Type: application/json"
201
202 response="$(_get "$Dynu_EndPoint/oauth2/token")"
203 if [ "$?" != "0" ]; then
204 _err "Authentication failed."
205 return 1
206 fi
207 if _contains "$response" "accessToken"; then
208 Dynu_Token=$(printf "%s" "$response" | tr -d "[]" | cut -d , -f 2 | cut -d : -f 2 | cut -d '"' -f 2)
209 fi
210 if _contains "$Dynu_Token" "null"; then
211 Dynu_Token=""
212 fi
213
214 _debug2 response "$response"
215 return 0
216 }