]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_cloudns.sh
Merge pull request #2047 from Neilpang/dev
[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/
5
3b7fbcd0 6#CLOUDNS_AUTH_ID=XXXXX
bab4f691 7#CLOUDNS_SUB_AUTH_ID=XXXXX
3b7fbcd0 8#CLOUDNS_AUTH_PASSWORD="YYYYYYYYY"
9CLOUDNS_API="https://api.cloudns.net"
10
11######## Public functions #####################
12
13#Usage: dns_cloudns_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
0dd6377f 14dns_cloudns_add() {
c73c33f9 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
c73c33f9 29
30 _debug zone "$zone"
31 _debug host "$host"
32 _debug record "$record"
c73c33f9 33
41e3ecad
BP
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
c73c33f9 39 fi
41e3ecad 40 _info "Added."
c73c33f9 41
42 return 0
3b7fbcd0 43}
44
45#Usage: dns_cloudns_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
0dd6377f 46dns_cloudns_rm() {
c73c33f9 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
c73c33f9 63
5309afc3
BP
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
c73c33f9 68
28355335
BP
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')
5309afc3
BP
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
c73c33f9 86 fi
5309afc3
BP
87 done
88
c73c33f9 89 return 0
3b7fbcd0 90}
91
92#################### Private functions below ##################################
0dd6377f 93_dns_cloudns_init_check() {
c73c33f9 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)}"
212d0f24 99 CLOUDNS_SUB_AUTH_ID="${CLOUDNS_SUB_AUTH_ID:-$(_readaccountconf_mutable CLOUDNS_SUB_AUTH_ID)}"
c73c33f9 100 CLOUDNS_AUTH_PASSWORD="${CLOUDNS_AUTH_PASSWORD:-$(_readaccountconf_mutable CLOUDNS_AUTH_PASSWORD)}"
212d0f24 101 if [ -z "$CLOUDNS_AUTH_ID$CLOUDNS_SUB_AUTH_ID" ] || [ -z "$CLOUDNS_AUTH_PASSWORD" ]; then
c73c33f9 102 CLOUDNS_AUTH_ID=""
212d0f24 103 CLOUDNS_SUB_AUTH_ID=""
c73c33f9 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
212d0f24
DLN
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"
c73c33f9 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
5309afc3 127 # save the api id and password to the account conf file.
c73c33f9 128 _saveaccountconf_mutable CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
212d0f24 129 _saveaccountconf_mutable CLOUDNS_SUB_AUTH_ID "$CLOUDNS_SUB_AUTH_ID"
c73c33f9 130 _saveaccountconf_mutable CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
131
132 CLOUDNS_INIT_CHECK_COMPLETED=1
133
134 return 0
3b7fbcd0 135}
136
0dd6377f 137_dns_cloudns_get_zone_name() {
c73c33f9 138 i=2
139 while true; do
140 zoneForCheck=$(printf "%s" "$1" | cut -d . -f $i-100)
3b7fbcd0 141
c73c33f9 142 if [ -z "$zoneForCheck" ]; then
143 return 1
144 fi
3b7fbcd0 145
c73c33f9 146 _debug zoneForCheck "$zoneForCheck"
3b7fbcd0 147
c73c33f9 148 _dns_cloudns_http_api_call "dns/get-zone-info.json" "domain-name=$zoneForCheck"
3b7fbcd0 149
c73c33f9 150 if ! _contains "$response" "\"status\":\"Failed\""; then
151 echo "$zoneForCheck"
152 return 0
153 fi
3b7fbcd0 154
c73c33f9 155 i=$(_math "$i" + 1)
156 done
157 return 1
3b7fbcd0 158}
159
0dd6377f 160_dns_cloudns_http_api_call() {
c73c33f9 161 method=$1
3b7fbcd0 162
c73c33f9 163 _debug CLOUDNS_AUTH_ID "$CLOUDNS_AUTH_ID"
212d0f24 164 _debug CLOUDNS_SUB_AUTH_ID "$CLOUDNS_SUB_AUTH_ID"
c73c33f9 165 _debug CLOUDNS_AUTH_PASSWORD "$CLOUDNS_AUTH_PASSWORD"
3b7fbcd0 166
212d0f24
DLN
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"
bab4f691 171 fi
212d0f24 172
c73c33f9 173 if [ -z "$2" ]; then
212d0f24 174 data="$auth_user&auth-password=$CLOUDNS_AUTH_PASSWORD"
c73c33f9 175 else
212d0f24 176 data="$auth_user&auth-password=$CLOUDNS_AUTH_PASSWORD&$2"
c73c33f9 177 fi
3b7fbcd0 178
c73c33f9 179 response="$(_get "$CLOUDNS_API/$method?$data")"
3b7fbcd0 180
5309afc3 181 _debug response "$response"
3b7fbcd0 182
c73c33f9 183 return 0
f881d6c4 184}