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.

193 lines
5.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_transip_info='TransIP.nl
  4. Site: TransIP.nl
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_transip
  6. Options:
  7. TRANSIP_Username Username
  8. TRANSIP_Key_File Private key file path
  9. Issues: github.com/acmesh-official/acme.sh/issues/2949
  10. '
  11. TRANSIP_Api_Url="https://api.transip.nl/v6"
  12. TRANSIP_Token_Read_Only="false"
  13. TRANSIP_Token_Expiration="30 minutes"
  14. # You can't reuse a label token, so we leave this empty normally
  15. TRANSIP_Token_Label=""
  16. ######## Public functions #####################
  17. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  18. dns_transip_add() {
  19. fulldomain="$1"
  20. _debug fulldomain="$fulldomain"
  21. txtvalue="$2"
  22. _debug txtvalue="$txtvalue"
  23. _transip_setup "$fulldomain" || return 1
  24. _info "Creating TXT record."
  25. if ! _transip_rest POST "domains/$_domain/dns" "{\"dnsEntry\":{\"name\":\"$_sub_domain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"expire\":300}}"; then
  26. _err "Could not add TXT record."
  27. return 1
  28. fi
  29. return 0
  30. }
  31. dns_transip_rm() {
  32. fulldomain=$1
  33. _debug fulldomain="$fulldomain"
  34. txtvalue=$2
  35. _debug txtvalue="$txtvalue"
  36. _transip_setup "$fulldomain" || return 1
  37. _info "Removing TXT record."
  38. if ! _transip_rest DELETE "domains/$_domain/dns" "{\"dnsEntry\":{\"name\":\"$_sub_domain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"expire\":300}}"; then
  39. _err "Could not remove TXT record $_sub_domain for $domain"
  40. return 1
  41. fi
  42. return 0
  43. }
  44. #################### Private functions below ##################################
  45. #_acme-challenge.www.domain.com
  46. #returns
  47. # _sub_domain=_acme-challenge.www
  48. # _domain=domain.com
  49. _get_root() {
  50. domain="$1"
  51. i=2
  52. p=1
  53. while true; do
  54. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  55. if [ -z "$h" ]; then
  56. #not valid
  57. return 1
  58. fi
  59. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  60. _domain="$h"
  61. if _transip_rest GET "domains/$h/dns" && _contains "$response" "dnsEntries"; then
  62. return 0
  63. fi
  64. p=$i
  65. i=$(_math "$i" + 1)
  66. done
  67. _err "Unable to parse this domain"
  68. return 1
  69. }
  70. _transip_rest() {
  71. m="$1"
  72. ep="$2"
  73. data="$3"
  74. _debug ep "$ep"
  75. export _H1="Accept: application/json"
  76. export _H2="Authorization: Bearer $_token"
  77. export _H4="Content-Type: application/json"
  78. if [ "$m" != "GET" ]; then
  79. _debug data "$data"
  80. response="$(_post "$data" "$TRANSIP_Api_Url/$ep" "" "$m")"
  81. retcode=$?
  82. else
  83. response="$(_get "$TRANSIP_Api_Url/$ep")"
  84. retcode=$?
  85. fi
  86. if [ "$retcode" != "0" ]; then
  87. _err "error $ep"
  88. return 1
  89. fi
  90. _debug2 response "$response"
  91. return 0
  92. }
  93. _transip_get_token() {
  94. nonce=$(echo "TRANSIP$(_time)" | _digest sha1 hex | cut -c 1-32)
  95. _debug nonce "$nonce"
  96. # make IP whitelisting configurable
  97. TRANSIP_Token_Global_Key="${TRANSIP_Token_Global_Key:-$(_readaccountconf_mutable TRANSIP_Token_Global_Key)}"
  98. _saveaccountconf_mutable TRANSIP_Token_Global_Key "$TRANSIP_Token_Global_Key"
  99. data="{\"login\":\"${TRANSIP_Username}\",\"nonce\":\"${nonce}\",\"read_only\":\"${TRANSIP_Token_Read_Only}\",\"expiration_time\":\"${TRANSIP_Token_Expiration}\",\"label\":\"${TRANSIP_Token_Label}\",\"global_key\":\"${TRANSIP_Token_Global_Key:-false}\"}"
  100. _debug data "$data"
  101. #_signature=$(printf "%s" "$data" | openssl dgst -sha512 -sign "$TRANSIP_Key_File" | _base64)
  102. _signature=$(printf "%s" "$data" | _sign "$TRANSIP_Key_File" "sha512")
  103. _debug2 _signature "$_signature"
  104. export _H1="Signature: $_signature"
  105. export _H2="Content-Type: application/json"
  106. response="$(_post "$data" "$TRANSIP_Api_Url/auth" "" "POST")"
  107. retcode=$?
  108. _debug2 response "$response"
  109. if [ "$retcode" != "0" ]; then
  110. _err "Authentication failed."
  111. return 1
  112. fi
  113. if _contains "$response" "token"; then
  114. _token="$(echo "$response" | _normalizeJson | sed -n 's/^{"token":"\(.*\)"}/\1/p')"
  115. _debug _token "$_token"
  116. return 0
  117. fi
  118. return 1
  119. }
  120. _transip_setup() {
  121. fulldomain=$1
  122. # retrieve the transip creds
  123. TRANSIP_Username="${TRANSIP_Username:-$(_readaccountconf_mutable TRANSIP_Username)}"
  124. TRANSIP_Key_File="${TRANSIP_Key_File:-$(_readaccountconf_mutable TRANSIP_Key_File)}"
  125. # check their vals for null
  126. if [ -z "$TRANSIP_Username" ] || [ -z "$TRANSIP_Key_File" ]; then
  127. TRANSIP_Username=""
  128. TRANSIP_Key_File=""
  129. _err "You didn't specify a TransIP username and api key file location"
  130. _err "Please set those values and try again."
  131. return 1
  132. fi
  133. # save the username and api key to the account conf file.
  134. _saveaccountconf_mutable TRANSIP_Username "$TRANSIP_Username"
  135. _saveaccountconf_mutable TRANSIP_Key_File "$TRANSIP_Key_File"
  136. # download key file if it's an URL
  137. if _startswith "$TRANSIP_Key_File" "http"; then
  138. _debug "download transip key file"
  139. TRANSIP_Key_URL=$TRANSIP_Key_File
  140. TRANSIP_Key_File="$(_mktemp)"
  141. chmod 600 "$TRANSIP_Key_File"
  142. if ! _get "$TRANSIP_Key_URL" >"$TRANSIP_Key_File"; then
  143. _err "Error getting key file from : $TRANSIP_Key_URL"
  144. return 1
  145. fi
  146. fi
  147. if [ -f "$TRANSIP_Key_File" ]; then
  148. if ! grep "BEGIN PRIVATE KEY" "$TRANSIP_Key_File" >/dev/null 2>&1; then
  149. _err "Key file doesn't seem to be a valid key: ${TRANSIP_Key_File}"
  150. return 1
  151. fi
  152. else
  153. _err "Can't read private key file: ${TRANSIP_Key_File}"
  154. return 1
  155. fi
  156. if [ -z "$_token" ]; then
  157. if ! _transip_get_token; then
  158. _err "Can not get token."
  159. return 1
  160. fi
  161. fi
  162. if [ -n "${TRANSIP_Key_URL}" ]; then
  163. _debug "delete transip key file"
  164. rm "${TRANSIP_Key_File}"
  165. TRANSIP_Key_File=$TRANSIP_Key_URL
  166. fi
  167. _get_root "$fulldomain" || return 1
  168. return 0
  169. }