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.

248 lines
7.7 KiB

2 years ago
2 years ago
  1. #!/usr/bin/env sh
  2. ## Will be called by acme.sh to add the TXT record via the Bunny DNS API.
  3. ## returns 0 means success, otherwise error.
  4. ## Author: nosilver4u <nosilver4u at ewww.io>
  5. ## GitHub: https://github.com/nosilver4u/acme.sh
  6. ##
  7. ## Environment Variables Required:
  8. ##
  9. ## BUNNY_API_KEY="75310dc4-ca77-9ac3-9a19-f6355db573b49ce92ae1-2655-3ebd-61ac-3a3ae34834cc"
  10. ##
  11. ##################### Public functions #####################
  12. ## Create the text record for validation.
  13. ## Usage: fulldomain txtvalue
  14. ## EG: "_acme-challenge.www.other.domain.com" "XKrxpRBosdq0HG9i01zxXp5CPBs"
  15. dns_bunny_add() {
  16. fulldomain="$(echo "$1" | _lower_case)"
  17. txtvalue=$2
  18. BUNNY_API_KEY="${BUNNY_API_KEY:-$(_readaccountconf_mutable BUNNY_API_KEY)}"
  19. # Check if API Key is set
  20. if [ -z "$BUNNY_API_KEY" ]; then
  21. BUNNY_API_KEY=""
  22. _err "You did not specify Bunny.net API key."
  23. _err "Please export BUNNY_API_KEY and try again."
  24. return 1
  25. fi
  26. _info "Using Bunny.net dns validation - add record"
  27. _debug fulldomain "$fulldomain"
  28. _debug txtvalue "$txtvalue"
  29. ## save the env vars (key and domain split location) for later automated use
  30. _saveaccountconf_mutable BUNNY_API_KEY "$BUNNY_API_KEY"
  31. ## split the domain for Bunny API
  32. if ! _get_base_domain "$fulldomain"; then
  33. _err "domain not found in your account for addition"
  34. return 1
  35. fi
  36. _debug _sub_domain "$_sub_domain"
  37. _debug _domain "$_domain"
  38. _debug _domain_id "$_domain_id"
  39. ## Set the header with our post type and auth key
  40. export _H1="Accept: application/json"
  41. export _H2="AccessKey: $BUNNY_API_KEY"
  42. export _H3="Content-Type: application/json"
  43. PURL="https://api.bunny.net/dnszone/$_domain_id/records"
  44. PBODY='{"Id":'$_domain_id',"Type":3,"Name":"'$_sub_domain'","Value":"'$txtvalue'","ttl":120}'
  45. _debug PURL "$PURL"
  46. _debug PBODY "$PBODY"
  47. ## the create request - POST
  48. ## args: BODY, URL, [need64, httpmethod]
  49. response="$(_post "$PBODY" "$PURL" "" "PUT")"
  50. ## check response
  51. if [ "$?" != "0" ]; then
  52. _err "error in response: $response"
  53. return 1
  54. fi
  55. _debug2 response "$response"
  56. ## finished correctly
  57. return 0
  58. }
  59. ## Remove the txt record after validation.
  60. ## Usage: fulldomain txtvalue
  61. ## EG: "_acme-challenge.www.other.domain.com" "XKrxpRBosdq0HG9i01zxXp5CPBs"
  62. dns_bunny_rm() {
  63. fulldomain="$(echo "$1" | _lower_case)"
  64. txtvalue=$2
  65. BUNNY_API_KEY="${BUNNY_API_KEY:-$(_readaccountconf_mutable BUNNY_API_KEY)}"
  66. # Check if API Key Exists
  67. if [ -z "$BUNNY_API_KEY" ]; then
  68. BUNNY_API_KEY=""
  69. _err "You did not specify Bunny.net API key."
  70. _err "Please export BUNNY_API_KEY and try again."
  71. return 1
  72. fi
  73. _info "Using Bunny.net dns validation - remove record"
  74. _debug fulldomain "$fulldomain"
  75. _debug txtvalue "$txtvalue"
  76. ## split the domain for Bunny API
  77. if ! _get_base_domain "$fulldomain"; then
  78. _err "Domain not found in your account for TXT record removal"
  79. return 1
  80. fi
  81. _debug _sub_domain "$_sub_domain"
  82. _debug _domain "$_domain"
  83. _debug _domain_id "$_domain_id"
  84. ## Set the header with our post type and key auth key
  85. export _H1="Accept: application/json"
  86. export _H2="AccessKey: $BUNNY_API_KEY"
  87. ## get URL for the list of DNS records
  88. GURL="https://api.bunny.net/dnszone/$_domain_id"
  89. ## 1) Get the domain/zone records
  90. ## the fetch request - GET
  91. ## args: URL, [onlyheader, timeout]
  92. domain_list="$(_get "$GURL")"
  93. ## check response
  94. if [ "$?" != "0" ]; then
  95. _err "error in domain_list response: $domain_list"
  96. return 1
  97. fi
  98. _debug2 domain_list "$domain_list"
  99. ## 2) search through records
  100. ## check for what we are looking for: "Type":3,"Value":"$txtvalue","Name":"$_sub_domain"
  101. record="$(echo "$domain_list" | _egrep_o "\"Id\"\s*\:\s*\"*[0-9]+\"*,\s*\"Type\"[^}]*\"Value\"\s*\:\s*\"$txtvalue\"[^}]*\"Name\"\s*\:\s*\"$_sub_domain\"")"
  102. if [ -n "$record" ]; then
  103. ## We found records
  104. rec_ids="$(echo "$record" | _egrep_o "Id\"\s*\:\s*\"*[0-9]+" | _egrep_o "[0-9]+")"
  105. _debug rec_ids "$rec_ids"
  106. if [ -n "$rec_ids" ]; then
  107. echo "$rec_ids" | while IFS= read -r rec_id; do
  108. ## delete the record
  109. ## delete URL for removing the one we dont want
  110. DURL="https://api.bunny.net/dnszone/$_domain_id/records/$rec_id"
  111. ## the removal request - DELETE
  112. ## args: BODY, URL, [need64, httpmethod]
  113. response="$(_post "" "$DURL" "" "DELETE")"
  114. ## check response (sort of)
  115. if [ "$?" != "0" ]; then
  116. _err "error in remove response: $response"
  117. return 1
  118. fi
  119. _debug2 response "$response"
  120. done
  121. fi
  122. fi
  123. ## finished correctly
  124. return 0
  125. }
  126. ##################### Private functions below #####################
  127. ## Split the domain provided into the "base domain" and the "start prefix".
  128. ## This function searches for the longest subdomain in your account
  129. ## for the full domain given and splits it into the base domain (zone)
  130. ## and the prefix/record to be added/removed
  131. ## USAGE: fulldomain
  132. ## EG: "_acme-challenge.two.three.four.domain.com"
  133. ## returns
  134. ## _sub_domain="_acme-challenge.two"
  135. ## _domain="three.four.domain.com" *IF* zone "three.four.domain.com" exists
  136. ## _domain_id=234
  137. ## if only "domain.com" exists it will return
  138. ## _sub_domain="_acme-challenge.two.three.four"
  139. ## _domain="domain.com"
  140. ## _domain_id=234
  141. _get_base_domain() {
  142. # args
  143. fulldomain="$(echo "$1" | _lower_case)"
  144. _debug fulldomain "$fulldomain"
  145. # domain max legal length = 253
  146. MAX_DOM=255
  147. page=1
  148. ## get a list of domains for the account to check thru
  149. ## Set the headers
  150. export _H1="Accept: application/json"
  151. export _H2="AccessKey: $BUNNY_API_KEY"
  152. _debug BUNNY_API_KEY "$BUNNY_API_KEY"
  153. ## get URL for the list of domains
  154. ## may get: "links":{"pages":{"last":".../v2/domains/DOM/records?page=2","next":".../v2/domains/DOM/records?page=2"}}
  155. DOMURL="https://api.bunny.net/dnszone"
  156. ## while we dont have a matching domain we keep going
  157. while [ -z "$found" ]; do
  158. ## get the domain list (current page)
  159. domain_list="$(_get "$DOMURL")"
  160. ## check response
  161. if [ "$?" != "0" ]; then
  162. _err "error in domain_list response: $domain_list"
  163. return 1
  164. fi
  165. _debug2 domain_list "$domain_list"
  166. i=1
  167. while [ $i -gt 0 ]; do
  168. ## get next longest domain
  169. _domain=$(printf "%s" "$fulldomain" | cut -d . -f "$i"-"$MAX_DOM")
  170. ## check we got something back from our cut (or are we at the end)
  171. if [ -z "$_domain" ]; then
  172. break
  173. fi
  174. ## we got part of a domain back - grep it out
  175. found="$(echo "$domain_list" | _egrep_o "\"Id\"\s*:\s*\"*[0-9]+\"*,\s*\"Domain\"\s*\:\s*\"$_domain\"")"
  176. ## check if it exists
  177. if [ -n "$found" ]; then
  178. ## exists - exit loop returning the parts
  179. sub_point=$(_math $i - 1)
  180. _sub_domain=$(printf "%s" "$fulldomain" | cut -d . -f 1-"$sub_point")
  181. _domain_id="$(echo "$found" | _egrep_o "Id\"\s*\:\s*\"*[0-9]+" | _egrep_o "[0-9]+")"
  182. _debug _domain_id "$_domain_id"
  183. _debug _domain "$_domain"
  184. _debug _sub_domain "$_sub_domain"
  185. found=""
  186. return 0
  187. fi
  188. ## increment cut point $i
  189. i=$(_math $i + 1)
  190. done
  191. if [ -z "$found" ]; then
  192. page=$(_math $page + 1)
  193. nextpage="https://api.bunny.net/dnszone?page=$page"
  194. ## Find the next page if we don't have a match.
  195. hasnextpage="$(echo "$domain_list" | _egrep_o "\"HasMoreItems\"\s*:\s*true")"
  196. if [ -z "$hasnextpage" ]; then
  197. _err "No record and no nextpage in Bunny.net domain search."
  198. found=""
  199. return 1
  200. fi
  201. _debug2 nextpage "$nextpage"
  202. DOMURL="$nextpage"
  203. fi
  204. done
  205. ## We went through the entire domain zone list and didn't find one that matched.
  206. ## If we ever get here, something is broken in the code...
  207. _err "Domain not found in Bunny.net account, but we should never get here!"
  208. found=""
  209. return 1
  210. }