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.

207 lines
5.3 KiB

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