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.

211 lines
5.6 KiB

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