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.

137 lines
3.7 KiB

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