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