You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

410 lines
16 KiB

  1. #!/usr/bin/env sh
  2. # This script has been created at June 2020, based on knowledge base of wedos.com provider.
  3. # It is intended to allow DNS-01 challenges for acme.sh using wedos's WAPI using XML.
  4. # See WIKI page how to use it https://github.com/acmesh-official/acme.sh/wiki/dnsapi#117-use-wedos-dns-api
  5. # Author Michal Tuma <mxtuma@gmail.com>
  6. # For issues, please perform the action with --debug switch and report to https://github.com/acmesh-official/acme.sh/issues/3166
  7. # MAIN WAPI ENDPOINT
  8. WEDOS_WAPI_ENDPOINT="https://api.wedos.com/wapi/xml"
  9. # WHEN SET TO ANYTHINK, THEN GENERATED XML WAPI REQUEST ADD TESTING SWITCH
  10. TESTING_STAGE=
  11. ######## Public functions #####################
  12. # Main implemented function for acme.sh.
  13. # Function manages provided user informations, parse requested domain and subdomain name and create new TXT row with provided value.
  14. # WEDOS WAPI Requests usage:
  15. # - dns-domains-list : to retrieve a list of valid managed domains and check input $fulldomain
  16. # - dns-row-add : to add new TXT row to a $fulldomain with $txtvalue set
  17. # - dns-domain-commit : to commit added dns row
  18. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  19. dns_wedos_add() {
  20. fulldomain=$1
  21. txtvalue=$2
  22. WEDOS_Username="${WEDOS_Username:-$(_readaccountconf_mutable WEDOS_Username)}"
  23. WEDOS_Wapipass="${WEDOS_Wapipass:-$(_readaccountconf_mutable WEDOS_Wapipass)}"
  24. WEDOS_Authtoken="${WEDOS_Authtoken:-$(_readaccountconf_mutable WEDOS_Authtoken)}"
  25. if [ "${WEDOS_Authtoken}" ]; then
  26. _debug "WEDOS Authtoken was already saved, using saved one"
  27. _saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}"
  28. else
  29. if [ -z "${WEDOS_Username}" ] || [ -z "${WEDOS_Wapipass}" ]; then
  30. WEDOS_Username=""
  31. WEDOS_Wapipass=""
  32. _err "You didn't specify a WEDOS's username and wapi key yet."
  33. _err "Please type: export WEDOS_Username=<your user name to login to wedos web account>"
  34. _err "And: export WEDOS_Wapipass=<your WAPI passwords you setup using wedos web pages>"
  35. _err "After you export those variables, run the script again, the values will be saved for future"
  36. return 1
  37. fi
  38. #build WEDOS_Authtoken
  39. _debug "WEDOS Authtoken were not saved yet, building"
  40. WEDOS_Authtoken=$(printf '%s' "${WEDOS_Wapipass}" | _digest "sha1" "true" | head -c 40)
  41. _debug "WEDOS_Authtoken step 1, WAPI PASS sha1 sum: '${WEDOS_Authtoken}'"
  42. WEDOS_Authtoken="${WEDOS_Username}${WEDOS_Authtoken}"
  43. _debug "WEDOS_Authtoken step 2, username concat with token without hours: '${WEDOS_Authtoken}'"
  44. #save details
  45. _saveaccountconf_mutable WEDOS_Username "${WEDOS_Username}"
  46. _saveaccountconf_mutable WEDOS_Wapipass "${WEDOS_Wapipass}"
  47. _saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}"
  48. fi
  49. if ! _get_root "${fulldomain}"; then
  50. _err "WEDOS Account do not contain primary domain to fullfill add of ${fulldomain}!"
  51. return 1
  52. fi
  53. _debug _sub_domain "${_sub_domain}"
  54. _debug _domain "${_domain}"
  55. if _wapi_row_add "${_domain}" "${_sub_domain}" "${txtvalue}" "300"; then
  56. _info "WEDOS WAPI: dns record added and dns changes were commited"
  57. return 0
  58. else
  59. _err "FAILED TO ADD DNS RECORD OR COMMIT DNS CHANGES"
  60. return 1
  61. fi
  62. }
  63. # Main implemented function for acme.sh
  64. # This function verify provided domain if is managed by stored account, try to find TXT row for the domain and removes it if it is found.
  65. # WEDOS WAPI Requests used:
  66. # - dns-domains-list : to verify requested $fulldomain is managed and to parse what is subdomain from it
  67. # - dns-rows-list : to verify if provided $txtvalue exists as TXT entry
  68. # - dns-row-delete : to request deletion of TXT value
  69. # - dns-domain-commit : to commit deletion
  70. # Usage: rm _acme_challenge.www.domain.org "e89fhwie73869yhe993e27d4hi"
  71. dns_wedos_rm() {
  72. fulldomain=$1
  73. txtvalue=$2
  74. WEDOS_Username="${WEDOS_Username:-$(_readaccountconf_mutable WEDOS_Username)}"
  75. WEDOS_Wapipass="${WEDOS_Wapipass:-$(_readaccountconf_mutable WEDOS_Wapipass)}"
  76. WEDOS_Authtoken="${WEDOS_Authtoken:-$(_readaccountconf_mutable WEDOS_Authtoken)}"
  77. if [ "${WEDOS_Authtoken}" ]; then
  78. _debug "WEDOS Authtoken was already saved, using saved one"
  79. _saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}"
  80. else
  81. if [ -z "${WEDOS_Username}" ] || [ -z "${WEDOS_Wapipass}" ]; then
  82. WEDOS_Username=""
  83. WEDOS_Wapipass=""
  84. _err "You didn't specify a WEDOS's username and wapi key yet."
  85. _err "Please type: export WEDOS_Username=<your user name to login to wedos web account>"
  86. _err "And: export WEDOS_Wapipass=<your WAPI passwords you setup using wedos web pages>"
  87. _err "After you export those variables, run the script again, the values will be saved for future"
  88. return 1
  89. fi
  90. #build WEDOS_Authtoken
  91. _debug "WEDOS Authtoken were not saved yet, building"
  92. WEDOS_Authtoken=$(printf '%s' "${WEDOS_Wapipass}" | sha1sum | head -c 40)
  93. _debug "WEDOS_Authtoken step 1, WAPI PASS sha1 sum: '${WEDOS_Authtoken}'"
  94. WEDOS_Authtoken="${WEDOS_Username}${WEDOS_Authtoken}"
  95. _debug "WEDOS_Authtoken step 2, username concat with token without hours: '${WEDOS_Authtoken}'"
  96. #save details
  97. _saveaccountconf_mutable WEDOS_Username "${WEDOS_Username}"
  98. _saveaccountconf_mutable WEDOS_Wapipass "${WEDOS_Wapipass}"
  99. _saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}"
  100. fi
  101. if ! _get_root "${fulldomain}"; then
  102. _err "WEDOS Account do not contain primary domain to fullfill add of ${fulldomain}!"
  103. return 1
  104. fi
  105. _debug _sub_domain "${_sub_domain}"
  106. _debug _domain "${_domain}"
  107. if _wapi_find_row "${_domain}" "${_sub_domain}" "${txtvalue}"; then
  108. _info "WEDOS WAPI: dns record found with id '${_row_id}'"
  109. if _wapi_delete_row "${_domain}" "${_row_id}"; then
  110. _info "WEDOS WAPI: dns row were deleted and changes commited!"
  111. return 0
  112. fi
  113. fi
  114. _err "Requested dns row were not found or was imposible to delete it, do it manually"
  115. _err "Delete: ${fulldomain}"
  116. _err "Value: ${txtvalue}"
  117. return 1
  118. }
  119. #################### Private functions below ##################################
  120. # Function _wapi_post(), only takes data, prepares auth token and provide result
  121. # $1 - WAPI command string, like 'dns-domains-list'
  122. # $2 - WAPI data for given command, is not required
  123. # returns WAPI response if request were successfully delivered to WAPI endpoint
  124. _wapi_post() {
  125. command=$1
  126. data=$2
  127. _debug "Command : ${command}"
  128. _debug "Data : ${data}"
  129. if [ -z "${command}" ]; then
  130. _err "No command were provided, implamantation error!"
  131. return 1
  132. fi
  133. # Prepare authentification token
  134. hour=$(TZ='Europe/Prague' date +%H)
  135. token=$(printf '%s' "${WEDOS_Authtoken}${hour}" | _digest "sha1" "true" | head -c 40)
  136. _debug "Authentification token is '${token}'"
  137. # Build xml request
  138. request="request=<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
  139. <request>\
  140. <user>${WEDOS_Username}</user>\
  141. <auth>${token}</auth>\
  142. <command>${command}</command>"
  143. if [ -z "${data}" ]; then
  144. echo "" 1>/dev/null
  145. else
  146. request="${request}${data}"
  147. fi
  148. if [ -z "$TESTING_STAGE" ]; then
  149. echo "" 1>/dev/null
  150. else
  151. request="${request}\
  152. <test>1</test>"
  153. fi
  154. request="${request}\
  155. </request>"
  156. _debug "Request to WAPI is: ${request}"
  157. if ! response="$(_post "${request}" "$WEDOS_WAPI_ENDPOINT")"; then
  158. _err "Error contacting WEDOS WAPI with command ${command}"
  159. return 1
  160. fi
  161. _debug "Response : ${response}"
  162. _contains "${response}" "<code>1000</code>"
  163. return "$?"
  164. }
  165. # _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
  166. # $1 - full domain to verify, ie _acme_challenge.www.example.com
  167. # builds ${_domain} found at WEDOS, like example.com and ${_sub_domain} from provided full domain, like _acme_challenge.www
  168. _get_root() {
  169. domain=$1
  170. if [ -z "${domain}" ]; then
  171. _err "Function _get_root was called without argument, implementation error!"
  172. return 1
  173. fi
  174. _debug "Get root for domain: ${domain}"
  175. _debug "Getting list of domains using WAPI ..."
  176. if ! _wapi_post "dns-domains-list"; then
  177. _err "Error on WAPI request for list of domains, response : ${response}"
  178. return 1
  179. else
  180. _debug "DNS list were successfully retrieved, response : ${response}"
  181. fi
  182. # In for each cycle, try parse the response to find primary active domains
  183. # For cycle description:
  184. # 1st tr -d '\011\012\015' = remove all newlines and tab characters - whole XML became single line
  185. # 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
  186. # 3rd sed "s/<\/data>.*$//g" = remove all the data after the data xml element - XML now contains only the content of data xml element
  187. # 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
  188. # 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)
  189. # 6th sed "s/[ ]*<\/domain>/\t/g" = replace all "spaces</domain>" by tab - now we are preparing to create multiple lines
  190. # 7th th '\011' '\n' = replace all tabs from previous sed (Mac OS change) - now we create multiple lines each should contain only <name>...</name><type>...</type><status>...</status>
  191. # 8th sed -n "/<name>\([a-zA-Z0-9_\-\.]\+\)<\/name><type>primary<\/type><status>active<\/status>/p" = remove all non primary or non active domains lines
  192. # 9th sed "s/<name>\([a-zA-Z0-9_\-\.]\+\)<\/name><type>primary<\/type><status>active<\/status>/\1/g" = substitute for domain names only
  193. 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>/\t/g" | tr '\011' '\n' | 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
  194. _debug "Found primary active domain: ${xml_domain}"
  195. if _endswith "${domain}" "${xml_domain}"; then
  196. length_difference=$(_math "${#domain} - ${#xml_domain}")
  197. possible_subdomain=$(echo "${domain}" | cut -c -"${length_difference}")
  198. if _endswith "${possible_subdomain}" "."; then
  199. length_difference=$(_math "${length_difference} - 1")
  200. _domain=${xml_domain}
  201. _sub_domain=$(echo "${possible_subdomain}" | cut -c -"${length_difference}")
  202. _info "Domain '${_domain}' was found at WEDOS account as primary, and subdomain is '${_sub_domain}'!"
  203. return 0
  204. fi
  205. fi
  206. _debug " ... found domain does not match required!"
  207. done
  208. return 1
  209. }
  210. # for provided domain, it commites all performed changes
  211. _wapi_dns_commit() {
  212. domain=$1
  213. if [ -z "${domain}" ]; then
  214. _err "Invalid request to commit dns changes, domain is empty, implementation error!"
  215. return 1
  216. fi
  217. data=" <data>\
  218. <name>${domain}</name>\
  219. </data>"
  220. if ! _wapi_post "dns-domain-commit" "${data}"; then
  221. _err "Error on WAPI request to commit DNS changes, response : ${response}"
  222. _err "PLEASE USE WEB ACCESS TO CHECK IF CHANGES ARE REQUIRED TO COMMIT OR ROLLBACKED IMMEDIATELLY!"
  223. return 1
  224. else
  225. _debug "DNS CHANGES COMMITED, response : ${response}"
  226. _info "WEDOS DNS WAPI: Changes were commited to domain '${domain}'"
  227. fi
  228. return 0
  229. }
  230. # add one TXT dns row to a specified fomain
  231. _wapi_row_add() {
  232. domain=$1
  233. sub_domain=$2
  234. value=$3
  235. ttl=$4
  236. if [ -z "${domain}" ] || [ -z "${sub_domain}" ] || [ -z "${value}" ] || [ -z "${ttl}" ]; then
  237. _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!"
  238. return 1
  239. fi
  240. # Prepare data for request to WAPI
  241. data=" <data>\
  242. <domain>${domain}</domain>\
  243. <name>${sub_domain}</name>\
  244. <ttl>${ttl}</ttl>\
  245. <type>TXT</type>\
  246. <rdata>${value}</rdata>\
  247. <auth_comment>Created using WAPI from acme.sh</auth_comment>\
  248. </data>"
  249. _debug "Adding row using WAPI ..."
  250. if ! _wapi_post "dns-row-add" "${data}"; then
  251. _err "Error on WAPI request to add new TXT row, response : ${response}"
  252. return 1
  253. else
  254. _debug "ROW ADDED, response : ${response}"
  255. _info "WEDOS DNS WAPI: Row to domain '${domain}' with name '${sub_domain}' were successfully added with value '${value}' and ttl set to ${ttl}"
  256. fi
  257. # Now we have to commit
  258. _wapi_dns_commit "${domain}"
  259. return "$?"
  260. }
  261. _wapi_find_row() {
  262. domain=$1
  263. sub_domain=$2
  264. value=$3
  265. if [ -z "${domain}" ] || [ -z "${sub_domain}" ] || [ -z "${value}" ]; then
  266. _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!"
  267. return 1
  268. fi
  269. data=" <data>\
  270. <domain>${domain}</domain>\
  271. </data>"
  272. _debug "Searching rows using WAPI ..."
  273. if ! _wapi_post "dns-rows-list" "${data}"; then
  274. _err "Error on WAPI request to list domain rows, response : ${response}"
  275. return 1
  276. fi
  277. _debug "Domain rows found, response : ${response}"
  278. # Prepare sub domain regex which will be later used for search domain row
  279. # from _acme_challenge.sub it should be _acme_challenge\.sub
  280. sub_domain_regex=$(echo "${sub_domain}" | sed "s/\./\\\\./g")
  281. _debug "Subdomain regex '${sub_domain_regex}'"
  282. # In for each cycle loops over the domains rows, description:
  283. # 1st tr -d '\011\012\015' = delete all newlines and tab characters - XML became a single line
  284. # 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
  285. # 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
  286. # 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
  287. # 5th sed "s/<row>//g" = remove all row xml start tags - XML now contains rows xml element content and its end tag
  288. # 6th sed "s/[ ]*<\/row>/\t/g" = replace all "spaces</row>" by tab - now we are preparing to create multiple lines
  289. # 7th tr '\011' '\n' = replace all tabs with new lines (Mac OS X hint) - we create multiple lines each should contain only single row xml content
  290. # 8th 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
  291. # 9th sed "s/^<ID>\([0-9]\+\)<\/ID>.*<rdata>\(.*\)<\/rdata>.*$/\1-\2/" = replace the whole lines to ID-value pairs
  292. # -- now there are only lines with ID-value but value might contain spaces (BAD FOR FOREACH LOOP) or special characters (BAD FOR REGEX MATCHING)
  293. # 10th grep "${value}" = match only a line containg searched value
  294. # 11th sed "s/^\([0-9]\+\).*$/\1/" = get only ID from the row
  295. 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>/\t/g" | tr '\011' '\n' | 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
  296. _row_id="${xml_row}"
  297. _info "WEDOS API: Found DNS row id ${_row_id} for domain ${domain}"
  298. return 0
  299. done
  300. _info "WEDOS API: No TXT row found for domain '${domain}' with name '${sub_domain}' and value '${value}'"
  301. return 1
  302. }
  303. _wapi_delete_row() {
  304. domain=$1
  305. row_id=$2
  306. if [ -z "${domain}" ] || [ -z "${row_id}" ]; then
  307. _err "Invalid request to delete domain dns row, domain: '${domain}' and row_id: '${row_id}', one of required input were not provided, implementation error!"
  308. return 1
  309. fi
  310. data=" <data>\
  311. <domain>${domain}</domain>
  312. <row_id>${row_id}</row_id>
  313. </data>"
  314. _debug "Deleting dns row using WAPI ..."
  315. if ! _wapi_post "dns-row-delete" "${data}"; then
  316. _err "Error on WAPI request to delete dns row, response: ${response}"
  317. return 1
  318. fi
  319. _debug "DNS row were deleted, response: ${response}"
  320. _info "WEDOS API: Required dns domain row with row_id '${row_id}' were correctly deleted at domain '${domain}'"
  321. # Now we have to commit changes
  322. _wapi_dns_commit "${domain}"
  323. return "$?"
  324. }