]> git.proxmox.com Git - mirror_acme.sh.git/blame - deploy/synology_dsm.sh
Use deployconf properly
[mirror_acme.sh.git] / deploy / synology_dsm.sh
CommitLineData
555e0de9
BH
1#!/usr/bin/env sh
2
8e8cda13 3# Here is a script to deploy cert to Synology DSM
555e0de9
BH
4#
5# it requires the jq and curl are in the $PATH and the following
6# environment variables must be set:
7#
8# SYNO_Username - Synology Username to login (must be an administrator)
9# SYNO_Password - Synology Password to login
10# SYNO_Certificate - Certificate description to target for replacement
11#
12# The following environmental variables may be set if you don't like their
13# default values:
14#
15# SYNO_Scheme - defaults to http
16# SYNO_Hostname - defaults to localhost
17# SYNO_Port - defaults to 5000
18#
19#returns 0 means success, otherwise error.
20
21######## Public functions #####################
22
52a168b9 23_syno_get_cookie_data() {
52a168b9
BH
24 grep "\W$1=" "$HTTP_HEADER" | grep "^Set-Cookie:" | _tail_n 1 | _egrep_o "$1=[^;]*;" | tr -d ';'
25}
26
555e0de9
BH
27#domain keyfile certfile cafile fullchain
28synology_dsm_deploy() {
29
30 _cdomain="$1"
31 _ckey="$2"
32 _ccert="$3"
33 _cca="$4"
34
35 _debug _cdomain "$_cdomain"
36
37 # Get Username and Password, but don't save until we successfully authenticate
12593410
BH
38 _getdeployconf SYNO_Username
39 _getdeployconf SYNO_Password
40 _getdeployconf SYNO_Create
555e0de9
BH
41 if [ -z "$SYNO_Username" ] || [ -z "$SYNO_Password" ]; then
42 SYNO_Username=""
43 SYNO_Password=""
44 _err "SYNO_Username & SYNO_Password must be set"
45 return 1
46 fi
47 _debug2 SYNO_Username "$SYNO_Username"
48 _secure_debug2 SYNO_Password "$SYNO_Password"
49
50 # Optional scheme, hostname, and port for Synology DSM
12593410
BH
51 _getdeployconf SYNO_Scheme
52 _getdeployconf SYNO_Hostname
53 _getdeployconf SYNO_Port
555e0de9
BH
54
55 # default vaules for scheme, hostname, and port
56 # defaulting to localhost and http because it's localhost...
57 [ -n "${SYNO_Scheme}" ] || SYNO_Scheme="http"
58 [ -n "${SYNO_Hostname}" ] || SYNO_Hostname="localhost"
59 [ -n "${SYNO_Port}" ] || SYNO_Port="5000"
60
12593410
BH
61 _savedeployconf SYNO_Scheme "$SYNO_Scheme"
62 _savedeployconf SYNO_Hostname "$SYNO_Hostname"
63 _savedeployconf SYNO_Port "$SYNO_Port"
64
555e0de9
BH
65 _debug2 SYNO_Scheme "$SYNO_Scheme"
66 _debug2 SYNO_Hostname "$SYNO_Hostname"
67 _debug2 SYNO_Port "$SYNO_Port"
68
69 # Get the certificate description, but don't save it until we verfiy it's real
70 _getdeployconf SYNO_Certificate
548f83c3 71 # shellcheck disable=SC2154
555e0de9
BH
72 if [ -z "${SYNO_Certificate}" ]; then
73 _err "SYNO_Certificate needs to be defined (with the Certificate description name)"
74 return 1
75 fi
76 _debug SYNO_Certificate "$SYNO_Certificate"
77
555e0de9
BH
78 _base_url="$SYNO_Scheme://$SYNO_Hostname:$SYNO_Port"
79 _debug _base_url "$_base_url"
80
555e0de9 81 # Login, get the token from JSON and session id from cookie
52a168b9
BH
82 _info "Logging into $SYNO_Hostname:$SYNO_Port"
83 response=$(_get "$_base_url/webman/login.cgi?username=$SYNO_Username&passwd=$SYNO_Password&enable_syno_token=yes")
84 token=$(echo "$response" | grep "SynoToken" | sed -n 's/.*"SynoToken" *: *"\([^"]*\).*/\1/p')
85 _debug3 response "$response"
86
87 if [ -z "$token" ]; then
555e0de9
BH
88 _err "Unable to authenticate to $SYNO_Hostname:$SYNO_Port using $SYNO_Scheme."
89 _err "Check your username and password."
555e0de9
BH
90 return 1
91 fi
92
52a168b9
BH
93 _H1="Cookie: $(_syno_get_cookie_data "id"); $(_syno_get_cookie_data "smid")"
94 _H2="X-SYNO-TOKEN: $token"
95 export _H1
96 export _H2
5d3bc95a
BH
97 _debug2 H1 "${_H1}"
98 _debug2 H2 "${_H2}"
52a168b9 99
555e0de9 100 # Now that we know the username and password are good, save them
52a168b9
BH
101 _savedeployconf SYNO_Username "$SYNO_Username"
102 _savedeployconf SYNO_Password "$SYNO_Password"
5d3bc95a 103 _debug token "$token"
555e0de9 104
52a168b9
BH
105 _info "Getting certificates in Synology DSM"
106 response=$(_post "api=SYNO.Core.Certificate.CRT&method=list&version=1" "$_base_url/webapi/entry.cgi")
555e0de9 107 _debug3 response "$response"
95769de4 108 id=$(echo "$response" | sed -n "s/.*\"desc\":\"$SYNO_Certificate\",\"id\":\"\([^\"]*\).*/\1/p")
52a168b9 109 _debug2 id "$id"
555e0de9 110
12593410 111 # shellcheck disable=SC2154
de25232a
BH
112 if [ -z "$id" ] && [ -z "$SYNO_Create" ]; then
113 _err "Unable to find certificate: $SYNO_Certificate and \$SYNO_Create is not set"
555e0de9
BH
114 return 1
115 fi
116
117 # we've verified this certificate description is a thing, so save it
118 _savedeployconf SYNO_Certificate "$SYNO_Certificate"
119
52a168b9 120 default=false
95769de4
BH
121 if echo "$response" | sed -n "s/.*\"desc\":\"$SYNO_Certificate\",\([^{]*\).*/\1/p" | grep -q -- 'is_default":true'; then
122 default=true
52a168b9 123 fi
555e0de9
BH
124 _debug2 default "$default"
125
52a168b9
BH
126 _info "Generate form POST request"
127 nl="\015\012"
128 delim="--------------------------$(date +%Y%m%d%H%M%S)"
129 content="--$delim${nl}Content-Disposition: form-data; name=\"key\"; filename=\"$(basename "$_ckey")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_ckey")\012"
130 content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"cert\"; filename=\"$(basename "$_ccert")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_ccert")\012"
131 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")\012"
132 content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"id\"${nl}${nl}$id"
133 content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"desc\"${nl}${nl}${SYNO_Certificate}"
134 content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"as_default\"${nl}${nl}${default}"
135 content="$content${nl}--$delim--${nl}"
95769de4
BH
136 content="$(printf "%b_" "$content")"
137 content="${content%_}" # protect trailing \n
52a168b9
BH
138
139 _info "Upload certificate to the Synology DSM"
140 response=$(_post "$content" "$_base_url/webapi/entry.cgi?api=SYNO.Core.Certificate&method=import&version=1&SynoToken=$token" "" "POST" "multipart/form-data; boundary=${delim}")
555e0de9 141 _debug3 response "$response"
555e0de9 142
95769de4
BH
143 if ! echo "$response" | grep -q '"error":'; then
144 if echo "$response" | grep -q '"restart_httpd":true'; then
6459ccb1 145 _info "http services were restarted"
555e0de9 146 else
6459ccb1 147 _info "http services were NOT restarted"
555e0de9 148 fi
6459ccb1 149 return 0
555e0de9 150 else
52a168b9 151 _err "Unable to update certificate, error code $response"
555e0de9
BH
152 return 1
153 fi
154}