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.

141 lines
3.0 KiB

  1. #!/usr/bin/env sh
  2. #ACTIVE24_Token="sdfsdfsdfljlbjkljlkjsdfoiwje"
  3. ACTIVE24_Api="https://api.active24.com"
  4. ######## Public functions #####################
  5. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  6. # Used to add txt record
  7. dns_active24_add() {
  8. fulldomain=$1
  9. txtvalue=$2
  10. _active24_init
  11. _info "Adding txt record"
  12. if _active24_rest POST "dns/$_domain/txt/v1" "{\"name\":\"$_sub_domain\",\"text\":\"$txtvalue\",\"ttl\":0}"; then
  13. if _contains "$response" "errors"; then
  14. _err "Add txt record error."
  15. return 1
  16. else
  17. _info "Added, OK"
  18. return 0
  19. fi
  20. fi
  21. _err "Add txt record error."
  22. return 1
  23. }
  24. # Usage: fulldomain txtvalue
  25. # Used to remove the txt record after validation
  26. dns_active24_rm() {
  27. fulldomain=$1
  28. txtvalue=$2
  29. _active24_init
  30. _debug "Getting txt records"
  31. _active24_rest GET "dns/$_domain/records/v1"
  32. if _contains "$response" "errors"; then
  33. _err "Error"
  34. return 1
  35. fi
  36. hash_ids=$(echo "$response" | _egrep_o "[^{]+${txtvalue}[^}]+" | _egrep_o "hashId\":\"[^\"]+" | cut -c10-)
  37. for hash_id in $hash_ids; do
  38. _debug "Removing hash_id" "$hash_id"
  39. if _active24_rest DELETE "dns/$_domain/$hash_id/v1" ""; then
  40. if _contains "$response" "errors"; then
  41. _err "Unable to remove txt record."
  42. return 1
  43. else
  44. _info "Removed txt record."
  45. return 0
  46. fi
  47. fi
  48. done
  49. _err "No txt records found."
  50. return 1
  51. }
  52. #################### Private functions below ##################################
  53. #_acme-challenge.www.domain.com
  54. #returns
  55. # _sub_domain=_acme-challenge.www
  56. # _domain=domain.com
  57. # _domain_id=sdjkglgdfewsdfg
  58. _get_root() {
  59. domain=$1
  60. if ! _active24_rest GET "dns/domains/v1"; then
  61. return 1
  62. fi
  63. i=2
  64. p=1
  65. while true; do
  66. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  67. _debug "h" "$h"
  68. if [ -z "$h" ]; then
  69. #not valid
  70. return 1
  71. fi
  72. if _contains "$response" "\"$h\"" >/dev/null; then
  73. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  74. _domain=$h
  75. return 0
  76. fi
  77. p=$i
  78. i=$(_math "$i" + 1)
  79. done
  80. return 1
  81. }
  82. _active24_rest() {
  83. m=$1
  84. ep="$2"
  85. data="$3"
  86. _debug "$ep"
  87. export _H1="Authorization: Bearer $ACTIVE24_Token"
  88. if [ "$m" != "GET" ]; then
  89. _debug "data" "$data"
  90. response="$(_post "$data" "$ACTIVE24_Api/$ep" "" "$m" "application/json")"
  91. else
  92. response="$(_get "$ACTIVE24_Api/$ep")"
  93. fi
  94. if [ "$?" != "0" ]; then
  95. _err "error $ep"
  96. return 1
  97. fi
  98. _debug2 response "$response"
  99. return 0
  100. }
  101. _active24_init() {
  102. ACTIVE24_Token="${ACTIVE24_Token:-$(_readaccountconf_mutable ACTIVE24_Token)}"
  103. if [ -z "$ACTIVE24_Token" ]; then
  104. ACTIVE24_Token=""
  105. _err "You didn't specify a Active24 api token yet."
  106. _err "Please create the token and try again."
  107. return 1
  108. fi
  109. _saveaccountconf_mutable ACTIVE24_Token "$ACTIVE24_Token"
  110. _debug "First detect the root zone"
  111. if ! _get_root "$fulldomain"; then
  112. _err "invalid domain"
  113. return 1
  114. fi
  115. _debug _sub_domain "$_sub_domain"
  116. _debug _domain "$_domain"
  117. }