Skip to content

Commit ffea264

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

File tree

1 file changed

+84
-72
lines changed

1 file changed

+84
-72
lines changed

dnsapi/dns_1984hosting.sh

Lines changed: 84 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
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-
#
11-
######## Public functions #####################
10+
11+
######## Public functions #####################
1212

1313
# Export 1984HOSTING username and password in following variables
1414
#
1515
# One984HOSTING_Username=username
1616
# One984HOSTING_Password=password
1717
#
18-
# sessionid cookie is saved in ~/.acme.sh/account.conf
19-
# username/password need to be set only when changed.
18+
# username/password and csrftoken/sessionid cookies are saved in ~/.acme.sh/account.conf
2019

21-
#Usage: dns_1984hosting_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
20+
# Usage: dns_1984hosting_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
21+
# Add a text record.
2222
dns_1984hosting_add() {
2323
fulldomain=$1
2424
txtvalue=$2
2525

26-
_info "Add TXT record using 1984Hosting"
26+
_info "Add TXT record using 1984Hosting."
2727
_debug fulldomain "$fulldomain"
2828
_debug txtvalue "$txtvalue"
2929

3030
if ! _1984hosting_login; then
31-
_err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file"
31+
_err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file."
3232
return 1
3333
fi
3434

35-
_debug "First detect the root zone"
35+
_debug "First detect the root zone."
3636
if ! _get_root "$fulldomain"; then
37-
_err "invalid domain" "$fulldomain"
37+
_err "Invalid domain '$fulldomain'."
3838
return 1
3939
fi
4040
_debug _sub_domain "$_sub_domain"
4141
_debug _domain "$_domain"
4242

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

@@ -53,93 +53,97 @@ dns_1984hosting_add() {
5353
_debug2 postdata "$postdata"
5454

5555
_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"
56+
if _contains "$_response" '"haserrors": true'; then
57+
_err "1984Hosting failed to add TXT record for $_sub_domain bad RC from _post."
6158
return 1
62-
elif _contains "$response" "html>"; then
63-
_err "1984Hosting failed to add TXT record for $_sub_domain. Check $HTTP_HEADER file"
59+
elif _contains "$_response" "html>"; then
60+
_err "1984Hosting failed to add TXT record for $_sub_domain. Check $HTTP_HEADER file."
6461
return 1
65-
elif _contains "$response" '"auth": false'; then
66-
_err "1984Hosting failed to add TXT record for $_sub_domain. Invalid or expired cookie"
62+
elif _contains "$_response" '"auth": false'; then
63+
_err "1984Hosting failed to add TXT record for $_sub_domain. Invalid or expired cookie."
6764
return 1
6865
fi
6966

70-
_info "Added acme challenge TXT record for $fulldomain at 1984Hosting"
67+
_info "Added acme challenge TXT record for $fulldomain at 1984Hosting."
7168
return 0
7269
}
7370

74-
#Usage: fulldomain txtvalue
75-
#Remove the txt record after validation.
71+
# Usage: fulldomain txtvalue
72+
# Remove the txt record after validation.
7673
dns_1984hosting_rm() {
7774
fulldomain=$1
7875
txtvalue=$2
7976

80-
_info "Delete TXT record using 1984Hosting"
77+
_info "Delete TXT record using 1984Hosting."
8178
_debug fulldomain "$fulldomain"
8279
_debug txtvalue "$txtvalue"
8380

8481
if ! _1984hosting_login; then
85-
_err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file"
82+
_err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file."
8683
return 1
8784
fi
8885

89-
_debug "First detect the root zone"
86+
_debug "First detect the root zone."
9087
if ! _get_root "$fulldomain"; then
91-
_err "invalid domain" "$fulldomain"
88+
_err "Invalid domain '$fulldomain'."
9289
return 1
9390
fi
9491
_debug _sub_domain "$_sub_domain"
9592
_debug _domain "$_domain"
96-
_debug "Delete $fulldomain TXT record"
93+
_debug "Delete $fulldomain TXT record."
9794

9895
url="https://1984.hosting/domains"
9996
if ! _get_zone_id "$url" "$_domain"; then
100-
_err "invalid zone" "$_domain"
97+
_err "Invalid zone '$_domain'."
10198
return 1
10299
fi
103100

104101
_htmlget "$url/$_zone_id" "$txtvalue"
105-
_debug2 _response "$_response"
106102
entry_id="$(echo "$_response" | _egrep_o 'entry_[0-9]+' | sed 's/entry_//')"
107103
_debug2 entry_id "$entry_id"
108104
if [ -z "$entry_id" ]; then
109-
_err "Error getting TXT entry_id for $1"
105+
_err "Error getting TXT entry_id for $1."
110106
return 1
111107
fi
112108

113109
_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"
110+
if ! _contains "$_response" '"ok": true'; then
111+
_err "1984Hosting failed to delete TXT record for $entry_id bad RC from _post."
119112
return 1
120113
fi
121114

122-
_info "Deleted acme challenge TXT record for $fulldomain at 1984Hosting"
115+
_info "Deleted acme challenge TXT record for $fulldomain at 1984Hosting."
123116
return 0
124117
}
125118

126119
#################### Private functions below ##################################
127-
128-
# usage: _1984hosting_login username password
129-
# returns 0 success
130120
_1984hosting_login() {
131121
if ! _check_credentials; then return 1; fi
132122

133123
if _check_cookies; then
134-
_debug "Already logged in"
124+
_debug "Already logged in."
135125
return 0
136126
fi
137127

138-
_debug "Login to 1984Hosting as user $One984HOSTING_Username"
128+
_debug "Login to 1984Hosting as user $One984HOSTING_Username."
139129
username=$(printf '%s' "$One984HOSTING_Username" | _url_encode)
140130
password=$(printf '%s' "$One984HOSTING_Password" | _url_encode)
141131
url="https://1984.hosting/accounts/checkuserauth/"
142132

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+
143147
response="$(_post "username=$username&password=$password&otpkey=" $url)"
144148
response="$(echo "$response" | _normalizeJson)"
145149
_debug2 response "$response"
@@ -149,6 +153,8 @@ _1984hosting_login() {
149153
One984HOSTING_CSRFTOKEN_COOKIE="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _egrep_o 'csrftoken=[^;]*;' | tr -d ';')"
150154
export One984HOSTING_SESSIONID_COOKIE
151155
export One984HOSTING_CSRFTOKEN_COOKIE
156+
_saveaccountconf_mutable One984HOSTING_Username "$One984HOSTING_Username"
157+
_saveaccountconf_mutable One984HOSTING_Password "$One984HOSTING_Password"
152158
_saveaccountconf_mutable One984HOSTING_SESSIONID_COOKIE "$One984HOSTING_SESSIONID_COOKIE"
153159
_saveaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE "$One984HOSTING_CSRFTOKEN_COOKIE"
154160
return 0
@@ -157,9 +163,13 @@ _1984hosting_login() {
157163
}
158164

159165
_check_credentials() {
166+
One984HOSTING_Username="${One984HOSTING_Username:-$(_readaccountconf_mutable One984HOSTING_Username)}"
167+
One984HOSTING_Password="${One984HOSTING_Password:-$(_readaccountconf_mutable One984HOSTING_Password)}"
160168
if [ -z "$One984HOSTING_Username" ] || [ -z "$One984HOSTING_Password" ]; then
161169
One984HOSTING_Username=""
162170
One984HOSTING_Password=""
171+
_clearaccountconf_mutable One984HOSTING_Username
172+
_clearaccountconf_mutable One984HOSTING_Password
163173
_err "You haven't specified 1984Hosting username or password yet."
164174
_err "Please export as One984HOSTING_Username / One984HOSTING_Password and try again."
165175
return 1
@@ -171,42 +181,43 @@ _check_cookies() {
171181
One984HOSTING_SESSIONID_COOKIE="${One984HOSTING_SESSIONID_COOKIE:-$(_readaccountconf_mutable One984HOSTING_SESSIONID_COOKIE)}"
172182
One984HOSTING_CSRFTOKEN_COOKIE="${One984HOSTING_CSRFTOKEN_COOKIE:-$(_readaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE)}"
173183
if [ -z "$One984HOSTING_SESSIONID_COOKIE" ] || [ -z "$One984HOSTING_CSRFTOKEN_COOKIE" ]; then
174-
_debug "No cached cookie(s) found"
184+
_debug "No cached cookie(s) found."
175185
return 1
176186
fi
177187

178188
_authget "https://1984.hosting/accounts/loginstatus/"
179-
if _contains "$response" '"ok": true'; then
180-
_debug "Cached cookies still valid"
189+
if _contains "$_response" '"ok": true'; then
190+
_debug "Cached cookies still valid."
181191
return 0
182192
fi
183-
_debug "Cached cookies no longer valid"
193+
194+
_debug "Cached cookies no longer valid. Clearing cookies."
184195
One984HOSTING_SESSIONID_COOKIE=""
185196
One984HOSTING_CSRFTOKEN_COOKIE=""
186-
_saveaccountconf_mutable One984HOSTING_SESSIONID_COOKIE "$One984HOSTING_SESSIONID_COOKIE"
187-
_saveaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE "$One984HOSTING_CSRFTOKEN_COOKIE"
197+
_clearaccountconf_mutable One984HOSTING_SESSIONID_COOKIE
198+
_clearaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE
188199
return 1
189200
}
190201

191-
#_acme-challenge.www.domain.com
192-
#returns
193-
# _sub_domain=_acme-challenge.www
194-
# _domain=domain.com
202+
# _acme-challenge.www.domain.com
203+
# Returns
204+
# _sub_domain=_acme-challenge.www
205+
# _domain=domain.com
195206
_get_root() {
196207
domain="$1"
197208
i=1
198209
p=1
199210
while true; do
200-
h=$(printf "%s" "$domain" | cut -d . -f $i-100)
211+
h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
201212

213+
# not valid
202214
if [ -z "$h" ]; then
203-
#not valid
204215
return 1
205216
fi
206217

207218
_authget "https://1984.hosting/domains/soacheck/?zone=$h&nameserver=ns0.1984.is."
208219
if _contains "$_response" "serial" && ! _contains "$_response" "null"; then
209-
_sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
220+
_sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
210221
_domain="$h"
211222
return 0
212223
fi
@@ -216,46 +227,47 @@ _get_root() {
216227
return 1
217228
}
218229

219-
#usage: _get_zone_id url domain.com
220-
#returns zone id for domain.com
230+
# Usage: _get_zone_id url domain.com
231+
# Returns zone id for domain.com
221232
_get_zone_id() {
222233
url=$1
223234
domain=$2
224235
_htmlget "$url" "$domain"
225-
_debug2 _response "$_response"
226236
_zone_id="$(echo "$_response" | _egrep_o 'zone\/[0-9]+' | _head_n 1)"
227237
_debug2 _zone_id "$_zone_id"
228238
if [ -z "$_zone_id" ]; then
229-
_err "Error getting _zone_id for $2"
239+
_err "Error getting _zone_id for $2."
230240
return 1
231241
fi
232242
return 0
233243
}
234244

235-
# add extra headers to request
245+
# Add extra headers to request
236246
_authget() {
237-
export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE;$One984HOSTING_SESSIONID_COOKIE"
247+
export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE; $One984HOSTING_SESSIONID_COOKIE"
238248
_response=$(_get "$1" | _normalizeJson)
239249
_debug2 _response "$_response"
240250
}
241251

242-
# truncate huge HTML response
243-
# echo: Argument list too long
252+
# Truncate huge HTML response
253+
# Echo: Argument list too long
244254
_htmlget() {
245-
export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE;$One984HOSTING_SESSIONID_COOKIE"
255+
export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE; $One984HOSTING_SESSIONID_COOKIE"
246256
_response=$(_get "$1" | grep "$2")
247257
if _contains "$_response" "@$2"; then
248258
_response=$(echo "$_response" | grep -v "[@]" | _head_n 1)
249259
fi
260+
_debug2 _response "$_response"
250261
}
251262

252-
# add extra headers to request
263+
# Add extra headers to request
253264
_authpost() {
254265
url="https://1984.hosting/domains"
255266
_get_zone_id "$url" "$_domain"
256267
csrf_header="$(echo "$One984HOSTING_CSRFTOKEN_COOKIE" | _egrep_o "=[^=][0-9a-zA-Z]*" | tr -d "=")"
257-
export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE;$One984HOSTING_SESSIONID_COOKIE"
268+
export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE; $One984HOSTING_SESSIONID_COOKIE"
258269
export _H2="Referer: https://1984.hosting/domains/$_zone_id"
259270
export _H3="X-CSRFToken: $csrf_header"
260-
_response=$(_post "$1" "$2")
271+
_response="$(_post "$1" "$2" | _normalizeJson)"
272+
_debug2 _response "$_response"
261273
}

0 commit comments

Comments
 (0)