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.

149 lines
3.8 KiB

  1. #!/usr/bin/env sh
  2. # Zone.ee dns API
  3. # https://help.zone.eu/kb/zoneid-api-v2/
  4. # required ZONE_Username and ZONE_Key
  5. ZONE_Api="https://api.zone.eu/v2"
  6. ######## Public functions #####################
  7. #Usage: dns_zone_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  8. dns_zone_add() {
  9. fulldomain=$1
  10. txtvalue=$2
  11. _info "Using zone.ee dns api"
  12. _debug fulldomain "$fulldomain"
  13. _debug txtvalue "$txtvalue"
  14. ZONE_Username="${ZONE_Username:-$(_readaccountconf_mutable ZONE_Username)}"
  15. ZONE_Key="${ZONE_Key:-$(_readaccountconf_mutable ZONE_Key)}"
  16. if [ -z "$ZONE_Username" ] || [ -z "$ZONE_Key" ]; then
  17. ZONE_Username=""
  18. ZONE_Key=""
  19. _err "Zone api key and username must be present."
  20. return 1
  21. fi
  22. _saveaccountconf_mutable ZONE_Username "$ZONE_Username"
  23. _saveaccountconf_mutable ZONE_Key "$ZONE_Key"
  24. _debug "First detect the root zone"
  25. if ! _get_root "$fulldomain"; then
  26. _err "invalid domain"
  27. return 1
  28. fi
  29. _debug "Adding txt record"
  30. if _zone_rest POST "dns/${_domain}/txt" "{\"name\": \"$fulldomain\", \"destination\": \"$txtvalue\"}"; then
  31. if printf -- "%s" "$response" | grep "$fulldomain" >/dev/null; then
  32. _info "Added, OK"
  33. return 0
  34. else
  35. _err "Adding txt record error."
  36. return 1
  37. fi
  38. else
  39. _err "Adding txt record error."
  40. fi
  41. }
  42. #Usage: fulldomain txtvalue
  43. #Remove the txt record after validation.
  44. dns_zone_rm() {
  45. fulldomain=$1
  46. txtvalue=$2
  47. _info "Using zone.ee dns api"
  48. _debug fulldomain "$fulldomain"
  49. _debug txtvalue "$txtvalue"
  50. ZONE_Username="${ZONE_Username:-$(_readaccountconf_mutable ZONE_Username)}"
  51. ZONE_Key="${ZONE_Key:-$(_readaccountconf_mutable ZONE_Key)}"
  52. if [ -z "$ZONE_Username" ] || [ -z "$ZONE_Key" ]; then
  53. ZONE_Username=""
  54. ZONE_Key=""
  55. _err "Zone api key and username must be present."
  56. return 1
  57. fi
  58. _saveaccountconf_mutable ZONE_Username "$ZONE_Username"
  59. _saveaccountconf_mutable ZONE_Key "$ZONE_Key"
  60. _debug "First detect the root zone"
  61. if ! _get_root "$fulldomain"; then
  62. _err "invalid domain"
  63. return 1
  64. fi
  65. _debug "Getting txt records"
  66. _debug _domain "$_domain"
  67. _zone_rest GET "dns/${_domain}/txt"
  68. if printf "%s" "$response" | grep \"error\" >/dev/null; then
  69. _err "Error"
  70. return 1
  71. fi
  72. count=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$fulldomain\"" | wc -l)
  73. _debug count "$count"
  74. if [ "$count" = "0" ]; then
  75. _info "Nothing to remove."
  76. else
  77. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\",\"resource_url\":\"[^\"]*\",\"name\":\"$fulldomain\"," | cut -d : -f2 | cut -d , -f1 | tr -d \" | _head_n 1)
  78. if [ -z "$record_id" ]; then
  79. _err "No id found to remove."
  80. return 1
  81. fi
  82. if ! _zone_rest DELETE "dns/${_domain}/txt/$record_id"; then
  83. _err "Record deleting error."
  84. return 1
  85. fi
  86. _info "Record deleted"
  87. return 0
  88. fi
  89. }
  90. #################### Private functions below ##################################
  91. _zone_rest() {
  92. m=$1
  93. ep="$2"
  94. data="$3"
  95. _debug "$ep"
  96. realm="$(printf "%s" "$ZONE_Username:$ZONE_Key" | _base64)"
  97. export _H1="Authorization: Basic $realm"
  98. export _H2="Content-Type: application/json"
  99. if [ "$m" != "GET" ]; then
  100. _debug data "$data"
  101. response="$(_post "$data" "$ZONE_Api/$ep" "" "$m")"
  102. else
  103. response="$(_get "$ZONE_Api/$ep")"
  104. fi
  105. if [ "$?" != "0" ]; then
  106. _err "error $ep"
  107. return 1
  108. fi
  109. _debug2 response "$response"
  110. return 0
  111. }
  112. _get_root() {
  113. domain=$1
  114. i=2
  115. while true; do
  116. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  117. _debug h "$h"
  118. if [ -z "$h" ]; then
  119. return 1
  120. fi
  121. if ! _zone_rest GET "dns/$h"; then
  122. return 1
  123. fi
  124. if _contains "$response" "\"identificator\":\"$h\"" >/dev/null; then
  125. _domain=$h
  126. return 0
  127. fi
  128. i=$(_math "$i" + 1)
  129. done
  130. return 0
  131. }