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.

144 lines
3.6 KiB

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