]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_online.sh
use read/saveconf_mutable, not readconf from OVH
[mirror_acme.sh.git] / dnsapi / dns_online.sh
CommitLineData
ec5fad43
AF
1#!/usr/bin/env sh
2
3# Online API
4# https://console.online.net/en/api/
5#
6# Requires Online API key set in ONLINE_API_KEY
7
8######## Public functions #####################
9
10ONLINE_API="https://api.online.net/api/v1"
11
12#Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
13dns_online_add() {
14 fulldomain=$1
15 txtvalue=$2
16
17 if ! _online_check_config; then
18 return 1
19 fi
20
21 _debug "First detect the root zone"
22 if ! _get_root "$fulldomain"; then
23 _err "invalid domain"
24 return 1
25 fi
26
27 _debug _sub_domain "$_sub_domain"
28 _debug _domain "$_domain"
29 _debug _real_dns_version "$_real_dns_version"
30
31 _info "Creating temporary zone version"
32 _online_create_temporary_zone_version
33 _info "Enabling temporary zone version"
34 _online_enable_zone "$_temporary_dns_version"
35
36 _info "Adding record"
37 _online_create_TXT_record "$_real_dns_version" "$_sub_domain" "$txtvalue"
38 _info "Disabling temporary version"
39 _online_enable_zone "$_real_dns_version"
40 _info "Destroying temporary version"
41 _online_destroy_zone "$_temporary_dns_version"
42
43 _info "Record added."
44 return 0
45}
46
47#fulldomain
48dns_online_rm() {
49 fulldomain=$1
50 txtvalue=$2
51
52 if ! _online_check_config; then
53 return 1
54 fi
55
56 _debug "First detect the root zone"
57 if ! _get_root "$fulldomain"; then
58 _err "invalid domain"
59 return 1
60 fi
61
62 _debug _sub_domain "$_sub_domain"
63 _debug _domain "$_domain"
64 _debug _real_dns_version "$_real_dns_version"
65
66 _debug "Getting txt records"
67 if ! _online_rest GET "domain/$_domain/version/active"; then
68 return 1
69 fi
70
71 rid=$(echo "$response" | _egrep_o "\"id\":[0-9]+,\"name\":\"$_sub_domain\",\"data\":\"\\\u0022$txtvalue\\\u0022\"" | cut -d ':' -f 2 | cut -d ',' -f 1)
72 _debug rid "$rid"
73 if [ -z "$rid" ]; then
74 return 1
75 fi
76
77 _info "Creating temporary zone version"
78 _online_create_temporary_zone_version
79 _info "Enabling temporary zone version"
80 _online_enable_zone "$_temporary_dns_version"
81
82 _info "Removing DNS record"
83 _online_rest DELETE "domain/$_domain/version/$_real_dns_version/zone/$rid"
84 _info "Disabling temporary version"
85 _online_enable_zone "$_real_dns_version"
86 _info "Destroying temporary version"
87 _online_destroy_zone "$_temporary_dns_version"
88
89 return 0
90}
91
92#################### Private functions below ##################################
93
94_online_check_config() {
02f6d4cb 95 ONLINE_API_KEY="${CF_Key:-$(_readaccountconf_mutable ONLINE_API_KEY)}"
ec5fad43
AF
96 if [ -z "$ONLINE_API_KEY" ]; then
97 _err "No API key specified for Online API."
98 _err "Create your key and export it as ONLINE_API_KEY"
99 return 1
100 fi
02f6d4cb
AF
101 if [ ! _online_rest GET "domain/" ]; then
102 _err "Invalid API key specified for Online API."
103 return 1
104 fi
ec5fad43 105
02f6d4cb 106 _saveaccountconf_mutable ONLINE_API_KEY "$ONLINE_API_KEY"
ec5fad43
AF
107
108 return 0
109}
110
111#_acme-challenge.www.domain.com
112#returns
113# _sub_domain=_acme-challenge.www
114# _domain=domain.com
115_get_root() {
116 domain=$1
117 i=2
118 p=1
119 while true; do
120 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
121 if [ -z "$h" ]; then
122 #not valid
123 return 1
124 fi
125 if ! _online_rest GET "domain/$h/version/active"; then
126 _err "Unable to retrive DNS zone matching this domain"
127 return 1
128 fi
129
130 if ! _contains "$response" "Domain not found" >/dev/null; then
131 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
132 _domain="$h"
133 _real_dns_version=$(echo "$response" | _egrep_o '"uuid_ref":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
134 return 0
135 fi
136 p=$i
137 i=$(_math "$i" + 1)
138 done
139 return 1
140}
141
142# this function create a temporary zone version
143# as online.net does not allow updating an active version
144_online_create_temporary_zone_version() {
145
146 _online_rest POST "domain/$_domain/version" "name=acme.sh"
147 if [ "$?" != "0" ]; then
148 return 1
149 fi
150
151 _temporary_dns_version=$(echo "$response" | _egrep_o '"uuid_ref":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
152
153 # Creating a dummy record in this temporary version, because online.net doesn't accept enabling an empty version
154 _online_create_TXT_record "$_temporary_dns_version" "dummy.acme.sh" "dummy"
155
156 return 0
157}
158
159_online_destroy_zone() {
160 version_id=$1
161 _online_rest DELETE "domain/$_domain/version/$version_id"
162
163 if [ "$?" != "0" ]; then
164 return 1
165 fi
166 return 0
167}
168
169_online_enable_zone() {
170 version_id=$1
171 _online_rest PATCH "domain/$_domain/version/$version_id/enable"
172
173 if [ "$?" != "0" ]; then
174 return 1
175 fi
176 return 0
177}
178
179_online_create_TXT_record() {
180 version=$1
181 txt_name=$2
182 txt_value=$3
183
184 _online_rest POST "domain/$_domain/version/$version/zone" "type=TXT&name=$txt_name&data=%22$txt_value%22&ttl=60&priority=0"
185
186 # Note : the normal, expected response SHOULD be "Unknown method".
187 # this happens because the API HTTP response contains a Location: header, that redirect
188 # to an unknown online.net endpoint.
189 if [ "$?" != "0" ] || _contains "$response" "Unknown method"; then
190 return 0
191 else
192 _err "error $response"
193 return 1
194 fi
195}
196
197_online_rest() {
198 m=$1
199 ep="$2"
200 data="$3"
201 _debug "$ep"
202 _online_url="$ONLINE_API/$ep"
203 _debug2 _online_url "$_online_url"
204 export _H1="Authorization: Bearer $ONLINE_API_KEY"
205 export _H2="X-Pretty-JSON: 1"
206 if [ "$data" ] || [ "$m" = "PATCH" ] || [ "$m" = "POST" ] || [ "$m" = "PUT" ] || [ "$m" = "DELETE" ]; then
207 _debug data "$data"
208 response="$(_post "$data" "$_online_url" "" "$m")"
209 else
210 response="$(_get "$_online_url")"
211 fi
212 if [ "$?" != "0" ] || _contains "$response" "invalid_grant" || _contains "$response" "Method not allowed"; then
213 _err "error $response"
214 return 1
215 fi
216 _debug2 response "$response"
217 return 0
218}