2016-04-17 11:47:22 +00:00
#!/usr/bin/env sh
2016-02-07 10:26:12 +00:00
# Cloudxns.com Domain api
#
#CX_Key="1234"
#
#CX_Secret="sADDsdasdgdsf"
CX_Api = "https://www.cloudxns.net/api2"
#REST_API
######## Public functions #####################
#Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
2016-04-16 14:19:29 +00:00
dns_cx_add( ) {
2016-02-07 10:26:12 +00:00
fulldomain = $1
txtvalue = $2
if [ -z " $CX_Key " ] || [ -z " $CX_Secret " ] ; then
_err "You don't specify cloudxns.com api key or secret yet."
_err "Please create you key and try again."
return 1
fi
REST_API = $CX_Api
#save the api key and email to the account conf file.
_saveaccountconf CX_Key " $CX_Key "
_saveaccountconf CX_Secret " $CX_Secret "
_debug "First detect the root zone"
if ! _get_root $fulldomain ; then
_err "invalid domain"
return 1
fi
existing_records $_domain $_sub_domain
_debug count " $count "
if [ " $? " != "0" ] ; then
_err "Error get existing records."
return 1
fi
2016-05-23 05:32:50 +00:00
if [ " $count " = "0" ] ; then
2016-02-07 10:26:12 +00:00
add_record $_domain $_sub_domain $txtvalue
else
update_record $_domain $_sub_domain $txtvalue
fi
2016-05-23 05:32:50 +00:00
if [ " $? " = "0" ] ; then
2016-02-07 10:26:12 +00:00
return 0
fi
return 1
}
#usage: root sub
#return if the sub record already exists.
#echos the existing records count.
# '0' means doesn't exist
existing_records( ) {
_debug "Getting txt records"
root = $1
sub = $2
if ! _rest GET " record/ $_domain_id ?:domain_id?host_id=0&offset=0&row_num=100 " ; then
return 1
fi
count = 0
2016-04-24 14:54:40 +00:00
seg = $( printf " $response " | grep -o " {[^{]*host\":\" $_sub_domain \"[^}]*} " )
2016-02-07 10:26:12 +00:00
_debug seg " $seg "
if [ -z " $seg " ] ; then
return 0
fi
if printf " $response " | grep '"type":"TXT"' > /dev/null ; then
count = 1
record_id = $( printf " $seg " | grep -o \" record_id\" :\" [ ^\" ] *\" | cut -d : -f 2 | tr -d \" )
_debug record_id " $record_id "
return 0
fi
}
#add the txt record.
#usage: root sub txtvalue
add_record( ) {
root = $1
sub = $2
txtvalue = $3
fulldomain = $sub .$root
_info "Adding record"
if ! _rest POST "record" " {\"domain_id\": $_domain_id , \"host\":\" $_sub_domain \", \"value\":\" $txtvalue \", \"type\":\"TXT\",\"ttl\":600, \"line_id\":1} " ; then
return 1
fi
return 0
}
#update the txt record
#Usage: root sub txtvalue
update_record( ) {
root = $1
sub = $2
txtvalue = $3
fulldomain = $sub .$root
_info "Updating record"
if _rest PUT " record/ $record_id " " {\"domain_id\": $_domain_id , \"host\":\" $_sub_domain \", \"value\":\" $txtvalue \", \"type\":\"TXT\",\"ttl\":600, \"line_id\":1} " ; then
return 0
fi
return 1
}
#################### Private functions bellow ##################################
#_acme-challenge.www.domain.com
#returns
# _sub_domain=_acme-challenge.www
# _domain=domain.com
# _domain_id=sdjkglgdfewsdfg
_get_root( ) {
domain = $1
i = 2
p = 1
if ! _rest GET "domain" ; then
return 1
fi
while [ '1' ] ; do
h = $( printf $domain | cut -d . -f $i -100)
_debug h " $h "
if [ -z " $h " ] ; then
#not valid
return 1;
fi
2016-04-27 15:34:29 +00:00
if printf " $response " | grep " $h . " >/dev/null ; then
2016-08-05 13:31:12 +00:00
seg = $( printf " $response " | grep -o " {[^{]*\" $h \.\"[^}]*\} " )
2016-02-07 10:26:12 +00:00
_debug seg " $seg "
_domain_id = $( printf " $seg " | grep -o \" id\" :\" [ ^\" ] *\" | cut -d : -f 2 | tr -d \" )
_debug _domain_id " $_domain_id "
if [ " $_domain_id " ] ; then
_sub_domain = $( printf $domain | cut -d . -f 1-$p )
_debug _sub_domain $_sub_domain
_domain = $h
_debug _domain $_domain
return 0
fi
return 1
fi
p = $i
2016-04-17 09:33:08 +00:00
i = $( expr $i + 1)
2016-02-07 10:26:12 +00:00
done
return 1
}
#Usage: method URI data
_rest( ) {
m = $1
ep = " $2 "
_debug $ep
url = " $REST_API / $ep "
_debug url " $url "
cdate = $( date -u "+%Y-%m-%d %H:%M:%S UTC" )
_debug cdate " $cdate "
data = " $3 "
_debug data " $data "
sec = " $CX_Key $url $data $cdate $CX_Secret "
_debug sec " $sec "
hmac = $( printf " $sec " | openssl md5 | cut -d " " -f 2)
_debug hmac " $hmac "
2016-05-07 15:33:42 +00:00
_H1 = " API-KEY: $CX_Key "
_H2 = " API-REQUEST-DATE: $cdate "
_H3 = " API-HMAC: $hmac "
_H4 = "Content-Type: application/json"
if [ " $data " ] ; then
response = " $( _post " $data " " $url " "" $m ) "
2016-02-07 10:26:12 +00:00
else
2016-05-07 15:33:42 +00:00
response = " $( _get " $url " ) "
2016-02-07 10:26:12 +00:00
fi
if [ " $? " != "0" ] ; then
_err " error $ep "
return 1
fi
2016-04-09 15:40:59 +00:00
_debug2 response " $response "
2016-02-07 10:26:12 +00:00
if ! printf " $response " | grep '"message":"success"' > /dev/null ; then
return 1
fi
return 0
}