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.

178 lines
7.0 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 following environment variables:
  5. #
  6. # SYNO_Username - Synology Username to login (must be an administrator)
  7. # SYNO_Password - Synology Password to login
  8. # SYNO_Certificate - Certificate description to target for replacement
  9. #
  10. # The following environmental variables may be set if you don't like their
  11. # default values:
  12. #
  13. # SYNO_Scheme - defaults to http
  14. # SYNO_Hostname - defaults to localhost
  15. # SYNO_Port - defaults to 5000
  16. # SYNO_DID - device ID to skip OTP - defaults to empty
  17. # SYNO_TOTP_SECRET - TOTP secret to generate OTP - defaults to empty
  18. #
  19. # Dependencies:
  20. # -------------
  21. # - jq and curl
  22. # - oathtool (When using 2 Factor Authentication and SYNO_TOTP_SECRET is set)
  23. #
  24. #returns 0 means success, otherwise error.
  25. ######## Public functions #####################
  26. #domain keyfile certfile cafile fullchain
  27. synology_dsm_deploy() {
  28. _cdomain="$1"
  29. _ckey="$2"
  30. _ccert="$3"
  31. _cca="$4"
  32. _debug _cdomain "$_cdomain"
  33. # Get Username and Password, but don't save until we successfully authenticate
  34. _getdeployconf SYNO_Username
  35. _getdeployconf SYNO_Password
  36. _getdeployconf SYNO_Create
  37. _getdeployconf SYNO_DID
  38. _getdeployconf SYNO_TOTP_SECRET
  39. if [ -z "${SYNO_Username:-}" ] || [ -z "${SYNO_Password:-}" ]; then
  40. _err "SYNO_Username & SYNO_Password must be set"
  41. return 1
  42. fi
  43. _debug2 SYNO_Username "$SYNO_Username"
  44. _secure_debug2 SYNO_Password "$SYNO_Password"
  45. # Optional scheme, hostname, and port for Synology DSM
  46. _getdeployconf SYNO_Scheme
  47. _getdeployconf SYNO_Hostname
  48. _getdeployconf SYNO_Port
  49. # default vaules for scheme, hostname, and port
  50. # defaulting to localhost and http because it's localhost...
  51. [ -n "${SYNO_Scheme}" ] || SYNO_Scheme="http"
  52. [ -n "${SYNO_Hostname}" ] || SYNO_Hostname="localhost"
  53. [ -n "${SYNO_Port}" ] || SYNO_Port="5000"
  54. _savedeployconf SYNO_Scheme "$SYNO_Scheme"
  55. _savedeployconf SYNO_Hostname "$SYNO_Hostname"
  56. _savedeployconf SYNO_Port "$SYNO_Port"
  57. _debug2 SYNO_Scheme "$SYNO_Scheme"
  58. _debug2 SYNO_Hostname "$SYNO_Hostname"
  59. _debug2 SYNO_Port "$SYNO_Port"
  60. # Get the certificate description, but don't save it until we verfiy it's real
  61. _getdeployconf SYNO_Certificate
  62. _debug SYNO_Certificate "${SYNO_Certificate:-}"
  63. # shellcheck disable=SC1003 # We are not trying to escape a single quote
  64. if printf "%s" "$SYNO_Certificate" | grep '\\'; then
  65. _err "Do not use a backslash (\) in your certificate description"
  66. return 1
  67. fi
  68. _base_url="$SYNO_Scheme://$SYNO_Hostname:$SYNO_Port"
  69. _debug _base_url "$_base_url"
  70. _debug "Getting API version"
  71. response=$(_get "$_base_url/webapi/query.cgi?api=SYNO.API.Info&version=1&method=query&query=SYNO.API.Auth")
  72. api_version=$(echo "$response" | grep "SYNO.API.Auth" | sed -n 's/.*"maxVersion" *: *\([0-9]*\).*/\1/p')
  73. _debug3 response "$response"
  74. _debug3 api_version "$api_version"
  75. # Login, get the token from JSON and session id from cookie
  76. _info "Logging into $SYNO_Hostname:$SYNO_Port"
  77. encoded_username="$(printf "%s" "$SYNO_Username" | _url_encode)"
  78. encoded_password="$(printf "%s" "$SYNO_Password" | _url_encode)"
  79. otp_code=""
  80. if [ -n "$SYNO_TOTP_SECRET" ]; then
  81. otp_code="$(oathtool --base32 --totp "${SYNO_TOTP_SECRET}" 2>/dev/null)"
  82. fi
  83. if [ -n "$SYNO_DID" ]; then
  84. _H1="Cookie: did=$SYNO_DID"
  85. export _H1
  86. _debug3 H1 "${_H1}"
  87. fi
  88. response=$(_post "method=login&account=$encoded_username&passwd=$encoded_password&api=SYNO.API.Auth&version=$api_version&enable_syno_token=yes&otp_code=$otp_code" "$_base_url/webapi/auth.cgi?enable_syno_token=yes")
  89. token=$(echo "$response" | grep "synotoken" | sed -n 's/.*"synotoken" *: *"\([^"]*\).*/\1/p')
  90. _debug3 response "$response"
  91. _debug token "$token"
  92. if [ -z "$token" ]; then
  93. _err "Unable to authenticate to $SYNO_Hostname:$SYNO_Port using $SYNO_Scheme."
  94. _err "Check your username and password."
  95. _err "If two-factor authentication is enabled for the user, set SYNO_TOTP_SECRET."
  96. return 1
  97. fi
  98. sid=$(echo "$response" | grep "sid" | sed -n 's/.*"sid" *: *"\([^"]*\).*/\1/p')
  99. _H1="X-SYNO-TOKEN: $token"
  100. export _H1
  101. _debug2 H1 "${_H1}"
  102. # Now that we know the username and password are good, save them
  103. _savedeployconf SYNO_Username "$SYNO_Username"
  104. _savedeployconf SYNO_Password "$SYNO_Password"
  105. _savedeployconf SYNO_DID "$SYNO_DID"
  106. _savedeployconf SYNO_TOTP_SECRET "$SYNO_TOTP_SECRET"
  107. _info "Getting certificates in Synology DSM"
  108. response=$(_post "api=SYNO.Core.Certificate.CRT&method=list&version=1&_sid=$sid" "$_base_url/webapi/entry.cgi")
  109. _debug3 response "$response"
  110. escaped_certificate="$(printf "%s" "$SYNO_Certificate" | sed 's/\([].*^$[]\)/\\\1/g;s/"/\\\\"/g')"
  111. _debug escaped_certificate "$escaped_certificate"
  112. id=$(echo "$response" | sed -n "s/.*\"desc\":\"$escaped_certificate\",\"id\":\"\([^\"]*\).*/\1/p")
  113. _debug2 id "$id"
  114. if [ -z "$id" ] && [ -z "${SYNO_Create:-}" ]; then
  115. _err "Unable to find certificate: $SYNO_Certificate and \$SYNO_Create is not set"
  116. return 1
  117. fi
  118. # we've verified this certificate description is a thing, so save it
  119. _savedeployconf SYNO_Certificate "$SYNO_Certificate" "base64"
  120. _info "Generate form POST request"
  121. nl="\0015\0012"
  122. delim="--------------------------$(_utc_date | tr -d -- '-: ')"
  123. content="--$delim${nl}Content-Disposition: form-data; name=\"key\"; filename=\"$(basename "$_ckey")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_ckey")\0012"
  124. 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"
  125. 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"
  126. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"id\"${nl}${nl}$id"
  127. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"desc\"${nl}${nl}${SYNO_Certificate}"
  128. if echo "$response" | sed -n "s/.*\"desc\":\"$escaped_certificate\",\([^{]*\).*/\1/p" | grep -- 'is_default":true' >/dev/null; then
  129. _debug2 default "this is the default certificate"
  130. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"as_default\"${nl}${nl}true"
  131. else
  132. _debug2 default "this is NOT the default certificate"
  133. fi
  134. content="$content${nl}--$delim--${nl}"
  135. content="$(printf "%b_" "$content")"
  136. content="${content%_}" # protect trailing \n
  137. _info "Upload certificate to the Synology DSM"
  138. 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}")
  139. _debug3 response "$response"
  140. if ! echo "$response" | grep '"error":' >/dev/null; then
  141. if echo "$response" | grep '"restart_httpd":true' >/dev/null; then
  142. _info "http services were restarted"
  143. else
  144. _info "http services were NOT restarted"
  145. fi
  146. return 0
  147. else
  148. _err "Unable to update certificate, error code $response"
  149. return 1
  150. fi
  151. }