2017-02-05 04:17:24 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
2017-02-05 19:35:05 +00:00
|
|
|
# Script to deploy certificates to remote server by SSH
|
|
|
|
# Note that SSH must be able to login to remote host without a password...
|
|
|
|
# SSH Keys must have been exchanged with the remote host. Validate and
|
2017-02-07 02:49:20 +00:00
|
|
|
# test that you can login to USER@SERVER from the host running acme.sh before
|
2017-02-05 19:35:05 +00:00
|
|
|
# using this script.
|
|
|
|
#
|
2017-02-05 04:17:24 +00:00
|
|
|
# The following variables exported from environment will be used.
|
|
|
|
# If not set then values previously saved in domain.conf file are used.
|
|
|
|
#
|
2017-02-05 19:35:05 +00:00
|
|
|
# Only a username is required. All others are optional.
|
|
|
|
#
|
2018-08-02 12:57:27 +00:00
|
|
|
# The following examples are for QNAP NAS running QTS 4.2
|
2020-02-23 01:09:24 +00:00
|
|
|
# export DEPLOY_SSH_CMD="" # defaults to "ssh -T"
|
2017-03-07 16:57:03 +00:00
|
|
|
# export DEPLOY_SSH_USER="admin" # required
|
2022-05-14 14:59:02 +00:00
|
|
|
# export DEPLOY_SSH_SERVER="host1 host2:8022 192.168.0.1:9022" # defaults to domain name, support multiple servers with optional port
|
2017-03-07 16:57:03 +00:00
|
|
|
# export DEPLOY_SSH_KEYFILE="/etc/stunnel/stunnel.pem"
|
|
|
|
# export DEPLOY_SSH_CERTFILE="/etc/stunnel/stunnel.pem"
|
|
|
|
# export DEPLOY_SSH_CAFILE="/etc/stunnel/uca.pem"
|
|
|
|
# export DEPLOY_SSH_FULLCHAIN=""
|
|
|
|
# export DEPLOY_SSH_REMOTE_CMD="/etc/init.d/stunnel.sh restart"
|
2020-03-11 14:58:36 +00:00
|
|
|
# export DEPLOY_SSH_BACKUP="" # yes or no, default to yes or previously saved value
|
2020-03-15 01:51:21 +00:00
|
|
|
# export DEPLOY_SSH_BACKUP_PATH=".acme_ssh_deploy" # path on remote system. Defaults to .acme_ssh_deploy
|
2020-03-11 14:58:36 +00:00
|
|
|
# export DEPLOY_SSH_MULTI_CALL="" # yes or no, default to no or previously saved value
|
2022-05-14 14:13:24 +00:00
|
|
|
# export DEPLOY_SSH_USE_SCP="" yes or no, default to no
|
|
|
|
# export DEPLOY_SSH_SCP_CMD="" defaults to "scp -q"
|
2017-02-11 21:42:44 +00:00
|
|
|
#
|
2017-02-05 04:17:24 +00:00
|
|
|
######## Public functions #####################
|
|
|
|
|
|
|
|
#domain keyfile certfile cafile fullchain
|
2017-02-07 18:05:22 +00:00
|
|
|
ssh_deploy() {
|
2017-02-05 04:17:24 +00:00
|
|
|
_cdomain="$1"
|
|
|
|
_ckey="$2"
|
|
|
|
_ccert="$3"
|
|
|
|
_cca="$4"
|
|
|
|
_cfullchain="$5"
|
2020-05-14 09:15:31 +00:00
|
|
|
_deploy_ssh_servers=""
|
2017-02-05 04:17:24 +00:00
|
|
|
|
|
|
|
_debug _cdomain "$_cdomain"
|
|
|
|
_debug _ckey "$_ckey"
|
|
|
|
_debug _ccert "$_ccert"
|
|
|
|
_debug _cca "$_cca"
|
|
|
|
_debug _cfullchain "$_cfullchain"
|
|
|
|
|
2017-02-05 19:35:05 +00:00
|
|
|
# USER is required to login by SSH to remote host.
|
2022-05-14 14:13:24 +00:00
|
|
|
_migratedeployconf Le_Deploy_ssh_user DEPLOY_SSH_USER
|
2021-09-01 20:36:11 +00:00
|
|
|
_getdeployconf DEPLOY_SSH_USER
|
|
|
|
_debug2 DEPLOY_SSH_USER "$DEPLOY_SSH_USER"
|
2017-03-07 16:57:03 +00:00
|
|
|
if [ -z "$DEPLOY_SSH_USER" ]; then
|
2022-05-14 14:13:24 +00:00
|
|
|
_err "DEPLOY_SSH_USER not defined."
|
|
|
|
return 1
|
2017-02-05 19:35:05 +00:00
|
|
|
fi
|
2022-05-14 14:13:24 +00:00
|
|
|
_savedeployconf DEPLOY_SSH_USER "$DEPLOY_SSH_USER"
|
2017-02-05 19:35:05 +00:00
|
|
|
|
2017-02-07 02:49:20 +00:00
|
|
|
# SERVER is optional. If not provided then use _cdomain
|
2022-05-14 14:13:24 +00:00
|
|
|
_migratedeployconf Le_Deploy_ssh_server DEPLOY_SSH_SERVER
|
2021-09-01 20:36:11 +00:00
|
|
|
_getdeployconf DEPLOY_SSH_SERVER
|
|
|
|
_debug2 DEPLOY_SSH_SERVER "$DEPLOY_SSH_SERVER"
|
2022-05-14 14:13:24 +00:00
|
|
|
if [ -z "$DEPLOY_SSH_SERVER" ]; then
|
|
|
|
DEPLOY_SSH_SERVER="$_cdomain"
|
2017-02-05 04:17:24 +00:00
|
|
|
fi
|
2022-05-14 14:13:24 +00:00
|
|
|
_savedeployconf DEPLOY_SSH_SERVER "$DEPLOY_SSH_SERVER"
|
2017-02-05 20:02:59 +00:00
|
|
|
|
2017-02-11 21:11:27 +00:00
|
|
|
# CMD is optional. If not provided then use ssh
|
2022-05-14 14:13:24 +00:00
|
|
|
_migratedeployconf Le_Deploy_ssh_cmd DEPLOY_SSH_CMD
|
2021-09-01 20:36:11 +00:00
|
|
|
_getdeployconf DEPLOY_SSH_CMD
|
|
|
|
_debug2 DEPLOY_SSH_CMD "$DEPLOY_SSH_CMD"
|
2022-05-14 14:13:24 +00:00
|
|
|
if [ -z "$DEPLOY_SSH_CMD" ]; then
|
|
|
|
DEPLOY_SSH_CMD="ssh -T"
|
2021-08-25 14:55:36 +00:00
|
|
|
fi
|
2022-05-14 14:13:24 +00:00
|
|
|
_savedeployconf DEPLOY_SSH_CMD "$DEPLOY_SSH_CMD"
|
2021-08-25 14:55:36 +00:00
|
|
|
|
2020-03-11 14:58:36 +00:00
|
|
|
# BACKUP is optional. If not provided then default to previously saved value or yes.
|
2022-05-14 14:13:24 +00:00
|
|
|
_migratedeployconf Le_Deploy_ssh_backup DEPLOY_SSH_BACKUP
|
2021-09-01 20:36:11 +00:00
|
|
|
_getdeployconf DEPLOY_SSH_BACKUP
|
|
|
|
_debug2 DEPLOY_SSH_BACKUP "$DEPLOY_SSH_BACKUP"
|
2022-05-14 14:13:24 +00:00
|
|
|
if [ -z "$DEPLOY_SSH_BACKUP" ]; then
|
|
|
|
DEPLOY_SSH_BACKUP="yes"
|
2017-02-11 21:42:44 +00:00
|
|
|
fi
|
2022-05-14 14:13:24 +00:00
|
|
|
_savedeployconf DEPLOY_SSH_BACKUP "$DEPLOY_SSH_BACKUP"
|
2017-02-11 21:42:44 +00:00
|
|
|
|
2020-03-15 01:51:21 +00:00
|
|
|
# BACKUP_PATH is optional. If not provided then default to previously saved value or .acme_ssh_deploy
|
2022-05-14 14:13:24 +00:00
|
|
|
_migratedeployconf Le_Deploy_ssh_backup_path DEPLOY_SSH_BACKUP_PATH
|
2021-09-01 20:36:11 +00:00
|
|
|
_getdeployconf DEPLOY_SSH_BACKUP_PATH
|
|
|
|
_debug2 DEPLOY_SSH_BACKUP_PATH "$DEPLOY_SSH_BACKUP_PATH"
|
2022-05-14 14:13:24 +00:00
|
|
|
if [ -z "$DEPLOY_SSH_BACKUP_PATH" ]; then
|
|
|
|
DEPLOY_SSH_BACKUP_PATH=".acme_ssh_deploy"
|
2020-03-15 01:51:21 +00:00
|
|
|
fi
|
2022-05-14 14:13:24 +00:00
|
|
|
_savedeployconf DEPLOY_SSH_BACKUP_PATH "$DEPLOY_SSH_BACKUP_PATH"
|
2020-03-15 01:51:21 +00:00
|
|
|
|
2020-03-11 14:58:36 +00:00
|
|
|
# MULTI_CALL is optional. If not provided then default to previously saved
|
|
|
|
# value (which may be undefined... equivalent to "no").
|
2022-05-14 14:13:24 +00:00
|
|
|
_migratedeployconf Le_Deploy_ssh_multi_call DEPLOY_SSH_MULTI_CALL
|
2021-09-01 20:36:11 +00:00
|
|
|
_getdeployconf DEPLOY_SSH_MULTI_CALL
|
|
|
|
_debug2 DEPLOY_SSH_MULTI_CALL "$DEPLOY_SSH_MULTI_CALL"
|
2022-05-14 14:13:24 +00:00
|
|
|
if [ -z "$DEPLOY_SSH_MULTI_CALL" ]; then
|
|
|
|
DEPLOY_SSH_MULTI_CALL="no"
|
|
|
|
fi
|
|
|
|
_savedeployconf DEPLOY_SSH_MULTI_CALL "$DEPLOY_SSH_MULTI_CALL"
|
|
|
|
|
|
|
|
# KEYFILE is optional.
|
|
|
|
# If provided then private key will be copied to provided filename.
|
|
|
|
_migratedeployconf Le_Deploy_ssh_keyfile DEPLOY_SSH_KEYFILE
|
|
|
|
_getdeployconf DEPLOY_SSH_KEYFILE
|
|
|
|
_debug2 DEPLOY_SSH_KEYFILE "$DEPLOY_SSH_KEYFILE"
|
|
|
|
if [ -n "$DEPLOY_SSH_KEYFILE" ]; then
|
|
|
|
_savedeployconf DEPLOY_SSH_KEYFILE "$DEPLOY_SSH_KEYFILE"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# CERTFILE is optional.
|
|
|
|
# If provided then certificate will be copied or appended to provided filename.
|
|
|
|
_migratedeployconf Le_Deploy_ssh_certfile DEPLOY_SSH_CERTFILE
|
|
|
|
_getdeployconf DEPLOY_SSH_CERTFILE
|
|
|
|
_debug2 DEPLOY_SSH_CERTFILE "$DEPLOY_SSH_CERTFILE"
|
|
|
|
if [ -n "$DEPLOY_SSH_CERTFILE" ]; then
|
|
|
|
_savedeployconf DEPLOY_SSH_CERTFILE "$DEPLOY_SSH_CERTFILE"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# CAFILE is optional.
|
|
|
|
# If provided then CA intermediate certificate will be copied or appended to provided filename.
|
|
|
|
_migratedeployconf Le_Deploy_ssh_cafile DEPLOY_SSH_CAFILE
|
|
|
|
_getdeployconf DEPLOY_SSH_CAFILE
|
|
|
|
_debug2 DEPLOY_SSH_CAFILE "$DEPLOY_SSH_CAFILE"
|
|
|
|
if [ -n "$DEPLOY_SSH_CAFILE" ]; then
|
|
|
|
_savedeployconf DEPLOY_SSH_CAFILE "$DEPLOY_SSH_CAFILE"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# FULLCHAIN is optional.
|
|
|
|
# If provided then fullchain certificate will be copied or appended to provided filename.
|
|
|
|
_migratedeployconf Le_Deploy_ssh_fullchain DEPLOY_SSH_FULLCHAIN
|
|
|
|
_getdeployconf DEPLOY_SSH_FULLCHAIN
|
|
|
|
_debug2 DEPLOY_SSH_FULLCHAIN "$DEPLOY_SSH_FULLCHAIN"
|
|
|
|
if [ -n "$DEPLOY_SSH_FULLCHAIN" ]; then
|
|
|
|
_savedeployconf DEPLOY_SSH_FULLCHAIN "$DEPLOY_SSH_FULLCHAIN"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# REMOTE_CMD is optional.
|
|
|
|
# If provided then this command will be executed on remote host.
|
|
|
|
_migratedeployconf Le_Deploy_ssh_remote_cmd DEPLOY_SSH_REMOTE_CMD
|
|
|
|
_getdeployconf DEPLOY_SSH_REMOTE_CMD
|
|
|
|
_debug2 DEPLOY_SSH_REMOTE_CMD "$DEPLOY_SSH_REMOTE_CMD"
|
|
|
|
if [ -n "$DEPLOY_SSH_REMOTE_CMD" ]; then
|
|
|
|
_savedeployconf DEPLOY_SSH_REMOTE_CMD "$DEPLOY_SSH_REMOTE_CMD"
|
2020-02-23 02:10:42 +00:00
|
|
|
fi
|
2020-02-23 03:09:28 +00:00
|
|
|
|
2021-08-25 14:55:36 +00:00
|
|
|
# USE_SCP is optional. If not provided then default to previously saved
|
|
|
|
# value (which may be undefined... equivalent to "no").
|
2022-05-14 14:13:24 +00:00
|
|
|
_getdeployconf DEPLOY_SSH_USE_SCP
|
|
|
|
_debug2 DEPLOY_SSH_USE_SCP "$DEPLOY_SSH_USE_SCP"
|
|
|
|
if [ -z "$DEPLOY_SSH_USE_SCP" ]; then
|
|
|
|
DEPLOY_SSH_USE_SCP="no"
|
2021-08-25 14:55:36 +00:00
|
|
|
fi
|
2022-05-14 14:13:24 +00:00
|
|
|
_savedeployconf DEPLOY_SSH_USE_SCP "$DEPLOY_SSH_USE_SCP"
|
2021-08-25 14:55:36 +00:00
|
|
|
|
|
|
|
# SCP_CMD is optional. If not provided then use scp
|
2022-05-14 14:13:24 +00:00
|
|
|
_getdeployconf DEPLOY_SSH_SCP_CMD
|
|
|
|
_debug2 DEPLOY_SSH_SCP_CMD "$DEPLOY_SSH_SCP_CMD"
|
|
|
|
if [ -z "$DEPLOY_SSH_SCP_CMD" ]; then
|
|
|
|
DEPLOY_SSH_SCP_CMD="scp -q"
|
|
|
|
fi
|
|
|
|
_savedeployconf DEPLOY_SSH_SCP_CMD "$DEPLOY_SSH_SCP_CMD"
|
|
|
|
|
|
|
|
if [ "$DEPLOY_SSH_USE_SCP" = "yes" ]; then
|
|
|
|
DEPLOY_SSH_MULTI_CALL="yes"
|
|
|
|
_info "Using scp as alternate method for copying files. Multicall Mode is implicit"
|
|
|
|
elif [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
|
|
|
|
_info "Using MULTI_CALL mode... Required commands sent in multiple calls to remote host"
|
|
|
|
else
|
|
|
|
_info "Required commands batched and sent in single call to remote host"
|
2021-08-25 14:55:36 +00:00
|
|
|
fi
|
|
|
|
|
2022-05-24 14:25:44 +00:00
|
|
|
_deploy_ssh_servers="$DEPLOY_SSH_SERVER"
|
2022-05-14 14:13:24 +00:00
|
|
|
for DEPLOY_SSH_SERVER in $_deploy_ssh_servers; do
|
2020-05-14 09:15:31 +00:00
|
|
|
_ssh_deploy
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
_ssh_deploy() {
|
|
|
|
_err_code=0
|
|
|
|
_cmdstr=""
|
|
|
|
_backupprefix=""
|
|
|
|
_backupdir=""
|
2021-08-25 14:55:36 +00:00
|
|
|
_local_cert_file=""
|
|
|
|
_local_ca_file=""
|
|
|
|
_local_full_file=""
|
2020-05-14 09:15:31 +00:00
|
|
|
|
2022-05-14 14:29:19 +00:00
|
|
|
case $DEPLOY_SSH_SERVER in
|
2022-05-14 14:49:40 +00:00
|
|
|
*:*)
|
|
|
|
_host=${DEPLOY_SSH_SERVER%:*}
|
|
|
|
_port=${DEPLOY_SSH_SERVER##*:}
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
_host=$DEPLOY_SSH_SERVER
|
|
|
|
_port=
|
|
|
|
;;
|
2022-05-14 14:29:19 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
_info "Deploy certificates to remote server $DEPLOY_SSH_USER@$_host:$_port"
|
2017-02-05 04:17:24 +00:00
|
|
|
|
2022-05-14 14:13:24 +00:00
|
|
|
if [ "$DEPLOY_SSH_BACKUP" = "yes" ]; then
|
|
|
|
_backupprefix="$DEPLOY_SSH_BACKUP_PATH/$_cdomain-backup"
|
2020-03-15 01:51:21 +00:00
|
|
|
_backupdir="$_backupprefix-$(_utc_date | tr ' ' '-')"
|
2020-02-23 01:43:28 +00:00
|
|
|
# run cleanup on the backup directory, erase all older
|
|
|
|
# than 180 days (15552000 seconds).
|
|
|
|
_cmdstr="{ now=\"\$(date -u +%s)\"; for fn in $_backupprefix*; \
|
|
|
|
do if [ -d \"\$fn\" ] && [ \"\$(expr \$now - \$(date -ur \$fn +%s) )\" -ge \"15552000\" ]; \
|
|
|
|
then rm -rf \"\$fn\"; echo \"Backup \$fn deleted as older than 180 days\"; fi; done; }; $_cmdstr"
|
|
|
|
# Alternate version of above... _cmdstr="find $_backupprefix* -type d -mtime +180 2>/dev/null | xargs rm -rf; $_cmdstr"
|
|
|
|
# Create our backup directory for overwritten cert files.
|
|
|
|
_cmdstr="mkdir -p $_backupdir; $_cmdstr"
|
|
|
|
_info "Backup of old certificate files will be placed in remote directory $_backupdir"
|
|
|
|
_info "Backup directories erased after 180 days."
|
2022-05-14 14:13:24 +00:00
|
|
|
if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
|
2020-02-23 02:10:42 +00:00
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2020-02-23 01:43:28 +00:00
|
|
|
fi
|
|
|
|
|
2017-03-07 16:57:03 +00:00
|
|
|
if [ -n "$DEPLOY_SSH_KEYFILE" ]; then
|
2022-05-14 14:13:24 +00:00
|
|
|
if [ "$DEPLOY_SSH_BACKUP" = "yes" ]; then
|
2017-02-11 21:42:44 +00:00
|
|
|
# backup file we are about to overwrite.
|
2022-05-14 14:13:24 +00:00
|
|
|
_cmdstr="$_cmdstr cp $DEPLOY_SSH_KEYFILE $_backupdir >/dev/null;"
|
|
|
|
if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
|
2021-08-25 14:55:36 +00:00
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2017-02-11 21:42:44 +00:00
|
|
|
fi
|
2021-08-25 14:55:36 +00:00
|
|
|
|
|
|
|
# copy new key into file.
|
2022-05-14 14:13:24 +00:00
|
|
|
if [ "$DEPLOY_SSH_USE_SCP" = "yes" ]; then
|
2021-08-25 14:55:36 +00:00
|
|
|
# scp the file
|
2022-05-14 14:13:24 +00:00
|
|
|
if ! _scp_remote_cmd "$_ckey" "$DEPLOY_SSH_KEYFILE"; then
|
2021-08-30 09:32:07 +00:00
|
|
|
return $_err_code
|
2021-08-25 14:55:36 +00:00
|
|
|
fi
|
|
|
|
else
|
2022-05-14 14:13:24 +00:00
|
|
|
# ssh echo to the file
|
|
|
|
_cmdstr="$_cmdstr echo \"$(cat "$_ckey")\" > $DEPLOY_SSH_KEYFILE;"
|
|
|
|
_info "will copy private key to remote file $DEPLOY_SSH_KEYFILE"
|
|
|
|
if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
|
2021-08-25 14:55:36 +00:00
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
2020-02-23 02:10:42 +00:00
|
|
|
fi
|
|
|
|
fi
|
2017-02-05 04:17:24 +00:00
|
|
|
fi
|
|
|
|
|
2017-03-07 16:57:03 +00:00
|
|
|
if [ -n "$DEPLOY_SSH_CERTFILE" ]; then
|
2017-02-11 21:42:44 +00:00
|
|
|
_pipe=">"
|
2022-05-14 14:13:24 +00:00
|
|
|
if [ "$DEPLOY_SSH_CERTFILE" = "$DEPLOY_SSH_KEYFILE" ]; then
|
2017-02-11 21:11:27 +00:00
|
|
|
# if filename is same as previous file then append.
|
|
|
|
_pipe=">>"
|
2022-05-14 14:13:24 +00:00
|
|
|
elif [ "$DEPLOY_SSH_BACKUP" = "yes" ]; then
|
2017-02-05 19:35:05 +00:00
|
|
|
# backup file we are about to overwrite.
|
2022-05-14 14:13:24 +00:00
|
|
|
_cmdstr="$_cmdstr cp $DEPLOY_SSH_CERTFILE $_backupdir >/dev/null;"
|
|
|
|
if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
|
2021-08-25 14:55:36 +00:00
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2017-02-05 04:17:24 +00:00
|
|
|
fi
|
2021-08-25 14:55:36 +00:00
|
|
|
|
2022-05-14 14:13:24 +00:00
|
|
|
# copy new certificate into file.
|
|
|
|
if [ "$DEPLOY_SSH_USE_SCP" = "yes" ]; then
|
|
|
|
# scp the file
|
|
|
|
_local_cert_file=$(_mktemp)
|
|
|
|
if [ "$DEPLOY_SSH_CERTFILE" = "$DEPLOY_SSH_KEYFILE" ]; then
|
2022-05-14 14:41:59 +00:00
|
|
|
cat "$_ckey" >>"$_local_cert_file"
|
2022-05-14 14:13:24 +00:00
|
|
|
fi
|
2022-05-14 14:41:59 +00:00
|
|
|
cat "$_ccert" >>"$_local_cert_file"
|
2022-05-14 14:13:24 +00:00
|
|
|
if ! _scp_remote_cmd "$_local_cert_file" "$DEPLOY_SSH_CERTFILE"; then
|
|
|
|
return $_err_code
|
2021-08-25 14:55:36 +00:00
|
|
|
fi
|
|
|
|
else
|
2022-05-14 14:13:24 +00:00
|
|
|
# ssh echo to the file
|
|
|
|
_cmdstr="$_cmdstr echo \"$(cat "$_ccert")\" $_pipe $DEPLOY_SSH_CERTFILE;"
|
|
|
|
_info "will copy certificate to remote file $DEPLOY_SSH_CERTFILE"
|
|
|
|
if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
|
2021-08-25 14:55:36 +00:00
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
2020-02-23 02:10:42 +00:00
|
|
|
fi
|
|
|
|
fi
|
2017-02-05 04:17:24 +00:00
|
|
|
fi
|
|
|
|
|
2017-03-07 16:57:03 +00:00
|
|
|
if [ -n "$DEPLOY_SSH_CAFILE" ]; then
|
2017-02-11 21:42:44 +00:00
|
|
|
_pipe=">"
|
2022-05-14 14:13:24 +00:00
|
|
|
if [ "$DEPLOY_SSH_CAFILE" = "$DEPLOY_SSH_KEYFILE" ] ||
|
|
|
|
[ "$DEPLOY_SSH_CAFILE" = "$DEPLOY_SSH_CERTFILE" ]; then
|
2017-02-11 21:11:27 +00:00
|
|
|
# if filename is same as previous file then append.
|
|
|
|
_pipe=">>"
|
2022-05-14 14:13:24 +00:00
|
|
|
elif [ "$DEPLOY_SSH_BACKUP" = "yes" ]; then
|
2017-02-11 21:11:27 +00:00
|
|
|
# backup file we are about to overwrite.
|
2022-05-14 14:13:24 +00:00
|
|
|
_cmdstr="$_cmdstr cp $DEPLOY_SSH_CAFILE $_backupdir >/dev/null;"
|
|
|
|
if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
|
2021-08-25 14:55:36 +00:00
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2017-02-11 21:11:27 +00:00
|
|
|
fi
|
2021-08-25 14:55:36 +00:00
|
|
|
|
2022-05-14 14:13:24 +00:00
|
|
|
# copy new certificate into file.
|
|
|
|
if [ "$DEPLOY_SSH_USE_SCP" = "yes" ]; then
|
|
|
|
# scp the file
|
|
|
|
_local_ca_file=$(_mktemp)
|
|
|
|
if [ "$DEPLOY_SSH_CAFILE" = "$DEPLOY_SSH_KEYFILE" ]; then
|
2022-05-14 14:41:59 +00:00
|
|
|
cat "$_ckey" >>"$_local_ca_file"
|
2022-05-14 14:13:24 +00:00
|
|
|
fi
|
|
|
|
if [ "$DEPLOY_SSH_CAFILE" = "$DEPLOY_SSH_CERTFILE" ]; then
|
2022-05-14 14:41:59 +00:00
|
|
|
cat "$_ccert" >>"$_local_ca_file"
|
2022-05-14 14:13:24 +00:00
|
|
|
fi
|
|
|
|
cat "$_cca" >>"$_local_ca_file"
|
|
|
|
if ! _scp_remote_cmd "$_local_ca_file" "$DEPLOY_SSH_CAFILE"; then
|
|
|
|
return $_err_code
|
2021-08-25 14:55:36 +00:00
|
|
|
fi
|
|
|
|
else
|
2022-05-14 14:13:24 +00:00
|
|
|
# ssh echo to the file
|
|
|
|
_cmdstr="$_cmdstr echo \"$(cat "$_cca")\" $_pipe $DEPLOY_SSH_CAFILE;"
|
|
|
|
_info "will copy CA file to remote file $DEPLOY_SSH_CAFILE"
|
|
|
|
if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
|
2021-08-25 14:55:36 +00:00
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
2020-02-23 02:10:42 +00:00
|
|
|
fi
|
|
|
|
fi
|
2017-02-05 04:17:24 +00:00
|
|
|
fi
|
|
|
|
|
2017-03-07 16:57:03 +00:00
|
|
|
if [ -n "$DEPLOY_SSH_FULLCHAIN" ]; then
|
2017-02-11 21:42:44 +00:00
|
|
|
_pipe=">"
|
2022-05-14 14:13:24 +00:00
|
|
|
if [ "$DEPLOY_SSH_FULLCHAIN" = "$DEPLOY_SSH_KEYFILE" ] ||
|
|
|
|
[ "$DEPLOY_SSH_FULLCHAIN" = "$DEPLOY_SSH_CERTFILE" ] ||
|
|
|
|
[ "$DEPLOY_SSH_FULLCHAIN" = "$DEPLOY_SSH_CAFILE" ]; then
|
2017-02-11 21:11:27 +00:00
|
|
|
# if filename is same as previous file then append.
|
|
|
|
_pipe=">>"
|
2022-05-14 14:13:24 +00:00
|
|
|
elif [ "$DEPLOY_SSH_BACKUP" = "yes" ]; then
|
2017-02-11 21:11:27 +00:00
|
|
|
# backup file we are about to overwrite.
|
2022-05-14 14:13:24 +00:00
|
|
|
_cmdstr="$_cmdstr cp $DEPLOY_SSH_FULLCHAIN $_backupdir >/dev/null;"
|
|
|
|
if [ "$DEPLOY_SSH_FULLCHAIN" = "yes" ]; then
|
2021-08-25 14:55:36 +00:00
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2017-02-11 21:11:27 +00:00
|
|
|
fi
|
2021-08-25 14:55:36 +00:00
|
|
|
|
2022-05-14 14:13:24 +00:00
|
|
|
# copy new certificate into file.
|
|
|
|
if [ "$DEPLOY_SSH_USE_SCP" = "yes" ]; then
|
|
|
|
# scp the file
|
|
|
|
_local_full_file=$(_mktemp)
|
|
|
|
if [ "$DEPLOY_SSH_FULLCHAIN" = "$DEPLOY_SSH_KEYFILE" ]; then
|
2022-05-14 14:41:59 +00:00
|
|
|
cat "$_ckey" >>"$_local_full_file"
|
2022-05-14 14:13:24 +00:00
|
|
|
fi
|
|
|
|
if [ "$DEPLOY_SSH_FULLCHAIN" = "$DEPLOY_SSH_CERTFILE" ]; then
|
2022-05-14 14:41:59 +00:00
|
|
|
cat "$_ccert" >>"$_local_full_file"
|
2022-05-14 14:13:24 +00:00
|
|
|
fi
|
|
|
|
if [ "$DEPLOY_SSH_FULLCHAIN" = "$DEPLOY_SSH_CAFILE" ]; then
|
2022-05-14 14:41:59 +00:00
|
|
|
cat "$_cca" >>"$_local_full_file"
|
2022-05-14 14:13:24 +00:00
|
|
|
fi
|
2022-05-14 14:41:59 +00:00
|
|
|
cat "$_cfullchain" >>"$_local_full_file"
|
2022-05-14 14:13:24 +00:00
|
|
|
if ! _scp_remote_cmd "$_local_full_file" "$DEPLOY_SSH_FULLCHAIN"; then
|
|
|
|
return $_err_code
|
2021-08-25 14:55:36 +00:00
|
|
|
fi
|
|
|
|
else
|
2022-05-14 14:13:24 +00:00
|
|
|
# ssh echo to the file
|
|
|
|
_cmdstr="$_cmdstr echo \"$(cat "$_cfullchain")\" $_pipe $DEPLOY_SSH_FULLCHAIN;"
|
|
|
|
_info "will copy fullchain to remote file $DEPLOY_SSH_FULLCHAIN"
|
|
|
|
if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
|
2021-08-25 14:55:36 +00:00
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
2020-02-23 02:10:42 +00:00
|
|
|
fi
|
|
|
|
fi
|
2017-02-05 04:17:24 +00:00
|
|
|
fi
|
2021-08-25 14:55:36 +00:00
|
|
|
|
2022-05-14 14:13:24 +00:00
|
|
|
# cleanup local files if any
|
|
|
|
if [ -f "$_local_cert_file" ]; then
|
|
|
|
rm -f "$_local_cert_file"
|
2021-08-25 14:55:36 +00:00
|
|
|
fi
|
2022-05-14 14:13:24 +00:00
|
|
|
if [ -f "$_local_ca_file" ]; then
|
|
|
|
rm -f "$_local_ca_file"
|
2021-08-25 14:55:36 +00:00
|
|
|
fi
|
2022-05-14 14:13:24 +00:00
|
|
|
if [ -f "$_local_full_file" ]; then
|
|
|
|
rm -f "$_local_full_file"
|
2021-08-25 14:55:36 +00:00
|
|
|
fi
|
|
|
|
|
2017-03-07 16:57:03 +00:00
|
|
|
if [ -n "$DEPLOY_SSH_REMOTE_CMD" ]; then
|
2022-05-14 14:13:24 +00:00
|
|
|
_cmdstr="$_cmdstr $DEPLOY_SSH_REMOTE_CMD;"
|
|
|
|
_info "Will execute remote command $DEPLOY_SSH_REMOTE_CMD"
|
|
|
|
if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
|
2020-02-23 02:10:42 +00:00
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2017-02-05 04:17:24 +00:00
|
|
|
fi
|
|
|
|
|
2020-03-03 18:40:33 +00:00
|
|
|
# if commands not all sent in multiple calls then all commands sent in a single SSH call now...
|
2020-02-23 02:10:42 +00:00
|
|
|
if [ -n "$_cmdstr" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
2020-02-23 01:31:52 +00:00
|
|
|
fi
|
2021-08-25 14:55:36 +00:00
|
|
|
# cleanup in case all is ok
|
2020-02-23 01:31:52 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
#cmd
|
|
|
|
_ssh_remote_cmd() {
|
|
|
|
_cmd="$1"
|
2022-05-14 14:29:19 +00:00
|
|
|
|
|
|
|
_ssh_cmd="$DEPLOY_SSH_CMD"
|
|
|
|
if [ -n "$_port" ]; then
|
|
|
|
_ssh_cmd="$_ssh_cmd -p $_port"
|
|
|
|
fi
|
|
|
|
|
2020-02-23 01:31:52 +00:00
|
|
|
_secure_debug "Remote commands to execute: $_cmd"
|
2022-05-14 14:29:19 +00:00
|
|
|
_info "Submitting sequence of commands to remote server by $_ssh_cmd"
|
2022-05-14 14:13:24 +00:00
|
|
|
|
2017-02-05 20:26:55 +00:00
|
|
|
# quotations in bash cmd below intended. Squash travis spellcheck error
|
|
|
|
# shellcheck disable=SC2029
|
2022-05-14 14:29:19 +00:00
|
|
|
$_ssh_cmd "$DEPLOY_SSH_USER@$_host" sh -c "'$_cmd'"
|
2020-02-23 01:31:52 +00:00
|
|
|
_err_code="$?"
|
2017-02-11 21:11:27 +00:00
|
|
|
|
2020-02-23 01:31:52 +00:00
|
|
|
if [ "$_err_code" != "0" ]; then
|
2020-02-23 03:05:06 +00:00
|
|
|
_err "Error code $_err_code returned from ssh"
|
2017-02-11 21:11:27 +00:00
|
|
|
fi
|
2017-02-05 04:17:24 +00:00
|
|
|
|
2020-02-23 01:31:52 +00:00
|
|
|
return $_err_code
|
2017-02-05 04:17:24 +00:00
|
|
|
}
|
2021-08-25 14:55:36 +00:00
|
|
|
|
|
|
|
# cmd scp
|
|
|
|
_scp_remote_cmd() {
|
2022-05-14 14:13:24 +00:00
|
|
|
_src=$1
|
|
|
|
_dest=$2
|
|
|
|
|
2022-05-14 14:29:19 +00:00
|
|
|
_scp_cmd="$DEPLOY_SSH_SCP_CMD"
|
|
|
|
if [ -n "$_port" ]; then
|
|
|
|
_scp_cmd="$_scp_cmd -P $_port"
|
|
|
|
fi
|
|
|
|
|
|
|
|
_secure_debug "Remote copy source $_src to destination $_dest"
|
|
|
|
_info "Submitting secure copy by $_scp_cmd"
|
|
|
|
|
|
|
|
$_scp_cmd "$_src" "$DEPLOY_SSH_USER"@"$_host":"$_dest"
|
2021-08-25 14:55:36 +00:00
|
|
|
_err_code="$?"
|
|
|
|
|
|
|
|
if [ "$_err_code" != "0" ]; then
|
|
|
|
_err "Error code $_err_code returned from scp"
|
|
|
|
fi
|
|
|
|
|
|
|
|
return $_err_code
|
|
|
|
}
|