]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_cx.sh
Merge pull request #3261 from NerLOR/master
[mirror_acme.sh.git] / dnsapi / dns_cx.sh
CommitLineData
0a7c9364 1#!/usr/bin/env sh
08094865 2
9f6f721a 3# CloudXNS Domain api
08094865 4#
5#CX_Key="1234"
6#
7#CX_Secret="sADDsdasdgdsf"
8
08094865 9CX_Api="https://www.cloudxns.net/api2"
10
08094865 11#REST_API
12######## Public functions #####################
13
14#Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
eccec5f6 15dns_cx_add() {
08094865 16 fulldomain=$1
17 txtvalue=$2
4c2a3841 18
a89d50d3 19 CX_Key="${CX_Key:-$(_readaccountconf_mutable CX_Key)}"
20 CX_Secret="${CX_Secret:-$(_readaccountconf_mutable CX_Secret)}"
4c2a3841 21 if [ -z "$CX_Key" ] || [ -z "$CX_Secret" ]; then
422e5026 22 CX_Key=""
23 CX_Secret=""
9f6f721a 24 _err "You don't specify cloudxns.net api key or secret yet."
08094865 25 _err "Please create you key and try again."
26 return 1
27 fi
4c2a3841 28
c7b16249 29 REST_API="$CX_Api"
4c2a3841 30
08094865 31 #save the api key and email to the account conf file.
a89d50d3 32 _saveaccountconf_mutable CX_Key "$CX_Key"
33 _saveaccountconf_mutable CX_Secret "$CX_Secret"
4c2a3841 34
08094865 35 _debug "First detect the root zone"
c7b16249 36 if ! _get_root "$fulldomain"; then
08094865 37 _err "invalid domain"
38 return 1
39 fi
4c2a3841 40
849a6c12 41 add_record "$_domain" "$_sub_domain" "$txtvalue"
08094865 42}
43
849a6c12 44#fulldomain txtvalue
5d6fd809 45dns_cx_rm() {
46 fulldomain=$1
849a6c12 47 txtvalue=$2
a89d50d3 48 CX_Key="${CX_Key:-$(_readaccountconf_mutable CX_Key)}"
49 CX_Secret="${CX_Secret:-$(_readaccountconf_mutable CX_Secret)}"
8379f015 50 REST_API="$CX_Api"
51 if _get_root "$fulldomain"; then
52 record_id=""
849a6c12 53 existing_records "$_domain" "$_sub_domain" "$txtvalue"
54 if [ "$record_id" ]; then
8379f015 55 _rest DELETE "record/$record_id/$_domain_id" "{}"
56 _info "Deleted record ${fulldomain}"
57 fi
58 fi
5d6fd809 59}
60
08094865 61#usage: root sub
62#return if the sub record already exists.
63#echos the existing records count.
64# '0' means doesn't exist
65existing_records() {
66 _debug "Getting txt records"
67 root=$1
68 sub=$2
4c2a3841 69 if ! _rest GET "record/$_domain_id?:domain_id?host_id=0&offset=0&row_num=100"; then
08094865 70 return 1
71 fi
d69f0289 72
646c0bfc 73 seg=$(printf "%s\n" "$response" | _egrep_o '"record_id":[^{]*host":"'"$_sub_domain"'"[^}]*\}')
08094865 74 _debug seg "$seg"
4c2a3841 75 if [ -z "$seg" ]; then
08094865 76 return 0
77 fi
78
c7b16249 79 if printf "%s" "$response" | grep '"type":"TXT"' >/dev/null; then
5be3f22d 80 record_id=$(printf "%s\n" "$seg" | _egrep_o '"record_id":"[^"]*"' | cut -d : -f 2 | tr -d \" | _head_n 1)
08094865 81 _debug record_id "$record_id"
4c2a3841 82 return 0
08094865 83 fi
4c2a3841 84
08094865 85}
86
87#add the txt record.
88#usage: root sub txtvalue
89add_record() {
90 root=$1
91 sub=$2
92 txtvalue=$3
c7b16249 93 fulldomain="$sub.$root"
4c2a3841 94
08094865 95 _info "Adding record"
4c2a3841 96
08094865 97 if ! _rest POST "record" "{\"domain_id\": $_domain_id, \"host\":\"$_sub_domain\", \"value\":\"$txtvalue\", \"type\":\"TXT\",\"ttl\":600, \"line_id\":1}"; then
98 return 1
99 fi
4c2a3841 100
08094865 101 return 0
102}
103
329174b6 104#################### Private functions below ##################################
08094865 105#_acme-challenge.www.domain.com
106#returns
107# _sub_domain=_acme-challenge.www
108# _domain=domain.com
109# _domain_id=sdjkglgdfewsdfg
110_get_root() {
111 domain=$1
112 i=2
113 p=1
4c2a3841 114
115 if ! _rest GET "domain"; then
08094865 116 return 1
117 fi
4c2a3841 118
c7b16249 119 while true; do
120 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
08094865 121 _debug h "$h"
4c2a3841 122 if [ -z "$h" ]; then
08094865 123 #not valid
4c2a3841 124 return 1
08094865 125 fi
126
c7b16249 127 if _contains "$response" "$h."; then
bcf96608 128 seg=$(printf "%s\n" "$response" | _egrep_o '"id":[^{]*"'"$h"'."[^}]*}')
08094865 129 _debug seg "$seg"
a6270667 130 _domain_id=$(printf "%s\n" "$seg" | _egrep_o "\"id\":\"[^\"]*\"" | cut -d : -f 2 | tr -d \")
08094865 131 _debug _domain_id "$_domain_id"
4c2a3841 132 if [ "$_domain_id" ]; then
c7b16249 133 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
134 _debug _sub_domain "$_sub_domain"
135 _domain="$h"
136 _debug _domain "$_domain"
08094865 137 return 0
138 fi
139 return 1
140 fi
c7b16249 141 p="$i"
142 i=$(_math "$i" + 1)
08094865 143 done
144 return 1
145}
146
08094865 147#Usage: method URI data
148_rest() {
149 m=$1
150 ep="$2"
5be3f22d 151 _debug ep "$ep"
08094865 152 url="$REST_API/$ep"
153 _debug url "$url"
4c2a3841 154
155 cdate=$(date -u "+%Y-%m-%d %H:%M:%S UTC")
08094865 156 _debug cdate "$cdate"
4c2a3841 157
08094865 158 data="$3"
159 _debug data "$data"
4c2a3841 160
08094865 161 sec="$CX_Key$url$data$cdate$CX_Secret"
162 _debug sec "$sec"
c7b16249 163 hmac=$(printf "%s" "$sec" | _digest md5 hex)
08094865 164 _debug hmac "$hmac"
4c2a3841 165
3ca93f4a
BB
166 export _H1="API-KEY: $CX_Key"
167 export _H2="API-REQUEST-DATE: $cdate"
168 export _H3="API-HMAC: $hmac"
169 export _H4="Content-Type: application/json"
a4270efa 170
4c2a3841 171 if [ "$data" ]; then
69925ce8 172 response="$(_post "$data" "$url" "" "$m")"
08094865 173 else
a4270efa 174 response="$(_get "$url")"
08094865 175 fi
4c2a3841 176
177 if [ "$?" != "0" ]; then
08094865 178 _err "error $ep"
179 return 1
180 fi
a63b05a9 181 _debug2 response "$response"
04683338 182
183 _contains "$response" '"code":1'
184
08094865 185}