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.

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