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.

241 lines
7.6 KiB

  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. {
  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 then 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. body="{\"properties\": {\"TTL\": 3600, \"TXTRecords\": [{\"value\": [\"$txtvalue\"]}]}}"
  64. _azure_rest PUT "$acmeRecordURI" "$body" "$accesstoken"
  65. _debug $response
  66. }
  67. # Usage: fulldomain txtvalue
  68. # Used to remove the txt record after validation
  69. #
  70. # Ref: https://docs.microsoft.com/en-us/rest/api/dns/recordsets/delete
  71. #
  72. dns_azure_rm()
  73. {
  74. fulldomain=$1
  75. txtvalue=$2
  76. AZUREDNS_SUBSCRIPTIONID="${AZUREDNS_SUBSCRIPTIONID:-$(_readaccountconf_mutable AZUREDNS_SUBSCRIPTIONID)}"
  77. AZUREDNS_TENANTID="${AZUREDNS_TENANTID:-$(_readaccountconf_mutable AZUREDNS_TENANTID)}"
  78. AZUREDNS_APPID="${AZUREDNS_APPID:-$(_readaccountconf_mutable AZUREDNS_APPID)}"
  79. AZUREDNS_CLIENTSECRET="${AZUREDNS_CLIENTSECRET:-$(_readaccountconf_mutable AZUREDNS_CLIENTSECRET)}"
  80. if [ -z "$AZUREDNS_SUBSCRIPTIONID" ]; then
  81. AZUREDNS_SUBSCRIPTIONID=""
  82. AZUREDNS_TENANTID=""
  83. AZUREDNS_APPID=""
  84. AZUREDNS_CLIENTSECRET=""
  85. _err "You didn't specify the Azure Subscription ID "
  86. return 1
  87. fi
  88. if [ -z "$AZUREDNS_TENANTID" ] ; then
  89. AZUREDNS_SUBSCRIPTIONID=""
  90. AZUREDNS_TENANTID=""
  91. AZUREDNS_APPID=""
  92. AZUREDNS_CLIENTSECRET=""
  93. _err "You didn't specify the Azure Tenant ID "
  94. return 1
  95. fi
  96. if [ -z "$AZUREDNS_APPID" ] ;then
  97. AZUREDNS_SUBSCRIPTIONID=""
  98. AZUREDNS_TENANTID=""
  99. AZUREDNS_APPID=""
  100. AZUREDNS_CLIENTSECRET=""
  101. _err "You didn't specify the Azure App ID"
  102. return 1
  103. fi
  104. if [ -z "$AZUREDNS_CLIENTSECRET" ]; then
  105. AZUREDNS_SUBSCRIPTIONID=""
  106. AZUREDNS_TENANTID=""
  107. AZUREDNS_APPID=""
  108. AZUREDNS_CLIENTSECRET=""
  109. _err "You didn't specify Azure Client Secret"
  110. return 1
  111. fi
  112. accesstoken=$(_azure_getaccess_token "$AZUREDNS_TENANTID" "$AZUREDNS_APPID" "$AZUREDNS_CLIENTSECRET")
  113. if ! _get_root "$fulldomain" "$AZUREDNS_SUBSCRIPTIONID" "$accesstoken"; then
  114. _err "invalid domain"
  115. return 1
  116. fi
  117. _debug _domain_id "$_domain_id"
  118. _debug _sub_domain "$_sub_domain"
  119. _debug _domain "$_domain"
  120. acmeRecordURI="https://management.azure.com$(printf '%s' $_domain_id |sed 's/\\//g')/TXT/$_sub_domain?api-version=2017-09-01"
  121. _debug $acmeRecordURI
  122. body="{\"properties\": {\"TTL\": 3600, \"TXTRecords\": [{\"value\": [\"$txtvalue\"]}]}}"
  123. _azure_rest DELETE "$acmeRecordURI" "" "$accesstoken"
  124. _debug $response
  125. }
  126. ################### Private functions below ##################################
  127. _azure_rest() {
  128. m=$1
  129. ep="$2"
  130. data="$3"
  131. accesstoken="$4"
  132. _debug "$ep"
  133. export _H1="authorization: Bearer $accesstoken"
  134. export _H2="accept: application/json"
  135. export _H3="Content-Type: application/json"
  136. _H1="authorization: Bearer $accesstoken"
  137. _H2="accept: application/json"
  138. _H3="Content-Type: application/json"
  139. if [ "$m" != "GET" ]; then
  140. _debug data "$data"
  141. response="$(_post "$data" "$ep" "" "$m")"
  142. else
  143. response="$(_get "$ep")"
  144. fi
  145. _debug2 response "$response"
  146. if [ "$?" != "0" ]; then
  147. _err "error $ep"
  148. return 1
  149. fi
  150. return 0
  151. }
  152. ## Ref: https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-service-to-service#request-an-access-token
  153. _azure_getaccess_token() {
  154. TENANTID=$1
  155. clientID=$2
  156. clientSecret=$3
  157. export _H1="accept: application/json"
  158. export _H2="Content-Type: application/x-www-form-urlencoded"
  159. export _H3=""
  160. 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"
  161. _debug data "$body"
  162. response="$(_post "$body" "https://login.windows.net/$TENANTID/oauth2/token" "" "POST" )"
  163. accesstoken=$(printf "%s\n" "$response" | _egrep_o "\"access_token\":\"[^\"]*\"" | head -n 1 | cut -d : -f 2 | tr -d \")
  164. if [ "$?" != "0" ]; then
  165. _err "error $response"
  166. return 1
  167. fi
  168. printf $accesstoken
  169. _debug2 response "$response"
  170. return 0
  171. }
  172. _get_root() {
  173. domain=$1
  174. subscriptionId=$2
  175. accesstoken=$3
  176. i=2
  177. p=1
  178. ## Ref: https://docs.microsoft.com/en-us/rest/api/dns/zones/list
  179. ## returns up to 100 zones in one response therefore handling more results is not not implemented
  180. ## (ZoneListResult with continuation token for the next page of results)
  181. ## Per https://docs.microsoft.com/en-us/azure/azure-subscription-service-limits#dns-limits you are limited to 100 Zone/subscriptions anyways
  182. ##
  183. _azure_rest GET "https://management.azure.com/subscriptions/$subscriptionId/providers/Microsoft.Network/dnszones?api-version=2017-09-01" "" $accesstoken
  184. # Find matching domain name is Json response
  185. while true; do
  186. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  187. _debug2 "Checking domain: $h"
  188. if [ -z "$h" ]; then
  189. #not valid
  190. _err "Invalid domain"
  191. return 1
  192. fi
  193. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  194. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\[.\"id\":\"[^\"]*\"" | head -n 1 | cut -d : -f 2 | tr -d \")
  195. if [ "$_domain_id" ]; then
  196. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  197. _domain=$h
  198. return 0
  199. fi
  200. return 1
  201. fi
  202. p=$i
  203. i=$(_math "$i" + 1)
  204. done
  205. return 1
  206. }