]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_ovh.sh
Merge pull request #745 from jsoref/spelling
[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 ######## Public functions #####################
82
83 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
84 dns_ovh_add() {
85 fulldomain=$1
86 txtvalue=$2
87
88 if [ -z "$OVH_AK" ] || [ -z "$OVH_AS" ]; then
89 OVH_AK=""
90 OVH_AS=""
91 _err "You don't specify OVH application key and application secret yet."
92 _err "Please create you key and try again."
93 return 1
94 fi
95
96 #save the api key and email to the account conf file.
97 _saveaccountconf OVH_AK "$OVH_AK"
98 _saveaccountconf OVH_AS "$OVH_AS"
99
100 if [ -z "$OVH_END_POINT" ]; then
101 OVH_END_POINT="ovh-eu"
102 fi
103 _info "Using OVH endpoint: $OVH_END_POINT"
104 if [ "$OVH_END_POINT" != "ovh-eu" ]; then
105 _saveaccountconf OVH_END_POINT "$OVH_END_POINT"
106 fi
107
108 OVH_API="$(_ovh_get_api $OVH_END_POINT)"
109 _debug OVH_API "$OVH_API"
110
111 if [ -z "$OVH_CK" ]; then
112 _info "OVH consumer key is empty, Let's get one:"
113 if ! _ovh_authentication; then
114 _err "Can not get consumer key."
115 fi
116 #return and wait for retry.
117 return 1
118 fi
119
120 _info "Checking authentication"
121
122 response="$(_ovh_rest GET "domain/")"
123 if _contains "$response" "INVALID_CREDENTIAL"; then
124 _err "The consumer key is invalid: $OVH_CK"
125 _err "Please retry to create a new one."
126 _clearaccountconf OVH_CK
127 return 1
128 fi
129 _info "Consumer key is ok."
130
131 _debug "First detect the root zone"
132 if ! _get_root "$fulldomain"; then
133 _err "invalid domain"
134 return 1
135 fi
136
137 _debug _sub_domain "$_sub_domain"
138 _debug _domain "$_domain"
139
140 _debug "Getting txt records"
141 _ovh_rest GET "domain/zone/$_domain/record?fieldType=TXT&subDomain=$_sub_domain"
142
143 if _contains "$response" '\[\]' || _contains "$response" "This service does not exist"; then
144 _info "Adding record"
145 if _ovh_rest POST "domain/zone/$_domain/record" "{\"fieldType\":\"TXT\",\"subDomain\":\"$_sub_domain\",\"target\":\"$txtvalue\",\"ttl\":60}"; then
146 if _contains "$response" "$txtvalue"; then
147 _ovh_rest POST "domain/zone/$_domain/refresh"
148 _debug "Refresh:$response"
149 _info "Added, sleeping 10 seconds"
150 sleep 10
151 return 0
152 fi
153 fi
154 _err "Add txt record error."
155 else
156 _info "Updating record"
157 record_id=$(printf "%s" "$response" | tr -d "[]" | cut -d , -f 1)
158 if [ -z "$record_id" ]; then
159 _err "Can not get record id."
160 return 1
161 fi
162 _debug "record_id" "$record_id"
163
164 if _ovh_rest PUT "domain/zone/$_domain/record/$record_id" "{\"target\":\"$txtvalue\",\"subDomain\":\"$_sub_domain\",\"ttl\":60}"; then
165 if _contains "$response" "null"; then
166 _ovh_rest POST "domain/zone/$_domain/refresh"
167 _debug "Refresh:$response"
168 _info "Updated, sleeping 10 seconds"
169 sleep 10
170 return 0
171 fi
172 fi
173 _err "Update error"
174 return 1
175 fi
176
177 }
178
179 #fulldomain
180 dns_ovh_rm() {
181 fulldomain=$1
182
183 }
184
185 #################### Private functions below ##################################
186
187 _ovh_authentication() {
188
189 _H1="X-Ovh-Application: $OVH_AK"
190 _H2="Content-type: application/json"
191 _H3=""
192 _H4=""
193
194 _ovhdata='{"accessRules": [{"method": "GET","path": "/*"},{"method": "POST","path": "/*"},{"method": "PUT","path": "/*"},{"method": "DELETE","path": "/*"}],"redirection":"'$ovh_success'"}'
195
196 response="$(_post "$_ovhdata" "$OVH_API/auth/credential")"
197 _debug3 response "$response"
198 validationUrl="$(echo "$response" | _egrep_o "validationUrl\":\"[^\"]*\"" | _egrep_o "http.*\"" | tr -d '"')"
199 if [ -z "$validationUrl" ]; then
200 _err "Unable to get validationUrl"
201 return 1
202 fi
203 _debug validationUrl "$validationUrl"
204
205 consumerKey="$(echo "$response" | _egrep_o "consumerKey\":\"[^\"]*\"" | cut -d : -f 2 | tr -d '"')"
206 if [ -z "$consumerKey" ]; then
207 _err "Unable to get consumerKey"
208 return 1
209 fi
210 _secure_debug consumerKey "$consumerKey"
211
212 OVH_CK="$consumerKey"
213 _saveaccountconf OVH_CK "$OVH_CK"
214
215 _info "Please open this link to do authentication: $(__green "$validationUrl")"
216
217 _info "Here is a guide for you: $(__green "$wiki")"
218 _info "Please retry after the authentication is done."
219
220 }
221
222 #_acme-challenge.www.domain.com
223 #returns
224 # _sub_domain=_acme-challenge.www
225 # _domain=domain.com
226 _get_root() {
227 domain=$1
228 i=2
229 p=1
230 while true; do
231 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
232 if [ -z "$h" ]; then
233 #not valid
234 return 1
235 fi
236
237 if ! _ovh_rest GET "domain/zone/$h"; then
238 return 1
239 fi
240
241 if ! _contains "$response" "This service does not exist" >/dev/null; then
242 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
243 _domain="$h"
244 return 0
245 fi
246 p=$i
247 i=$(_math "$i" + 1)
248 done
249 return 1
250 }
251
252 _ovh_timestamp() {
253 _H1=""
254 _H2=""
255 _H3=""
256 _H4=""
257 _H5=""
258 _get "$OVH_API/auth/time" "" 30
259 }
260
261 _ovh_rest() {
262 m=$1
263 ep="$2"
264 data="$3"
265 _debug "$ep"
266
267 _ovh_url="$OVH_API/$ep"
268 _debug2 _ovh_url "$_ovh_url"
269 _ovh_t="$(_ovh_timestamp)"
270 _debug2 _ovh_t "$_ovh_t"
271 _ovh_p="$OVH_AS+$OVH_CK+$m+$_ovh_url+$data+$_ovh_t"
272 _secure_debug _ovh_p "$_ovh_p"
273 _ovh_hex="$(printf "%s" "$_ovh_p" | _digest sha1 hex)"
274 _debug2 _ovh_hex "$_ovh_hex"
275
276 export _H1="X-Ovh-Application: $OVH_AK"
277 export _H2="X-Ovh-Signature: \$1\$$_ovh_hex"
278 _debug2 _H2 "$_H2"
279 export _H3="X-Ovh-Timestamp: $_ovh_t"
280 export _H4="X-Ovh-Consumer: $OVH_CK"
281 export _H5="Content-Type: application/json;charset=utf-8"
282 if [ "$data" ] || [ "$m" = "POST" ] || [ "$m" = "PUT" ]; then
283 _debug data "$data"
284 response="$(_post "$data" "$_ovh_url" "" "$m")"
285 else
286 response="$(_get "$_ovh_url")"
287 fi
288
289 if [ "$?" != "0" ]; then
290 _err "error $ep"
291 return 1
292 fi
293 _debug2 response "$response"
294 return 0
295 }