]>
Commit | Line | Data |
---|---|---|
0ca31410 A |
1 | #!/usr/bin/env sh |
2 | ||
3 | # | |
4 | #Uno_Key="sdfsdfsdfljlbjkljlkjsdfoiwje" | |
5 | # | |
6 | #Uno_User="UExxxxxx" | |
7 | ||
4a9f607d | 8 | Uno_Api="https://api.unoeuro.com/1" |
0ca31410 A |
9 | |
10 | ######## Public functions ##################### | |
11 | ||
12 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
13 | dns_unoeuro_add() { | |
14 | fulldomain=$1 | |
15 | txtvalue=$2 | |
16 | ||
17 | Uno_Key="${Uno_Key:-$(_readaccountconf_mutable Uno_Key)}" | |
18 | Uno_User="${Uno_User:-$(_readaccountconf_mutable Uno_User)}" | |
19 | if [ -z "$Uno_Key" ] || [ -z "$Uno_User" ]; then | |
20 | Uno_Key="" | |
21 | Uno_User="" | |
22 | _err "You haven't specified a UnoEuro api key and account yet." | |
23 | _err "Please create your key and try again." | |
24 | return 1 | |
25 | fi | |
26 | ||
27 | if ! _contains "$Uno_User" "UE"; then | |
4a9f607d | 28 | _err "It seems that the Uno_User=$Uno_User is not a valid username." |
0ca31410 A |
29 | _err "Please check and retry." |
30 | return 1 | |
31 | fi | |
32 | ||
33 | #save the api key and email to the account conf file. | |
34 | _saveaccountconf_mutable Uno_Key "$Uno_Key" | |
35 | _saveaccountconf_mutable Uno_User "$Uno_User" | |
36 | ||
37 | _debug "First detect the root zone" | |
38 | if ! _get_root "$fulldomain"; then | |
39 | _err "invalid domain" | |
40 | return 1 | |
41 | fi | |
42 | _debug _domain_id "$_domain_id" | |
43 | _debug _sub_domain "$_sub_domain" | |
44 | _debug _domain "$_domain" | |
45 | ||
46 | _debug "Getting txt records" | |
47 | _uno_rest GET "my/products/$h/dns/records" | |
48 | ||
49 | if ! _contains "$response" "\"status\": 200" >/dev/null; then | |
50 | _err "Error" | |
51 | return 1 | |
52 | fi | |
53 | ||
54 | if ! _contains "$response" "$_sub_domain" >/dev/null; then | |
55 | _info "Adding record" | |
db304355 | 56 | |
0ca31410 A |
57 | if _uno_rest POST "my/products/$h/dns/records" "{\"name\":\"$fulldomain\",\"type\":\"TXT\",\"data\":\"$txtvalue\",\"ttl\":120}"; then |
58 | if _contains "$response" "\"status\": 200" >/dev/null; then | |
59 | _info "Added, OK" | |
60 | return 0 | |
61 | else | |
62 | _err "Add txt record error." | |
63 | return 1 | |
64 | fi | |
65 | fi | |
66 | _err "Add txt record error." | |
67 | else | |
68 | _info "Updating record" | |
db304355 | 69 | record_line_number=$(echo "$response" | grep -n "$_sub_domain" | cut -d : -f 1) |
dbc3ad13 | 70 | record_line_number=$(_math "$record_line_number" - 1) |
db304355 | 71 | record_id=$(echo "$response" | _head_n "$record_line_number" | _tail_n 1 1 | _egrep_o "[0-9]{1,}") |
0ca31410 A |
72 | _debug "record_id" "$record_id" |
73 | ||
74 | _uno_rest PUT "my/products/$h/dns/records/$record_id" "{\"name\":\"$fulldomain\",\"type\":\"TXT\",\"data\":\"$txtvalue\",\"ttl\":120}" | |
75 | if _contains "$response" "\"status\": 200" >/dev/null; then | |
76 | _info "Updated, OK" | |
77 | return 0 | |
78 | fi | |
79 | _err "Update error" | |
80 | return 1 | |
81 | fi | |
0ca31410 A |
82 | } |
83 | ||
84 | #fulldomain txtvalue | |
85 | dns_unoeuro_rm() { | |
86 | fulldomain=$1 | |
87 | txtvalue=$2 | |
88 | ||
89 | Uno_Key="${Uno_Key:-$(_readaccountconf_mutable Uno_Key)}" | |
90 | Uno_User="${Uno_User:-$(_readaccountconf_mutable Uno_User)}" | |
91 | if [ -z "$Uno_Key" ] || [ -z "$Uno_User" ]; then | |
92 | Uno_Key="" | |
93 | Uno_User="" | |
94 | _err "You haven't specified a UnoEuro api key and account yet." | |
95 | _err "Please create your key and try again." | |
96 | return 1 | |
97 | fi | |
98 | ||
4a9f607d J |
99 | if ! _contains "$Uno_User" "UE"; then |
100 | _err "It seems that the Uno_User=$Uno_User is not a valid username." | |
101 | _err "Please check and retry." | |
102 | return 1 | |
103 | fi | |
104 | ||
0ca31410 A |
105 | _debug "First detect the root zone" |
106 | if ! _get_root "$fulldomain"; then | |
107 | _err "invalid domain" | |
108 | return 1 | |
109 | fi | |
110 | _debug _domain_id "$_domain_id" | |
111 | _debug _sub_domain "$_sub_domain" | |
112 | _debug _domain "$_domain" | |
113 | ||
114 | _debug "Getting txt records" | |
115 | _uno_rest GET "my/products/$h/dns/records" | |
116 | ||
f763e1ed | 117 | if ! _contains "$response" "\"status\": 200"; then |
0ca31410 A |
118 | _err "Error" |
119 | return 1 | |
120 | fi | |
121 | ||
f763e1ed | 122 | if ! _contains "$response" "$_sub_domain"; then |
0ca31410 A |
123 | _info "Don't need to remove." |
124 | else | |
db304355 | 125 | record_line_number=$(echo "$response" | grep -n "$_sub_domain" | cut -d : -f 1) |
dbc3ad13 | 126 | record_line_number=$(_math "$record_line_number" - 1) |
db304355 | 127 | record_id=$(echo "$response" | _head_n "$record_line_number" | _tail_n 1 1 | _egrep_o "[0-9]{1,}") |
0ca31410 A |
128 | _debug "record_id" "$record_id" |
129 | ||
130 | if [ -z "$record_id" ]; then | |
131 | _err "Can not get record id to remove." | |
132 | return 1 | |
133 | fi | |
134 | ||
135 | if ! _uno_rest DELETE "my/products/$h/dns/records/$record_id"; then | |
136 | _err "Delete record error." | |
137 | return 1 | |
138 | fi | |
139 | _contains "$response" "\"status\": 200" | |
140 | fi | |
141 | ||
142 | } | |
143 | ||
144 | #################### Private functions below ################################## | |
145 | #_acme-challenge.www.domain.com | |
146 | #returns | |
147 | # _sub_domain=_acme-challenge.www | |
148 | # _domain=domain.com | |
149 | # _domain_id=sdjkglgdfewsdfg | |
150 | _get_root() { | |
151 | domain=$1 | |
152 | i=2 | |
153 | p=1 | |
154 | while true; do | |
155 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
156 | _debug h "$h" | |
157 | if [ -z "$h" ]; then | |
158 | #not valid | |
159 | return 1 | |
160 | fi | |
161 | ||
162 | if ! _uno_rest GET "my/products/$h/dns/records"; then | |
163 | return 1 | |
164 | fi | |
165 | ||
f763e1ed | 166 | if _contains "$response" "\"status\": 200"; then |
0ca31410 A |
167 | _domain_id=$h |
168 | if [ "$_domain_id" ]; then | |
169 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
170 | _domain=$h | |
171 | return 0 | |
172 | fi | |
173 | return 1 | |
174 | fi | |
175 | p=$i | |
176 | i=$(_math "$i" + 1) | |
177 | done | |
178 | return 1 | |
179 | } | |
180 | ||
181 | _uno_rest() { | |
182 | m=$1 | |
183 | ep="$2" | |
184 | data="$3" | |
185 | _debug "$ep" | |
186 | ||
0ca31410 A |
187 | export _H1="Content-Type: application/json" |
188 | ||
189 | if [ "$m" != "GET" ]; then | |
190 | _debug data "$data" | |
4a9f607d | 191 | response="$(_post "$data" "$Uno_Api/$Uno_User/$Uno_Key/$ep" "" "$m")" |
0ca31410 | 192 | else |
4a9f607d | 193 | response="$(_get "$Uno_Api/$Uno_User/$Uno_Key/$ep")" |
0ca31410 A |
194 | fi |
195 | ||
196 | if [ "$?" != "0" ]; then | |
197 | _err "error $ep" | |
198 | return 1 | |
199 | fi | |
200 | _debug2 response "$response" | |
201 | return 0 | |
202 | } |