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.

123 lines
2.8 KiB

8 years ago
8 years ago
  1. #!/usr/bin/env sh
  2. # Gandi LiveDNS v5 API
  3. # http://doc.livedns.gandi.net/
  4. # currently under beta
  5. #
  6. # Requires GANDI API KEY set in GANDI_LIVEDNS_KEY set as environment variable
  7. #
  8. #Author: Frédéric Crozat <fcrozat@suse.com>
  9. #Report Bugs here: https://github.com/fcrozat/acme.sh
  10. #
  11. ######## Public functions #####################
  12. GANDI_LIVEDNS_API="https://dns.beta.gandi.net/api/v5"
  13. #Usage: dns_gandi_livedns_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_gandi_livedns_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. if [ -z "$GANDI_LIVEDNS_KEY" ]; then
  18. _err "No API key specifed for Gandi LiveDNS."
  19. _err "Create your key and export it as GANDI_LIVEDNS_KEY"
  20. return 1
  21. fi
  22. _saveaccountconf GANDI_LIVEDNS_KEY "$GANDI_LIVEDNS_KEY"
  23. _debug "First detect the root zone"
  24. if ! _get_root "$fulldomain"; then
  25. _err "invalid domain"
  26. return 1
  27. fi
  28. _debug fulldomain "$fulldomain"
  29. _debug txtvalue "$txtvalue"
  30. _debug domain "$_domain"
  31. _debug sub_domain "$_sub_domain"
  32. _gandi_livedns_rest PUT "domains/$_domain/records/$_sub_domain/TXT" "{\"rrset_ttl\": 300, \"rrset_values\":[\"$txtvalue\"]}" \
  33. && _contains "$response" '{"message": "Zone Record Created"}' \
  34. && _info "Add $(__green "success")"
  35. }
  36. #Usage: fulldomain txtvalue
  37. #Remove the txt record after validation.
  38. dns_gandi_livedns_rm() {
  39. fulldomain=$1
  40. txtvalue=$2
  41. _debug "First detect the root zone"
  42. if ! _get_root "$fulldomain"; then
  43. _err "invalid domain"
  44. return 1
  45. fi
  46. _debug fulldomain "$fulldomain"
  47. _debug domain "$_domain"
  48. _debug sub_domain "$_sub_domain"
  49. _gandi_livedns_rest DELETE "domains/$_domain/records/$_sub_domain/TXT" ""
  50. }
  51. #################### Private functions below ##################################
  52. #_acme-challenge.www.domain.com
  53. #returns
  54. # _sub_domain=_acme-challenge.www
  55. # _domain=domain.com
  56. _get_root() {
  57. domain=$1
  58. i=2
  59. p=1
  60. while true; do
  61. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  62. _debug h "$h"
  63. if [ -z "$h" ]; then
  64. #not valid
  65. return 1
  66. fi
  67. if ! _gandi_livedns_rest GET "domains/$h"; then
  68. return 1
  69. fi
  70. if _contains "$response" '"code": 401'; then
  71. _err "$response"
  72. return 1
  73. elif _contains "$response" '"code": 404'; then
  74. _debug "$h not found"
  75. else
  76. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  77. _domain="$h"
  78. return 0
  79. fi
  80. p="$i"
  81. i=$(_math "$i" + 1)
  82. done
  83. return 1
  84. }
  85. _gandi_livedns_rest() {
  86. m=$1
  87. ep="$2"
  88. data="$3"
  89. _debug "$ep"
  90. export _H1="Content-Type: application/json"
  91. export _H2="X-Api-Key: $GANDI_LIVEDNS_KEY"
  92. if [ "$m" = "GET" ]; then
  93. response="$(_get "$GANDI_LIVEDNS_API/$ep")"
  94. else
  95. _debug data "$data"
  96. response="$(_post "$data" "$GANDI_LIVEDNS_API/$ep" "" "$m")"
  97. fi
  98. if [ "$?" != "0" ]; then
  99. _err "error $ep"
  100. return 1
  101. fi
  102. _debug2 response "$response"
  103. return 0
  104. }