]>
Commit | Line | Data |
---|---|---|
eef9a600 AF |
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. | |
6a0ed51f | 6 | |
eef9a600 AF |
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 | ||
5dbfc278 AF |
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 | |
eef9a600 AF |
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 | ||
eef9a600 | 97 | _debug "Delete $fulldomain TXT record" |
eef9a600 AF |
98 | url="https://management.1984hosting.com/domains" |
99 | ||
5dbfc278 | 100 | _htmlget "$url" "$_domain" |
eef9a600 AF |
101 | _debug2 _response "$_response" |
102 | zone_id="$(echo "$_response" | _egrep_o 'zone\/[0-9]+')" | |
103 | _debug2 zone_id "$zone_id" | |
104 | if [ -z "$zone_id" ]; then | |
105 | _err "Error getting zone_id for $1" | |
106 | return 1 | |
107 | fi | |
108 | ||
5dbfc278 | 109 | _htmlget "$url/$zone_id" "$_sub_domain" |
eef9a600 AF |
110 | _debug2 _response "$_response" |
111 | entry_id="$(echo "$_response" | _egrep_o 'entry_[0-9]+' | sed 's/entry_//')" | |
112 | _debug2 entry_id "$entry_id" | |
113 | if [ -z "$entry_id" ]; then | |
114 | _err "Error getting TXT entry_id for $1" | |
115 | return 1 | |
116 | fi | |
117 | ||
118 | _authpost "entry=$entry_id" "$url/delentry/" | |
119 | response="$(echo "$_response" | _normalizeJson)" | |
120 | _debug2 response "$response" | |
121 | ||
122 | if ! _contains "$response" '"ok": true'; then | |
123 | _err "1984Hosting failed to delete TXT record for $entry_id bad RC from _post" | |
124 | return 1 | |
125 | fi | |
126 | ||
127 | _info "Deleted acme challenge TXT record for $fulldomain at 1984Hosting" | |
128 | return 0 | |
129 | } | |
130 | ||
5dbfc278 AF |
131 | #################### Private functions below ################################## |
132 | ||
eef9a600 AF |
133 | # usage: _1984hosting_login username password |
134 | # returns 0 success | |
135 | _1984hosting_login() { | |
136 | if ! _check_credentials; then return 1; fi | |
137 | ||
138 | if _check_cookie; then | |
139 | _debug "Already logged in" | |
140 | return 0 | |
141 | fi | |
142 | ||
143 | _debug "Login to 1984Hosting as user $One984HOSTING_Username" | |
144 | username=$(printf '%s' "$One984HOSTING_Username" | _url_encode) | |
145 | password=$(printf '%s' "$One984HOSTING_Password" | _url_encode) | |
146 | url="https://management.1984hosting.com/accounts/checkuserauth/" | |
147 | ||
3891a52a | 148 | response="$(_post "username=$username&password=$password&otpkey=" $url)" |
eef9a600 AF |
149 | response="$(echo "$response" | _normalizeJson)" |
150 | _debug2 response "$response" | |
151 | ||
6a0ed51f | 152 | if _contains "$response" '"loggedin": true'; then |
025da924 | 153 | One984HOSTING_COOKIE="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _tail_n 1 | _egrep_o 'sessionid=[^;]*;' | tr -d ';')" |
eef9a600 AF |
154 | export One984HOSTING_COOKIE |
155 | _saveaccountconf_mutable One984HOSTING_COOKIE "$One984HOSTING_COOKIE" | |
156 | return 0 | |
157 | fi | |
158 | return 1 | |
159 | } | |
160 | ||
161 | _check_credentials() { | |
162 | if [ -z "$One984HOSTING_Username" ] || [ -z "$One984HOSTING_Password" ]; then | |
163 | One984HOSTING_Username="" | |
164 | One984HOSTING_Password="" | |
165 | _err "You haven't specified 1984Hosting username or password yet." | |
166 | _err "Please export as One984HOSTING_Username / One984HOSTING_Password and try again." | |
167 | return 1 | |
168 | fi | |
169 | return 0 | |
170 | } | |
171 | ||
172 | _check_cookie() { | |
173 | One984HOSTING_COOKIE="${One984HOSTING_COOKIE:-$(_readaccountconf_mutable One984HOSTING_COOKIE)}" | |
174 | if [ -z "$One984HOSTING_COOKIE" ]; then | |
175 | _debug "No cached cookie found" | |
176 | return 1 | |
177 | fi | |
178 | ||
179 | _authget "https://management.1984hosting.com/accounts/loginstatus/" | |
180 | response="$(echo "$_response" | _normalizeJson)" | |
6a0ed51f | 181 | if _contains "$response" '"ok": true'; then |
eef9a600 AF |
182 | _debug "Cached cookie still valid" |
183 | return 0 | |
184 | fi | |
185 | _debug "Cached cookie no longer valid" | |
186 | One984HOSTING_COOKIE="" | |
187 | _saveaccountconf_mutable One984HOSTING_COOKIE "$One984HOSTING_COOKIE" | |
188 | return 1 | |
189 | } | |
190 | ||
191 | #_acme-challenge.www.domain.com | |
192 | #returns | |
193 | # _sub_domain=_acme-challenge.www | |
194 | # _domain=domain.com | |
195 | _get_root() { | |
196 | domain="$1" | |
197 | i=2 | |
198 | p=1 | |
199 | while true; do | |
200 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
201 | ||
202 | if [ -z "$h" ]; then | |
203 | #not valid | |
204 | return 1 | |
205 | fi | |
206 | ||
207 | _authget "https://management.1984hosting.com/domains/soacheck/?zone=$h&nameserver=ns0.1984.is." | |
208 | if _contains "$_response" "serial"; then | |
209 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
210 | _domain="$h" | |
211 | return 0 | |
212 | fi | |
213 | p=$i | |
214 | i=$(_math "$i" + 1) | |
215 | done | |
216 | return 1 | |
217 | } | |
218 | ||
219 | # add extra headers to request | |
220 | _authget() { | |
221 | export _H1="Cookie: $One984HOSTING_COOKIE" | |
222 | _response=$(_get "$1") | |
223 | } | |
224 | ||
225 | # truncate huge HTML response | |
226 | # echo: Argument list too long | |
227 | _htmlget() { | |
228 | export _H1="Cookie: $One984HOSTING_COOKIE" | |
229 | _response=$(_get "$1" | grep "$2" | _head_n 1) | |
230 | } | |
231 | ||
232 | # add extra headers to request | |
233 | _authpost() { | |
234 | export _H1="Cookie: $One984HOSTING_COOKIE" | |
235 | _response=$(_post "$1" "$2") | |
236 | } |