]>
Commit | Line | Data |
---|---|---|
c0285fbc MG |
1 | #!/usr/bin/env sh |
2 | ||
3 | # | |
184dde92 | 4 | #AZION_Email="" |
c0285fbc | 5 | #AZION_Password="" |
c0285fbc MG |
6 | # |
7 | ||
8 | AZION_Api="https://api.azionapi.net" | |
9 | ||
10 | ######## Public functions ######## | |
11 | ||
12 | # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
13 | # Used to add txt record | |
14 | dns_azion_add() { | |
15 | fulldomain=$1 | |
16 | txtvalue=$2 | |
17 | ||
c0285fbc MG |
18 | _debug "Detect the root zone" |
19 | if ! _get_root "$fulldomain"; then | |
20 | _err "Domain not found" | |
21 | return 1 | |
22 | fi | |
23 | ||
24 | _debug _sub_domain "$_sub_domain" | |
25 | _debug _domain "$_domain" | |
26 | _debug _domain_id "$_domain_id" | |
27 | ||
28 | _info "Add or update record" | |
406ca66c | 29 | _get_record "$_domain_id" "$_sub_domain" |
c0285fbc MG |
30 | if [ "$record_id" ]; then |
31 | _payload="{\"record_type\": \"TXT\", \"entry\": \"$_sub_domain\", \"answers_list\": [$answers_list, \"$txtvalue\"], \"ttl\": 20}" | |
32 | if _azion_rest PUT "intelligent_dns/$_domain_id/records/$record_id" "$_payload"; then | |
33 | if _contains "$response" "$txtvalue"; then | |
34 | _info "Record updated." | |
35 | return 0 | |
36 | fi | |
37 | fi | |
38 | else | |
39 | _payload="{\"record_type\": \"TXT\", \"entry\": \"$_sub_domain\", \"answers_list\": [\"$txtvalue\"], \"ttl\": 20}" | |
40 | if _azion_rest POST "intelligent_dns/$_domain_id/records" "$_payload"; then | |
41 | if _contains "$response" "$txtvalue"; then | |
42 | _info "Record added." | |
43 | return 0 | |
44 | fi | |
45 | fi | |
46 | fi | |
47 | _err "Failed to add or update record." | |
48 | return 1 | |
49 | } | |
50 | ||
51 | # Usage: fulldomain txtvalue | |
52 | # Used to remove the txt record after validation | |
53 | dns_azion_rm() { | |
54 | fulldomain=$1 | |
55 | txtvalue=$2 | |
56 | ||
c0285fbc MG |
57 | _debug "Detect the root zone" |
58 | if ! _get_root "$fulldomain"; then | |
59 | _err "Domain not found" | |
60 | return 1 | |
61 | fi | |
62 | ||
63 | _debug _sub_domain "$_sub_domain" | |
64 | _debug _domain "$_domain" | |
65 | _debug _domain_id "$_domain_id" | |
66 | ||
67 | _info "Removing record" | |
406ca66c | 68 | _get_record "$_domain_id" "$_sub_domain" |
c0285fbc MG |
69 | if [ "$record_id" ]; then |
70 | if _azion_rest DELETE "intelligent_dns/$_domain_id/records/$record_id"; then | |
71 | _info "Record removed." | |
72 | return 0 | |
73 | else | |
74 | _err "Failed to remove record." | |
75 | return 1 | |
76 | fi | |
77 | else | |
78 | _info "Record not found or already removed." | |
79 | return 0 | |
80 | fi | |
81 | } | |
82 | ||
83 | #################### Private functions below ################################## | |
84 | # Usage: _acme-challenge.www.domain.com | |
85 | # returns | |
86 | # _sub_domain=_acme-challenge.www | |
87 | # _domain=domain.com | |
88 | # _domain_id=sdjkglgdfewsdfg | |
89 | _get_root() { | |
90 | domain=$1 | |
91 | i=1 | |
92 | p=1 | |
93 | ||
c0285fbc MG |
94 | if ! _azion_rest GET "intelligent_dns"; then |
95 | return 1 | |
96 | fi | |
97 | ||
98 | while true; do | |
99 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
100 | _debug h "$h" | |
101 | if [ -z "$h" ]; then | |
102 | # not valid | |
103 | return 1 | |
104 | fi | |
105 | ||
106 | if _contains "$response" "\"domain\":\"$h\""; then | |
107 | _domain_id=$(echo "$response" | tr '{' "\n" | grep "\"domain\":\"$h\"" | _egrep_o "\"id\":[0-9]*" | _head_n 1 | cut -d : -f 2 | tr -d \") | |
108 | _debug _domain_id "$_domain_id" | |
109 | if [ "$_domain_id" ]; then | |
110 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
111 | _domain=$h | |
c0285fbc MG |
112 | return 0 |
113 | fi | |
114 | return 1 | |
115 | fi | |
116 | p=$i | |
117 | i=$(_math "$i" + 1) | |
118 | done | |
119 | return 1 | |
120 | } | |
121 | ||
122 | _get_record() { | |
406ca66c MG |
123 | _domain_id=$1 |
124 | _record=$2 | |
c0285fbc | 125 | |
406ca66c | 126 | if ! _azion_rest GET "intelligent_dns/$_domain_id/records"; then |
c0285fbc MG |
127 | return 1 |
128 | fi | |
129 | ||
130 | if _contains "$response" "\"entry\":\"$_record\""; then | |
522dec34 | 131 | _json_record=$(echo "$response" | tr '{' "\n" | grep "\"entry\":\"$_record\"") |
c0285fbc MG |
132 | if [ "$_json_record" ]; then |
133 | record_id=$(echo "$_json_record" | _egrep_o "\"record_id\":[0-9]*" | _head_n 1 | cut -d : -f 2 | tr -d \") | |
134 | answers_list=$(echo "$_json_record" | _egrep_o "\"answers_list\":\[.*\]" | _head_n 1 | cut -d : -f 2 | tr -d \[\]) | |
135 | return 0 | |
136 | fi | |
137 | return 1 | |
138 | fi | |
139 | return 1 | |
140 | } | |
141 | ||
142 | _get_token() { | |
184dde92 | 143 | AZION_Email="${AZION_Email:-$(_readaccountconf_mutable AZION_Email)}" |
c0285fbc MG |
144 | AZION_Password="${AZION_Password:-$(_readaccountconf_mutable AZION_Password)}" |
145 | ||
184dde92 MG |
146 | if ! _contains "$AZION_Email" "@"; then |
147 | _err "It seems that the AZION_Email is not a valid email address. Revalidate your environments." | |
406ca66c MG |
148 | return 1 |
149 | fi | |
150 | ||
184dde92 MG |
151 | if [ -z "$AZION_Email" ] || [ -z "$AZION_Password" ]; then |
152 | _err "You didn't specified a AZION_Email/AZION_Password to generate Azion token." | |
406ca66c MG |
153 | return 1 |
154 | fi | |
155 | ||
184dde92 | 156 | _saveaccountconf_mutable AZION_Email "$AZION_Email" |
406ca66c MG |
157 | _saveaccountconf_mutable AZION_Password "$AZION_Password" |
158 | ||
184dde92 | 159 | _basic_auth=$(printf "%s:%s" "$AZION_Email" "$AZION_Password" | _base64) |
c0285fbc MG |
160 | _debug _basic_auth "$_basic_auth" |
161 | ||
162 | export _H1="Accept: application/json; version=3" | |
163 | export _H2="Content-Type: application/json" | |
164 | export _H3="Authorization: Basic $_basic_auth" | |
165 | ||
166 | response="$(_post "" "$AZION_Api/tokens" "" "POST")" | |
c0285fbc MG |
167 | if _contains "$response" "\"token\":\"" >/dev/null; then |
168 | _azion_token=$(echo "$response" | _egrep_o "\"token\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \") | |
406ca66c | 169 | export AZION_Token="$_azion_token" |
c0285fbc MG |
170 | else |
171 | _err "Failed to generate Azion token" | |
172 | return 1 | |
173 | fi | |
174 | } | |
175 | ||
176 | _azion_rest() { | |
177 | _method=$1 | |
178 | _uri="$2" | |
179 | _data="$3" | |
180 | ||
406ca66c MG |
181 | if [ -z "$AZION_Token" ]; then |
182 | _get_token | |
183 | fi | |
184 | _debug2 token "$AZION_Token" | |
c0285fbc MG |
185 | |
186 | export _H1="Accept: application/json; version=3" | |
187 | export _H2="Content-Type: application/json" | |
188 | export _H3="Authorization: token $AZION_Token" | |
189 | ||
190 | if [ "$_method" != "GET" ]; then | |
191 | _debug _data "$_data" | |
192 | response="$(_post "$_data" "$AZION_Api/$_uri" "" "$_method")" | |
193 | else | |
194 | response="$(_get "$AZION_Api/$_uri")" | |
195 | fi | |
196 | ||
197 | _debug2 response "$response" | |
198 | ||
199 | if [ "$?" != "0" ]; then | |
200 | _err "error $_method $_uri $_data" | |
201 | return 1 | |
202 | fi | |
203 | return 0 | |
204 | } |