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.

149 lines
3.8 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_variomedia_info='variomedia.de
  4. Site: variomedia.de
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_variomedia
  6. Options:
  7. VARIOMEDIA_API_TOKEN API Token
  8. Issues: github.com/acmesh-official/acme.sh/issues/2564
  9. '
  10. VARIOMEDIA_API="https://api.variomedia.de"
  11. ######## Public functions #####################
  12. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. dns_variomedia_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. _debug fulldomain "$fulldomain"
  17. _debug txtvalue "$txtvalue"
  18. VARIOMEDIA_API_TOKEN="${VARIOMEDIA_API_TOKEN:-$(_readaccountconf_mutable VARIOMEDIA_API_TOKEN)}"
  19. if test -z "$VARIOMEDIA_API_TOKEN"; then
  20. VARIOMEDIA_API_TOKEN=""
  21. _err 'VARIOMEDIA_API_TOKEN was not exported'
  22. return 1
  23. fi
  24. _saveaccountconf_mutable VARIOMEDIA_API_TOKEN "$VARIOMEDIA_API_TOKEN"
  25. _debug 'First detect the root zone'
  26. if ! _get_root "$fulldomain"; then
  27. return 1
  28. fi
  29. _debug _sub_domain "$_sub_domain"
  30. _debug _domain "$_domain"
  31. if ! _variomedia_rest POST "dns-records" "{\"data\": {\"type\": \"dns-record\", \"attributes\": {\"record_type\": \"TXT\", \"name\": \"$_sub_domain\", \"domain\": \"$_domain\", \"data\": \"$txtvalue\", \"ttl\":300}}}"; then
  32. _err "$response"
  33. return 1
  34. fi
  35. _debug2 _response "$response"
  36. return 0
  37. }
  38. #fulldomain txtvalue
  39. dns_variomedia_rm() {
  40. fulldomain=$1
  41. txtvalue=$2
  42. _debug fulldomain "$fulldomain"
  43. _debug txtvalue "$txtvalue"
  44. VARIOMEDIA_API_TOKEN="${VARIOMEDIA_API_TOKEN:-$(_readaccountconf_mutable VARIOMEDIA_API_TOKEN)}"
  45. if test -z "$VARIOMEDIA_API_TOKEN"; then
  46. VARIOMEDIA_API_TOKEN=""
  47. _err 'VARIOMEDIA_API_TOKEN was not exported'
  48. return 1
  49. fi
  50. _saveaccountconf_mutable VARIOMEDIA_API_TOKEN "$VARIOMEDIA_API_TOKEN"
  51. _debug 'First detect the root zone'
  52. if ! _get_root "$fulldomain"; then
  53. return 1
  54. fi
  55. _debug _sub_domain "$_sub_domain"
  56. _debug _domain "$_domain"
  57. _debug 'Getting txt records'
  58. if ! _variomedia_rest GET "dns-records?filter[domain]=$_domain"; then
  59. _err 'Error'
  60. return 1
  61. fi
  62. _record_id="$(echo "$response" | sed -E 's/,"tags":\[[^]]*\]//g' | cut -d '[' -f2 | cut -d']' -f1 | sed 's/},[ \t]*{/\},§\{/g' | tr § '\n' | grep "$_sub_domain" | grep -- "$txtvalue" | sed 's/^{//;s/}[,]?$//' | tr , '\n' | tr -d '\"' | grep ^id | cut -d : -f2 | tr -d ' ')"
  63. _debug _record_id "$_record_id"
  64. if [ "$_record_id" ]; then
  65. _info "Successfully retrieved the record id for ACME challenge."
  66. else
  67. _info "Empty record id, it seems no such record."
  68. return 0
  69. fi
  70. if ! _variomedia_rest DELETE "/dns-records/$_record_id"; then
  71. _err "$response"
  72. return 1
  73. fi
  74. _debug2 _response "$response"
  75. return 0
  76. }
  77. #################### Private functions below ##################################
  78. #_acme-challenge.www.domain.com
  79. #returns
  80. # _sub_domain=_acme-challenge.www
  81. # _domain=domain.com
  82. _get_root() {
  83. domain=$1
  84. i=1
  85. p=1
  86. while true; do
  87. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  88. if [ -z "$h" ]; then
  89. return 1
  90. fi
  91. if ! _variomedia_rest GET "domains/$h"; then
  92. return 1
  93. fi
  94. if _contains "$response" "\"id\":\"$h\""; then
  95. _sub_domain=$(printf "%s" "$domain" | cut -d '.' -f 1-$p)
  96. _domain="$h"
  97. return 0
  98. fi
  99. p=$i
  100. i=$(_math "$i" + 1)
  101. done
  102. return 1
  103. }
  104. _variomedia_rest() {
  105. m=$1
  106. ep="$2"
  107. data="$3"
  108. _debug "$ep"
  109. export _H1="Authorization: token $VARIOMEDIA_API_TOKEN"
  110. export _H2="Content-Type: application/vnd.api+json"
  111. export _H3="Accept: application/vnd.variomedia.v1+json"
  112. if [ "$m" != "GET" ]; then
  113. _debug data "$data"
  114. response="$(_post "$data" "$VARIOMEDIA_API/$ep" "" "$m")"
  115. else
  116. response="$(_get "$VARIOMEDIA_API/$ep")"
  117. fi
  118. if [ "$?" != "0" ]; then
  119. _err "Error $ep"
  120. return 1
  121. fi
  122. _debug2 response "$response"
  123. return 0
  124. }