2019-06-30 03:47:24 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
2019-06-30 06:30:35 +00:00
|
|
|
# Here is a script to deploy cert to Synology DSM
|
2019-06-30 03:47:24 +00:00
|
|
|
#
|
|
|
|
# it requires the jq and curl are in the $PATH and the following
|
|
|
|
# environment variables must be set:
|
|
|
|
#
|
|
|
|
# SYNO_Username - Synology Username to login (must be an administrator)
|
|
|
|
# SYNO_Password - Synology Password to login
|
|
|
|
# SYNO_Certificate - Certificate description to target for replacement
|
|
|
|
#
|
|
|
|
# The following environmental variables may be set if you don't like their
|
|
|
|
# default values:
|
|
|
|
#
|
|
|
|
# SYNO_Scheme - defaults to http
|
|
|
|
# SYNO_Hostname - defaults to localhost
|
|
|
|
# SYNO_Port - defaults to 5000
|
2020-03-08 18:49:46 +00:00
|
|
|
# SYNO_DID - device ID to skip OTP - defaults to empty
|
2019-06-30 03:47:24 +00:00
|
|
|
#
|
|
|
|
#returns 0 means success, otherwise error.
|
|
|
|
|
|
|
|
######## Public functions #####################
|
|
|
|
|
2020-02-09 00:27:18 +00:00
|
|
|
_syno_get_cookie_data() {
|
2020-07-26 03:48:35 +00:00
|
|
|
grep -i "\W$1=" | grep -i "^Set-Cookie:" | _tail_n 1 | _egrep_o "$1=[^;]*;" | tr -d ';'
|
2020-02-09 00:27:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 03:47:24 +00:00
|
|
|
#domain keyfile certfile cafile fullchain
|
|
|
|
synology_dsm_deploy() {
|
|
|
|
|
|
|
|
_cdomain="$1"
|
|
|
|
_ckey="$2"
|
|
|
|
_ccert="$3"
|
|
|
|
_cca="$4"
|
|
|
|
|
|
|
|
_debug _cdomain "$_cdomain"
|
|
|
|
|
|
|
|
# Get Username and Password, but don't save until we successfully authenticate
|
2020-02-09 11:10:11 +00:00
|
|
|
_getdeployconf SYNO_Username
|
|
|
|
_getdeployconf SYNO_Password
|
|
|
|
_getdeployconf SYNO_Create
|
2020-03-08 19:22:31 +00:00
|
|
|
_getdeployconf SYNO_DID
|
2020-05-16 06:19:18 +00:00
|
|
|
if [ -z "${SYNO_Username:-}" ] || [ -z "${SYNO_Password:-}" ]; then
|
2019-06-30 03:47:24 +00:00
|
|
|
_err "SYNO_Username & SYNO_Password must be set"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
_debug2 SYNO_Username "$SYNO_Username"
|
|
|
|
_secure_debug2 SYNO_Password "$SYNO_Password"
|
|
|
|
|
|
|
|
# Optional scheme, hostname, and port for Synology DSM
|
2020-02-09 11:10:11 +00:00
|
|
|
_getdeployconf SYNO_Scheme
|
|
|
|
_getdeployconf SYNO_Hostname
|
|
|
|
_getdeployconf SYNO_Port
|
2019-06-30 03:47:24 +00:00
|
|
|
|
|
|
|
# default vaules 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 11:10:11 +00:00
|
|
|
_savedeployconf SYNO_Scheme "$SYNO_Scheme"
|
|
|
|
_savedeployconf SYNO_Hostname "$SYNO_Hostname"
|
|
|
|
_savedeployconf SYNO_Port "$SYNO_Port"
|
|
|
|
|
2019-06-30 03:47:24 +00:00
|
|
|
_debug2 SYNO_Scheme "$SYNO_Scheme"
|
|
|
|
_debug2 SYNO_Hostname "$SYNO_Hostname"
|
|
|
|
_debug2 SYNO_Port "$SYNO_Port"
|
|
|
|
|
|
|
|
# Get the certificate description, but don't save it until we verfiy it's real
|
|
|
|
_getdeployconf SYNO_Certificate
|
2020-05-16 08:25:53 +00:00
|
|
|
_debug SYNO_Certificate "${SYNO_Certificate:-}"
|
2019-06-30 03:47:24 +00:00
|
|
|
|
|
|
|
_base_url="$SYNO_Scheme://$SYNO_Hostname:$SYNO_Port"
|
|
|
|
_debug _base_url "$_base_url"
|
|
|
|
|
|
|
|
# Login, get the token from JSON and session id from cookie
|
2020-02-09 00:27:18 +00:00
|
|
|
_info "Logging into $SYNO_Hostname:$SYNO_Port"
|
2020-05-16 05:48:50 +00:00
|
|
|
encoded_username="$(printf "%s" "$SYNO_Username" | _url_encode)"
|
|
|
|
encoded_password="$(printf "%s" "$SYNO_Password" | _url_encode)"
|
|
|
|
encoded_did="$(printf "%s" "$SYNO_DID" | _url_encode)"
|
2020-05-16 05:53:00 +00:00
|
|
|
response=$(_get "$_base_url/webman/login.cgi?username=$encoded_username&passwd=$encoded_password&enable_syno_token=yes&device_id=$encoded_did" 1)
|
2020-07-26 03:48:35 +00:00
|
|
|
token=$(echo "$response" | grep -i "X-SYNO-TOKEN:" | sed -n 's/^X-SYNO-TOKEN: \(.*\)$/\1/pI' | tr -d "\r\n")
|
2020-02-09 00:27:18 +00:00
|
|
|
_debug3 response "$response"
|
2020-05-16 05:53:00 +00:00
|
|
|
_debug token "$token"
|
2020-02-09 00:27:18 +00:00
|
|
|
|
|
|
|
if [ -z "$token" ]; then
|
2019-06-30 03:47:24 +00:00
|
|
|
_err "Unable to authenticate to $SYNO_Hostname:$SYNO_Port using $SYNO_Scheme."
|
|
|
|
_err "Check your username and password."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2020-05-16 05:53:00 +00:00
|
|
|
_H1="Cookie: $(echo "$response" | _syno_get_cookie_data "id"); $(echo "$response" | _syno_get_cookie_data "smid")"
|
2020-02-09 00:27:18 +00:00
|
|
|
_H2="X-SYNO-TOKEN: $token"
|
|
|
|
export _H1
|
|
|
|
export _H2
|
2020-02-09 10:50:29 +00:00
|
|
|
_debug2 H1 "${_H1}"
|
|
|
|
_debug2 H2 "${_H2}"
|
2020-02-09 00:27:18 +00:00
|
|
|
|
2019-06-30 03:47:24 +00:00
|
|
|
# Now that we know the username and password are good, save them
|
2020-02-09 00:27:18 +00:00
|
|
|
_savedeployconf SYNO_Username "$SYNO_Username"
|
|
|
|
_savedeployconf SYNO_Password "$SYNO_Password"
|
2020-03-08 19:22:31 +00:00
|
|
|
_savedeployconf SYNO_DID "$SYNO_DID"
|
2019-06-30 03:47:24 +00:00
|
|
|
|
2020-02-09 00:27:18 +00:00
|
|
|
_info "Getting certificates in Synology DSM"
|
|
|
|
response=$(_post "api=SYNO.Core.Certificate.CRT&method=list&version=1" "$_base_url/webapi/entry.cgi")
|
2019-06-30 03:47:24 +00:00
|
|
|
_debug3 response "$response"
|
2020-02-09 10:01:26 +00:00
|
|
|
id=$(echo "$response" | sed -n "s/.*\"desc\":\"$SYNO_Certificate\",\"id\":\"\([^\"]*\).*/\1/p")
|
2020-02-09 00:27:18 +00:00
|
|
|
_debug2 id "$id"
|
2019-06-30 03:47:24 +00:00
|
|
|
|
2020-05-16 06:19:18 +00:00
|
|
|
if [ -z "$id" ] && [ -z "${SYNO_Create:-}" ]; then
|
2020-02-09 10:26:55 +00:00
|
|
|
_err "Unable to find certificate: $SYNO_Certificate and \$SYNO_Create is not set"
|
2019-06-30 03:47:24 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# we've verified this certificate description is a thing, so save it
|
|
|
|
_savedeployconf SYNO_Certificate "$SYNO_Certificate"
|
|
|
|
|
2020-02-09 00:27:18 +00:00
|
|
|
default=false
|
2020-02-11 04:02:27 +00:00
|
|
|
if echo "$response" | sed -n "s/.*\"desc\":\"$SYNO_Certificate\",\([^{]*\).*/\1/p" | grep -- 'is_default":true' >/dev/null; then
|
2020-02-09 10:01:26 +00:00
|
|
|
default=true
|
2020-02-09 00:27:18 +00:00
|
|
|
fi
|
2019-06-30 03:47:24 +00:00
|
|
|
_debug2 default "$default"
|
|
|
|
|
2020-02-09 00:27:18 +00:00
|
|
|
_info "Generate form POST request"
|
2020-05-19 05:27:00 +00:00
|
|
|
nl="\0015\0012"
|
2020-02-09 19:50:50 +00:00
|
|
|
delim="--------------------------$(_utc_date | tr -d -- '-: ')"
|
2020-05-19 05:27:00 +00: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 00:27:18 +00: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}"
|
|
|
|
content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"as_default\"${nl}${nl}${default}"
|
|
|
|
content="$content${nl}--$delim--${nl}"
|
2020-02-09 10:01:26 +00:00
|
|
|
content="$(printf "%b_" "$content")"
|
|
|
|
content="${content%_}" # protect trailing \n
|
2020-02-09 00:27:18 +00:00
|
|
|
|
|
|
|
_info "Upload certificate to the Synology DSM"
|
|
|
|
response=$(_post "$content" "$_base_url/webapi/entry.cgi?api=SYNO.Core.Certificate&method=import&version=1&SynoToken=$token" "" "POST" "multipart/form-data; boundary=${delim}")
|
2019-06-30 03:47:24 +00:00
|
|
|
_debug3 response "$response"
|
|
|
|
|
2020-02-11 04:02:27 +00:00
|
|
|
if ! echo "$response" | grep '"error":' >/dev/null; then
|
|
|
|
if echo "$response" | grep '"restart_httpd":true' >/dev/null; then
|
2019-06-30 06:13:45 +00:00
|
|
|
_info "http services were restarted"
|
2019-06-30 03:47:24 +00:00
|
|
|
else
|
2019-06-30 06:13:45 +00:00
|
|
|
_info "http services were NOT restarted"
|
2019-06-30 03:47:24 +00:00
|
|
|
fi
|
2019-06-30 06:13:45 +00:00
|
|
|
return 0
|
2019-06-30 03:47:24 +00:00
|
|
|
else
|
2020-02-09 00:27:18 +00:00
|
|
|
_err "Unable to update certificate, error code $response"
|
2019-06-30 03:47:24 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|