# _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
# build ${_domain} found at WEDOS, like example.com and ${_sub_domain} from provided full domain, like _acme_challenge.www
_get_root(){
domain=$1
if[ -z "${domain}"];then
_err "Function _get_root was called without argument, implementation error!"
return1
fi
_debug "Get root for domain: ${domain}"
_debug "Getting list of domains using WAPI ..."
if ! _wapi_post "dns-domains-list";then
_err "Error on WAPI request for list of domains, response : ${response}"
return1
else
_debug "DNS list were successfully retrieved, response : ${response}"
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
_debug "Active and primary XML DOMAIN found: ${xml_domain}"
_info "Domain '${_domain}' was found at WEDOS account as primary, and subdomain is '${_sub_domain}'!"
return0
fi
done
return1
domain=$1
if[ -z "${domain}"];then
_err "Function _get_root was called without argument, implementation error!"
return1
fi
_debug "Get root for domain: ${domain}"
_debug "Getting list of domains using WAPI ..."
if ! _wapi_post "dns-domains-list";then
_err "Error on WAPI request for list of domains, response : ${response}"
return1
else
_debug "DNS list were successfully retrieved, response : ${response}"
fi
# In for each cycle, try parse the response to find primary active domains
# For cycle description:
# 1st tr -d '\011\012\015' = remove all newlines and tab characters - whole XML became single line
# 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
# 3rd sed "s/<\/data>.*$//g" = remove all the data after the data xml element - XML now contains only the content of data xml element
# 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/<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>
# 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
# 8th sed "s/<name>\([a-zA-Z0-9_\-\.]\+\)<\/name><type>primary<\/type><status>active<\/status>/\1/g" = substitute for domain names only
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
_debug "Found primary active domain: ${xml_domain}"
_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!"
return1
fi
# Prepare data for request to WAPI
data=" <data>\
<domain>${domain}</domain>\
<name>${sub_domain}</name>\
<ttl>${ttl}</ttl>\
@ -277,90 +287,105 @@ _wapi_row_add() {
<auth_comment>Created using WAPI from acme.sh</auth_comment>\
</data>"
_debug "Adding row using WAPI ..."
_debug "Adding row using WAPI ..."
if ! _wapi_post "dns-row-add""${data}";then
_err "Error on WAPI request to add new TXT row, response : ${response}"
return1
else
_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}"
fi
if ! _wapi_post "dns-row-add""${data}";then
_err "Error on WAPI request to add new TXT row, response : ${response}"
return1
else
_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}"
_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!"
return1
fi
data=" <data>\
data=" <data>\
<domain>${domain}</domain>\
</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}"
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
_debug "Found row in DNS with 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}"
return0
done
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
_row_id="${xml_row}"
_info "WEDOS API: Found DNS row id ${_row_id} for domain ${domain}"
return0
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}'"
return1
return1
}
_wapi_delete_row(){
domain=$1
row_id=$2
domain=$1
row_id=$2
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!"
return1
fi
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!"
return1
fi
data=" <data>\
data=" <data>\
<domain>${domain}</domain>
<row_id>${row_id}</row_id>
</data>"
_debug "Deleting dns row using WAPI ..."
_debug "Deleting dns row using WAPI ..."
if ! _wapi_post "dns-row-delete""${data}";then
_err "Error on WAPI request to delete dns row, response: ${response}"
return1
fi
if ! _wapi_post "dns-row-delete""${data}";then
_err "Error on WAPI request to delete dns row, response: ${response}"
return1
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}'"