]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_unoeuro.sh
fix #2830 Autorization segment typo fixed
[mirror_acme.sh.git] / dnsapi / dns_unoeuro.sh
1 #!/usr/bin/env sh
2
3 #
4 #UNO_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
5 #
6 #UNO_User="UExxxxxx"
7
8 Uno_Api="https://api.unoeuro.com/1"
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
28 _err "It seems that the UNO_User=$UNO_User is not a valid username."
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 _info "Adding record"
54
55 if _uno_rest POST "my/products/$h/dns/records" "{\"name\":\"$fulldomain\",\"type\":\"TXT\",\"data\":\"$txtvalue\",\"ttl\":120,\"priority\":0}"; then
56 if _contains "$response" "\"status\": 200" >/dev/null; then
57 _info "Added, OK"
58 return 0
59 else
60 _err "Add txt record error."
61 return 1
62 fi
63 fi
64 }
65
66 #fulldomain txtvalue
67 dns_unoeuro_rm() {
68 fulldomain=$1
69 txtvalue=$2
70
71 UNO_Key="${UNO_Key:-$(_readaccountconf_mutable UNO_Key)}"
72 UNO_User="${UNO_User:-$(_readaccountconf_mutable UNO_User)}"
73 if [ -z "$UNO_Key" ] || [ -z "$UNO_User" ]; then
74 UNO_Key=""
75 UNO_User=""
76 _err "You haven't specified a UnoEuro api key and account yet."
77 _err "Please create your key and try again."
78 return 1
79 fi
80
81 if ! _contains "$UNO_User" "UE"; then
82 _err "It seems that the UNO_User=$UNO_User is not a valid username."
83 _err "Please check and retry."
84 return 1
85 fi
86
87 _debug "First detect the root zone"
88 if ! _get_root "$fulldomain"; then
89 _err "invalid domain"
90 return 1
91 fi
92 _debug _domain_id "$_domain_id"
93 _debug _sub_domain "$_sub_domain"
94 _debug _domain "$_domain"
95
96 _debug "Getting txt records"
97 _uno_rest GET "my/products/$h/dns/records"
98
99 if ! _contains "$response" "\"status\": 200"; then
100 _err "Error"
101 return 1
102 fi
103
104 if ! _contains "$response" "$_sub_domain"; then
105 _info "Don't need to remove."
106 else
107 for record_line_number in $(echo "$response" | grep -n "$_sub_domain" | cut -d : -f 1); do
108 record_line_number=$(_math "$record_line_number" - 1)
109 _debug "record_line_number" "$record_line_number"
110 record_id=$(echo "$response" | _head_n "$record_line_number" | _tail_n 1 1 | _egrep_o "[0-9]{1,}")
111 _debug "record_id" "$record_id"
112
113 if [ -z "$record_id" ]; then
114 _err "Can not get record id to remove."
115 return 1
116 fi
117
118 if ! _uno_rest DELETE "my/products/$h/dns/records/$record_id"; then
119 _err "Delete record error."
120 return 1
121 fi
122 _contains "$response" "\"status\": 200"
123 done
124 fi
125 }
126
127 #################### Private functions below ##################################
128 #_acme-challenge.www.domain.com
129 #returns
130 # _sub_domain=_acme-challenge.www
131 # _domain=domain.com
132 # _domain_id=sdjkglgdfewsdfg
133 _get_root() {
134 domain=$1
135 i=2
136 p=1
137 while true; do
138 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
139 _debug h "$h"
140 if [ -z "$h" ]; then
141 #not valid
142 return 1
143 fi
144
145 if ! _uno_rest GET "my/products/$h/dns/records"; then
146 return 1
147 fi
148
149 if _contains "$response" "\"status\": 200"; then
150 _domain_id=$h
151 if [ "$_domain_id" ]; then
152 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
153 _domain=$h
154 return 0
155 fi
156 return 1
157 fi
158 p=$i
159 i=$(_math "$i" + 1)
160 done
161 return 1
162 }
163
164 _uno_rest() {
165 m=$1
166 ep="$2"
167 data="$3"
168 _debug "$ep"
169
170 export _H1="Content-Type: application/json"
171
172 if [ "$m" != "GET" ]; then
173 _debug data "$data"
174 response="$(_post "$data" "$Uno_Api/$UNO_User/$UNO_Key/$ep" "" "$m")"
175 else
176 response="$(_get "$Uno_Api/$UNO_User/$UNO_Key/$ep")"
177 fi
178
179 if [ "$?" != "0" ]; then
180 _err "error $ep"
181 return 1
182 fi
183 _debug2 response "$response"
184 return 0
185 }