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.

121 lines
2.7 KiB

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