]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_azure.sh
fix format
[mirror_acme.sh.git] / dnsapi / dns_azure.sh
1 #!/usr/bin/env sh
2
3 ######## Public functions #####################
4
5 # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
6 # Used to add txt record
7 #
8 # Ref: https://docs.microsoft.com/en-us/rest/api/dns/recordsets/createorupdate
9 #
10 dns_azure_add() {
11 fulldomain=$1
12 txtvalue=$2
13
14 AZUREDNS_SUBSCRIPTIONID="${AZUREDNS_SUBSCRIPTIONID:-$(_readaccountconf_mutable AZUREDNS_SUBSCRIPTIONID)}"
15 AZUREDNS_TENANTID="${AZUREDNS_TENANTID:-$(_readaccountconf_mutable AZUREDNS_TENANTID)}"
16 AZUREDNS_APPID="${AZUREDNS_APPID:-$(_readaccountconf_mutable AZUREDNS_APPID)}"
17 AZUREDNS_CLIENTSECRET="${AZUREDNS_CLIENTSECRET:-$(_readaccountconf_mutable AZUREDNS_CLIENTSECRET)}"
18
19 if [ -z "$AZUREDNS_SUBSCRIPTIONID" ]; then
20 AZUREDNS_SUBSCRIPTIONID=""
21 AZUREDNS_TENANTID=""
22 AZUREDNS_APPID=""
23 AZUREDNS_CLIENTSECRET=""
24 _err "You didn't specify the Azure Subscription ID "
25 return 1
26 fi
27
28 if [ -z "$AZUREDNS_TENANTID" ]; then
29 AZUREDNS_SUBSCRIPTIONID=""
30 AZUREDNS_TENANTID=""
31 AZUREDNS_APPID=""
32 AZUREDNS_CLIENTSECRET=""
33 _err "You didn't specify the Azure Tenant ID "
34 return 1
35 fi
36
37 if [ -z "$AZUREDNS_APPID" ]; then
38 AZUREDNS_SUBSCRIPTIONID=""
39 AZUREDNS_TENANTID=""
40 AZUREDNS_APPID=""
41 AZUREDNS_CLIENTSECRET=""
42 _err "You didn't specify the Azure App ID"
43 return 1
44 fi
45
46 if [ -z "$AZUREDNS_CLIENTSECRET" ]; then
47 AZUREDNS_SUBSCRIPTIONID=""
48 AZUREDNS_TENANTID=""
49 AZUREDNS_APPID=""
50 AZUREDNS_CLIENTSECRET=""
51 _err "You didn't specify the Azure Client Secret"
52 return 1
53 fi
54 #save account details to account conf file.
55 _saveaccountconf_mutable AZUREDNS_SUBSCRIPTIONID "$AZUREDNS_SUBSCRIPTIONID"
56 _saveaccountconf_mutable AZUREDNS_TENANTID "$AZUREDNS_TENANTID"
57 _saveaccountconf_mutable AZUREDNS_APPID "$AZUREDNS_APPID"
58 _saveaccountconf_mutable AZUREDNS_CLIENTSECRET "$AZUREDNS_CLIENTSECRET"
59
60 accesstoken=$(_azure_getaccess_token "$AZUREDNS_TENANTID" "$AZUREDNS_APPID" "$AZUREDNS_CLIENTSECRET")
61
62 if ! _get_root "$fulldomain" "$AZUREDNS_SUBSCRIPTIONID" "$accesstoken"; then
63 _err "invalid domain"
64 return 1
65 fi
66 _debug _domain_id "$_domain_id"
67 _debug _sub_domain "$_sub_domain"
68 _debug _domain "$_domain"
69
70 acmeRecordURI="https://management.azure.com$(printf '%s' "$_domain_id" | sed 's/\\//g')/TXT/$_sub_domain?api-version=2017-09-01"
71 _debug "$acmeRecordURI"
72 body="{\"properties\": {\"TTL\": 3600, \"TXTRecords\": [{\"value\": [\"$txtvalue\"]}]}}"
73 _azure_rest PUT "$acmeRecordURI" "$body" "$accesstoken"
74 if [ "$_code" = "200" ] || [ "$_code" = '201' ]; then
75 _info "validation record added"
76 else
77 _err "error adding validation record ($_code)"
78 return 1
79 fi
80 }
81
82 # Usage: fulldomain txtvalue
83 # Used to remove the txt record after validation
84 #
85 # Ref: https://docs.microsoft.com/en-us/rest/api/dns/recordsets/delete
86 #
87 dns_azure_rm() {
88 fulldomain=$1
89 txtvalue=$2
90
91 AZUREDNS_SUBSCRIPTIONID="${AZUREDNS_SUBSCRIPTIONID:-$(_readaccountconf_mutable AZUREDNS_SUBSCRIPTIONID)}"
92 AZUREDNS_TENANTID="${AZUREDNS_TENANTID:-$(_readaccountconf_mutable AZUREDNS_TENANTID)}"
93 AZUREDNS_APPID="${AZUREDNS_APPID:-$(_readaccountconf_mutable AZUREDNS_APPID)}"
94 AZUREDNS_CLIENTSECRET="${AZUREDNS_CLIENTSECRET:-$(_readaccountconf_mutable AZUREDNS_CLIENTSECRET)}"
95
96 if [ -z "$AZUREDNS_SUBSCRIPTIONID" ]; then
97 AZUREDNS_SUBSCRIPTIONID=""
98 AZUREDNS_TENANTID=""
99 AZUREDNS_APPID=""
100 AZUREDNS_CLIENTSECRET=""
101 _err "You didn't specify the Azure Subscription ID "
102 return 1
103 fi
104
105 if [ -z "$AZUREDNS_TENANTID" ]; then
106 AZUREDNS_SUBSCRIPTIONID=""
107 AZUREDNS_TENANTID=""
108 AZUREDNS_APPID=""
109 AZUREDNS_CLIENTSECRET=""
110 _err "You didn't specify the Azure Tenant ID "
111 return 1
112 fi
113
114 if [ -z "$AZUREDNS_APPID" ]; then
115 AZUREDNS_SUBSCRIPTIONID=""
116 AZUREDNS_TENANTID=""
117 AZUREDNS_APPID=""
118 AZUREDNS_CLIENTSECRET=""
119 _err "You didn't specify the Azure App ID"
120 return 1
121 fi
122
123 if [ -z "$AZUREDNS_CLIENTSECRET" ]; then
124 AZUREDNS_SUBSCRIPTIONID=""
125 AZUREDNS_TENANTID=""
126 AZUREDNS_APPID=""
127 AZUREDNS_CLIENTSECRET=""
128 _err "You didn't specify the Azure Client Secret"
129 return 1
130 fi
131
132 accesstoken=$(_azure_getaccess_token "$AZUREDNS_TENANTID" "$AZUREDNS_APPID" "$AZUREDNS_CLIENTSECRET")
133
134 if ! _get_root "$fulldomain" "$AZUREDNS_SUBSCRIPTIONID" "$accesstoken"; then
135 _err "invalid domain"
136 return 1
137 fi
138 _debug _domain_id "$_domain_id"
139 _debug _sub_domain "$_sub_domain"
140 _debug _domain "$_domain"
141
142 acmeRecordURI="https://management.azure.com$(printf '%s' "$_domain_id" | sed 's/\\//g')/TXT/$_sub_domain?api-version=2017-09-01"
143 _debug "$acmeRecordURI"
144 body="{\"properties\": {\"TTL\": 3600, \"TXTRecords\": [{\"value\": [\"$txtvalue\"]}]}}"
145 _azure_rest DELETE "$acmeRecordURI" "" "$accesstoken"
146 if [ "$_code" = "200" ] || [ "$_code" = '204' ]; then
147 _info "validation record removed"
148 else
149 _err "error removing validation record ($_code)"
150 return 1
151 fi
152 }
153
154 ################### Private functions below ##################################
155
156 _azure_rest() {
157 m=$1
158 ep="$2"
159 data="$3"
160 accesstoken="$4"
161
162 export _H1="authorization: Bearer $accesstoken"
163 export _H2="accept: application/json"
164 export _H3="Content-Type: application/json"
165
166 _debug "$ep"
167 if [ "$m" != "GET" ]; then
168 _debug data "$data"
169 response="$(_post "$data" "$ep" "" "$m")"
170 else
171 response="$(_get "$ep")"
172 fi
173 _debug2 response "$response"
174
175 _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\r\n")"
176 _debug2 "http response code $_code"
177
178 if [ "$?" != "0" ]; then
179 _err "error $ep"
180 return 1
181 fi
182 return 0
183 }
184
185 ## Ref: https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-service-to-service#request-an-access-token
186 _azure_getaccess_token() {
187 TENANTID=$1
188 clientID=$2
189 clientSecret=$3
190
191 export _H1="accept: application/json"
192 export _H2="Content-Type: application/x-www-form-urlencoded"
193
194 body="resource=$(printf "%s" 'https://management.core.windows.net/' | _url_encode)&client_id=$(printf "%s" "$clientID" | _url_encode)&client_secret=$(printf "%s" "$clientSecret" | _url_encode)&grant_type=client_credentials"
195 _debug data "$body"
196 response="$(_post "$body" "https://login.windows.net/$TENANTID/oauth2/token" "" "POST")"
197 accesstoken=$(echo "$response" | _egrep_o "\"access_token\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \")
198 _debug2 "response $response"
199
200 if [ -z "$accesstoken" ]; then
201 _err "no acccess token received"
202 return 1
203 fi
204 if [ "$?" != "0" ]; then
205 _err "error $response"
206 return 1
207 fi
208 printf "%s" "$accesstoken"
209 return 0
210 }
211
212 _get_root() {
213 domain=$1
214 subscriptionId=$2
215 accesstoken=$3
216 i=2
217 p=1
218
219 ## Ref: https://docs.microsoft.com/en-us/rest/api/dns/zones/list
220 ## returns up to 100 zones in one response therefore handling more results is not not implemented
221 ## (ZoneListResult with continuation token for the next page of results)
222 ## Per https://docs.microsoft.com/en-us/azure/azure-subscription-service-limits#dns-limits you are limited to 100 Zone/subscriptions anyways
223 ##
224 _azure_rest GET "https://management.azure.com/subscriptions/$subscriptionId/providers/Microsoft.Network/dnszones?api-version=2017-09-01" "" "$accesstoken"
225
226 # Find matching domain name is Json response
227 while true; do
228 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
229 _debug2 "Checking domain: $h"
230 if [ -z "$h" ]; then
231 #not valid
232 _err "Invalid domain"
233 return 1
234 fi
235
236 if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
237 _domain_id=$(echo "$response" | _egrep_o "\{\"id\":\"[^\"]*$h\"" | head -n 1 | cut -d : -f 2 | tr -d \")
238 if [ "$_domain_id" ]; then
239 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
240 _domain=$h
241 return 0
242 fi
243 return 1
244 fi
245 p=$i
246 i=$(_math "$i" + 1)
247 done
248 return 1
249 }