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
|
|
|
|
# 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"
|
|
|
|
# export DEPLOY_SSH_BACKUP="" # yes or no, default to yes
|
2020-02-23 02:10:42 +00:00
|
|
|
# export DEPLOY_SSH_BATCH_MODE="yes" # yes or no, default to yes
|
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-02-23 01:31:52 +00:00
|
|
|
_err_code=0
|
2017-02-05 19:35:05 +00:00
|
|
|
_cmdstr=""
|
2017-02-05 20:02:59 +00:00
|
|
|
_homedir='~'
|
2017-02-08 15:15:39 +00:00
|
|
|
_backupprefix="$_homedir/.acme_ssh_deploy/$_cdomain-backup"
|
2017-02-07 02:49:20 +00:00
|
|
|
_backupdir="$_backupprefix-$(_utc_date | tr ' ' '-')"
|
2017-02-05 04:17:24 +00:00
|
|
|
|
2017-02-05 20:16:05 +00:00
|
|
|
if [ -f "$DOMAIN_CONF" ]; then
|
2017-02-05 20:20:06 +00:00
|
|
|
# shellcheck disable=SC1090
|
2017-02-05 20:16:05 +00:00
|
|
|
. "$DOMAIN_CONF"
|
2017-02-05 20:12:23 +00:00
|
|
|
fi
|
|
|
|
|
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.
|
2017-03-07 16:57:03 +00:00
|
|
|
if [ -z "$DEPLOY_SSH_USER" ]; then
|
2017-02-05 19:35:05 +00:00
|
|
|
if [ -z "$Le_Deploy_ssh_user" ]; then
|
2017-03-07 16:57:03 +00:00
|
|
|
_err "DEPLOY_SSH_USER not defined."
|
2017-02-05 04:17:24 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
else
|
2017-03-07 16:57:03 +00:00
|
|
|
Le_Deploy_ssh_user="$DEPLOY_SSH_USER"
|
2017-02-05 19:35:05 +00:00
|
|
|
_savedomainconf Le_Deploy_ssh_user "$Le_Deploy_ssh_user"
|
|
|
|
fi
|
|
|
|
|
2017-02-07 02:49:20 +00:00
|
|
|
# SERVER is optional. If not provided then use _cdomain
|
2017-03-07 16:57:03 +00:00
|
|
|
if [ -n "$DEPLOY_SSH_SERVER" ]; then
|
|
|
|
Le_Deploy_ssh_server="$DEPLOY_SSH_SERVER"
|
2017-02-07 02:49:20 +00: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 04:17:24 +00:00
|
|
|
fi
|
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
|
2017-03-07 16:57:03 +00:00
|
|
|
if [ -n "$DEPLOY_SSH_CMD" ]; then
|
|
|
|
Le_Deploy_ssh_cmd="$DEPLOY_SSH_CMD"
|
2017-02-11 21:11:27 +00:00
|
|
|
_savedomainconf Le_Deploy_ssh_cmd "$Le_Deploy_ssh_cmd"
|
|
|
|
elif [ -z "$Le_Deploy_ssh_cmd" ]; then
|
2020-02-23 01:09:24 +00:00
|
|
|
Le_Deploy_ssh_cmd="ssh -T"
|
2017-02-07 02:49:20 +00:00
|
|
|
fi
|
|
|
|
|
2017-02-11 21:42:44 +00:00
|
|
|
# BACKUP is optional. If not provided then default to yes
|
2017-03-07 16:57:03 +00:00
|
|
|
if [ "$DEPLOY_SSH_BACKUP" = "no" ]; then
|
2017-02-11 21:42:44 +00:00
|
|
|
Le_Deploy_ssh_backup="no"
|
2020-02-23 02:23:59 +00:00
|
|
|
elif [ -z "$Le_Deploy_ssh_backup" ] || [ "$DEPLOY_SSH_BACKUP" = "yes" ]; then
|
2017-02-11 21:42:44 +00:00
|
|
|
Le_Deploy_ssh_backup="yes"
|
|
|
|
fi
|
|
|
|
_savedomainconf Le_Deploy_ssh_backup "$Le_Deploy_ssh_backup"
|
|
|
|
|
2020-02-23 02:10:42 +00:00
|
|
|
# BATCH_MODE is optional. If not provided then default to yes
|
|
|
|
if [ "$DEPLOY_SSH_BATCH_MODE" = "no" ]; then
|
|
|
|
Le_Deploy_ssh_batch_mode="no"
|
2020-02-23 02:23:59 +00:00
|
|
|
elif [ -z "$Le_Deploy_ssh_batch_mode" ] || [ "$DEPLOY_SSH_BATCH_MODE" = "yes" ]; then
|
2020-02-23 02:10:42 +00:00
|
|
|
Le_Deploy_ssh_batch_mode="yes"
|
|
|
|
fi
|
|
|
|
_savedomainconf Le_Deploy_ssh_batch_mode "$Le_Deploy_ssh_batch_mode"
|
|
|
|
|
2017-02-11 21:11:27 +00:00
|
|
|
_info "Deploy certificates to remote server $Le_Deploy_ssh_user@$Le_Deploy_ssh_server"
|
2020-02-23 02:10:42 +00:00
|
|
|
if [ "$Le_Deploy_ssh_batch_mode" = "yes" ]; then
|
|
|
|
_info "Using BATCH MODE... Multiple commands sent in single call to remote host"
|
|
|
|
else
|
|
|
|
_info "Commands sent individually in multiple calls to remote host"
|
|
|
|
fi
|
2017-02-05 04:17:24 +00:00
|
|
|
|
2020-02-23 01:43:28 +00:00
|
|
|
if [ "$Le_Deploy_ssh_backup" = "yes" ]; then
|
|
|
|
# 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-02-23 02:10:42 +00:00
|
|
|
if [ "$Le_Deploy_ssh_batch_mode" = "no" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2020-02-23 01:43:28 +00:00
|
|
|
fi
|
|
|
|
|
2017-02-05 19:35:05 +00:00
|
|
|
# KEYFILE is optional.
|
|
|
|
# If provided then private key will be copied to provided filename.
|
2017-03-07 16:57:03 +00:00
|
|
|
if [ -n "$DEPLOY_SSH_KEYFILE" ]; then
|
|
|
|
Le_Deploy_ssh_keyfile="$DEPLOY_SSH_KEYFILE"
|
2017-02-05 04:17:24 +00:00
|
|
|
_savedomainconf Le_Deploy_ssh_keyfile "$Le_Deploy_ssh_keyfile"
|
|
|
|
fi
|
|
|
|
if [ -n "$Le_Deploy_ssh_keyfile" ]; then
|
2017-02-11 21:42:44 +00:00
|
|
|
if [ "$Le_Deploy_ssh_backup" = "yes" ]; then
|
|
|
|
# backup file we are about to overwrite.
|
2017-02-12 16:17:23 +00:00
|
|
|
_cmdstr="$_cmdstr cp $Le_Deploy_ssh_keyfile $_backupdir >/dev/null;"
|
2017-02-11 21:42:44 +00:00
|
|
|
fi
|
2017-02-05 19:35:05 +00:00
|
|
|
# copy new certificate into file.
|
2017-02-12 16:17:23 +00:00
|
|
|
_cmdstr="$_cmdstr echo \"$(cat "$_ckey")\" > $Le_Deploy_ssh_keyfile;"
|
2017-02-05 04:17:24 +00:00
|
|
|
_info "will copy private key to remote file $Le_Deploy_ssh_keyfile"
|
2020-02-23 02:10:42 +00:00
|
|
|
if [ "$Le_Deploy_ssh_batch_mode" = "no" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2017-02-05 04:17:24 +00:00
|
|
|
fi
|
|
|
|
|
2017-02-05 19:35:05 +00:00
|
|
|
# CERTFILE is optional.
|
2018-08-02 12:57:27 +00:00
|
|
|
# If provided then certificate will be copied or appended to provided filename.
|
2017-03-07 16:57:03 +00:00
|
|
|
if [ -n "$DEPLOY_SSH_CERTFILE" ]; then
|
|
|
|
Le_Deploy_ssh_certfile="$DEPLOY_SSH_CERTFILE"
|
2017-02-05 04:17:24 +00:00
|
|
|
_savedomainconf Le_Deploy_ssh_certfile "$Le_Deploy_ssh_certfile"
|
|
|
|
fi
|
|
|
|
if [ -n "$Le_Deploy_ssh_certfile" ]; then
|
2017-02-11 21:42:44 +00:00
|
|
|
_pipe=">"
|
2017-02-05 04:17:24 +00:00
|
|
|
if [ "$Le_Deploy_ssh_certfile" = "$Le_Deploy_ssh_keyfile" ]; then
|
2017-02-11 21:11:27 +00:00
|
|
|
# if filename is same as previous file then append.
|
|
|
|
_pipe=">>"
|
2017-02-11 21:42:44 +00:00
|
|
|
elif [ "$Le_Deploy_ssh_backup" = "yes" ]; then
|
2017-02-05 19:35:05 +00:00
|
|
|
# backup file we are about to overwrite.
|
2017-02-12 16:17:23 +00:00
|
|
|
_cmdstr="$_cmdstr cp $Le_Deploy_ssh_certfile $_backupdir >/dev/null;"
|
2017-02-05 04:17:24 +00:00
|
|
|
fi
|
2017-02-11 21:11:27 +00:00
|
|
|
# copy new certificate into file.
|
2017-02-12 16:17:23 +00:00
|
|
|
_cmdstr="$_cmdstr echo \"$(cat "$_ccert")\" $_pipe $Le_Deploy_ssh_certfile;"
|
2017-02-11 21:11:27 +00:00
|
|
|
_info "will copy certificate to remote file $Le_Deploy_ssh_certfile"
|
2020-02-23 02:10:42 +00:00
|
|
|
if [ "$Le_Deploy_ssh_batch_mode" = "no" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2017-02-05 04:17:24 +00:00
|
|
|
fi
|
|
|
|
|
2017-02-05 19:35:05 +00:00
|
|
|
# CAFILE is optional.
|
2017-02-11 21:11:27 +00:00
|
|
|
# If provided then CA intermediate certificate will be copied or appended to provided filename.
|
2017-03-07 16:57:03 +00:00
|
|
|
if [ -n "$DEPLOY_SSH_CAFILE" ]; then
|
|
|
|
Le_Deploy_ssh_cafile="$DEPLOY_SSH_CAFILE"
|
2017-02-05 04:17:24 +00:00
|
|
|
_savedomainconf Le_Deploy_ssh_cafile "$Le_Deploy_ssh_cafile"
|
|
|
|
fi
|
|
|
|
if [ -n "$Le_Deploy_ssh_cafile" ]; then
|
2017-02-11 21:42:44 +00:00
|
|
|
_pipe=">"
|
|
|
|
if [ "$Le_Deploy_ssh_cafile" = "$Le_Deploy_ssh_keyfile" ] \
|
|
|
|
|| [ "$Le_Deploy_ssh_cafile" = "$Le_Deploy_ssh_certfile" ]; then
|
2017-02-11 21:11:27 +00:00
|
|
|
# if filename is same as previous file then append.
|
|
|
|
_pipe=">>"
|
2017-02-11 21:42:44 +00:00
|
|
|
elif [ "$Le_Deploy_ssh_backup" = "yes" ]; then
|
2017-02-11 21:11:27 +00:00
|
|
|
# backup file we are about to overwrite.
|
2017-02-12 16:17:23 +00:00
|
|
|
_cmdstr="$_cmdstr cp $Le_Deploy_ssh_cafile $_backupdir >/dev/null;"
|
2017-02-11 21:11:27 +00:00
|
|
|
fi
|
2017-02-05 19:35:05 +00:00
|
|
|
# copy new certificate into file.
|
2017-02-12 16:17:23 +00:00
|
|
|
_cmdstr="$_cmdstr echo \"$(cat "$_cca")\" $_pipe $Le_Deploy_ssh_cafile;"
|
2017-02-05 04:17:24 +00:00
|
|
|
_info "will copy CA file to remote file $Le_Deploy_ssh_cafile"
|
2020-02-23 02:10:42 +00:00
|
|
|
if [ "$Le_Deploy_ssh_batch_mode" = "no" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2017-02-05 04:17:24 +00:00
|
|
|
fi
|
|
|
|
|
2017-02-05 19:35:05 +00:00
|
|
|
# FULLCHAIN is optional.
|
2017-02-11 21:11:27 +00:00
|
|
|
# If provided then fullchain certificate will be copied or appended to provided filename.
|
2017-03-07 16:57:03 +00:00
|
|
|
if [ -n "$DEPLOY_SSH_FULLCHAIN" ]; then
|
|
|
|
Le_Deploy_ssh_fullchain="$DEPLOY_SSH_FULLCHAIN"
|
2017-02-05 04:17:24 +00:00
|
|
|
_savedomainconf Le_Deploy_ssh_fullchain "$Le_Deploy_ssh_fullchain"
|
|
|
|
fi
|
|
|
|
if [ -n "$Le_Deploy_ssh_fullchain" ]; then
|
2017-02-11 21:42:44 +00:00
|
|
|
_pipe=">"
|
|
|
|
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 21:11:27 +00:00
|
|
|
# if filename is same as previous file then append.
|
|
|
|
_pipe=">>"
|
2017-02-11 21:42:44 +00:00
|
|
|
elif [ "$Le_Deploy_ssh_backup" = "yes" ]; then
|
2017-02-11 21:11:27 +00:00
|
|
|
# backup file we are about to overwrite.
|
2017-02-12 16:17:23 +00:00
|
|
|
_cmdstr="$_cmdstr cp $Le_Deploy_ssh_fullchain $_backupdir >/dev/null;"
|
2017-02-11 21:11:27 +00:00
|
|
|
fi
|
2017-02-05 19:35:05 +00:00
|
|
|
# copy new certificate into file.
|
2017-02-12 16:17:23 +00:00
|
|
|
_cmdstr="$_cmdstr echo \"$(cat "$_cfullchain")\" $_pipe $Le_Deploy_ssh_fullchain;"
|
2017-02-11 21:11:27 +00:00
|
|
|
_info "will copy fullchain to remote file $Le_Deploy_ssh_fullchain"
|
2020-02-23 02:10:42 +00:00
|
|
|
if [ "$Le_Deploy_ssh_batch_mode" = "no" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2017-02-05 04:17:24 +00:00
|
|
|
fi
|
|
|
|
|
2017-02-05 19:35:05 +00:00
|
|
|
# REMOTE_CMD is optional.
|
|
|
|
# If provided then this command will be executed on remote host.
|
2017-03-07 16:57:03 +00:00
|
|
|
if [ -n "$DEPLOY_SSH_REMOTE_CMD" ]; then
|
|
|
|
Le_Deploy_ssh_remote_cmd="$DEPLOY_SSH_REMOTE_CMD"
|
2017-02-05 04:17:24 +00:00
|
|
|
_savedomainconf Le_Deploy_ssh_remote_cmd "$Le_Deploy_ssh_remote_cmd"
|
|
|
|
fi
|
|
|
|
if [ -n "$Le_Deploy_ssh_remote_cmd" ]; then
|
2017-02-12 16:17:23 +00:00
|
|
|
_cmdstr="$_cmdstr $Le_Deploy_ssh_remote_cmd;"
|
2017-02-05 19:35:05 +00:00
|
|
|
_info "Will execute remote command $Le_Deploy_ssh_remote_cmd"
|
2020-02-23 02:10:42 +00:00
|
|
|
if [ "$Le_Deploy_ssh_batch_mode" = "no" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
|
|
|
_cmdstr=""
|
|
|
|
fi
|
2017-02-05 04:17:24 +00:00
|
|
|
fi
|
|
|
|
|
2020-02-23 02:10:42 +00:00
|
|
|
# if running as batch mode then all commands sent in a single SSH call now...
|
|
|
|
if [ -n "$_cmdstr" ]; then
|
|
|
|
if ! _ssh_remote_cmd "$_cmdstr"; then
|
|
|
|
return $_err_code
|
|
|
|
fi
|
2020-02-23 01:31:52 +00:00
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
#cmd
|
|
|
|
_ssh_remote_cmd() {
|
|
|
|
_cmd="$1"
|
|
|
|
_secure_debug "Remote commands to execute: $_cmd"
|
|
|
|
_info "Submitting sequence of commands to remote server by $Le_Deploy_ssh_cmd"
|
2017-02-05 20:26:55 +00:00
|
|
|
# quotations in bash cmd below intended. Squash travis spellcheck error
|
|
|
|
# shellcheck disable=SC2029
|
2020-02-23 01:31:52 +00:00
|
|
|
$Le_Deploy_ssh_cmd "$Le_Deploy_ssh_user@$Le_Deploy_ssh_server" sh -c "'$_cmd'"
|
|
|
|
_err_code="$?"
|
2017-02-11 21:11:27 +00:00
|
|
|
|
2020-02-23 01:31:52 +00:00
|
|
|
if [ "$_err_code" != "0" ]; then
|
|
|
|
_err "Error code $_err_code returned from $Le_Deploy_ssh_cmd"
|
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
|
|
|
}
|