]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_simply.sh
change arvan api script
[mirror_acme.sh.git] / dnsapi / dns_simply.sh
CommitLineData
65c06da2 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]"
65c06da2 9
8de3698b 10SIMPLY_Api_Default="https://api.simply.com/1"
c384ed96 11
69bdbaed 12######## Public functions #####################
65c06da2 13#Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
14dns_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
fcb97f80 24 _debug "First detect the root zone"
65c06da2 25 if ! _get_root "$fulldomain"; then
26 _err "invalid domain"
27 return 1
28 fi
fcb97f80 29
65c06da2 30 _debug _sub_domain "$_sub_domain"
31 _debug _domain "$_domain"
32
33 _info "Adding record"
34
fcb97f80 35 if ! _simply_add_record "$_domain" "$_sub_domain" "$txtvalue"; then
65c06da2 36 _err "Could not add DNS record"
37 return 1
fcb97f80 38 fi
65c06da2 39 return 0
40}
41
42dns_simply_rm() {
43 fulldomain=$1
44 txtvalue=$2
fcb97f80 45
65c06da2 46 if ! _simply_load_config; then
47 return 1
48 fi
49
50 _simply_save_config
51
52 _debug "First detect the root zone"
30f359e6 53
65c06da2 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"
81c496d9 61 _debug txtvalue "$txtvalue"
65c06da2 62
42847775 63 _info "Getting all existing records"
fcb97f80 64
65c06da2 65 if ! _simply_get_all_records "$_domain"; then
66 _err "invalid domain"
67 return 1
68 fi
fcb97f80 69
29d0a171 70 records=$(echo "$response" | tr '{' "\n" | grep 'record_id\|type\|data\|\name' | sed 's/\"record_id/;\"record_id/' | tr "\n" ' ' | tr -d ' ' | tr ';' ' ')
8e64329d 71
65c06da2 72 nr_of_deleted_records=0
8e64329d 73 _info "Fetching txt record"
65c06da2 74
29d0a171 75 for record in $records; do
42847775 76 _debug record "$record"
fcb97f80 77
29d0a171 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"
30f359e6 83
fcb97f80 84 if [ "$record_data" = "$txtvalue" ] && [ "$record_type" = "TXT" ]; then
85
449f00f9 86 record_id=$(echo "$record" | cut -d "," -f 1 | grep "record_id" | cut -d ":" -f 2)
fcb97f80 87
449f00f9 88 _info "Deleting record $record"
8e64329d 89 _debug2 record_id "$record_id"
fcb97f80 90
8e64329d 91 if [ "$record_id" -gt 0 ]; then
fcb97f80 92
42847775 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
fcb97f80 97
42847775 98 nr_of_deleted_records=1
99 break
29d0a171 100 else
42847775 101 _err "Fetching record_id could not be done, this should not happen, exiting function. Failing record is $record"
102 break
103 fi
65c06da2 104 fi
fcb97f80 105
65c06da2 106 done
107
8e64329d 108 if [ "$nr_of_deleted_records" -eq 0 ]; then
29d0a171 109 _err "No record deleted, the DNS record needs to be removed manually."
65c06da2 110 else
111 _info "Deleted $nr_of_deleted_records record"
112 fi
fcb97f80 113
65c06da2 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
9ad05e64 127
65c06da2 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() {
9ad05e64 150 domain=$1
151
65c06da2 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
8de3698b 174 if _contains "$response" '"code":"NOT_FOUND"'; then
65c06da2 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
9ad05e64 191
65c06da2 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
9ad05e64 198
65c06da2 199 return 0
200}
201
202_simply_delete_record() {
203 domain=$1
204 sub_domain=$2
205 record_id=$3
9ad05e64 206
81c496d9 207 _debug record_id "Delete record with id $record_id"
9ad05e64 208
65c06da2 209 if ! _simply_rest DELETE "my/products/$domain/dns/records/$record_id"; then
210 _err "Deleting record not successfull!"
211 return 1
212 fi
9ad05e64 213
65c06da2 214 return 0
215}
216
217_simply_rest() {
218 m=$1
219 ep="$2"
220 data="$3"
9ad05e64 221
81c496d9 222 _debug2 data "$data"
223 _debug2 ep "$ep"
224 _debug2 m "$m"
65c06da2 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
9ad05e64 238
65c06da2 239 _debug2 response "$response"
9ad05e64 240
65c06da2 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
fcb97f80 245
65c06da2 246 return 0
247}