]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_1984hosting.sh
Merge pull request #3124 from acmesh-official/dev
[mirror_acme.sh.git] / dnsapi / dns_1984hosting.sh
CommitLineData
eef9a600
AF
1#!/usr/bin/env sh
2#This file name is "dns_1984hosting.sh"
3#So, here must be a method dns_1984hosting_add()
4#Which will be called by acme.sh to add the txt record to your api system.
5#returns 0 means success, otherwise error.
6a0ed51f 6
eef9a600
AF
7#Author: Adrian Fedoreanu
8#Report Bugs here: https://github.com/acmesh-official/acme.sh
9# or here... https://github.com/acmesh-official/acme.sh/issues/2851
10#
11######## Public functions #####################
12
13# Export 1984HOSTING username and password in following variables
14#
15# One984HOSTING_Username=username
16# One984HOSTING_Password=password
17#
18# sessionid cookie is saved in ~/.acme.sh/account.conf
19# username/password need to be set only when changed.
20
21#Usage: dns_1984hosting_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
22dns_1984hosting_add() {
23 fulldomain=$1
24 txtvalue=$2
25
26 _info "Add TXT record using 1984Hosting"
27 _debug fulldomain "$fulldomain"
28 _debug txtvalue "$txtvalue"
29
30 if ! _1984hosting_login; then
31 _err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file"
32 return 1
33 fi
34
35 _debug "First detect the root zone"
36 if ! _get_root "$fulldomain"; then
37 _err "invalid domain" "$fulldomain"
38 return 1
39 fi
40 _debug _sub_domain "$_sub_domain"
41 _debug _domain "$_domain"
42
43 _1984hosting_add_txt_record "$_domain" "$_sub_domain" "$txtvalue"
44 return $?
45}
46
47#Usage: fulldomain txtvalue
48#Remove the txt record after validation.
49dns_1984hosting_rm() {
50 fulldomain=$1
51 txtvalue=$2
52
53 _info "Delete TXT record using 1984Hosting"
54 _debug fulldomain "$fulldomain"
55 _debug txtvalue "$txtvalue"
56
57 if ! _1984hosting_login; then
58 _err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file"
59 return 1
60 fi
61
62 _debug "First detect the root zone"
63 if ! _get_root "$fulldomain"; then
64 _err "invalid domain" "$fulldomain"
65 return 1
66 fi
67 _debug _sub_domain "$_sub_domain"
68 _debug _domain "$_domain"
69
70 _1984hosting_delete_txt_record "$_domain" "$_sub_domain"
71 return $?
72}
73
74#################### Private functions below ##################################
75
76# usage _1984hosting_add_txt_record domain subdomain value
77# returns 0 success
78_1984hosting_add_txt_record() {
79 _debug "Add TXT record $1 with value '$3'"
80 domain="$1"
81 subdomain="$2"
82 value="$(printf '%s' "$3" | _url_encode)"
83 url="https://management.1984hosting.com/domains/entry/"
84
85 postdata="entry=new"
86 postdata="$postdata&type=TXT"
87 postdata="$postdata&ttl=3600"
88 postdata="$postdata&zone=$domain"
89 postdata="$postdata&host=$subdomain"
90 postdata="$postdata&rdata=%22$value%22"
91 _debug2 postdata "$postdata"
92
93 _authpost "$postdata" "$url"
94 response="$(echo "$_response" | _normalizeJson)"
95 _debug2 response "$response"
96
97 if _contains "$response" '"haserrors": true'; then
98 _err "1984Hosting failed to add TXT record for $subdomain bad RC from _post"
99 return 1
100 elif _contains "$response" "<html>"; then
101 _err "1984Hosting failed to add TXT record for $subdomain. Check $HTTP_HEADER file"
102 return 1
6a0ed51f 103 elif _contains "$response" '"auth": false'; then
eef9a600
AF
104 _err "1984Hosting failed to add TXT record for $subdomain. Invalid or expired cookie"
105 return 1
106 fi
107
108 _info "Added acme challenge TXT record for $fulldomain at 1984Hosting"
109 return 0
110}
111
112# usage _1984hosting_delete_txt_record entry_id
113# returns 0 success
114_1984hosting_delete_txt_record() {
115 _debug "Delete $fulldomain TXT record"
116 domain="$1"
117 subdomain="$2"
118 url="https://management.1984hosting.com/domains"
119
120 _htmlget "$url" "$domain"
121 _debug2 _response "$_response"
122 zone_id="$(echo "$_response" | _egrep_o 'zone\/[0-9]+')"
123 _debug2 zone_id "$zone_id"
124 if [ -z "$zone_id" ]; then
125 _err "Error getting zone_id for $1"
126 return 1
127 fi
128
129 _htmlget "$url/$zone_id" "$subdomain"
130 _debug2 _response "$_response"
131 entry_id="$(echo "$_response" | _egrep_o 'entry_[0-9]+' | sed 's/entry_//')"
132 _debug2 entry_id "$entry_id"
133 if [ -z "$entry_id" ]; then
134 _err "Error getting TXT entry_id for $1"
135 return 1
136 fi
137
138 _authpost "entry=$entry_id" "$url/delentry/"
139 response="$(echo "$_response" | _normalizeJson)"
140 _debug2 response "$response"
141
142 if ! _contains "$response" '"ok": true'; then
143 _err "1984Hosting failed to delete TXT record for $entry_id bad RC from _post"
144 return 1
145 fi
146
147 _info "Deleted acme challenge TXT record for $fulldomain at 1984Hosting"
148 return 0
149}
150
151# usage: _1984hosting_login username password
152# returns 0 success
153_1984hosting_login() {
154 if ! _check_credentials; then return 1; fi
155
156 if _check_cookie; then
157 _debug "Already logged in"
158 return 0
159 fi
160
161 _debug "Login to 1984Hosting as user $One984HOSTING_Username"
162 username=$(printf '%s' "$One984HOSTING_Username" | _url_encode)
163 password=$(printf '%s' "$One984HOSTING_Password" | _url_encode)
164 url="https://management.1984hosting.com/accounts/checkuserauth/"
165
166 response="$(_post "username=$username&password=$password&otpkey=" "$url")"
167 response="$(echo "$response" | _normalizeJson)"
168 _debug2 response "$response"
169
6a0ed51f 170 if _contains "$response" '"loggedin": true'; then
025da924 171 One984HOSTING_COOKIE="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _tail_n 1 | _egrep_o 'sessionid=[^;]*;' | tr -d ';')"
eef9a600
AF
172 export One984HOSTING_COOKIE
173 _saveaccountconf_mutable One984HOSTING_COOKIE "$One984HOSTING_COOKIE"
174 return 0
175 fi
176 return 1
177}
178
179_check_credentials() {
180 if [ -z "$One984HOSTING_Username" ] || [ -z "$One984HOSTING_Password" ]; then
181 One984HOSTING_Username=""
182 One984HOSTING_Password=""
183 _err "You haven't specified 1984Hosting username or password yet."
184 _err "Please export as One984HOSTING_Username / One984HOSTING_Password and try again."
185 return 1
186 fi
187 return 0
188}
189
190_check_cookie() {
191 One984HOSTING_COOKIE="${One984HOSTING_COOKIE:-$(_readaccountconf_mutable One984HOSTING_COOKIE)}"
192 if [ -z "$One984HOSTING_COOKIE" ]; then
193 _debug "No cached cookie found"
194 return 1
195 fi
196
197 _authget "https://management.1984hosting.com/accounts/loginstatus/"
198 response="$(echo "$_response" | _normalizeJson)"
6a0ed51f 199 if _contains "$response" '"ok": true'; then
eef9a600
AF
200 _debug "Cached cookie still valid"
201 return 0
202 fi
203 _debug "Cached cookie no longer valid"
204 One984HOSTING_COOKIE=""
205 _saveaccountconf_mutable One984HOSTING_COOKIE "$One984HOSTING_COOKIE"
206 return 1
207}
208
209#_acme-challenge.www.domain.com
210#returns
211# _sub_domain=_acme-challenge.www
212# _domain=domain.com
213_get_root() {
214 domain="$1"
215 i=2
216 p=1
217 while true; do
218 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
219
220 if [ -z "$h" ]; then
221 #not valid
222 return 1
223 fi
224
225 _authget "https://management.1984hosting.com/domains/soacheck/?zone=$h&nameserver=ns0.1984.is."
226 if _contains "$_response" "serial"; then
227 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
228 _domain="$h"
229 return 0
230 fi
231 p=$i
232 i=$(_math "$i" + 1)
233 done
234 return 1
235}
236
237# add extra headers to request
238_authget() {
239 export _H1="Cookie: $One984HOSTING_COOKIE"
240 _response=$(_get "$1")
241}
242
243# truncate huge HTML response
244# echo: Argument list too long
245_htmlget() {
246 export _H1="Cookie: $One984HOSTING_COOKIE"
247 _response=$(_get "$1" | grep "$2" | _head_n 1)
248}
249
250# add extra headers to request
251_authpost() {
252 export _H1="Cookie: $One984HOSTING_COOKIE"
253 _response=$(_post "$1" "$2")
254}