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.

297 lines
13 KiB

2 years ago
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. Set required environment variables (two ways):
  12. # - Create temp admin user automatically
  13. # `export SYNO_UseTempAdmin=1`
  14. # - Or provide your own admin user credential by:
  15. # 1. `export SYNO_Username="adminUser"`
  16. # 2. `export SYNO_Password="adminPassword"`
  17. # 2. Set optional environment variables (shown values are the defaults)
  18. # - `export SYNO_Scheme="http"`
  19. # - `export SYNO_Hostname="localhost"`
  20. # - `export SYNO_Port="5000"`
  21. # - `export SYNO_Create=1` - to allow creating the cert if it doesn't exist
  22. # - `export SYNO_Certificate=""` - to replace a specific cert by its description
  23. # - `export SYNO_DeviceName=""` - required for 2FA-OTP
  24. # - `export SYNO_DeviceID=""` - required for omitting 2FA-OTP (only pro
  25. # users may set)
  26. # 3. Run command:
  27. # acme.sh --deploy --deploy-hook synology_dsm -d example.com
  28. ################################################################################
  29. # Dependencies:
  30. # - jq & curl
  31. # - synouser & synogroup (When available and SYNO_UseTempAdmin is set)
  32. ################################################################################
  33. # Return value:
  34. # 0 means success, otherwise error.
  35. ################################################################################
  36. ########## Public functions ####################################################
  37. #domain keyfile certfile cafile fullchain
  38. synology_dsm_deploy() {
  39. _cdomain="$1"
  40. _ckey="$2"
  41. _ccert="$3"
  42. _cca="$4"
  43. _debug _cdomain "$_cdomain"
  44. # Get username and password, but don't save until we authenticated successfully
  45. _getdeployconf SYNO_Username
  46. _getdeployconf SYNO_Password
  47. _getdeployconf SYNO_DeviceName
  48. _getdeployconf SYNO_DeviceID
  49. _getdeployconf SYNO_Create
  50. # ## START ## - DEPRECATED, for backward compatibility
  51. _getdeployconf SYNO_Device_ID
  52. _getdeployconf SYNO_Device_Name
  53. [ -n "$SYNO_DeviceID" ] || SYNO_DeviceID="$SYNO_Device_ID"
  54. [ -n "$SYNO_DeviceName" ] || SYNO_DeviceName="$SYNO_Device_Name"
  55. # ## END ## - DEPRECATED, for backward compatibility
  56. [ -n "$SYNO_Create" ] || SYNO_Create=1
  57. # Prepare to use temp admin if SYNO_UseTempAdmin is set
  58. _getdeployconf SYNO_UseTempAdmin
  59. _debug2 SYNO_UseTempAdmin "$SYNO_UseTempAdmin"
  60. if [ -n "$SYNO_UseTempAdmin" ]; then
  61. if ! _exists synouser || ! _exists synogroup; then
  62. _err "Tools are missing for creating temp admin user, please set SYNO_Username and SYNO_Password instead."
  63. return 1
  64. fi
  65. # Clean up
  66. [ -n "$SYNO_Username" ] || _savedeployconf SYNO_Username ""
  67. [ -n "$SYNO_Password" ] || _savedeployconf SYNO_Password ""
  68. synouser --del "$SYNO_Username" >/dev/null 2>/dev/null
  69. _debug "Setting temp admin user credential..."
  70. SYNO_Username=sc-acmesh-tmp
  71. SYNO_Password=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 16)
  72. # Ignore 2FA-OTP settings which won't be needed.
  73. SYNO_DeviceID=
  74. SYNO_DeviceName=
  75. _savedeployconf SYNO_UseTempAdmin "$SYNO_UseTempAdmin"
  76. else
  77. _debug2 SYNO_Username "$SYNO_Username"
  78. _secure_debug2 SYNO_Password "$SYNO_Password"
  79. _debug2 SYNO_Create "$SYNO_Create"
  80. _debug2 SYNO_DeviceName "$SYNO_DeviceName"
  81. _secure_debug2 SYNO_DeviceID "$SYNO_DeviceID"
  82. fi
  83. if [ -z "$SYNO_Username" ] || [ -z "$SYNO_Password" ]; then
  84. _err "You must set either SYNO_UseTempAdmin, or set both SYNO_Username and SYNO_Password."
  85. return 1
  86. fi
  87. # Optional scheme, hostname and port for Synology DSM
  88. _getdeployconf SYNO_Scheme
  89. _getdeployconf SYNO_Hostname
  90. _getdeployconf SYNO_Port
  91. # Default values for scheme, hostname and port
  92. # Defaulting to localhost and http, because it's localhost…
  93. [ -n "$SYNO_Scheme" ] || SYNO_Scheme="http"
  94. [ -n "$SYNO_Hostname" ] || SYNO_Hostname="localhost"
  95. [ -n "$SYNO_Port" ] || SYNO_Port="5000"
  96. _savedeployconf SYNO_Scheme "$SYNO_Scheme"
  97. _savedeployconf SYNO_Hostname "$SYNO_Hostname"
  98. _savedeployconf SYNO_Port "$SYNO_Port"
  99. _debug2 SYNO_Scheme "$SYNO_Scheme"
  100. _debug2 SYNO_Hostname "$SYNO_Hostname"
  101. _debug2 SYNO_Port "$SYNO_Port"
  102. # Get the certificate description, but don't save it until we verify it's real
  103. _getdeployconf SYNO_Certificate
  104. _debug SYNO_Certificate "$SYNO_Certificate"
  105. # shellcheck disable=SC1003 # We are not trying to escape a single quote
  106. if printf "%s" "$SYNO_Certificate" | grep '\\'; then
  107. _err "Do not use a backslash (\) in your certificate description"
  108. return 1
  109. fi
  110. if [ -n "$SYNO_UseTempAdmin" ]; then
  111. _debug "Creating temp admin user in Synology DSM..."
  112. synouser --add "$SYNO_Username" "$SYNO_Password" "" 0 "scruelt@hotmail.com" 0 >/dev/null
  113. synogroup --memberadd administrators "$SYNO_Username" >/dev/null
  114. fi
  115. _base_url="$SYNO_Scheme://$SYNO_Hostname:$SYNO_Port"
  116. _debug _base_url "$_base_url"
  117. _debug "Getting API version..."
  118. response=$(_get "$_base_url/webapi/query.cgi?api=SYNO.API.Info&version=1&method=query&query=SYNO.API.Auth")
  119. api_path=$(echo "$response" | grep "SYNO.API.Auth" | sed -n 's/.*"path" *: *"\([^"]*\)".*/\1/p')
  120. api_version=$(echo "$response" | grep "SYNO.API.Auth" | sed -n 's/.*"maxVersion" *: *\([0-9]*\).*/\1/p')
  121. _debug3 response "$response"
  122. _debug3 api_path "$api_path"
  123. _debug3 api_version "$api_version"
  124. # Login, get the session ID and SynoToken from JSON
  125. _info "Logging into $SYNO_Hostname:$SYNO_Port..."
  126. encoded_username="$(printf "%s" "$SYNO_Username" | _url_encode)"
  127. encoded_password="$(printf "%s" "$SYNO_Password" | _url_encode)"
  128. otp_code=""
  129. # ## START ## - DEPRECATED, for backward compatibility
  130. _getdeployconf SYNO_TOTP_SECRET
  131. if [ -n "$SYNO_TOTP_SECRET" ]; then
  132. _info "WARNING: Usage of SYNO_TOTP_SECRET is deprecated!"
  133. _info " See synology_dsm.sh script or ACME.sh Wiki page for details:"
  134. _info " https://github.com/acmesh-official/acme.sh/wiki/Synology-NAS-Guide"
  135. if ! _exists oathtool; then
  136. _err "oathtool could not be found, install oathtool to use SYNO_TOTP_SECRET"
  137. return 1
  138. fi
  139. DEPRECATED_otp_code="$(oathtool --base32 --totp "$SYNO_TOTP_SECRET" 2>/dev/null)"
  140. if [ -z "$SYNO_DeviceID" ]; then
  141. _getdeployconf SYNO_DID
  142. [ -n "$SYNO_DID" ] || SYNO_DeviceID="$SYNO_DID"
  143. fi
  144. if [ -n "$SYNO_DeviceID" ]; then
  145. _H1="Cookie: did=$SYNO_DeviceID"
  146. export _H1
  147. _debug3 H1 "${_H1}"
  148. fi
  149. 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_DeviceID" "$_base_url/webapi/auth.cgi?enable_syno_token=yes")
  150. _debug3 response "$response"
  151. # ## END ## - DEPRECATED, for backward compatibility
  152. # If SYNO_Device_ID & SYNO_DeviceName both empty, just log in normally
  153. elif [ -z "${SYNO_DeviceID:-}" ] && [ -z "${SYNO_DeviceName:-}" ]; then
  154. 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")
  155. _debug3 response "$response"
  156. # Get device ID if still empty first, otherwise log in right away
  157. # If SYNO_DeviceName is set, we treat that account enabled two-factor authorization, consider SYNO_DeviceID is not set, so it won't be able to login without requiring the OTP code.
  158. elif [ -n "${SYNO_DeviceName:-}" ] && [ -z "${SYNO_DeviceID:-}" ]; then
  159. printf "Enter OTP code for user '%s': " "$SYNO_Username"
  160. read -r otp_code
  161. response=$(_get "$_base_url/webapi/$api_path?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_DeviceName")
  162. _secure_debug3 response "$response"
  163. id_property='device_id'
  164. [ "${api_version}" -gt '6' ] || id_property='did'
  165. SYNO_DeviceID=$(echo "$response" | grep "$id_property" | sed -n 's/.*"'$id_property'" *: *"\([^"]*\).*/\1/p')
  166. _secure_debug2 SYNO_DeviceID "$SYNO_DeviceID"
  167. # Otherwise, if SYNO_DeviceID is set, we can just use it to login.
  168. else
  169. if [ -z "${SYNO_DeviceName:-}" ]; then
  170. printf "Enter device name or leave empty for default (CertRenewal): "
  171. read -r SYNO_DeviceName
  172. [ -n "${SYNO_DeviceName}" ] || SYNO_DeviceName="CertRenewal"
  173. fi
  174. response=$(_get "$_base_url/webapi/$api_path?api=SYNO.API.Auth&version=$api_version&method=login&format=sid&account=$encoded_username&passwd=$encoded_password&enable_syno_token=yes&device_name=$SYNO_DeviceName&device_id=$SYNO_DeviceID")
  175. _secure_debug3 response "$response"
  176. fi
  177. sid=$(echo "$response" | grep "sid" | sed -n 's/.*"sid" *: *"\([^"]*\).*/\1/p')
  178. token=$(echo "$response" | grep "synotoken" | sed -n 's/.*"synotoken" *: *"\([^"]*\).*/\1/p')
  179. _debug "Session ID" "$sid"
  180. _debug SynoToken "$token"
  181. if [ -z "$sid" ] || [ -z "$token" ]; then
  182. _err "Unable to authenticate to $_base_url - check your username & password."
  183. _err "If two-factor authentication is enabled for the user:"
  184. _err "- set SYNO_DeviceName then input *correct* OTP-code manually"
  185. _err "- get & set SYNO_DeviceID via your browser cookies"
  186. _remove_temp_admin "$SYNO_USE_TEMP_ADMIN" "$SYNO_Username"
  187. return 1
  188. fi
  189. _H1="X-SYNO-TOKEN: $token"
  190. export _H1
  191. _debug2 H1 "${_H1}"
  192. # Now that we know the username and password are good, save them if not in temp admin mode.
  193. if [ -z "$SYNO_UseTempAdmin" ]; then
  194. _savedeployconf SYNO_Username "$SYNO_Username"
  195. _savedeployconf SYNO_Password "$SYNO_Password"
  196. _savedeployconf SYNO_DeviceID "$SYNO_DeviceID"
  197. _savedeployconf SYNO_DeviceName "$SYNO_DeviceName"
  198. fi
  199. _info "Getting certificates in Synology DSM..."
  200. response=$(_post "api=SYNO.Core.Certificate.CRT&method=list&version=1&_sid=$sid" "$_base_url/webapi/entry.cgi")
  201. _debug3 response "$response"
  202. escaped_certificate="$(printf "%s" "$SYNO_Certificate" | sed 's/\([].*^$[]\)/\\\1/g;s/"/\\\\"/g')"
  203. _debug escaped_certificate "$escaped_certificate"
  204. id=$(echo "$response" | sed -n "s/.*\"desc\":\"$escaped_certificate\",\"id\":\"\([^\"]*\).*/\1/p")
  205. _debug2 id "$id"
  206. if [ -z "$id" ] && [ -z "$SYNO_Create" ]; then
  207. _err "Unable to find certificate: $SYNO_Certificate and $SYNO_Create is not set."
  208. _remove_temp_admin "$SYNO_UseTempAdmin" "$SYNO_Username"
  209. return 1
  210. fi
  211. # We've verified this certificate description is a thing, so save it
  212. _savedeployconf SYNO_Certificate "$SYNO_Certificate" "base64"
  213. _info "Generating form POST request.."
  214. nl="\0015\0012"
  215. delim="--------------------------$(_utc_date | tr -d -- '-: ')"
  216. content="--$delim${nl}Content-Disposition: form-data; name=\"key\"; filename=\"$(basename "$_ckey")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_ckey")\0012"
  217. 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"
  218. 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"
  219. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"id\"${nl}${nl}$id"
  220. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"desc\"${nl}${nl}${SYNO_Certificate}"
  221. if echo "$response" | sed -n "s/.*\"desc\":\"$escaped_certificate\",\([^{]*\).*/\1/p" | grep -- 'is_default":true' >/dev/null; then
  222. _debug2 default "This is the default certificate"
  223. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"as_default\"${nl}${nl}true"
  224. else
  225. _debug2 default "This is NOT the default certificate"
  226. fi
  227. content="$content${nl}--$delim--${nl}"
  228. content="$(printf "%b_" "$content")"
  229. content="${content%_}" # protect trailing \n
  230. _info "Upload certificate to the Synology DSM."
  231. 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}")
  232. _debug3 response "$response"
  233. if ! echo "$response" | grep '"error":' >/dev/null; then
  234. if echo "$response" | grep '"restart_httpd":true' >/dev/null; then
  235. _info "Restarting HTTP services succeeded..."
  236. else
  237. _info "Restarting HTTP services failed."
  238. fi
  239. _remove_temp_admin "$SYNO_UseTempAdmin" "$SYNO_Username"
  240. _logout
  241. return 0
  242. else
  243. _remove_temp_admin "$SYNO_UseTempAdmin" "$SYNO_Username"
  244. _err "Unable to update certificate, error code $response."
  245. _logout
  246. return 1
  247. fi
  248. }
  249. #################### Private functions below ##################################
  250. _logout() {
  251. # Logout CERT user only to not occupy a permanent session, e.g. in DSM's "Connected Users" widget (based on previous variables)
  252. response=$(_get "$_base_url/webapi/$api_path?api=SYNO.API.Auth&version=$api_version&method=logout&_sid=$sid")
  253. _debug3 response "$response"
  254. }
  255. _remove_temp_admin() {
  256. flag=$1
  257. username=$2
  258. if [ -n "${flag}" ]; then
  259. _debug "Removing temp admin user in Synology DSM..."
  260. synouser --del "$username" >/dev/null
  261. fi
  262. }