Skip to content

Commit f64bdc7

Browse files
committed
dns_1984hosting.sh: fix login with valid csrftoken and sessionid
1 parent ec0e871 commit f64bdc7

File tree

1 file changed

+72
-63
lines changed

1 file changed

+72
-63
lines changed

dnsapi/dns_1984hosting.sh

Lines changed: 72 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/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.
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.
66

7-
#Author: Adrian Fedoreanu
8-
#Report Bugs here: https://github.com/acmesh-official/acme.sh
7+
# Author: Adrian Fedoreanu
8+
# Report Bugs here: https://github.com/acmesh-official/acme.sh
99
# or here... https://github.com/acmesh-official/acme.sh/issues/2851
10-
#
10+
1111
######## Public functions #####################
1212

1313
# Export 1984HOSTING username and password in following variables
@@ -18,29 +18,30 @@
1818
# sessionid cookie is saved in ~/.acme.sh/account.conf
1919
# username/password need to be set only when changed.
2020

21-
#Usage: dns_1984hosting_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
21+
# Usage: dns_1984hosting_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
22+
# Add a text record.
2223
dns_1984hosting_add() {
2324
fulldomain=$1
2425
txtvalue=$2
2526

26-
_info "Add TXT record using 1984Hosting"
27+
_info "Add TXT record using 1984Hosting."
2728
_debug fulldomain "$fulldomain"
2829
_debug txtvalue "$txtvalue"
2930

3031
if ! _1984hosting_login; then
31-
_err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file"
32+
_err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file."
3233
return 1
3334
fi
3435

35-
_debug "First detect the root zone"
36+
_debug "First detect the root zone."
3637
if ! _get_root "$fulldomain"; then
37-
_err "invalid domain" "$fulldomain"
38+
_err "Invalid domain" "$fulldomain"
3839
return 1
3940
fi
4041
_debug _sub_domain "$_sub_domain"
4142
_debug _domain "$_domain"
4243

43-
_debug "Add TXT record $fulldomain with value '$txtvalue'"
44+
_debug "Add TXT record $fulldomain with value '$txtvalue'."
4445
value="$(printf '%s' "$txtvalue" | _url_encode)"
4546
url="https://1984.hosting/domains/entry/"
4647

@@ -53,93 +54,99 @@ dns_1984hosting_add() {
5354
_debug2 postdata "$postdata"
5455

5556
_authpost "$postdata" "$url"
56-
response="$(echo "$_response" | _normalizeJson)"
57-
_debug2 response "$response"
58-
59-
if _contains "$response" '"haserrors": true'; then
60-
_err "1984Hosting failed to add TXT record for $_sub_domain bad RC from _post"
57+
if _contains "$_response" '"haserrors": true'; then
58+
_err "1984Hosting failed to add TXT record for $_sub_domain bad RC from _post."
6159
return 1
62-
elif _contains "$response" "html>"; then
63-
_err "1984Hosting failed to add TXT record for $_sub_domain. Check $HTTP_HEADER file"
60+
elif _contains "$_response" "html>"; then
61+
_err "1984Hosting failed to add TXT record for $_sub_domain. Check $HTTP_HEADER file."
6462
return 1
65-
elif _contains "$response" '"auth": false'; then
66-
_err "1984Hosting failed to add TXT record for $_sub_domain. Invalid or expired cookie"
63+
elif _contains "$_response" '"auth": false'; then
64+
_err "1984Hosting failed to add TXT record for $_sub_domain. Invalid or expired cookie."
6765
return 1
6866
fi
6967

70-
_info "Added acme challenge TXT record for $fulldomain at 1984Hosting"
68+
_info "Added acme challenge TXT record for $fulldomain at 1984Hosting."
7169
return 0
7270
}
7371

74-
#Usage: fulldomain txtvalue
75-
#Remove the txt record after validation.
72+
# Usage: fulldomain txtvalue
73+
# Remove the txt record after validation.
7674
dns_1984hosting_rm() {
7775
fulldomain=$1
7876
txtvalue=$2
7977

80-
_info "Delete TXT record using 1984Hosting"
78+
_info "Delete TXT record using 1984Hosting."
8179
_debug fulldomain "$fulldomain"
8280
_debug txtvalue "$txtvalue"
8381

8482
if ! _1984hosting_login; then
85-
_err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file"
83+
_err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file."
8684
return 1
8785
fi
8886

89-
_debug "First detect the root zone"
87+
_debug "First detect the root zone."
9088
if ! _get_root "$fulldomain"; then
91-
_err "invalid domain" "$fulldomain"
89+
_err "Invalid domain" "$fulldomain"
9290
return 1
9391
fi
9492
_debug _sub_domain "$_sub_domain"
9593
_debug _domain "$_domain"
96-
_debug "Delete $fulldomain TXT record"
94+
_debug "Delete $fulldomain TXT record."
9795

9896
url="https://1984.hosting/domains"
9997
if ! _get_zone_id "$url" "$_domain"; then
100-
_err "invalid zone" "$_domain"
98+
_err "Invalid zone" "$_domain"
10199
return 1
102100
fi
103101

104102
_htmlget "$url/$_zone_id" "$txtvalue"
105-
_debug2 _response "$_response"
106103
entry_id="$(echo "$_response" | _egrep_o 'entry_[0-9]+' | sed 's/entry_//')"
107104
_debug2 entry_id "$entry_id"
108105
if [ -z "$entry_id" ]; then
109-
_err "Error getting TXT entry_id for $1"
106+
_err "Error getting TXT entry_id for $1."
110107
return 1
111108
fi
112109

113110
_authpost "entry=$entry_id" "$url/delentry/"
114-
response="$(echo "$_response" | _normalizeJson)"
115-
_debug2 response "$response"
116-
117-
if ! _contains "$response" '"ok": true'; then
118-
_err "1984Hosting failed to delete TXT record for $entry_id bad RC from _post"
111+
if ! _contains "$_response" '"ok": true'; then
112+
_err "1984Hosting failed to delete TXT record for $entry_id bad RC from _post."
119113
return 1
120114
fi
121115

122-
_info "Deleted acme challenge TXT record for $fulldomain at 1984Hosting"
116+
_info_info "Deleted acme challenge TXT record for $fulldomain at 1984Hosting."
123117
return 0
124118
}
125119

126120
#################### Private functions below ##################################
127121

128-
# usage: _1984hosting_login username password
129-
# returns 0 success
122+
# Usage: _1984hosting_login username password
123+
# Returns 0 success
130124
_1984hosting_login() {
131125
if ! _check_credentials; then return 1; fi
132126

133127
if _check_cookies; then
134-
_debug "Already logged in"
128+
_debug "Already logged in."
135129
return 0
136130
fi
137131

138-
_debug "Login to 1984Hosting as user $One984HOSTING_Username"
132+
_debug "Login to 1984Hosting as user $One984HOSTING_Username."
139133
username=$(printf '%s' "$One984HOSTING_Username" | _url_encode)
140134
password=$(printf '%s' "$One984HOSTING_Password" | _url_encode)
141135
url="https://1984.hosting/accounts/checkuserauth/"
142136

137+
_get "https://1984.hosting/accounts/login/" | grep "csrfmiddlewaretoken"
138+
csrftoken="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _egrep_o 'csrftoken=[^;]*;' | tr -d ';')"
139+
sessionid="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _egrep_o 'sessionid=[^;]*;' | tr -d ';')"
140+
141+
if [ -z "$csrftoken" ] || [ -z "$sessionid" ]; then
142+
_err "One or more cookies are empty: '$csrftoken', '$sessionid'."
143+
return 1
144+
fi
145+
146+
export _H1="Cookie: $csrftoken; $sessionid"
147+
csrf_header=$(echo "$csrftoken" | sed 's/csrftoken=//' | _head_n 1)
148+
export _H2="X-CSRFToken: $csrf_header"
149+
143150
response="$(_post "username=$username&password=$password&otpkey=" $url)"
144151
response="$(echo "$response" | _normalizeJson)"
145152
_debug2 response "$response"
@@ -171,42 +178,43 @@ _check_cookies() {
171178
One984HOSTING_SESSIONID_COOKIE="${One984HOSTING_SESSIONID_COOKIE:-$(_readaccountconf_mutable One984HOSTING_SESSIONID_COOKIE)}"
172179
One984HOSTING_CSRFTOKEN_COOKIE="${One984HOSTING_CSRFTOKEN_COOKIE:-$(_readaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE)}"
173180
if [ -z "$One984HOSTING_SESSIONID_COOKIE" ] || [ -z "$One984HOSTING_CSRFTOKEN_COOKIE" ]; then
174-
_debug "No cached cookie(s) found"
181+
_debug "No cached cookie(s) found."
175182
return 1
176183
fi
177184

178185
_authget "https://1984.hosting/accounts/loginstatus/"
179-
if _contains "$response" '"ok": true'; then
180-
_debug "Cached cookies still valid"
186+
if _contains "$_response" '"ok": true'; then
187+
_debug "Cached cookies still valid."
181188
return 0
182189
fi
183-
_debug "Cached cookies no longer valid"
190+
191+
_debug "Cached cookies no longer valid. Clearing cookies."
184192
One984HOSTING_SESSIONID_COOKIE=""
185193
One984HOSTING_CSRFTOKEN_COOKIE=""
186194
_saveaccountconf_mutable One984HOSTING_SESSIONID_COOKIE "$One984HOSTING_SESSIONID_COOKIE"
187195
_saveaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE "$One984HOSTING_CSRFTOKEN_COOKIE"
188196
return 1
189197
}
190198

191-
#_acme-challenge.www.domain.com
192-
#returns
193-
# _sub_domain=_acme-challenge.www
194-
# _domain=domain.com
199+
# _acme-challenge.www.domain.com
200+
# Returns
201+
# _sub_domain=_acme-challenge.www
202+
# _domain=domain.com
195203
_get_root() {
196204
domain="$1"
197205
i=1
198206
p=1
199207
while true; do
200-
h=$(printf "%s" "$domain" | cut -d . -f $i-100)
208+
h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
201209

210+
# not valid
202211
if [ -z "$h" ]; then
203-
#not valid
204212
return 1
205213
fi
206214

207215
_authget "https://1984.hosting/domains/soacheck/?zone=$h&nameserver=ns0.1984.is."
208216
if _contains "$_response" "serial" && ! _contains "$_response" "null"; then
209-
_sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
217+
_sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
210218
_domain="$h"
211219
return 0
212220
fi
@@ -216,46 +224,47 @@ _get_root() {
216224
return 1
217225
}
218226

219-
#usage: _get_zone_id url domain.com
220-
#returns zone id for domain.com
227+
# Usage: _get_zone_id url domain.com
228+
# Returns zone id for domain.com
221229
_get_zone_id() {
222230
url=$1
223231
domain=$2
224232
_htmlget "$url" "$domain"
225-
_debug2 _response "$_response"
226233
_zone_id="$(echo "$_response" | _egrep_o 'zone\/[0-9]+' | _head_n 1)"
227234
_debug2 _zone_id "$_zone_id"
228235
if [ -z "$_zone_id" ]; then
229-
_err "Error getting _zone_id for $2"
236+
_err "Error getting _zone_id for $2."
230237
return 1
231238
fi
232239
return 0
233240
}
234241

235-
# add extra headers to request
242+
# Add extra headers to request
236243
_authget() {
237244
export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE;$One984HOSTING_SESSIONID_COOKIE"
238245
_response=$(_get "$1" | _normalizeJson)
239246
_debug2 _response "$_response"
240247
}
241248

242-
# truncate huge HTML response
243-
# echo: Argument list too long
249+
# Truncate huge HTML response
250+
# Echo: Argument list too long
244251
_htmlget() {
245252
export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE;$One984HOSTING_SESSIONID_COOKIE"
246253
_response=$(_get "$1" | grep "$2")
247254
if _contains "$_response" "@$2"; then
248255
_response=$(echo "$_response" | grep -v "[@]" | _head_n 1)
249256
fi
257+
_debug2 _response "$_response"
250258
}
251259

252-
# add extra headers to request
260+
# Add extra headers to request
253261
_authpost() {
254262
url="https://1984.hosting/domains"
255263
_get_zone_id "$url" "$_domain"
256264
csrf_header="$(echo "$One984HOSTING_CSRFTOKEN_COOKIE" | _egrep_o "=[^=][0-9a-zA-Z]*" | tr -d "=")"
257265
export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE;$One984HOSTING_SESSIONID_COOKIE"
258266
export _H2="Referer: https://1984.hosting/domains/$_zone_id"
259267
export _H3="X-CSRFToken: $csrf_header"
260-
_response=$(_post "$1" "$2")
268+
_response="$(_post "$1" "$2" | _normalizeJson)"
269+
_debug2 _response "$_response"
261270
}

0 commit comments

Comments
 (0)