]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_one.sh
Merge pull request #4658 from Justman10000/master
[mirror_acme.sh.git] / dnsapi / dns_one.sh
1 #!/usr/bin/env sh
2 # one.com ui wrapper for acme.sh
3
4 #
5 # export ONECOM_User="username"
6 # export ONECOM_Password="password"
7
8 dns_one_add() {
9 fulldomain=$1
10 txtvalue=$2
11
12 if ! _dns_one_login; then
13 _err "login failed"
14 return 1
15 fi
16
17 _debug "detect the root domain"
18 if ! _get_root "$fulldomain"; then
19 _err "root domain not found"
20 return 1
21 fi
22
23 subdomain="${_sub_domain}"
24 maindomain=${_domain}
25
26 _debug subdomain "$subdomain"
27 _debug maindomain "$maindomain"
28
29 #Check if the TXT exists
30 _dns_one_getrecord "TXT" "$subdomain" "$txtvalue"
31 if [ -n "$id" ]; then
32 _info "$(__green "Txt record with the same value found. Skip adding.")"
33 return 0
34 fi
35
36 _dns_one_addrecord "TXT" "$subdomain" "$txtvalue"
37 if [ -z "$id" ]; then
38 _err "Add TXT record error."
39 return 1
40 else
41 _info "$(__green "Added, OK ($id)")"
42 return 0
43 fi
44 }
45
46 dns_one_rm() {
47 fulldomain=$1
48 txtvalue=$2
49
50 if ! _dns_one_login; then
51 _err "login failed"
52 return 1
53 fi
54
55 _debug "detect the root domain"
56 if ! _get_root "$fulldomain"; then
57 _err "root domain not found"
58 return 1
59 fi
60
61 subdomain="${_sub_domain}"
62 maindomain=${_domain}
63
64 _debug subdomain "$subdomain"
65 _debug maindomain "$maindomain"
66
67 #Check if the TXT exists
68 _dns_one_getrecord "TXT" "$subdomain" "$txtvalue"
69 if [ -z "$id" ]; then
70 _err "Txt record not found."
71 return 1
72 fi
73
74 # delete entry
75 if _dns_one_delrecord "$id"; then
76 _info "$(__green Removed, OK)"
77 return 0
78 else
79 _err "Removing txt record error."
80 return 1
81 fi
82 }
83
84 #_acme-challenge.www.domain.com
85 #returns
86 # _sub_domain=_acme-challenge.www
87 # _domain=domain.com
88 _get_root() {
89 domain="$1"
90 i=1
91 p=1
92 while true; do
93 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
94
95 if [ -z "$h" ]; then
96 #not valid
97 return 1
98 fi
99
100 response="$(_get "https://www.one.com/admin/api/domains/$h/dns/custom_records")"
101
102 if ! _contains "$response" "CRMRST_000302"; then
103 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
104 _domain="$h"
105 return 0
106 fi
107 p=$i
108 i=$(_math "$i" + 1)
109 done
110 _err "Unable to parse this domain"
111 return 1
112 }
113
114 _dns_one_login() {
115
116 # get credentials
117 ONECOM_User="${ONECOM_User:-$(_readaccountconf_mutable ONECOM_User)}"
118 ONECOM_Password="${ONECOM_Password:-$(_readaccountconf_mutable ONECOM_Password)}"
119 if [ -z "$ONECOM_User" ] || [ -z "$ONECOM_Password" ]; then
120 ONECOM_User=""
121 ONECOM_Password=""
122 _err "You didn't specify a one.com username and password yet."
123 _err "Please create the key and try again."
124 return 1
125 fi
126
127 #save the api key and email to the account conf file.
128 _saveaccountconf_mutable ONECOM_User "$ONECOM_User"
129 _saveaccountconf_mutable ONECOM_Password "$ONECOM_Password"
130
131 # Login with user and password
132 postdata="loginDomain=true"
133 postdata="$postdata&displayUsername=$ONECOM_User"
134 postdata="$postdata&username=$ONECOM_User"
135 postdata="$postdata&targetDomain="
136 postdata="$postdata&password1=$ONECOM_Password"
137 postdata="$postdata&loginTarget="
138 #_debug postdata "$postdata"
139
140 response="$(_post "$postdata" "https://www.one.com/admin/login.do" "" "POST" "application/x-www-form-urlencoded")"
141 #_debug response "$response"
142
143 # Get SessionID
144 JSESSIONID="$(grep "OneSIDCrmAdmin" "$HTTP_HEADER" | grep "^[Ss]et-[Cc]ookie:" | _head_n 1 | _egrep_o 'OneSIDCrmAdmin=[^;]*;' | tr -d ';')"
145 _debug jsessionid "$JSESSIONID"
146
147 if [ -z "$JSESSIONID" ]; then
148 _err "error sessionid cookie not found"
149 return 1
150 fi
151
152 export _H1="Cookie: ${JSESSIONID}"
153
154 return 0
155 }
156
157 _dns_one_getrecord() {
158 type="$1"
159 name="$2"
160 value="$3"
161 if [ -z "$type" ]; then
162 type="TXT"
163 fi
164 if [ -z "$name" ]; then
165 _err "Record name is empty."
166 return 1
167 fi
168
169 response="$(_get "https://www.one.com/admin/api/domains/$maindomain/dns/custom_records")"
170 response="$(echo "$response" | _normalizeJson)"
171 _debug response "$response"
172
173 if [ -z "${value}" ]; then
174 id=$(printf -- "%s" "$response" | sed -n "s/.*{\"type\":\"dns_custom_records\",\"id\":\"\([^\"]*\)\",\"attributes\":{\"prefix\":\"${name}\",\"type\":\"${type}\",\"content\":\"[^\"]*\",\"priority\":0,\"ttl\":600}.*/\1/p")
175 response=$(printf -- "%s" "$response" | sed -n "s/.*{\"type\":\"dns_custom_records\",\"id\":\"[^\"]*\",\"attributes\":{\"prefix\":\"${name}\",\"type\":\"${type}\",\"content\":\"\([^\"]*\)\",\"priority\":0,\"ttl\":600}.*/\1/p")
176 else
177 id=$(printf -- "%s" "$response" | sed -n "s/.*{\"type\":\"dns_custom_records\",\"id\":\"\([^\"]*\)\",\"attributes\":{\"prefix\":\"${name}\",\"type\":\"${type}\",\"content\":\"${value}\",\"priority\":0,\"ttl\":600}.*/\1/p")
178 fi
179 if [ -z "$id" ]; then
180 return 1
181 fi
182 return 0
183 }
184
185 _dns_one_addrecord() {
186 type="$1"
187 name="$2"
188 value="$3"
189 if [ -z "$type" ]; then
190 type="TXT"
191 fi
192 if [ -z "$name" ]; then
193 _err "Record name is empty."
194 return 1
195 fi
196
197 postdata="{\"type\":\"dns_custom_records\",\"attributes\":{\"priority\":0,\"ttl\":600,\"type\":\"${type}\",\"prefix\":\"${name}\",\"content\":\"${value}\"}}"
198 _debug postdata "$postdata"
199 response="$(_post "$postdata" "https://www.one.com/admin/api/domains/$maindomain/dns/custom_records" "" "POST" "application/json")"
200 response="$(echo "$response" | _normalizeJson)"
201 _debug response "$response"
202
203 id=$(echo "$response" | sed -n "s/{\"result\":{\"data\":{\"type\":\"dns_custom_records\",\"id\":\"\([^\"]*\)\",\"attributes\":{\"prefix\":\"$subdomain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"priority\":0,\"ttl\":600}}},\"metadata\":null}/\1/p")
204
205 if [ -z "$id" ]; then
206 return 1
207 else
208 return 0
209 fi
210 }
211
212 _dns_one_delrecord() {
213 id="$1"
214 if [ -z "$id" ]; then
215 return 1
216 fi
217
218 response="$(_post "" "https://www.one.com/admin/api/domains/$maindomain/dns/custom_records/$id" "" "DELETE" "application/json")"
219 response="$(echo "$response" | _normalizeJson)"
220 _debug response "$response"
221
222 if [ "$response" = '{"result":null,"metadata":null}' ]; then
223 return 0
224 else
225 return 1
226 fi
227 }