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.

209 lines
5.5 KiB

  1. #!/usr/bin/env sh
  2. #This is the websupport.sk api wrapper for acme.sh
  3. #
  4. #Author: trgo.sk
  5. #Report Bugs here: https://github.com/trgosk/acme.sh
  6. #WS_ApiKey="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  7. #WS_ApiSecret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  8. WS_Api="https://rest.websupport.sk"
  9. ######## Public functions #####################
  10. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  11. dns_websupport_add() {
  12. fulldomain=$1
  13. txtvalue=$2
  14. WS_ApiKey="${WS_ApiKey:-$(_readaccountconf_mutable WS_ApiKey)}"
  15. WS_ApiSecret="${WS_ApiSecret:-$(_readaccountconf_mutable WS_ApiSecret)}"
  16. if [ "$WS_ApiKey" ] && [ "$WS_ApiSecret" ]; then
  17. _saveaccountconf_mutable WS_ApiKey "$WS_ApiKey"
  18. _saveaccountconf_mutable WS_ApiSecret "$WS_ApiSecret"
  19. else
  20. WS_ApiKey=""
  21. WS_ApiSecret=""
  22. _err "You didn't specify a api key and/or api secret yet."
  23. _err "You can get yours from here https://admin.websupport.sk/en/auth/apiKey"
  24. return 1
  25. fi
  26. _debug "First detect the root zone"
  27. if ! _get_root "$fulldomain"; then
  28. _err "invalid domain"
  29. return 1
  30. fi
  31. _debug _sub_domain "$_sub_domain"
  32. _debug _domain "$_domain"
  33. # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
  34. # we can not use updating anymore.
  35. # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
  36. # _debug count "$count"
  37. # if [ "$count" = "0" ]; then
  38. _info "Adding record"
  39. if _ws_rest POST "/v1/user/self/zone/$_domain/record" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  40. if _contains "$response" "$txtvalue"; then
  41. _info "Added, OK"
  42. return 0
  43. elif _contains "$response" "The record already exists"; then
  44. _info "Already exists, OK"
  45. return 0
  46. else
  47. _err "Add txt record error."
  48. return 1
  49. fi
  50. fi
  51. _err "Add txt record error."
  52. return 1
  53. }
  54. #fulldomain txtvalue
  55. dns_websupport_rm() {
  56. fulldomain=$1
  57. txtvalue=$2
  58. _debug2 fulldomain "$fulldomain"
  59. _debug2 txtvalue "$txtvalue"
  60. _debug "First detect the root zone"
  61. if ! _get_root "$fulldomain"; then
  62. _err "invalid domain"
  63. return 1
  64. fi
  65. _debug _sub_domain "$_sub_domain"
  66. _debug _domain "$_domain"
  67. _debug "Getting txt records"
  68. _ws_rest GET "/v1/user/self/zone/$_domain/record"
  69. if [ "$(printf "%s" "$response" | tr -d " " | grep -c \"items\")" -lt "1" ]; then
  70. _err "Error: $response"
  71. return 1
  72. fi
  73. record_line="$(_get_from_array "$response" "$txtvalue")"
  74. _debug record_line "$record_line"
  75. if [ -z "$record_line" ]; then
  76. _info "Don't need to remove."
  77. else
  78. record_id=$(echo "$record_line" | _egrep_o "\"id\": *[^,]*" | _head_n 1 | cut -d : -f 2 | tr -d \" | tr -d " ")
  79. _debug "record_id" "$record_id"
  80. if [ -z "$record_id" ]; then
  81. _err "Can not get record id to remove."
  82. return 1
  83. fi
  84. if ! _ws_rest DELETE "/v1/user/self/zone/$_domain/record/$record_id"; then
  85. _err "Delete record error."
  86. return 1
  87. fi
  88. if [ "$(printf "%s" "$response" | tr -d " " | grep -c \"success\")" -lt "1" ]; then
  89. return 1
  90. else
  91. return 0
  92. fi
  93. fi
  94. }
  95. #################### Private functions below ##################################
  96. #_acme-challenge.www.domain.com
  97. #returns
  98. # _sub_domain=_acme-challenge.www
  99. # _domain=domain.com
  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="$(date +%s)"
  135. datez=$(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)
  136. canonical_request="${me} ${pa} ${timestamp}"
  137. alg="sha1"
  138. signature_hash=$( (printf "%s" "$canonical_request" | ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -mac HMAC -macopt "key:$WS_ApiSecret" 2>/dev/null || printf "%s" "$canonical_request" | ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hmac "$(printf "%s" "$WS_ApiSecret" | _h2b)") | cut -d = -f 2 | tr -d ' ')
  139. basicauth="$(printf "%s:%s" "$WS_ApiKey" "$signature_hash" | _base64)"
  140. _debug2 method "$me"
  141. _debug2 path "$pa"
  142. _debug2 data "$da"
  143. _debug2 timestamp "$timestamp"
  144. _debug2 datez "$datez"
  145. _debug2 canonical_request "$canonical_request"
  146. _debug2 alg "$alg"
  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. }