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.

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