]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_ovh.sh
Correct edits to README.md this time
[mirror_acme.sh.git] / dnsapi / dns_ovh.sh
1 #!/usr/bin/env sh
2
3 #Application Key
4 #OVH_AK="sdfsdfsdfljlbjkljlkjsdfoiwje"
5 #
6 #Application Secret
7 #OVH_AS="sdfsafsdfsdfdsfsdfsa"
8 #
9 #Consumer Key
10 #OVH_CK="sdfsdfsdfsdfsdfdsf"
11
12 #OVH_END_POINT=ovh-eu
13
14 #'ovh-eu'
15 OVH_EU='https://eu.api.ovh.com/1.0'
16
17 #'ovh-ca':
18 OVH_CA='https://ca.api.ovh.com/1.0'
19
20 #'kimsufi-eu'
21 KSF_EU='https://eu.api.kimsufi.com/1.0'
22
23 #'kimsufi-ca'
24 KSF_CA='https://ca.api.kimsufi.com/1.0'
25
26 #'soyoustart-eu'
27 SYS_EU='https://eu.api.soyoustart.com/1.0'
28
29 #'soyoustart-ca'
30 SYS_CA='https://ca.api.soyoustart.com/1.0'
31
32 #'runabove-ca'
33 RAV_CA='https://api.runabove.com/1.0'
34
35 wiki="https://github.com/Neilpang/acme.sh/wiki/How-to-use-OVH-domain-api"
36
37 ovh_success="https://github.com/Neilpang/acme.sh/wiki/OVH-Success"
38
39 _ovh_get_api() {
40 _ogaep="$1"
41
42 case "${_ogaep}" in
43
44 ovh-eu | ovheu)
45 printf "%s" $OVH_EU
46 return
47 ;;
48 ovh-ca | ovhca)
49 printf "%s" $OVH_CA
50 return
51 ;;
52 kimsufi-eu | kimsufieu)
53 printf "%s" $KSF_EU
54 return
55 ;;
56 kimsufi-ca | kimsufica)
57 printf "%s" $KSF_CA
58 return
59 ;;
60 soyoustart-eu | soyoustarteu)
61 printf "%s" $SYS_EU
62 return
63 ;;
64 soyoustart-ca | soyoustartca)
65 printf "%s" $SYS_CA
66 return
67 ;;
68 runabove-ca | runaboveca)
69 printf "%s" $RAV_CA
70 return
71 ;;
72
73 *)
74
75 _err "Unknown parameter : $1"
76 return 1
77 ;;
78 esac
79 }
80
81 _initAuth() {
82 OVH_AK="${OVH_AK:-$(_readaccountconf_mutable OVH_AK)}"
83 OVH_AS="${OVH_AS:-$(_readaccountconf_mutable OVH_AS)}"
84
85 if [ -z "$OVH_AK" ] || [ -z "$OVH_AS" ]; then
86 OVH_AK=""
87 OVH_AS=""
88 _err "You don't specify OVH application key and application secret yet."
89 _err "Please create you key and try again."
90 return 1
91 fi
92
93 if [ "$OVH_AK" != "$(_readaccountconf OVH_AK)" ]; then
94 _info "It seems that your ovh key is changed, let's clear consumer key first."
95 _clearaccountconf OVH_CK
96 fi
97 _saveaccountconf_mutable OVH_AK "$OVH_AK"
98 _saveaccountconf_mutable OVH_AS "$OVH_AS"
99
100 OVH_END_POINT="${OVH_END_POINT:-$(_readaccountconf_mutable OVH_END_POINT)}"
101 if [ -z "$OVH_END_POINT" ]; then
102 OVH_END_POINT="ovh-eu"
103 fi
104 _info "Using OVH endpoint: $OVH_END_POINT"
105 if [ "$OVH_END_POINT" != "ovh-eu" ]; then
106 _saveaccountconf_mutable OVH_END_POINT "$OVH_END_POINT"
107 fi
108
109 OVH_API="$(_ovh_get_api $OVH_END_POINT)"
110 _debug OVH_API "$OVH_API"
111
112 OVH_CK="${OVH_CK:-$(_readaccountconf_mutable OVH_CK)}"
113 if [ -z "$OVH_CK" ]; then
114 _info "OVH consumer key is empty, Let's get one:"
115 if ! _ovh_authentication; then
116 _err "Can not get consumer key."
117 fi
118 #return and wait for retry.
119 return 1
120 fi
121
122 _info "Checking authentication"
123
124 if ! _ovh_rest GET "domain" || _contains "$response" "INVALID_CREDENTIAL"; then
125 _err "The consumer key is invalid: $OVH_CK"
126 _err "Please retry to create a new one."
127 _clearaccountconf OVH_CK
128 return 1
129 fi
130 _info "Consumer key is ok."
131 return 0
132 }
133
134 ######## Public functions #####################
135
136 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
137 dns_ovh_add() {
138 fulldomain=$1
139 txtvalue=$2
140
141 if ! _initAuth; then
142 return 1
143 fi
144
145 _debug "First detect the root zone"
146 if ! _get_root "$fulldomain"; then
147 _err "invalid domain"
148 return 1
149 fi
150
151 _debug _sub_domain "$_sub_domain"
152 _debug _domain "$_domain"
153
154 _info "Adding record"
155 if _ovh_rest POST "domain/zone/$_domain/record" "{\"fieldType\":\"TXT\",\"subDomain\":\"$_sub_domain\",\"target\":\"$txtvalue\",\"ttl\":60}"; then
156 if _contains "$response" "$txtvalue"; then
157 _ovh_rest POST "domain/zone/$_domain/refresh"
158 _debug "Refresh:$response"
159 _info "Added, sleep 10 seconds."
160 _sleep 10
161 return 0
162 fi
163 fi
164 _err "Add txt record error."
165 return 1
166
167 }
168
169 #fulldomain
170 dns_ovh_rm() {
171 fulldomain=$1
172 txtvalue=$2
173
174 if ! _initAuth; then
175 return 1
176 fi
177
178 _debug "First detect the root zone"
179 if ! _get_root "$fulldomain"; then
180 _err "invalid domain"
181 return 1
182 fi
183
184 _debug _sub_domain "$_sub_domain"
185 _debug _domain "$_domain"
186 _debug "Getting txt records"
187 if ! _ovh_rest GET "domain/zone/$_domain/record?fieldType=TXT&subDomain=$_sub_domain"; then
188 return 1
189 fi
190
191 for rid in $(echo "$response" | tr '][,' ' '); do
192 _debug rid "$rid"
193 if ! _ovh_rest GET "domain/zone/$_domain/record/$rid"; then
194 return 1
195 fi
196 if _contains "$response" "\"target\":\"$txtvalue\""; then
197 _debug "Found txt id:$rid"
198 if ! _ovh_rest DELETE "domain/zone/$_domain/record/$rid"; then
199 return 1
200 fi
201 return 0
202 fi
203 done
204
205 return 1
206 }
207
208 #################### Private functions below ##################################
209
210 _ovh_authentication() {
211
212 _H1="X-Ovh-Application: $OVH_AK"
213 _H2="Content-type: application/json"
214 _H3=""
215 _H4=""
216
217 _ovhdata='{"accessRules": [{"method": "GET","path": "/auth/time"},{"method": "GET","path": "/domain"},{"method": "GET","path": "/domain/zone/*"},{"method": "GET","path": "/domain/zone/*/record"},{"method": "POST","path": "/domain/zone/*/record"},{"method": "POST","path": "/domain/zone/*/refresh"},{"method": "PUT","path": "/domain/zone/*/record/*"},{"method": "DELETE","path": "/domain/zone/*/record/*"}],"redirection":"'$ovh_success'"}'
218
219 response="$(_post "$_ovhdata" "$OVH_API/auth/credential")"
220 _debug3 response "$response"
221 validationUrl="$(echo "$response" | _egrep_o "validationUrl\":\"[^\"]*\"" | _egrep_o "http.*\"" | tr -d '"')"
222 if [ -z "$validationUrl" ]; then
223 _err "Unable to get validationUrl"
224 return 1
225 fi
226 _debug validationUrl "$validationUrl"
227
228 consumerKey="$(echo "$response" | _egrep_o "consumerKey\":\"[^\"]*\"" | cut -d : -f 2 | tr -d '"')"
229 if [ -z "$consumerKey" ]; then
230 _err "Unable to get consumerKey"
231 return 1
232 fi
233 _secure_debug consumerKey "$consumerKey"
234
235 OVH_CK="$consumerKey"
236 _saveaccountconf OVH_CK "$OVH_CK"
237
238 _info "Please open this link to do authentication: $(__green "$validationUrl")"
239
240 _info "Here is a guide for you: $(__green "$wiki")"
241 _info "Please retry after the authentication is done."
242
243 }
244
245 #_acme-challenge.www.domain.com
246 #returns
247 # _sub_domain=_acme-challenge.www
248 # _domain=domain.com
249 _get_root() {
250 domain=$1
251 i=2
252 p=1
253 while true; do
254 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
255 if [ -z "$h" ]; then
256 #not valid
257 return 1
258 fi
259
260 if ! _ovh_rest GET "domain/zone/$h"; then
261 return 1
262 fi
263
264 if ! _contains "$response" "This service does not exist" >/dev/null && ! _contains "$response" "NOT_GRANTED_CALL" >/dev/null; then
265 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
266 _domain="$h"
267 return 0
268 fi
269 p=$i
270 i=$(_math "$i" + 1)
271 done
272 return 1
273 }
274
275 _ovh_timestamp() {
276 _H1=""
277 _H2=""
278 _H3=""
279 _H4=""
280 _H5=""
281 _get "$OVH_API/auth/time" "" 30
282 }
283
284 _ovh_rest() {
285 m=$1
286 ep="$2"
287 data="$3"
288 _debug "$ep"
289
290 _ovh_url="$OVH_API/$ep"
291 _debug2 _ovh_url "$_ovh_url"
292 _ovh_t="$(_ovh_timestamp)"
293 _debug2 _ovh_t "$_ovh_t"
294 _ovh_p="$OVH_AS+$OVH_CK+$m+$_ovh_url+$data+$_ovh_t"
295 _secure_debug _ovh_p "$_ovh_p"
296 _ovh_hex="$(printf "%s" "$_ovh_p" | _digest sha1 hex)"
297 _debug2 _ovh_hex "$_ovh_hex"
298
299 export _H1="X-Ovh-Application: $OVH_AK"
300 export _H2="X-Ovh-Signature: \$1\$$_ovh_hex"
301 _debug2 _H2 "$_H2"
302 export _H3="X-Ovh-Timestamp: $_ovh_t"
303 export _H4="X-Ovh-Consumer: $OVH_CK"
304 export _H5="Content-Type: application/json;charset=utf-8"
305 if [ "$data" ] || [ "$m" = "POST" ] || [ "$m" = "PUT" ] || [ "$m" = "DELETE" ]; then
306 _debug data "$data"
307 response="$(_post "$data" "$_ovh_url" "" "$m")"
308 else
309 response="$(_get "$_ovh_url")"
310 fi
311
312 if [ "$?" != "0" ] || _contains "$response" "INVALID_CREDENTIAL"; then
313 _err "error $response"
314 return 1
315 fi
316 _debug2 response "$response"
317 return 0
318 }