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