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.

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