]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_one.sh
Merge pull request #3734 from acmesh-official/dev
[mirror_acme.sh.git] / dnsapi / dns_one.sh
CommitLineData
9ff6d6e7 1#!/usr/bin/env sh
9ff6d6e7 2# one.com ui wrapper for acme.sh
cfbc2948 3
19c43451 4#
e340593a 5# export ONECOM_User="username"
6# export ONECOM_Password="password"
cfbc2948 7
9ff6d6e7 8dns_one_add() {
68b42a00 9 fulldomain=$1
9ff6d6e7 10 txtvalue=$2
937d5b54 11
a3089a71 12 if ! _dns_one_login; then
13 _err "login failed"
14 return 1
15 fi
16
17 _debug "detect the root domain"
68b42a00 18 if ! _get_root "$fulldomain"; then
a3089a71 19 _err "root domain not found"
68b42a00 20 return 1
21 fi
da7b1fb0 22
5fac282e 23 subdomain="${_sub_domain}"
24 maindomain=${_domain}
da7b1fb0 25
5fac282e 26 _debug subdomain "$subdomain"
27 _debug maindomain "$maindomain"
da7b1fb0 28
5fac282e 29 #Check if the TXT exists
30 _dns_one_getrecord "TXT" "$subdomain" "$txtvalue"
da7b1fb0 31 if [ -n "$id" ]; then
5fac282e 32 _info "$(__green "Txt record with the same value found. Skip adding.")"
33 return 0
34 fi
da7b1fb0 35
5fac282e 36 _dns_one_addrecord "TXT" "$subdomain" "$txtvalue"
9ff6d6e7 37 if [ -z "$id" ]; then
da7b1fb0 38 _err "Add TXT record error."
9ff6d6e7 39 return 1
40 else
5fac282e 41 _info "$(__green "Added, OK ($id)")"
9ff6d6e7 42 return 0
43 fi
9ff6d6e7 44}
45
46dns_one_rm() {
68b42a00 47 fulldomain=$1
9ff6d6e7 48 txtvalue=$2
937d5b54 49
a3089a71 50 if ! _dns_one_login; then
51 _err "login failed"
52 return 1
53 fi
54
55 _debug "detect the root domain"
68b42a00 56 if ! _get_root "$fulldomain"; then
a3089a71 57 _err "root domain not found"
68b42a00 58 return 1
59 fi
937d5b54 60
5fac282e 61 subdomain="${_sub_domain}"
62 maindomain=${_domain}
da7b1fb0 63
5fac282e 64 _debug subdomain "$subdomain"
65 _debug maindomain "$maindomain"
da7b1fb0 66
5fac282e 67 #Check if the TXT exists
68 _dns_one_getrecord "TXT" "$subdomain" "$txtvalue"
9ff6d6e7 69 if [ -z "$id" ]; then
70 _err "Txt record not found."
71 return 1
72 fi
da7b1fb0 73
9ff6d6e7 74 # delete entry
5fac282e 75 if _dns_one_delrecord "$id"; then
da7b1fb0 76 _info "$(__green Removed, OK)"
77 return 0
ed3f2646 78 else
da7b1fb0 79 _err "Removing txt record error."
80 return 1
9ff6d6e7 81 fi
0bb746ba 82}
68b42a00 83
84#_acme-challenge.www.domain.com
85#returns
86# _sub_domain=_acme-challenge.www
87# _domain=domain.com
88_get_root() {
1a5279bd 89 domain="$1"
cfbc2948 90 i=1
68b42a00 91 p=1
92 while true; do
93 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
1a5279bd 94
68b42a00 95 if [ -z "$h" ]; then
96 #not valid
97 return 1
98 fi
1a5279bd 99
a3089a71 100 response="$(_get "https://www.one.com/admin/api/domains/$h/dns/custom_records")"
1a5279bd 101
89e73594 102 if ! _contains "$response" "CRMRST_000302"; then
68b42a00 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}
a3089a71 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"
1a5279bd 139
a3089a71 140 response="$(_post "$postdata" "https://www.one.com/admin/login.do" "" "POST" "application/x-www-form-urlencoded")"
141 #_debug response "$response"
1a5279bd 142
a3089a71 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"
1a5279bd 146
a3089a71 147 if [ -z "$JSESSIONID" ]; then
148 _err "error sessionid cookie not found"
149 return 1
150 fi
1a5279bd 151
a3089a71 152 export _H1="Cookie: ${JSESSIONID}"
1a5279bd 153
a3089a71 154 return 0
937d5b54 155}
5fac282e 156
157_dns_one_getrecord() {
158 type="$1"
159 name="$2"
160 value="$3"
161 if [ -z "$type" ]; then
da7b1fb0 162 type="TXT"
5fac282e 163 fi
164 if [ -z "$name" ]; then
da7b1fb0 165 _err "Record name is empty."
166 return 1
5fac282e 167 fi
da7b1fb0 168
5fac282e 169 response="$(_get "https://www.one.com/admin/api/domains/$maindomain/dns/custom_records")"
170 response="$(echo "$response" | _normalizeJson)"
171 _debug response "$response"
da7b1fb0 172
5fac282e 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
5fac282e 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
da7b1fb0 190 type="TXT"
5fac282e 191 fi
192 if [ -z "$name" ]; then
da7b1fb0 193 _err "Record name is empty."
194 return 1
5fac282e 195 fi
da7b1fb0 196
5fac282e 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
da7b1fb0 215 return 1
5fac282e 216 fi
da7b1fb0 217
5fac282e 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
da7b1fb0 223 return 0
5fac282e 224 else
da7b1fb0 225 return 1
5fac282e 226 fi
227}