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.

244 lines
9.0 KiB

6 years ago
  1. #!/usr/bin/env sh
  2. # This is the OpenProvider API wrapper for acme.sh
  3. #
  4. # Author: Sylvia van Os
  5. # Report Bugs here: https://github.com/acmesh-official/acme.sh/issues/2104
  6. #
  7. # export OPENPROVIDER_USER="username"
  8. # export OPENPROVIDER_PASSWORDHASH="hashed_password"
  9. #
  10. # Usage:
  11. # acme.sh --issue --dns dns_openprovider -d example.com
  12. OPENPROVIDER_API="https://api.openprovider.eu/"
  13. #OPENPROVIDER_API="https://api.cte.openprovider.eu/" # Test API
  14. ######## Public functions #####################
  15. #Usage: dns_openprovider_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  16. dns_openprovider_add() {
  17. fulldomain="$1"
  18. txtvalue="$2"
  19. OPENPROVIDER_USER="${OPENPROVIDER_USER:-$(_readaccountconf_mutable OPENPROVIDER_USER)}"
  20. OPENPROVIDER_PASSWORDHASH="${OPENPROVIDER_PASSWORDHASH:-$(_readaccountconf_mutable OPENPROVIDER_PASSWORDHASH)}"
  21. if [ -z "$OPENPROVIDER_USER" ] || [ -z "$OPENPROVIDER_PASSWORDHASH" ]; then
  22. _err "You didn't specify the openprovider user and/or password hash."
  23. return 1
  24. fi
  25. # save the username and password to the account conf file.
  26. _saveaccountconf_mutable OPENPROVIDER_USER "$OPENPROVIDER_USER"
  27. _saveaccountconf_mutable OPENPROVIDER_PASSWORDHASH "$OPENPROVIDER_PASSWORDHASH"
  28. _debug "First detect the root zone"
  29. if ! _get_root "$fulldomain"; then
  30. _err "invalid domain"
  31. return 1
  32. fi
  33. _debug _domain_name "$_domain_name"
  34. _debug _domain_extension "$_domain_extension"
  35. _debug "Getting current records"
  36. existing_items=""
  37. results_retrieved=0
  38. while true; do
  39. _openprovider_request "$(printf '<searchZoneRecordDnsRequest><name>%s.%s</name><offset>%s</offset></searchZoneRecordDnsRequest>' "$_domain_name" "$_domain_extension" "$results_retrieved")"
  40. items="$response"
  41. while true; do
  42. item="$(echo "$items" | _egrep_o '<openXML>.*<\/openXML>' | sed -n 's/.*\(<item>.*<\/item>\).*/\1/p')"
  43. _debug existing_items "$existing_items"
  44. _debug results_retrieved "$results_retrieved"
  45. _debug item "$item"
  46. if [ -z "$item" ]; then
  47. break
  48. fi
  49. items="$(echo "$items" | sed "s|${item}||")"
  50. results_retrieved="$(_math "$results_retrieved" + 1)"
  51. new_item="$(echo "$item" | sed -n 's/.*<item>.*\(<name>\(.*\)\.'"$_domain_name"'\.'"$_domain_extension"'<\/name>.*\(<type>.*<\/type>\).*\(<value>.*<\/value>\).*\(<prio>.*<\/prio>\).*\(<ttl>.*<\/ttl>\)\).*<\/item>.*/<item><name>\2<\/name>\3\4\5\6<\/item>/p')"
  52. if [ -z "$new_item" ]; then
  53. # Base record
  54. new_item="$(echo "$item" | sed -n 's/.*<item>.*\(<name>\(.*\)'"$_domain_name"'\.'"$_domain_extension"'<\/name>.*\(<type>.*<\/type>\).*\(<value>.*<\/value>\).*\(<prio>.*<\/prio>\).*\(<ttl>.*<\/ttl>\)\).*<\/item>.*/<item><name>\2<\/name>\3\4\5\6<\/item>/p')"
  55. fi
  56. if [ -z "$(echo "$new_item" | _egrep_o ".*<type>(A|AAAA|CNAME|MX|SPF|SRV|TXT|TLSA|SSHFP|CAA)<\/type>.*")" ]; then
  57. _debug "not an allowed record type, skipping" "$new_item"
  58. continue
  59. fi
  60. existing_items="$existing_items$new_item"
  61. done
  62. total="$(echo "$response" | _egrep_o '<total>.*?<\/total>' | sed -n 's/.*<total>\(.*\)<\/total>.*/\1/p')"
  63. _debug total "$total"
  64. if [ "$results_retrieved" -eq "$total" ]; then
  65. break
  66. fi
  67. done
  68. _debug "Creating acme record"
  69. acme_record="$(echo "$fulldomain" | sed -e "s/.$_domain_name.$_domain_extension$//")"
  70. _openprovider_request "$(printf '<modifyZoneDnsRequest><domain><name>%s</name><extension>%s</extension></domain><type>master</type><records><array>%s<item><name>%s</name><type>TXT</type><value>%s</value><ttl>86400</ttl></item></array></records></modifyZoneDnsRequest>' "$_domain_name" "$_domain_extension" "$existing_items" "$acme_record" "$txtvalue")"
  71. return 0
  72. }
  73. #Usage: fulldomain txtvalue
  74. #Remove the txt record after validation.
  75. dns_openprovider_rm() {
  76. fulldomain="$1"
  77. txtvalue="$2"
  78. OPENPROVIDER_USER="${OPENPROVIDER_USER:-$(_readaccountconf_mutable OPENPROVIDER_USER)}"
  79. OPENPROVIDER_PASSWORDHASH="${OPENPROVIDER_PASSWORDHASH:-$(_readaccountconf_mutable OPENPROVIDER_PASSWORDHASH)}"
  80. if [ -z "$OPENPROVIDER_USER" ] || [ -z "$OPENPROVIDER_PASSWORDHASH" ]; then
  81. _err "You didn't specify the openprovider user and/or password hash."
  82. return 1
  83. fi
  84. # save the username and password to the account conf file.
  85. _saveaccountconf_mutable OPENPROVIDER_USER "$OPENPROVIDER_USER"
  86. _saveaccountconf_mutable OPENPROVIDER_PASSWORDHASH "$OPENPROVIDER_PASSWORDHASH"
  87. _debug "First detect the root zone"
  88. if ! _get_root "$fulldomain"; then
  89. _err "invalid domain"
  90. return 1
  91. fi
  92. _debug _domain_name "$_domain_name"
  93. _debug _domain_extension "$_domain_extension"
  94. _debug "Getting current records"
  95. existing_items=""
  96. results_retrieved=0
  97. while true; do
  98. _openprovider_request "$(printf '<searchZoneRecordDnsRequest><name>%s.%s</name><offset>%s</offset></searchZoneRecordDnsRequest>' "$_domain_name" "$_domain_extension" "$results_retrieved")"
  99. # Remove acme records from items
  100. items="$response"
  101. while true; do
  102. item="$(echo "$items" | _egrep_o '<openXML>.*<\/openXML>' | sed -n 's/.*\(<item>.*<\/item>\).*/\1/p')"
  103. _debug existing_items "$existing_items"
  104. _debug results_retrieved "$results_retrieved"
  105. _debug item "$item"
  106. if [ -z "$item" ]; then
  107. break
  108. fi
  109. items="$(echo "$items" | sed "s|${item}||")"
  110. results_retrieved="$(_math "$results_retrieved" + 1)"
  111. if ! echo "$item" | grep -v "$fulldomain"; then
  112. _debug "acme record, skipping" "$item"
  113. continue
  114. fi
  115. new_item="$(echo "$item" | sed -n 's/.*<item>.*\(<name>\(.*\)\.'"$_domain_name"'\.'"$_domain_extension"'<\/name>.*\(<type>.*<\/type>\).*\(<value>.*<\/value>\).*\(<prio>.*<\/prio>\).*\(<ttl>.*<\/ttl>\)\).*<\/item>.*/<item><name>\2<\/name>\3\4\5\6<\/item>/p')"
  116. if [ -z "$new_item" ]; then
  117. # Base record
  118. new_item="$(echo "$item" | sed -n 's/.*<item>.*\(<name>\(.*\)'"$_domain_name"'\.'"$_domain_extension"'<\/name>.*\(<type>.*<\/type>\).*\(<value>.*<\/value>\).*\(<prio>.*<\/prio>\).*\(<ttl>.*<\/ttl>\)\).*<\/item>.*/<item><name>\2<\/name>\3\4\5\6<\/item>/p')"
  119. fi
  120. if [ -z "$(echo "$new_item" | _egrep_o ".*<type>(A|AAAA|CNAME|MX|SPF|SRV|TXT|TLSA|SSHFP|CAA)<\/type>.*")" ]; then
  121. _debug "not an allowed record type, skipping" "$new_item"
  122. continue
  123. fi
  124. existing_items="$existing_items$new_item"
  125. done
  126. total="$(echo "$response" | _egrep_o '<total>.*?<\/total>' | sed -n 's/.*<total>\(.*\)<\/total>.*/\1/p')"
  127. _debug total "$total"
  128. if [ "$results_retrieved" -eq "$total" ]; then
  129. break
  130. fi
  131. done
  132. _debug "Removing acme record"
  133. _openprovider_request "$(printf '<modifyZoneDnsRequest><domain><name>%s</name><extension>%s</extension></domain><type>master</type><records><array>%s</array></records></modifyZoneDnsRequest>' "$_domain_name" "$_domain_extension" "$existing_items")"
  134. return 0
  135. }
  136. #################### Private functions below ##################################
  137. #_acme-challenge.www.domain.com
  138. #returns
  139. # _domain_name=domain
  140. # _domain_extension=com
  141. _get_root() {
  142. domain=$1
  143. i=2
  144. results_retrieved=0
  145. while true; do
  146. h=$(echo "$domain" | cut -d . -f $i-100)
  147. _debug h "$h"
  148. if [ -z "$h" ]; then
  149. #not valid
  150. return 1
  151. fi
  152. _openprovider_request "$(printf '<searchDomainRequest><domainNamePattern>%s</domainNamePattern><offset>%s</offset></searchDomainRequest>' "$(echo "$h" | cut -d . -f 1)" "$results_retrieved")"
  153. items="$response"
  154. while true; do
  155. item="$(echo "$items" | _egrep_o '<openXML>.*<\/openXML>' | sed -n 's/.*\(<domain>.*<\/domain>\).*/\1/p')"
  156. _debug existing_items "$existing_items"
  157. _debug results_retrieved "$results_retrieved"
  158. _debug item "$item"
  159. if [ -z "$item" ]; then
  160. break
  161. fi
  162. items="$(echo "$items" | sed "s|${item}||")"
  163. results_retrieved="$(_math "$results_retrieved" + 1)"
  164. _domain_name="$(echo "$item" | sed -n 's/.*<domain>.*<name>\(.*\)<\/name>.*<\/domain>.*/\1/p')"
  165. _domain_extension="$(echo "$item" | sed -n 's/.*<domain>.*<extension>\(.*\)<\/extension>.*<\/domain>.*/\1/p')"
  166. _debug _domain_name "$_domain_name"
  167. _debug _domain_extension "$_domain_extension"
  168. if [ "$_domain_name.$_domain_extension" = "$h" ]; then
  169. return 0
  170. fi
  171. done
  172. total="$(echo "$response" | _egrep_o '<total>.*?<\/total>' | sed -n 's/.*<total>\(.*\)<\/total>.*/\1/p')"
  173. _debug total "$total"
  174. if [ "$results_retrieved" -eq "$total" ]; then
  175. results_retrieved=0
  176. i="$(_math "$i" + 1)"
  177. fi
  178. done
  179. return 1
  180. }
  181. _openprovider_request() {
  182. request_xml=$1
  183. xml_prefix='<?xml version="1.0" encoding="UTF-8"?>'
  184. xml_content=$(printf '<openXML><credentials><username>%s</username><hash>%s</hash></credentials>%s</openXML>' "$OPENPROVIDER_USER" "$OPENPROVIDER_PASSWORDHASH" "$request_xml")
  185. response="$(_post "$(echo "$xml_prefix$xml_content" | tr -d '\n')" "$OPENPROVIDER_API" "" "POST" "application/xml")"
  186. _debug response "$response"
  187. if ! _contains "$response" "<openXML><reply><code>0</code>.*</reply></openXML>"; then
  188. _err "API request failed."
  189. return 1
  190. fi
  191. }