]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_websupport.sh
Update dns_websupport.sh
[mirror_acme.sh.git] / dnsapi / dns_websupport.sh
1 #!/usr/bin/env sh
2
3 #This is the websupport.sk api wrapper for acme.sh
4 #
5 #Author: trgo.sk
6 #Report Bugs here: https://github.com/trgosk/acme.sh
7
8 #WS_ApiKey="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
9 #WS_ApiSecret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
10
11 WS_Api="https://rest.websupport.sk"
12
13 ######## Public functions #####################
14
15 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
16 dns_websupport_add() {
17 fulldomain=$1
18 txtvalue=$2
19
20 WS_ApiKey="${WS_ApiKey:-$(_readaccountconf_mutable WS_ApiKey)}"
21 WS_ApiSecret="${WS_ApiSecret:-$(_readaccountconf_mutable WS_ApiSecret)}"
22
23 if [ "$WS_ApiKey" ] && [ "$WS_ApiSecret" ]; then
24 _saveaccountconf_mutable WS_ApiKey "$WS_ApiKey"
25 _saveaccountconf_mutable WS_ApiSecret "$WS_ApiSecret"
26 else
27 WS_ApiKey=""
28 WS_ApiSecret=""
29 _err "You didn't specify a api key and/or api secret yet."
30 _err "You can get yours from here https://admin.websupport.sk/en/auth/apiKey"
31 return 1
32 fi
33
34 _debug "First detect the root zone"
35 if ! _get_root "$fulldomain"; then
36 _err "invalid domain"
37 return 1
38 fi
39 _debug _sub_domain "$_sub_domain"
40 _debug _domain "$_domain"
41
42 # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
43 # we can not use updating anymore.
44 # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
45 # _debug count "$count"
46 # if [ "$count" = "0" ]; then
47 _info "Adding record"
48 if _ws_rest POST "/v1/user/self/zone/$_domain/record" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
49 if _contains "$response" "$txtvalue"; then
50 _info "Added, OK"
51 return 0
52 elif _contains "$response" "The record already exists"; then
53 _info "Already exists, OK"
54 return 0
55 else
56 _err "Add txt record error."
57 return 1
58 fi
59 fi
60 _err "Add txt record error."
61 return 1
62
63 }
64
65 #fulldomain txtvalue
66 dns_websupport_rm() {
67 fulldomain=$1
68 txtvalue=$2
69
70 _debug2 fulldomain "$fulldomain"
71 _debug2 txtvalue "$txtvalue"
72
73 _debug "First detect the root zone"
74 if ! _get_root "$fulldomain"; then
75 _err "invalid domain"
76 return 1
77 fi
78
79 _debug _sub_domain "$_sub_domain"
80 _debug _domain "$_domain"
81
82 _debug "Getting txt records"
83 _ws_rest GET "/v1/user/self/zone/$_domain/record"
84
85 if [ "$(printf "%s" "$response" | tr -d " " | grep -c \"items\")" -lt "1" ]; then
86 _err "Error: $response"
87 return 1
88 fi
89
90 record_line="$(_get_from_array "$response" "$txtvalue")"
91 _debug record_line "$record_line"
92 if [ -z "$record_line" ]; then
93 _info "Don't need to remove."
94 else
95 record_id=$(echo "$record_line" | _egrep_o "\"id\": *[^,]*" | _head_n 1 | cut -d : -f 2 | tr -d \" | tr -d " ")
96 _debug "record_id" "$record_id"
97 if [ -z "$record_id" ]; then
98 _err "Can not get record id to remove."
99 return 1
100 fi
101 if ! _ws_rest DELETE "/v1/user/self/zone/$_domain/record/$record_id"; then
102 _err "Delete record error."
103 return 1
104 fi
105 if [ "$(printf "%s" "$response" | tr -d " " | grep -c \"success\")" -lt "1" ]; then
106 return 1
107 else
108 return 0
109 fi
110 fi
111
112 }
113
114 #################### Private functions below ##################################
115 #_acme-challenge.www.domain.com
116 #returns
117 # _sub_domain=_acme-challenge.www
118 # _domain=domain.com
119 _get_root() {
120 domain=$1
121 i=1
122 p=1
123
124 while true; do
125 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
126 _debug h "$h"
127 if [ -z "$h" ]; then
128 #not valid
129 return 1
130 fi
131
132 if ! _ws_rest GET "/v1/user/self/zone"; then
133 return 1
134 fi
135
136 if _contains "$response" "\"name\":\"$h\""; then
137 _domain_id=$(echo "$response" | _egrep_o "\[.\"id\": *[^,]*" | _head_n 1 | cut -d : -f 2 | tr -d \" | tr -d " ")
138 if [ "$_domain_id" ]; then
139 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
140 _domain=$h
141 return 0
142 fi
143 return 1
144 fi
145 p=$i
146 i=$(_math "$i" + 1)
147 done
148 return 1
149 }
150
151 _ws_rest() {
152 me=$1
153 pa="$2"
154 da="$3"
155
156 _debug2 api_key "$WS_ApiKey"
157 _debug2 api_secret "$WS_ApiSecret"
158
159 timestamp="$(date +%s)"
160 datez=$(date -u -r "$timestamp" +%Y-%m-%dT%H:%M:%S%z 2>/dev/null || date -u -d@"$timestamp" +%Y-%m-%dT%H:%M:%S%z)
161 canonical_request="${me} ${pa} ${timestamp}"
162 alg="sha1"
163 signature_hash=$( (printf "%s" "$canonical_request" | ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -mac HMAC -macopt "key:$WS_ApiSecret" 2>/dev/null || printf "%s" "$canonical_request" | ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hmac "$(printf "%s" "$WS_ApiSecret" | _h2b)") | cut -d = -f 2 | tr -d ' ')
164 basicauth="$(printf "%s:%s" "$WS_ApiKey" "$signature_hash" | _base64)"
165
166 _debug2 method "$me"
167 _debug2 path "$pa"
168 _debug2 data "$da"
169 _debug2 timestamp "$timestamp"
170 _debug2 datez "$datez"
171 _debug2 canonical_request "$canonical_request"
172 _debug2 alg "$alg"
173 _debug2 signature_hash "$signature_hash"
174 _debug2 basicauth "$basicauth"
175
176 export _H1="Accept: application/json"
177 export _H2="Content-Type: application/json"
178 export _H3="Authorization: Basic ${basicauth}"
179 export _H4="Date: ${datez}"
180
181 _debug2 H1 "$_H1"
182 _debug2 H2 "$_H2"
183 _debug2 H3 "$_H3"
184 _debug2 H4 "$_H4"
185
186 if [ "$me" != "GET" ]; then
187 _debug2 "${me} $WS_Api${pa}"
188 _debug data "$da"
189 response="$(_post "$da" "${WS_Api}${pa}" "" "$me")"
190 else
191 _debug2 "GET $WS_Api${pa}"
192 response="$(_get "$WS_Api${pa}")"
193 fi
194
195 _debug2 response "$response"
196 return "$?"
197 }
198
199 _get_from_array() {
200 va="$1"
201 fi="$2"
202 for i in $(echo "$va" | sed "s/{/ /g"); do
203 if _contains "$i" "$fi"; then
204 echo "$i"
205 break
206 fi
207 done
208 }