]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_cloudns.sh
Merge pull request #4777 from acmesh-official/dev
[mirror_acme.sh.git] / dnsapi / dns_cloudns.sh
1 #!/usr/bin/env sh
2
3 # Author: Boyan Peychev <boyan at cloudns dot net>
4 # Repository: https://github.com/ClouDNS/acme.sh/
5 # Editor: I Komang Suryadana
6
7 #CLOUDNS_AUTH_ID=XXXXX
8 #CLOUDNS_SUB_AUTH_ID=XXXXX
9 #CLOUDNS_AUTH_PASSWORD="YYYYYYYYY"
10 CLOUDNS_API="https://api.cloudns.net"
11 DOMAIN_TYPE=
12 DOMAIN_MASTER=
13
14 ######## Public functions #####################
15
16 #Usage: dns_cloudns_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
17 dns_cloudns_add() {
18 _info "Using cloudns"
19
20 if ! _dns_cloudns_init_check; then
21 return 1
22 fi
23
24 zone="$(_dns_cloudns_get_zone_name "$1")"
25 if [ -z "$zone" ]; then
26 _err "Missing DNS zone at ClouDNS. Please log into your control panel and create the required DNS zone for the initial setup."
27 return 1
28 fi
29
30 host="$(echo "$1" | sed "s/\.$zone\$//")"
31 record=$2
32
33 _debug zone "$zone"
34 _debug host "$host"
35 _debug record "$record"
36
37 _info "Adding the TXT record for $1"
38 _dns_cloudns_http_api_call "dns/add-record.json" "domain-name=$zone&record-type=TXT&host=$host&record=$record&ttl=60"
39 if ! _contains "$response" "\"status\":\"Success\""; then
40 _err "Record cannot be added."
41 return 1
42 fi
43 _info "Added."
44
45 return 0
46 }
47
48 #Usage: dns_cloudns_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
49 dns_cloudns_rm() {
50 _info "Using cloudns"
51
52 if ! _dns_cloudns_init_check; then
53 return 1
54 fi
55
56 if [ -z "$zone" ]; then
57 zone="$(_dns_cloudns_get_zone_name "$1")"
58 if [ -z "$zone" ]; then
59 _err "Missing DNS zone at ClouDNS. Please log into your control panel and create the required DNS zone for the initial setup."
60 return 1
61 fi
62 fi
63
64 host="$(echo "$1" | sed "s/\.$zone\$//")"
65 record=$2
66
67 _dns_cloudns_get_zone_info "$zone"
68
69 _debug "Type" "$DOMAIN_TYPE"
70 _debug "Cloud Master" "$DOMAIN_MASTER"
71 if _contains "$DOMAIN_TYPE" "cloud"; then
72 zone=$DOMAIN_MASTER
73 fi
74 _debug "ZONE" "$zone"
75
76 _dns_cloudns_http_api_call "dns/records.json" "domain-name=$zone&host=$host&type=TXT"
77 if ! _contains "$response" "\"id\":"; then
78 return 1
79 fi
80
81 for i in $(echo "$response" | tr '{' "\n" | grep -- "$record"); do
82 record_id=$(echo "$i" | tr ',' "\n" | grep -E '^"id"' | sed -re 's/^\"id\"\:\"([0-9]+)\"$/\1/g')
83
84 if [ -n "$record_id" ]; then
85 _debug zone "$zone"
86 _debug host "$host"
87 _debug record "$record"
88 _debug record_id "$record_id"
89
90 _info "Deleting the TXT record for $1"
91 _dns_cloudns_http_api_call "dns/delete-record.json" "domain-name=$zone&record-id=$record_id"
92
93 if ! _contains "$response" "\"status\":\"Success\""; then
94 _err "The TXT record for $1 cannot be deleted."
95 else
96 _info "Deleted."
97 fi
98 fi
99 done
100
101 return 0
102 }
103
104 #################### Private functions below ##################################
105 _dns_cloudns_init_check() {
106 if [ -n "$CLOUDNS_INIT_CHECK_COMPLETED" ]; then
107 return 0
108 fi
109
110 CLOUDNS_AUTH_ID="${CLOUDNS_AUTH_ID:-$(_readaccountconf_mutable CLOUDNS_AUTH_ID)}"
111 CLOUDNS_SUB_AUTH_ID="${CLOUDNS_SUB_AUTH_ID:-$(_readaccountconf_mutable CLOUDNS_SUB_AUTH_ID)}"
112 CLOUDNS_AUTH_PASSWORD="${CLOUDNS_AUTH_PASSWORD:-$(_readaccountconf_mutable CLOUDNS_AUTH_PASSWORD)}"
113 if [ -z "$CLOUDNS_AUTH_ID$CLOUDNS_SUB_AUTH_ID" ] || [ -z "$CLOUDNS_AUTH_PASSWORD" ]; then
114 CLOUDNS_AUTH_ID=""
115 CLOUDNS_SUB_AUTH_ID=""
116 CLOUDNS_AUTH_PASSWORD=""
117 _err "You don't specify cloudns api id and password yet."
118 _err "Please create you id and password and try again."
119 return 1
120 fi
121
122 if [ -z "$CLOUDNS_AUTH_ID" ] && [ -z "$CLOUDNS_SUB_AUTH_ID" ]; then
123 _err "CLOUDNS_AUTH_ID or CLOUDNS_SUB_AUTH_ID is not configured"
124 return 1
125 fi
126
127 if [ -z "$CLOUDNS_AUTH_PASSWORD" ]; then
128 _err "CLOUDNS_AUTH_PASSWORD is not configured"
129 return 1
130 fi
131
132 _dns_cloudns_http_api_call "dns/login.json" ""
133
134 if ! _contains "$response" "\"status\":\"Success\""; then
135 _err "Invalid CLOUDNS_AUTH_ID or CLOUDNS_AUTH_PASSWORD. Please check your login credentials."
136 return 1
137 fi
138
139 # save the api id and password to the account conf file.
140 _saveaccountconf_mutable CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
141 _saveaccountconf_mutable CLOUDNS_SUB_AUTH_ID "$CLOUDNS_SUB_AUTH_ID"
142 _saveaccountconf_mutable CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
143
144 CLOUDNS_INIT_CHECK_COMPLETED=1
145
146 return 0
147 }
148
149 _dns_cloudns_get_zone_info() {
150 zone=$1
151 _dns_cloudns_http_api_call "dns/get-zone-info.json" "domain-name=$zone"
152 if ! _contains "$response" "\"status\":\"Failed\""; then
153 DOMAIN_TYPE=$(echo "$response" | _egrep_o '"type":"[^"]*"' | cut -d : -f 2 | tr -d '"')
154 if _contains "$DOMAIN_TYPE" "cloud"; then
155 DOMAIN_MASTER=$(echo "$response" | _egrep_o '"cloud-master":"[^"]*"' | cut -d : -f 2 | tr -d '"')
156 fi
157 fi
158 return 0
159 }
160
161 _dns_cloudns_get_zone_name() {
162 i=2
163 while true; do
164 zoneForCheck=$(printf "%s" "$1" | cut -d . -f $i-100)
165
166 if [ -z "$zoneForCheck" ]; then
167 return 1
168 fi
169
170 _debug zoneForCheck "$zoneForCheck"
171
172 _dns_cloudns_http_api_call "dns/get-zone-info.json" "domain-name=$zoneForCheck"
173
174 if ! _contains "$response" "\"status\":\"Failed\""; then
175 echo "$zoneForCheck"
176 return 0
177 fi
178
179 i=$(_math "$i" + 1)
180 done
181 return 1
182 }
183
184 _dns_cloudns_http_api_call() {
185 method=$1
186
187 _debug CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
188 _debug CLOUDNS_SUB_AUTH_ID "$CLOUDNS_SUB_AUTH_ID"
189 _debug CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
190
191 if [ -n "$CLOUDNS_SUB_AUTH_ID" ]; then
192 auth_user="sub-auth-id=$CLOUDNS_SUB_AUTH_ID"
193 else
194 auth_user="auth-id=$CLOUDNS_AUTH_ID"
195 fi
196
197 if [ -z "$2" ]; then
198 data="$auth_user&auth-password=$CLOUDNS_AUTH_PASSWORD"
199 else
200 data="$auth_user&auth-password=$CLOUDNS_AUTH_PASSWORD&$2"
201 fi
202
203 response="$(_get "$CLOUDNS_API/$method?$data")"
204
205 _debug response "$response"
206
207 return 0
208 }