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.

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