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.

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