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.

205 lines
5.3 KiB

1 month ago
1 month ago
4 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_websupport_info='Websupport.sk
  4. Site: Websupport.sk
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_websupport
  6. Options:
  7. WS_ApiKey API Key. Called "Identifier" in the WS Admin
  8. WS_ApiSecret API Secret. Called "Secret key" in the WS Admin
  9. Issues: github.com/acmesh-official/acme.sh/issues/3486
  10. Author: trgo.sk <https://github.com/trgosk>, akulumbeg <https://github.com/akulumbeg>
  11. '
  12. # Requirements: API Key and Secret from https://admin.websupport.sk/en/auth/apiKey
  13. WS_Api="https://rest.websupport.sk"
  14. ######## Public functions #####################
  15. dns_websupport_add() {
  16. fulldomain=$1
  17. txtvalue=$2
  18. WS_ApiKey="${WS_ApiKey:-$(_readaccountconf_mutable WS_ApiKey)}"
  19. WS_ApiSecret="${WS_ApiSecret:-$(_readaccountconf_mutable WS_ApiSecret)}"
  20. if [ "$WS_ApiKey" ] && [ "$WS_ApiSecret" ]; then
  21. _saveaccountconf_mutable WS_ApiKey "$WS_ApiKey"
  22. _saveaccountconf_mutable WS_ApiSecret "$WS_ApiSecret"
  23. else
  24. WS_ApiKey=""
  25. WS_ApiSecret=""
  26. _err "You did not specify the API Key and/or API Secret"
  27. _err "You can get the API login credentials from https://admin.websupport.sk/en/auth/apiKey"
  28. return 1
  29. fi
  30. _debug "First detect the root zone"
  31. if ! _get_root "$fulldomain"; then
  32. _err "invalid domain"
  33. return 1
  34. fi
  35. _debug _sub_domain "$_sub_domain"
  36. _debug _domain "$_domain"
  37. # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
  38. # we can not use updating anymore.
  39. # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
  40. # _debug count "$count"
  41. # if [ "$count" = "0" ]; then
  42. _info "Adding record"
  43. if _ws_rest POST "/v1/user/self/zone/$_domain/record" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  44. if _contains "$response" "$txtvalue"; then
  45. _info "Added, OK"
  46. return 0
  47. elif _contains "$response" "The record already exists"; then
  48. _info "Already exists, OK"
  49. return 0
  50. else
  51. _err "Add txt record error."
  52. return 1
  53. fi
  54. fi
  55. _err "Add txt record error."
  56. return 1
  57. }
  58. dns_websupport_rm() {
  59. fulldomain=$1
  60. txtvalue=$2
  61. _debug2 fulldomain "$fulldomain"
  62. _debug2 txtvalue "$txtvalue"
  63. _debug "First detect the root zone"
  64. if ! _get_root "$fulldomain"; then
  65. _err "invalid domain"
  66. return 1
  67. fi
  68. _debug _sub_domain "$_sub_domain"
  69. _debug _domain "$_domain"
  70. _debug "Getting txt records"
  71. _ws_rest GET "/v1/user/self/zone/$_domain/record"
  72. if [ "$(printf "%s" "$response" | tr -d " " | grep -c \"items\")" -lt "1" ]; then
  73. _err "Error: $response"
  74. return 1
  75. fi
  76. record_line="$(_get_from_array "$response" "$txtvalue")"
  77. _debug record_line "$record_line"
  78. if [ -z "$record_line" ]; then
  79. _info "Don't need to remove."
  80. else
  81. record_id=$(echo "$record_line" | _egrep_o "\"id\": *[^,]*" | _head_n 1 | cut -d : -f 2 | tr -d \" | tr -d " ")
  82. _debug "record_id" "$record_id"
  83. if [ -z "$record_id" ]; then
  84. _err "Can not get record id to remove."
  85. return 1
  86. fi
  87. if ! _ws_rest DELETE "/v1/user/self/zone/$_domain/record/$record_id"; then
  88. _err "Delete record error."
  89. return 1
  90. fi
  91. if [ "$(printf "%s" "$response" | tr -d " " | grep -c \"success\")" -lt "1" ]; then
  92. return 1
  93. else
  94. return 0
  95. fi
  96. fi
  97. }
  98. #################### Private Functions ##################################
  99. _get_root() {
  100. domain=$1
  101. i=1
  102. p=1
  103. while true; do
  104. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  105. _debug h "$h"
  106. if [ -z "$h" ]; then
  107. #not valid
  108. return 1
  109. fi
  110. if ! _ws_rest GET "/v1/user/self/zone"; then
  111. return 1
  112. fi
  113. if _contains "$response" "\"name\":\"$h\""; then
  114. _domain_id=$(echo "$response" | _egrep_o "\[.\"id\": *[^,]*" | _head_n 1 | cut -d : -f 2 | tr -d \" | tr -d " ")
  115. if [ "$_domain_id" ]; then
  116. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  117. _domain=$h
  118. return 0
  119. fi
  120. return 1
  121. fi
  122. p=$i
  123. i=$(_math "$i" + 1)
  124. done
  125. return 1
  126. }
  127. _ws_rest() {
  128. me=$1
  129. pa="$2"
  130. da="$3"
  131. _debug2 api_key "$WS_ApiKey"
  132. _debug2 api_secret "$WS_ApiSecret"
  133. timestamp=$(_time)
  134. datez="$(_utc_date | sed "s/ /T/" | sed "s/$/+0000/")"
  135. canonical_request="${me} ${pa} ${timestamp}"
  136. signature_hash=$(printf "%s" "$canonical_request" | _hmac sha1 "$(printf "%s" "$WS_ApiSecret" | _hex_dump | tr -d " ")" hex)
  137. basicauth="$(printf "%s:%s" "$WS_ApiKey" "$signature_hash" | _base64)"
  138. _debug2 method "$me"
  139. _debug2 path "$pa"
  140. _debug2 data "$da"
  141. _debug2 timestamp "$timestamp"
  142. _debug2 datez "$datez"
  143. _debug2 canonical_request "$canonical_request"
  144. _debug2 signature_hash "$signature_hash"
  145. _debug2 basicauth "$basicauth"
  146. export _H1="Accept: application/json"
  147. export _H2="Content-Type: application/json"
  148. export _H3="Authorization: Basic ${basicauth}"
  149. export _H4="Date: ${datez}"
  150. _debug2 H1 "$_H1"
  151. _debug2 H2 "$_H2"
  152. _debug2 H3 "$_H3"
  153. _debug2 H4 "$_H4"
  154. if [ "$me" != "GET" ]; then
  155. _debug2 "${me} $WS_Api${pa}"
  156. _debug data "$da"
  157. response="$(_post "$da" "${WS_Api}${pa}" "" "$me")"
  158. else
  159. _debug2 "GET $WS_Api${pa}"
  160. response="$(_get "$WS_Api${pa}")"
  161. fi
  162. _debug2 response "$response"
  163. return "$?"
  164. }
  165. _get_from_array() {
  166. va="$1"
  167. fi="$2"
  168. for i in $(echo "$va" | sed "s/{/ /g"); do
  169. if _contains "$i" "$fi"; then
  170. echo "$i"
  171. break
  172. fi
  173. done
  174. }