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.6 KiB

1 month ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_nw_info='Nexcess.net (NocWorx)
  4. Domains: Thermo.io Futurehosting.com
  5. Site: Nexcess.net
  6. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_nw
  7. Options:
  8. NW_API_TOKEN API Token
  9. NW_API_ENDPOINT API Endpoint. Default: "https://portal.nexcess.net".
  10. Issues: github.com/acmesh-official/acme.sh/issues/2088
  11. Author: Frank Laszlo <flaszlo@nexcess.net>
  12. '
  13. # Endpoints:
  14. # - https://portal.nexcess.net (default)
  15. # - https://core.thermo.io
  16. # - https://my.futurehosting.com
  17. #
  18. # Note: If you do not have an API token, one can be generated at one
  19. # of the following URLs:
  20. # - https://portal.nexcess.net/api-token
  21. # - https://core.thermo.io/api-token
  22. # - https://my.futurehosting.com/api-token
  23. NW_API_VERSION="0"
  24. # dns_nw_add() - Add TXT record
  25. # Usage: dns_nw_add _acme-challenge.subdomain.domain.com "XyZ123..."
  26. dns_nw_add() {
  27. host="${1}"
  28. txtvalue="${2}"
  29. _debug host "${host}"
  30. _debug txtvalue "${txtvalue}"
  31. if ! _check_nw_api_creds; then
  32. return 1
  33. fi
  34. _info "Using NocWorx (${NW_API_ENDPOINT})"
  35. _debug "Calling: dns_nw_add() '${host}' '${txtvalue}'"
  36. _debug "Detecting root zone"
  37. if ! _get_root "${host}"; then
  38. _err "Zone for domain does not exist."
  39. return 1
  40. fi
  41. _debug _zone_id "${_zone_id}"
  42. _debug _sub_domain "${_sub_domain}"
  43. _debug _domain "${_domain}"
  44. _post_data="{\"zone_id\": \"${_zone_id}\", \"type\": \"TXT\", \"host\": \"${host}\", \"target\": \"${txtvalue}\", \"ttl\": \"300\"}"
  45. if _rest POST "dns-record" "${_post_data}" && [ -n "${response}" ]; then
  46. _record_id=$(printf "%s\n" "${response}" | _egrep_o "\"record_id\": *[0-9]+" | cut -d : -f 2 | tr -d " " | _head_n 1)
  47. _debug _record_id "${_record_id}"
  48. if [ -z "$_record_id" ]; then
  49. _err "Error adding the TXT record."
  50. return 1
  51. fi
  52. _info "TXT record successfully added."
  53. return 0
  54. fi
  55. return 1
  56. }
  57. # dns_nw_rm() - Remove TXT record
  58. # Usage: dns_nw_rm _acme-challenge.subdomain.domain.com "XyZ123..."
  59. dns_nw_rm() {
  60. host="${1}"
  61. txtvalue="${2}"
  62. _debug host "${host}"
  63. _debug txtvalue "${txtvalue}"
  64. if ! _check_nw_api_creds; then
  65. return 1
  66. fi
  67. _info "Using NocWorx (${NW_API_ENDPOINT})"
  68. _debug "Calling: dns_nw_rm() '${host}'"
  69. _debug "Detecting root zone"
  70. if ! _get_root "${host}"; then
  71. _err "Zone for domain does not exist."
  72. return 1
  73. fi
  74. _debug _zone_id "${_zone_id}"
  75. _debug _sub_domain "${_sub_domain}"
  76. _debug _domain "${_domain}"
  77. _parameters="?zone_id=${_zone_id}"
  78. if _rest GET "dns-record" "${_parameters}" && [ -n "${response}" ]; then
  79. response="$(echo "${response}" | tr -d "\n" | sed 's/^\[\(.*\)\]$/\1/' | sed -e 's/{"record_id":/|"record_id":/g' | sed 's/|/&{/g' | tr "|" "\n")"
  80. _debug response "${response}"
  81. record="$(echo "${response}" | _egrep_o "{.*\"host\": *\"${_sub_domain}\", *\"target\": *\"${txtvalue}\".*}")"
  82. _debug record "${record}"
  83. if [ "${record}" ]; then
  84. _record_id=$(printf "%s\n" "${record}" | _egrep_o "\"record_id\": *[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
  85. if [ "${_record_id}" ]; then
  86. _debug _record_id "${_record_id}"
  87. _rest DELETE "dns-record/${_record_id}"
  88. _info "TXT record successfully deleted."
  89. return 0
  90. fi
  91. return 1
  92. fi
  93. return 0
  94. fi
  95. return 1
  96. }
  97. _check_nw_api_creds() {
  98. NW_API_TOKEN="${NW_API_TOKEN:-$(_readaccountconf_mutable NW_API_TOKEN)}"
  99. NW_API_ENDPOINT="${NW_API_ENDPOINT:-$(_readaccountconf_mutable NW_API_ENDPOINT)}"
  100. if [ -z "${NW_API_ENDPOINT}" ]; then
  101. NW_API_ENDPOINT="https://portal.nexcess.net"
  102. fi
  103. if [ -z "${NW_API_TOKEN}" ]; then
  104. _err "You have not defined your NW_API_TOKEN."
  105. _err "Please create your token and try again."
  106. _err "If you need to generate a new token, please visit one of the following URLs:"
  107. _err " - https://portal.nexcess.net/api-token"
  108. _err " - https://core.thermo.io/api-token"
  109. _err " - https://my.futurehosting.com/api-token"
  110. return 1
  111. fi
  112. _saveaccountconf_mutable NW_API_TOKEN "${NW_API_TOKEN}"
  113. _saveaccountconf_mutable NW_API_ENDPOINT "${NW_API_ENDPOINT}"
  114. }
  115. _get_root() {
  116. domain="${1}"
  117. i=2
  118. p=1
  119. if _rest GET "dns-zone"; then
  120. response="$(echo "${response}" | tr -d "\n" | sed 's/^\[\(.*\)\]$/\1/' | sed -e 's/{"zone_id":/|"zone_id":/g' | sed 's/|/&{/g' | tr "|" "\n")"
  121. _debug response "${response}"
  122. while true; do
  123. h=$(printf "%s" "${domain}" | cut -d . -f "$i"-100)
  124. _debug h "${h}"
  125. if [ -z "${h}" ]; then
  126. #not valid
  127. return 1
  128. fi
  129. hostedzone="$(echo "${response}" | _egrep_o "{.*\"domain\": *\"${h}\".*}")"
  130. if [ "${hostedzone}" ]; then
  131. _zone_id=$(printf "%s\n" "${hostedzone}" | _egrep_o "\"zone_id\": *[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
  132. if [ "${_zone_id}" ]; then
  133. _sub_domain=$(printf "%s" "${domain}" | cut -d . -f 1-${p})
  134. _domain="${h}"
  135. return 0
  136. fi
  137. return 1
  138. fi
  139. p=$i
  140. i=$(_math "${i}" + 1)
  141. done
  142. fi
  143. return 1
  144. }
  145. _rest() {
  146. method="${1}"
  147. ep="/${2}"
  148. data="${3}"
  149. _debug method "${method}"
  150. _debug ep "${ep}"
  151. export _H1="Accept: application/json"
  152. export _H2="Content-Type: application/json"
  153. export _H3="Api-Version: ${NW_API_VERSION}"
  154. export _H4="User-Agent: NW-ACME-CLIENT"
  155. export _H5="Authorization: Bearer ${NW_API_TOKEN}"
  156. if [ "${method}" != "GET" ]; then
  157. _debug data "${data}"
  158. response="$(_post "${data}" "${NW_API_ENDPOINT}${ep}" "" "${method}")"
  159. else
  160. response="$(_get "${NW_API_ENDPOINT}${ep}${data}")"
  161. fi
  162. if [ "${?}" != "0" ]; then
  163. _err "error ${ep}"
  164. return 1
  165. fi
  166. _debug2 response "${response}"
  167. return 0
  168. }