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.

171 lines
4.8 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_ultra_info='UltraDNS.com
  4. Site: UltraDNS.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_ultra
  6. Options:
  7. ULTRA_USR Username
  8. ULTRA_PWD Password
  9. Issues: github.com/acmesh-official/acme.sh/issues/2118
  10. '
  11. ULTRA_API="https://api.ultradns.com/v3/"
  12. ULTRA_AUTH_API="https://api.ultradns.com/v2/"
  13. #Usage: add _acme-challenge.www.domain.com "some_long_string_of_characters_go_here_from_lets_encrypt"
  14. dns_ultra_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. export txtvalue
  18. ULTRA_USR="${ULTRA_USR:-$(_readaccountconf_mutable ULTRA_USR)}"
  19. ULTRA_PWD="${ULTRA_PWD:-$(_readaccountconf_mutable ULTRA_PWD)}"
  20. if [ -z "$ULTRA_USR" ] || [ -z "$ULTRA_PWD" ]; then
  21. ULTRA_USR=""
  22. ULTRA_PWD=""
  23. _err "You didn't specify an UltraDNS username and password yet"
  24. return 1
  25. fi
  26. # save the username and password to the account conf file.
  27. _saveaccountconf_mutable ULTRA_USR "$ULTRA_USR"
  28. _saveaccountconf_mutable ULTRA_PWD "$ULTRA_PWD"
  29. _debug "First detect the root zone"
  30. if ! _get_root "$fulldomain"; then
  31. _err "invalid domain"
  32. return 1
  33. fi
  34. _debug _domain_id "${_domain_id}"
  35. _debug _sub_domain "${_sub_domain}"
  36. _debug _domain "${_domain}"
  37. _debug "Getting txt records"
  38. _ultra_rest GET "zones/${_domain_id}/rrsets/TXT?q=value:${fulldomain}"
  39. if printf "%s" "$response" | grep \"totalCount\" >/dev/null; then
  40. _err "Error, it would appear that this record already exists. Please review existing TXT records for this domain."
  41. return 1
  42. fi
  43. _info "Adding record"
  44. if _ultra_rest POST "zones/$_domain_id/rrsets/TXT/${_sub_domain}" '{"ttl":300,"rdata":["'"${txtvalue}"'"]}'; then
  45. if _contains "$response" "Successful"; then
  46. _info "Added, OK"
  47. return 0
  48. elif _contains "$response" "Resource Record of type 16 with these attributes already exists"; then
  49. _info "Already exists, OK"
  50. return 0
  51. else
  52. _err "Add txt record error."
  53. return 1
  54. fi
  55. fi
  56. _err "Add txt record error."
  57. }
  58. dns_ultra_rm() {
  59. fulldomain=$1
  60. txtvalue=$2
  61. export txtvalue
  62. ULTRA_USR="${ULTRA_USR:-$(_readaccountconf_mutable ULTRA_USR)}"
  63. ULTRA_PWD="${ULTRA_PWD:-$(_readaccountconf_mutable ULTRA_PWD)}"
  64. if [ -z "$ULTRA_USR" ] || [ -z "$ULTRA_PWD" ]; then
  65. ULTRA_USR=""
  66. ULTRA_PWD=""
  67. _err "You didn't specify an UltraDNS username and password yet"
  68. return 1
  69. fi
  70. _debug "First detect the root zone"
  71. if ! _get_root "$fulldomain"; then
  72. _err "invalid domain"
  73. return 1
  74. fi
  75. _debug _domain_id "${_domain_id}"
  76. _debug _sub_domain "${_sub_domain}"
  77. _debug _domain "${domain}"
  78. _debug "Getting TXT records"
  79. _ultra_rest GET "zones/${_domain_id}/rrsets?q=kind:RECORDS+owner:${_sub_domain}"
  80. if ! printf "%s" "$response" | grep \"resultInfo\" >/dev/null; then
  81. _err "There was an error in obtaining the resource records for ${_domain_id}"
  82. return 1
  83. fi
  84. count=$(echo "$response" | _egrep_o "\"returnedCount\":[^,]*" | cut -d: -f2 | cut -d'}' -f1)
  85. _debug count "${count}"
  86. if [ "${count}" = "" ]; then
  87. _info "Text record is not present, will not delete anything."
  88. else
  89. if ! _ultra_rest DELETE "zones/$_domain_id/rrsets/TXT/${_sub_domain}" '{"ttl":300,"rdata":["'"${txtvalue}"'"]}'; then
  90. _err "Deleting the record did not succeed, please verify/check."
  91. return 1
  92. fi
  93. _contains "$response" ""
  94. fi
  95. }
  96. #################### Private functions below ##################################
  97. #_acme-challenge.www.domain.com
  98. #returns
  99. # _sub_domain=_acme-challenge.www
  100. # _domain=domain.com
  101. # _domain_id=sdjkglgdfewsdfg
  102. _get_root() {
  103. domain=$1
  104. i=2
  105. p=1
  106. while true; do
  107. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  108. _debug h "$h"
  109. _debug response "$response"
  110. if [ -z "$h" ]; then
  111. #not valid
  112. return 1
  113. fi
  114. if ! _ultra_rest GET "zones"; then
  115. return 1
  116. fi
  117. if _contains "${response}" "${h}." >/dev/null; then
  118. _domain_id=$(echo "$response" | _egrep_o "${h}" | head -1)
  119. if [ "$_domain_id" ]; then
  120. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  121. _domain="${h}"
  122. _debug sub_domain "${_sub_domain}"
  123. _debug domain "${_domain}"
  124. return 0
  125. fi
  126. return 1
  127. fi
  128. p=$i
  129. i=$(_math "$i" + 1)
  130. done
  131. return 1
  132. }
  133. _ultra_rest() {
  134. m=$1
  135. ep="$2"
  136. data="$3"
  137. _debug "$ep"
  138. if [ -z "$AUTH_TOKEN" ]; then
  139. _ultra_login
  140. fi
  141. _debug TOKEN "$AUTH_TOKEN"
  142. export _H1="Content-Type: application/json"
  143. export _H2="Authorization: Bearer $AUTH_TOKEN"
  144. if [ "$m" != "GET" ]; then
  145. _debug data "$data"
  146. response="$(_post "$data" "$ULTRA_API$ep" "" "$m")"
  147. else
  148. response="$(_get "$ULTRA_API$ep")"
  149. fi
  150. }
  151. _ultra_login() {
  152. export _H1=""
  153. export _H2=""
  154. AUTH_TOKEN=$(_post "grant_type=password&username=${ULTRA_USR}&password=${ULTRA_PWD}" "${ULTRA_AUTH_API}authorization/token" | cut -d, -f3 | cut -d\" -f4)
  155. export AUTH_TOKEN
  156. }