]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_lua.sh
Merge pull request #4531 from NCDGHA/bugfix/issue_4530_fix_http_status_503
[mirror_acme.sh.git] / dnsapi / dns_lua.sh
1 #!/usr/bin/env sh
2
3 # bug reports to dev@1e.ca
4
5 #
6 #LUA_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
7 #
8 #LUA_Email="user@luadns.net"
9
10 LUA_Api="https://api.luadns.com/v1"
11
12 ######## Public functions #####################
13
14 #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
15 dns_lua_add() {
16 fulldomain=$1
17 txtvalue=$2
18
19 LUA_Key="${LUA_Key:-$(_readaccountconf_mutable LUA_Key)}"
20 LUA_Email="${LUA_Email:-$(_readaccountconf_mutable LUA_Email)}"
21 LUA_auth=$(printf "%s" "$LUA_Email:$LUA_Key" | _base64)
22
23 if [ -z "$LUA_Key" ] || [ -z "$LUA_Email" ]; then
24 LUA_Key=""
25 LUA_Email=""
26 _err "You don't specify luadns api key and email yet."
27 _err "Please create you key and try again."
28 return 1
29 fi
30
31 #save the api key and email to the account conf file.
32 _saveaccountconf_mutable LUA_Key "$LUA_Key"
33 _saveaccountconf_mutable LUA_Email "$LUA_Email"
34
35 _debug "First detect the root zone"
36 if ! _get_root "$fulldomain"; then
37 _err "invalid domain"
38 return 1
39 fi
40 _debug _domain_id "$_domain_id"
41 _debug _sub_domain "$_sub_domain"
42 _debug _domain "$_domain"
43
44 _info "Adding record"
45 if _LUA_rest POST "zones/$_domain_id/records" "{\"type\":\"TXT\",\"name\":\"$fulldomain.\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
46 if _contains "$response" "$fulldomain"; then
47 _info "Added"
48 #todo: check if the record takes effect
49 return 0
50 else
51 _err "Add txt record error."
52 return 1
53 fi
54 fi
55 }
56
57 #fulldomain
58 dns_lua_rm() {
59 fulldomain=$1
60 txtvalue=$2
61
62 LUA_Key="${LUA_Key:-$(_readaccountconf_mutable LUA_Key)}"
63 LUA_Email="${LUA_Email:-$(_readaccountconf_mutable LUA_Email)}"
64 LUA_auth=$(printf "%s" "$LUA_Email:$LUA_Key" | _base64)
65 _debug "First detect the root zone"
66 if ! _get_root "$fulldomain"; then
67 _err "invalid domain"
68 return 1
69 fi
70 _debug _domain_id "$_domain_id"
71 _debug _sub_domain "$_sub_domain"
72 _debug _domain "$_domain"
73
74 _debug "Getting txt records"
75 _LUA_rest GET "zones/${_domain_id}/records"
76
77 count=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | wc -l | tr -d " ")
78 _debug count "$count"
79 if [ "$count" = "0" ]; then
80 _info "Don't need to remove."
81 else
82 record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | _head_n 1 | cut -d: -f2 | cut -d, -f1)
83 _debug "record_id" "$record_id"
84 if [ -z "$record_id" ]; then
85 _err "Can not get record id to remove."
86 return 1
87 fi
88 if ! _LUA_rest DELETE "/zones/$_domain_id/records/$record_id"; then
89 _err "Delete record error."
90 return 1
91 fi
92 _contains "$response" "$record_id"
93 fi
94 }
95
96 #################### Private functions below ##################################
97 #_acme-challenge.www.domain.com
98 #returns
99 # _sub_domain=_acme-challenge.www
100 # _domain=domain.com
101 # _domain_id=sdjkglgdfewsdfg
102 _get_root() {
103 domain=$1
104 i=2
105 p=1
106 if ! _LUA_rest GET "zones"; then
107 return 1
108 fi
109 while true; do
110 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
111 _debug h "$h"
112 if [ -z "$h" ]; then
113 #not valid
114 return 1
115 fi
116
117 if _contains "$response" "\"name\":\"$h\""; then
118 _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$h\"" | cut -d : -f 2 | cut -d , -f 1)
119 _debug _domain_id "$_domain_id"
120 if [ "$_domain_id" ]; then
121 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
122 _domain="$h"
123 return 0
124 fi
125 return 1
126 fi
127 p=$i
128 i=$(_math "$i" + 1)
129 done
130 return 1
131 }
132
133 _LUA_rest() {
134 m=$1
135 ep="$2"
136 data="$3"
137 _debug "$ep"
138
139 export _H1="Accept: application/json"
140 export _H2="Authorization: Basic $LUA_auth"
141 if [ "$m" != "GET" ]; then
142 _debug data "$data"
143 response="$(_post "$data" "$LUA_Api/$ep" "" "$m")"
144 else
145 response="$(_get "$LUA_Api/$ep")"
146 fi
147
148 if [ "$?" != "0" ]; then
149 _err "error $ep"
150 return 1
151 fi
152 _debug2 response "$response"
153 return 0
154 }