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.

120 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. return $?
  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. return $?
  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. 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": 404'; then
  70. _debug "$h not found"
  71. else
  72. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  73. _domain="$h"
  74. return 0
  75. fi
  76. p="$i"
  77. i=$(_math "$i" + 1)
  78. done
  79. return 1
  80. }
  81. _gandi_livedns_rest() {
  82. m=$1
  83. ep="$2"
  84. data="$3"
  85. _debug "$ep"
  86. export _H1="Content-Type: application/json"
  87. export _H2="X-Api-Key: $GANDI_LIVEDNS_KEY"
  88. if [ "$data" ] || [ "$m" = "DELETE" ]; then
  89. _debug data "$data"
  90. response="$(_post "$data" "$GANDI_LIVEDNS_API/$ep" "" "$m")"
  91. else
  92. response="$(_get "$GANDI_LIVEDNS_API/$ep")"
  93. fi
  94. if [ "$?" != "0" ]; then
  95. _err "error $ep"
  96. return 1
  97. fi
  98. _debug2 response "$response"
  99. return 0
  100. }