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
3.7 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 | 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. fulldomain=$1
  78. i=1
  79. while true; do
  80. h=$(printf "%s" "$fulldomain" | cut -d . -f $i-100)
  81. _debug h "$h"
  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 _startswith "$response" "\{\"data\":"; then
  89. if _contains "$response" "\"id\": \"$h\""; then
  90. _sub_domain="$(echo "$fulldomain" | sed "s/\\.$h\$//")"
  91. _domain=$h
  92. return 0
  93. fi
  94. fi
  95. i=$(_math "$i" + 1)
  96. done
  97. _debug "root domain not found"
  98. return 1
  99. }
  100. _variomedia_rest() {
  101. m=$1
  102. ep="$2"
  103. data="$3"
  104. _debug "$ep"
  105. export _H1="Authorization: token $VARIOMEDIA_API_TOKEN"
  106. export _H2="Content-Type: application/vnd.api+json"
  107. export _H3="Accept: application/vnd.variomedia.v1+json"
  108. if [ "$m" != "GET" ]; then
  109. _debug data "$data"
  110. response="$(_post "$data" "$VARIOMEDIA_API/$ep" "" "$m")"
  111. else
  112. response="$(_get "$VARIOMEDIA_API/$ep")"
  113. fi
  114. if [ "$?" != "0" ]; then
  115. _err "Error $ep"
  116. return 1
  117. fi
  118. _debug2 response "$response"
  119. return 0
  120. }