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.

339 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
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/Neilpang/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. else
  92. _err "error adding validation value ($_code)"
  93. return 1
  94. fi
  95. }
  96. # Usage: fulldomain txtvalue
  97. # Used to remove the txt record after validation
  98. #
  99. # Ref: https://docs.microsoft.com/en-us/rest/api/dns/recordsets/delete
  100. #
  101. dns_azure_rm() {
  102. fulldomain=$1
  103. txtvalue=$2
  104. AZUREDNS_SUBSCRIPTIONID="${AZUREDNS_SUBSCRIPTIONID:-$(_readaccountconf_mutable AZUREDNS_SUBSCRIPTIONID)}"
  105. AZUREDNS_TENANTID="${AZUREDNS_TENANTID:-$(_readaccountconf_mutable AZUREDNS_TENANTID)}"
  106. AZUREDNS_APPID="${AZUREDNS_APPID:-$(_readaccountconf_mutable AZUREDNS_APPID)}"
  107. AZUREDNS_CLIENTSECRET="${AZUREDNS_CLIENTSECRET:-$(_readaccountconf_mutable AZUREDNS_CLIENTSECRET)}"
  108. if [ -z "$AZUREDNS_SUBSCRIPTIONID" ]; then
  109. AZUREDNS_SUBSCRIPTIONID=""
  110. AZUREDNS_TENANTID=""
  111. AZUREDNS_APPID=""
  112. AZUREDNS_CLIENTSECRET=""
  113. _err "You didn't specify the Azure Subscription ID "
  114. return 1
  115. fi
  116. if [ -z "$AZUREDNS_TENANTID" ]; then
  117. AZUREDNS_SUBSCRIPTIONID=""
  118. AZUREDNS_TENANTID=""
  119. AZUREDNS_APPID=""
  120. AZUREDNS_CLIENTSECRET=""
  121. _err "You didn't specify the Azure Tenant ID "
  122. return 1
  123. fi
  124. if [ -z "$AZUREDNS_APPID" ]; then
  125. AZUREDNS_SUBSCRIPTIONID=""
  126. AZUREDNS_TENANTID=""
  127. AZUREDNS_APPID=""
  128. AZUREDNS_CLIENTSECRET=""
  129. _err "You didn't specify the Azure App ID"
  130. return 1
  131. fi
  132. if [ -z "$AZUREDNS_CLIENTSECRET" ]; then
  133. AZUREDNS_SUBSCRIPTIONID=""
  134. AZUREDNS_TENANTID=""
  135. AZUREDNS_APPID=""
  136. AZUREDNS_CLIENTSECRET=""
  137. _err "You didn't specify the Azure Client Secret"
  138. return 1
  139. fi
  140. accesstoken=$(_azure_getaccess_token "$AZUREDNS_TENANTID" "$AZUREDNS_APPID" "$AZUREDNS_CLIENTSECRET")
  141. if ! _get_root "$fulldomain" "$AZUREDNS_SUBSCRIPTIONID" "$accesstoken"; then
  142. _err "invalid domain"
  143. return 1
  144. fi
  145. _debug _domain_id "$_domain_id"
  146. _debug _sub_domain "$_sub_domain"
  147. _debug _domain "$_domain"
  148. acmeRecordURI="https://management.azure.com$(printf '%s' "$_domain_id" | sed 's/\\//g')/TXT/$_sub_domain?api-version=2017-09-01"
  149. _debug "$acmeRecordURI"
  150. # Get existing TXT record
  151. _azure_rest GET "$acmeRecordURI" "" "$accesstoken"
  152. timestamp="$(_time)"
  153. if [ "$_code" = "200" ]; then
  154. vlist="$(echo "$response" | _egrep_o "\"value\"\s*:\s*\[\s*\"[^\"]*\"\s*]" | cut -d : -f 2 | tr -d "[]\"" | grep -v "$txtvalue")"
  155. values=""
  156. comma=""
  157. for v in $vlist; do
  158. values="$values$comma{\"value\":[\"$v\"]}"
  159. comma=","
  160. done
  161. if [ -z "$values" ]; then
  162. # No values left remove record
  163. _debug "removing validation record completely $acmeRecordURI"
  164. _azure_rest DELETE "$acmeRecordURI" "" "$accesstoken"
  165. if [ "$_code" = "200" ] || [ "$_code" = '204' ]; then
  166. _info "validation record removed"
  167. else
  168. _err "error removing validation record ($_code)"
  169. return 1
  170. fi
  171. else
  172. # Remove only txtvalue from the TXT Record
  173. body="{\"properties\":{\"metadata\":{\"acmetscheck\":\"$timestamp\"},\"TTL\":10, \"TXTRecords\":[$values]}}"
  174. _azure_rest PUT "$acmeRecordURI" "$body" "$accesstoken"
  175. if [ "$_code" = "200" ] || [ "$_code" = '201' ]; then
  176. _info "validation value removed"
  177. else
  178. _err "error removing validation value ($_code)"
  179. return 1
  180. fi
  181. fi
  182. fi
  183. }
  184. ################### Private functions below ##################################
  185. _azure_rest() {
  186. m=$1
  187. ep="$2"
  188. data="$3"
  189. accesstoken="$4"
  190. MAX_REQUEST_RETRY_TIMES=5
  191. _request_retry_times=0
  192. while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do
  193. _debug3 _request_retry_times "$_request_retry_times"
  194. export _H1="authorization: Bearer $accesstoken"
  195. export _H2="accept: application/json"
  196. export _H3="Content-Type: application/json"
  197. # clear headers from previous request to avoid getting wrong http code on timeouts
  198. :>"$HTTP_HEADER"
  199. _debug "$ep"
  200. if [ "$m" != "GET" ]; then
  201. _secure_debug2 "data $data"
  202. response="$(_post "$data" "$ep" "" "$m")"
  203. else
  204. response="$(_get "$ep")"
  205. fi
  206. _secure_debug2 "response $response"
  207. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\r\n")"
  208. _debug "http response code $_code"
  209. if [ "$_code" = "401" ]; then
  210. # we have an invalid access token set to expired
  211. _saveaccountconf_mutable AZUREDNS_TOKENVALIDTO "0"
  212. _err "access denied make sure your Azure settings are correct. See $WIKI"
  213. return 1
  214. fi
  215. # See https://docs.microsoft.com/en-us/azure/architecture/best-practices/retry-service-specific#general-rest-and-retry-guidelines for retryable HTTP codes
  216. if [ "$?" != "0" ] || [ -z "$_code" ] || [ "$_code" = "408" ] || [ "$_code" = "500" ] || [ "$_code" = "503" ] || [ "$_code" = "504" ]; then
  217. _request_retry_times="$(_math "$_request_retry_times" + 1)"
  218. _info "REST call error $_code retrying $ep in $_request_retry_times s"
  219. _sleep "$_request_retry_times"
  220. continue
  221. fi
  222. break
  223. done
  224. if [ "$_request_retry_times" = "$MAX_REQUEST_RETRY_TIMES" ]; then
  225. _err "Error Azure REST called was retried $MAX_REQUEST_RETRY_TIMES times."
  226. _err "Calling $ep failed."
  227. return 1
  228. fi
  229. response="$(echo "$response" | _normalizeJson)"
  230. return 0
  231. }
  232. ## Ref: https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-service-to-service#request-an-access-token
  233. _azure_getaccess_token() {
  234. tenantID=$1
  235. clientID=$2
  236. clientSecret=$3
  237. accesstoken="${AZUREDNS_BEARERTOKEN:-$(_readaccountconf_mutable AZUREDNS_BEARERTOKEN)}"
  238. expires_on="${AZUREDNS_TOKENVALIDTO:-$(_readaccountconf_mutable AZUREDNS_TOKENVALIDTO)}"
  239. # can we reuse the bearer token?
  240. if [ -n "$accesstoken" ] && [ -n "$expires_on" ]; then
  241. if [ "$(_time)" -lt "$expires_on" ]; then
  242. # brearer token is still valid - reuse it
  243. _debug "reusing bearer token"
  244. printf "%s" "$accesstoken"
  245. return 0
  246. else
  247. _debug "bearer token expired"
  248. fi
  249. fi
  250. _debug "getting new bearer token"
  251. export _H1="accept: application/json"
  252. export _H2="Content-Type: application/x-www-form-urlencoded"
  253. 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"
  254. _secure_debug2 "data $body"
  255. response="$(_post "$body" "https://login.microsoftonline.com/$tenantID/oauth2/token" "" "POST")"
  256. _secure_debug2 "response $response"
  257. response="$(echo "$response" | _normalizeJson)"
  258. accesstoken=$(echo "$response" | _egrep_o "\"access_token\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \")
  259. expires_on=$(echo "$response" | _egrep_o "\"expires_on\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \")
  260. if [ -z "$accesstoken" ]; then
  261. _err "no acccess token received. Check your Azure settings see $WIKI"
  262. return 1
  263. fi
  264. if [ "$?" != "0" ]; then
  265. _err "error $response"
  266. return 1
  267. fi
  268. _saveaccountconf_mutable AZUREDNS_BEARERTOKEN "$accesstoken"
  269. _saveaccountconf_mutable AZUREDNS_TOKENVALIDTO "$expires_on"
  270. printf "%s" "$accesstoken"
  271. return 0
  272. }
  273. _get_root() {
  274. domain=$1
  275. subscriptionId=$2
  276. accesstoken=$3
  277. i=2
  278. p=1
  279. ## Ref: https://docs.microsoft.com/en-us/rest/api/dns/zones/list
  280. ## returns up to 100 zones in one response therefore handling more results is not not implemented
  281. ## (ZoneListResult with continuation token for the next page of results)
  282. ## Per https://docs.microsoft.com/en-us/azure/azure-subscription-service-limits#dns-limits you are limited to 100 Zone/subscriptions anyways
  283. ##
  284. _azure_rest GET "https://management.azure.com/subscriptions/$subscriptionId/providers/Microsoft.Network/dnszones?api-version=2017-09-01" "" "$accesstoken"
  285. # Find matching domain name is Json response
  286. while true; do
  287. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  288. _debug2 "Checking domain: $h"
  289. if [ -z "$h" ]; then
  290. #not valid
  291. _err "Invalid domain"
  292. return 1
  293. fi
  294. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  295. _domain_id=$(echo "$response" | _egrep_o "\{\"id\":\"[^\"]*$h\"" | head -n 1 | cut -d : -f 2 | tr -d \")
  296. if [ "$_domain_id" ]; then
  297. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  298. _domain=$h
  299. return 0
  300. fi
  301. return 1
  302. fi
  303. p=$i
  304. i=$(_math "$i" + 1)
  305. done
  306. return 1
  307. }