]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_la.sh
dns.la official acme script
[mirror_acme.sh.git] / dnsapi / dns_la.sh
1 #!/usr/bin/env sh
2 # dns.la Domain api
3 #
4 #LA_Id="test123"
5 #
6 #LA_Key="d1j2fdo4dee3948"
7 DNSLA_API="https://api.dns.la/api/"
8 ######## Public functions #####################
9 #Usage: dns_la_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
10 dns_la_add() {
11 fulldomain=$1
12 txtvalue=$2
13
14 LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}"
15 LA_Key="${LA_Key:-$(_readaccountconf_mutable LA_Key)}"
16 if [ -z "$LA_Id" ] || [ -z "$LA_Key" ]; then
17 LA_Id=""
18 LA_Key=""
19 _err "You don't specify dnsla api id and key yet."
20 _err "Please create your key and try again."
21 return 1
22 fi
23
24 #save the api key and email to the account conf file.
25 _saveaccountconf_mutable LA_Id "$LA_Id"
26 _saveaccountconf_mutable LA_Key "$LA_Key"
27
28 _debug "detect the root zone"
29 if ! _get_root "$fulldomain"; then
30 _err "invalid domain"
31 return 1
32 fi
33
34 add_record "$_domain" "$_sub_domain" "$txtvalue"
35
36 }
37
38 #fulldomain txtvalue
39 dns_la_rm() {
40 fulldomain=$1
41 txtvalue=$2
42
43 LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}"
44 LA_Key="${LA_Key:-$(_readaccountconf_mutable LA_Key)}"
45
46 _debug "First detect the root zone"
47 if ! _get_root "$fulldomain"; then
48 _err "invalid domain"
49 return 1
50 fi
51
52 if ! _rest GET "record.ashx?cmd=listn&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&domain=$_domain&host=$_sub_domain&recordtype=TXT&recorddata=$txtvalue"; then
53 _err "get record list error."
54 return 1
55 fi
56
57 if ! _contains "$response" "recordid"; then
58 _info "no need to remove record."
59 return 0
60 fi
61
62 _record_id=$(printf "%s" "$response" | grep '"recordid":' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n')
63
64 _debug delete_rid "$_record_id"
65 if ! _rest GET "record.ashx?cmd=remove&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&domain=$_domain&recordid=$_record_id"; then
66 _err "record remove error."
67 return 1
68 fi
69
70 _contains "$response" "\"code\":300"
71 }
72
73 #add the txt record.
74 #usage: root sub txtvalue
75 add_record() {
76 root=$1
77 sub=$2
78 txtvalue=$3
79 fulldomain="$sub.$root"
80
81 _info "adding txt record"
82 if ! _rest GET "record.ashx?cmd=create&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&host=$_sub_domain&recordtype=TXT&recorddata=$txtvalue&recordline="; then
83 return 1
84 fi
85
86 if _contains "$response" "resultid" || _contains "$response" "\"code\":532"; then
87 return 0
88 fi
89 return 1
90 }
91
92 #################### Private functions below ##################################
93 #_acme-challenge.www.domain.com
94 #returns
95 # _sub_domain=_acme-challenge.www
96 # _domain=domain.com
97 # _domain_id=sdjkglgdfewsdfg
98 _get_root() {
99 domain=$1
100 i=2
101 p=1
102 while true; do
103 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
104 if [ -z "$h" ]; then
105 #not valid
106 return 1
107 fi
108
109 if ! _rest GET "domain.ashx?cmd=get&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domain=$h"; then
110 return 1
111 fi
112
113 if _contains "$response" "\"code\":300"; then
114 _domain_id=$(printf "%s" "$response" | grep '"domainid"' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n')
115 _debug _domain_id "$_domain_id"
116 if [ "$_domain_id" ]; then
117 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
118 _debug _sub_domain "$_sub_domain"
119 _domain="$h"
120 _debug _domain "$_domain"
121 return 0
122 fi
123 return 1
124 fi
125 p="$i"
126 i=$(_math "$i" + 1)
127 done
128 return 1
129 }
130
131 #Usage: method URI data
132 _rest() {
133 m="$1"
134 ep="$2"
135 data="$3"
136 _debug "$ep"
137 url="$DNSLA_API$ep"
138
139 _debug url "$url"
140
141 if [ "$m" = "GET" ]; then
142 response="$(_get "$url" | tr -d ' ' | tr "}" ",")"
143 else
144 _debug2 data "$data"
145 response="$(_post "$data" "$url" | tr -d ' ' | tr "}" ",")"
146 fi
147
148 if [ "$?" != "0" ]; then
149 _err "error $ep"
150 return 1
151 fi
152 _debug2 response "$response"
153 return 0
154 }