]>
Commit | Line | Data |
---|---|---|
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 | ||
10 | ONLINE_API="https://api.online.net/api/v1" | |
11 | ||
12 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
13 | dns_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 | |
48 | dns_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() { | |
5c941476 | 95 | ONLINE_API_KEY="${ONLINE_API_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 | |
5c941476 | 101 | if ! _online_rest GET "domain/"; then |
02f6d4cb AF |
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 | |
84151350 AF |
125 | |
126 | _online_rest GET "domain/$h/version/active" | |
ec5fad43 AF |
127 | |
128 | if ! _contains "$response" "Domain not found" >/dev/null; then | |
129 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
130 | _domain="$h" | |
131 | _real_dns_version=$(echo "$response" | _egrep_o '"uuid_ref":.*' | cut -d ':' -f 2 | cut -d '"' -f 2) | |
132 | return 0 | |
133 | fi | |
134 | p=$i | |
135 | i=$(_math "$i" + 1) | |
136 | done | |
1ad6742d | 137 | _err "Unable to retrive DNS zone matching this domain" |
ec6569fb | 138 | return 1 |
ec5fad43 AF |
139 | } |
140 | ||
141 | # this function create a temporary zone version | |
142 | # as online.net does not allow updating an active version | |
143 | _online_create_temporary_zone_version() { | |
144 | ||
145 | _online_rest POST "domain/$_domain/version" "name=acme.sh" | |
146 | if [ "$?" != "0" ]; then | |
147 | return 1 | |
148 | fi | |
149 | ||
150 | _temporary_dns_version=$(echo "$response" | _egrep_o '"uuid_ref":.*' | cut -d ':' -f 2 | cut -d '"' -f 2) | |
151 | ||
152 | # Creating a dummy record in this temporary version, because online.net doesn't accept enabling an empty version | |
153 | _online_create_TXT_record "$_temporary_dns_version" "dummy.acme.sh" "dummy" | |
154 | ||
155 | return 0 | |
156 | } | |
157 | ||
158 | _online_destroy_zone() { | |
159 | version_id=$1 | |
160 | _online_rest DELETE "domain/$_domain/version/$version_id" | |
161 | ||
162 | if [ "$?" != "0" ]; then | |
163 | return 1 | |
164 | fi | |
165 | return 0 | |
166 | } | |
167 | ||
168 | _online_enable_zone() { | |
169 | version_id=$1 | |
170 | _online_rest PATCH "domain/$_domain/version/$version_id/enable" | |
171 | ||
172 | if [ "$?" != "0" ]; then | |
173 | return 1 | |
174 | fi | |
175 | return 0 | |
176 | } | |
177 | ||
178 | _online_create_TXT_record() { | |
179 | version=$1 | |
180 | txt_name=$2 | |
181 | txt_value=$3 | |
182 | ||
183 | _online_rest POST "domain/$_domain/version/$version/zone" "type=TXT&name=$txt_name&data=%22$txt_value%22&ttl=60&priority=0" | |
184 | ||
185 | # Note : the normal, expected response SHOULD be "Unknown method". | |
186 | # this happens because the API HTTP response contains a Location: header, that redirect | |
187 | # to an unknown online.net endpoint. | |
63ea3e8d | 188 | if [ "$?" != "0" ] || _contains "$response" "Unknown method" || _contains "$response" "\$ref"; then |
ec5fad43 AF |
189 | return 0 |
190 | else | |
191 | _err "error $response" | |
192 | return 1 | |
193 | fi | |
194 | } | |
195 | ||
196 | _online_rest() { | |
197 | m=$1 | |
198 | ep="$2" | |
199 | data="$3" | |
200 | _debug "$ep" | |
201 | _online_url="$ONLINE_API/$ep" | |
202 | _debug2 _online_url "$_online_url" | |
203 | export _H1="Authorization: Bearer $ONLINE_API_KEY" | |
204 | export _H2="X-Pretty-JSON: 1" | |
9ace7db2 | 205 | if [ "$data" ] || [ "$m" != "GET" ]; then |
ec5fad43 AF |
206 | _debug data "$data" |
207 | response="$(_post "$data" "$_online_url" "" "$m")" | |
208 | else | |
209 | response="$(_get "$_online_url")" | |
210 | fi | |
211 | if [ "$?" != "0" ] || _contains "$response" "invalid_grant" || _contains "$response" "Method not allowed"; then | |
212 | _err "error $response" | |
213 | return 1 | |
214 | fi | |
215 | _debug2 response "$response" | |
216 | return 0 | |
217 | } |