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.

139 lines
3.1 KiB

7 years ago
7 years ago
  1. #!/usr/bin/env sh
  2. Zilore_API="https://api.zilore.com/dns/v1"
  3. # Zilore_Key="YOUR-ZILORE-API-KEY"
  4. ######## Public functions #####################
  5. dns_zilore_add() {
  6. fulldomain=$1
  7. txtvalue=$2
  8. _info "Using Zilore"
  9. _debug fulldomain "$fulldomain"
  10. _debug txtvalue "$txtvalue"
  11. Zilore_Key="${Zilore_Key:-$(_readaccountconf_mutable Zilore_Key)}"
  12. if [ -z "$Zilore_Key" ]; then
  13. Zilore_Key=""
  14. _err "Please define Zilore API key"
  15. return 1
  16. fi
  17. _saveaccountconf_mutable Zilore_Key "$Zilore_Key"
  18. if ! _get_root "$fulldomain"; then
  19. _err "Unable to determine root domain"
  20. return 1
  21. else
  22. _debug _domain "$_domain"
  23. fi
  24. if _zilore_rest POST "domains/$_domain/records?record_type=TXT&record_ttl=600&record_name=$fulldomain&record_value=\"$txtvalue\""; then
  25. if _contains "$response" '"added"' >/dev/null; then
  26. _info "Added TXT record, waiting for validation"
  27. return 0
  28. else
  29. _debug response "$response"
  30. _err "Error while adding DNS records"
  31. return 1
  32. fi
  33. fi
  34. return 1
  35. }
  36. dns_zilore_rm() {
  37. fulldomain=$1
  38. txtvalue=$2
  39. _info "Using Zilore"
  40. _debug fulldomain "$fulldomain"
  41. _debug txtvalue "$txtvalue"
  42. Zilore_Key="${Zilore_Key:-$(_readaccountconf_mutable Zilore_Key)}"
  43. if [ -z "$Zilore_Key" ]; then
  44. Zilore_Key=""
  45. _err "Please define Zilore API key"
  46. return 1
  47. fi
  48. _saveaccountconf_mutable Zilore_Key "$Zilore_Key"
  49. if ! _get_root "$fulldomain"; then
  50. _err "Unable to determine root domain"
  51. return 1
  52. else
  53. _debug _domain "$_domain"
  54. fi
  55. _debug "Getting TXT records"
  56. _zilore_rest GET "domains/${_domain}/records?search_text=$txtvalue&search_record_type=TXT"
  57. _debug response "$response"
  58. if ! _contains "$response" '"ok"' >/dev/null; then
  59. _err "Error while getting records list"
  60. return 1
  61. else
  62. _record_id=$(printf "%s\n" "$response" | _egrep_o "\"record_id\":\"[^\"]+\"" | cut -d : -f 2 | tr -d \" | _head_n 1)
  63. if [ -z "$_record_id" ]; then
  64. _err "Cannot determine _record_id"
  65. return 1
  66. else
  67. _debug _record_id "$_record_id"
  68. fi
  69. if ! _zilore_rest DELETE "domains/${_domain}/records?record_id=$_record_id"; then
  70. _err "Error while deleting chosen record"
  71. return 1
  72. fi
  73. _contains "$response" '"ok"'
  74. fi
  75. }
  76. #################### Private functions below ##################################
  77. _get_root() {
  78. domain=$1
  79. i=2
  80. while true; do
  81. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  82. _debug h "$h"
  83. if [ -z "$h" ]; then
  84. #not valid
  85. return 1
  86. fi
  87. if ! _zilore_rest GET "domains?search_text=$h"; then
  88. return 1
  89. fi
  90. if _contains "$response" "\"$h\"" >/dev/null; then
  91. _domain=$h
  92. return 0
  93. else
  94. _debug "$h not found"
  95. fi
  96. i=$(_math "$i" + 1)
  97. done
  98. return 1
  99. }
  100. _zilore_rest() {
  101. method=$1
  102. param=$2
  103. data=$3
  104. export _H1="X-Auth-Key: $Zilore_Key"
  105. if [ "$method" != "GET" ]; then
  106. response="$(_post "$data" "$Zilore_API/$param" "" "$method")"
  107. else
  108. response="$(_get "$Zilore_API/$param")"
  109. fi
  110. if [ "$?" != "0" ]; then
  111. _err "error $param"
  112. return 1
  113. fi
  114. _debug2 response "$response"
  115. return 0
  116. }