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

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