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.

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