]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_1984hosting.sh
Merge pull request #4755 from glocknerc/master-1
[mirror_acme.sh.git] / dnsapi / dns_1984hosting.sh
CommitLineData
eef9a600 1#!/usr/bin/env sh
0d047824
AF
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
0d047824
AF
7# Author: Adrian Fedoreanu
8# Report Bugs here: https://github.com/acmesh-official/acme.sh
eef9a600 9# or here... https://github.com/acmesh-official/acme.sh/issues/2851
0d047824
AF
10
11######## Public functions #####################
eef9a600
AF
12
13# Export 1984HOSTING username and password in following variables
14#
15# One984HOSTING_Username=username
16# One984HOSTING_Password=password
17#
0d047824 18# username/password and csrftoken/sessionid cookies are saved in ~/.acme.sh/account.conf
eef9a600 19
0d047824
AF
20# Usage: dns_1984hosting_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
21# Add a text record.
eef9a600
AF
22dns_1984hosting_add() {
23 fulldomain=$1
24 txtvalue=$2
25
0d047824 26 _info "Add TXT record using 1984Hosting."
eef9a600
AF
27 _debug fulldomain "$fulldomain"
28 _debug txtvalue "$txtvalue"
29
30 if ! _1984hosting_login; then
0d047824 31 _err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file."
eef9a600
AF
32 return 1
33 fi
34
0d047824 35 _debug "First detect the root zone."
eef9a600 36 if ! _get_root "$fulldomain"; then
0d047824 37 _err "Invalid domain '$fulldomain'."
eef9a600
AF
38 return 1
39 fi
40 _debug _sub_domain "$_sub_domain"
41 _debug _domain "$_domain"
42
0d047824 43 _debug "Add TXT record $fulldomain with value '$txtvalue'."
5dbfc278 44 value="$(printf '%s' "$txtvalue" | _url_encode)"
5e465a29 45 url="https://1984.hosting/domains/entry/"
5dbfc278
AF
46
47 postdata="entry=new"
48 postdata="$postdata&type=TXT"
41a2d0e0 49 postdata="$postdata&ttl=900"
5dbfc278
AF
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"
0d047824
AF
56 if _contains "$_response" '"haserrors": true'; then
57 _err "1984Hosting failed to add TXT record for $_sub_domain bad RC from _post."
5dbfc278 58 return 1
0d047824
AF
59 elif _contains "$_response" "html>"; then
60 _err "1984Hosting failed to add TXT record for $_sub_domain. Check $HTTP_HEADER file."
5dbfc278 61 return 1
0d047824
AF
62 elif _contains "$_response" '"auth": false'; then
63 _err "1984Hosting failed to add TXT record for $_sub_domain. Invalid or expired cookie."
5dbfc278
AF
64 return 1
65 fi
66
0d047824 67 _info "Added acme challenge TXT record for $fulldomain at 1984Hosting."
5dbfc278 68 return 0
eef9a600
AF
69}
70
0d047824
AF
71# Usage: fulldomain txtvalue
72# Remove the txt record after validation.
eef9a600
AF
73dns_1984hosting_rm() {
74 fulldomain=$1
75 txtvalue=$2
76
0d047824 77 _info "Delete TXT record using 1984Hosting."
eef9a600
AF
78 _debug fulldomain "$fulldomain"
79 _debug txtvalue "$txtvalue"
80
81 if ! _1984hosting_login; then
0d047824 82 _err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file."
eef9a600
AF
83 return 1
84 fi
85
0d047824 86 _debug "First detect the root zone."
eef9a600 87 if ! _get_root "$fulldomain"; then
0d047824 88 _err "Invalid domain '$fulldomain'."
eef9a600
AF
89 return 1
90 fi
91 _debug _sub_domain "$_sub_domain"
92 _debug _domain "$_domain"
0d047824 93 _debug "Delete $fulldomain TXT record."
a196958b 94
5e465a29 95 url="https://1984.hosting/domains"
b45a44e4 96 if ! _get_zone_id "$url" "$_domain"; then
0d047824 97 _err "Invalid zone '$_domain'."
b45a44e4 98 return 1
a196958b 99 fi
c5c20140 100
4d95e35c 101 _htmlget "$url/$_zone_id" "$txtvalue"
4d95e35c 102 entry_id="$(echo "$_response" | _egrep_o 'entry_[0-9]+' | sed 's/entry_//')"
eef9a600
AF
103 _debug2 entry_id "$entry_id"
104 if [ -z "$entry_id" ]; then
0d047824 105 _err "Error getting TXT entry_id for $1."
eef9a600
AF
106 return 1
107 fi
108
109 _authpost "entry=$entry_id" "$url/delentry/"
0d047824
AF
110 if ! _contains "$_response" '"ok": true'; then
111 _err "1984Hosting failed to delete TXT record for $entry_id bad RC from _post."
eef9a600
AF
112 return 1
113 fi
114
0d047824 115 _info "Deleted acme challenge TXT record for $fulldomain at 1984Hosting."
eef9a600
AF
116 return 0
117}
118
5dbfc278 119#################### Private functions below ##################################
eef9a600
AF
120_1984hosting_login() {
121 if ! _check_credentials; then return 1; fi
122
92f13eb8 123 if _check_cookies; then
0d047824 124 _debug "Already logged in."
eef9a600
AF
125 return 0
126 fi
127
0d047824 128 _debug "Login to 1984Hosting as user $One984HOSTING_Username."
eef9a600
AF
129 username=$(printf '%s' "$One984HOSTING_Username" | _url_encode)
130 password=$(printf '%s' "$One984HOSTING_Password" | _url_encode)
5e465a29 131 url="https://1984.hosting/accounts/checkuserauth/"
eef9a600 132
0d047824
AF
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
3891a52a 147 response="$(_post "username=$username&password=$password&otpkey=" $url)"
eef9a600
AF
148 response="$(echo "$response" | _normalizeJson)"
149 _debug2 response "$response"
150
6a0ed51f 151 if _contains "$response" '"loggedin": true'; then
ced7110a
CB
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 ';')"
92f13eb8
CB
154 export One984HOSTING_SESSIONID_COOKIE
155 export One984HOSTING_CSRFTOKEN_COOKIE
0d047824
AF
156 _saveaccountconf_mutable One984HOSTING_Username "$One984HOSTING_Username"
157 _saveaccountconf_mutable One984HOSTING_Password "$One984HOSTING_Password"
92f13eb8
CB
158 _saveaccountconf_mutable One984HOSTING_SESSIONID_COOKIE "$One984HOSTING_SESSIONID_COOKIE"
159 _saveaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE "$One984HOSTING_CSRFTOKEN_COOKIE"
eef9a600
AF
160 return 0
161 fi
162 return 1
163}
164
165_check_credentials() {
0d047824
AF
166 One984HOSTING_Username="${One984HOSTING_Username:-$(_readaccountconf_mutable One984HOSTING_Username)}"
167 One984HOSTING_Password="${One984HOSTING_Password:-$(_readaccountconf_mutable One984HOSTING_Password)}"
eef9a600
AF
168 if [ -z "$One984HOSTING_Username" ] || [ -z "$One984HOSTING_Password" ]; then
169 One984HOSTING_Username=""
170 One984HOSTING_Password=""
0d047824
AF
171 _clearaccountconf_mutable One984HOSTING_Username
172 _clearaccountconf_mutable One984HOSTING_Password
eef9a600
AF
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
92f13eb8
CB
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
0d047824 184 _debug "No cached cookie(s) found."
eef9a600
AF
185 return 1
186 fi
187
5e465a29 188 _authget "https://1984.hosting/accounts/loginstatus/"
0d047824
AF
189 if _contains "$_response" '"ok": true'; then
190 _debug "Cached cookies still valid."
eef9a600
AF
191 return 0
192 fi
0d047824
AF
193
194 _debug "Cached cookies no longer valid. Clearing cookies."
92f13eb8
CB
195 One984HOSTING_SESSIONID_COOKIE=""
196 One984HOSTING_CSRFTOKEN_COOKIE=""
0d047824
AF
197 _clearaccountconf_mutable One984HOSTING_SESSIONID_COOKIE
198 _clearaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE
eef9a600
AF
199 return 1
200}
201
0d047824
AF
202# _acme-challenge.www.domain.com
203# Returns
204# _sub_domain=_acme-challenge.www
205# _domain=domain.com
eef9a600
AF
206_get_root() {
207 domain="$1"
c2273d2c 208 i=1
eef9a600
AF
209 p=1
210 while true; do
0d047824 211 h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
eef9a600 212
0d047824 213 # not valid
eef9a600 214 if [ -z "$h" ]; then
eef9a600
AF
215 return 1
216 fi
217
5e465a29 218 _authget "https://1984.hosting/domains/soacheck/?zone=$h&nameserver=ns0.1984.is."
a55cf40b 219 if _contains "$_response" "serial" && ! _contains "$_response" "null"; then
0d047824 220 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
eef9a600
AF
221 _domain="$h"
222 return 0
223 fi
224 p=$i
225 i=$(_math "$i" + 1)
226 done
227 return 1
228}
229
0d047824
AF
230# Usage: _get_zone_id url domain.com
231# Returns zone id for domain.com
ea18c470 232_get_zone_id() {
b45a44e4
CB
233 url=$1
234 domain=$2
a196958b 235 _htmlget "$url" "$domain"
f3196396 236 _zone_id="$(echo "$_response" | _egrep_o 'zone\/[0-9]+' | _head_n 1)"
ea18c470 237 _debug2 _zone_id "$_zone_id"
46e62f1a 238 if [ -z "$_zone_id" ]; then
0d047824 239 _err "Error getting _zone_id for $2."
ea18c470
CB
240 return 1
241 fi
242 return 0
243}
244
0d047824 245# Add extra headers to request
eef9a600 246_authget() {
0d047824 247 export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE; $One984HOSTING_SESSIONID_COOKIE"
d1541186 248 _response=$(_get "$1" | _normalizeJson)
c2273d2c 249 _debug2 _response "$_response"
eef9a600
AF
250}
251
0d047824
AF
252# Truncate huge HTML response
253# Echo: Argument list too long
eef9a600 254_htmlget() {
0d047824 255 export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE; $One984HOSTING_SESSIONID_COOKIE"
f3196396 256 _response=$(_get "$1" | grep "$2")
2f3ec3a7 257 if _contains "$_response" "@$2"; then
14833692 258 _response=$(echo "$_response" | grep -v "[@]" | _head_n 1)
2f3ec3a7 259 fi
0d047824 260 _debug2 _response "$_response"
eef9a600
AF
261}
262
0d047824 263# Add extra headers to request
eef9a600 264_authpost() {
5e465a29 265 url="https://1984.hosting/domains"
64e3cab6 266 _get_zone_id "$url" "$_domain"
c668c603 267 csrf_header="$(echo "$One984HOSTING_CSRFTOKEN_COOKIE" | _egrep_o "=[^=][0-9a-zA-Z]*" | tr -d "=")"
0d047824 268 export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE; $One984HOSTING_SESSIONID_COOKIE"
5e465a29 269 export _H2="Referer: https://1984.hosting/domains/$_zone_id"
c668c603 270 export _H3="X-CSRFToken: $csrf_header"
0d047824
AF
271 _response="$(_post "$1" "$2" | _normalizeJson)"
272 _debug2 _response "$_response"
eef9a600 273}