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