]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_la.sh
Merge pull request #4531 from NCDGHA/bugfix/issue_4530_fix_http_status_503
[mirror_acme.sh.git] / dnsapi / dns_la.sh
1 #!/usr/bin/env sh
2
3 #LA_Id="test123"
4 #LA_Key="d1j2fdo4dee3948"
5
6 LA_Api="https://api.dns.la/api"
7
8 ######## Public functions #####################
9
10 #Usage: dns_la_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
11 dns_la_add() {
12 fulldomain=$1
13 txtvalue=$2
14
15 LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}"
16 LA_Key="${LA_Key:-$(_readaccountconf_mutable LA_Key)}"
17
18 if [ -z "$LA_Id" ] || [ -z "$LA_Key" ]; then
19 LA_Id=""
20 LA_Key=""
21 _err "You didn't specify a dnsla api id and key yet."
22 return 1
23 fi
24
25 #save the api key and email to the account conf file.
26 _saveaccountconf_mutable LA_Id "$LA_Id"
27 _saveaccountconf_mutable LA_Key "$LA_Key"
28
29 _debug "First detect the root zone"
30 if ! _get_root "$fulldomain"; then
31 _err "invalid domain"
32 return 1
33 fi
34 _debug _domain_id "$_domain_id"
35 _debug _sub_domain "$_sub_domain"
36 _debug _domain "$_domain"
37
38 _info "Adding record"
39 if _la_rest "record.ashx?cmd=create&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&host=$_sub_domain&recordtype=TXT&recorddata=$txtvalue&recordline="; then
40 if _contains "$response" '"resultid":'; then
41 _info "Added, OK"
42 return 0
43 elif _contains "$response" '"code":532'; then
44 _info "Already exists, OK"
45 return 0
46 else
47 _err "Add txt record error."
48 return 1
49 fi
50 fi
51 _err "Add txt record error."
52 return 1
53
54 }
55
56 #fulldomain txtvalue
57 dns_la_rm() {
58 fulldomain=$1
59 txtvalue=$2
60
61 LA_Id="${LA_Id:-$(_readaccountconf_mutable LA_Id)}"
62 LA_Key="${LA_Key:-$(_readaccountconf_mutable LA_Key)}"
63
64 _debug "First detect the root zone"
65 if ! _get_root "$fulldomain"; then
66 _err "invalid domain"
67 return 1
68 fi
69 _debug _domain_id "$_domain_id"
70 _debug _sub_domain "$_sub_domain"
71 _debug _domain "$_domain"
72
73 _debug "Getting txt records"
74 if ! _la_rest "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
75 _err "Error"
76 return 1
77 fi
78
79 if ! _contains "$response" '"recordid":'; then
80 _info "Don't need to remove."
81 return 0
82 fi
83
84 record_id=$(printf "%s" "$response" | grep '"recordid":' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n')
85 _debug "record_id" "$record_id"
86 if [ -z "$record_id" ]; then
87 _err "Can not get record id to remove."
88 return 1
89 fi
90 if ! _la_rest "record.ashx?cmd=remove&apiid=$LA_Id&apipass=$LA_Key&rtype=json&domainid=$_domain_id&domain=$_domain&recordid=$record_id"; then
91 _err "Delete record error."
92 return 1
93 fi
94 _contains "$response" '"code":300'
95
96 }
97
98 #################### Private functions below ##################################
99 #_acme-challenge.www.domain.com
100 #returns
101 # _sub_domain=_acme-challenge.www
102 # _domain=domain.com
103 # _domain_id=sdjkglgdfewsdfg
104 _get_root() {
105 domain=$1
106 i=1
107 p=1
108
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 ! _la_rest "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" '"domainid":'; then
121 _domain_id=$(printf "%s" "$response" | grep '"domainid":' | cut -d : -f 2 | cut -d , -f 1 | tr -d '\r' | tr -d '\n')
122 if [ "$_domain_id" ]; then
123 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
124 _domain="$h"
125 return 0
126 fi
127 return 1
128 fi
129 p="$i"
130 i=$(_math "$i" + 1)
131 done
132 return 1
133 }
134
135 #Usage: URI
136 _la_rest() {
137 url="$LA_Api/$1"
138 _debug "$url"
139
140 if ! response="$(_get "$url" | tr -d ' ' | tr "}" ",")"; then
141 _err "Error: $url"
142 return 1
143 fi
144
145 _debug2 response "$response"
146 return 0
147 }