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.

148 lines
3.5 KiB

2 months ago
2 months ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_ad_info='AlwaysData.com
  4. Site: AlwaysData.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_ad
  6. Options:
  7. AD_API_KEY API Key
  8. Issues: github.com/acmesh-official/acme.sh/pull/503
  9. Author: Paul Koppen
  10. '
  11. AD_API_URL="https://$AD_API_KEY:@api.alwaysdata.com/v1"
  12. ######## Public functions #####################
  13. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_ad_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. if [ -z "$AD_API_KEY" ]; then
  18. AD_API_KEY=""
  19. _err "You didn't specify the AD api key yet."
  20. _err "Please create you key and try again."
  21. return 1
  22. fi
  23. _saveaccountconf AD_API_KEY "$AD_API_KEY"
  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. _ad_tmpl_json="{\"domain\":$_domain_id,\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"value\":\"$txtvalue\"}"
  33. if _ad_rest POST "record/" "$_ad_tmpl_json" && [ -z "$response" ]; then
  34. _info "txt record updated success."
  35. return 0
  36. fi
  37. return 1
  38. }
  39. #fulldomain txtvalue
  40. dns_ad_rm() {
  41. fulldomain=$1
  42. txtvalue=$2
  43. _debug "First detect the root zone"
  44. if ! _get_root "$fulldomain"; then
  45. _err "invalid domain"
  46. return 1
  47. fi
  48. _debug _domain_id "$_domain_id"
  49. _debug _sub_domain "$_sub_domain"
  50. _debug _domain "$_domain"
  51. _debug "Getting txt records"
  52. _ad_rest GET "record/?domain=$_domain_id&name=$_sub_domain"
  53. if [ -n "$response" ]; then
  54. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\s*[0-9]+" | cut -d : -f 2 | tr -d " " | _head_n 1)
  55. _debug record_id "$record_id"
  56. if [ -z "$record_id" ]; then
  57. _err "Can not get record id to remove."
  58. return 1
  59. fi
  60. if _ad_rest DELETE "record/$record_id/" && [ -z "$response" ]; then
  61. _info "txt record deleted success."
  62. return 0
  63. fi
  64. _debug response "$response"
  65. return 1
  66. fi
  67. return 1
  68. }
  69. #################### Private functions below ##################################
  70. #_acme-challenge.www.domain.com
  71. #returns
  72. # _sub_domain=_acme-challenge.www
  73. # _domain=domain.com
  74. # _domain_id=12345
  75. _get_root() {
  76. domain=$1
  77. i=2
  78. p=1
  79. if _ad_rest GET "domain/"; then
  80. response="$(echo "$response" | tr -d "\n" | sed 's/{/\n&/g')"
  81. while true; do
  82. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  83. _debug h "$h"
  84. if [ -z "$h" ]; then
  85. #not valid
  86. return 1
  87. fi
  88. hostedzone="$(echo "$response" | _egrep_o "{.*\"name\":\s*\"$h\".*}")"
  89. if [ "$hostedzone" ]; then
  90. _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "\"id\":\s*[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
  91. if [ "$_domain_id" ]; then
  92. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  93. _domain=$h
  94. return 0
  95. fi
  96. return 1
  97. fi
  98. p=$i
  99. i=$(_math "$i" + 1)
  100. done
  101. fi
  102. return 1
  103. }
  104. #method uri qstr data
  105. _ad_rest() {
  106. mtd="$1"
  107. ep="$2"
  108. data="$3"
  109. _debug mtd "$mtd"
  110. _debug ep "$ep"
  111. export _H1="Accept: application/json"
  112. export _H2="Content-Type: application/json"
  113. if [ "$mtd" != "GET" ]; then
  114. # both POST and DELETE.
  115. _debug data "$data"
  116. response="$(_post "$data" "$AD_API_URL/$ep" "" "$mtd")"
  117. else
  118. response="$(_get "$AD_API_URL/$ep")"
  119. fi
  120. if [ "$?" != "0" ]; then
  121. _err "error $ep"
  122. return 1
  123. fi
  124. _debug2 response "$response"
  125. return 0
  126. }