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.

163 lines
4.6 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)
  87. nonce=${nonce:0:32}
  88. _debug nonce "$nonce"
  89. 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}\"}"
  90. _debug data "$data"
  91. #_signature=$(printf "%s" "$data" | openssl dgst -sha512 -sign "$TRANSIP_Key_File" | _base64)
  92. _signature=$(printf "%s" "$data" | _sign "$TRANSIP_Key_File" "sha512")
  93. _debug2 _signature "$_signature"
  94. export _H1="Signature: $_signature"
  95. export _H2="Content-Type: application/json"
  96. response="$(_post "$data" "$TRANSIP_Api_Url/auth" "" "POST")"
  97. retcode=$?
  98. _debug2 response "$response"
  99. if [ "$retcode" != "0" ]; then
  100. _err "Authentication failed."
  101. return 1
  102. fi
  103. if _contains "$response" "token"; then
  104. _token="$(echo "$response" | _normalizeJson | sed -n 's/^{"token":"\(.*\)"}/\1/p')"
  105. _debug _token "$_token"
  106. return 0
  107. fi
  108. return 1
  109. }
  110. _transip_setup() {
  111. fulldomain=$1
  112. # retrieve the transip creds
  113. TRANSIP_Username="${TRANSIP_Username:-$(_readaccountconf_mutable TRANSIP_Username)}"
  114. TRANSIP_Key_File="${TRANSIP_Key_File:-$(_readaccountconf_mutable TRANSIP_Key_File)}"
  115. # check their vals for null
  116. if [ -z "$TRANSIP_Username" ] || [ -z "$TRANSIP_Key_File" ]; then
  117. TRANSIP_Username=""
  118. TRANSIP_Key_File=""
  119. _err "You didn't specify a TransIP username and api key file location"
  120. _err "Please set those values and try again."
  121. return 1
  122. fi
  123. # save the username and api key to the account conf file.
  124. _saveaccountconf_mutable TRANSIP_Username "$TRANSIP_Username"
  125. _saveaccountconf_mutable TRANSIP_Key_File "$TRANSIP_Key_File"
  126. if [ -f "$TRANSIP_Key_File" ]; then
  127. if ! grep "BEGIN PRIVATE KEY" "$TRANSIP_Key_File" >/dev/null 2>&1; then
  128. _err "Key file doesn't seem to be a valid key: ${TRANSIP_Key_File}"
  129. return 1
  130. fi
  131. else
  132. _err "Can't read private key file: ${TRANSIP_Key_File}"
  133. return 1
  134. fi
  135. if [ -z "$_token" ]; then
  136. if ! _transip_get_token; then
  137. _err "Can not get token."
  138. return 1
  139. fi
  140. fi
  141. _get_root "$fulldomain" || return 1
  142. return 0
  143. }