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.

198 lines
4.8 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_hosttech_info='hosttech.eu
  4. Site: hosttech.eu
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_hosttech
  6. Options:
  7. Hosttech_Key API Key
  8. Issues: github.com/acmesh-official/acme.sh/issues/4900
  9. '
  10. #Hosttech_Key="asdfasdfawefasdfawefasdafe"
  11. Hosttech_Api="https://api.ns1.hosttech.eu/api/user/v1"
  12. ######## Public functions #####################
  13. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_hosttech_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. Hosttech_Key="${Hosttech_Key:-$(_readaccountconf_mutable Hosttech_Key)}"
  18. if [ -z "$Hosttech_Key" ]; then
  19. Hosttech_Key=""
  20. _err "You didn't specify a Hosttech api key"
  21. _err "You can get yours from https://www.myhosttech.eu/user/dns/api"
  22. return 1
  23. fi
  24. _debug "First detect the root zone"
  25. if ! _get_root "$fulldomain"; then
  26. _err "invalid domain"
  27. return 1
  28. fi
  29. _debug _sub_domain "$_sub_domain"
  30. _debug _domain "$_domain"
  31. _info "Adding record"
  32. if _hosttech_rest POST "zones/$_domain/records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"text\":\"$txtvalue\",\"ttl\":600}"; then
  33. if _contains "$_response" "$_sub_domain"; then
  34. _info "Added, OK"
  35. return 0
  36. else
  37. _err "Add txt record error."
  38. return 1
  39. fi
  40. fi
  41. _err "Add txt record error."
  42. return 1
  43. }
  44. #fulldomain txtvalue
  45. dns_hosttech_rm() {
  46. fulldomain=$1
  47. txtvalue=$2
  48. Hosttech_Key="${Hosttech_Key:-$(_readaccountconf_mutable Hosttech_Key)}"
  49. if [ -z "$Hosttech_Key" ]; then
  50. Hosttech_Key=""
  51. _err "You didn't specify a Hosttech api key."
  52. _err "You can get yours from You can get yours from https://www.myhosttech.eu/user/dns/api"
  53. return 1
  54. fi
  55. _debug "First detect the zoneid"
  56. if ! _get_zoneid "$fulldomain"; then
  57. _err "invalid zoneid"
  58. return 1
  59. fi
  60. _debug _sub_domain "$_sub_domain"
  61. _debug _domain "$_domain"
  62. _debug _zoneid "$_zoneid"
  63. _debug _txtvalue "${txtvalue}"
  64. _debug "Second detect the recordid"
  65. if ! _get_recordid "$_domain" "$_sub_domain" "${txtvalue}"; then
  66. _err "invalid recordid"
  67. return 1
  68. fi
  69. _debug _recordid "$_recordid"
  70. _debug "Removing txt record"
  71. _hosttech_rest DELETE "zones/$_domain/records/$_recordid"
  72. }
  73. #################### Private functions below ##################################
  74. #_acme-challenge.www.domain.com
  75. #returns
  76. # _sub_domain=_acme-challenge.www
  77. # _domain=domain.com
  78. _get_root() {
  79. domain=$1
  80. i=1
  81. p=1
  82. while true; do
  83. _domain=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  84. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  85. _debug _domain "$_domain"
  86. if [ -z "$_domain" ]; then
  87. #not valid
  88. return 1
  89. fi
  90. if _hosttech_rest GET "zones?query=${_domain}"; then
  91. if [ "$(echo "$_response" | _egrep_o '"name":"[^"]*' | cut -d'"' -f4)" = "${_domain}" ]; then
  92. return 0
  93. fi
  94. else
  95. return 1
  96. fi
  97. p=$i
  98. i=$(_math "$i" + 1)
  99. done
  100. return 1
  101. }
  102. _get_zoneid() {
  103. domain=$1
  104. i=1
  105. p=1
  106. while true; do
  107. _domain=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  108. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  109. _debug _domain "$_domain"
  110. if [ -z "$_domain" ]; then
  111. #not valid
  112. return 1
  113. fi
  114. if _hosttech_rest GET "zones?query=${_domain}"; then
  115. if [ "$(echo "$_response" | _egrep_o '"name":"[^"]*' | cut -d'"' -f4)" = "${_domain}" ]; then
  116. # Get the id of the zone in question
  117. _zoneid="$(echo "$_response" | _egrep_o '"id":[0-9]*' | cut -d':' -f2)"
  118. if [ -z "$_zoneid" ]; then
  119. return 1
  120. fi
  121. return 0
  122. fi
  123. else
  124. return 1
  125. fi
  126. p=$i
  127. i=$(_math "$i" + 1)
  128. done
  129. return 1
  130. }
  131. _get_recordid() {
  132. domainid=$1
  133. subdomain=$2
  134. txtvalue=$3
  135. # Get all dns records for the domainname
  136. if _hosttech_rest GET "zones/$_zoneid/records"; then
  137. if ! _contains "$_response" '"id"'; then
  138. _debug "No records in dns"
  139. return 1
  140. fi
  141. if ! _contains "$_response" '\"name\":\"'"$subdomain"'\"'; then
  142. _debug "Record does not exist"
  143. return 1
  144. fi
  145. # Get the id of the record in question
  146. _recordid=$(printf "%s" "$_response" | _egrep_o "[^{]*\"name\":\"$subdomain\"[^}]*" | _egrep_o "[^{]*\"text\":\"$txtvalue\"[^}]*" | _egrep_o "\"id\":[0-9]+" | cut -d : -f 2)
  147. if [ -z "$_recordid" ]; then
  148. return 1
  149. fi
  150. return 0
  151. fi
  152. return 0
  153. }
  154. _hosttech_rest() {
  155. m=$1
  156. ep="$2"
  157. data="$3"
  158. _debug "$ep"
  159. export _H1="Authorization: Bearer $Hosttech_Key"
  160. export _H2="accept: application/json"
  161. export _H3="Content-Type: application/json"
  162. _debug data "$data"
  163. _response="$(_post "$data" "$Hosttech_Api/$ep" "" "$m")"
  164. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  165. _debug "http response code $_code"
  166. if [ "$?" != "0" ]; then
  167. _err "error $ep"
  168. return 1
  169. fi
  170. _debug2 response "$_response"
  171. return 0
  172. }