3 # Script to deploy certificates to remote server by SSH
4 # Note that SSH must be able to login to remote host without a password...
5 # SSH Keys must have been exchanged with the remote host. Validate and
6 # test that you can login to USER@SERVER from the host running acme.sh before
9 # The following variables exported from environment will be used.
10 # If not set then values previously saved in domain.conf file are used.
12 # Only a username is required. All others are optional.
14 # The following examples are for QNAP NAS running QTS 4.2
15 # export DEPLOY_SSH_CMD="" # defaults to "ssh -T"
16 # export DEPLOY_SSH_USER="admin" # required
17 # export DEPLOY_SSH_SERVER="host1 host2:8022 192.168.0.1:9022" # defaults to domain name, support multiple servers with optional port
18 # export DEPLOY_SSH_KEYFILE="/etc/stunnel/stunnel.pem"
19 # export DEPLOY_SSH_CERTFILE="/etc/stunnel/stunnel.pem"
20 # export DEPLOY_SSH_CAFILE="/etc/stunnel/uca.pem"
21 # export DEPLOY_SSH_FULLCHAIN=""
22 # export DEPLOY_SSH_REMOTE_CMD="/etc/init.d/stunnel.sh restart"
23 # export DEPLOY_SSH_BACKUP="" # yes or no, default to yes or previously saved value
24 # export DEPLOY_SSH_BACKUP_PATH=".acme_ssh_deploy" # path on remote system. Defaults to .acme_ssh_deploy
25 # export DEPLOY_SSH_MULTI_CALL="" # yes or no, default to no or previously saved value
26 # export DEPLOY_SSH_USE_SCP="" yes or no, default to no
27 # export DEPLOY_SSH_SCP_CMD="" defaults to "scp -q"
29 ######## Public functions #####################
31 #domain keyfile certfile cafile fullchain
38 _deploy_ssh_servers
=""
40 _debug _cdomain
"$_cdomain"
42 _debug _ccert
"$_ccert"
44 _debug _cfullchain
"$_cfullchain"
46 # USER is required to login by SSH to remote host.
47 _migratedeployconf Le_Deploy_ssh_user DEPLOY_SSH_USER
48 _getdeployconf DEPLOY_SSH_USER
49 _debug2 DEPLOY_SSH_USER
"$DEPLOY_SSH_USER"
50 if [ -z "$DEPLOY_SSH_USER" ]; then
51 _err
"DEPLOY_SSH_USER not defined."
54 _savedeployconf DEPLOY_SSH_USER
"$DEPLOY_SSH_USER"
56 # SERVER is optional. If not provided then use _cdomain
57 _migratedeployconf Le_Deploy_ssh_server DEPLOY_SSH_SERVER
58 _getdeployconf DEPLOY_SSH_SERVER
59 _debug2 DEPLOY_SSH_SERVER
"$DEPLOY_SSH_SERVER"
60 if [ -z "$DEPLOY_SSH_SERVER" ]; then
61 DEPLOY_SSH_SERVER
="$_cdomain"
63 _savedeployconf DEPLOY_SSH_SERVER
"$DEPLOY_SSH_SERVER"
65 # CMD is optional. If not provided then use ssh
66 _migratedeployconf Le_Deploy_ssh_cmd DEPLOY_SSH_CMD
67 _getdeployconf DEPLOY_SSH_CMD
68 _debug2 DEPLOY_SSH_CMD
"$DEPLOY_SSH_CMD"
69 if [ -z "$DEPLOY_SSH_CMD" ]; then
70 DEPLOY_SSH_CMD
="ssh -T"
72 _savedeployconf DEPLOY_SSH_CMD
"$DEPLOY_SSH_CMD"
74 # BACKUP is optional. If not provided then default to previously saved value or yes.
75 _migratedeployconf Le_Deploy_ssh_backup DEPLOY_SSH_BACKUP
76 _getdeployconf DEPLOY_SSH_BACKUP
77 _debug2 DEPLOY_SSH_BACKUP
"$DEPLOY_SSH_BACKUP"
78 if [ -z "$DEPLOY_SSH_BACKUP" ]; then
79 DEPLOY_SSH_BACKUP
="yes"
81 _savedeployconf DEPLOY_SSH_BACKUP
"$DEPLOY_SSH_BACKUP"
83 # BACKUP_PATH is optional. If not provided then default to previously saved value or .acme_ssh_deploy
84 _migratedeployconf Le_Deploy_ssh_backup_path DEPLOY_SSH_BACKUP_PATH
85 _getdeployconf DEPLOY_SSH_BACKUP_PATH
86 _debug2 DEPLOY_SSH_BACKUP_PATH
"$DEPLOY_SSH_BACKUP_PATH"
87 if [ -z "$DEPLOY_SSH_BACKUP_PATH" ]; then
88 DEPLOY_SSH_BACKUP_PATH
=".acme_ssh_deploy"
90 _savedeployconf DEPLOY_SSH_BACKUP_PATH
"$DEPLOY_SSH_BACKUP_PATH"
92 # MULTI_CALL is optional. If not provided then default to previously saved
93 # value (which may be undefined... equivalent to "no").
94 _migratedeployconf Le_Deploy_ssh_multi_call DEPLOY_SSH_MULTI_CALL
95 _getdeployconf DEPLOY_SSH_MULTI_CALL
96 _debug2 DEPLOY_SSH_MULTI_CALL
"$DEPLOY_SSH_MULTI_CALL"
97 if [ -z "$DEPLOY_SSH_MULTI_CALL" ]; then
98 DEPLOY_SSH_MULTI_CALL
="no"
100 _savedeployconf DEPLOY_SSH_MULTI_CALL
"$DEPLOY_SSH_MULTI_CALL"
102 # KEYFILE is optional.
103 # If provided then private key will be copied to provided filename.
104 _migratedeployconf Le_Deploy_ssh_keyfile DEPLOY_SSH_KEYFILE
105 _getdeployconf DEPLOY_SSH_KEYFILE
106 _debug2 DEPLOY_SSH_KEYFILE
"$DEPLOY_SSH_KEYFILE"
107 if [ -n "$DEPLOY_SSH_KEYFILE" ]; then
108 _savedeployconf DEPLOY_SSH_KEYFILE
"$DEPLOY_SSH_KEYFILE"
111 # CERTFILE is optional.
112 # If provided then certificate will be copied or appended to provided filename.
113 _migratedeployconf Le_Deploy_ssh_certfile DEPLOY_SSH_CERTFILE
114 _getdeployconf DEPLOY_SSH_CERTFILE
115 _debug2 DEPLOY_SSH_CERTFILE
"$DEPLOY_SSH_CERTFILE"
116 if [ -n "$DEPLOY_SSH_CERTFILE" ]; then
117 _savedeployconf DEPLOY_SSH_CERTFILE
"$DEPLOY_SSH_CERTFILE"
120 # CAFILE is optional.
121 # If provided then CA intermediate certificate will be copied or appended to provided filename.
122 _migratedeployconf Le_Deploy_ssh_cafile DEPLOY_SSH_CAFILE
123 _getdeployconf DEPLOY_SSH_CAFILE
124 _debug2 DEPLOY_SSH_CAFILE
"$DEPLOY_SSH_CAFILE"
125 if [ -n "$DEPLOY_SSH_CAFILE" ]; then
126 _savedeployconf DEPLOY_SSH_CAFILE
"$DEPLOY_SSH_CAFILE"
129 # FULLCHAIN is optional.
130 # If provided then fullchain certificate will be copied or appended to provided filename.
131 _migratedeployconf Le_Deploy_ssh_fullchain DEPLOY_SSH_FULLCHAIN
132 _getdeployconf DEPLOY_SSH_FULLCHAIN
133 _debug2 DEPLOY_SSH_FULLCHAIN
"$DEPLOY_SSH_FULLCHAIN"
134 if [ -n "$DEPLOY_SSH_FULLCHAIN" ]; then
135 _savedeployconf DEPLOY_SSH_FULLCHAIN
"$DEPLOY_SSH_FULLCHAIN"
138 # REMOTE_CMD is optional.
139 # If provided then this command will be executed on remote host.
140 _migratedeployconf Le_Deploy_ssh_remote_cmd DEPLOY_SSH_REMOTE_CMD
141 _getdeployconf DEPLOY_SSH_REMOTE_CMD
142 _debug2 DEPLOY_SSH_REMOTE_CMD
"$DEPLOY_SSH_REMOTE_CMD"
143 if [ -n "$DEPLOY_SSH_REMOTE_CMD" ]; then
144 _savedeployconf DEPLOY_SSH_REMOTE_CMD
"$DEPLOY_SSH_REMOTE_CMD"
147 # USE_SCP is optional. If not provided then default to previously saved
148 # value (which may be undefined... equivalent to "no").
149 _getdeployconf DEPLOY_SSH_USE_SCP
150 _debug2 DEPLOY_SSH_USE_SCP
"$DEPLOY_SSH_USE_SCP"
151 if [ -z "$DEPLOY_SSH_USE_SCP" ]; then
152 DEPLOY_SSH_USE_SCP
="no"
154 _savedeployconf DEPLOY_SSH_USE_SCP
"$DEPLOY_SSH_USE_SCP"
156 # SCP_CMD is optional. If not provided then use scp
157 _getdeployconf DEPLOY_SSH_SCP_CMD
158 _debug2 DEPLOY_SSH_SCP_CMD
"$DEPLOY_SSH_SCP_CMD"
159 if [ -z "$DEPLOY_SSH_SCP_CMD" ]; then
160 DEPLOY_SSH_SCP_CMD
="scp -q"
162 _savedeployconf DEPLOY_SSH_SCP_CMD
"$DEPLOY_SSH_SCP_CMD"
164 if [ "$DEPLOY_SSH_USE_SCP" = "yes" ]; then
165 DEPLOY_SSH_MULTI_CALL
="yes"
166 _info
"Using scp as alternate method for copying files. Multicall Mode is implicit"
167 elif [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
168 _info
"Using MULTI_CALL mode... Required commands sent in multiple calls to remote host"
170 _info
"Required commands batched and sent in single call to remote host"
173 _deploy_ssh_servers
="$DEPLOY_SSH_SERVER"
174 for DEPLOY_SSH_SERVER
in $_deploy_ssh_servers; do
188 case $DEPLOY_SSH_SERVER in
190 _host
=${DEPLOY_SSH_SERVER%:*}
191 _port
=${DEPLOY_SSH_SERVER##*:}
194 _host
=$DEPLOY_SSH_SERVER
199 _info
"Deploy certificates to remote server $DEPLOY_SSH_USER@$_host:$_port"
201 if [ "$DEPLOY_SSH_BACKUP" = "yes" ]; then
202 _backupprefix
="$DEPLOY_SSH_BACKUP_PATH/$_cdomain-backup"
203 _backupdir
="$_backupprefix-$(_utc_date | tr ' ' '-')"
204 # run cleanup on the backup directory, erase all older
205 # than 180 days (15552000 seconds).
206 _cmdstr
="{ now=\"\$(date -u +%s)\"; for fn in $_backupprefix*; \
207 do if [ -d \"\$fn\" ] && [ \"\$(expr \$now - \$(date -ur \$fn +%s) )\" -ge \"15552000\" ]; \
208 then rm -rf \"\$fn\"; echo \"Backup \$fn deleted as older than 180 days\"; fi; done; }; $_cmdstr"
209 # Alternate version of above... _cmdstr="find $_backupprefix* -type d -mtime +180 2>/dev/null | xargs rm -rf; $_cmdstr"
210 # Create our backup directory for overwritten cert files.
211 _cmdstr
="mkdir -p $_backupdir; $_cmdstr"
212 _info
"Backup of old certificate files will be placed in remote directory $_backupdir"
213 _info
"Backup directories erased after 180 days."
214 if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
215 if ! _ssh_remote_cmd
"$_cmdstr"; then
222 if [ -n "$DEPLOY_SSH_KEYFILE" ]; then
223 if [ "$DEPLOY_SSH_BACKUP" = "yes" ]; then
224 # backup file we are about to overwrite.
225 _cmdstr
="$_cmdstr cp $DEPLOY_SSH_KEYFILE $_backupdir >/dev/null;"
226 if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
227 if ! _ssh_remote_cmd
"$_cmdstr"; then
234 # copy new key into file.
235 if [ "$DEPLOY_SSH_USE_SCP" = "yes" ]; then
237 if ! _scp_remote_cmd
"$_ckey" "$DEPLOY_SSH_KEYFILE"; then
241 # ssh echo to the file
242 _cmdstr
="$_cmdstr echo \"$(cat "$_ckey")\" > $DEPLOY_SSH_KEYFILE;"
243 _info
"will copy private key to remote file $DEPLOY_SSH_KEYFILE"
244 if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
245 if ! _ssh_remote_cmd
"$_cmdstr"; then
253 if [ -n "$DEPLOY_SSH_CERTFILE" ]; then
255 if [ "$DEPLOY_SSH_CERTFILE" = "$DEPLOY_SSH_KEYFILE" ]; then
256 # if filename is same as previous file then append.
258 elif [ "$DEPLOY_SSH_BACKUP" = "yes" ]; then
259 # backup file we are about to overwrite.
260 _cmdstr
="$_cmdstr cp $DEPLOY_SSH_CERTFILE $_backupdir >/dev/null;"
261 if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
262 if ! _ssh_remote_cmd
"$_cmdstr"; then
269 # copy new certificate into file.
270 if [ "$DEPLOY_SSH_USE_SCP" = "yes" ]; then
272 _local_cert_file
=$
(_mktemp
)
273 if [ "$DEPLOY_SSH_CERTFILE" = "$DEPLOY_SSH_KEYFILE" ]; then
274 cat "$_ckey" >>"$_local_cert_file"
276 cat "$_ccert" >>"$_local_cert_file"
277 if ! _scp_remote_cmd
"$_local_cert_file" "$DEPLOY_SSH_CERTFILE"; then
281 # ssh echo to the file
282 _cmdstr
="$_cmdstr echo \"$(cat "$_ccert")\" $_pipe $DEPLOY_SSH_CERTFILE;"
283 _info
"will copy certificate to remote file $DEPLOY_SSH_CERTFILE"
284 if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
285 if ! _ssh_remote_cmd
"$_cmdstr"; then
293 if [ -n "$DEPLOY_SSH_CAFILE" ]; then
295 if [ "$DEPLOY_SSH_CAFILE" = "$DEPLOY_SSH_KEYFILE" ] ||
296 [ "$DEPLOY_SSH_CAFILE" = "$DEPLOY_SSH_CERTFILE" ]; then
297 # if filename is same as previous file then append.
299 elif [ "$DEPLOY_SSH_BACKUP" = "yes" ]; then
300 # backup file we are about to overwrite.
301 _cmdstr
="$_cmdstr cp $DEPLOY_SSH_CAFILE $_backupdir >/dev/null;"
302 if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
303 if ! _ssh_remote_cmd
"$_cmdstr"; then
310 # copy new certificate into file.
311 if [ "$DEPLOY_SSH_USE_SCP" = "yes" ]; then
313 _local_ca_file
=$
(_mktemp
)
314 if [ "$DEPLOY_SSH_CAFILE" = "$DEPLOY_SSH_KEYFILE" ]; then
315 cat "$_ckey" >>"$_local_ca_file"
317 if [ "$DEPLOY_SSH_CAFILE" = "$DEPLOY_SSH_CERTFILE" ]; then
318 cat "$_ccert" >>"$_local_ca_file"
320 cat "$_cca" >>"$_local_ca_file"
321 if ! _scp_remote_cmd
"$_local_ca_file" "$DEPLOY_SSH_CAFILE"; then
325 # ssh echo to the file
326 _cmdstr
="$_cmdstr echo \"$(cat "$_cca")\" $_pipe $DEPLOY_SSH_CAFILE;"
327 _info
"will copy CA file to remote file $DEPLOY_SSH_CAFILE"
328 if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
329 if ! _ssh_remote_cmd
"$_cmdstr"; then
337 if [ -n "$DEPLOY_SSH_FULLCHAIN" ]; then
339 if [ "$DEPLOY_SSH_FULLCHAIN" = "$DEPLOY_SSH_KEYFILE" ] ||
340 [ "$DEPLOY_SSH_FULLCHAIN" = "$DEPLOY_SSH_CERTFILE" ] ||
341 [ "$DEPLOY_SSH_FULLCHAIN" = "$DEPLOY_SSH_CAFILE" ]; then
342 # if filename is same as previous file then append.
344 elif [ "$DEPLOY_SSH_BACKUP" = "yes" ]; then
345 # backup file we are about to overwrite.
346 _cmdstr
="$_cmdstr cp $DEPLOY_SSH_FULLCHAIN $_backupdir >/dev/null;"
347 if [ "$DEPLOY_SSH_FULLCHAIN" = "yes" ]; then
348 if ! _ssh_remote_cmd
"$_cmdstr"; then
355 # copy new certificate into file.
356 if [ "$DEPLOY_SSH_USE_SCP" = "yes" ]; then
358 _local_full_file
=$
(_mktemp
)
359 if [ "$DEPLOY_SSH_FULLCHAIN" = "$DEPLOY_SSH_KEYFILE" ]; then
360 cat "$_ckey" >>"$_local_full_file"
362 if [ "$DEPLOY_SSH_FULLCHAIN" = "$DEPLOY_SSH_CERTFILE" ]; then
363 cat "$_ccert" >>"$_local_full_file"
365 if [ "$DEPLOY_SSH_FULLCHAIN" = "$DEPLOY_SSH_CAFILE" ]; then
366 cat "$_cca" >>"$_local_full_file"
368 cat "$_cfullchain" >>"$_local_full_file"
369 if ! _scp_remote_cmd
"$_local_full_file" "$DEPLOY_SSH_FULLCHAIN"; then
373 # ssh echo to the file
374 _cmdstr
="$_cmdstr echo \"$(cat "$_cfullchain")\" $_pipe $DEPLOY_SSH_FULLCHAIN;"
375 _info
"will copy fullchain to remote file $DEPLOY_SSH_FULLCHAIN"
376 if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
377 if ! _ssh_remote_cmd
"$_cmdstr"; then
385 # cleanup local files if any
386 if [ -f "$_local_cert_file" ]; then
387 rm -f "$_local_cert_file"
389 if [ -f "$_local_ca_file" ]; then
390 rm -f "$_local_ca_file"
392 if [ -f "$_local_full_file" ]; then
393 rm -f "$_local_full_file"
396 if [ -n "$DEPLOY_SSH_REMOTE_CMD" ]; then
397 _cmdstr
="$_cmdstr $DEPLOY_SSH_REMOTE_CMD;"
398 _info
"Will execute remote command $DEPLOY_SSH_REMOTE_CMD"
399 if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
400 if ! _ssh_remote_cmd
"$_cmdstr"; then
407 # if commands not all sent in multiple calls then all commands sent in a single SSH call now...
408 if [ -n "$_cmdstr" ]; then
409 if ! _ssh_remote_cmd
"$_cmdstr"; then
413 # cleanup in case all is ok
421 _ssh_cmd
="$DEPLOY_SSH_CMD"
422 if [ -n "$_port" ]; then
423 _ssh_cmd
="$_ssh_cmd -p $_port"
426 _secure_debug
"Remote commands to execute: $_cmd"
427 _info
"Submitting sequence of commands to remote server by $_ssh_cmd"
429 # quotations in bash cmd below intended. Squash travis spellcheck error
430 # shellcheck disable=SC2029
431 $_ssh_cmd "$DEPLOY_SSH_USER@$_host" sh
-c "'$_cmd'"
434 if [ "$_err_code" != "0" ]; then
435 _err
"Error code $_err_code returned from ssh"
446 _scp_cmd
="$DEPLOY_SSH_SCP_CMD"
447 if [ -n "$_port" ]; then
448 _scp_cmd
="$_scp_cmd -P $_port"
451 _secure_debug
"Remote copy source $_src to destination $_dest"
452 _info
"Submitting secure copy by $_scp_cmd"
454 $_scp_cmd "$_src" "$DEPLOY_SSH_USER"@
"$_host":"$_dest"
457 if [ "$_err_code" != "0" ]; then
458 _err
"Error code $_err_code returned from scp"