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