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.

176 lines
4.4 KiB

6 years ago
2 months ago
6 years ago
2 months ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_exoscale_info='Exoscale.com
  4. Site: Exoscale.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_exoscale
  6. Options:
  7. EXOSCALE_API_KEY API Key
  8. EXOSCALE_SECRET_KEY API Secret key
  9. '
  10. EXOSCALE_API=https://api.exoscale.com/dns/v1
  11. ######## Public functions #####################
  12. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. # Used to add txt record
  14. dns_exoscale_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. if ! _checkAuth; then
  18. return 1
  19. fi
  20. _debug "First detect the root zone"
  21. if ! _get_root "$fulldomain"; then
  22. _err "invalid domain"
  23. return 1
  24. fi
  25. _debug _sub_domain "$_sub_domain"
  26. _debug _domain "$_domain"
  27. _info "Adding record"
  28. if _exoscale_rest POST "domains/$_domain_id/records" "{\"record\":{\"name\":\"$_sub_domain\",\"record_type\":\"TXT\",\"content\":\"$txtvalue\",\"ttl\":120}}" "$_domain_token"; then
  29. if _contains "$response" "$txtvalue"; then
  30. _info "Added, OK"
  31. return 0
  32. fi
  33. fi
  34. _err "Add txt record error."
  35. return 1
  36. }
  37. # Usage: fulldomain txtvalue
  38. # Used to remove the txt record after validation
  39. dns_exoscale_rm() {
  40. fulldomain=$1
  41. txtvalue=$2
  42. if ! _checkAuth; then
  43. return 1
  44. fi
  45. _debug "First detect the root zone"
  46. if ! _get_root "$fulldomain"; then
  47. _err "invalid domain"
  48. return 1
  49. fi
  50. _debug _sub_domain "$_sub_domain"
  51. _debug _domain "$_domain"
  52. _debug "Getting txt records"
  53. _exoscale_rest GET "domains/${_domain_id}/records?type=TXT&name=$_sub_domain" "" "$_domain_token"
  54. if _contains "$response" "\"name\":\"$_sub_domain\"" >/dev/null; then
  55. _record_id=$(echo "$response" | tr '{' "\n" | grep "\"content\":\"$txtvalue\"" | _egrep_o "\"id\":[^,]+" | _head_n 1 | cut -d : -f 2 | tr -d \")
  56. fi
  57. if [ -z "$_record_id" ]; then
  58. _err "Can not get record id to remove."
  59. return 1
  60. fi
  61. _debug "Deleting record $_record_id"
  62. if ! _exoscale_rest DELETE "domains/$_domain_id/records/$_record_id" "" "$_domain_token"; then
  63. _err "Delete record error."
  64. return 1
  65. fi
  66. return 0
  67. }
  68. #################### Private functions below ##################################
  69. _checkAuth() {
  70. EXOSCALE_API_KEY="${EXOSCALE_API_KEY:-$(_readaccountconf_mutable EXOSCALE_API_KEY)}"
  71. EXOSCALE_SECRET_KEY="${EXOSCALE_SECRET_KEY:-$(_readaccountconf_mutable EXOSCALE_SECRET_KEY)}"
  72. if [ -z "$EXOSCALE_API_KEY" ] || [ -z "$EXOSCALE_SECRET_KEY" ]; then
  73. EXOSCALE_API_KEY=""
  74. EXOSCALE_SECRET_KEY=""
  75. _err "You don't specify Exoscale application key and application secret yet."
  76. _err "Please create you key and try again."
  77. return 1
  78. fi
  79. _saveaccountconf_mutable EXOSCALE_API_KEY "$EXOSCALE_API_KEY"
  80. _saveaccountconf_mutable EXOSCALE_SECRET_KEY "$EXOSCALE_SECRET_KEY"
  81. return 0
  82. }
  83. #_acme-challenge.www.domain.com
  84. #returns
  85. # _sub_domain=_acme-challenge.www
  86. # _domain=domain.com
  87. # _domain_id=sdjkglgdfewsdfg
  88. # _domain_token=sdjkglgdfewsdfg
  89. _get_root() {
  90. if ! _exoscale_rest GET "domains"; then
  91. return 1
  92. fi
  93. domain=$1
  94. i=2
  95. p=1
  96. while true; do
  97. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  98. _debug h "$h"
  99. if [ -z "$h" ]; then
  100. #not valid
  101. return 1
  102. fi
  103. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  104. _domain_id=$(echo "$response" | tr '{' "\n" | grep "\"name\":\"$h\"" | _egrep_o "\"id\":[^,]+" | _head_n 1 | cut -d : -f 2 | tr -d \")
  105. _domain_token=$(echo "$response" | tr '{' "\n" | grep "\"name\":\"$h\"" | _egrep_o "\"token\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \")
  106. if [ "$_domain_token" ] && [ "$_domain_id" ]; then
  107. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  108. _domain=$h
  109. return 0
  110. fi
  111. return 1
  112. fi
  113. p=$i
  114. i=$(_math "$i" + 1)
  115. done
  116. return 1
  117. }
  118. # returns response
  119. _exoscale_rest() {
  120. method=$1
  121. path="$2"
  122. data="$3"
  123. token="$4"
  124. request_url="$EXOSCALE_API/$path"
  125. _debug "$path"
  126. export _H1="Accept: application/json"
  127. if [ "$token" ]; then
  128. export _H2="X-DNS-Domain-Token: $token"
  129. else
  130. export _H2="X-DNS-Token: $EXOSCALE_API_KEY:$EXOSCALE_SECRET_KEY"
  131. fi
  132. if [ "$data" ] || [ "$method" = "DELETE" ]; then
  133. export _H3="Content-Type: application/json"
  134. _debug data "$data"
  135. response="$(_post "$data" "$request_url" "" "$method")"
  136. else
  137. response="$(_get "$request_url" "" "" "$method")"
  138. fi
  139. if [ "$?" != "0" ]; then
  140. _err "error $request_url"
  141. return 1
  142. fi
  143. _debug2 response "$response"
  144. return 0
  145. }