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.

208 lines
5.4 KiB

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