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