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.

167 lines
4.6 KiB

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