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