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.

166 lines
4.1 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. # _debug 'Getting txt records'
  26. # _variomedia_rest GET "/dns-records?filter[domain]=$_domain"
  27. # if printf "%s\n" "$response" | grep "\"record_type\": \"A\", \"fqdn\": \"$fulldomain\"" >/dev/null; then
  28. # _err 'Error'
  29. # return 1
  30. # fi
  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 | 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. fulldomain=$1
  84. i=1
  85. while true; do
  86. h=$(printf "%s" "$fulldomain" | cut -d . -f $i-100)
  87. _debug h "$h"
  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 _startswith "$response" "\{\"data\":"; then
  95. if _contains "$response" "\"id\": \"$h\""; then
  96. _sub_domain="$(echo "$fulldomain" | sed "s/\\.$h\$//")"
  97. _domain=$h
  98. return 0
  99. # else
  100. # _err 'Invalid domain'
  101. # return 1
  102. fi
  103. # else
  104. # _err "$response"
  105. # return 1
  106. fi
  107. i=$(_math "$i" + 1)
  108. done
  109. _debug "root domain not found"
  110. return 1
  111. }
  112. _variomedia_rest() {
  113. m=$1
  114. ep="$2"
  115. data="$3"
  116. _debug "$ep"
  117. # api_key_trimmed=$(echo $VARIOMEDIA_API_TOKEN | tr -d '"')
  118. # export _H1="Api-Key: $api_key_trimmed"
  119. export _H1="Authorization: token $VARIOMEDIA_API_TOKEN"
  120. export _H2="Content-Type: application/vnd.api+json"
  121. export _H3="Accept: application/vnd.variomedia.v1+json"
  122. if [ "$m" != "GET" ]; then
  123. _debug data "$data"
  124. response="$(_post "$data" "$VARIOMEDIA_API/$ep" "" "$m")"
  125. else
  126. response="$(_get "$VARIOMEDIA_API/$ep")"
  127. fi
  128. if [ "$?" != "0" ]; then
  129. _err "Error $ep"
  130. return 1
  131. fi
  132. _debug2 response "$response"
  133. return 0
  134. }