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.

215 lines
5.0 KiB

  1. #!/usr/bin/env sh
  2. #
  3. #Author: lebaned
  4. #Report Bugs here: https://github.com/lebaned/acme.sh
  5. #
  6. ######## Public functions #####################
  7. #Usage: dns_versio_add _acme-challenge.www.domain.com "[txtvalue]"
  8. dns_versio_add() {
  9. fulldomain=$1
  10. txtvalue=$2
  11. _info "Using Versio"
  12. _debug fulldomain "$fulldomain"
  13. _debug txtvalue "$txtvalue"
  14. if ! _get_credentials; then
  15. return 1
  16. fi
  17. #save the credentials to the account conf file.
  18. _saveaccountconf_mutable Versio_Username "$Versio_Username"
  19. _saveaccountconf_mutable Versio_Password "$Versio_Password"
  20. _debug "First detect the root zone"
  21. if ! _get_root "$fulldomain"; then
  22. _err "invalid domain"
  23. return 1
  24. fi
  25. _info fulldomain "$fulldomain"
  26. _info _domain "$_domain"
  27. _info _sub_domain "$_sub_domain"
  28. if ! _get_dns_records "$_domain"; then
  29. _err "invalid domain"
  30. return 1
  31. fi
  32. _debug "orginal dnsrecords" "$_dns_records"
  33. _delete_dns_record "TXT" "$fulldomain."
  34. _debug "dnsrecords after deleted old record" "$_dns_records"
  35. _add_dns_record "TXT" "$fulldomain" "\\\"$txtvalue\\\"" 0 300
  36. _debug "dnsrecords after add record" "{\"dns_records\":[$_dns_records]}"
  37. if _versio_rest POST "domains/$_domain/update" "{\"dns_records\":[$_dns_records]}"; then
  38. _debug "rest update response" "$response"
  39. return 0
  40. fi
  41. _err "Error!"
  42. return 1
  43. }
  44. #Usage: fulldomain txtvalue
  45. #Remove the txt record after validation.
  46. dns_versio_rm() {
  47. fulldomain=$1
  48. txtvalue=$2
  49. _info "Using Versio"
  50. _debug fulldomain "$fulldomain"
  51. _debug txtvalue "$txtvalue"
  52. if ! _get_credentials; then
  53. return 1
  54. fi
  55. _debug "First detect the root zone"
  56. if ! _get_root "$fulldomain"; then
  57. _err "invalid domain"
  58. return 1
  59. fi
  60. _info fulldomain "$fulldomain"
  61. _info _domain "$_domain"
  62. _info _sub_domain "$_sub_domain"
  63. if ! _get_dns_records "$_domain"; then
  64. _err "invalid domain"
  65. return 1
  66. fi
  67. _debug "orginal dnsrecords" "$_dns_records"
  68. _delete_dns_record "TXT" "$fulldomain."
  69. _debug "dnsrecords after deleted old record" "$_dns_records"
  70. if _versio_rest POST "domains/$_domain/update" "{\"dns_records\":[$_dns_records]}"; then
  71. _debug "rest update response" "$response"
  72. return 0
  73. fi
  74. _err "Error!"
  75. return 1
  76. }
  77. #################### Private functions below ##################################
  78. #_acme-challenge.www.domain.com
  79. #returns
  80. # _sub_domain=_acme-challenge.www
  81. # _domain=domain.com
  82. _get_root() {
  83. domain=$1
  84. i=2
  85. p=1
  86. if _versio_rest GET "domains?status=OK"; then
  87. response="$(echo "$response" | tr -d "\n" | sed 's/{/\n&/g')"
  88. while true; do
  89. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  90. _info h "$h"
  91. _debug h "$h"
  92. if [ -z "$h" ]; then
  93. #not valid
  94. return 1
  95. fi
  96. hostedzone="$(echo "$response" | _egrep_o "{.*\"domain\":\s*\"$h\".*}")"
  97. if [ "$hostedzone" ]; then
  98. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  99. _domain=$h
  100. return 0
  101. fi
  102. p=$i
  103. i=$(_math "$i" + 1)
  104. done
  105. fi
  106. return 1
  107. }
  108. #parameters: [record type] [record name]
  109. _delete_dns_record() {
  110. _dns_records=$(echo "$_dns_records" | sed 's/{"type":"'"$1"'","name":"'"$2"'"[^}]*}[,]\?//' | sed 's/,$//')
  111. }
  112. #parameters: [type] [name] [value] [prio] [ttl]
  113. _add_dns_record() {
  114. _dns_records="$_dns_records,{\"type\":\"$1\",\"name\":\"$2\",\"value\":\"$3\",\"prio\":$4,\"ttl\":$5}"
  115. }
  116. #parameters: [root domain]
  117. #returns
  118. # _dns_records
  119. _get_dns_records() {
  120. if _versio_rest GET "domains/$1?show_dns_records=true"; then
  121. _dns_records=$(echo "$response" | grep -oP '(?<="dns_records":\[)[^\]]*')
  122. return 0
  123. fi
  124. return 1
  125. }
  126. #method uri qstr data
  127. _versio_rest() {
  128. mtd="$1"
  129. ep="$2"
  130. data="$3"
  131. _debug mtd "$mtd"
  132. _debug ep "$ep"
  133. VERSIO_API_URL="https://www.versio.nl/api/v1"
  134. VERSIO_CREDENTIALS_BASE64=$(printf "%s:%s" "$Versio_Username" "$Versio_Password" | openssl enc -base64)
  135. export _H1="Accept: application/json"
  136. export _H2="Content-Type: application/json"
  137. export _H3="Authorization: Basic $VERSIO_CREDENTIALS_BASE64"
  138. if [ "$mtd" != "GET" ]; then
  139. # both POST and DELETE.
  140. _debug data "$data"
  141. response="$(_post "$data" "$VERSIO_API_URL/$ep" "" "$mtd")"
  142. else
  143. response="$(_get "$VERSIO_API_URL/$ep")"
  144. fi
  145. case $? in
  146. 0)
  147. _debug response "$response"
  148. return 0
  149. ;;
  150. 6)
  151. _err "Authentication failure. Check your Versio email address and password"
  152. return 1
  153. ;;
  154. *)
  155. _err "Unknown error"
  156. return 1
  157. ;;
  158. esac
  159. }
  160. #parameters: []
  161. #returns:
  162. # Versio_Username
  163. # Versio_Password
  164. _get_credentials() {
  165. Versio_Username="${Versio_Username:-$(_readaccountconf_mutable Versio_Username)}"
  166. Versio_Password="${Versio_Password:-$(_readaccountconf_mutable Versio_Password)}"
  167. if [ -z "$Versio_Username" ] || [ -z "$Versio_Password" ]; then
  168. Versio_Username=""
  169. Versio_Password=""
  170. _err "You don't specify Versio email address and/or password yet."
  171. _err "Example:"
  172. _err "export Versio_Username=[email address]"
  173. _err "export Versio_Password=[password]"
  174. _err "Please create you key and try again."
  175. return 1
  176. fi
  177. return 0
  178. }