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