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.

211 lines
5.0 KiB

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