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.

147 lines
4.1 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_ionos_cloud_info='IONOS Cloud DNS
  4. Site: ionos.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_ionos_cloud
  6. Options:
  7. IONOS_TOKEN API Token.
  8. Issues: github.com/acmesh-official/acme.sh/issues/5243
  9. '
  10. # Supports IONOS Cloud DNS API v1.15.4
  11. IONOS_CLOUD_API="https://dns.de-fra.ionos.com"
  12. IONOS_CLOUD_ROUTE_ZONES="/zones"
  13. dns_ionos_cloud_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. if ! _ionos_init; then
  17. return 1
  18. fi
  19. _record_name=$(printf "%s" "$fulldomain" | cut -d . -f 1)
  20. _body="{\"properties\":{\"name\":\"$_record_name\", \"type\":\"TXT\", \"content\":\"$txtvalue\"}}"
  21. if _ionos_cloud_rest POST "$IONOS_CLOUD_ROUTE_ZONES/$_zone_id/records" "$_body" && [ "$_code" = "202" ]; then
  22. _info "TXT record has been created successfully."
  23. return 0
  24. fi
  25. return 1
  26. }
  27. dns_ionos_cloud_rm() {
  28. fulldomain=$1
  29. txtvalue=$2
  30. if ! _ionos_init; then
  31. return 1
  32. fi
  33. if ! _ionos_cloud_get_record "$_zone_id" "$txtvalue" "$fulldomain"; then
  34. _err "Could not find _acme-challenge TXT record."
  35. return 1
  36. fi
  37. if _ionos_cloud_rest DELETE "$IONOS_CLOUD_ROUTE_ZONES/$_zone_id/records/$_record_id" && [ "$_code" = "202" ]; then
  38. _info "TXT record has been deleted successfully."
  39. return 0
  40. fi
  41. return 1
  42. }
  43. _ionos_init() {
  44. IONOS_TOKEN="${IONOS_TOKEN:-$(_readaccountconf_mutable IONOS_TOKEN)}"
  45. if [ -z "$IONOS_TOKEN" ]; then
  46. _err "You didn't specify an IONOS token yet."
  47. _err "Read https://api.ionos.com/docs/authentication/v1/#tag/tokens/operation/tokensGenerate to learn how to get a token."
  48. _err "You need to set it before calling acme.sh:"
  49. _err "\$ export IONOS_TOKEN=\"...\""
  50. _err "\$ acme.sh --issue -d ... --dns dns_ionos_cloud"
  51. return 1
  52. fi
  53. _saveaccountconf_mutable IONOS_TOKEN "$IONOS_TOKEN"
  54. if ! _get_cloud_zone "$fulldomain"; then
  55. _err "Cannot find zone $zone in your IONOS account."
  56. return 1
  57. fi
  58. return 0
  59. }
  60. _get_cloud_zone() {
  61. domain=$1
  62. zone=$(printf "%s" "$domain" | cut -d . -f 2-)
  63. if _ionos_cloud_rest GET "$IONOS_CLOUD_ROUTE_ZONES?filter.zoneName=$zone"; then
  64. _response="$(echo "$_response" | tr -d "\n")"
  65. _zone_list_items=$(echo "$_response" | _egrep_o "\"items\":.*")
  66. _zone_id=$(printf "%s\n" "$_zone_list_items" | _egrep_o "\"id\":\"[a-fA-F0-9\-]*\"" | _head_n 1 | cut -d : -f 2 | tr -d '\"')
  67. if [ "$_zone_id" ]; then
  68. return 0
  69. fi
  70. fi
  71. return 1
  72. }
  73. _ionos_cloud_get_record() {
  74. zone_id=$1
  75. txtrecord=$2
  76. # this is to transform the domain to lower case
  77. fulldomain=$(printf "%s" "$3" | _lower_case)
  78. # this is to transform record name to lower case
  79. # IONOS Cloud API transforms all record names to lower case
  80. _record_name=$(printf "%s" "$fulldomain" | cut -d . -f 1 | _lower_case)
  81. if _ionos_cloud_rest GET "$IONOS_CLOUD_ROUTE_ZONES/$zone_id/records"; then
  82. _response="$(echo "$_response" | tr -d "\n")"
  83. pattern="\{\"id\":\"[a-fA-F0-9\-]*\",\"type\":\"record\",\"href\":\"/zones/$zone_id/records/[a-fA-F0-9\-]*\",\"metadata\":\{\"createdDate\":\"[A-Z0-9\:\.\-]*\",\"lastModifiedDate\":\"[A-Z0-9\:\.\-]*\",\"fqdn\":\"$fulldomain\",\"state\":\"AVAILABLE\",\"zoneId\":\"$zone_id\"\},\"properties\":\{\"content\":\"$txtrecord\",\"enabled\":true,\"name\":\"$_record_name\",\"priority\":[0-9]*,\"ttl\":[0-9]*,\"type\":\"TXT\"\}\}"
  84. _record="$(echo "$_response" | _egrep_o "$pattern")"
  85. if [ "$_record" ]; then
  86. _record_id=$(printf "%s\n" "$_record" | _egrep_o "\"id\":\"[a-fA-F0-9\-]*\"" | _head_n 1 | cut -d : -f 2 | tr -d '\"')
  87. return 0
  88. fi
  89. fi
  90. return 1
  91. }
  92. _ionos_cloud_rest() {
  93. method="$1"
  94. route="$2"
  95. data="$3"
  96. export _H1="Authorization: Bearer $IONOS_TOKEN"
  97. # clear headers
  98. : >"$HTTP_HEADER"
  99. if [ "$method" != "GET" ]; then
  100. _response="$(_post "$data" "$IONOS_CLOUD_API$route" "" "$method" "application/json")"
  101. else
  102. _response="$(_get "$IONOS_CLOUD_API$route")"
  103. fi
  104. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  105. if [ "$?" != "0" ]; then
  106. _err "Error $route: $_response"
  107. return 1
  108. fi
  109. _debug2 "_response" "$_response"
  110. _debug2 "_code" "$_code"
  111. return 0
  112. }