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