]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_cloudns.sh
Merge pull request #4542 from alexleigh/master
[mirror_acme.sh.git] / dnsapi / dns_cloudns.sh
CommitLineData
3b7fbcd0 1#!/usr/bin/env sh
2
5ffca2d1 3# Author: Boyan Peychev <boyan at cloudns dot net>
4# Repository: https://github.com/ClouDNS/acme.sh/
bda454fe 5# Editor: I Komang Suryadana
5ffca2d1 6
3b7fbcd0 7#CLOUDNS_AUTH_ID=XXXXX
bab4f691 8#CLOUDNS_SUB_AUTH_ID=XXXXX
3b7fbcd0 9#CLOUDNS_AUTH_PASSWORD="YYYYYYYYY"
10CLOUDNS_API="https://api.cloudns.net"
bda454fe
KS
11DOMAIN_TYPE=
12DOMAIN_MASTER=
3b7fbcd0 13
14######## Public functions #####################
15
16#Usage: dns_cloudns_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
0dd6377f 17dns_cloudns_add() {
c73c33f9 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
c73c33f9 32
33 _debug zone "$zone"
34 _debug host "$host"
35 _debug record "$record"
c73c33f9 36
41e3ecad
BP
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
c73c33f9 42 fi
41e3ecad 43 _info "Added."
c73c33f9 44
45 return 0
3b7fbcd0 46}
47
48#Usage: dns_cloudns_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
0dd6377f 49dns_cloudns_rm() {
c73c33f9 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
c73c33f9 66
bda454fe
KS
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
5309afc3
BP
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
c73c33f9 80
28355335
BP
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')
5309afc3 83
fe4111a9 84 if [ -n "$record_id" ]; then
5309afc3
BP
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
c73c33f9 98 fi
5309afc3
BP
99 done
100
c73c33f9 101 return 0
3b7fbcd0 102}
103
104#################### Private functions below ##################################
0dd6377f 105_dns_cloudns_init_check() {
fe4111a9 106 if [ -n "$CLOUDNS_INIT_CHECK_COMPLETED" ]; then
c73c33f9 107 return 0
108 fi
109
110 CLOUDNS_AUTH_ID="${CLOUDNS_AUTH_ID:-$(_readaccountconf_mutable CLOUDNS_AUTH_ID)}"
212d0f24 111 CLOUDNS_SUB_AUTH_ID="${CLOUDNS_SUB_AUTH_ID:-$(_readaccountconf_mutable CLOUDNS_SUB_AUTH_ID)}"
c73c33f9 112 CLOUDNS_AUTH_PASSWORD="${CLOUDNS_AUTH_PASSWORD:-$(_readaccountconf_mutable CLOUDNS_AUTH_PASSWORD)}"
212d0f24 113 if [ -z "$CLOUDNS_AUTH_ID$CLOUDNS_SUB_AUTH_ID" ] || [ -z "$CLOUDNS_AUTH_PASSWORD" ]; then
c73c33f9 114 CLOUDNS_AUTH_ID=""
212d0f24 115 CLOUDNS_SUB_AUTH_ID=""
c73c33f9 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
212d0f24
DLN
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"
c73c33f9 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
5309afc3 139 # save the api id and password to the account conf file.
c73c33f9 140 _saveaccountconf_mutable CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
212d0f24 141 _saveaccountconf_mutable CLOUDNS_SUB_AUTH_ID "$CLOUDNS_SUB_AUTH_ID"
c73c33f9 142 _saveaccountconf_mutable CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
143
144 CLOUDNS_INIT_CHECK_COMPLETED=1
145
146 return 0
3b7fbcd0 147}
148
bda454fe
KS
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
0dd6377f 161_dns_cloudns_get_zone_name() {
c73c33f9 162 i=2
163 while true; do
164 zoneForCheck=$(printf "%s" "$1" | cut -d . -f $i-100)
3b7fbcd0 165
c73c33f9 166 if [ -z "$zoneForCheck" ]; then
167 return 1
168 fi
3b7fbcd0 169
c73c33f9 170 _debug zoneForCheck "$zoneForCheck"
3b7fbcd0 171
c73c33f9 172 _dns_cloudns_http_api_call "dns/get-zone-info.json" "domain-name=$zoneForCheck"
3b7fbcd0 173
c73c33f9 174 if ! _contains "$response" "\"status\":\"Failed\""; then
175 echo "$zoneForCheck"
176 return 0
177 fi
3b7fbcd0 178
c73c33f9 179 i=$(_math "$i" + 1)
180 done
181 return 1
3b7fbcd0 182}
183
0dd6377f 184_dns_cloudns_http_api_call() {
c73c33f9 185 method=$1
3b7fbcd0 186
c73c33f9 187 _debug CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
212d0f24 188 _debug CLOUDNS_SUB_AUTH_ID "$CLOUDNS_SUB_AUTH_ID"
c73c33f9 189 _debug CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
3b7fbcd0 190
fe4111a9 191 if [ -n "$CLOUDNS_SUB_AUTH_ID" ]; then
212d0f24
DLN
192 auth_user="sub-auth-id=$CLOUDNS_SUB_AUTH_ID"
193 else
194 auth_user="auth-id=$CLOUDNS_AUTH_ID"
bab4f691 195 fi
212d0f24 196
c73c33f9 197 if [ -z "$2" ]; then
212d0f24 198 data="$auth_user&auth-password=$CLOUDNS_AUTH_PASSWORD"
c73c33f9 199 else
212d0f24 200 data="$auth_user&auth-password=$CLOUDNS_AUTH_PASSWORD&$2"
c73c33f9 201 fi
3b7fbcd0 202
c73c33f9 203 response="$(_get "$CLOUDNS_API/$method?$data")"
3b7fbcd0 204
5309afc3 205 _debug response "$response"
3b7fbcd0 206
c73c33f9 207 return 0
f881d6c4 208}