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.

156 lines
3.3 KiB

  1. #!/usr/bin/env sh
  2. #ANX_Token="xxxx"
  3. ANX_API='https://engine.anexia-it.com/api/clouddns/v1'
  4. ######## Public functions #####################
  5. dns_anx_add() {
  6. fulldomain=$1
  7. txtvalue=$2
  8. _info "Using ANX CDNS API"
  9. ANX_Token="${ANX_Token:-$(_readaccountconf_mutable ANX_Token)}"
  10. _debug fulldomain "$fulldomain"
  11. _debug txtvalue "$txtvalue"
  12. if [ "$ANX_Token" ]; then
  13. _saveaccountconf_mutable ANX_Token "$ANX_Token"
  14. else
  15. _err "You didn't specify a ANEXIA Engine API token."
  16. return 1
  17. fi
  18. _debug "First detect the root zone"
  19. if ! _get_root "$fulldomain"; then
  20. _err "invalid domain"
  21. return 1
  22. fi
  23. # Always add records, wildcard need two records with the same name
  24. _anx_rest POST "zone.json/${_domain}/records" "{\"name\":\"$_sub_domain\",\"type\":\"TXT\",\"rdata\":\"$txtvalue\"}"
  25. if _contains "$response" "$txtvalue"; then
  26. return 0
  27. else
  28. return 1
  29. fi
  30. }
  31. dns_anx_rm() {
  32. fulldomain=$1
  33. txtvalue=$2
  34. _info "Using ANX CDNS API"
  35. ANX_Token="${ANX_Token:-$(_readaccountconf_mutable ANX_Token)}"
  36. _debug fulldomain "$fulldomain"
  37. _debug txtvalue "$txtvalue"
  38. _debug "First detect the root zone"
  39. if ! _get_root "$fulldomain"; then
  40. _err "invalid domain"
  41. return 1
  42. fi
  43. _get_record_id
  44. if _is_uuid "$_record_id"; then
  45. if ! _anx_rest DELETE "zone.json/${_domain}/records/$_record_id"; then
  46. _err "Delete record"
  47. return 1
  48. fi
  49. else
  50. _info "No record found."
  51. fi
  52. echo "$response" | tr -d " " | grep \"status\":\"OK\" >/dev/null
  53. }
  54. #################### Private functions below ##################################
  55. _is_uuid() {
  56. pattern='^\{?[A-Z0-9a-z]{8}-[A-Z0-9a-z]{4}-[A-Z0-9a-z]{4}-[A-Z0-9a-z]{4}-[A-Z0-9a-z]{12}\}?$'
  57. if echo "$1" | _egrep_o "$pattern" >/dev/null; then
  58. return 0
  59. fi
  60. return 1
  61. }
  62. _get_record_id() {
  63. _debug subdomain "$_sub_domain"
  64. _debug domain "$_domain"
  65. if _anx_rest GET "zone.json/${_domain}/records?name=$_sub_domain&type=TXT"; then
  66. _debug response "$response"
  67. if _contains "$response" "\"name\":\"$_sub_domain\"" >/dev/null; then
  68. _record_id=$(printf "%s\n" "$response" | _egrep_o "\[.\"identifier\":\"[^\"]*\"" | head -n 1 | cut -d : -f 2 | tr -d \")
  69. else
  70. _record_id=''
  71. fi
  72. else
  73. _err "Search existing record"
  74. fi
  75. }
  76. _anx_rest() {
  77. m=$1
  78. ep="$2"
  79. data="$3"
  80. _debug "$ep"
  81. export _H1="Content-Type: application/json"
  82. export _H2="Authorization: Token $ANX_Token"
  83. if [ "$m" != "GET" ]; then
  84. _debug data "$data"
  85. response="$(_post "$data" "${ANX_API}/$ep" "" "$m")"
  86. else
  87. response="$(_get "${ANX_API}/$ep")"
  88. fi
  89. # shellcheck disable=SC2181
  90. if [ "$?" != "0" ]; then
  91. _err "error $ep"
  92. return 1
  93. fi
  94. _debug response "$response"
  95. return 0
  96. }
  97. #_acme-challenge.www.domain.com
  98. #returns
  99. # _sub_domain=_acme-challenge.www
  100. # _domain=domain.com
  101. _get_root() {
  102. domain=$1
  103. i=1
  104. p=1
  105. while true; do
  106. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  107. _debug h "$h"
  108. if [ -z "$h" ]; then
  109. #not valid
  110. return 1
  111. fi
  112. # Does a zone with that name exist?
  113. _anx_rest GET "zone.json/$h"
  114. # shellcheck disable=SC2154
  115. if [ "$code" -ne 200 ]; then
  116. continue
  117. fi
  118. if _contains "$response" "\"name\":\"$h\""; then
  119. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  120. _domain=$h
  121. return 0
  122. fi
  123. p=$i
  124. i=$(_math "$i" + 1)
  125. done
  126. return 1
  127. }