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
6.3 KiB

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