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.

177 lines
4.8 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_aurora_info='versio.nl AuroraDNS
  4. Domains: pcextreme.nl
  5. Site: versio.nl
  6. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_aurora
  7. Options:
  8. AURORA_Key API Key
  9. AURORA_Secret API Secret
  10. Issues: github.com/acmesh-official/acme.sh/issues/3459
  11. Author: Jasper Zonneveld
  12. '
  13. AURORA_Api="https://api.auroradns.eu"
  14. ######## Public functions #####################
  15. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  16. dns_aurora_add() {
  17. fulldomain=$1
  18. txtvalue=$2
  19. AURORA_Key="${AURORA_Key:-$(_readaccountconf_mutable AURORA_Key)}"
  20. AURORA_Secret="${AURORA_Secret:-$(_readaccountconf_mutable AURORA_Secret)}"
  21. if [ -z "$AURORA_Key" ] || [ -z "$AURORA_Secret" ]; then
  22. AURORA_Key=""
  23. AURORA_Secret=""
  24. _err "You didn't specify an Aurora api key and secret yet."
  25. _err "You can get yours from here https://cp.pcextreme.nl/auroradns/users."
  26. return 1
  27. fi
  28. #save the api key and secret to the account conf file.
  29. _saveaccountconf_mutable AURORA_Key "$AURORA_Key"
  30. _saveaccountconf_mutable AURORA_Secret "$AURORA_Secret"
  31. _debug "First detect the root zone"
  32. if ! _get_root "$fulldomain"; then
  33. _err "invalid domain"
  34. return 1
  35. fi
  36. _debug _domain_id "$_domain_id"
  37. _debug _sub_domain "$_sub_domain"
  38. _debug _domain "$_domain"
  39. _info "Adding record"
  40. if _aurora_rest POST "zones/$_domain_id/records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":300}"; then
  41. if _contains "$response" "$txtvalue"; then
  42. _info "Added, OK"
  43. return 0
  44. elif _contains "$response" "RecordExistsError"; then
  45. _info "Already exists, OK"
  46. return 0
  47. else
  48. _err "Add txt record error."
  49. return 1
  50. fi
  51. fi
  52. _err "Add txt record error."
  53. return 1
  54. }
  55. #fulldomain txtvalue
  56. dns_aurora_rm() {
  57. fulldomain=$1
  58. txtvalue=$2
  59. AURORA_Key="${AURORA_Key:-$(_readaccountconf_mutable AURORA_Key)}"
  60. AURORA_Secret="${AURORA_Secret:-$(_readaccountconf_mutable AURORA_Secret)}"
  61. _debug "First detect the root zone"
  62. if ! _get_root "$fulldomain"; then
  63. _err "invalid domain"
  64. return 1
  65. fi
  66. _debug _domain_id "$_domain_id"
  67. _debug _sub_domain "$_sub_domain"
  68. _debug _domain "$_domain"
  69. _debug "Getting records"
  70. _aurora_rest GET "zones/${_domain_id}/records"
  71. if ! _contains "$response" "$txtvalue"; then
  72. _info "Don't need to remove."
  73. else
  74. records=$(echo "$response" | _normalizeJson | tr -d "[]" | sed "s/},{/}|{/g" | tr "|" "\n")
  75. if [ "$(echo "$records" | wc -l)" -le 2 ]; then
  76. _err "Can not parse records."
  77. return 1
  78. fi
  79. record_id=$(echo "$records" | grep "\"type\": *\"TXT\"" | grep "\"name\": *\"$_sub_domain\"" | grep "\"content\": *\"$txtvalue\"" | _egrep_o "\"id\": *\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | _head_n 1 | tr -d " ")
  80. _debug "record_id" "$record_id"
  81. if [ -z "$record_id" ]; then
  82. _err "Can not get record id to remove."
  83. return 1
  84. fi
  85. if ! _aurora_rest DELETE "zones/$_domain_id/records/$record_id"; then
  86. _err "Delete record error."
  87. return 1
  88. fi
  89. fi
  90. return 0
  91. }
  92. #################### Private functions below ##################################
  93. #_acme-challenge.www.domain.com
  94. #returns
  95. # _sub_domain=_acme-challenge.www
  96. # _domain=domain.com
  97. # _domain_id=sdjkglgdfewsdfg
  98. _get_root() {
  99. domain=$1
  100. i=1
  101. p=1
  102. while true; do
  103. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  104. _debug h "$h"
  105. if [ -z "$h" ]; then
  106. #not valid
  107. return 1
  108. fi
  109. if ! _aurora_rest GET "zones/$h"; then
  110. return 1
  111. fi
  112. if _contains "$response" "\"name\": \"$h\""; then
  113. _domain_id=$(echo "$response" | _normalizeJson | tr -d "{}" | tr "," "\n" | grep "\"id\": *\"" | cut -d : -f 2 | tr -d \" | _head_n 1 | tr -d " ")
  114. _debug _domain_id "$_domain_id"
  115. if [ "$_domain_id" ]; then
  116. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  117. _domain=$h
  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. _aurora_rest() {
  128. m=$1
  129. ep="$2"
  130. data="$3"
  131. _debug "$ep"
  132. key_trimmed=$(echo "$AURORA_Key" | tr -d '"')
  133. secret_trimmed=$(echo "$AURORA_Secret" | tr -d '"')
  134. timestamp=$(date -u +"%Y%m%dT%H%M%SZ")
  135. signature=$(printf "%s/%s%s" "$m" "$ep" "$timestamp" | _hmac sha256 "$(printf "%s" "$secret_trimmed" | _hex_dump | tr -d " ")" | _base64)
  136. authorization=$(printf "AuroraDNSv1 %s" "$(printf "%s:%s" "$key_trimmed" "$signature" | _base64)")
  137. export _H1="Content-Type: application/json; charset=UTF-8"
  138. export _H2="X-AuroraDNS-Date: $timestamp"
  139. export _H3="Authorization: $authorization"
  140. if [ "$m" != "GET" ]; then
  141. _debug data "$data"
  142. response="$(_post "$data" "$AURORA_Api/$ep" "" "$m")"
  143. else
  144. response="$(_get "$AURORA_Api/$ep")"
  145. fi
  146. if [ "$?" != "0" ]; then
  147. _err "error $ep"
  148. return 1
  149. fi
  150. _debug2 response "$response"
  151. return 0
  152. }