]>
Commit | Line | Data |
---|---|---|
662df85e | 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" | |
a0636d5a | 11 | LUA_auth=$(printf "%s" "$LUA_Email:$LUA_Key" | _base64) |
f06c1e6c | 12 | |
f06c1e6c | 13 | ######## Public functions ##################### |
14 | ||
15 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
16 | dns_lua_add() { | |
17 | fulldomain=$1 | |
18 | txtvalue=$2 | |
4c2a3841 | 19 | |
20 | if [ -z "$LUA_Key" ] || [ -z "$LUA_Email" ]; then | |
a8c61111 | 21 | LUA_Key="" |
22 | LUA_Email="" | |
f06c1e6c | 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 | |
4c2a3841 | 27 | |
f06c1e6c | 28 | #save the api key and email to the account conf file. |
29 | _saveaccountconf LUA_Key "$LUA_Key" | |
30 | _saveaccountconf LUA_Email "$LUA_Email" | |
4c2a3841 | 31 | |
f06c1e6c | 32 | _debug "First detect the root zone" |
c7b16249 | 33 | if ! _get_root "$fulldomain"; then |
f06c1e6c | 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" | |
4c2a3841 | 40 | |
f06c1e6c | 41 | _debug "Getting txt records" |
42 | _LUA_rest GET "zones/${_domain_id}/records" | |
4c2a3841 | 43 | |
a0636d5a | 44 | if ! _contains "$response" "\"id\":"; then |
f06c1e6c | 45 | _err "Error" |
46 | return 1 | |
47 | fi | |
4c2a3841 | 48 | |
53fa16d3 | 49 | count=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | wc -l | tr -d " ") |
f06c1e6c | 50 | _debug count "$count" |
4c2a3841 | 51 | if [ "$count" = "0" ]; then |
f06c1e6c | 52 | _info "Adding record" |
4c2a3841 | 53 | if _LUA_rest POST "zones/$_domain_id/records" "{\"type\":\"TXT\",\"name\":\"$fulldomain.\",\"content\":\"$txtvalue\",\"ttl\":120}"; then |
5d833336 | 54 | if _contains "$response" "$fulldomain"; then |
f06c1e6c | 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" | |
ab5c1b0a | 66 | record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | _head_n 1 | cut -d: -f2 | cut -d, -f1) |
c7b16249 | 67 | _debug "record_id" "$record_id" |
4c2a3841 | 68 | |
d78ba322 | 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}" |
1476a9ec | 70 | if [ "$?" = "0" ] && _contains "$response" "updated_at"; then |
f06c1e6c | 71 | _info "Updated!" |
72 | #todo: check if the record takes effect | |
4c2a3841 | 73 | return 0 |
f06c1e6c | 74 | fi |
75 | _err "Update error" | |
76 | return 1 | |
77 | fi | |
f06c1e6c | 78 | |
4c2a3841 | 79 | } |
f06c1e6c | 80 | |
81 | #fulldomain | |
82 | dns_lua_rm() { | |
83 | fulldomain=$1 | |
8d53ec53 | 84 | txtvalue=$2 |
85 | _debug "First detect the root zone" | |
86 | if ! _get_root "$fulldomain"; then | |
87 | _err "invalid domain" | |
88 | return 1 | |
89 | fi | |
90 | _debug _domain_id "$_domain_id" | |
91 | _debug _sub_domain "$_sub_domain" | |
92 | _debug _domain "$_domain" | |
93 | ||
94 | _debug "Getting txt records" | |
95 | _LUA_rest GET "zones/${_domain_id}/records" | |
f06c1e6c | 96 | |
8d53ec53 | 97 | count=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | wc -l | tr -d " ") |
98 | _debug count "$count" | |
99 | if [ "$count" = "0" ]; then | |
100 | _info "Don't need to remove." | |
101 | else | |
102 | record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | _head_n 1 | cut -d: -f2 | cut -d, -f1) | |
103 | _debug "record_id" "$record_id" | |
104 | if [ -z "$record_id" ]; then | |
105 | _err "Can not get record id to remove." | |
106 | return 1 | |
107 | fi | |
108 | if ! _LUA_rest DELETE "/zones/$_domain_id/records/$record_id"; then | |
109 | _err "Delete record error." | |
110 | return 1 | |
111 | fi | |
112 | _contains "$response" "$record_id" | |
113 | fi | |
f06c1e6c | 114 | } |
115 | ||
329174b6 | 116 | #################### Private functions below ################################## |
f06c1e6c | 117 | #_acme-challenge.www.domain.com |
118 | #returns | |
119 | # _sub_domain=_acme-challenge.www | |
120 | # _domain=domain.com | |
121 | # _domain_id=sdjkglgdfewsdfg | |
122 | _get_root() { | |
123 | domain=$1 | |
124 | i=2 | |
125 | p=1 | |
4c2a3841 | 126 | if ! _LUA_rest GET "zones"; then |
127 | return 1 | |
128 | fi | |
c7b16249 | 129 | while true; do |
130 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
5d833336 | 131 | _debug h "$h" |
4c2a3841 | 132 | if [ -z "$h" ]; then |
f06c1e6c | 133 | #not valid |
4c2a3841 | 134 | return 1 |
f06c1e6c | 135 | fi |
4c2a3841 | 136 | |
c7b16249 | 137 | if _contains "$response" "\"name\":\"$h\""; then |
e9f9f515 | 138 | _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$h\"" | cut -d : -f 2 | cut -d , -f 1) |
5d833336 | 139 | _debug _domain_id "$_domain_id" |
4c2a3841 | 140 | if [ "$_domain_id" ]; then |
c7b16249 | 141 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) |
142 | _domain="$h" | |
f06c1e6c | 143 | return 0 |
144 | fi | |
145 | return 1 | |
146 | fi | |
147 | p=$i | |
c7b16249 | 148 | i=$(_math "$i" + 1) |
f06c1e6c | 149 | done |
150 | return 1 | |
151 | } | |
152 | ||
153 | _LUA_rest() { | |
154 | m=$1 | |
155 | ep="$2" | |
156 | data="$3" | |
c7b16249 | 157 | _debug "$ep" |
4c2a3841 | 158 | |
3ca93f4a BB |
159 | export _H1="Accept: application/json" |
160 | export _H2="Authorization: Basic $LUA_auth" | |
8d53ec53 | 161 | if [ "$m" != "GET" ]; then |
f06c1e6c | 162 | _debug data "$data" |
e9f9f515 | 163 | response="$(_post "$data" "$LUA_Api/$ep" "" "$m")" |
f06c1e6c | 164 | else |
165 | response="$(_get "$LUA_Api/$ep")" | |
166 | fi | |
4c2a3841 | 167 | |
168 | if [ "$?" != "0" ]; then | |
f06c1e6c | 169 | _err "error $ep" |
170 | return 1 | |
171 | fi | |
172 | _debug2 response "$response" | |
173 | return 0 | |
174 | } |