]> 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://www.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 _fullkey=$(printf "%s" "$fulldomain" | awk '{ string=substr($0, 17); print string; }' | tr '.' '_' )
43
44 LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}"
45 LA_Key="${LA_Key:-$(_readaccountconf_mutable LA_Key)}"
46 _debug fullkey "$_fullkey"
47 RM_recordid="$(_readaccountconf "$_fullkey")"
48 _debug rm_recordid "$RM_recordid"
49 _debug "detect the root zone"
50 if ! _get_root "$fulldomain"; then
51 _err "invalid domain"
52 return 1
53 fi
54
55 if ! _rest GET "record.ashx?cmd=get&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&domain=$_domain&recordid=$RM_recordid"; then
56 _err "get record lis error."
57 return 1
58 fi
59
60 if ! _contains "$response" "$RM_recordid"; then
61 _info "no need to remove record."
62 return 0
63 fi
64
65 if ! _rest GET "record.ashx?cmd=remove&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&domain=$_domain&recordid=$RM_recordid"; then
66 _err "record remove error."
67 return 1
68 fi
69
70 _clearaccountconf "$_fullkey"
71
72 _contains "$response" "\"code\":300"
73 }
74
75 #add the txt record.
76 #usage: root sub txtvalue
77 add_record() {
78 root=$1
79 sub=$2
80 txtvalue=$3
81 fulldomain="$sub.$root"
82
83 _info "adding txt record"
84
85 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
86 return 1
87 fi
88
89 if _contains "$response" "\"code\":300"; then
90 _record_id=$(printf "%s" "$response" | grep '"resultid"' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n' )
91 _fullkey=$(printf "%s" "$fulldomain" | awk '{ string=substr($0, 17); print string; }' | tr '.' '_' )
92 _debug fullkey "$_fullkey"
93 _saveaccountconf "$_fullkey" "$_record_id"
94 _debug _record_id "$_record_id"
95 fi
96 _contains "$response" "\"code\":300"
97 }
98
99 #################### Private functions below ##################################
100 #_acme-challenge.www.domain.com
101 #returns
102 # _sub_domain=_acme-challenge.www
103 # _domain=domain.com
104 # _domain_id=sdjkglgdfewsdfg
105 _get_root() {
106 domain=$1
107 i=2
108 p=1
109 while true; do
110 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
111 if [ -z "$h" ]; then
112 #not valid
113 return 1
114 fi
115
116 if ! _rest GET "domain.ashx?cmd=get&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domain=$h"; then
117 return 1
118 fi
119
120 if _contains "$response" "\"code\":300"; then
121 _domain_id=$(printf "%s" "$response" | grep '"domainid"' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n' )
122 _debug _domain_id "$_domain_id"
123 if [ "$_domain_id" ]; then
124 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
125 _debug _sub_domain "$_sub_domain"
126 _domain="$h"
127 _debug _domain "$_domain"
128 return 0
129 fi
130 return 1
131 fi
132 p="$i"
133 i=$(_math "$i" + 1)
134 done
135 return 1
136 }
137
138 #Usage: method URI data
139 _rest() {
140 m="$1"
141 ep="$2"
142 data="$3"
143 _debug "$ep"
144 url="$DNSLA_API$ep"
145
146 _debug url "$url"
147
148 if [ "$m" = "GET" ]; then
149 response="$(_get "$url" | tr -d ' ' | tr "}" ",")"
150 else
151 _debug2 data "$data"
152 response="$(_post "$data" "$url" | tr -d ' ' | tr "}" ",")"
153 fi
154
155 if [ "$?" != "0" ]; then
156 _err "error $ep"
157 return 1
158 fi
159 _debug2 response "$response"
160 return 0
161 }