]>
Commit | Line | Data |
---|---|---|
65c06da2 | 1 | #!/usr/bin/env sh |
2 | ||
8cdceb83 JV |
3 | # API-integration for Simply.com (https://www.simply.com) |
4 | ||
65c06da2 | 5 | #SIMPLY_AccountName="accountname" |
65c06da2 | 6 | #SIMPLY_ApiKey="apikey" |
7 | # | |
227d62a5 NBJ |
8 | #SIMPLY_Api="https://api.simply.com/2/" |
9 | SIMPLY_Api_Default="https://api.simply.com/2" | |
65c06da2 | 10 | |
1917c4b0 | 11 | #This is used for determining success of REST call |
8cdceb83 | 12 | SIMPLY_SUCCESS_CODE='"status":200' |
1917c4b0 | 13 | |
69bdbaed | 14 | ######## Public functions ##################### |
65c06da2 | 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 | ||
fcb97f80 | 26 | _debug "First detect the root zone" |
65c06da2 | 27 | if ! _get_root "$fulldomain"; then |
28 | _err "invalid domain" | |
29 | return 1 | |
30 | fi | |
fcb97f80 | 31 | |
65c06da2 | 32 | _debug _sub_domain "$_sub_domain" |
33 | _debug _domain "$_domain" | |
34 | ||
35 | _info "Adding record" | |
36 | ||
fcb97f80 | 37 | if ! _simply_add_record "$_domain" "$_sub_domain" "$txtvalue"; then |
65c06da2 | 38 | _err "Could not add DNS record" |
39 | return 1 | |
fcb97f80 | 40 | fi |
65c06da2 | 41 | return 0 |
42 | } | |
43 | ||
44 | dns_simply_rm() { | |
45 | fulldomain=$1 | |
46 | txtvalue=$2 | |
fcb97f80 | 47 | |
65c06da2 | 48 | if ! _simply_load_config; then |
49 | return 1 | |
50 | fi | |
51 | ||
52 | _simply_save_config | |
53 | ||
8cdceb83 | 54 | _debug "Find the DNS zone" |
30f359e6 | 55 | |
65c06da2 | 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" | |
81c496d9 | 63 | _debug txtvalue "$txtvalue" |
65c06da2 | 64 | |
42847775 | 65 | _info "Getting all existing records" |
fcb97f80 | 66 | |
65c06da2 | 67 | if ! _simply_get_all_records "$_domain"; then |
68 | _err "invalid domain" | |
69 | return 1 | |
70 | fi | |
fcb97f80 | 71 | |
29d0a171 | 72 | records=$(echo "$response" | tr '{' "\n" | grep 'record_id\|type\|data\|\name' | sed 's/\"record_id/;\"record_id/' | tr "\n" ' ' | tr -d ' ' | tr ';' ' ') |
8e64329d | 73 | |
65c06da2 | 74 | nr_of_deleted_records=0 |
8e64329d | 75 | _info "Fetching txt record" |
65c06da2 | 76 | |
29d0a171 | 77 | for record in $records; do |
42847775 | 78 | _debug record "$record" |
fcb97f80 | 79 | |
8cdceb83 JV |
80 | record_data=$(echo "$record" | sed -n "s/.*\"data\":\"\([^\"]*\)\".*/\1/p") |
81 | record_type=$(echo "$record" | sed -n "s/.*\"type\":\"\([^\"]*\)\".*/\1/p") | |
29d0a171 | 82 | |
83 | _debug2 record_data "$record_data" | |
84 | _debug2 record_type "$record_type" | |
30f359e6 | 85 | |
fcb97f80 | 86 | if [ "$record_data" = "$txtvalue" ] && [ "$record_type" = "TXT" ]; then |
87 | ||
449f00f9 | 88 | record_id=$(echo "$record" | cut -d "," -f 1 | grep "record_id" | cut -d ":" -f 2) |
fcb97f80 | 89 | |
449f00f9 | 90 | _info "Deleting record $record" |
8e64329d | 91 | _debug2 record_id "$record_id" |
fcb97f80 | 92 | |
8e64329d | 93 | if [ "$record_id" -gt 0 ]; then |
fcb97f80 | 94 | |
42847775 | 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 | |
fcb97f80 | 99 | |
42847775 | 100 | nr_of_deleted_records=1 |
101 | break | |
29d0a171 | 102 | else |
42847775 | 103 | _err "Fetching record_id could not be done, this should not happen, exiting function. Failing record is $record" |
104 | break | |
105 | fi | |
65c06da2 | 106 | fi |
fcb97f80 | 107 | |
65c06da2 | 108 | done |
109 | ||
8e64329d | 110 | if [ "$nr_of_deleted_records" -eq 0 ]; then |
29d0a171 | 111 | _err "No record deleted, the DNS record needs to be removed manually." |
65c06da2 | 112 | else |
113 | _info "Deleted $nr_of_deleted_records record" | |
114 | fi | |
fcb97f80 | 115 | |
65c06da2 | 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 | |
9ad05e64 | 129 | |
65c06da2 | 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() { | |
9ad05e64 | 152 | domain=$1 |
153 | ||
8cdceb83 | 154 | if ! _simply_rest GET "my/products/$domain/dns/records/"; then |
65c06da2 | 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 | ||
8cdceb83 | 172 | if ! _simply_rest GET "my/products/$h/dns/"; then |
65c06da2 | 173 | return 1 |
174 | fi | |
175 | ||
c5100219 | 176 | if ! _contains "$response" "$SIMPLY_SUCCESS_CODE"; then |
65c06da2 | 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 | |
9ad05e64 | 193 | |
65c06da2 | 194 | data="{\"name\": \"$sub_domain\", \"type\":\"TXT\", \"data\": \"$txtval\", \"priority\":0, \"ttl\": 3600}" |
195 | ||
8cdceb83 | 196 | if ! _simply_rest POST "my/products/$domain/dns/records/" "$data"; then |
65c06da2 | 197 | _err "Adding record not successfull!" |
198 | return 1 | |
199 | fi | |
9ad05e64 | 200 | |
c5100219 KJ |
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 | |
459faf4d | 206 | |
65c06da2 | 207 | return 0 |
208 | } | |
209 | ||
210 | _simply_delete_record() { | |
211 | domain=$1 | |
212 | sub_domain=$2 | |
213 | record_id=$3 | |
9ad05e64 | 214 | |
81c496d9 | 215 | _debug record_id "Delete record with id $record_id" |
9ad05e64 | 216 | |
8cdceb83 | 217 | if ! _simply_rest DELETE "my/products/$domain/dns/records/$record_id/"; then |
65c06da2 | 218 | _err "Deleting record not successfull!" |
219 | return 1 | |
220 | fi | |
9ad05e64 | 221 | |
c5100219 KJ |
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 | ||
65c06da2 | 228 | return 0 |
229 | } | |
230 | ||
231 | _simply_rest() { | |
232 | m=$1 | |
233 | ep="$2" | |
234 | data="$3" | |
9ad05e64 | 235 | |
81c496d9 | 236 | _debug2 data "$data" |
237 | _debug2 ep "$ep" | |
238 | _debug2 m "$m" | |
65c06da2 | 239 | |
227d62a5 NBJ |
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" | |
65c06da2 | 247 | |
248 | if [ "$m" != "GET" ]; then | |
227d62a5 | 249 | response="$(_post "$data" "$SIMPLY_Api/$ep" "" "$m")" |
65c06da2 | 250 | else |
227d62a5 | 251 | response="$(_get "$SIMPLY_Api/$ep")" |
65c06da2 | 252 | fi |
253 | ||
254 | if [ "$?" != "0" ]; then | |
255 | _err "error $ep" | |
256 | return 1 | |
257 | fi | |
459faf4d | 258 | |
8cdceb83 | 259 | response="$(echo "$response" | _normalizeJson)" |
9ad05e64 | 260 | |
65c06da2 | 261 | _debug2 response "$response" |
9ad05e64 | 262 | |
65c06da2 | 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 | |
fcb97f80 | 267 | |
65c06da2 | 268 | return 0 |
269 | } |