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