]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_1984hosting.sh
Merge pull request #4776 from KincaidYang/master
[mirror_acme.sh.git] / dnsapi / dns_1984hosting.sh
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.
6
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 # username/password and csrftoken/sessionid cookies are saved in ~/.acme.sh/account.conf
19
20 # Usage: dns_1984hosting_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
21 # Add a text record.
22 dns_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 _debug "Add TXT record $fulldomain with value '$txtvalue'."
44 value="$(printf '%s' "$txtvalue" | _url_encode)"
45 url="https://1984.hosting/domains/entry/"
46
47 postdata="entry=new"
48 postdata="$postdata&type=TXT"
49 postdata="$postdata&ttl=900"
50 postdata="$postdata&zone=$_domain"
51 postdata="$postdata&host=$_sub_domain"
52 postdata="$postdata&rdata=%22$value%22"
53 _debug2 postdata "$postdata"
54
55 _authpost "$postdata" "$url"
56 if _contains "$_response" '"haserrors": true'; then
57 _err "1984Hosting failed to add TXT record for $_sub_domain bad RC from _post."
58 return 1
59 elif _contains "$_response" "html>"; then
60 _err "1984Hosting failed to add TXT record for $_sub_domain. Check $HTTP_HEADER file."
61 return 1
62 elif _contains "$_response" '"auth": false'; then
63 _err "1984Hosting failed to add TXT record for $_sub_domain. Invalid or expired cookie."
64 return 1
65 fi
66
67 _info "Added acme challenge TXT record for $fulldomain at 1984Hosting."
68 return 0
69 }
70
71 # Usage: fulldomain txtvalue
72 # Remove the txt record after validation.
73 dns_1984hosting_rm() {
74 fulldomain=$1
75 txtvalue=$2
76
77 _info "Delete TXT record using 1984Hosting."
78 _debug fulldomain "$fulldomain"
79 _debug txtvalue "$txtvalue"
80
81 if ! _1984hosting_login; then
82 _err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file."
83 return 1
84 fi
85
86 _debug "First detect the root zone."
87 if ! _get_root "$fulldomain"; then
88 _err "Invalid domain '$fulldomain'."
89 return 1
90 fi
91 _debug _sub_domain "$_sub_domain"
92 _debug _domain "$_domain"
93 _debug "Delete $fulldomain TXT record."
94
95 url="https://1984.hosting/domains"
96 if ! _get_zone_id "$url" "$_domain"; then
97 _err "Invalid zone '$_domain'."
98 return 1
99 fi
100
101 _htmlget "$url/$_zone_id" "$txtvalue"
102 entry_id="$(echo "$_response" | _egrep_o 'entry_[0-9]+' | sed 's/entry_//')"
103 _debug2 entry_id "$entry_id"
104 if [ -z "$entry_id" ]; then
105 _err "Error getting TXT entry_id for $1."
106 return 1
107 fi
108
109 _authpost "entry=$entry_id" "$url/delentry/"
110 if ! _contains "$_response" '"ok": true'; then
111 _err "1984Hosting failed to delete TXT record for $entry_id bad RC from _post."
112 return 1
113 fi
114
115 _info "Deleted acme challenge TXT record for $fulldomain at 1984Hosting."
116 return 0
117 }
118
119 #################### Private functions below ##################################
120 _1984hosting_login() {
121 if ! _check_credentials; then return 1; fi
122
123 if _check_cookies; then
124 _debug "Already logged in."
125 return 0
126 fi
127
128 _debug "Login to 1984Hosting as user $One984HOSTING_Username."
129 username=$(printf '%s' "$One984HOSTING_Username" | _url_encode)
130 password=$(printf '%s' "$One984HOSTING_Password" | _url_encode)
131 url="https://1984.hosting/accounts/checkuserauth/"
132
133 _get "https://1984.hosting/accounts/login/" | grep "csrfmiddlewaretoken"
134 csrftoken="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _egrep_o 'csrftoken=[^;]*;' | tr -d ';')"
135 sessionid="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _egrep_o 'sessionid=[^;]*;' | tr -d ';')"
136
137 if [ -z "$csrftoken" ] || [ -z "$sessionid" ]; then
138 _err "One or more cookies are empty: '$csrftoken', '$sessionid'."
139 return 1
140 fi
141
142 export _H1="Cookie: $csrftoken; $sessionid"
143 export _H2="Referer: https://1984.hosting/accounts/login/"
144 csrf_header=$(echo "$csrftoken" | sed 's/csrftoken=//' | _head_n 1)
145 export _H3="X-CSRFToken: $csrf_header"
146
147 response="$(_post "username=$username&password=$password&otpkey=" $url)"
148 response="$(echo "$response" | _normalizeJson)"
149 _debug2 response "$response"
150
151 if _contains "$response" '"loggedin": true'; then
152 One984HOSTING_SESSIONID_COOKIE="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _egrep_o 'sessionid=[^;]*;' | tr -d ';')"
153 One984HOSTING_CSRFTOKEN_COOKIE="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _egrep_o 'csrftoken=[^;]*;' | tr -d ';')"
154 export One984HOSTING_SESSIONID_COOKIE
155 export One984HOSTING_CSRFTOKEN_COOKIE
156 _saveaccountconf_mutable One984HOSTING_Username "$One984HOSTING_Username"
157 _saveaccountconf_mutable One984HOSTING_Password "$One984HOSTING_Password"
158 _saveaccountconf_mutable One984HOSTING_SESSIONID_COOKIE "$One984HOSTING_SESSIONID_COOKIE"
159 _saveaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE "$One984HOSTING_CSRFTOKEN_COOKIE"
160 return 0
161 fi
162 return 1
163 }
164
165 _check_credentials() {
166 One984HOSTING_Username="${One984HOSTING_Username:-$(_readaccountconf_mutable One984HOSTING_Username)}"
167 One984HOSTING_Password="${One984HOSTING_Password:-$(_readaccountconf_mutable One984HOSTING_Password)}"
168 if [ -z "$One984HOSTING_Username" ] || [ -z "$One984HOSTING_Password" ]; then
169 One984HOSTING_Username=""
170 One984HOSTING_Password=""
171 _clearaccountconf_mutable One984HOSTING_Username
172 _clearaccountconf_mutable One984HOSTING_Password
173 _err "You haven't specified 1984Hosting username or password yet."
174 _err "Please export as One984HOSTING_Username / One984HOSTING_Password and try again."
175 return 1
176 fi
177 return 0
178 }
179
180 _check_cookies() {
181 One984HOSTING_SESSIONID_COOKIE="${One984HOSTING_SESSIONID_COOKIE:-$(_readaccountconf_mutable One984HOSTING_SESSIONID_COOKIE)}"
182 One984HOSTING_CSRFTOKEN_COOKIE="${One984HOSTING_CSRFTOKEN_COOKIE:-$(_readaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE)}"
183 if [ -z "$One984HOSTING_SESSIONID_COOKIE" ] || [ -z "$One984HOSTING_CSRFTOKEN_COOKIE" ]; then
184 _debug "No cached cookie(s) found."
185 return 1
186 fi
187
188 _authget "https://1984.hosting/accounts/loginstatus/"
189 if _contains "$_response" '"ok": true'; then
190 _debug "Cached cookies still valid."
191 return 0
192 fi
193
194 _debug "Cached cookies no longer valid. Clearing cookies."
195 One984HOSTING_SESSIONID_COOKIE=""
196 One984HOSTING_CSRFTOKEN_COOKIE=""
197 _clearaccountconf_mutable One984HOSTING_SESSIONID_COOKIE
198 _clearaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE
199 return 1
200 }
201
202 # _acme-challenge.www.domain.com
203 # Returns
204 # _sub_domain=_acme-challenge.www
205 # _domain=domain.com
206 _get_root() {
207 domain="$1"
208 i=1
209 p=1
210 while true; do
211 h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
212
213 # not valid
214 if [ -z "$h" ]; then
215 return 1
216 fi
217
218 _authget "https://1984.hosting/domains/soacheck/?zone=$h&nameserver=ns0.1984.is."
219 if _contains "$_response" "serial" && ! _contains "$_response" "null"; then
220 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
221 _domain="$h"
222 return 0
223 fi
224 p=$i
225 i=$(_math "$i" + 1)
226 done
227 return 1
228 }
229
230 # Usage: _get_zone_id url domain.com
231 # Returns zone id for domain.com
232 _get_zone_id() {
233 url=$1
234 domain=$2
235 _htmlget "$url" "$domain"
236 _zone_id="$(echo "$_response" | _egrep_o 'zone\/[0-9]+' | _head_n 1)"
237 _debug2 _zone_id "$_zone_id"
238 if [ -z "$_zone_id" ]; then
239 _err "Error getting _zone_id for $2."
240 return 1
241 fi
242 return 0
243 }
244
245 # Add extra headers to request
246 _authget() {
247 export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE; $One984HOSTING_SESSIONID_COOKIE"
248 _response=$(_get "$1" | _normalizeJson)
249 _debug2 _response "$_response"
250 }
251
252 # Truncate huge HTML response
253 # Echo: Argument list too long
254 _htmlget() {
255 export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE; $One984HOSTING_SESSIONID_COOKIE"
256 _response=$(_get "$1" | grep "$2")
257 if _contains "$_response" "@$2"; then
258 _response=$(echo "$_response" | grep -v "[@]" | _head_n 1)
259 fi
260 _debug2 _response "$_response"
261 }
262
263 # Add extra headers to request
264 _authpost() {
265 url="https://1984.hosting/domains"
266 _get_zone_id "$url" "$_domain"
267 csrf_header="$(echo "$One984HOSTING_CSRFTOKEN_COOKIE" | _egrep_o "=[^=][0-9a-zA-Z]*" | tr -d "=")"
268 export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE; $One984HOSTING_SESSIONID_COOKIE"
269 export _H2="Referer: https://1984.hosting/domains/$_zone_id"
270 export _H3="X-CSRFToken: $csrf_header"
271 _response="$(_post "$1" "$2" | _normalizeJson)"
272 _debug2 _response "$_response"
273 }