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.

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