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.6 KiB

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