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.

153 lines
5.9 KiB

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