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.

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