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.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env sh
  2. # Author: Wout Decre <wout@canodus.be>
  3. CONSTELLIX_Api="https://api.dns.constellix.com/v1"
  4. #CONSTELLIX_Key="XXX"
  5. #CONSTELLIX_Secret="XXX"
  6. ######## Public functions #####################
  7. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  8. # Used to add txt record
  9. dns_constellix_add() {
  10. fulldomain=$1
  11. txtvalue=$2
  12. CONSTELLIX_Key="${CONSTELLIX_Key:-$(_readaccountconf_mutable CONSTELLIX_Key)}"
  13. CONSTELLIX_Secret="${CONSTELLIX_Secret:-$(_readaccountconf_mutable CONSTELLIX_Secret)}"
  14. if [ -z "$CONSTELLIX_Key" ] || [ -z "$CONSTELLIX_Secret" ]; then
  15. _err "You did not specify the Contellix API key and secret yet."
  16. return 1
  17. fi
  18. _saveaccountconf_mutable CONSTELLIX_Key "$CONSTELLIX_Key"
  19. _saveaccountconf_mutable CONSTELLIX_Secret "$CONSTELLIX_Secret"
  20. if ! _get_root "$fulldomain"; then
  21. _err "Invalid domain"
  22. return 1
  23. fi
  24. _info "Adding TXT record"
  25. if _constellix_rest POST "domains/${_domain_id}/records" "[{\"type\":\"txt\",\"add\":true,\"set\":{\"name\":\"${_sub_domain}\",\"ttl\":120,\"roundRobin\":[{\"value\":\"${txtvalue}\"}]}}]"; then
  26. if printf -- "%s" "$response" | grep "{\"success\":\"1 record(s) added, 0 record(s) updated, 0 record(s) deleted\"}" >/dev/null; then
  27. _info "Added"
  28. return 0
  29. else
  30. _err "Error adding TXT record"
  31. return 1
  32. fi
  33. fi
  34. }
  35. # Usage: fulldomain txtvalue
  36. # Used to remove the txt record after validation
  37. dns_constellix_rm() {
  38. fulldomain=$1
  39. txtvalue=$2
  40. CONSTELLIX_Key="${CONSTELLIX_Key:-$(_readaccountconf_mutable CONSTELLIX_Key)}"
  41. CONSTELLIX_Secret="${CONSTELLIX_Secret:-$(_readaccountconf_mutable CONSTELLIX_Secret)}"
  42. if [ -z "$CONSTELLIX_Key" ] || [ -z "$CONSTELLIX_Secret" ]; then
  43. _err "You did not specify the Contellix API key and secret yet."
  44. return 1
  45. fi
  46. if ! _get_root "$fulldomain"; then
  47. _err "Invalid domain"
  48. return 1
  49. fi
  50. _info "Removing TXT record"
  51. if _constellix_rest POST "domains/${_domain_id}/records" "[{\"type\":\"txt\",\"delete\":true,\"filter\":{\"field\":\"name\",\"op\":\"eq\",\"value\":\"${_sub_domain}\"}}]"; then
  52. if printf -- "%s" "$response" | grep "{\"success\":\"0 record(s) added, 0 record(s) updated, 1 record(s) deleted\"}" >/dev/null; then
  53. _info "Removed"
  54. return 0
  55. else
  56. _err "Error removing TXT record"
  57. return 1
  58. fi
  59. fi
  60. }
  61. #################### Private functions below ##################################
  62. _get_root() {
  63. domain=$1
  64. i=2
  65. p=1
  66. _debug "Detecting root zone"
  67. while true; do
  68. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  69. if [ -z "$h" ]; then
  70. return 1
  71. fi
  72. if ! _constellix_rest GET "domains/search?exact=$h"; then
  73. return 1
  74. fi
  75. if _contains "$response" "\"name\":\"$h\""; then
  76. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[0-9]+" | cut -d ':' -f 2)
  77. if [ "$_domain_id" ]; then
  78. _sub_domain=$(printf "%s" "$domain" | cut -d '.' -f 1-$p)
  79. _domain="$h"
  80. _debug _domain_id "$_domain_id"
  81. _debug _sub_domain "$_sub_domain"
  82. _debug _domain "$_domain"
  83. return 0
  84. fi
  85. return 1
  86. fi
  87. p=$i
  88. i=$(_math "$i" + 1)
  89. done
  90. return 1
  91. }
  92. _constellix_rest() {
  93. m=$1
  94. ep="$2"
  95. data="$3"
  96. _debug "$ep"
  97. rdate=$(date +"%s")"000"
  98. hmac=$(printf "%s" "$rdate" | _hmac sha1 "$(printf "%s" "$CONSTELLIX_Secret" | _hex_dump | tr -d ' ')" | _base64)
  99. export _H1="x-cnsdns-apiKey: $CONSTELLIX_Key"
  100. export _H2="x-cnsdns-requestDate: $rdate"
  101. export _H3="x-cnsdns-hmac: $hmac"
  102. export _H4="Accept: application/json"
  103. export _H5="Content-Type: application/json"
  104. if [ "$m" != "GET" ]; then
  105. _debug data "$data"
  106. response="$(_post "$data" "$CONSTELLIX_Api/$ep" "" "$m")"
  107. else
  108. response="$(_get "$CONSTELLIX_Api/$ep")"
  109. fi
  110. if [ "$?" != "0" ]; then
  111. _err "Error $ep"
  112. return 1
  113. fi
  114. _debug response "$response"
  115. return 0
  116. }