]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_yandex.sh
Merge branch 'dev' of https://github.com/Neilpang/acme.sh into dev
[mirror_acme.sh.git] / dnsapi / dns_yandex.sh
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
9 ######## Public functions #####################
10
11 #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
12 dns_yandex_add() {
13 fulldomain="${1}"
14 txtvalue="${2}"
15 _debug "Calling: dns_yandex_add() '${fulldomain}' '${txtvalue}'"
16 _PDD_credentials || return 1
17 export _H1="PddToken: $PDD_Token"
18
19 _PDD_get_domain "$fulldomain" || return 1
20 _debug "Found suitable domain in pdd: $curDomain"
21 curData="domain=${curDomain}&type=TXT&subdomain=${curSubdomain}&ttl=360&content=${txtvalue}"
22 curUri="https://pddimp.yandex.ru/api2/admin/dns/add"
23 curResult="$(_post "${curData}" "${curUri}")"
24 _debug "Result: $curResult"
25 }
26
27 #Usage: dns_myapi_rm _acme-challenge.www.domain.com
28 dns_yandex_rm() {
29 fulldomain="${1}"
30 _debug "Calling: dns_yandex_rm() '${fulldomain}'"
31 _PDD_credentials || return 1
32 export _H1="PddToken: $PDD_Token"
33
34 _PDD_get_domain "$fulldomain" || return 1
35 _debug "Found suitable domain in pdd: $curDomain"
36
37 record_id=$(pdd_get_record_id "${fulldomain}")
38 _debug "Result: $record_id"
39
40 for rec_i in $record_id; do
41 curUri="https://pddimp.yandex.ru/api2/admin/dns/del"
42 curData="domain=${curDomain}&record_id=${rec_i}"
43 curResult="$(_post "${curData}" "${curUri}")"
44 _debug "Result: $curResult"
45 done
46 }
47
48 #################### Private functions below ##################################
49
50 _PDD_get_domain() {
51 fulldomain="${1}"
52 __page=1
53 __last=0
54 while [ $__last -eq 0 ]; do
55 uri1="https://pddimp.yandex.ru/api2/admin/domain/domains?page=${__page}&on_page=20"
56 res1="$(_get "$uri1" | _normalizeJson)"
57 _debug2 "res1" "$res1"
58 __found="$(echo "$res1" | sed -n -e 's#.* "found": \([^,]*\),.*#\1#p')"
59 _debug "found: $__found results on page"
60 if [ "0$__found" -lt 20 ]; then
61 _debug "last page: $__page"
62 __last=1
63 fi
64
65 __all_domains="$__all_domains $(echo "$res1" | tr "," "\n" | grep '"name"' | cut -d: -f2 | sed -e 's@"@@g')"
66
67 __page=$(_math $__page + 1)
68 done
69
70 k=2
71 while [ $k -lt 10 ]; do
72 __t=$(echo "$fulldomain" | cut -d . -f $k-100)
73 _debug "finding zone for domain $__t"
74 for d in $__all_domains; do
75 if [ "$d" = "$__t" ]; then
76 p=$(_math $k - 1)
77 curSubdomain="$(echo "$fulldomain" | cut -d . -f "1-$p")"
78 curDomain="$__t"
79 return 0
80 fi
81 done
82 k=$(_math $k + 1)
83 done
84 _err "No suitable domain found in your account"
85 return 1
86 }
87
88 _PDD_credentials() {
89 if [ -z "${PDD_Token}" ]; then
90 PDD_Token=""
91 _err "You need to export PDD_Token=xxxxxxxxxxxxxxxxx"
92 _err "You can get it at https://pddimp.yandex.ru/api2/admin/get_token"
93 return 1
94 else
95 _saveaccountconf PDD_Token "${PDD_Token}"
96 fi
97 }
98
99 pdd_get_record_id() {
100 fulldomain="${1}"
101
102 _PDD_get_domain "$fulldomain"
103 _debug "Found suitable domain in pdd: $curDomain"
104
105 curUri="https://pddimp.yandex.ru/api2/admin/dns/list?domain=${curDomain}"
106 curResult="$(_get "${curUri}" | _normalizeJson)"
107 _debug "Result: $curResult"
108 echo "$curResult" | _egrep_o "{[^{]*\"content\":[^{]*\"subdomain\":\"${curSubdomain}\"" | sed -n -e 's#.* "record_id": \(.*\),[^,]*#\1#p'
109 }