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.

121 lines
3.4 KiB

4 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_yandex_info='Yandex Domains
  4. Site: tech.Yandex.com/domain/
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_yandex
  6. Options:
  7. PDD_Token API Token
  8. Issues: github.com/non7top/acme.sh/issues
  9. Author: <non7top@gmail.com>
  10. '
  11. ######## Public functions #####################
  12. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. dns_yandex_add() {
  14. fulldomain="${1}"
  15. txtvalue="${2}"
  16. _debug "Calling: dns_yandex_add() '${fulldomain}' '${txtvalue}'"
  17. _PDD_credentials || return 1
  18. _PDD_get_domain || return 1
  19. _debug "Found suitable domain: $domain"
  20. _PDD_get_record_ids || return 1
  21. _debug "Record_ids: $record_ids"
  22. if [ -n "$record_ids" ]; then
  23. _info "All existing $subdomain records from $domain will be removed at the very end."
  24. fi
  25. data="domain=${domain}&type=TXT&subdomain=${subdomain}&ttl=300&content=${txtvalue}"
  26. uri="https://pddimp.yandex.ru/api2/admin/dns/add"
  27. result="$(_post "${data}" "${uri}" | _normalizeJson)"
  28. _debug "Result: $result"
  29. if ! _contains "$result" '"success":"ok"'; then
  30. if _contains "$result" '"success":"error"' && _contains "$result" '"error":"record_exists"'; then
  31. _info "Record already exists."
  32. else
  33. _err "Can't add $subdomain to $domain."
  34. return 1
  35. fi
  36. fi
  37. }
  38. #Usage: dns_myapi_rm _acme-challenge.www.domain.com
  39. dns_yandex_rm() {
  40. fulldomain="${1}"
  41. _debug "Calling: dns_yandex_rm() '${fulldomain}'"
  42. _PDD_credentials || return 1
  43. _PDD_get_domain "$fulldomain" || return 1
  44. _debug "Found suitable domain: $domain"
  45. _PDD_get_record_ids "${domain}" "${subdomain}" || return 1
  46. _debug "Record_ids: $record_ids"
  47. for record_id in $record_ids; do
  48. data="domain=${domain}&record_id=${record_id}"
  49. uri="https://pddimp.yandex.ru/api2/admin/dns/del"
  50. result="$(_post "${data}" "${uri}" | _normalizeJson)"
  51. _debug "Result: $result"
  52. if ! _contains "$result" '"success":"ok"'; then
  53. _info "Can't remove $subdomain from $domain."
  54. fi
  55. done
  56. }
  57. #################### Private functions below ##################################
  58. _PDD_get_domain() {
  59. subdomain_start=1
  60. while true; do
  61. domain_start=$(_math $subdomain_start + 1)
  62. domain=$(echo "$fulldomain" | cut -d . -f "$domain_start"-)
  63. subdomain=$(echo "$fulldomain" | cut -d . -f -"$subdomain_start")
  64. _debug "Checking domain $domain"
  65. if [ -z "$domain" ]; then
  66. return 1
  67. fi
  68. uri="https://pddimp.yandex.ru/api2/admin/dns/list?domain=$domain"
  69. result="$(_get "${uri}" | _normalizeJson)"
  70. _debug "Result: $result"
  71. if _contains "$result" '"success":"ok"'; then
  72. return 0
  73. fi
  74. subdomain_start=$(_math $subdomain_start + 1)
  75. done
  76. }
  77. _PDD_credentials() {
  78. if [ -z "${PDD_Token}" ]; then
  79. PDD_Token=""
  80. _err "You need to export PDD_Token=xxxxxxxxxxxxxxxxx."
  81. _err "You can get it at https://pddimp.yandex.ru/api2/admin/get_token."
  82. return 1
  83. else
  84. _saveaccountconf PDD_Token "${PDD_Token}"
  85. fi
  86. export _H1="PddToken: $PDD_Token"
  87. }
  88. _PDD_get_record_ids() {
  89. _debug "Check existing records for $subdomain"
  90. uri="https://pddimp.yandex.ru/api2/admin/dns/list?domain=${domain}"
  91. result="$(_get "${uri}" | _normalizeJson)"
  92. _debug "Result: $result"
  93. if ! _contains "$result" '"success":"ok"'; then
  94. return 1
  95. fi
  96. record_ids=$(echo "$result" | _egrep_o "{[^{]*\"subdomain\":\"${subdomain}\"[^}]*}" | sed -n -e 's#.*"record_id": \([0-9]*\).*#\1#p')
  97. }