]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_cx.sh
fix shellcheck warnings.
[mirror_acme.sh.git] / dnsapi / dns_cx.sh
CommitLineData
0a7c9364 1#!/usr/bin/env sh
08094865 2
3# Cloudxns.com Domain api
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
19 if [ -z "$CX_Key" ] || [ -z "$CX_Secret" ]; then
08094865 20 _err "You don't specify cloudxns.com api key or secret yet."
21 _err "Please create you key and try again."
22 return 1
23 fi
4c2a3841 24
c7b16249 25 REST_API="$CX_Api"
4c2a3841 26
08094865 27 #save the api key and email to the account conf file.
28 _saveaccountconf CX_Key "$CX_Key"
29 _saveaccountconf CX_Secret "$CX_Secret"
4c2a3841 30
08094865 31 _debug "First detect the root zone"
c7b16249 32 if ! _get_root "$fulldomain"; then
08094865 33 _err "invalid domain"
34 return 1
35 fi
4c2a3841 36
c7b16249 37 existing_records "$_domain" "$_sub_domain"
08094865 38 _debug count "$count"
4c2a3841 39 if [ "$?" != "0" ]; then
08094865 40 _err "Error get existing records."
41 return 1
42 fi
43
4c2a3841 44 if [ "$count" = "0" ]; then
c7b16249 45 add_record "$_domain" "$_sub_domain" "$txtvalue"
08094865 46 else
c7b16249 47 update_record "$_domain" "$_sub_domain" "$txtvalue"
08094865 48 fi
4c2a3841 49
50 if [ "$?" = "0" ]; then
08094865 51 return 0
52 fi
53 return 1
54}
55
5d6fd809 56#fulldomain
57dns_cx_rm() {
58 fulldomain=$1
59
60}
61
08094865 62#usage: root sub
63#return if the sub record already exists.
64#echos the existing records count.
65# '0' means doesn't exist
66existing_records() {
67 _debug "Getting txt records"
68 root=$1
69 sub=$2
4c2a3841 70
71 if ! _rest GET "record/$_domain_id?:domain_id?host_id=0&offset=0&row_num=100"; then
08094865 72 return 1
73 fi
74 count=0
fdcb6b72 75 seg=$(printf "%s\n" "$response" | _egrep_o "{[^\{]*host\":\"$_sub_domain\"[^\}]*\}")
08094865 76 _debug seg "$seg"
4c2a3841 77 if [ -z "$seg" ]; then
08094865 78 return 0
79 fi
80
c7b16249 81 if printf "%s" "$response" | grep '"type":"TXT"' >/dev/null; then
08094865 82 count=1
22ea4004 83 record_id=$(printf "%s\n" "$seg" | _egrep_o \"record_id\":\"[^\"]*\" | cut -d : -f 2 | tr -d \")
08094865 84 _debug record_id "$record_id"
4c2a3841 85 return 0
08094865 86 fi
4c2a3841 87
08094865 88}
89
90#add the txt record.
91#usage: root sub txtvalue
92add_record() {
93 root=$1
94 sub=$2
95 txtvalue=$3
c7b16249 96 fulldomain="$sub.$root"
4c2a3841 97
08094865 98 _info "Adding record"
4c2a3841 99
08094865 100 if ! _rest POST "record" "{\"domain_id\": $_domain_id, \"host\":\"$_sub_domain\", \"value\":\"$txtvalue\", \"type\":\"TXT\",\"ttl\":600, \"line_id\":1}"; then
101 return 1
102 fi
4c2a3841 103
08094865 104 return 0
105}
106
107#update the txt record
108#Usage: root sub txtvalue
109update_record() {
110 root=$1
111 sub=$2
112 txtvalue=$3
c7b16249 113 fulldomain="$sub.$root"
4c2a3841 114
08094865 115 _info "Updating record"
4c2a3841 116
117 if _rest PUT "record/$record_id" "{\"domain_id\": $_domain_id, \"host\":\"$_sub_domain\", \"value\":\"$txtvalue\", \"type\":\"TXT\",\"ttl\":600, \"line_id\":1}"; then
08094865 118 return 0
119 fi
4c2a3841 120
08094865 121 return 1
122}
123
08094865 124#################### Private functions bellow ##################################
125#_acme-challenge.www.domain.com
126#returns
127# _sub_domain=_acme-challenge.www
128# _domain=domain.com
129# _domain_id=sdjkglgdfewsdfg
130_get_root() {
131 domain=$1
132 i=2
133 p=1
4c2a3841 134
135 if ! _rest GET "domain"; then
08094865 136 return 1
137 fi
4c2a3841 138
c7b16249 139 while true; do
140 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
08094865 141 _debug h "$h"
4c2a3841 142 if [ -z "$h" ]; then
08094865 143 #not valid
4c2a3841 144 return 1
08094865 145 fi
146
c7b16249 147 if _contains "$response" "$h."; then
4c2a3841 148 seg=$(printf "%s" "$response" | _egrep_o "\{[^\{]*\"$h\.\"[^\}]*\}")
08094865 149 _debug seg "$seg"
22ea4004 150 _domain_id=$(printf "%s" "$seg" | _egrep_o \"id\":\"[^\"]*\" | cut -d : -f 2 | tr -d \")
08094865 151 _debug _domain_id "$_domain_id"
4c2a3841 152 if [ "$_domain_id" ]; then
c7b16249 153 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
154 _debug _sub_domain "$_sub_domain"
155 _domain="$h"
156 _debug _domain "$_domain"
08094865 157 return 0
158 fi
159 return 1
160 fi
c7b16249 161 p="$i"
162 i=$(_math "$i" + 1)
08094865 163 done
164 return 1
165}
166
08094865 167#Usage: method URI data
168_rest() {
169 m=$1
170 ep="$2"
c7b16249 171 _debug "$ep"
08094865 172 url="$REST_API/$ep"
173 _debug url "$url"
4c2a3841 174
175 cdate=$(date -u "+%Y-%m-%d %H:%M:%S UTC")
08094865 176 _debug cdate "$cdate"
4c2a3841 177
08094865 178 data="$3"
179 _debug data "$data"
4c2a3841 180
08094865 181 sec="$CX_Key$url$data$cdate$CX_Secret"
182 _debug sec "$sec"
c7b16249 183 hmac=$(printf "%s" "$sec" | _digest md5 hex)
08094865 184 _debug hmac "$hmac"
4c2a3841 185
a4270efa 186 _H1="API-KEY: $CX_Key"
187 _H2="API-REQUEST-DATE: $cdate"
188 _H3="API-HMAC: $hmac"
189 _H4="Content-Type: application/json"
190
4c2a3841 191 if [ "$data" ]; then
a4270efa 192 response="$(_post "$data" "$url" "" $m)"
08094865 193 else
a4270efa 194 response="$(_get "$url")"
08094865 195 fi
4c2a3841 196
197 if [ "$?" != "0" ]; then
08094865 198 _err "error $ep"
199 return 1
200 fi
a63b05a9 201 _debug2 response "$response"
c7b16249 202 if ! _contains "$response" '"message":"success"'; then
08094865 203 return 1
204 fi
205 return 0
206}