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