You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

348 lines
12 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
4 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. #!/usr/bin/env sh
  2. WIKI="https://github.com/acmesh-official/acme.sh/wiki/How-to-use-Azure-DNS"
  3. ######## Public functions #####################
  4. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  5. # Used to add txt record
  6. #
  7. # Ref: https://docs.microsoft.com/en-us/rest/api/dns/recordsets/createorupdate
  8. #
  9. dns_azure_add() {
  10. fulldomain=$1
  11. txtvalue=$2
  12. AZUREDNS_SUBSCRIPTIONID="${AZUREDNS_SUBSCRIPTIONID:-$(_readaccountconf_mutable AZUREDNS_SUBSCRIPTIONID)}"
  13. AZUREDNS_TENANTID="${AZUREDNS_TENANTID:-$(_readaccountconf_mutable AZUREDNS_TENANTID)}"
  14. AZUREDNS_APPID="${AZUREDNS_APPID:-$(_readaccountconf_mutable AZUREDNS_APPID)}"
  15. AZUREDNS_CLIENTSECRET="${AZUREDNS_CLIENTSECRET:-$(_readaccountconf_mutable AZUREDNS_CLIENTSECRET)}"
  16. if [ -z "$AZUREDNS_SUBSCRIPTIONID" ]; then
  17. AZUREDNS_SUBSCRIPTIONID=""
  18. AZUREDNS_TENANTID=""
  19. AZUREDNS_APPID=""
  20. AZUREDNS_CLIENTSECRET=""
  21. _err "You didn't specify the Azure Subscription ID "
  22. return 1
  23. fi
  24. if [ -z "$AZUREDNS_TENANTID" ]; then
  25. AZUREDNS_SUBSCRIPTIONID=""
  26. AZUREDNS_TENANTID=""
  27. AZUREDNS_APPID=""
  28. AZUREDNS_CLIENTSECRET=""
  29. _err "You didn't specify the Azure Tenant ID "
  30. return 1
  31. fi
  32. if [ -z "$AZUREDNS_APPID" ]; then
  33. AZUREDNS_SUBSCRIPTIONID=""
  34. AZUREDNS_TENANTID=""
  35. AZUREDNS_APPID=""
  36. AZUREDNS_CLIENTSECRET=""
  37. _err "You didn't specify the Azure App ID"
  38. return 1
  39. fi
  40. if [ -z "$AZUREDNS_CLIENTSECRET" ]; then
  41. AZUREDNS_SUBSCRIPTIONID=""
  42. AZUREDNS_TENANTID=""
  43. AZUREDNS_APPID=""
  44. AZUREDNS_CLIENTSECRET=""
  45. _err "You didn't specify the Azure Client Secret"
  46. return 1
  47. fi
  48. #save account details to account conf file.
  49. _saveaccountconf_mutable AZUREDNS_SUBSCRIPTIONID "$AZUREDNS_SUBSCRIPTIONID"
  50. _saveaccountconf_mutable AZUREDNS_TENANTID "$AZUREDNS_TENANTID"
  51. _saveaccountconf_mutable AZUREDNS_APPID "$AZUREDNS_APPID"
  52. _saveaccountconf_mutable AZUREDNS_CLIENTSECRET "$AZUREDNS_CLIENTSECRET"
  53. accesstoken=$(_azure_getaccess_token "$AZUREDNS_TENANTID" "$AZUREDNS_APPID" "$AZUREDNS_CLIENTSECRET")
  54. if ! _get_root "$fulldomain" "$AZUREDNS_SUBSCRIPTIONID" "$accesstoken"; then
  55. _err "invalid domain"
  56. return 1
  57. fi
  58. _debug _domain_id "$_domain_id"
  59. _debug _sub_domain "$_sub_domain"
  60. _debug _domain "$_domain"
  61. acmeRecordURI="https://management.azure.com$(printf '%s' "$_domain_id" | sed 's/\\//g')/TXT/$_sub_domain?api-version=2017-09-01"
  62. _debug "$acmeRecordURI"
  63. # Get existing TXT record
  64. _azure_rest GET "$acmeRecordURI" "" "$accesstoken"
  65. values="{\"value\":[\"$txtvalue\"]}"
  66. timestamp="$(_time)"
  67. if [ "$_code" = "200" ]; then
  68. vlist="$(echo "$response" | _egrep_o "\"value\"\\s*:\\s*\\[\\s*\"[^\"]*\"\\s*]" | cut -d : -f 2 | tr -d "[]\"")"
  69. _debug "existing TXT found"
  70. _debug "$vlist"
  71. existingts="$(echo "$response" | _egrep_o "\"acmetscheck\"\\s*:\\s*\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d "\"")"
  72. if [ -z "$existingts" ]; then
  73. # the record was not created by acme.sh. Copy the exisiting entires
  74. existingts=$timestamp
  75. fi
  76. _diff="$(_math "$timestamp - $existingts")"
  77. _debug "existing txt age: $_diff"
  78. # only use recently added records and discard if older than 2 hours because they are probably orphaned
  79. if [ "$_diff" -lt 7200 ]; then
  80. _debug "existing txt value: $vlist"
  81. for v in $vlist; do
  82. values="$values ,{\"value\":[\"$v\"]}"
  83. done
  84. fi
  85. fi
  86. # Add the txtvalue TXT Record
  87. body="{\"properties\":{\"metadata\":{\"acmetscheck\":\"$timestamp\"},\"TTL\":10, \"TXTRecords\":[$values]}}"
  88. _azure_rest PUT "$acmeRecordURI" "$body" "$accesstoken"
  89. if [ "$_code" = "200" ] || [ "$_code" = '201' ]; then
  90. _info "validation value added"
  91. return 0
  92. else
  93. _err "error adding validation value ($_code)"
  94. return 1
  95. fi
  96. }
  97. # Usage: fulldomain txtvalue
  98. # Used to remove the txt record after validation
  99. #
  100. # Ref: https://docs.microsoft.com/en-us/rest/api/dns/recordsets/delete
  101. #
  102. dns_azure_rm() {
  103. fulldomain=$1
  104. txtvalue=$2
  105. AZUREDNS_SUBSCRIPTIONID="${AZUREDNS_SUBSCRIPTIONID:-$(_readaccountconf_mutable AZUREDNS_SUBSCRIPTIONID)}"
  106. AZUREDNS_TENANTID="${AZUREDNS_TENANTID:-$(_readaccountconf_mutable AZUREDNS_TENANTID)}"
  107. AZUREDNS_APPID="${AZUREDNS_APPID:-$(_readaccountconf_mutable AZUREDNS_APPID)}"
  108. AZUREDNS_CLIENTSECRET="${AZUREDNS_CLIENTSECRET:-$(_readaccountconf_mutable AZUREDNS_CLIENTSECRET)}"
  109. if [ -z "$AZUREDNS_SUBSCRIPTIONID" ]; then
  110. AZUREDNS_SUBSCRIPTIONID=""
  111. AZUREDNS_TENANTID=""
  112. AZUREDNS_APPID=""
  113. AZUREDNS_CLIENTSECRET=""
  114. _err "You didn't specify the Azure Subscription ID "
  115. return 1
  116. fi
  117. if [ -z "$AZUREDNS_TENANTID" ]; then
  118. AZUREDNS_SUBSCRIPTIONID=""
  119. AZUREDNS_TENANTID=""
  120. AZUREDNS_APPID=""
  121. AZUREDNS_CLIENTSECRET=""
  122. _err "You didn't specify the Azure Tenant ID "
  123. return 1
  124. fi
  125. if [ -z "$AZUREDNS_APPID" ]; then
  126. AZUREDNS_SUBSCRIPTIONID=""
  127. AZUREDNS_TENANTID=""
  128. AZUREDNS_APPID=""
  129. AZUREDNS_CLIENTSECRET=""
  130. _err "You didn't specify the Azure App ID"
  131. return 1
  132. fi
  133. if [ -z "$AZUREDNS_CLIENTSECRET" ]; then
  134. AZUREDNS_SUBSCRIPTIONID=""
  135. AZUREDNS_TENANTID=""
  136. AZUREDNS_APPID=""
  137. AZUREDNS_CLIENTSECRET=""
  138. _err "You didn't specify the Azure Client Secret"
  139. return 1
  140. fi
  141. accesstoken=$(_azure_getaccess_token "$AZUREDNS_TENANTID" "$AZUREDNS_APPID" "$AZUREDNS_CLIENTSECRET")
  142. if ! _get_root "$fulldomain" "$AZUREDNS_SUBSCRIPTIONID" "$accesstoken"; then
  143. _err "invalid domain"
  144. return 1
  145. fi
  146. _debug _domain_id "$_domain_id"
  147. _debug _sub_domain "$_sub_domain"
  148. _debug _domain "$_domain"
  149. acmeRecordURI="https://management.azure.com$(printf '%s' "$_domain_id" | sed 's/\\//g')/TXT/$_sub_domain?api-version=2017-09-01"
  150. _debug "$acmeRecordURI"
  151. # Get existing TXT record
  152. _azure_rest GET "$acmeRecordURI" "" "$accesstoken"
  153. timestamp="$(_time)"
  154. if [ "$_code" = "200" ]; then
  155. vlist="$(echo "$response" | _egrep_o "\"value\"\\s*:\\s*\\[\\s*\"[^\"]*\"\\s*]" | cut -d : -f 2 | tr -d "[]\"" | grep -v -- "$txtvalue")"
  156. values=""
  157. comma=""
  158. for v in $vlist; do
  159. values="$values$comma{\"value\":[\"$v\"]}"
  160. comma=","
  161. done
  162. if [ -z "$values" ]; then
  163. # No values left remove record
  164. _debug "removing validation record completely $acmeRecordURI"
  165. _azure_rest DELETE "$acmeRecordURI" "" "$accesstoken"
  166. if [ "$_code" = "200" ] || [ "$_code" = '204' ]; then
  167. _info "validation record removed"
  168. else
  169. _err "error removing validation record ($_code)"
  170. return 1
  171. fi
  172. else
  173. # Remove only txtvalue from the TXT Record
  174. body="{\"properties\":{\"metadata\":{\"acmetscheck\":\"$timestamp\"},\"TTL\":10, \"TXTRecords\":[$values]}}"
  175. _azure_rest PUT "$acmeRecordURI" "$body" "$accesstoken"
  176. if [ "$_code" = "200" ] || [ "$_code" = '201' ]; then
  177. _info "validation value removed"
  178. return 0
  179. else
  180. _err "error removing validation value ($_code)"
  181. return 1
  182. fi
  183. fi
  184. fi
  185. }
  186. ################### Private functions below ##################################
  187. _azure_rest() {
  188. m=$1
  189. ep="$2"
  190. data="$3"
  191. accesstoken="$4"
  192. MAX_REQUEST_RETRY_TIMES=5
  193. _request_retry_times=0
  194. while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do
  195. _debug3 _request_retry_times "$_request_retry_times"
  196. export _H1="authorization: Bearer $accesstoken"
  197. export _H2="accept: application/json"
  198. export _H3="Content-Type: application/json"
  199. # clear headers from previous request to avoid getting wrong http code on timeouts
  200. : >"$HTTP_HEADER"
  201. _debug "$ep"
  202. if [ "$m" != "GET" ]; then
  203. _secure_debug2 "data $data"
  204. response="$(_post "$data" "$ep" "" "$m")"
  205. else
  206. response="$(_get "$ep")"
  207. fi
  208. _ret="$?"
  209. _secure_debug2 "response $response"
  210. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  211. _debug "http response code $_code"
  212. if [ "$_code" = "401" ]; then
  213. # we have an invalid access token set to expired
  214. _saveaccountconf_mutable AZUREDNS_TOKENVALIDTO "0"
  215. _err "access denied make sure your Azure settings are correct. See $WIKI"
  216. return 1
  217. fi
  218. # See https://docs.microsoft.com/en-us/azure/architecture/best-practices/retry-service-specific#general-rest-and-retry-guidelines for retryable HTTP codes
  219. if [ "$_ret" != "0" ] || [ -z "$_code" ] || [ "$_code" = "408" ] || [ "$_code" = "500" ] || [ "$_code" = "503" ] || [ "$_code" = "504" ]; then
  220. _request_retry_times="$(_math "$_request_retry_times" + 1)"
  221. _info "REST call error $_code retrying $ep in $_request_retry_times s"
  222. _sleep "$_request_retry_times"
  223. continue
  224. fi
  225. break
  226. done
  227. if [ "$_request_retry_times" = "$MAX_REQUEST_RETRY_TIMES" ]; then
  228. _err "Error Azure REST called was retried $MAX_REQUEST_RETRY_TIMES times."
  229. _err "Calling $ep failed."
  230. return 1
  231. fi
  232. response="$(echo "$response" | _normalizeJson)"
  233. return 0
  234. }
  235. ## Ref: https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-service-to-service#request-an-access-token
  236. _azure_getaccess_token() {
  237. tenantID=$1
  238. clientID=$2
  239. clientSecret=$3
  240. accesstoken="${AZUREDNS_BEARERTOKEN:-$(_readaccountconf_mutable AZUREDNS_BEARERTOKEN)}"
  241. expires_on="${AZUREDNS_TOKENVALIDTO:-$(_readaccountconf_mutable AZUREDNS_TOKENVALIDTO)}"
  242. # can we reuse the bearer token?
  243. if [ -n "$accesstoken" ] && [ -n "$expires_on" ]; then
  244. if [ "$(_time)" -lt "$expires_on" ]; then
  245. # brearer token is still valid - reuse it
  246. _debug "reusing bearer token"
  247. printf "%s" "$accesstoken"
  248. return 0
  249. else
  250. _debug "bearer token expired"
  251. fi
  252. fi
  253. _debug "getting new bearer token"
  254. export _H1="accept: application/json"
  255. export _H2="Content-Type: application/x-www-form-urlencoded"
  256. 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"
  257. _secure_debug2 "data $body"
  258. response="$(_post "$body" "https://login.microsoftonline.com/$tenantID/oauth2/token" "" "POST")"
  259. _ret="$?"
  260. _secure_debug2 "response $response"
  261. response="$(echo "$response" | _normalizeJson)"
  262. accesstoken=$(echo "$response" | _egrep_o "\"access_token\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \")
  263. expires_on=$(echo "$response" | _egrep_o "\"expires_on\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \")
  264. if [ -z "$accesstoken" ]; then
  265. _err "no acccess token received. Check your Azure settings see $WIKI"
  266. return 1
  267. fi
  268. if [ "$_ret" != "0" ]; then
  269. _err "error $response"
  270. return 1
  271. fi
  272. _saveaccountconf_mutable AZUREDNS_BEARERTOKEN "$accesstoken"
  273. _saveaccountconf_mutable AZUREDNS_TOKENVALIDTO "$expires_on"
  274. printf "%s" "$accesstoken"
  275. return 0
  276. }
  277. _get_root() {
  278. domain=$1
  279. subscriptionId=$2
  280. accesstoken=$3
  281. i=1
  282. p=1
  283. ## Ref: https://docs.microsoft.com/en-us/rest/api/dns/zones/list
  284. ## returns up to 100 zones in one response therefore handling more results is not not implemented
  285. ## (ZoneListResult with continuation token for the next page of results)
  286. ## Per https://docs.microsoft.com/en-us/azure/azure-subscription-service-limits#dns-limits you are limited to 100 Zone/subscriptions anyways
  287. ##
  288. _azure_rest GET "https://management.azure.com/subscriptions/$subscriptionId/providers/Microsoft.Network/dnszones?\$top=500&api-version=2017-09-01" "" "$accesstoken"
  289. # Find matching domain name in Json response
  290. while true; do
  291. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  292. _debug2 "Checking domain: $h"
  293. if [ -z "$h" ]; then
  294. #not valid
  295. _err "Invalid domain"
  296. return 1
  297. fi
  298. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  299. _domain_id=$(echo "$response" | _egrep_o "\\{\"id\":\"[^\"]*\\/$h\"" | head -n 1 | cut -d : -f 2 | tr -d \")
  300. if [ "$_domain_id" ]; then
  301. if [ "$i" = 1 ]; then
  302. #create the record at the domain apex (@) if only the domain name was provided as --domain-alias
  303. _sub_domain="@"
  304. else
  305. _sub_domain=$(echo "$domain" | cut -d . -f 1-$p)
  306. fi
  307. _domain=$h
  308. return 0
  309. fi
  310. return 1
  311. fi
  312. p=$i
  313. i=$(_math "$i" + 1)
  314. done
  315. return 1
  316. }