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.

124 lines
4.3 KiB

  1. #!/usr/bin/env sh
  2. # Author: non7top@gmail.com (yandex pdd plugin),
  3. # vos@vos.uz (multitoken support)
  4. # 07 Jul 2017 (yandex pdd), 03 May 2018 (multitoken)
  5. # report bugs at https://github.com/non7top/acme.sh (pdd),
  6. # https://github.com/v0s/acme.sh (multitoken)
  7. # This plugin supports separate PDD API Tokens for different domains
  8. # USAGE:
  9. # export PDD_Multi_Token="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  10. # acme.sh --issue --dns -d example.com
  11. # After first run, plugin saves the token, no need to export it again.
  12. # It keeps the tokens in your account.conf by appending the MD5 hash of challenge domain
  13. # (e.g. for _acme-challenge.example.com: PDD_Multi_Token_7f5e9f17430f06a79f380bb1d457ff6f='xxxxx')
  14. ######## Public functions #####################
  15. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  16. dns_yandex_multitoken_add() {
  17. fulldomain="${1}"
  18. txtvalue="${2}"
  19. _debug "Calling: dns_yandex_multitoken_add() '${fulldomain}' '${txtvalue}'"
  20. _PDD_credentials "$fulldomain" || return 1
  21. export _H1="PddToken: $PDD_Multi_Token"
  22. _PDD_get_domain "$fulldomain" || return 1
  23. _debug "Found suitable domain in pdd: $curDomain"
  24. curData="domain=${curDomain}&type=TXT&subdomain=${curSubdomain}&ttl=90&content=${txtvalue}"
  25. curUri="https://pddimp.yandex.ru/api2/admin/dns/add"
  26. curResult="$(_post "${curData}" "${curUri}")"
  27. _debug "Result: $curResult"
  28. }
  29. #Usage: dns_myapi_rm _acme-challenge.www.domain.com
  30. dns_yandex_multitoken_rm() {
  31. fulldomain="${1}"
  32. _debug "Calling: dns_yandex_multitoken_rm() '${fulldomain}'"
  33. _PDD_credentials "$fulldomain" || return 1
  34. export _H1="PddToken: $PDD_Multi_Token"
  35. _PDD_get_domain "$fulldomain" || return 1
  36. _debug "Found suitable domain in pdd: $curDomain"
  37. record_id=$(pdd_get_record_id "${fulldomain}")
  38. _debug "Result: $record_id"
  39. for rec_i in $record_id; do
  40. curUri="https://pddimp.yandex.ru/api2/admin/dns/del"
  41. curData="domain=${curDomain}&record_id=${rec_i}"
  42. curResult="$(_post "${curData}" "${curUri}")"
  43. _debug "Result: $curResult"
  44. done
  45. }
  46. #################### Private functions below ##################################
  47. _PDD_get_domain() {
  48. fulldomain="${1}"
  49. __page=1
  50. __last=0
  51. while [ $__last -eq 0 ]; do
  52. uri1="https://pddimp.yandex.ru/api2/admin/domain/domains?page=${__page}&on_page=20"
  53. res1="$(_get "$uri1" | _normalizeJson)"
  54. _debug2 "res1" "$res1"
  55. __found="$(echo "$res1" | sed -n -e 's#.* "found": \([^,]*\),.*#\1#p')"
  56. _debug "found: $__found results on page"
  57. if [ "0$__found" -lt 20 ]; then
  58. _debug "last page: $__page"
  59. __last=1
  60. fi
  61. __all_domains="$__all_domains $(echo "$res1" | tr "," "\n" | grep '"name"' | cut -d: -f2 | sed -e 's@"@@g')"
  62. __page=$(_math $__page + 1)
  63. done
  64. k=2
  65. while [ $k -lt 10 ]; do
  66. __t=$(echo "$fulldomain" | cut -d . -f $k-100)
  67. _debug "finding zone for domain $__t"
  68. for d in $__all_domains; do
  69. if [ "$d" = "$__t" ]; then
  70. p=$(_math $k - 1)
  71. curSubdomain="$(echo "$fulldomain" | cut -d . -f "1-$p")"
  72. curDomain="$__t"
  73. return 0
  74. fi
  75. done
  76. k=$(_math $k + 1)
  77. done
  78. _err "No suitable domain found in your account"
  79. _err "Export PDD_Multi_Token=xxxxxxxxxxxxxxxxx for correct domain"
  80. _err "You can get it at https://pddimp.yandex.ru/api2/admin/get_token"
  81. return 1
  82. }
  83. _PDD_credentials() {
  84. fulldomain="${1}"
  85. domainhash="$(printf "%s" "$fulldomain" | openssl md5 | cut -d ' ' -f2)"
  86. if [ -z "${PDD_Multi_Token}" ]; then
  87. eval "PDD_Multi_Token=\$PDD_Multi_Token_$domainhash"
  88. if [ -z "${PDD_Multi_Token}" ]; then
  89. PDD_Multi_Token=""
  90. _err "You need to export PDD_Multi_Token=xxxxxxxxxxxxxxxxx"
  91. _err "You can get it at https://pddimp.yandex.ru/api2/admin/get_token"
  92. return 1
  93. fi
  94. else
  95. _saveaccountconf "PDD_Multi_Token_$domainhash" "${PDD_Multi_Token}"
  96. fi
  97. }
  98. pdd_get_record_id() {
  99. fulldomain="${1}"
  100. _PDD_get_domain "$fulldomain"
  101. _debug "Found suitable domain in pdd: $curDomain"
  102. curUri="https://pddimp.yandex.ru/api2/admin/dns/list?domain=${curDomain}"
  103. curResult="$(_get "${curUri}" | _normalizeJson)"
  104. _debug "Result: $curResult"
  105. echo "$curResult" | _egrep_o "{[^{]*\"content\":[^{]*\"subdomain\":\"${curSubdomain}\"" | sed -n -e 's#.* "record_id": \(.*\),[^,]*#\1#p'
  106. }