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.

161 lines
3.7 KiB

4 years ago
4 years ago
  1. #!/usr/bin/env sh
  2. #
  3. #VULTR_API_KEY=000011112222333344445555666677778888
  4. VULTR_Api="https://api.vultr.com/v2"
  5. ######## Public functions #####################
  6. #
  7. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  8. dns_vultr_add() {
  9. fulldomain=$1
  10. txtvalue=$2
  11. _debug fulldomain "$fulldomain"
  12. _debug txtvalue "$txtvalue"
  13. VULTR_API_KEY="${VULTR_API_KEY:-$(_readaccountconf_mutable VULTR_API_KEY)}"
  14. if test -z "$VULTR_API_KEY"; then
  15. VULTR_API_KEY=''
  16. _err 'VULTR_API_KEY was not exported'
  17. return 1
  18. fi
  19. _saveaccountconf_mutable VULTR_API_KEY "$VULTR_API_KEY"
  20. _debug 'First detect the root zone'
  21. if ! _get_root "$fulldomain"; then
  22. return 1
  23. fi
  24. _debug _sub_domain "$_sub_domain"
  25. _debug _domain "$_domain"
  26. _debug 'Getting txt records'
  27. _vultr_rest GET "domains/$_domain/records"
  28. if printf "%s\n" "$response" | grep -- "\"type\":\"TXT\",\"name\":\"$fulldomain\"" >/dev/null; then
  29. _err 'Error'
  30. return 1
  31. fi
  32. if ! _vultr_rest POST "domains/$_domain/records" "{\"name\":\"$_sub_domain\",\"data\":\"$txtvalue\",\"type\":\"TXT\"}"; then
  33. _err "$response"
  34. return 1
  35. fi
  36. _debug2 _response "$response"
  37. return 0
  38. }
  39. #fulldomain txtvalue
  40. dns_vultr_rm() {
  41. fulldomain=$1
  42. txtvalue=$2
  43. _debug fulldomain "$fulldomain"
  44. _debug txtvalue "$txtvalue"
  45. VULTR_API_KEY="${VULTR_API_KEY:-$(_readaccountconf_mutable VULTR_API_KEY)}"
  46. if test -z "$VULTR_API_KEY"; then
  47. VULTR_API_KEY=""
  48. _err 'VULTR_API_KEY was not exported'
  49. return 1
  50. fi
  51. _saveaccountconf_mutable VULTR_API_KEY "$VULTR_API_KEY"
  52. _debug 'First detect the root zone'
  53. if ! _get_root "$fulldomain"; then
  54. return 1
  55. fi
  56. _debug _sub_domain "$_sub_domain"
  57. _debug _domain "$_domain"
  58. _debug 'Getting txt records'
  59. _vultr_rest GET "domains/$_domain/records"
  60. if printf "%s\n" "$response" | grep -- "\"type\":\"TXT\",\"name\":\"$fulldomain\"" >/dev/null; then
  61. _err 'Error'
  62. return 1
  63. fi
  64. _record_id="$(echo "$response" | tr '{}' '\n' | grep '"TXT"' | grep -- "$txtvalue" | tr ',' '\n' | grep -i 'id' | cut -d : -f 2 | tr -d '"')"
  65. _debug _record_id "$_record_id"
  66. if [ "$_record_id" ]; then
  67. _info "Successfully retrieved the record id for ACME challenge."
  68. else
  69. _info "Empty record id, it seems no such record."
  70. return 0
  71. fi
  72. if ! _vultr_rest DELETE "domains/$_domain/records/$_record_id"; then
  73. _err "$response"
  74. return 1
  75. fi
  76. _debug2 _response "$response"
  77. return 0
  78. }
  79. #################### Private functions below ##################################
  80. #_acme-challenge.www.domain.com
  81. #returns
  82. # _sub_domain=_acme-challenge.www
  83. # _domain=domain.com
  84. # _domain_id=sdjkglgdfewsdfg
  85. _get_root() {
  86. domain=$1
  87. i=1
  88. while true; do
  89. _domain=$(printf "%s" "$domain" | cut -d . -f $i-100)
  90. _debug h "$_domain"
  91. if [ -z "$_domain" ]; then
  92. return 1
  93. fi
  94. if ! _vultr_rest GET "domains"; then
  95. return 1
  96. fi
  97. if printf "%s\n" "$response" | grep -E '^\{.*\}' >/dev/null; then
  98. if _contains "$response" "\"domain\":\"$_domain\""; then
  99. _sub_domain="$(echo "$fulldomain" | sed "s/\\.$_domain\$//")"
  100. return 0
  101. else
  102. _debug "Go to next level of $_domain"
  103. fi
  104. else
  105. _err "$response"
  106. return 1
  107. fi
  108. i=$(_math "$i" + 1)
  109. done
  110. return 1
  111. }
  112. _vultr_rest() {
  113. m=$1
  114. ep="$2"
  115. data="$3"
  116. _debug "$ep"
  117. api_key_trimmed=$(echo "$VULTR_API_KEY" | tr -d '"')
  118. export _H1="Authorization: Bearer $api_key_trimmed"
  119. export _H2='Content-Type: application/json'
  120. if [ "$m" != "GET" ]; then
  121. _debug data "$data"
  122. response="$(_post "$data" "$VULTR_Api/$ep" "" "$m")"
  123. else
  124. response="$(_get "$VULTR_Api/$ep")"
  125. fi
  126. if [ "$?" != "0" ]; then
  127. _err "Error $ep"
  128. return 1
  129. fi
  130. _debug2 response "$response"
  131. return 0
  132. }