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.

225 lines
7.3 KiB

  1. #!/usr/bin/env sh
  2. ## Will be called by acme.sh to add the txt record to your api system.
  3. ## returns 0 means success, otherwise error.
  4. ## Author: thewer <github at thewer.com>
  5. ## GitHub: https://github.com/gitwer/acme.sh
  6. ##
  7. ## Environment Variables Required:
  8. ##
  9. ## DO_API_KEY="75310dc4ca779ac39a19f6355db573b49ce92ae126553ebd61ac3a3ae34834cc"
  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_dgon_add() {
  16. fulldomain="$(echo "$1" | _lower_case)"
  17. txtvalue=$2
  18. DO_API_KEY="${DO_API_KEY:-$(_readaccountconf_mutable DO_API_KEY)}"
  19. # Check if API Key Exist
  20. if [ -z "$DO_API_KEY" ]; then
  21. DO_API_KEY=""
  22. _err "You did not specify DigitalOcean API key."
  23. _err "Please export DO_API_KEY and try again."
  24. return 1
  25. fi
  26. _info "Using digitalocean 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 DO_API_KEY "$DO_API_KEY"
  31. ## split the domain for DO 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. ## Set the header with our post type and key auth key
  39. export _H1="Content-Type: application/json"
  40. export _H2="Authorization: Bearer $DO_API_KEY"
  41. PURL='https://api.digitalocean.com/v2/domains/'$_domain'/records'
  42. PBODY='{"type":"TXT","name":"'$_sub_domain'","data":"'$txtvalue'","ttl":120}'
  43. _debug PURL "$PURL"
  44. _debug PBODY "$PBODY"
  45. ## the create request - post
  46. ## args: BODY, URL, [need64, httpmethod]
  47. response="$(_post "$PBODY" "$PURL")"
  48. ## check response
  49. if [ "$?" != "0" ]; then
  50. _err "error in response: $response"
  51. return 1
  52. fi
  53. _debug2 response "$response"
  54. ## finished correctly
  55. return 0
  56. }
  57. ## Remove the txt record after validation.
  58. ## Usage: fulldomain txtvalue
  59. ## EG: "_acme-challenge.www.other.domain.com" "XKrxpRBosdq0HG9i01zxXp5CPBs"
  60. dns_dgon_rm() {
  61. fulldomain="$(echo "$1" | _lower_case)"
  62. txtvalue=$2
  63. DO_API_KEY="${DO_API_KEY:-$(_readaccountconf_mutable DO_API_KEY)}"
  64. # Check if API Key Exist
  65. if [ -z "$DO_API_KEY" ]; then
  66. DO_API_KEY=""
  67. _err "You did not specify DigitalOcean API key."
  68. _err "Please export DO_API_KEY and try again."
  69. return 1
  70. fi
  71. _info "Using digitalocean dns validation - remove record"
  72. _debug fulldomain "$fulldomain"
  73. _debug txtvalue "$txtvalue"
  74. ## split the domain for DO API
  75. if ! _get_base_domain "$fulldomain"; then
  76. _err "domain not found in your account for removal"
  77. return 1
  78. fi
  79. _debug _sub_domain "$_sub_domain"
  80. _debug _domain "$_domain"
  81. ## Set the header with our post type and key auth key
  82. export _H1="Content-Type: application/json"
  83. export _H2="Authorization: Bearer $DO_API_KEY"
  84. ## get URL for the list of domains
  85. ## may get: "links":{"pages":{"last":".../v2/domains/DOM/records?page=2","next":".../v2/domains/DOM/records?page=2"}}
  86. GURL="https://api.digitalocean.com/v2/domains/$_domain/records"
  87. ## while we dont have a record ID we keep going
  88. while [ -z "$record" ]; do
  89. ## 1) get the URL
  90. ## the create request - get
  91. ## args: URL, [onlyheader, timeout]
  92. domain_list="$(_get "$GURL")"
  93. ## 2) find record
  94. ## check for what we are looing for: "type":"A","name":"$_sub_domain"
  95. record="$(echo "$domain_list" | _egrep_o "\"id\"\s*\:\s*\"*[0-9]+\"*[^}]*\"name\"\s*\:\s*\"$_sub_domain\"[^}]*\"data\"\s*\:\s*\"$txtvalue\"")"
  96. ## 3) check record and get next page
  97. if [ -z "$record" ]; then
  98. ## find the next page if we dont have a match
  99. nextpage="$(echo "$domain_list" | _egrep_o "\"links\".*" | _egrep_o "\"next\".*" | _egrep_o "http.*page\=[0-9]+")"
  100. if [ -z "$nextpage" ]; then
  101. _err "no record and no nextpage in digital ocean DNS removal"
  102. return 1
  103. fi
  104. _debug2 nextpage "$nextpage"
  105. GURL="$nextpage"
  106. fi
  107. ## we break out of the loop when we have a record
  108. done
  109. ## we found the record
  110. rec_id="$(echo "$record" | _egrep_o "id\"\s*\:\s*\"*[0-9]+" | _egrep_o "[0-9]+")"
  111. _debug rec_id "$rec_id"
  112. ## delete the record
  113. ## delete URL for removing the one we dont want
  114. DURL="https://api.digitalocean.com/v2/domains/$_domain/records/$rec_id"
  115. ## the create request - delete
  116. ## args: BODY, URL, [need64, httpmethod]
  117. response="$(_post "" "$DURL" "" "DELETE")"
  118. ## check response (sort of)
  119. if [ "$?" != "0" ]; then
  120. _err "error in remove response: $response"
  121. return 1
  122. fi
  123. _debug2 response "$response"
  124. ## finished correctly
  125. return 0
  126. }
  127. ##################### Private functions below #####################
  128. ## Split the domain provided into the "bade domain" and the "start prefix".
  129. ## This function searches for the longest subdomain in your account
  130. ## for the full domain given and splits it into the base domain (zone)
  131. ## and the prefix/record to be added/removed
  132. ## USAGE: fulldomain
  133. ## EG: "_acme-challenge.two.three.four.domain.com"
  134. ## returns
  135. ## _sub_domain="_acme-challenge.two"
  136. ## _domain="three.four.domain.com" *IF* zone "three.four.domain.com" exists
  137. ## if only "domain.com" exists it will return
  138. ## _sub_domain="_acme-challenge.two.three.four"
  139. ## _domain="domain.com"
  140. _get_base_domain() {
  141. # args
  142. fulldomain="$(echo "$1" | tr '[:upper:]' '[:lower:]')"
  143. _debug fulldomain "$fulldomain"
  144. # domain max legal length = 253
  145. MAX_DOM=255
  146. ## get a list of domains for the account to check thru
  147. ## Set the headers
  148. export _H1="Content-Type: application/json"
  149. export _H2="Authorization: Bearer $DO_API_KEY"
  150. _debug DO_API_KEY "$DO_API_KEY"
  151. ## get URL for the list of domains
  152. ## havent seen this request paginated, tested with 18 domains (more requires manual requests with DO)
  153. DOMURL="https://api.digitalocean.com/v2/domains"
  154. ## get the domain list (DO gives basically a full XFER!)
  155. domain_list="$(_get "$DOMURL")"
  156. ## check response
  157. if [ "$?" != "0" ]; then
  158. _err "error in domain_list response: $domain_list"
  159. return 1
  160. fi
  161. _debug2 domain_list "$domain_list"
  162. ## for each shortening of our $fulldomain, check if it exists in the $domain_list
  163. ## can never start on 1 (aka whole $fulldomain) as $fulldomain starts with "_acme-challenge"
  164. i=2
  165. while [ $i -gt 0 ]; do
  166. ## get next longest domain
  167. _domain=$(printf "%s" "$fulldomain" | cut -d . -f "$i"-"$MAX_DOM")
  168. ## check we got something back from our cut (or are we at the end)
  169. if [ -z "$_domain" ]; then
  170. ## we got to the end of the domain - invalid domain
  171. _err "domain not found in DigitalOcean account"
  172. return 1
  173. fi
  174. ## we got part of a domain back - grep it out
  175. found="$(echo "$domain_list" | _egrep_o "\"name\"\s*\:\s*\"$_domain\"")"
  176. ## check if it exists
  177. if [ ! -z "$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. _debug _domain "$_domain"
  182. _debug _sub_domain "$_sub_domain"
  183. return 0
  184. fi
  185. ## increment cut point $i
  186. i=$(_math $i + 1)
  187. done
  188. ## we went through the entire domain zone list and dint find one that matched
  189. ## doesnt look like we can add in the record
  190. _err "domain not found in DigitalOcean account, but we should never get here"
  191. return 1
  192. }