]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_simply.sh
Merge pull request #4542 from alexleigh/master
[mirror_acme.sh.git] / dnsapi / dns_simply.sh
1 #!/usr/bin/env sh
2
3 # API-integration for Simply.com (https://www.simply.com)
4
5 #SIMPLY_AccountName="accountname"
6 #SIMPLY_ApiKey="apikey"
7 #
8 #SIMPLY_Api="https://api.simply.com/2/"
9 SIMPLY_Api_Default="https://api.simply.com/2"
10
11 #This is used for determining success of REST call
12 SIMPLY_SUCCESS_CODE='"status":200'
13
14 ######## Public functions #####################
15 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
16 dns_simply_add() {
17 fulldomain=$1
18 txtvalue=$2
19
20 if ! _simply_load_config; then
21 return 1
22 fi
23
24 _simply_save_config
25
26 _debug "First detect the root zone"
27 if ! _get_root "$fulldomain"; then
28 _err "invalid domain"
29 return 1
30 fi
31
32 _debug _sub_domain "$_sub_domain"
33 _debug _domain "$_domain"
34
35 _info "Adding record"
36
37 if ! _simply_add_record "$_domain" "$_sub_domain" "$txtvalue"; then
38 _err "Could not add DNS record"
39 return 1
40 fi
41 return 0
42 }
43
44 dns_simply_rm() {
45 fulldomain=$1
46 txtvalue=$2
47
48 if ! _simply_load_config; then
49 return 1
50 fi
51
52 _simply_save_config
53
54 _debug "Find the DNS zone"
55
56 if ! _get_root "$fulldomain"; then
57 _err "invalid domain"
58 return 1
59 fi
60
61 _debug _sub_domain "$_sub_domain"
62 _debug _domain "$_domain"
63 _debug txtvalue "$txtvalue"
64
65 _info "Getting all existing records"
66
67 if ! _simply_get_all_records "$_domain"; then
68 _err "invalid domain"
69 return 1
70 fi
71
72 records=$(echo "$response" | tr '{' "\n" | grep 'record_id\|type\|data\|\name' | sed 's/\"record_id/;\"record_id/' | tr "\n" ' ' | tr -d ' ' | tr ';' ' ')
73
74 nr_of_deleted_records=0
75 _info "Fetching txt record"
76
77 for record in $records; do
78 _debug record "$record"
79
80 record_data=$(echo "$record" | sed -n "s/.*\"data\":\"\([^\"]*\)\".*/\1/p")
81 record_type=$(echo "$record" | sed -n "s/.*\"type\":\"\([^\"]*\)\".*/\1/p")
82
83 _debug2 record_data "$record_data"
84 _debug2 record_type "$record_type"
85
86 if [ "$record_data" = "$txtvalue" ] && [ "$record_type" = "TXT" ]; then
87
88 record_id=$(echo "$record" | cut -d "," -f 1 | grep "record_id" | cut -d ":" -f 2)
89
90 _info "Deleting record $record"
91 _debug2 record_id "$record_id"
92
93 if [ "$record_id" -gt 0 ]; then
94
95 if ! _simply_delete_record "$_domain" "$_sub_domain" "$record_id"; then
96 _err "Record with id $record_id could not be deleted"
97 return 1
98 fi
99
100 nr_of_deleted_records=1
101 break
102 else
103 _err "Fetching record_id could not be done, this should not happen, exiting function. Failing record is $record"
104 break
105 fi
106 fi
107
108 done
109
110 if [ "$nr_of_deleted_records" -eq 0 ]; then
111 _err "No record deleted, the DNS record needs to be removed manually."
112 else
113 _info "Deleted $nr_of_deleted_records record"
114 fi
115
116 return 0
117 }
118
119 #################### Private functions below ##################################
120
121 _simply_load_config() {
122 SIMPLY_Api="${SIMPLY_Api:-$(_readaccountconf_mutable SIMPLY_Api)}"
123 SIMPLY_AccountName="${SIMPLY_AccountName:-$(_readaccountconf_mutable SIMPLY_AccountName)}"
124 SIMPLY_ApiKey="${SIMPLY_ApiKey:-$(_readaccountconf_mutable SIMPLY_ApiKey)}"
125
126 if [ -z "$SIMPLY_Api" ]; then
127 SIMPLY_Api="$SIMPLY_Api_Default"
128 fi
129
130 if [ -z "$SIMPLY_AccountName" ] || [ -z "$SIMPLY_ApiKey" ]; then
131 SIMPLY_AccountName=""
132 SIMPLY_ApiKey=""
133
134 _err "A valid Simply API account and apikey not provided."
135 _err "Please provide a valid API user and try again."
136
137 return 1
138 fi
139
140 return 0
141 }
142
143 _simply_save_config() {
144 if [ "$SIMPLY_Api" != "$SIMPLY_Api_Default" ]; then
145 _saveaccountconf_mutable SIMPLY_Api "$SIMPLY_Api"
146 fi
147 _saveaccountconf_mutable SIMPLY_AccountName "$SIMPLY_AccountName"
148 _saveaccountconf_mutable SIMPLY_ApiKey "$SIMPLY_ApiKey"
149 }
150
151 _simply_get_all_records() {
152 domain=$1
153
154 if ! _simply_rest GET "my/products/$domain/dns/records/"; then
155 return 1
156 fi
157
158 return 0
159 }
160
161 _get_root() {
162 domain=$1
163 i=2
164 p=1
165 while true; do
166 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
167 if [ -z "$h" ]; then
168 #not valid
169 return 1
170 fi
171
172 if ! _simply_rest GET "my/products/$h/dns/"; then
173 return 1
174 fi
175
176 if ! _contains "$response" "$SIMPLY_SUCCESS_CODE"; then
177 _debug "$h not found"
178 else
179 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
180 _domain="$h"
181 return 0
182 fi
183 p="$i"
184 i=$(_math "$i" + 1)
185 done
186 return 1
187 }
188
189 _simply_add_record() {
190 domain=$1
191 sub_domain=$2
192 txtval=$3
193
194 data="{\"name\": \"$sub_domain\", \"type\":\"TXT\", \"data\": \"$txtval\", \"priority\":0, \"ttl\": 3600}"
195
196 if ! _simply_rest POST "my/products/$domain/dns/records/" "$data"; then
197 _err "Adding record not successfull!"
198 return 1
199 fi
200
201 if ! _contains "$response" "$SIMPLY_SUCCESS_CODE"; then
202 _err "Call to API not sucessfull, see below message for more details"
203 _err "$response"
204 return 1
205 fi
206
207 return 0
208 }
209
210 _simply_delete_record() {
211 domain=$1
212 sub_domain=$2
213 record_id=$3
214
215 _debug record_id "Delete record with id $record_id"
216
217 if ! _simply_rest DELETE "my/products/$domain/dns/records/$record_id/"; then
218 _err "Deleting record not successfull!"
219 return 1
220 fi
221
222 if ! _contains "$response" "$SIMPLY_SUCCESS_CODE"; then
223 _err "Call to API not sucessfull, see below message for more details"
224 _err "$response"
225 return 1
226 fi
227
228 return 0
229 }
230
231 _simply_rest() {
232 m=$1
233 ep="$2"
234 data="$3"
235
236 _debug2 data "$data"
237 _debug2 ep "$ep"
238 _debug2 m "$m"
239
240 basicauth=$(printf "%s:%s" "$SIMPLY_AccountName" "$SIMPLY_ApiKey" | _base64)
241
242 if [ "$basicauth" ]; then
243 export _H1="Authorization: Basic $basicauth"
244 fi
245
246 export _H2="Content-Type: application/json"
247
248 if [ "$m" != "GET" ]; then
249 response="$(_post "$data" "$SIMPLY_Api/$ep" "" "$m")"
250 else
251 response="$(_get "$SIMPLY_Api/$ep")"
252 fi
253
254 if [ "$?" != "0" ]; then
255 _err "error $ep"
256 return 1
257 fi
258
259 response="$(echo "$response" | _normalizeJson)"
260
261 _debug2 response "$response"
262
263 if _contains "$response" "Invalid account authorization"; then
264 _err "It seems that your api key or accountnumber is not correct."
265 return 1
266 fi
267
268 return 0
269 }