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.

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