]>
Commit | Line | Data |
---|---|---|
55787ff7 | 1 | #!/usr/bin/env sh |
f06c1e6c | 2 | |
1d9f76e2 | 3 | # bug reports to dev@1e.ca |
f06c1e6c | 4 | |
5 | # | |
6 | #LUA_Key="sdfsdfsdfljlbjkljlkjsdfoiwje" | |
7 | # | |
a43d6972 | 8 | #LUA_Email="user@luadns.net" |
f06c1e6c | 9 | |
10 | LUA_Api="https://api.luadns.com/v1" | |
f06c1e6c | 11 | |
f06c1e6c | 12 | ######## Public functions ##################### |
13 | ||
14 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
15 | dns_lua_add() { | |
16 | fulldomain=$1 | |
17 | txtvalue=$2 | |
4c2a3841 | 18 | |
f213215c | 19 | LUA_Key="${LUA_Key:-$(_readaccountconf_mutable LUA_Key)}" |
20 | LUA_Email="${LUA_Email:-$(_readaccountconf_mutable LUA_Email)}" | |
b00919c6 | 21 | LUA_auth=$(printf "%s" "$LUA_Email:$LUA_Key" | _base64) |
22 | ||
4c2a3841 | 23 | if [ -z "$LUA_Key" ] || [ -z "$LUA_Email" ]; then |
a8c61111 | 24 | LUA_Key="" |
25 | LUA_Email="" | |
f06c1e6c | 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 | |
4c2a3841 | 30 | |
f06c1e6c | 31 | #save the api key and email to the account conf file. |
f213215c | 32 | _saveaccountconf_mutable LUA_Key "$LUA_Key" |
33 | _saveaccountconf_mutable LUA_Email "$LUA_Email" | |
4c2a3841 | 34 | |
f06c1e6c | 35 | _debug "First detect the root zone" |
c7b16249 | 36 | if ! _get_root "$fulldomain"; then |
f06c1e6c | 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" | |
4c2a3841 | 43 | |
f213215c | 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" | |
f06c1e6c | 48 | #todo: check if the record takes effect |
4c2a3841 | 49 | return 0 |
f213215c | 50 | else |
51 | _err "Add txt record error." | |
52 | return 1 | |
f06c1e6c | 53 | fi |
f06c1e6c | 54 | fi |
4c2a3841 | 55 | } |
f06c1e6c | 56 | |
57 | #fulldomain | |
58 | dns_lua_rm() { | |
59 | fulldomain=$1 | |
8d53ec53 | 60 | txtvalue=$2 |
f213215c | 61 | |
62 | LUA_Key="${LUA_Key:-$(_readaccountconf_mutable LUA_Key)}" | |
63 | LUA_Email="${LUA_Email:-$(_readaccountconf_mutable LUA_Email)}" | |
b00919c6 | 64 | LUA_auth=$(printf "%s" "$LUA_Email:$LUA_Key" | _base64) |
8d53ec53 | 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" | |
f06c1e6c | 76 | |
8d53ec53 | 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 | |
f06c1e6c | 94 | } |
95 | ||
329174b6 | 96 | #################### Private functions below ################################## |
f06c1e6c | 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 | |
4c2a3841 | 106 | if ! _LUA_rest GET "zones"; then |
107 | return 1 | |
108 | fi | |
c7b16249 | 109 | while true; do |
110 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
5d833336 | 111 | _debug h "$h" |
4c2a3841 | 112 | if [ -z "$h" ]; then |
f06c1e6c | 113 | #not valid |
4c2a3841 | 114 | return 1 |
f06c1e6c | 115 | fi |
4c2a3841 | 116 | |
c7b16249 | 117 | if _contains "$response" "\"name\":\"$h\""; then |
e9f9f515 | 118 | _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$h\"" | cut -d : -f 2 | cut -d , -f 1) |
5d833336 | 119 | _debug _domain_id "$_domain_id" |
4c2a3841 | 120 | if [ "$_domain_id" ]; then |
c7b16249 | 121 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) |
122 | _domain="$h" | |
f06c1e6c | 123 | return 0 |
124 | fi | |
125 | return 1 | |
126 | fi | |
127 | p=$i | |
c7b16249 | 128 | i=$(_math "$i" + 1) |
f06c1e6c | 129 | done |
130 | return 1 | |
131 | } | |
132 | ||
133 | _LUA_rest() { | |
134 | m=$1 | |
135 | ep="$2" | |
136 | data="$3" | |
c7b16249 | 137 | _debug "$ep" |
4c2a3841 | 138 | |
3ca93f4a BB |
139 | export _H1="Accept: application/json" |
140 | export _H2="Authorization: Basic $LUA_auth" | |
8d53ec53 | 141 | if [ "$m" != "GET" ]; then |
f06c1e6c | 142 | _debug data "$data" |
e9f9f515 | 143 | response="$(_post "$data" "$LUA_Api/$ep" "" "$m")" |
f06c1e6c | 144 | else |
145 | response="$(_get "$LUA_Api/$ep")" | |
146 | fi | |
4c2a3841 | 147 | |
148 | if [ "$?" != "0" ]; then | |
f06c1e6c | 149 | _err "error $ep" |
150 | return 1 | |
151 | fi | |
152 | _debug2 response "$response" | |
153 | return 0 | |
154 | } |