]>
Commit | Line | Data |
---|---|---|
914808b8 H |
1 | #!/usr/bin/env sh |
2 | ||
3 | Zilore_API="https://api.zilore.com/dns/v1" | |
4 | # Zilore_Key="YOUR-ZILORE-API-KEY" | |
5 | ||
6 | ######## Public functions ##################### | |
7 | ||
8 | dns_zilore_add() { | |
9 | fulldomain=$1 | |
10 | txtvalue=$2 | |
11 | ||
12 | _info "Using Zilore" | |
13 | _debug fulldomain "$fulldomain" | |
14 | _debug txtvalue "$txtvalue" | |
15 | ||
16 | Zilore_Key="${Zilore_Key:-$(_readaccountconf_mutable Zilore_Key)}" | |
17 | if [ -z "$Zilore_Key" ]; then | |
18 | Zilore_Key="" | |
19 | _err "Please define Zilore API key" | |
20 | return 1 | |
21 | fi | |
22 | _saveaccountconf_mutable Zilore_Key "$Zilore_Key" | |
23 | ||
24 | if ! _get_root "$fulldomain"; then | |
25 | _err "Unable to determine root domain" | |
26 | return 1 | |
27 | else | |
28 | _debug _domain "$_domain" | |
29 | fi | |
30 | ||
31 | if _zilore_rest POST "domains/$_domain/records?record_type=TXT&record_ttl=600&record_name=$fulldomain&record_value=\"$txtvalue\""; then | |
32 | if _contains "$response" '"added"' >/dev/null; then | |
33 | _info "Added TXT record, waiting for validation" | |
34 | return 0 | |
35 | else | |
36 | _debug response "$response" | |
37 | _err "Error while adding DNS records" | |
38 | return 1 | |
39 | fi | |
40 | fi | |
41 | ||
42 | return 1 | |
43 | } | |
44 | ||
45 | dns_zilore_rm() { | |
46 | fulldomain=$1 | |
47 | txtvalue=$2 | |
48 | ||
49 | _info "Using Zilore" | |
50 | _debug fulldomain "$fulldomain" | |
51 | _debug txtvalue "$txtvalue" | |
52 | ||
53 | Zilore_Key="${Zilore_Key:-$(_readaccountconf_mutable Zilore_Key)}" | |
54 | if [ -z "$Zilore_Key" ]; then | |
55 | Zilore_Key="" | |
56 | _err "Please define Zilore API key" | |
57 | return 1 | |
58 | fi | |
59 | _saveaccountconf_mutable Zilore_Key "$Zilore_Key" | |
60 | ||
61 | if ! _get_root "$fulldomain"; then | |
62 | _err "Unable to determine root domain" | |
63 | return 1 | |
64 | else | |
65 | _debug _domain "$_domain" | |
66 | fi | |
67 | ||
68 | _debug "Getting TXT records" | |
69 | _zilore_rest GET "domains/${_domain}/records?search_text=$txtvalue&search_record_type=TXT" | |
70 | _debug response "$response" | |
71 | ||
72 | if ! _contains "$response" '"ok"' >/dev/null; then | |
73 | _err "Error while getting records list" | |
74 | return 1 | |
75 | else | |
b14ef537 | 76 | _record_id=$(printf "%s\n" "$response" | _egrep_o "\"record_id\":\"[^\"]+\"" | cut -d : -f 2 | tr -d \" | _head_n 1) |
914808b8 H |
77 | if [ -z "$_record_id" ]; then |
78 | _err "Cannot determine _record_id" | |
79 | return 1 | |
80 | else | |
81 | _debug _record_id "$_record_id" | |
82 | fi | |
83 | if ! _zilore_rest DELETE "domains/${_domain}/records?record_id=$_record_id"; then | |
84 | _err "Error while deleting chosen record" | |
85 | return 1 | |
86 | fi | |
87 | _contains "$response" '"ok"' | |
88 | fi | |
89 | } | |
90 | ||
91 | #################### Private functions below ################################## | |
92 | ||
93 | _get_root() { | |
94 | domain=$1 | |
95 | i=2 | |
96 | while true; do | |
97 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
98 | _debug h "$h" | |
99 | if [ -z "$h" ]; then | |
100 | #not valid | |
101 | return 1 | |
102 | fi | |
103 | ||
104 | if ! _zilore_rest GET "domains?search_text=$h"; then | |
105 | return 1 | |
106 | fi | |
107 | ||
108 | if _contains "$response" "\"$h\"" >/dev/null; then | |
fde971fe | 109 | _domain=$h |
914808b8 H |
110 | return 0 |
111 | else | |
112 | _debug "$h not found" | |
113 | fi | |
114 | i=$(_math "$i" + 1) | |
115 | done | |
116 | return 1 | |
117 | } | |
118 | ||
119 | _zilore_rest() { | |
120 | method=$1 | |
121 | param=$2 | |
122 | data=$3 | |
123 | ||
124 | export _H1="X-Auth-Key: $Zilore_Key" | |
125 | ||
126 | if [ "$method" != "GET" ]; then | |
127 | response="$(_post "$data" "$Zilore_API/$param" "" "$method")" | |
128 | else | |
129 | response="$(_get "$Zilore_API/$param")" | |
130 | fi | |
131 | ||
132 | if [ "$?" != "0" ]; then | |
133 | _err "error $param" | |
134 | return 1 | |
135 | fi | |
136 | ||
137 | _debug2 response "$response" | |
138 | return 0 | |
139 | } |