dns_ultra.sh 4.5 KB

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