2023-05-28 21:42:53 +02:00
#!/bin/bash
################################################################################
# ACME.sh 3rd party deploy plugin for Synology DSM
################################################################################
# Authors: Brian Hartvigsen (creator), https://github.com/tresni
# Martin Arndt (contributor), https://troublezone.net/
2023-07-04 15:47:19 +02:00
# Updated: 2023-07-03
2023-05-28 21:42:53 +02:00
# Issues: https://github.com/acmesh-official/acme.sh/issues/2727
################################################################################
# Usage:
2024-02-22 05:38:51 +01:00
# 1. Set required environment variables (two ways):
2024-02-23 12:58:24 +01:00
# - Create temp admin user automatically
# `export SYNO_UseTempAdmin=1`
# - Or provide your own admin user credential by:
# 1. `export SYNO_Username="adminUser"`
# 2. `export SYNO_Password="adminPassword"`
# 2. Set optional environment variables (shown values are the defaults)
# - `export SYNO_Scheme="http"`
# - `export SYNO_Hostname="localhost"`
# - `export SYNO_Port="5000"`
# - `export SYNO_Create=1` - to allow creating the cert if it doesn't exist
# - `export SYNO_Certificate=""` - to replace a specific cert by its description
# - `export SYNO_DeviceName=""` - required for 2FA-OTP
# - `export SYNO_DeviceID=""` - required for omitting 2FA-OTP (only pro
# users may set)
2024-02-22 05:38:51 +01:00
# 3. Run command:
# acme.sh --deploy --deploy-hook synology_dsm -d example.com
2023-05-28 21:42:53 +02:00
################################################################################
2021-11-13 10:56:10 +01:00
# Dependencies:
2023-05-28 21:42:53 +02:00
# - jq & curl
2024-02-23 12:58:24 +01:00
# - synouser & synogroup (When available and SYNO_UseTempAdmin is set)
2023-05-28 21:42:53 +02:00
################################################################################
# Return value:
# 0 means success, otherwise error.
################################################################################
2019-06-30 05:47:24 +02:00
2023-05-28 21:42:53 +02:00
########## Public functions ####################################################
2019-06-30 05:47:24 +02:00
#domain keyfile certfile cafile fullchain
synology_dsm_deploy( ) {
_cdomain = " $1 "
_ckey = " $2 "
_ccert = " $3 "
_cca = " $4 "
_debug _cdomain " $_cdomain "
2024-02-23 12:58:24 +01:00
# Get username and password, but don't save until we authenticated successfully
2020-02-09 12:10:11 +01:00
_getdeployconf SYNO_Username
_getdeployconf SYNO_Password
2024-02-23 12:58:24 +01:00
_getdeployconf SYNO_DeviceName
_getdeployconf SYNO_DeviceID
2020-02-09 12:10:11 +01:00
_getdeployconf SYNO_Create
2024-02-23 12:58:24 +01:00
# ## START ## - DEPRECATED, for backward compatibility
2023-05-28 21:42:53 +02:00
_getdeployconf SYNO_Device_ID
2024-02-23 12:58:24 +01:00
_getdeployconf SYNO_Device_Name
[ -n " $SYNO_DeviceID " ] || SYNO_DeviceID = " $SYNO_Device_ID "
[ -n " $SYNO_DeviceName " ] || SYNO_DeviceName = " $SYNO_Device_Name "
# ## END ## - DEPRECATED, for backward compatibility
[ -n " $SYNO_Create " ] || SYNO_Create = 1
# Prepare to use temp admin if SYNO_UseTempAdmin is set
_getdeployconf SYNO_UseTempAdmin
_debug2 SYNO_UseTempAdmin " $SYNO_UseTempAdmin "
2023-07-19 20:48:29 +02:00
2024-02-23 12:58:24 +01:00
if [ -n " $SYNO_UseTempAdmin " ] ; then
if ! _exists synouser || ! _exists synogroup; then
_err "Tools are missing for creating temp admin user, please set SYNO_Username and SYNO_Password instead."
return 1
2023-07-19 20:48:29 +02:00
fi
2024-02-23 12:58:24 +01:00
# Clean up
[ -n " $SYNO_Username " ] || _savedeployconf SYNO_Username ""
[ -n " $SYNO_Password " ] || _savedeployconf SYNO_Password ""
synouser --del " $SYNO_Username " >/dev/null 2>/dev/null
2023-07-19 20:48:29 +02:00
_debug "Setting temp admin user credential..."
SYNO_Username = sc-acmesh-tmp
2023-07-20 07:09:21 +02:00
SYNO_Password = $( head /dev/urandom | tr -dc A-Za-z0-9 | head -c 16)
2023-07-19 20:48:29 +02:00
# Ignore 2FA-OTP settings which won't be needed.
2024-02-23 12:58:24 +01:00
SYNO_DeviceID =
SYNO_DeviceName =
_savedeployconf SYNO_UseTempAdmin " $SYNO_UseTempAdmin "
else
_debug2 SYNO_Username " $SYNO_Username "
_secure_debug2 SYNO_Password " $SYNO_Password "
_debug2 SYNO_Create " $SYNO_Create "
_debug2 SYNO_DeviceName " $SYNO_DeviceName "
_secure_debug2 SYNO_DeviceID " $SYNO_DeviceID "
2019-06-30 05:47:24 +02:00
fi
2023-07-20 07:34:57 +02:00
2024-02-23 12:58:24 +01:00
if [ -z " $SYNO_Username " ] || [ -z " $SYNO_Password " ] ; then
_err "You must set either SYNO_UseTempAdmin, or set both SYNO_Username and SYNO_Password."
2023-05-28 21:42:53 +02:00
return 1
fi
2019-06-30 05:47:24 +02:00
2024-02-23 12:58:24 +01:00
# Optional scheme, hostname and port for Synology DSM
2020-02-09 12:10:11 +01:00
_getdeployconf SYNO_Scheme
_getdeployconf SYNO_Hostname
_getdeployconf SYNO_Port
2019-06-30 05:47:24 +02:00
2024-02-23 12:58:24 +01:00
# Default values for scheme, hostname and port
# Defaulting to localhost and http, because it's localhost…
[ -n " $SYNO_Scheme " ] || SYNO_Scheme = "http"
[ -n " $SYNO_Hostname " ] || SYNO_Hostname = "localhost"
[ -n " $SYNO_Port " ] || SYNO_Port = "5000"
2020-02-09 12:10:11 +01:00
_savedeployconf SYNO_Scheme " $SYNO_Scheme "
_savedeployconf SYNO_Hostname " $SYNO_Hostname "
_savedeployconf SYNO_Port " $SYNO_Port "
2019-06-30 05:47:24 +02:00
_debug2 SYNO_Scheme " $SYNO_Scheme "
_debug2 SYNO_Hostname " $SYNO_Hostname "
_debug2 SYNO_Port " $SYNO_Port "
2023-07-04 15:47:19 +02:00
# Get the certificate description, but don't save it until we verify it's real
2019-06-30 05:47:24 +02:00
_getdeployconf SYNO_Certificate
2024-02-23 12:58:24 +01:00
_debug SYNO_Certificate " $SYNO_Certificate "
2019-06-30 05:47:24 +02:00
2021-05-26 23:24:50 +02:00
# shellcheck disable=SC1003 # We are not trying to escape a single quote
2021-05-26 23:07:23 +02:00
if printf "%s" " $SYNO_Certificate " | grep '\\' ; then
_err "Do not use a backslash (\) in your certificate description"
return 1
fi
2024-02-23 12:58:24 +01:00
if [ -n " $SYNO_UseTempAdmin " ] ; then
_debug "Creating temp admin user in Synology DSM..."
synouser --add " $SYNO_Username " " $SYNO_Password " "" 0 "scruelt@hotmail.com" 0 >/dev/null
synogroup --memberadd administrators " $SYNO_Username " >/dev/null
fi
2019-06-30 05:47:24 +02:00
_base_url = " $SYNO_Scheme :// $SYNO_Hostname : $SYNO_Port "
_debug _base_url " $_base_url "
2024-02-23 12:58:24 +01:00
_debug "Getting API version..."
2020-12-09 11:45:25 +01:00
response = $( _get " $_base_url /webapi/query.cgi?api=SYNO.API.Info&version=1&method=query&query=SYNO.API.Auth " )
2023-09-25 17:43:01 +02:00
api_path = $( echo " $response " | grep "SYNO.API.Auth" | sed -n 's/.*"path" *: *"\([^"]*\)".*/\1/p' )
2020-12-09 11:45:25 +01:00
api_version = $( echo " $response " | grep "SYNO.API.Auth" | sed -n 's/.*"maxVersion" *: *\([0-9]*\).*/\1/p' )
_debug3 response " $response "
2023-08-09 19:52:37 +02:00
_debug3 api_path " $api_path "
2020-12-09 11:45:25 +01:00
_debug3 api_version " $api_version "
2024-02-23 12:58:24 +01:00
# Login, get the session ID and SynoToken from JSON
_info " Logging into $SYNO_Hostname : $SYNO_Port ... "
2020-05-16 07:48:50 +02:00
encoded_username = " $( printf "%s" " $SYNO_Username " | _url_encode) "
encoded_password = " $( printf "%s" " $SYNO_Password " | _url_encode) "
2023-07-04 16:58:14 +02:00
otp_code = ""
2024-02-23 12:58:24 +01:00
# ## START ## - DEPRECATED, for backward compatibility
_getdeployconf SYNO_TOTP_SECRET
2023-07-04 15:47:19 +02:00
if [ -n " $SYNO_TOTP_SECRET " ] ; then
_info "WARNING: Usage of SYNO_TOTP_SECRET is deprecated!"
_info " See synology_dsm.sh script or ACME.sh Wiki page for details:"
_info " https://github.com/acmesh-official/acme.sh/wiki/Synology-NAS-Guide"
2023-07-19 20:48:29 +02:00
if ! _exists oathtool; then
2023-07-04 15:47:19 +02:00
_err "oathtool could not be found, install oathtool to use SYNO_TOTP_SECRET"
return 1
fi
2024-02-23 12:58:24 +01:00
DEPRECATED_otp_code = " $( oathtool --base32 --totp " $SYNO_TOTP_SECRET " 2>/dev/null) "
2020-12-09 11:45:25 +01:00
2024-02-23 12:58:24 +01:00
if [ -z " $SYNO_DeviceID " ] ; then
_getdeployconf SYNO_DID
[ -n " $SYNO_DID " ] || SYNO_DeviceID = " $SYNO_DID "
fi
if [ -n " $SYNO_DeviceID " ] ; then
_H1 = " Cookie: did= $SYNO_DeviceID "
2023-07-04 15:47:19 +02:00
export _H1
_debug3 H1 " ${ _H1 } "
fi
2024-02-23 12:58:24 +01:00
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 " )
2023-07-04 15:47:19 +02:00
_debug3 response " $response "
2024-02-23 12:58:24 +01:00
# ## END ## - DEPRECATED, for backward compatibility
# If SYNO_Device_ID & SYNO_DeviceName both empty, just log in normally
elif [ -z " ${ SYNO_DeviceID :- } " ] && [ -z " ${ SYNO_DeviceName :- } " ] ; then
2023-07-19 20:48:29 +02:00
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 " )
_debug3 response " $response "
2023-05-28 21:42:53 +02:00
# Get device ID if still empty first, otherwise log in right away
2024-02-23 12:58:24 +01:00
# 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.
elif [ -n " ${ SYNO_DeviceName :- } " ] && [ -z " ${ SYNO_DeviceID :- } " ] ; then
printf "Enter OTP code for user '%s': " " $SYNO_Username "
read -r otp_code
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 " )
_secure_debug3 response " $response "
2023-08-11 17:55:45 +02:00
2024-02-23 12:58:24 +01:00
id_property = 'device_id'
[ " ${ api_version } " -gt '6' ] || id_property = 'did'
SYNO_DeviceID = $( echo " $response " | grep " $id_property " | sed -n 's/.*"' $id_property '" *: *"\([^"]*\).*/\1/p' )
_secure_debug2 SYNO_DeviceID " $SYNO_DeviceID "
# Otherwise, if SYNO_DeviceID is set, we can just use it to login.
2023-05-28 21:42:53 +02:00
else
2024-02-23 12:58:24 +01:00
if [ -z " ${ SYNO_DeviceName :- } " ] ; then
2023-07-19 20:48:29 +02:00
printf "Enter device name or leave empty for default (CertRenewal): "
2024-02-23 12:58:24 +01:00
read -r SYNO_DeviceName
[ -n " ${ SYNO_DeviceName } " ] || SYNO_DeviceName = "CertRenewal"
2023-07-19 20:48:29 +02:00
fi
2024-02-23 12:58:24 +01:00
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 " )
2023-09-07 08:57:53 +02:00
_secure_debug3 response " $response "
2020-12-09 11:45:25 +01:00
fi
2023-05-28 21:42:53 +02:00
sid = $( echo " $response " | grep "sid" | sed -n 's/.*"sid" *: *"\([^"]*\).*/\1/p' )
2020-12-09 11:45:25 +01:00
token = $( echo " $response " | grep "synotoken" | sed -n 's/.*"synotoken" *: *"\([^"]*\).*/\1/p' )
2023-05-28 22:33:15 +02:00
_debug "Session ID" " $sid "
2023-05-28 21:42:53 +02:00
_debug SynoToken " $token "
2023-07-19 20:48:29 +02:00
if [ -z " $sid " ] || [ -z " $token " ] ; then
2023-05-28 21:42:53 +02:00
_err " Unable to authenticate to $_base_url - check your username & password. "
2023-07-19 20:48:29 +02:00
_err "If two-factor authentication is enabled for the user:"
2024-02-23 12:58:24 +01:00
_err "- set SYNO_DeviceName then input *correct* OTP-code manually"
_err "- get & set SYNO_DeviceID via your browser cookies"
2023-07-19 20:48:29 +02:00
_remove_temp_admin " $SYNO_USE_TEMP_ADMIN " " $SYNO_Username "
2019-06-30 05:47:24 +02:00
return 1
fi
2020-12-09 11:45:25 +01:00
_H1 = " X-SYNO-TOKEN: $token "
2020-02-09 01:27:18 +01:00
export _H1
2020-02-09 11:50:29 +01:00
_debug2 H1 " ${ _H1 } "
2020-02-09 01:27:18 +01:00
2024-02-23 12:58:24 +01:00
# Now that we know the username and password are good, save them if not in temp admin mode.
if [ -z " $SYNO_UseTempAdmin " ] ; then
_savedeployconf SYNO_Username " $SYNO_Username "
_savedeployconf SYNO_Password " $SYNO_Password "
_savedeployconf SYNO_DeviceID " $SYNO_DeviceID "
_savedeployconf SYNO_DeviceName " $SYNO_DeviceName "
2023-07-19 20:48:29 +02:00
fi
2019-06-30 05:47:24 +02:00
2024-02-23 12:58:24 +01:00
_info "Getting certificates in Synology DSM..."
2020-12-09 11:45:25 +01:00
response = $( _post " api=SYNO.Core.Certificate.CRT&method=list&version=1&_sid= $sid " " $_base_url /webapi/entry.cgi " )
2019-06-30 05:47:24 +02:00
_debug3 response " $response "
2021-05-26 23:07:23 +02:00
escaped_certificate = " $( printf "%s" " $SYNO_Certificate " | sed 's/\([].*^$[]\)/\\\1/g;s/"/\\\\"/g' ) "
_debug escaped_certificate " $escaped_certificate "
id = $( echo " $response " | sed -n " s/.*\"desc\":\" $escaped_certificate \",\"id\":\"\([^\"]*\).*/\1/p " )
2020-02-09 01:27:18 +01:00
_debug2 id " $id "
2019-06-30 05:47:24 +02:00
2024-02-23 12:58:24 +01:00
if [ -z " $id " ] && [ -z " $SYNO_Create " ] ; then
_err " Unable to find certificate: $SYNO_Certificate and $SYNO_Create is not set. "
_remove_temp_admin " $SYNO_UseTempAdmin " " $SYNO_Username "
2019-06-30 05:47:24 +02:00
return 1
fi
2023-07-04 15:47:19 +02:00
# We've verified this certificate description is a thing, so save it
2021-05-26 23:07:23 +02:00
_savedeployconf SYNO_Certificate " $SYNO_Certificate " "base64"
2019-06-30 05:47:24 +02:00
2024-02-23 12:58:24 +01:00
_info "Generating form POST request.."
2020-05-19 07:27:00 +02:00
nl = "\0015\0012"
2020-02-09 20:50:50 +01:00
delim = " -------------------------- $( _utc_date | tr -d -- '-: ' ) "
2020-05-19 07:27:00 +02:00
content = " -- $delim ${ nl } Content-Disposition: form-data; name=\"key\"; filename=\" $( basename " $_ckey " ) \" ${ nl } Content-Type: application/octet-stream ${ nl } ${ nl } $( cat " $_ckey " ) \0012 "
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 "
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 "
2020-02-09 01:27:18 +01:00
content = " $content ${ nl } -- $delim ${ nl } Content-Disposition: form-data; name=\"id\" ${ nl } ${ nl } $id "
content = " $content ${ nl } -- $delim ${ nl } Content-Disposition: form-data; name=\"desc\" ${ nl } ${ nl } ${ SYNO_Certificate } "
2021-05-26 23:07:23 +02:00
if echo " $response " | sed -n " s/.*\"desc\":\" $escaped_certificate \",\([^{]*\).*/\1/p " | grep -- 'is_default":true' >/dev/null; then
2023-07-04 15:47:19 +02:00
_debug2 default "This is the default certificate"
2021-05-19 21:21:34 +02:00
content = " $content ${ nl } -- $delim ${ nl } Content-Disposition: form-data; name=\"as_default\" ${ nl } ${ nl } true "
else
2023-07-04 15:47:19 +02:00
_debug2 default "This is NOT the default certificate"
2021-05-19 21:21:34 +02:00
fi
2020-02-09 01:27:18 +01:00
content = " $content ${ nl } -- $delim -- ${ nl } "
2020-02-09 11:01:26 +01:00
content = " $( printf "%b_" " $content " ) "
content = " ${ content %_ } " # protect trailing \n
2020-02-09 01:27:18 +01:00
2024-02-23 12:58:24 +01:00
_info "Upload certificate to the Synology DSM."
2020-12-09 11:45:25 +01:00
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 } " )
2019-06-30 05:47:24 +02:00
_debug3 response " $response "
2020-02-11 05:02:27 +01:00
if ! echo " $response " | grep '"error":' >/dev/null; then
if echo " $response " | grep '"restart_httpd":true' >/dev/null; then
2024-02-23 12:58:24 +01:00
_info "Restarting HTTP services succeeded..."
2019-06-30 05:47:24 +02:00
else
2024-02-23 12:58:24 +01:00
_info "Restarting HTTP services failed."
2019-06-30 05:47:24 +02:00
fi
2024-02-23 12:58:24 +01:00
_remove_temp_admin " $SYNO_UseTempAdmin " " $SYNO_Username "
2023-05-28 21:42:53 +02:00
_logout
2019-06-30 08:13:45 +02:00
return 0
2019-06-30 05:47:24 +02:00
else
2024-02-23 12:58:24 +01:00
_remove_temp_admin " $SYNO_UseTempAdmin " " $SYNO_Username "
_err " Unable to update certificate, error code $response . "
2023-05-28 21:42:53 +02:00
_logout
2019-06-30 05:47:24 +02:00
return 1
fi
}
2023-05-28 21:42:53 +02:00
#################### Private functions below ##################################
_logout( ) {
2023-12-22 20:36:52 +01:00
# Logout CERT user only to not occupy a permanent session, e.g. in DSM's "Connected Users" widget (based on previous variables)
2023-12-22 15:09:29 +01:00
response = $( _get " $_base_url /webapi/ $api_path ?api=SYNO.API.Auth&version= $api_version &method=logout&_sid= $sid " )
2023-05-28 21:42:53 +02:00
_debug3 response " $response "
}
2023-07-19 20:48:29 +02:00
_remove_temp_admin( ) {
flag = $1
username = $2
2023-07-20 07:34:57 +02:00
2023-07-19 20:48:29 +02:00
if [ -n " ${ flag } " ] ; then
2024-02-23 12:58:24 +01:00
_debug "Removing temp admin user in Synology DSM..."
2023-07-19 20:48:29 +02:00
synouser --del " $username " >/dev/null
fi
}