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.

224 lines
10 KiB

5 years ago
  1. #!/bin/bash
  2. ################################################################################
  3. # ACME.sh 3rd party deploy plugin for Synology DSM
  4. ################################################################################
  5. # Authors: Brian Hartvigsen (creator), https://github.com/tresni
  6. # Martin Arndt (contributor), https://troublezone.net/
  7. # Updated: 2023-07-03
  8. # Issues: https://github.com/acmesh-official/acme.sh/issues/2727
  9. ################################################################################
  10. # Usage:
  11. # 1. export SYNO_Username="adminUser"
  12. # 2. export SYNO_Password="adminPassword"
  13. # Optional exports (shown values are the defaults):
  14. # - export SYNO_Certificate="" to replace a specific certificate via description
  15. # - export SYNO_Scheme="http"
  16. # - export SYNO_Hostname="localhost"
  17. # - export SYNO_Port="5000"
  18. # - export SYNO_Device_Name="CertRenewal" - required for skipping 2FA-OTP
  19. # - export SYNO_Device_ID="" - required for skipping 2FA-OTP
  20. # 3. acme.sh --deploy --deploy-hook synology_dsm -d example.com
  21. ################################################################################
  22. # Dependencies:
  23. # - jq & curl
  24. ################################################################################
  25. # Return value:
  26. # 0 means success, otherwise error.
  27. ################################################################################
  28. ########## Public functions ####################################################
  29. #domain keyfile certfile cafile fullchain
  30. synology_dsm_deploy() {
  31. _cdomain="$1"
  32. _ckey="$2"
  33. _ccert="$3"
  34. _cca="$4"
  35. _debug _cdomain "$_cdomain"
  36. # Get username & password, but don't save until we authenticated successfully
  37. _getdeployconf SYNO_Username
  38. _getdeployconf SYNO_Password
  39. _getdeployconf SYNO_Create
  40. _getdeployconf SYNO_DID
  41. _getdeployconf SYNO_TOTP_SECRET
  42. _getdeployconf SYNO_Device_Name
  43. _getdeployconf SYNO_Device_ID
  44. if [ -z "${SYNO_Username:-}" ] || [ -z "${SYNO_Password:-}" ]; then
  45. _err "SYNO_Username & SYNO_Password must be set"
  46. return 1
  47. fi
  48. if [ -n "${SYNO_Device_Name:-}" ] && [ -z "${SYNO_Device_ID:-}" ]; then
  49. _err "SYNO_Device_Name set, but SYNO_Device_ID is empty"
  50. return 1
  51. fi
  52. _debug2 SYNO_Username "$SYNO_Username"
  53. _secure_debug2 SYNO_Password "$SYNO_Password"
  54. _debug2 SYNO_Create "$SYNO_Create"
  55. _debug2 SYNO_Device_Name "$SYNO_Device_Name"
  56. _secure_debug2 SYNO_Device_ID "$SYNO_Device_ID"
  57. # Optional scheme, hostname & port for Synology DSM
  58. _getdeployconf SYNO_Scheme
  59. _getdeployconf SYNO_Hostname
  60. _getdeployconf SYNO_Port
  61. # Default values for scheme, hostname & port
  62. # Defaulting to localhost & http, because it's localhost…
  63. [ -n "${SYNO_Scheme}" ] || SYNO_Scheme="http"
  64. [ -n "${SYNO_Hostname}" ] || SYNO_Hostname="localhost"
  65. [ -n "${SYNO_Port}" ] || SYNO_Port="5000"
  66. _savedeployconf SYNO_Scheme "$SYNO_Scheme"
  67. _savedeployconf SYNO_Hostname "$SYNO_Hostname"
  68. _savedeployconf SYNO_Port "$SYNO_Port"
  69. _debug2 SYNO_Scheme "$SYNO_Scheme"
  70. _debug2 SYNO_Hostname "$SYNO_Hostname"
  71. _debug2 SYNO_Port "$SYNO_Port"
  72. # Get the certificate description, but don't save it until we verify it's real
  73. _getdeployconf SYNO_Certificate
  74. _debug SYNO_Certificate "${SYNO_Certificate:-}"
  75. # shellcheck disable=SC1003 # We are not trying to escape a single quote
  76. if printf "%s" "$SYNO_Certificate" | grep '\\'; then
  77. _err "Do not use a backslash (\) in your certificate description"
  78. return 1
  79. fi
  80. _base_url="$SYNO_Scheme://$SYNO_Hostname:$SYNO_Port"
  81. _debug _base_url "$_base_url"
  82. _debug "Getting API version"
  83. response=$(_get "$_base_url/webapi/query.cgi?api=SYNO.API.Info&version=1&method=query&query=SYNO.API.Auth")
  84. api_version=$(echo "$response" | grep "SYNO.API.Auth" | sed -n 's/.*"maxVersion" *: *\([0-9]*\).*/\1/p')
  85. _debug3 response "$response"
  86. _debug3 api_version "$api_version"
  87. # Login, get the session ID & SynoToken from JSON
  88. _info "Logging into $SYNO_Hostname:$SYNO_Port"
  89. encoded_username="$(printf "%s" "$SYNO_Username" | _url_encode)"
  90. encoded_password="$(printf "%s" "$SYNO_Password" | _url_encode)"
  91. otp_code=""
  92. # START - DEPRECATED, only kept for legacy compatibility reasons
  93. if [ -n "$SYNO_TOTP_SECRET" ]; then
  94. _info "WARNING: Usage of SYNO_TOTP_SECRET is deprecated!"
  95. _info " See synology_dsm.sh script or ACME.sh Wiki page for details:"
  96. _info " https://github.com/acmesh-official/acme.sh/wiki/Synology-NAS-Guide"
  97. DEPRECATED_otp_code=""
  98. if _exists oathtool; then
  99. DEPRECATED_otp_code="$(oathtool --base32 --totp "${SYNO_TOTP_SECRET}" 2>/dev/null)"
  100. else
  101. _err "oathtool could not be found, install oathtool to use SYNO_TOTP_SECRET"
  102. return 1
  103. fi
  104. if [ -n "$SYNO_DID" ]; then
  105. _H1="Cookie: did=$SYNO_DID"
  106. export _H1
  107. _debug3 H1 "${_H1}"
  108. fi
  109. response=$(_post "method=login&account=$encoded_username&passwd=$encoded_password&api=SYNO.API.Auth&version=$api_version&enable_syno_token=yes&otp_code=$DEPRECATED_otp_code&device_name=certrenewal&device_id=$SYNO_DID" "$_base_url/webapi/auth.cgi?enable_syno_token=yes")
  110. _debug3 response "$response"
  111. # END - DEPRECATED, only kept for legacy compatibility reasons
  112. # Get device ID if still empty first, otherwise log in right away
  113. elif [ -z "${SYNO_Device_ID:-}" ]; then
  114. printf "Enter OTP code for user '%s': " "$SYNO_Username"
  115. read -r otp_code
  116. if [ -z "${SYNO_Device_Name:-}" ]; then
  117. printf "Enter device name or leave empty for default (CertRenewal): "
  118. read -r SYNO_Device_Name
  119. [ -n "${SYNO_Device_Name}" ] || SYNO_Device_Name="CertRenewal"
  120. fi
  121. response=$(_get "$_base_url/webapi/entry.cgi?api=SYNO.API.Auth&version=$api_version&method=login&format=sid&account=$encoded_username&passwd=$encoded_password&otp_code=$otp_code&enable_syno_token=yes&enable_device_token=yes&device_name=$SYNO_Device_Name")
  122. _debug3 response "$response"
  123. SYNO_Device_ID=$(echo "$response" | grep "device_id" | sed -n 's/.*"device_id" *: *"\([^"]*\).*/\1/p')
  124. _secure_debug2 SYNO_Device_ID "$SYNO_Device_ID"
  125. else
  126. response=$(_get "$_base_url/webapi/entry.cgi?api=SYNO.API.Auth&version=$api_version&method=login&format=sid&account=$encoded_username&passwd=$encoded_password&enable_syno_token=yes&device_name=$SYNO_Device_Name&device_id=$SYNO_Device_ID")
  127. _debug3 response "$response"
  128. fi
  129. sid=$(echo "$response" | grep "sid" | sed -n 's/.*"sid" *: *"\([^"]*\).*/\1/p')
  130. token=$(echo "$response" | grep "synotoken" | sed -n 's/.*"synotoken" *: *"\([^"]*\).*/\1/p')
  131. _debug "Session ID" "$sid"
  132. _debug SynoToken "$token"
  133. if [ -z "$SYNO_DID" ] && [ -z "$SYNO_Device_ID" ] || [ -z "$sid" ] || [ -z "$token" ]; then
  134. _err "Unable to authenticate to $_base_url - check your username & password."
  135. _err "If two-factor authentication is enabled for the user, set SYNO_Device_ID."
  136. return 1
  137. fi
  138. _H1="X-SYNO-TOKEN: $token"
  139. export _H1
  140. _debug2 H1 "${_H1}"
  141. # Now that we know the username & password are good, save them
  142. _savedeployconf SYNO_Username "$SYNO_Username"
  143. _savedeployconf SYNO_Password "$SYNO_Password"
  144. _savedeployconf SYNO_Device_Name "$SYNO_Device_Name"
  145. _savedeployconf SYNO_Device_ID "$SYNO_Device_ID"
  146. _info "Getting certificates in Synology DSM"
  147. response=$(_post "api=SYNO.Core.Certificate.CRT&method=list&version=1&_sid=$sid" "$_base_url/webapi/entry.cgi")
  148. _debug3 response "$response"
  149. escaped_certificate="$(printf "%s" "$SYNO_Certificate" | sed 's/\([].*^$[]\)/\\\1/g;s/"/\\\\"/g')"
  150. _debug escaped_certificate "$escaped_certificate"
  151. id=$(echo "$response" | sed -n "s/.*\"desc\":\"$escaped_certificate\",\"id\":\"\([^\"]*\).*/\1/p")
  152. _debug2 id "$id"
  153. if [ -z "$id" ] && [ -z "${SYNO_Create:-}" ]; then
  154. _err "Unable to find certificate: $SYNO_Certificate & \$SYNO_Create is not set"
  155. return 1
  156. fi
  157. # We've verified this certificate description is a thing, so save it
  158. _savedeployconf SYNO_Certificate "$SYNO_Certificate" "base64"
  159. _info "Generate form POST request"
  160. nl="\0015\0012"
  161. delim="--------------------------$(_utc_date | tr -d -- '-: ')"
  162. content="--$delim${nl}Content-Disposition: form-data; name=\"key\"; filename=\"$(basename "$_ckey")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_ckey")\0012"
  163. 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"
  164. 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"
  165. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"id\"${nl}${nl}$id"
  166. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"desc\"${nl}${nl}${SYNO_Certificate}"
  167. if echo "$response" | sed -n "s/.*\"desc\":\"$escaped_certificate\",\([^{]*\).*/\1/p" | grep -- 'is_default":true' >/dev/null; then
  168. _debug2 default "This is the default certificate"
  169. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"as_default\"${nl}${nl}true"
  170. else
  171. _debug2 default "This is NOT the default certificate"
  172. fi
  173. content="$content${nl}--$delim--${nl}"
  174. content="$(printf "%b_" "$content")"
  175. content="${content%_}" # protect trailing \n
  176. _info "Upload certificate to the Synology DSM"
  177. 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}")
  178. _debug3 response "$response"
  179. if ! echo "$response" | grep '"error":' >/dev/null; then
  180. if echo "$response" | grep '"restart_httpd":true' >/dev/null; then
  181. _info "Restarting HTTP services succeeded"
  182. else
  183. _info "Restarting HTTP services failed"
  184. fi
  185. _logout
  186. return 0
  187. else
  188. _err "Unable to update certificate, error code $response"
  189. _logout
  190. return 1
  191. fi
  192. }
  193. #################### Private functions below ##################################
  194. _logout() {
  195. # Logout to not occupy a permanent session, e.g. in DSM's "Connected Users" widget
  196. response=$(_get "$_base_url/webapi/entry.cgi?api=SYNO.API.Auth&version=$api_version&method=logout")
  197. _debug3 response "$response"
  198. }