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.

122 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. }
  35. #Usage: fulldomain txtvalue
  36. #Remove the txt record after validation.
  37. dns_gandi_livedns_rm() {
  38. fulldomain=$1
  39. txtvalue=$2
  40. _debug "First detect the root zone"
  41. if ! _get_root "$fulldomain"; then
  42. _err "invalid domain"
  43. return 1
  44. fi
  45. _debug fulldomain "$fulldomain"
  46. _debug domain "$_domain"
  47. _debug sub_domain "$_sub_domain"
  48. _gandi_livedns_rest DELETE "domains/$_domain/records/$_sub_domain/TXT" ""
  49. }
  50. #################### Private functions below ##################################
  51. #_acme-challenge.www.domain.com
  52. #returns
  53. # _sub_domain=_acme-challenge.www
  54. # _domain=domain.com
  55. _get_root() {
  56. domain=$1
  57. i=2
  58. p=1
  59. while true; do
  60. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  61. _debug h "$h"
  62. if [ -z "$h" ]; then
  63. #not valid
  64. return 1
  65. fi
  66. if ! _gandi_livedns_rest GET "domains/$h"; then
  67. return 1
  68. fi
  69. if _contains "$response" '"code": 401'; then
  70. _err "$response"
  71. return 1
  72. elif _contains "$response" '"code": 404'; then
  73. _debug "$h not found"
  74. else
  75. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  76. _domain="$h"
  77. return 0
  78. fi
  79. p="$i"
  80. i=$(_math "$i" + 1)
  81. done
  82. return 1
  83. }
  84. _gandi_livedns_rest() {
  85. m=$1
  86. ep="$2"
  87. data="$3"
  88. _debug "$ep"
  89. export _H1="Content-Type: application/json"
  90. export _H2="X-Api-Key: $GANDI_LIVEDNS_KEY"
  91. if [ "$m" = "GET" ]; then
  92. response="$(_get "$GANDI_LIVEDNS_API/$ep")"
  93. else
  94. _debug data "$data"
  95. response="$(_post "$data" "$GANDI_LIVEDNS_API/$ep" "" "$m")"
  96. fi
  97. if [ "$?" != "0" ]; then
  98. _err "error $ep"
  99. return 1
  100. fi
  101. _debug2 response "$response"
  102. return 0
  103. }