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.

214 lines
5.2 KiB

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