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