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.5 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_Key_File="transip2.key"
  4. TRANSIP_Token_Read_Only="false"
  5. TRANSIP_Token_Global_Key="false"
  6. TRANSIP_Token_Expiration="30 minutes"
  7. # You can't reuse a label token, so we leave this empty normally
  8. TRANSIP_Token_Label=""
  9. ######## Public functions #####################
  10. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  11. dns_transip_add() {
  12. fulldomain="$1"
  13. _debug fulldomain="$fulldomain"
  14. txtvalue="$2"
  15. _debug txtvalue="$txtvalue"
  16. _transip_setup "$fulldomain" || return 1
  17. _info "Creating TXT record."
  18. if ! _transip_rest POST "domains/$_domain/dns" "{\"dnsEntry\":{\"name\":\"$_sub_domain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"expire\":300}}"; then
  19. _err "Could not add TXT record."
  20. return 1
  21. fi
  22. return 0
  23. }
  24. dns_transip_rm() {
  25. fulldomain=$1
  26. _debug fulldomain="$fulldomain"
  27. txtvalue=$2
  28. _debug txtvalue="$txtvalue"
  29. _transip_setup "$fulldomain" || return 1
  30. _info "Removing TXT record."
  31. if ! _transip_rest DELETE "domains/$_domain/dns" "{\"dnsEntry\":{\"name\":\"$_sub_domain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"expire\":300}}"; then
  32. _err "Could not remove TXT record $_sub_domain for $domain"
  33. return 1
  34. fi
  35. return 0
  36. }
  37. #################### Private functions below ##################################
  38. #_acme-challenge.www.domain.com
  39. #returns
  40. # _sub_domain=_acme-challenge.www
  41. # _domain=domain.com
  42. _get_root() {
  43. domain="$1"
  44. i=2
  45. p=1
  46. while true; do
  47. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  48. if [ -z "$h" ]; then
  49. #not valid
  50. return 1
  51. fi
  52. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  53. _domain="$h"
  54. if _transip_rest GET "domains/$h/dns" && _contains "$response" "dnsEntries"; then
  55. return 0
  56. fi
  57. p=$i
  58. i=$(_math "$i" + 1)
  59. done
  60. _err "Unable to parse this domain"
  61. return 1
  62. }
  63. _transip_rest() {
  64. m="$1"
  65. ep="$2"
  66. data="$3"
  67. _debug ep "$ep"
  68. export _H1="Accept: application/json"
  69. export _H2="Authorization: Bearer $_token"
  70. export _H4="Content-Type: application/json"
  71. if [ "$m" != "GET" ]; then
  72. _debug data "$data"
  73. response="$(_post "$data" "$TRANSIP_Api_Url/$ep" "" "$m")"
  74. retcode=$?
  75. else
  76. response="$(_get "$TRANSIP_Api_Url/$ep")"
  77. retcode=$?
  78. fi
  79. if [ "$retcode" != "0" ]; then
  80. _err "error $ep"
  81. return 1
  82. fi
  83. _debug2 response "$response"
  84. return 0
  85. }
  86. _transip_get_token() {
  87. nonce=$(openssl rand -hex 12)
  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. }