Fix review issues

All "reviewed functions" replaced with acme.sh functions. Added time zone for authentication token. Fix all XML list search with proper descriptions.
pull/3165/head
mxtuma 4 years ago committed by GitHub
parent c99f1ac83d
commit 55f9c4d2cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -13,116 +13,116 @@ TESTING_STAGE=
#Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
dns_wedos_add() { dns_wedos_add() {
fulldomain=$1 fulldomain=$1
txtvalue=$2 txtvalue=$2
WEDOS_Username="${WEDOS_Username:-$(_readaccountconf_mutable WEDOS_Username)}" WEDOS_Username="${WEDOS_Username:-$(_readaccountconf_mutable WEDOS_Username)}"
WEDOS_Wapipass="${WEDOS_Wapipass:-$(_readaccountconf_mutable WEDOS_Wapipass)}" WEDOS_Wapipass="${WEDOS_Wapipass:-$(_readaccountconf_mutable WEDOS_Wapipass)}"
WEDOS_Authtoken="${WEDOS_Authtoken:-$(_readaccountconf_mutable WEDOS_Authtoken)}" WEDOS_Authtoken="${WEDOS_Authtoken:-$(_readaccountconf_mutable WEDOS_Authtoken)}"
if [ "${WEDOS_Authtoken}" ]; then if [ "${WEDOS_Authtoken}" ]; then
_debug "WEDOS Authtoken was already saved, using saved one" _debug "WEDOS Authtoken was already saved, using saved one"
_saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}" _saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}"
else else
if [ -z "${WEDOS_Username}" ] || [ -z "${WEDOS_Wapipass}" ]; then if [ -z "${WEDOS_Username}" ] || [ -z "${WEDOS_Wapipass}" ]; then
WEDOS_Username="" WEDOS_Username=""
WEDOS_Wapipass="" WEDOS_Wapipass=""
_err "You didn't specify a WEDOS's username and wapi key yet." _err "You didn't specify a WEDOS's username and wapi key yet."
_err "Please type: export WEDOS_Username=<your user name to login to wedos web account>" _err "Please type: export WEDOS_Username=<your user name to login to wedos web account>"
_err "And: export WEDOS_Wapipass=<your WAPI passwords you setup using wedos web pages>" _err "And: export WEDOS_Wapipass=<your WAPI passwords you setup using wedos web pages>"
_err "After you export those variables, run the script again, the values will be saved for future" _err "After you export those variables, run the script again, the values will be saved for future"
return 1 return 1
fi fi
#build WEDOS_Authtoken #build WEDOS_Authtoken
_debug "WEDOS Authtoken were not saved yet, building" _debug "WEDOS Authtoken were not saved yet, building"
WEDOS_Authtoken=$(printf '%s' "${WEDOS_Wapipass}" | sha1sum | head -c 40) WEDOS_Authtoken=$(printf '%s' "${WEDOS_Wapipass}" | _digest "sha1" "true" | head -c 40)
_debug "WEDOS_Authtoken step 1, WAPI PASS sha1 sum: '${WEDOS_Authtoken}'" _debug "WEDOS_Authtoken step 1, WAPI PASS sha1 sum: '${WEDOS_Authtoken}'"
WEDOS_Authtoken="${WEDOS_Username}${WEDOS_Authtoken}" WEDOS_Authtoken="${WEDOS_Username}${WEDOS_Authtoken}"
_debug "WEDOS_Authtoken step 2, username concat with token without hours: '${WEDOS_Authtoken}'" _debug "WEDOS_Authtoken step 2, username concat with token without hours: '${WEDOS_Authtoken}'"
#save details #save details
_saveaccountconf_mutable WEDOS_Username "${WEDOS_Username}" _saveaccountconf_mutable WEDOS_Username "${WEDOS_Username}"
_saveaccountconf_mutable WEDOS_Wapipass "${WEDOS_Wapipass}" _saveaccountconf_mutable WEDOS_Wapipass "${WEDOS_Wapipass}"
_saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}" _saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}"
fi fi
if ! _get_root "${fulldomain}"; then if ! _get_root "${fulldomain}"; then
_err "WEDOS Account do not contain primary domain to fullfill add of ${fulldomain}!" _err "WEDOS Account do not contain primary domain to fullfill add of ${fulldomain}!"
return 1 return 1
fi fi
_debug _sub_domain "${_sub_domain}" _debug _sub_domain "${_sub_domain}"
_debug _domain "${_domain}" _debug _domain "${_domain}"
if _wapi_row_add "${_domain}" "${_sub_domain}" "${txtvalue}" "300"; then if _wapi_row_add "${_domain}" "${_sub_domain}" "${txtvalue}" "300"; then
_info "WEDOS WAPI: dns record added and dns changes were commited" _info "WEDOS WAPI: dns record added and dns changes were commited"
return 0 return 0
else else
_err "FAILED TO ADD DNS RECORD OR COMMIT DNS CHANGES" _err "FAILED TO ADD DNS RECORD OR COMMIT DNS CHANGES"
return 1 return 1
fi fi
} }
#fulldomain txtvalue #fulldomain txtvalue
dns_wedos_rm() { dns_wedos_rm() {
fulldomain=$1 fulldomain=$1
txtvalue=$2 txtvalue=$2
WEDOS_Username="${WEDOS_Username:-$(_readaccountconf_mutable WEDOS_Username)}" WEDOS_Username="${WEDOS_Username:-$(_readaccountconf_mutable WEDOS_Username)}"
WEDOS_Wapipass="${WEDOS_Wapipass:-$(_readaccountconf_mutable WEDOS_Wapipass)}" WEDOS_Wapipass="${WEDOS_Wapipass:-$(_readaccountconf_mutable WEDOS_Wapipass)}"
WEDOS_Authtoken="${WEDOS_Authtoken:-$(_readaccountconf_mutable WEDOS_Authtoken)}" WEDOS_Authtoken="${WEDOS_Authtoken:-$(_readaccountconf_mutable WEDOS_Authtoken)}"
if [ "${WEDOS_Authtoken}" ]; then if [ "${WEDOS_Authtoken}" ]; then
_debug "WEDOS Authtoken was already saved, using saved one" _debug "WEDOS Authtoken was already saved, using saved one"
_saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}" _saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}"
else else
if [ -z "${WEDOS_Username}" ] || [ -z "${WEDOS_Wapipass}" ]; then if [ -z "${WEDOS_Username}" ] || [ -z "${WEDOS_Wapipass}" ]; then
WEDOS_Username="" WEDOS_Username=""
WEDOS_Wapipass="" WEDOS_Wapipass=""
_err "You didn't specify a WEDOS's username and wapi key yet." _err "You didn't specify a WEDOS's username and wapi key yet."
_err "Please type: export WEDOS_Username=<your user name to login to wedos web account>" _err "Please type: export WEDOS_Username=<your user name to login to wedos web account>"
_err "And: export WEDOS_Wapipass=<your WAPI passwords you setup using wedos web pages>" _err "And: export WEDOS_Wapipass=<your WAPI passwords you setup using wedos web pages>"
_err "After you export those variables, run the script again, the values will be saved for future" _err "After you export those variables, run the script again, the values will be saved for future"
return 1 return 1
fi fi
#build WEDOS_Authtoken #build WEDOS_Authtoken
_debug "WEDOS Authtoken were not saved yet, building" _debug "WEDOS Authtoken were not saved yet, building"
WEDOS_Authtoken=$(printf '%s' "${WEDOS_Wapipass}" | sha1sum | head -c 40) WEDOS_Authtoken=$(printf '%s' "${WEDOS_Wapipass}" | sha1sum | head -c 40)
_debug "WEDOS_Authtoken step 1, WAPI PASS sha1 sum: '${WEDOS_Authtoken}'" _debug "WEDOS_Authtoken step 1, WAPI PASS sha1 sum: '${WEDOS_Authtoken}'"
WEDOS_Authtoken="${WEDOS_Username}${WEDOS_Authtoken}" WEDOS_Authtoken="${WEDOS_Username}${WEDOS_Authtoken}"
_debug "WEDOS_Authtoken step 2, username concat with token without hours: '${WEDOS_Authtoken}'" _debug "WEDOS_Authtoken step 2, username concat with token without hours: '${WEDOS_Authtoken}'"
#save details #save details
_saveaccountconf_mutable WEDOS_Username "${WEDOS_Username}" _saveaccountconf_mutable WEDOS_Username "${WEDOS_Username}"
_saveaccountconf_mutable WEDOS_Wapipass "${WEDOS_Wapipass}" _saveaccountconf_mutable WEDOS_Wapipass "${WEDOS_Wapipass}"
_saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}" _saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}"
fi fi
if ! _get_root "${fulldomain}"; then if ! _get_root "${fulldomain}"; then
_err "WEDOS Account do not contain primary domain to fullfill add of ${fulldomain}!" _err "WEDOS Account do not contain primary domain to fullfill add of ${fulldomain}!"
return 1 return 1
fi fi
_debug _sub_domain "${_sub_domain}" _debug _sub_domain "${_sub_domain}"
_debug _domain "${_domain}" _debug _domain "${_domain}"
if _wapi_find_row "${_domain}" "${_sub_domain}" "${txtvalue}"; then if _wapi_find_row "${_domain}" "${_sub_domain}" "${txtvalue}"; then
_info "WEDOS WAPI: dns record found with id '${_row_id}'" _info "WEDOS WAPI: dns record found with id '${_row_id}'"
if _wapi_delete_row "${_domain}" "${_row_id}"; then if _wapi_delete_row "${_domain}" "${_row_id}"; then
_info "WEDOS WAPI: dns row were deleted and changes commited!" _info "WEDOS WAPI: dns row were deleted and changes commited!"
return 0 return 0
fi fi
fi fi
_err "Requested dns row were not found or was imposible to delete it, do it manually" _err "Requested dns row were not found or was imposible to delete it, do it manually"
_err "Delete: ${fulldomain}" _err "Delete: ${fulldomain}"
_err "Value: ${txtvalue}" _err "Value: ${txtvalue}"
return 1 return 1
} }
#################### Private functions below ################################## #################### Private functions below ##################################
@ -132,143 +132,153 @@ dns_wedos_rm() {
# $2 - WAPI data for given command, is not required # $2 - WAPI data for given command, is not required
# returns WAPI response if request were successfully delivered to WAPI endpoint # returns WAPI response if request were successfully delivered to WAPI endpoint
_wapi_post() { _wapi_post() {
command=$1 command=$1
data=$2 data=$2
_debug "Command : ${command}" _debug "Command : ${command}"
_debug "Data : ${data}" _debug "Data : ${data}"
if [ -z "${command}" ]; then if [ -z "${command}" ]; then
_err "No command were provided, implamantation error!" _err "No command were provided, implamantation error!"
return 1 return 1
fi fi
# Prepare authentification token # Prepare authentification token
hour=$(date +%H) hour=$(TZ='Europe/Prague' date +%H)
token=$(printf '%s' "${WEDOS_Authtoken}${hour}" | sha1sum | head -c 40) token=$(printf '%s' "${WEDOS_Authtoken}${hour}" | _digest "sha1" "true" | head -c 40)
_debug "Authentification token is '${token}'" _debug "Authentification token is '${token}'"
# Build xml request # Build xml request
request="request=<?xml version=\"1.0\" encoding=\"UTF-8\"?>\ request="request=<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
<request>\ <request>\
<user>${WEDOS_Username}</user>\ <user>${WEDOS_Username}</user>\
<auth>${token}</auth>\ <auth>${token}</auth>\
<command>${command}</command>" <command>${command}</command>"
if [ -z "${data}" ]; then if [ -z "${data}" ]; then
echo "" 1>/dev/null echo "" 1>/dev/null
else else
request="${request}${data}" request="${request}${data}"
fi fi
if [ -z "$TESTING_STAGE" ]; then if [ -z "$TESTING_STAGE" ]; then
echo "" 1>/dev/null echo "" 1>/dev/null
else else
request="${request}\ request="${request}\
<test>1</test>" <test>1</test>"
fi fi
request="${request}\ request="${request}\
</request>" </request>"
_debug "Request to WAPI is: ${request}" _debug "Request to WAPI is: ${request}"
if ! response="$(_post "${request}" "$WEDOS_WAPI_ENDPOINT")"; then if ! response="$(_post "${request}" "$WEDOS_WAPI_ENDPOINT")"; then
_err "Error contacting WEDOS WAPI with command ${command}" _err "Error contacting WEDOS WAPI with command ${command}"
return 1 return 1
fi fi
_debug "Response : ${response}" _debug "Response : ${response}"
echo "${response}" | grep "<code>1000</code>" 1>/dev/null 2>/dev/null _contains "${response}" "<code>1000</code>"
return "$?" return "$?"
} }
# _get_root() function, for provided full domain, like _acme_challenge.www.example.com verify if WEDOS contains a primary active domain and found what is subdomain # _get_root() function, for provided full domain, like _acme_challenge.www.example.com verify if WEDOS contains a primary active domain and found what is subdomain
# $1 - full domain to verify, ie _acme_challenge.www.example.com # $1 - full domain to verify, ie _acme_challenge.www.example.com
# build ${_domain} found at WEDOS, like example.com and ${_sub_domain} from provided full domain, like _acme_challenge.www # build ${_domain} found at WEDOS, like example.com and ${_sub_domain} from provided full domain, like _acme_challenge.www
_get_root() { _get_root() {
domain=$1 domain=$1
if [ -z "${domain}" ]; then if [ -z "${domain}" ]; then
_err "Function _get_root was called without argument, implementation error!" _err "Function _get_root was called without argument, implementation error!"
return 1 return 1
fi fi
_debug "Get root for domain: ${domain}" _debug "Get root for domain: ${domain}"
_debug "Getting list of domains using WAPI ..." _debug "Getting list of domains using WAPI ..."
if ! _wapi_post "dns-domains-list"; then if ! _wapi_post "dns-domains-list"; then
_err "Error on WAPI request for list of domains, response : ${response}" _err "Error on WAPI request for list of domains, response : ${response}"
return 1 return 1
else else
_debug "DNS list were successfully retrieved, response : ${response}" _debug "DNS list were successfully retrieved, response : ${response}"
fi fi
for xml_domain in $(echo "${response}" | tr -d '\012\015' | grep -o -E "<domain>( )*<name>.*</name>( )*<type>primary</type>( )*<status>active</status>" | grep -o -E "<name>.*</name>"); do # In for each cycle, try parse the response to find primary active domains
_debug "Active and primary XML DOMAIN found: ${xml_domain}" # For cycle description:
end_of_name=$((${#xml_domain} - 7)) # 1st tr -d '\011\012\015' = remove all newlines and tab characters - whole XML became single line
xml_domain_name=$(echo "${xml_domain}" | cut -c 7-${end_of_name}) # 2nd sed "s/^.*<data>[ ]*//g" = remove all the xml data from the beggining of the XML - XML now start with the content of <data> element
_debug "Found primary active domain: ${xml_domain_name}" # 3rd sed "s/<\/data>.*$//g" = remove all the data after the data xml element - XML now contains only the content of data xml element
regex=".*\\."$(echo "${xml_domain_name}" | sed 's/\./\\./g') # 4th sed "s/>[ ]*<\([^\/]\)/><\1/g" = remove all spaces between XML tag and XML start tag - XML now contains content of data xml element and is without spaces between end and start xml tags
_debug "Regex for matching domain: '${regex}'" # 5th sed "s/<domain>//g" = remove all domain xml start tags - XML now contains only <name>...</name><type>...</type><status>...</status> </domain>(next xml domain)
# 6th sed "s/[ ]*<\/domain>/\n/g"= replace all "spaces</domain>" by new line - now we create multiple lines each should contain only <name>...</name><type>...</type><status>...</status>
if ! echo "${domain}" | grep -E "${regex}" 1>/dev/null 2>/dev/null; then # 7th sed -n "/<name>\([a-zA-Z0-9_\-\.]\+\)<\/name><type>primary<\/type><status>active<\/status>/p" = remove all non primary or non active domains lines
_debug "found domain do not match required" # 8th sed "s/<name>\([a-zA-Z0-9_\-\.]\+\)<\/name><type>primary<\/type><status>active<\/status>/\1/g" = substitute for domain names only
else
end_of_name=$((${#domain} - ${#xml_domain_name} - 1)) for xml_domain in $(echo "${response}" | tr -d '\011\012\015' | sed "s/^.*<data>[ ]*//g" | sed "s/<\/data>.*$//g" | sed "s/>[ ]*<\([^\/]\)/><\1/g" | sed "s/<domain>//g" | sed "s/[ ]*<\/domain>/\n/g" | sed -n "/<name>\([a-zA-Z0-9_\-\.]\+\)<\/name><type>primary<\/type><status>active<\/status>/p" | sed "s/<name>\([a-zA-Z0-9_\-\.]\+\)<\/name><type>primary<\/type><status>active<\/status>/\1/g"); do
_domain=${xml_domain_name} _debug "Found primary active domain: ${xml_domain}"
_sub_domain=$(echo "${domain}" | cut -c -${end_of_name}) if _endswith "${domain}" "${xml_domain}"; then
_info "Domain '${_domain}' was found at WEDOS account as primary, and subdomain is '${_sub_domain}'!" length_difference=$(_math "${#domain} - ${#xml_domain}")
return 0 possible_subdomain=$(echo "${domain}" | cut -c -"${length_difference}")
fi if _endswith "${possible_subdomain}" "."; then
done length_difference=$(_math "${length_difference} - 1")
_domain=${xml_domain}
return 1 _sub_domain=$(echo "${possible_subdomain}" | cut -c -"${length_difference}")
_info "Domain '${_domain}' was found at WEDOS account as primary, and subdomain is '${_sub_domain}'!"
return 0
fi
fi
_debug " ... found domain does not match required!"
done
return 1
} }
# for provided domain, it commites all performed changes # for provided domain, it commites all performed changes
_wapi_dns_commit() { _wapi_dns_commit() {
domain=$1 domain=$1
if [ -z "${domain}" ]; then if [ -z "${domain}" ]; then
_err "Invalid request to commit dns changes, domain is empty, implementation error!" _err "Invalid request to commit dns changes, domain is empty, implementation error!"
return 1 return 1
fi fi
data=" <data>\ data=" <data>\
<name>${domain}</name>\ <name>${domain}</name>\
</data>" </data>"
if ! _wapi_post "dns-domain-commit" "${data}"; then if ! _wapi_post "dns-domain-commit" "${data}"; then
_err "Error on WAPI request to commit DNS changes, response : ${response}" _err "Error on WAPI request to commit DNS changes, response : ${response}"
_err "PLEASE USE WEB ACCESS TO CHECK IF CHANGES ARE REQUIRED TO COMMIT OR ROLLBACKED IMMEDIATELLY!" _err "PLEASE USE WEB ACCESS TO CHECK IF CHANGES ARE REQUIRED TO COMMIT OR ROLLBACKED IMMEDIATELLY!"
return 1 return 1
else else
_debug "DNS CHANGES COMMITED, response : ${response}" _debug "DNS CHANGES COMMITED, response : ${response}"
_info "WEDOS DNS WAPI: Changes were commited to domain '${domain}'" _info "WEDOS DNS WAPI: Changes were commited to domain '${domain}'"
fi fi
return 0 return 0
} }
# add one TXT dns row to a specified fomain # add one TXT dns row to a specified fomain
_wapi_row_add() { _wapi_row_add() {
domain=$1 domain=$1
sub_domain=$2 sub_domain=$2
value=$3 value=$3
ttl=$4 ttl=$4
if [ -z "${domain}" ] || [ -z "${sub_domain}" ] || [ -z "${value}" ] || [ -z "${ttl}" ]; then if [ -z "${domain}" ] || [ -z "${sub_domain}" ] || [ -z "${value}" ] || [ -z "${ttl}" ]; then
_err "Invalid request to add record, domain: '${domain}', sub_domain: '${sub_domain}', value: '${value}' and ttl: '${ttl}', on of required input were not provided, implementation error!" _err "Invalid request to add record, domain: '${domain}', sub_domain: '${sub_domain}', value: '${value}' and ttl: '${ttl}', on of required input were not provided, implementation error!"
return 1 return 1
fi fi
# Prepare data for request to WAPI # Prepare data for request to WAPI
data=" <data>\ data=" <data>\
<domain>${domain}</domain>\ <domain>${domain}</domain>\
<name>${sub_domain}</name>\ <name>${sub_domain}</name>\
<ttl>${ttl}</ttl>\ <ttl>${ttl}</ttl>\
@ -277,90 +287,105 @@ _wapi_row_add() {
<auth_comment>Created using WAPI from acme.sh</auth_comment>\ <auth_comment>Created using WAPI from acme.sh</auth_comment>\
</data>" </data>"
_debug "Adding row using WAPI ..." _debug "Adding row using WAPI ..."
if ! _wapi_post "dns-row-add" "${data}"; then if ! _wapi_post "dns-row-add" "${data}"; then
_err "Error on WAPI request to add new TXT row, response : ${response}" _err "Error on WAPI request to add new TXT row, response : ${response}"
return 1 return 1
else else
_debug "ROW ADDED, response : ${response}" _debug "ROW ADDED, response : ${response}"
_info "WEDOS DNS WAPI: Row to domain '${domain}' with name '${sub_domain}' were successfully added with value '${value}' and ttl set to ${ttl}" _info "WEDOS DNS WAPI: Row to domain '${domain}' with name '${sub_domain}' were successfully added with value '${value}' and ttl set to ${ttl}"
fi fi
# Now we have to commit # Now we have to commit
_wapi_dns_commit "${domain}" _wapi_dns_commit "${domain}"
return "$?" return "$?"
} }
_wapi_find_row() { _wapi_find_row() {
domain=$1 domain=$1
sub_domain=$2 sub_domain=$2
value=$3 value=$3
if [ -z "${domain}" ] || [ -z "${sub_domain}" ] || [ -z "${value}" ]; then if [ -z "${domain}" ] || [ -z "${sub_domain}" ] || [ -z "${value}" ]; then
_err "Invalud request to finad a row, domain: '${domain}', sub_domain: '${sub_domain}' and value: '${value}', one of required input were not provided, implementation error!" _err "Invalud request to finad a row, domain: '${domain}', sub_domain: '${sub_domain}' and value: '${value}', one of required input were not provided, implementation error!"
return 1 return 1
fi fi
data=" <data>\ data=" <data>\
<domain>${domain}</domain>\ <domain>${domain}</domain>\
</data>" </data>"
_debug "Searching rows using WAPI ..." _debug "Searching rows using WAPI ..."
if ! _wapi_post "dns-rows-list" "${data}"; then
_err "Error on WAPI request to list domain rows, response : ${response}"
return 1
fi
_debug "Domain rows found, response : ${response}"
if ! _wapi_post "dns-rows-list" "${data}"; then # Prepare sub domain regex which will be later used for search domain row
_err "Error on WAPI request to list domain rows, response : ${response}" # from _acme_challenge.sub it should be _acme_challenge\.sub
return 1
fi
_debug "Domain rows found, response : ${response}" sub_domain_regex=$(echo "${sub_domain}" | sed "s/\./\\\\./g")
sub_domain_regex=$(echo "${sub_domain}" | sed "s/\./\\\\./g") _debug "Subdomain regex '${sub_domain_regex}'"
_debug "Subdomain regex '${sub_domain_regex}'" # In for each cycle loops over the domains rows, description:
# 1st tr -d '\011\012\015' = delete all newlines and tab characters - XML became a single line
# 2nd sed "s/^.*<data>[ ]*//g" = remove all from the beggining to the start of the content of the data xml element - XML is without unusefull beginning
# 3rd sed "s/[ ]*<\/data>.*$//g" = remove the end of the xml starting with xml end tag data - XML contains only the content of data xml element and is trimmed
# 4th sed "s/>[ ]*<\([^\/]\)/><\1/g" = remove all spaces between XML tag and XML start tag - XML now contains content of data xml element and is without spaces between end and start xml tags
# 5th sed "s/<row>//g" = remove all row xml start tags - XML now contains rows xml element content and its end tag
# 6th sed "s/[ ]*<\/row>/\n/g" = replace all "spaces</row>" by new line - now we create multiple lines each should contain only single row xml content
# 7th sed -n "/<name>${sub_domain_regex}<\/name>.*<rdtype>TXT<\/rdtype>/p" = remove all non TXT and non name matching row lines - now we have only xml lines with TXT rows matching requested values
# 8th sed "s/^<ID>\([0-9]\+\)<\/ID>.*<rdata>\(.*\)<\/rdata>.*$/\1-\2/" = replace the whole lines to ID-value pairs
# -- now there are only lines with ID-value but value might contain spaces (BAD FOR FOREACH LOOP) or special characters (BAD FOR REGEX MATCHING)
# 9th grep "${value}" = match only a line containg searched value
# 10th sed "s/^\([0-9]\+\).*$/\1/" = get only ID from the row
for xml_row in $(echo "${response}" | tr -d '\012\015' | grep -o -E "<row>( )*<ID>[0-9]*</ID>( )*<name>${sub_domain_regex}</name>( )*<ttl>[0-9]*</ttl>( )*<rdtype>TXT</rdtype>( )*<rdata>${value}</rdata>" | grep -o -e "<ID>[0-9]*</ID>"); do for xml_row in $(echo "${response}" | tr -d '\011\012\015' | sed "s/^.*<data>[ ]*//g" | sed "s/[ ]*<\/data>.*$//g" | sed "s/>[ ]*<\([^\/]\)/><\1/g" | sed "s/<row>//g" | sed "s/[ ]*<\/row>/\n/g" | sed -n "/<name>${sub_domain_regex}<\/name>.*<rdtype>TXT<\/rdtype>/p" | sed "s/^<ID>\([0-9]\+\)<\/ID>.*<rdata>\(.*\)<\/rdata>.*$/\1-\2/" | grep "${value}" | sed "s/^\([0-9]\+\).*$/\1/"); do
_debug "Found row in DNS with ID : ${xml_row}" _row_id="${xml_row}"
_row_id=$(echo "${xml_row}" | grep -o -E "[0-9]*") _info "WEDOS API: Found DNS row id ${_row_id} for domain ${domain}"
_info "WEDOS API: Found DNS row id ${_row_id} for domain ${domain}" return 0
return 0 done
done
_info "WEDOS API: No TXT row found for domain '${domain}' with name '${sub_domain}' and value '${value}'" _info "WEDOS API: No TXT row found for domain '${domain}' with name '${sub_domain}' and value '${value}'"
return 1 return 1
} }
_wapi_delete_row() { _wapi_delete_row() {
domain=$1 domain=$1
row_id=$2 row_id=$2
if [ -z "${domain}" ] || [ -z "${row_id}" ]; then if [ -z "${domain}" ] || [ -z "${row_id}" ]; then
_err "Invalid request to delete domain dns row, domain: '${domain}' and row_id: '${row_id}', one of required input were not provided, implementation error!" _err "Invalid request to delete domain dns row, domain: '${domain}' and row_id: '${row_id}', one of required input were not provided, implementation error!"
return 1 return 1
fi fi
data=" <data>\ data=" <data>\
<domain>${domain}</domain> <domain>${domain}</domain>
<row_id>${row_id}</row_id> <row_id>${row_id}</row_id>
</data>" </data>"
_debug "Deleting dns row using WAPI ..." _debug "Deleting dns row using WAPI ..."
if ! _wapi_post "dns-row-delete" "${data}"; then if ! _wapi_post "dns-row-delete" "${data}"; then
_err "Error on WAPI request to delete dns row, response: ${response}" _err "Error on WAPI request to delete dns row, response: ${response}"
return 1 return 1
fi fi
_debug "DNS row were deleted, response: ${response}" _debug "DNS row were deleted, response: ${response}"
_info "WEDOS API: Required dns domain row with row_id '${row_id}' were correctly deleted at domain '${domain}'" _info "WEDOS API: Required dns domain row with row_id '${row_id}' were correctly deleted at domain '${domain}'"
# Now we have to commit changes # Now we have to commit changes
_wapi_dns_commit "${domain}" _wapi_dns_commit "${domain}"
return "$?" return "$?"
} }

Loading…
Cancel
Save