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.

174 lines
5.2 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. export _H1="Content-Type: application/json"
  34. export _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. ## finished correctly
  49. return 0
  50. }
  51. ## Remove the txt record after validation.
  52. ## Usage: fulldomain txtvalue
  53. ## EG: "_acme-challenge.www.other.domain.com" "XKrxpRBosdq0HG9i01zxXp5CPBs"
  54. dns_digitalocean_rm() {
  55. fulldomain=$1
  56. txtvalue=$2
  57. _info "Using digitalocean dns validation - remove record"
  58. _debug fulldomain "$fulldomain"
  59. _debug txtvalue "$txtvalue"
  60. _debug DO_DOMAIN_START "$DO_DOMAIN_START"
  61. ## split the domain for DO API
  62. if ! _get_base_domain "$fulldomain" "$DO_DOMAIN_START"; then
  63. _err "invalid domain or split in remove"
  64. return 1
  65. fi
  66. _debug _sub_domain "$_sub_domain"
  67. _debug _domain "$_domain"
  68. ## Set the header with our post type and key auth key
  69. export _H1="Content-Type: application/json"
  70. export _H2="Authorization: Bearer $DO_API_KEY"
  71. ## get URL for the list of domains
  72. ## may get: "links":{"pages":{"last":".../v2/domains/DOM/records?page=2","next":".../v2/domains/DOM/records?page=2"}}
  73. GURL="https://api.digitalocean.com/v2/domains/$_domain/records"
  74. ## while we dont have a record ID we keep going
  75. while [ -z "$record" ]; do
  76. ## 1) get the URL
  77. ## the create request - get
  78. ## args: URL, [onlyheader, timeout]
  79. domain_list="$(_get "$GURL")"
  80. ## 2) find record
  81. ## check for what we are looing for: "type":"A","name":"$_sub_domain"
  82. record="$(echo "$domain_list" | _egrep_o "\"id\"\s*\:\s*\"*\d+\"*[^}]*\"name\"\s*\:\s*\"$_sub_domain\"[^}]*\"data\"\s*\:\s*\"$txtvalue\"")"
  83. ## 3) check record and get next page
  84. if [ -z "$record" ]; then
  85. ## find the next page if we dont have a match
  86. nextpage="$(echo "$domain_list" | _egrep_o "\"links\".*" | _egrep_o "\"next\".*" | _egrep_o "http.*page\=\d+")"
  87. if [ -z "$nextpage" ]; then
  88. _err "no record and no nextpage in digital ocean DNS removal"
  89. return 1
  90. fi
  91. _debug nextpage "$nextpage"
  92. GURL="$nextpage"
  93. fi
  94. ## we break out of the loop when we have a record
  95. done
  96. ## we found the record
  97. rec_id="$(echo "$record" | _egrep_o "id\"\s*\:\s*\"*\d+" | _egrep_o "\d+")"
  98. _debug rec_id "$rec_id"
  99. ## delete the record
  100. ## delete URL for removing the one we dont want
  101. DURL="https://api.digitalocean.com/v2/domains/$_domain/records/$rec_id"
  102. ## the create request - delete
  103. ## args: BODY, URL, [need64, httpmethod]
  104. response="$(_post "" "$DURL" "" "DELETE")"
  105. ## check response (sort of)
  106. if [ "$?" != "0" ]; then
  107. _err "error in remove response: $response"
  108. return 1
  109. fi
  110. _debug response "$response"
  111. ## finished correctly
  112. return 0
  113. }
  114. ##################### Private functions below #####################
  115. ## Split the domain provided at "base_domain_start_position" from the FRONT
  116. ## USAGE: fulldomain base_domain_start_position
  117. ## EG: "_acme-challenge.two.three.four.domain.com" "3"
  118. ## returns
  119. ## _sub_domain="_acme-challenge.two"
  120. ## _domain="three.four.domain.com"
  121. _get_base_domain() {
  122. # args
  123. domain=$1
  124. dom_point=$2
  125. sub_point=$(_math "$dom_point" - 1)
  126. _debug "split domain" "$domain"
  127. _debug "split dom_point" "$dom_point"
  128. _debug "split sub_point" "$sub_point"
  129. # domain max length - 253
  130. MAX_DOM=255
  131. ## cut in half and check
  132. _domain=$(printf "%s" "$domain" | cut -d . -f "$dom_point"-"$MAX_DOM")
  133. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$sub_point")
  134. if [ -z "$_domain" ]; then
  135. ## not valid
  136. _err "invalid split location"
  137. return 1
  138. fi
  139. if [ -z "$_sub_domain" ]; then
  140. ## not valid
  141. _err "invalid split location"
  142. return 1
  143. fi
  144. _debug "split _domain" "$_domain"
  145. _debug "split _sub_domain" "$_sub_domain"
  146. ## all ok
  147. return 0
  148. }