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.

190 lines
5.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. ##
  6. ## Environment Variables Required:
  7. ##
  8. ## DO_API_KEY="75310dc4ca779ac39a19f6355db573b49ce92ae126553ebd61ac3a3ae34834cc"
  9. ##
  10. ## DO_DOMAIN_START="3"
  11. ## start of the digital ocean dns base domain from the LEFT
  12. ## (EG: one.two.three.four.five.com -> one.two & three.four.five.com)
  13. ##
  14. ##################### Public functions #####################
  15. ## Create the text record for validation.
  16. ## Usage: fulldomain txtvalue
  17. ## EG: "_acme-challenge.www.other.domain.com" "XKrxpRBosdq0HG9i01zxXp5CPBs"
  18. dns_digitalocean_add() {
  19. fulldomain=$1
  20. txtvalue=$2
  21. _info "Using digitalocean dns validation - add record"
  22. _debug fulldomain "$fulldomain"
  23. _debug txtvalue "$txtvalue"
  24. _debug DO_DOMAIN_START "$DO_DOMAIN_START"
  25. ## split the domain for DO API
  26. if ! _get_base_domain "$fulldomain" "$DO_DOMAIN_START"; then
  27. _err "invalid domain or split"
  28. return 1
  29. fi
  30. _debug _sub_domain "$_sub_domain"
  31. _debug _domain "$_domain"
  32. ## Set the header with our post type and key auth key
  33. _H1="Content-Type: application/json"
  34. _H2="Authorization: Bearer $DO_API_KEY"
  35. PURL='https://api.digitalocean.com/v2/domains/'$_domain'/records'
  36. PBODY='{"type":"TXT","name":"'$_sub_domain'","data":"'$txtvalue'"}'
  37. _debug PURL "$PURL"
  38. _debug PBODY "$PBODY"
  39. ## the create request - post
  40. ## args: BODY, URL, [need64, httpmethod]
  41. response="$(_post "$PBODY" "$PURL")"
  42. ## check response (sort of)
  43. if [ "$?" != "0" ]; then
  44. _err "error in response: $response"
  45. return 1
  46. fi
  47. _debug response "$response"
  48. ## check for result and get the ID so we can delete it later
  49. #response="$(echo "$response" | tr -d "\n" | sed 's/{/\n&/g')"
  50. #do_id="$(echo "$response" | _egrep_o "id\"\s*\:\s*\d+" | _egrep_o "\d+" )"
  51. #if [ -z "$do_id" ]; then
  52. # _err "error getting ID from response: $response"
  53. # return 1;
  54. #fi
  55. #_debug do_id "$do_id"
  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_digitalocean_rm() {
  63. fulldomain=$1
  64. txtvalue=$2
  65. _info "Using digitalocean dns validation - remove record"
  66. _debug fulldomain "$fulldomain"
  67. _debug txtvalue "$txtvalue"
  68. _debug DO_DOMAIN_START "$DO_DOMAIN_START"
  69. ## split the domain for DO API
  70. if ! _get_base_domain "$fulldomain" "$DO_DOMAIN_START"; then
  71. _err "invalid domain or split in remove"
  72. return 1
  73. fi
  74. _debug _sub_domain "$_sub_domain"
  75. _debug _domain "$_domain"
  76. ## Set the header with our post type and key auth key
  77. _H1="Content-Type: application/json"
  78. _H2="Authorization: Bearer $DO_API_KEY"
  79. ## get URL for the list of domains
  80. ## may get: "links":{"pages":{"last":".../v2/domains/DOM/records?page=2","next":".../v2/domains/DOM/records?page=2"}}
  81. GURL="https://api.digitalocean.com/v2/domains/$_domain/records"
  82. ## while we dont have a record ID we keep going
  83. while [ -z "$record" ]; do
  84. ## 1) get the URL
  85. ## the create request - get
  86. ## args: URL, [onlyheader, timeout]
  87. domain_list="$(_get "$GURL")"
  88. ## 2) find record
  89. ## check for what we are looing for: "type":"A","name":"$_sub_domain"
  90. record="$(echo "$domain_list" | _egrep_o "\"id\"\s*\:\s*\"*\d+\"*[^}]*\"name\"\s*\:\s*\"$_sub_domain\"[^}]*\"data\"\s*\:\s*\"$txtvalue\"" )"
  91. ## 3) check record and get next page
  92. if [ -z "$record" ]; then
  93. ## find the next page if we dont have a match
  94. nextpage="$(echo "$domain_list" | _egrep_o "\"links\".*" | _egrep_o "\"next\".*" | _egrep_o "http.*page\=\d+" )"
  95. if [ -z "$nextpage" ]; then
  96. _err "no record and no nextpage in digital ocean DNS removal"
  97. return 1
  98. fi
  99. _debug nextpage "$nextpage"
  100. GURL="$nextpage"
  101. fi
  102. ## we break out of the loop when we have a record
  103. done
  104. ## we found the record
  105. rec_id="$(echo "$record" | _egrep_o "id\"\s*\:\s*\"*\d+" | _egrep_o "\d+" )"
  106. _debug rec_id "$rec_id"
  107. ## delete the record
  108. ## delete URL for removing the one we dont want
  109. DURL="https://api.digitalocean.com/v2/domains/$_domain/records/$rec_id"
  110. ## the create request - delete
  111. ## args: BODY, URL, [need64, httpmethod]
  112. response="$(_post "" "$DURL" "" "DELETE")"
  113. ## check response (sort of)
  114. if [ "$?" != "0" ]; then
  115. _err "error in remove response: $response"
  116. return 1
  117. fi
  118. _debug response "$response"
  119. ## finished correctly
  120. return 0
  121. }
  122. ##################### Private functions below #####################
  123. ## Split the domain provided at "base_domain_start_position" from the FRONT
  124. ## USAGE: fulldomain base_domain_start_position
  125. ## EG: "_acme-challenge.two.three.four.domain.com" "3"
  126. ## returns
  127. ## _sub_domain="_acme-challenge.two"
  128. ## _domain="three.four.domain.com"
  129. _get_base_domain() {
  130. # args
  131. domain=$1
  132. dom_point=$2
  133. sub_point=$(_math $dom_point - 1)
  134. _debug "split domain" "$domain"
  135. _debug "split dom_point" "$dom_point"
  136. _debug "split sub_point" "$sub_point"
  137. # domain max length - 253
  138. MAX_DOM=255
  139. ## cut in half and check
  140. _domain=$(printf "%s" "$domain" | cut -d . -f $dom_point-$MAX_DOM)
  141. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$sub_point)
  142. if [ -z "$_domain" ]; then
  143. ## not valid
  144. _err "invalid split location"
  145. return 1
  146. fi
  147. if [ -z "$_sub_domain" ]; then
  148. ## not valid
  149. _err "invalid split location"
  150. return 1
  151. fi
  152. _debug "split _domain" "$_domain"
  153. _debug "split _sub_domain" "$_sub_domain"
  154. ## all ok
  155. return 0
  156. }