]>
Commit | Line | Data |
---|---|---|
a460ac02 VB |
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 | ||
6 | # Values to export: | |
7 | # export PDD_Token="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
8 | ||
ef7b51be | 9 | # Sometimes cloudflare / google doesn't pick new dns records fast enough. |
8494ac8f NS |
10 | # You can add --dnssleep XX to params as workaround. |
11 | ||
a460ac02 VB |
12 | ######## Public functions ##################### |
13 | ||
14 | #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
15 | dns_yandex_add() { | |
ef7b51be NS |
16 | fulldomain="${1}" |
17 | txtvalue="${2}" | |
4eff3b6a | 18 | _debug "Calling: dns_yandex_add() '${fulldomain}' '${txtvalue}'" |
8494ac8f | 19 | |
fceb7285 | 20 | _PDD_credentials || return 1 |
a460ac02 | 21 | |
4eff3b6a | 22 | _PDD_get_domain || return 1 |
8494ac8f NS |
23 | _debug "Found suitable domain: $domain" |
24 | ||
4eff3b6a | 25 | _PDD_get_record_ids || return 1 |
8494ac8f NS |
26 | _debug "Record_ids: $record_ids" |
27 | ||
eb9005ad | 28 | if [ -n "$record_ids" ]; then |
4eff3b6a | 29 | _info "All existing $subdomain records from $domain will be removed at the very end." |
8494ac8f NS |
30 | fi |
31 | ||
ef7b51be NS |
32 | data="domain=${domain}&type=TXT&subdomain=${subdomain}&ttl=300&content=${txtvalue}" |
33 | uri="https://pddimp.yandex.ru/api2/admin/dns/add" | |
34 | result="$(_post "${data}" "${uri}" | _normalizeJson)" | |
8494ac8f NS |
35 | _debug "Result: $result" |
36 | ||
37 | if ! _contains "$result" '"success":"ok"'; then | |
4eff3b6a NS |
38 | if _contains "$result" '"success":"error"' && _contains "$result" '"error":"record_exists"'; then |
39 | _info "Record already exists." | |
40 | else | |
41 | _err "Can't add $subdomain to $domain." | |
42 | return 1 | |
43 | fi | |
8494ac8f | 44 | fi |
a460ac02 VB |
45 | } |
46 | ||
47 | #Usage: dns_myapi_rm _acme-challenge.www.domain.com | |
48 | dns_yandex_rm() { | |
ef7b51be | 49 | fulldomain="${1}" |
a460ac02 | 50 | _debug "Calling: dns_yandex_rm() '${fulldomain}'" |
8494ac8f | 51 | |
fceb7285 | 52 | _PDD_credentials || return 1 |
a460ac02 | 53 | |
f254bb39 | 54 | _PDD_get_domain "$fulldomain" || return 1 |
8494ac8f | 55 | _debug "Found suitable domain: $domain" |
eb6be88f | 56 | |
8494ac8f NS |
57 | _PDD_get_record_ids "${domain}" "${subdomain}" || return 1 |
58 | _debug "Record_ids: $record_ids" | |
2f15ad4b | 59 | |
8494ac8f | 60 | for record_id in $record_ids; do |
ef7b51be NS |
61 | data="domain=${domain}&record_id=${record_id}" |
62 | uri="https://pddimp.yandex.ru/api2/admin/dns/del" | |
63 | result="$(_post "${data}" "${uri}" | _normalizeJson)" | |
8494ac8f NS |
64 | _debug "Result: $result" |
65 | ||
66 | if ! _contains "$result" '"success":"ok"'; then | |
4eff3b6a | 67 | _info "Can't remove $subdomain from $domain." |
8494ac8f | 68 | fi |
f85348ba | 69 | done |
a460ac02 VB |
70 | } |
71 | ||
72 | #################### Private functions below ################################## | |
73 | ||
eb6be88f | 74 | _PDD_get_domain() { |
ef7b51be | 75 | subdomain_start=1 |
8494ac8f | 76 | while true; do |
ef7b51be NS |
77 | domain_start=$(_math $subdomain_start + 1) |
78 | domain=$(echo "$fulldomain" | cut -d . -f "$domain_start"-) | |
79 | subdomain=$(echo "$fulldomain" | cut -d . -f -"$subdomain_start") | |
eb6be88f | 80 | |
8494ac8f NS |
81 | _debug "Checking domain $domain" |
82 | if [ -z "$domain" ]; then | |
83 | return 1 | |
84 | fi | |
85 | ||
ef7b51be NS |
86 | uri="https://pddimp.yandex.ru/api2/admin/dns/list?domain=$domain" |
87 | result="$(_get "${uri}" | _normalizeJson)" | |
8494ac8f | 88 | _debug "Result: $result" |
eb6be88f | 89 | |
8494ac8f | 90 | if _contains "$result" '"success":"ok"'; then |
e5f69f08 | 91 | return 0 |
8494ac8f NS |
92 | fi |
93 | subdomain_start=$(_math $subdomain_start + 1) | |
eb6be88f | 94 | done |
eb6be88f VB |
95 | } |
96 | ||
a460ac02 VB |
97 | _PDD_credentials() { |
98 | if [ -z "${PDD_Token}" ]; then | |
99 | PDD_Token="" | |
4eff3b6a NS |
100 | _err "You need to export PDD_Token=xxxxxxxxxxxxxxxxx." |
101 | _err "You can get it at https://pddimp.yandex.ru/api2/admin/get_token." | |
a460ac02 VB |
102 | return 1 |
103 | else | |
104 | _saveaccountconf PDD_Token "${PDD_Token}" | |
105 | fi | |
8494ac8f | 106 | export _H1="PddToken: $PDD_Token" |
a460ac02 VB |
107 | } |
108 | ||
8494ac8f | 109 | _PDD_get_record_ids() { |
8494ac8f | 110 | _debug "Check existing records for $subdomain" |
eb6be88f | 111 | |
ef7b51be NS |
112 | uri="https://pddimp.yandex.ru/api2/admin/dns/list?domain=${domain}" |
113 | result="$(_get "${uri}" | _normalizeJson)" | |
8494ac8f NS |
114 | _debug "Result: $result" |
115 | ||
116 | if ! _contains "$result" '"success":"ok"'; then | |
e5f69f08 | 117 | return 1 |
8494ac8f | 118 | fi |
eb6be88f | 119 | |
8494ac8f | 120 | record_ids=$(echo "$result" | _egrep_o "{[^{]*\"subdomain\":\"${subdomain}\"[^}]*}" | sed -n -e 's#.*"record_id": \([0-9]*\).*#\1#p') |
a460ac02 | 121 | } |