2017-02-05 05:17:24 +01:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
2017-02-05 20:35:05 +01: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 03:49:20 +01:00
|
|
|
# test that you can login to USER@SERVER from the host running acme.sh before
|
2017-02-05 20:35:05 +01:00
|
|
|
# using this script.
|
|
|
|
#
|
2017-02-05 05:17:24 +01: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 20:35:05 +01:00
|
|
|
# Only a username is required. All others are optional.
|
|
|
|
#
|
2018-08-02 14:57:27 +02:00
|
|
|
# The following examples are for QNAP NAS running QTS 4.2
|
2020-02-23 02:09:24 +01:00
|
|
|
# export DEPLOY_SSH_CMD="" # defaults to "ssh -T"
|
2017-03-07 17:57:03 +01:00
|
|
|
# export DEPLOY_SSH_USER="admin" # required
|
|
|
|
# export DEPLOY_SSH_SERVER="qnap" # defaults to domain name
|
|
|
|
# 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 15:58:36 +01:00
|
|
|
# export DEPLOY_SSH_BACKUP="" # yes or no, default to yes or previously saved value
|
2020-03-15 02:51:21 +01:00
|
|
|
# export DEPLOY_SSH_BACKUP_PATH=".acme_ssh_deploy" # path on remote system. Defaults to .acme_ssh_deploy
|
2020-03-11 15:58:36 +01:00
|
|
|
# export DEPLOY_SSH_MULTI_CALL="" # yes or no, default to no or previously saved value
|
2021-08-25 16:55:36 +02:00
|
|
|
# export DEPLOY_SSH_USE_SCP="" yes or no , default to no
|
|
|
|
# export DEPLOY_SSH_SCP_CMD="" defaults to "scp -T -q "
|
2017-02-11 22:42:44 +01:00
|
|
|
#
|
2017-02-05 05:17:24 +01:00
|
|
|
######## Public functions #####################
|
|
|
|
|
|
|
|
#domain keyfile certfile cafile fullchain
|
2017-02-07 19:05:22 +01:00
|
|
|
ssh_deploy() {
|
2017-02-05 05:17:24 +01:00
|
|
|
_cdomain="$1"
|
|
|
|
_ckey="$2"
|
|
|
|
_ccert="$3"
|
|
|
|
_cca="$4"
|
|
|
|
_cfullchain="$5"
|
2020-05-14 11:15:31 +02:00
|
|
|
_deploy_ssh_servers=""
|
2017-02-05 05:17:24 +01:00
|
|
|
|
2017-02-05 21:16:05 +01:00
|
|
|
if [ -f "$DOMAIN_CONF" ]; then
|
2017-02-05 21:20:06 +01:00
|
|
|
# shellcheck disable=SC1090
|
2017-02-05 21:16:05 +01:00
|
|
|
. "$DOMAIN_CONF"
|
2017-02-05 21:12:23 +01:00
|
|
|
fi
|
|
|
|
|
2017-02-05 05:17:24 +01:00
|
|
|
_debug _cdomain "$_cdomain"
|
|
|
|
_debug _ckey "$_ckey"
|
|
|
|
_debug _ccert "$_ccert"
|
|
|
|
_debug _cca "$_cca"
|
|
|
|
_debug _cfullchain "$_cfullchain"
|
|
|
|
|
2017-02-05 20:35:05 +01:00
|
|
|
# USER is required to login by SSH to remote host.
|
2017-03-07 17:57:03 +01:00
|
|
|
if [ -z "$DEPLOY_SSH_USER" ]; then
|
2017-02-05 20:35:05 +01:00
|
|
|
if [ -z "$Le_Deploy_ssh_user" ]; then
|
2017-03-07 17:57:03 +01:00
|
|
|
_err "DEPLOY_SSH_USER not defined."
|
2017-02-05 05:17:24 +01:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
else
|
2017-03-07 17:57:03 +01:00
|
|
|
Le_Deploy_ssh_user="$DEPLOY_SSH_USER"
|
2017-02-05 20:35:05 +01:00
|
|
|
_savedomainconf Le_Deploy_ssh_user "$Le_Deploy_ssh_user"
|
|
|
|
fi
|
|
|
|
|
2017-02-07 03:49:20 +01:00
|
|
|
# SERVER is optional. If not provided then use _cdomain
|
2017-03-07 17:57:03 +01:00
|
|
|
if [ -n "$DEPLOY_SSH_SERVER" ]; then
|
|
|
|
Le_Deploy_ssh_server="$DEPLOY_SSH_SERVER"
|
2017-02-07 03:49:20 +01:00
|
|
|
_savedomainconf Le_Deploy_ssh_server "$Le_Deploy_ssh_server"
|
|
|
|
elif [ -z "$Le_Deploy_ssh_server" ]; then
|
|
|
|
Le_Deploy_ssh_server="$_cdomain"
|
2017-02-05 05:17:24 +01:00
|
|
|
fi
|
2017-02-05 21:02:59 +01:00
|
|
|
|
2017-02-11 22:11:27 +01:00
|
|
|
# CMD is optional. If not provided then use ssh
|
2017-03-07 17:57:03 +01:00
|
|
|
if [ -n "$DEPLOY_SSH_CMD" ]; then
|
|
|
|
Le_Deploy_ssh_cmd="$DEPLOY_SSH_CMD"
|
2017-02-11 22:11:27 +01:00
|
|
|
_savedomainconf Le_Deploy_ssh_cmd "$Le_Deploy_ssh_cmd"
|
|
|
|
elif [ -z "$Le_Deploy_ssh_cmd" ]; then
|
2020-02-23 02:09:24 +01:00
|
|
|
Le_Deploy_ssh_cmd="ssh -T"
|
2017-02-07 03:49:20 +01:00
|
|
|
fi
|
|
|
|
|
2021-08-25 16:55:36 +02:00
|
|
|
# USE_SCP is optional. If not provided then default to previously saved
|
|
|
|
# value (which may be undefined... equivalent to "no").
|
|
|
|
if [ "$DEPLOY_SSH_USE_SCP" = "yes" ]; then
|
|
|
|
Le_Deploy_ssh_use_scp="yes"
|
|
|
|
_savedomainconf Le_Deploy_ssh_use_scp "$Le_Deploy_ssh_use_scp"
|
|
|
|
elif [ "$DEPLOY_SSH_USE_SCP" = "no" ]; then
|
|
|
|
Le_Deploy_ssh_use_scp=""
|
|
|
|
_cleardomainconf Le_Deploy_ssh_use_scp
|
|
|
|
fi
|
|
|
|
|
|
|
|
# SCP_CMD is optional. If not provided then use scp
|
|
|
|
if [ -n "$DEPLOY_SSH_SCP_CMD" ]; then
|
|
|
|
Le_Deploy_ssh_scp_cmd="$DEPLOY_SSH_SCP_CMD"
|
|
|
|
_savedomainconf Le_Deploy_ssh_scp_cmd "$Le_Deploy_ssh_scp_cmd"
|
|
|
|
elif [ -z "$Le_Deploy_ssh_scp_cmd" ]; then
|
|
|
|
Le_Deploy_ssh_scp_cmd="scp -T"
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2020-03-11 15:58:36 +01:00
|
|
|
# BACKUP is optional. If not provided then default to previously saved value or yes.
|
2017-03-07 17:57:03 +01:00
|
|
|
if [ "$DEPLOY_SSH_BACKUP" = "no" ]; then
|
2017-02-11 22:42:44 +01:00
|
|
|
Le_Deploy_ssh_backup="no"
|
2020-02-23 03:23:59 +01:00
|
|
|
elif [ -z "$Le_Deploy_ssh_backup" ] || [ "$DEPLOY_SSH_BACKUP" = "yes" ]; then
|
2017-02-11 22:42:44 +01:00
|
|
|
Le_Deploy_ssh_backup="yes"
|
|
|
|
fi
|
|
|
|
_savedomainconf Le_Deploy_ssh_backup "$Le_Deploy_ssh_backup"
|
|
|
|
|
2020-03-15 02:51:21 +01:00
|
|
|
# BACKUP_PATH is optional. If not provided then default to previously saved value or .acme_ssh_deploy
|
|
|
|
if [ -n "$DEPLOY_SSH_BACKUP_PATH" ]; then
|
|
|
|
Le_Deploy_ssh_backup_path="$DEPLOY_SSH_BACKUP_PATH"
|
|
|
|
elif [ -z "$Le_Deploy_ssh_backup_path" ]; then
|
|
|
|
Le_Deploy_ssh_backup_path=".acme_ssh_deploy"
|
|
|
|
fi
|
|
|
|
_savedomainconf Le_Deploy_ssh_backup_path "$Le_Deploy_ssh_backup_path"
|
|
|
|
|
2020-03-11 15:58:36 +01:00
|
|
|
# MULTI_CALL is optional. If not provided then default to previously saved
|
|
|
|
# value (which may be undefined... equivalent to "no").
|
2020-03-03 19:40:33 +01:00
|
|
|
if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
|
|
|
|
Le_Deploy_ssh_multi_call="yes"
|
2020-03-11 15:58:36 +01:00
|
|
|
_savedomainconf Le_Deploy_ssh_multi_call "$Le_Deploy_ssh_multi_call"
|
|
|
|
elif [ "$DEPLOY_SSH_MULTI_CALL" = "no" ]; then
|
|
|
|
Le_Deploy_ssh_multi_call=""
|
|
|
|
_cleardomainconf Le_Deploy_ssh_multi_call
|
2020-02-23 03:10:42 +01:00
|
|
|
fi
|
2020-02-23 04:09:28 +01:00
|
|
|
|
2021-08-25 16:55:36 +02:00
|
|
|
# USE_SCP is optional. If not provided then default to previously saved
|
|
|
|
# value (which may be undefined... equivalent to "no").
|
|
|
|
if [ "$DEPLOY_SSH_USE_SCP" = "yes" ]; then
|
|
|
|
Le_Deploy_ssh_use_scp="yes"
|
|
|
|
_savedomainconf Le_Deploy_ssh_use_scp "$Le_Deploy_ssh_use_scp"
|
|
|
|
Le_Deploy_ssh_multi_call="yes"
|
|
|
|
_savedomainconf Le_Deploy_ssh_multi_call "$Le_Deploy_ssh_multi_call"
|
|
|
|
elif [ "$DEPLOY_SSH_USE_SCP" = "no" ]; then
|
|
|
|
Le_Deploy_ssh_use_scp=""
|
|
|
|
_cleardomainconf Le_Deploy_ssh_use_scp
|
|
|
|
fi
|
|
|
|
|
|
|
|
# SCP_CMD is optional. If not provided then use scp
|
|
|
|
if [ -n "$DEPLOY_SSH_SCP_CMD" ]; then
|
|
|
|
Le_Deploy_ssh_scp_cmd="$DEPLOY_SSH_SCP_CMD"
|
|
|
|
_savedomainconf Le_Deploy_ssh_scp_cmd "$Le_Deploy_ssh_scp_cmd"
|
|
|
|
elif [ -z "$Le_Deploy_ssh_scp_cmd" ]; then
|
|
|
|
Le_Deploy_ssh_scp_cmd="scp -T -q "
|
|
|
|
fi
|
|
|
|
|
2020-05-14 11:15:31 +02:00
|
|
|
_deploy_ssh_servers=$Le_Deploy_ssh_server
|
|
|
|
for Le_Deploy_ssh_server in $_deploy_ssh_servers; do
|
|
|
|
_ssh_deploy
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
_ssh_deploy() {
|
|
|
|
_err_code=0
|
|
|
|
_cmdstr=""
|
|
|
|
_backupprefix=""
|
|
|
|
_backupdir=""
|
2021-08-25 16:55:36 +02:00
|
|
|
_local_cert_file=""
|
|
|
|
_local_ca_file=""
|
|
|
|
_local_full_file=""
|
2020-05-14 11:15:31 +02:00
|
|
|
|
2017-02-11 22:11:27 +01:00
|
|
|
_info "Deploy certificates to remote server $Le_Deploy_ssh_user@$Le_Deploy_ssh_server"
|
2021-08-25 16:55:36 +02:00
|
|
|
if [ "$Le_Deploy_ssh_use_scp" = "yes" ]; then
|
|
|
|
_info "Using scp as alternate method for copying files. Multicall Mode is implicit"
|
|
|
|
Le_Deploy_ssh_multi_call="yes"
|
|
|
|
_savedomainconf Le_Deploy_ssh_multi_call "$Le_Deploy_ssh_multi_call"
|
|
|
|
fi
|
2020-03-03 19:40:33 +01:00
|
|
|
if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
|
|
|
|
_info "Using MULTI_CALL mode... Required commands sent in multiple calls to remote host"
|
2020-02-23 03:10:42 +01:00
|
|
|
else
|
2020-03-03 19:40:33 +01:00
|
|
|
_info "Required commands batched and sent in single call to remote host"
|
2020-02-23 03:10:42 +01:00
|
|
|
fi
|
2017-02-05 05:17:24 +01:00
|
|
|
|
2020-02-23 02:43:28 +01:00
|
|
|
if [ "$Le_Deploy_ssh_backup" = "yes" ]; then
|
2020-03-15 02:51:21 +01:00
|
|
|
_backupprefix="$Le_Deploy_ssh_backup_path/$_cdomain-backup"
|
|
|
|
_backupdir="$_backupprefix-$(_utc_date | tr ' ' '-')"
|
2020-02-23 02:43:28 +01: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."
|
2020-03-03 19:40:33 +01:00
|
|
|
if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
|
2020-02-23 03:10:42 +01:00
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2020-02-23 02:43:28 +01:00
|
|
|
fi
|
|
|
|
|
2017-02-05 20:35:05 +01:00
|
|
|
# KEYFILE is optional.
|
|
|
|
# If provided then private key will be copied to provided filename.
|
2017-03-07 17:57:03 +01:00
|
|
|
if [ -n "$DEPLOY_SSH_KEYFILE" ]; then
|
|
|
|
Le_Deploy_ssh_keyfile="$DEPLOY_SSH_KEYFILE"
|
2017-02-05 05:17:24 +01:00
|
|
|
_savedomainconf Le_Deploy_ssh_keyfile "$Le_Deploy_ssh_keyfile"
|
|
|
|
fi
|
|
|
|
if [ -n "$Le_Deploy_ssh_keyfile" ]; then
|
2017-02-11 22:42:44 +01:00
|
|
|
if [ "$Le_Deploy_ssh_backup" = "yes" ]; then
|
|
|
|
# backup file we are about to overwrite.
|
2017-02-12 17:17:23 +01:00
|
|
|
_cmdstr="$_cmdstr cp $Le_Deploy_ssh_keyfile $_backupdir >/dev/null;"
|
2021-08-25 16:55:36 +02:00
|
|
|
if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2017-02-11 22:42:44 +01:00
|
|
|
fi
|
2021-08-25 16:55:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
# copy new key into file.
|
|
|
|
if [ "$Le_Deploy_ssh_use_scp" = "yes" ]; then
|
|
|
|
# scp the file
|
|
|
|
if ! _scp_remote_cmd "$_ckey" "$Le_Deploy_ssh_keyfile"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
_cmdstr="$_cmdstr echo \"$(cat "$_ckey")\" > $Le_Deploy_ssh_keyfile;"
|
|
|
|
_info "will copy private key to remote file $Le_Deploy_ssh_keyfile"
|
|
|
|
if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
2020-02-23 03:10:42 +01:00
|
|
|
fi
|
|
|
|
fi
|
2017-02-05 05:17:24 +01:00
|
|
|
fi
|
|
|
|
|
2017-02-05 20:35:05 +01:00
|
|
|
# CERTFILE is optional.
|
2018-08-02 14:57:27 +02:00
|
|
|
# If provided then certificate will be copied or appended to provided filename.
|
2017-03-07 17:57:03 +01:00
|
|
|
if [ -n "$DEPLOY_SSH_CERTFILE" ]; then
|
|
|
|
Le_Deploy_ssh_certfile="$DEPLOY_SSH_CERTFILE"
|
2017-02-05 05:17:24 +01:00
|
|
|
_savedomainconf Le_Deploy_ssh_certfile "$Le_Deploy_ssh_certfile"
|
|
|
|
fi
|
|
|
|
if [ -n "$Le_Deploy_ssh_certfile" ]; then
|
2017-02-11 22:42:44 +01:00
|
|
|
_pipe=">"
|
2017-02-05 05:17:24 +01:00
|
|
|
if [ "$Le_Deploy_ssh_certfile" = "$Le_Deploy_ssh_keyfile" ]; then
|
2017-02-11 22:11:27 +01:00
|
|
|
# if filename is same as previous file then append.
|
|
|
|
_pipe=">>"
|
2021-08-25 16:55:36 +02:00
|
|
|
_local_cert_file=$(_mktemp)
|
2021-08-30 11:08:06 +02:00
|
|
|
cat "$_ckey" > "$_local_cert_file"
|
|
|
|
cat "$_ccert" >> "$_local_cert_file"
|
2017-02-11 22:42:44 +01:00
|
|
|
elif [ "$Le_Deploy_ssh_backup" = "yes" ]; then
|
2017-02-05 20:35:05 +01:00
|
|
|
# backup file we are about to overwrite.
|
2017-02-12 17:17:23 +01:00
|
|
|
_cmdstr="$_cmdstr cp $Le_Deploy_ssh_certfile $_backupdir >/dev/null;"
|
2021-08-25 16:55:36 +02:00
|
|
|
if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2017-02-05 05:17:24 +01:00
|
|
|
fi
|
2021-08-25 16:55:36 +02:00
|
|
|
|
|
|
|
if [ "$Le_Deploy_ssh_use_scp" = "yes" ]; then
|
2021-08-30 11:17:03 +02:00
|
|
|
if [ -n "$_local_cert_file" ]; then
|
2021-08-25 16:55:36 +02:00
|
|
|
if ! _scp_remote_cmd "$_local_cert_file" "$Le_Deploy_ssh_certfile"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if ! _scp_remote_cmd "$_ccert" "$Le_Deploy_ssh_certfile"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# copy new certificate into file.
|
|
|
|
_cmdstr="$_cmdstr echo \"$(cat "$_ccert")\" $_pipe $Le_Deploy_ssh_certfile;"
|
|
|
|
_info "will copy certificate to remote file $Le_Deploy_ssh_certfile"
|
|
|
|
if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
2020-02-23 03:10:42 +01:00
|
|
|
fi
|
|
|
|
fi
|
2017-02-05 05:17:24 +01:00
|
|
|
fi
|
|
|
|
|
2017-02-05 20:35:05 +01:00
|
|
|
# CAFILE is optional.
|
2017-02-11 22:11:27 +01:00
|
|
|
# If provided then CA intermediate certificate will be copied or appended to provided filename.
|
2017-03-07 17:57:03 +01:00
|
|
|
if [ -n "$DEPLOY_SSH_CAFILE" ]; then
|
|
|
|
Le_Deploy_ssh_cafile="$DEPLOY_SSH_CAFILE"
|
2017-02-05 05:17:24 +01:00
|
|
|
_savedomainconf Le_Deploy_ssh_cafile "$Le_Deploy_ssh_cafile"
|
|
|
|
fi
|
|
|
|
if [ -n "$Le_Deploy_ssh_cafile" ]; then
|
2017-02-11 22:42:44 +01:00
|
|
|
_pipe=">"
|
2020-08-17 16:18:20 +02:00
|
|
|
if [ "$Le_Deploy_ssh_cafile" = "$Le_Deploy_ssh_keyfile" ] ||
|
|
|
|
[ "$Le_Deploy_ssh_cafile" = "$Le_Deploy_ssh_certfile" ]; then
|
2017-02-11 22:11:27 +01:00
|
|
|
# if filename is same as previous file then append.
|
|
|
|
_pipe=">>"
|
2021-08-25 16:55:36 +02:00
|
|
|
_local_ca_file=$(_mktemp)
|
|
|
|
if [ "$Le_Deploy_ssh_cafile" = "$Le_Deploy_ssh_keyfile" ] ; then
|
2021-08-30 11:17:03 +02:00
|
|
|
cat "$_ckey" >> "$_local_ca_file"
|
2021-08-25 16:55:36 +02:00
|
|
|
fi
|
|
|
|
if [ "$Le_Deploy_ssh_cafile" = "$Le_Deploy_ssh_certfile" ]; then
|
2021-08-30 11:17:03 +02:00
|
|
|
cat "$_ccert" >> "$_local_ca_file"
|
2021-08-25 16:55:36 +02:00
|
|
|
fi
|
|
|
|
|
2021-08-30 11:17:03 +02:00
|
|
|
cat "$_cca" >> "$_local_ca_file"
|
2021-08-25 16:55:36 +02:00
|
|
|
|
2017-02-11 22:42:44 +01:00
|
|
|
elif [ "$Le_Deploy_ssh_backup" = "yes" ]; then
|
2017-02-11 22:11:27 +01:00
|
|
|
# backup file we are about to overwrite.
|
2017-02-12 17:17:23 +01:00
|
|
|
_cmdstr="$_cmdstr cp $Le_Deploy_ssh_cafile $_backupdir >/dev/null;"
|
2021-08-25 16:55:36 +02:00
|
|
|
if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2017-02-11 22:11:27 +01:00
|
|
|
fi
|
2021-08-25 16:55:36 +02:00
|
|
|
|
|
|
|
if [ "$Le_Deploy_ssh_use_scp" = "yes" ]; then
|
2021-08-30 11:17:03 +02:00
|
|
|
if [ -n "$_local_ca_file" ]; then
|
2021-08-25 16:55:36 +02:00
|
|
|
if ! _scp_remote_cmd "$_local_ca_file" "$Le_Deploy_ssh_cafile"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if ! _scp_remote_cmd "$_cca" "$Le_Deploy_ssh_cafile"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# copy new certificate into file.
|
|
|
|
_cmdstr="$_cmdstr echo \"$(cat "$_cca")\" $_pipe $Le_Deploy_ssh_cafile;"
|
|
|
|
_info "will copy CA file to remote file $Le_Deploy_ssh_cafile"
|
|
|
|
if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
2020-02-23 03:10:42 +01:00
|
|
|
fi
|
|
|
|
fi
|
2017-02-05 05:17:24 +01:00
|
|
|
fi
|
|
|
|
|
2017-02-05 20:35:05 +01:00
|
|
|
# FULLCHAIN is optional.
|
2017-02-11 22:11:27 +01:00
|
|
|
# If provided then fullchain certificate will be copied or appended to provided filename.
|
2017-03-07 17:57:03 +01:00
|
|
|
if [ -n "$DEPLOY_SSH_FULLCHAIN" ]; then
|
|
|
|
Le_Deploy_ssh_fullchain="$DEPLOY_SSH_FULLCHAIN"
|
2017-02-05 05:17:24 +01:00
|
|
|
_savedomainconf Le_Deploy_ssh_fullchain "$Le_Deploy_ssh_fullchain"
|
|
|
|
fi
|
|
|
|
if [ -n "$Le_Deploy_ssh_fullchain" ]; then
|
2017-02-11 22:42:44 +01:00
|
|
|
_pipe=">"
|
2020-08-17 16:18:20 +02:00
|
|
|
if [ "$Le_Deploy_ssh_fullchain" = "$Le_Deploy_ssh_keyfile" ] ||
|
|
|
|
[ "$Le_Deploy_ssh_fullchain" = "$Le_Deploy_ssh_certfile" ] ||
|
|
|
|
[ "$Le_Deploy_ssh_fullchain" = "$Le_Deploy_ssh_cafile" ]; then
|
2017-02-11 22:11:27 +01:00
|
|
|
# if filename is same as previous file then append.
|
|
|
|
_pipe=">>"
|
2021-08-25 16:55:36 +02:00
|
|
|
_local_full_file=$(_mktemp)
|
|
|
|
if [ "$Le_Deploy_ssh_fullchain" = "$Le_Deploy_ssh_keyfile" ] ; then
|
2021-08-30 11:08:06 +02:00
|
|
|
cat "$_ckey" >> "$_local_full_file"
|
2021-08-25 16:55:36 +02:00
|
|
|
fi
|
|
|
|
if [ "$Le_Deploy_ssh_fullchain" = "$Le_Deploy_ssh_certfile" ]; then
|
2021-08-30 11:08:06 +02:00
|
|
|
cat "$_ccert" >> "$_local_full_file"
|
2021-08-25 16:55:36 +02:00
|
|
|
fi
|
|
|
|
if [ "$Le_Deploy_ssh_fullchain" = "$Le_Deploy_ssh_cafile" ]; then
|
2021-08-30 11:08:06 +02:00
|
|
|
cat "$_cca" >> "$_local_full_file"
|
2021-08-25 16:55:36 +02:00
|
|
|
fi
|
2021-08-30 11:08:06 +02:00
|
|
|
cat "$_cfullchain" >> "$_local_full_file"
|
2021-08-25 16:55:36 +02:00
|
|
|
|
2017-02-11 22:42:44 +01:00
|
|
|
elif [ "$Le_Deploy_ssh_backup" = "yes" ]; then
|
2017-02-11 22:11:27 +01:00
|
|
|
# backup file we are about to overwrite.
|
2017-02-12 17:17:23 +01:00
|
|
|
_cmdstr="$_cmdstr cp $Le_Deploy_ssh_fullchain $_backupdir >/dev/null;"
|
2021-08-25 16:55:36 +02:00
|
|
|
if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2017-02-11 22:11:27 +01:00
|
|
|
fi
|
2021-08-25 16:55:36 +02:00
|
|
|
|
|
|
|
if [ "$Le_Deploy_ssh_use_scp" = "yes" ]; then
|
2021-08-30 11:17:03 +02:00
|
|
|
if [ -n "$_local_full_file" ] ; then
|
2021-08-25 16:55:36 +02:00
|
|
|
if ! _scp_remote_cmd "$_local_full_file" "$Le_Deploy_ssh_fullchain"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if ! _scp_remote_cmd "$_cfullchain" "$Le_Deploy_ssh_fullchain"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# copy new certificate into file.
|
|
|
|
_cmdstr="$_cmdstr echo \"$(cat "$_cfullchain")\" $_pipe $Le_Deploy_ssh_fullchain;"
|
|
|
|
_info "will copy fullchain to remote file $Le_Deploy_ssh_fullchain"
|
|
|
|
if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
2020-02-23 03:10:42 +01:00
|
|
|
fi
|
|
|
|
fi
|
2017-02-05 05:17:24 +01:00
|
|
|
fi
|
2021-08-25 16:55:36 +02:00
|
|
|
# cleanup local files if any
|
|
|
|
|
|
|
|
if [ -n "$_local_cert_file" ]; then
|
2021-08-30 11:08:06 +02:00
|
|
|
rm "$_local_cert_file" > /dev/null 1>&2
|
2021-08-25 16:55:36 +02:00
|
|
|
fi
|
|
|
|
if [ -n "$_local_ca_file" ]; then
|
2021-08-30 11:08:06 +02:00
|
|
|
rm "$_local_ca_file" > /dev/null 1>&2
|
2021-08-25 16:55:36 +02:00
|
|
|
fi
|
|
|
|
if [ -n "$_local_full_file" ]; then
|
2021-08-30 11:08:06 +02:00
|
|
|
rm "$_local_full_file" > /dev/null 1>&2
|
2021-08-25 16:55:36 +02:00
|
|
|
fi
|
|
|
|
|
2017-02-05 05:17:24 +01:00
|
|
|
|
2017-02-05 20:35:05 +01:00
|
|
|
# REMOTE_CMD is optional.
|
|
|
|
# If provided then this command will be executed on remote host.
|
2017-03-07 17:57:03 +01:00
|
|
|
if [ -n "$DEPLOY_SSH_REMOTE_CMD" ]; then
|
|
|
|
Le_Deploy_ssh_remote_cmd="$DEPLOY_SSH_REMOTE_CMD"
|
2017-02-05 05:17:24 +01:00
|
|
|
_savedomainconf Le_Deploy_ssh_remote_cmd "$Le_Deploy_ssh_remote_cmd"
|
|
|
|
fi
|
|
|
|
if [ -n "$Le_Deploy_ssh_remote_cmd" ]; then
|
2017-02-12 17:17:23 +01:00
|
|
|
_cmdstr="$_cmdstr $Le_Deploy_ssh_remote_cmd;"
|
2017-02-05 20:35:05 +01:00
|
|
|
_info "Will execute remote command $Le_Deploy_ssh_remote_cmd"
|
2020-03-03 19:40:33 +01:00
|
|
|
if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
|
2020-02-23 03:10:42 +01:00
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2017-02-05 05:17:24 +01:00
|
|
|
fi
|
|
|
|
|
2020-03-03 19:40:33 +01:00
|
|
|
# if commands not all sent in multiple calls then all commands sent in a single SSH call now...
|
2020-02-23 03:10:42 +01:00
|
|
|
if [ -n "$_cmdstr" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
2020-02-23 02:31:52 +01:00
|
|
|
fi
|
2021-08-25 16:55:36 +02:00
|
|
|
# cleanup in case all is ok
|
2020-02-23 02:31:52 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
#cmd
|
|
|
|
_ssh_remote_cmd() {
|
|
|
|
_cmd="$1"
|
|
|
|
_secure_debug "Remote commands to execute: $_cmd"
|
2020-02-23 04:05:06 +01:00
|
|
|
_info "Submitting sequence of commands to remote server by ssh"
|
2017-02-05 21:26:55 +01:00
|
|
|
# quotations in bash cmd below intended. Squash travis spellcheck error
|
|
|
|
# shellcheck disable=SC2029
|
2020-02-23 02:31:52 +01:00
|
|
|
$Le_Deploy_ssh_cmd "$Le_Deploy_ssh_user@$Le_Deploy_ssh_server" sh -c "'$_cmd'"
|
|
|
|
_err_code="$?"
|
2017-02-11 22:11:27 +01:00
|
|
|
|
2020-02-23 02:31:52 +01:00
|
|
|
if [ "$_err_code" != "0" ]; then
|
2020-02-23 04:05:06 +01:00
|
|
|
_err "Error code $_err_code returned from ssh"
|
2017-02-11 22:11:27 +01:00
|
|
|
fi
|
2017-02-05 05:17:24 +01:00
|
|
|
|
2020-02-23 02:31:52 +01:00
|
|
|
return $_err_code
|
2017-02-05 05:17:24 +01:00
|
|
|
}
|
2021-08-25 16:55:36 +02:00
|
|
|
|
|
|
|
# cmd scp
|
|
|
|
_scp_remote_cmd() {
|
|
|
|
_secure_debug "Remote scp source $1 and destination $2 using : $Le_Deploy_ssh_scp_cmd"
|
|
|
|
_info "Submitting secure copy command : $Le_Deploy_ssh_scp_cmd"
|
|
|
|
$Le_Deploy_ssh_scp_cmd "$1" "$Le_Deploy_ssh_user"@"$Le_Deploy_ssh_server":"$2"
|
|
|
|
_err_code="$?"
|
|
|
|
|
|
|
|
if [ "$_err_code" != "0" ]; then
|
|
|
|
_err "Error code $_err_code returned from scp"
|
|
|
|
fi
|
|
|
|
|
|
|
|
return $_err_code
|
|
|
|
}
|