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.

152 lines
5.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env sh
  2. # Here is a script to deploy cert to Synology DSM
  3. #
  4. # it requires the jq and curl are in the $PATH and the following
  5. # environment variables must be set:
  6. #
  7. # SYNO_Username - Synology Username to login (must be an administrator)
  8. # SYNO_Password - Synology Password to login
  9. # SYNO_Certificate - Certificate description to target for replacement
  10. #
  11. # The following environmental variables may be set if you don't like their
  12. # default values:
  13. #
  14. # SYNO_Scheme - defaults to http
  15. # SYNO_Hostname - defaults to localhost
  16. # SYNO_Port - defaults to 5000
  17. # SYNO_DID - device ID to skip OTP - defaults to empty
  18. #
  19. #returns 0 means success, otherwise error.
  20. ######## Public functions #####################
  21. _syno_get_cookie_data() {
  22. grep "\W$1=" | grep "^Set-Cookie:" | _tail_n 1 | _egrep_o "$1=[^;]*;" | tr -d ';'
  23. }
  24. #domain keyfile certfile cafile fullchain
  25. synology_dsm_deploy() {
  26. _cdomain="$1"
  27. _ckey="$2"
  28. _ccert="$3"
  29. _cca="$4"
  30. _debug _cdomain "$_cdomain"
  31. # Get Username and Password, but don't save until we successfully authenticate
  32. _getdeployconf SYNO_Username
  33. _getdeployconf SYNO_Password
  34. _getdeployconf SYNO_Create
  35. _getdeployconf SYNO_DID
  36. if [ -z "${SYNO_Username:-}" ] || [ -z "${SYNO_Password:-}" ]; then
  37. _err "SYNO_Username & SYNO_Password must be set"
  38. return 1
  39. fi
  40. _debug2 SYNO_Username "$SYNO_Username"
  41. _secure_debug2 SYNO_Password "$SYNO_Password"
  42. # Optional scheme, hostname, and port for Synology DSM
  43. _getdeployconf SYNO_Scheme
  44. _getdeployconf SYNO_Hostname
  45. _getdeployconf SYNO_Port
  46. # default vaules for scheme, hostname, and port
  47. # defaulting to localhost and http because it's localhost...
  48. [ -n "${SYNO_Scheme}" ] || SYNO_Scheme="http"
  49. [ -n "${SYNO_Hostname}" ] || SYNO_Hostname="localhost"
  50. [ -n "${SYNO_Port}" ] || SYNO_Port="5000"
  51. _savedeployconf SYNO_Scheme "$SYNO_Scheme"
  52. _savedeployconf SYNO_Hostname "$SYNO_Hostname"
  53. _savedeployconf SYNO_Port "$SYNO_Port"
  54. _debug2 SYNO_Scheme "$SYNO_Scheme"
  55. _debug2 SYNO_Hostname "$SYNO_Hostname"
  56. _debug2 SYNO_Port "$SYNO_Port"
  57. # Get the certificate description, but don't save it until we verfiy it's real
  58. _getdeployconf SYNO_Certificate
  59. _debug SYNO_Certificate "${SYNO_Certificate:-}"
  60. _base_url="$SYNO_Scheme://$SYNO_Hostname:$SYNO_Port"
  61. _debug _base_url "$_base_url"
  62. # Login, get the token from JSON and session id from cookie
  63. _info "Logging into $SYNO_Hostname:$SYNO_Port"
  64. encoded_username="$(printf "%s" "$SYNO_Username" | _url_encode)"
  65. encoded_password="$(printf "%s" "$SYNO_Password" | _url_encode)"
  66. encoded_did="$(printf "%s" "$SYNO_DID" | _url_encode)"
  67. response=$(_get "$_base_url/webman/login.cgi?username=$encoded_username&passwd=$encoded_password&enable_syno_token=yes&device_id=$encoded_did" 1)
  68. token=$(echo "$response" | grep "X-SYNO-TOKEN:" | sed -n 's/^X-SYNO-TOKEN: \(.*\)$/\1/p' | tr -d "\r\n")
  69. _debug3 response "$response"
  70. _debug token "$token"
  71. if [ -z "$token" ]; then
  72. _err "Unable to authenticate to $SYNO_Hostname:$SYNO_Port using $SYNO_Scheme."
  73. _err "Check your username and password."
  74. return 1
  75. fi
  76. _H1="Cookie: $(echo "$response" | _syno_get_cookie_data "id"); $(echo "$response" | _syno_get_cookie_data "smid")"
  77. _H2="X-SYNO-TOKEN: $token"
  78. export _H1
  79. export _H2
  80. _debug2 H1 "${_H1}"
  81. _debug2 H2 "${_H2}"
  82. # Now that we know the username and password are good, save them
  83. _savedeployconf SYNO_Username "$SYNO_Username"
  84. _savedeployconf SYNO_Password "$SYNO_Password"
  85. _savedeployconf SYNO_DID "$SYNO_DID"
  86. _info "Getting certificates in Synology DSM"
  87. response=$(_post "api=SYNO.Core.Certificate.CRT&method=list&version=1" "$_base_url/webapi/entry.cgi")
  88. _debug3 response "$response"
  89. id=$(echo "$response" | sed -n "s/.*\"desc\":\"$SYNO_Certificate\",\"id\":\"\([^\"]*\).*/\1/p")
  90. _debug2 id "$id"
  91. if [ -z "$id" ] && [ -z "${SYNO_Create:-}" ]; then
  92. _err "Unable to find certificate: $SYNO_Certificate and \$SYNO_Create is not set"
  93. return 1
  94. fi
  95. # we've verified this certificate description is a thing, so save it
  96. _savedeployconf SYNO_Certificate "$SYNO_Certificate"
  97. default=false
  98. if echo "$response" | sed -n "s/.*\"desc\":\"$SYNO_Certificate\",\([^{]*\).*/\1/p" | grep -- 'is_default":true' >/dev/null; then
  99. default=true
  100. fi
  101. _debug2 default "$default"
  102. _info "Generate form POST request"
  103. nl="\0015\0012"
  104. delim="--------------------------$(_utc_date | tr -d -- '-: ')"
  105. content="--$delim${nl}Content-Disposition: form-data; name=\"key\"; filename=\"$(basename "$_ckey")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_ckey")\0012"
  106. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"cert\"; filename=\"$(basename "$_ccert")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_ccert")\0012"
  107. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"inter_cert\"; filename=\"$(basename "$_cca")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_cca")\0012"
  108. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"id\"${nl}${nl}$id"
  109. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"desc\"${nl}${nl}${SYNO_Certificate}"
  110. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"as_default\"${nl}${nl}${default}"
  111. content="$content${nl}--$delim--${nl}"
  112. content="$(printf "%b_" "$content")"
  113. content="${content%_}" # protect trailing \n
  114. _info "Upload certificate to the Synology DSM"
  115. response=$(_post "$content" "$_base_url/webapi/entry.cgi?api=SYNO.Core.Certificate&method=import&version=1&SynoToken=$token" "" "POST" "multipart/form-data; boundary=${delim}")
  116. _debug3 response "$response"
  117. if ! echo "$response" | grep '"error":' >/dev/null; then
  118. if echo "$response" | grep '"restart_httpd":true' >/dev/null; then
  119. _info "http services were restarted"
  120. else
  121. _info "http services were NOT restarted"
  122. fi
  123. return 0
  124. else
  125. _err "Unable to update certificate, error code $response"
  126. return 1
  127. fi
  128. }