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.

366 lines
12 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. # Author Michal Tuma <mxtuma@gmail.com>
  5. # For issues send me an email
  6. WEDOS_WAPI_ENDPOINT="https://api.wedos.com/wapi/xml"
  7. TESTING_STAGE=
  8. ######## Public functions #####################
  9. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  10. dns_wedos_add() {
  11. fulldomain=$1
  12. txtvalue=$2
  13. WEDOS_Username="${WEDOS_Username:-$(_readaccountconf_mutable WEDOS_Username)}"
  14. WEDOS_Wapipass="${WEDOS_Wapipass:-$(_readaccountconf_mutable WEDOS_Wapipass)}"
  15. WEDOS_Authtoken="${WEDOS_Authtoken:-$(_readaccountconf_mutable WEDOS_Authtoken)}"
  16. if [ "${WEDOS_Authtoken}" ]; then
  17. _debug "WEDOS Authtoken was already saved, using saved one"
  18. _saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}"
  19. else
  20. if [ -z "${WEDOS_Username}" ] || [ -z "${WEDOS_Wapipass}" ]; then
  21. WEDOS_Username=""
  22. WEDOS_Wapipass=""
  23. _err "You didn't specify a WEDOS's username and wapi key yet."
  24. _err "Please type: export WEDOS_Username=<your user name to login to wedos web account>"
  25. _err "And: export WEDOS_Wapipass=<your WAPI passwords you setup using wedos web pages>"
  26. _err "After you export those variables, run the script again, the values will be saved for future"
  27. return 1
  28. fi
  29. #build WEDOS_Authtoken
  30. _debug "WEDOS Authtoken were not saved yet, building"
  31. WEDOS_Authtoken=$(printf '%s' "${WEDOS_Wapipass}" | sha1sum | head -c 40)
  32. _debug "WEDOS_Authtoken step 1, WAPI PASS sha1 sum: '${WEDOS_Authtoken}'"
  33. WEDOS_Authtoken="${WEDOS_Username}${WEDOS_Authtoken}"
  34. _debug "WEDOS_Authtoken step 2, username concat with token without hours: '${WEDOS_Authtoken}'"
  35. #save details
  36. _saveaccountconf_mutable WEDOS_Username "${WEDOS_Username}"
  37. _saveaccountconf_mutable WEDOS_Wapipass "${WEDOS_Wapipass}"
  38. _saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}"
  39. fi
  40. if ! _get_root "${fulldomain}"; then
  41. _err "WEDOS Account do not contain primary domain to fullfill add of ${fulldomain}!"
  42. return 1
  43. fi
  44. _debug _sub_domain "${_sub_domain}"
  45. _debug _domain "${_domain}"
  46. if _wapi_row_add "${_domain}" "${_sub_domain}" "${txtvalue}" "300"; then
  47. _info "WEDOS WAPI: dns record added and dns changes were commited"
  48. return 0
  49. else
  50. _err "FAILED TO ADD DNS RECORD OR COMMIT DNS CHANGES"
  51. return 1
  52. fi
  53. }
  54. #fulldomain txtvalue
  55. dns_wedos_rm() {
  56. fulldomain=$1
  57. txtvalue=$2
  58. WEDOS_Username="${WEDOS_Username:-$(_readaccountconf_mutable WEDOS_Username)}"
  59. WEDOS_Wapipass="${WEDOS_Wapipass:-$(_readaccountconf_mutable WEDOS_Wapipass)}"
  60. WEDOS_Authtoken="${WEDOS_Authtoken:-$(_readaccountconf_mutable WEDOS_Authtoken)}"
  61. if [ "${WEDOS_Authtoken}" ]; then
  62. _debug "WEDOS Authtoken was already saved, using saved one"
  63. _saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}"
  64. else
  65. if [ -z "${WEDOS_Username}" ] || [ -z "${WEDOS_Wapipass}" ]; then
  66. WEDOS_Username=""
  67. WEDOS_Wapipass=""
  68. _err "You didn't specify a WEDOS's username and wapi key yet."
  69. _err "Please type: export WEDOS_Username=<your user name to login to wedos web account>"
  70. _err "And: export WEDOS_Wapipass=<your WAPI passwords you setup using wedos web pages>"
  71. _err "After you export those variables, run the script again, the values will be saved for future"
  72. return 1
  73. fi
  74. #build WEDOS_Authtoken
  75. _debug "WEDOS Authtoken were not saved yet, building"
  76. WEDOS_Authtoken=$(printf '%s' "${WEDOS_Wapipass}" | sha1sum | head -c 40)
  77. _debug "WEDOS_Authtoken step 1, WAPI PASS sha1 sum: '${WEDOS_Authtoken}'"
  78. WEDOS_Authtoken="${WEDOS_Username}${WEDOS_Authtoken}"
  79. _debug "WEDOS_Authtoken step 2, username concat with token without hours: '${WEDOS_Authtoken}'"
  80. #save details
  81. _saveaccountconf_mutable WEDOS_Username "${WEDOS_Username}"
  82. _saveaccountconf_mutable WEDOS_Wapipass "${WEDOS_Wapipass}"
  83. _saveaccountconf_mutable WEDOS_Authtoken "${WEDOS_Authtoken}"
  84. fi
  85. if ! _get_root "${fulldomain}"; then
  86. _err "WEDOS Account do not contain primary domain to fullfill add of ${fulldomain}!"
  87. return 1
  88. fi
  89. _debug _sub_domain "${_sub_domain}"
  90. _debug _domain "${_domain}"
  91. if _wapi_find_row "${_domain}" "${_sub_domain}" "${txtvalue}"; then
  92. _info "WEDOS WAPI: dns record found with id '${_row_id}'"
  93. if _wapi_delete_row "${_domain}" "${_row_id}"; then
  94. _info "WEDOS WAPI: dns row were deleted and changes commited!"
  95. return 0
  96. fi
  97. fi
  98. _err "Requested dns row were not found or was imposible to delete it, do it manually"
  99. _err "Delete: ${fulldomain}"
  100. _err "Value: ${txtvalue}"
  101. return 1
  102. }
  103. #################### Private functions below ##################################
  104. # Function _wapi_post(), only takes data, prepares auth token and provide result
  105. # $1 - WAPI command string, like 'dns-domains-list'
  106. # $2 - WAPI data for given command, is not required
  107. # returns WAPI response if request were successfully delivered to WAPI endpoint
  108. _wapi_post() {
  109. command=$1
  110. data=$2
  111. _debug "Command : ${command}"
  112. _debug "Data : ${data}"
  113. if [ -z "${command}" ]; then
  114. _err "No command were provided, implamantation error!"
  115. return 1
  116. fi
  117. # Prepare authentification token
  118. hour=$(date +%H)
  119. token=$(printf '%s' "${WEDOS_Authtoken}${hour}" | sha1sum | head -c 40)
  120. _debug "Authentification token is '${token}'"
  121. # Build xml request
  122. request="request=<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
  123. <request>\
  124. <user>${WEDOS_Username}</user>\
  125. <auth>${token}</auth>\
  126. <command>${command}</command>"
  127. if [ -z "${data}" ]; then
  128. echo "" 1>/dev/null
  129. else
  130. request="${request}${data}"
  131. fi
  132. if [ -z "$TESTING_STAGE" ]; then
  133. echo "" 1>/dev/null
  134. else
  135. request="${request}\
  136. <test>1</test>"
  137. fi
  138. request="${request}\
  139. </request>"
  140. _debug "Request to WAPI is: ${request}"
  141. if ! response="$(_post "${request}" "$WEDOS_WAPI_ENDPOINT")"; then
  142. _err "Error contacting WEDOS WAPI with command ${command}"
  143. return 1
  144. fi
  145. _debug "Response : ${response}"
  146. echo "${response}" | grep "<code>1000</code>" 1>/dev/null 2>/dev/null
  147. return "$?"
  148. }
  149. # _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
  150. # $1 - full domain to verify, ie _acme_challenge.www.example.com
  151. # build ${_domain} found at WEDOS, like example.com and ${_sub_domain} from provided full domain, like _acme_challenge.www
  152. _get_root() {
  153. domain=$1
  154. if [ -z "${domain}" ]; then
  155. _err "Function _get_root was called without argument, implementation error!"
  156. return 1
  157. fi
  158. _debug "Get root for domain: ${domain}"
  159. _debug "Getting list of domains using WAPI ..."
  160. if ! _wapi_post "dns-domains-list"; then
  161. _err "Error on WAPI request for list of domains, response : ${response}"
  162. return 1
  163. else
  164. _debug "DNS list were successfully retrieved, response : ${response}"
  165. fi
  166. 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
  167. _debug "Active and primary XML DOMAIN found: ${xml_domain}"
  168. end_of_name=$((${#xml_domain} - 7))
  169. xml_domain_name=$(echo "${xml_domain}" | cut -c 7-${end_of_name})
  170. _debug "Found primary active domain: ${xml_domain_name}"
  171. regex=".*\\."$(echo "${xml_domain_name}" | sed 's/\./\\./g')
  172. _debug "Regex for matching domain: '${regex}'"
  173. if ! echo "${domain}" | grep -E "${regex}" 1>/dev/null 2>/dev/null; then
  174. _debug "found domain do not match required"
  175. else
  176. end_of_name=$((${#domain} - ${#xml_domain_name} - 1))
  177. _domain=${xml_domain_name}
  178. _sub_domain=$(echo "${domain}" | cut -c -${end_of_name})
  179. _info "Domain '${_domain}' was found at WEDOS account as primary, and subdomain is '${_sub_domain}'!"
  180. return 0
  181. fi
  182. done
  183. return 1
  184. }
  185. # for provided domain, it commites all performed changes
  186. _wapi_dns_commit() {
  187. domain=$1
  188. if [ -z "${domain}" ]; then
  189. _err "Invalid request to commit dns changes, domain is empty, implementation error!"
  190. return 1
  191. fi
  192. data=" <data>\
  193. <name>${domain}</name>\
  194. </data>"
  195. if ! _wapi_post "dns-domain-commit" "${data}"; then
  196. _err "Error on WAPI request to commit DNS changes, response : ${response}"
  197. _err "PLEASE USE WEB ACCESS TO CHECK IF CHANGES ARE REQUIRED TO COMMIT OR ROLLBACKED IMMEDIATELLY!"
  198. return 1
  199. else
  200. _debug "DNS CHANGES COMMITED, response : ${response}"
  201. _info "WEDOS DNS WAPI: Changes were commited to domain '${domain}'"
  202. fi
  203. return 0
  204. }
  205. # add one TXT dns row to a specified fomain
  206. _wapi_row_add() {
  207. domain=$1
  208. sub_domain=$2
  209. value=$3
  210. ttl=$4
  211. if [ -z "${domain}" ] || [ -z "${sub_domain}" ] || [ -z "${value}" ] || [ -z "${ttl}" ]; then
  212. _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!"
  213. return 1
  214. fi
  215. # Prepare data for request to WAPI
  216. data=" <data>\
  217. <domain>${domain}</domain>\
  218. <name>${sub_domain}</name>\
  219. <ttl>${ttl}</ttl>\
  220. <type>TXT</type>\
  221. <rdata>${value}</rdata>\
  222. <auth_comment>Created using WAPI from acme.sh</auth_comment>\
  223. </data>"
  224. _debug "Adding row using WAPI ..."
  225. if ! _wapi_post "dns-row-add" "${data}"; then
  226. _err "Error on WAPI request to add new TXT row, response : ${response}"
  227. return 1
  228. else
  229. _debug "ROW ADDED, response : ${response}"
  230. _info "WEDOS DNS WAPI: Row to domain '${domain}' with name '${sub_domain}' were successfully added with value '${value}' and ttl set to ${ttl}"
  231. fi
  232. # Now we have to commit
  233. _wapi_dns_commit "${domain}"
  234. return "$?"
  235. }
  236. _wapi_find_row() {
  237. domain=$1
  238. sub_domain=$2
  239. value=$3
  240. if [ -z "${domain}" ] || [ -z "${sub_domain}" ] || [ -z "${value}" ]; then
  241. _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!"
  242. return 1
  243. fi
  244. data=" <data>\
  245. <domain>${domain}</domain>\
  246. </data>"
  247. _debug "Searching rows using WAPI ..."
  248. if ! _wapi_post "dns-rows-list" "${data}"; then
  249. _err "Error on WAPI request to list domain rows, response : ${response}"
  250. return 1
  251. fi
  252. _debug "Domain rows found, response : ${response}"
  253. sub_domain_regex=$(echo "${sub_domain}" | sed "s/\./\\\\./g")
  254. _debug "Subdomain regex '${sub_domain_regex}'"
  255. 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
  256. _debug "Found row in DNS with ID : ${xml_row}"
  257. _row_id=$(echo "${xml_row}" | grep -o -E "[0-9]*")
  258. _info "WEDOS API: Found DNS row id ${_row_id} for domain ${domain}"
  259. return 0
  260. done
  261. _info "WEDOS API: No TXT row found for domain '${domain}' with name '${sub_domain}' and value '${value}'"
  262. return 1
  263. }
  264. _wapi_delete_row() {
  265. domain=$1
  266. row_id=$2
  267. if [ -z "${domain}" ] || [ -z "${row_id}" ]; then
  268. _err "Invalid request to delete domain dns row, domain: '${domain}' and row_id: '${row_id}', one of required input were not provided, implementation error!"
  269. return 1
  270. fi
  271. data=" <data>\
  272. <domain>${domain}</domain>
  273. <row_id>${row_id}</row_id>
  274. </data>"
  275. _debug "Deleting dns row using WAPI ..."
  276. if ! _wapi_post "dns-row-delete" "${data}"; then
  277. _err "Error on WAPI request to delete dns row, response: ${response}"
  278. return 1
  279. fi
  280. _debug "DNS row were deleted, response: ${response}"
  281. _info "WEDOS API: Required dns domain row with row_id '${row_id}' were correctly deleted at domain '${domain}'"
  282. # Now we have to commit changes
  283. _wapi_dns_commit "${domain}"
  284. return "$?"
  285. }