You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
7.1 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. #!/usr/bin/env sh
  2. # Script to deploy certificates to remote server by SSH
  3. # Note that SSH must be able to login to remote host without a password...
  4. # SSH Keys must have been exchanged with the remote host. Validate and
  5. # test that you can login to USER@SERVER from the host running acme.sh before
  6. # using this script.
  7. #
  8. # The following variables exported from environment will be used.
  9. # If not set then values previously saved in domain.conf file are used.
  10. #
  11. # Only a username is required. All others are optional.
  12. #
  13. # The following examples are for QNAP NAS running QTS 4.2
  14. # export ACME_DEPLOY_SSH_CMD=""
  15. # export ACME_DEPLOY_SSH_USER="admin"
  16. # export ACME_DEPLOY_SSH_SERVER="qnap"
  17. # export ACME_DEPLOY_SSH_KEYFILE="/etc/stunnel/stunnel.pem"
  18. # export ACME_DEPLOY_SSH_CERTFILE="/etc/stunnel/stunnel.pem"
  19. # export ACME_DEPLOY_SSH_CAFILE="/etc/stunnel/uca.pem"
  20. # export ACME_DEPLOY_SSH_FULLCHAIN=""
  21. # export ACME_DEPLOY_SSH_REMOTE_CMD="/etc/init.d/stunnel.sh restart"
  22. ######## Public functions #####################
  23. #domain keyfile certfile cafile fullchain
  24. ssh_deploy() {
  25. _cdomain="$1"
  26. _ckey="$2"
  27. _ccert="$3"
  28. _cca="$4"
  29. _cfullchain="$5"
  30. _cmdstr=""
  31. _homedir='~'
  32. _backupprefix="$_homedir/.acme_ssh_deploy/$_cdomain-backup"
  33. _backupdir="$_backupprefix-$(_utc_date | tr ' ' '-')"
  34. if [ -f "$DOMAIN_CONF" ]; then
  35. # shellcheck disable=SC1090
  36. . "$DOMAIN_CONF"
  37. fi
  38. _debug _cdomain "$_cdomain"
  39. _debug _ckey "$_ckey"
  40. _debug _ccert "$_ccert"
  41. _debug _cca "$_cca"
  42. _debug _cfullchain "$_cfullchain"
  43. # USER is required to login by SSH to remote host.
  44. if [ -z "$ACME_DEPLOY_SSH_USER" ]; then
  45. if [ -z "$Le_Deploy_ssh_user" ]; then
  46. _err "ACME_DEPLOY_SSH_USER not defined."
  47. return 1
  48. fi
  49. else
  50. Le_Deploy_ssh_user="$ACME_DEPLOY_SSH_USER"
  51. _savedomainconf Le_Deploy_ssh_user "$Le_Deploy_ssh_user"
  52. fi
  53. # SERVER is optional. If not provided then use _cdomain
  54. if [ -n "$ACME_DEPLOY_SSH_SERVER" ]; then
  55. Le_Deploy_ssh_server="$ACME_DEPLOY_SSH_SERVER"
  56. _savedomainconf Le_Deploy_ssh_server "$Le_Deploy_ssh_server"
  57. elif [ -z "$Le_Deploy_ssh_server" ]; then
  58. Le_Deploy_ssh_server="$_cdomain"
  59. fi
  60. # CMD is optional. If not provided then use ssh
  61. if [ -n "$ACME_DEPLOY_SSH_CMD" ]; then
  62. Le_Deploy_ssh_cmd="$ACME_DEPLOY_SSH_CMD"
  63. _savedomainconf Le_Deploy_ssh_cmd "$Le_Deploy_ssh_cmd"
  64. elif [ -z "$Le_Deploy_ssh_cmd" ]; then
  65. Le_Deploy_ssh_cmd="ssh"
  66. fi
  67. _info "Deploy certificates to remote server $Le_Deploy_ssh_user@$Le_Deploy_ssh_server"
  68. # KEYFILE is optional.
  69. # If provided then private key will be copied to provided filename.
  70. if [ -n "$ACME_DEPLOY_SSH_KEYFILE" ]; then
  71. Le_Deploy_ssh_keyfile="$ACME_DEPLOY_SSH_KEYFILE"
  72. _savedomainconf Le_Deploy_ssh_keyfile "$Le_Deploy_ssh_keyfile"
  73. fi
  74. if [ -n "$Le_Deploy_ssh_keyfile" ]; then
  75. # backup file we are about to overwrite.
  76. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_keyfile $_backupdir ;"
  77. # copy new certificate into file.
  78. _cmdstr="$_cmdstr echo \"$(cat "$_ckey")\" > $Le_Deploy_ssh_keyfile ;"
  79. _info "will copy private key to remote file $Le_Deploy_ssh_keyfile"
  80. fi
  81. # CERTFILE is optional.
  82. # If provided then private key will be copied or appended to provided filename.
  83. if [ -n "$ACME_DEPLOY_SSH_CERTFILE" ]; then
  84. Le_Deploy_ssh_certfile="$ACME_DEPLOY_SSH_CERTFILE"
  85. _savedomainconf Le_Deploy_ssh_certfile "$Le_Deploy_ssh_certfile"
  86. fi
  87. if [ -n "$Le_Deploy_ssh_certfile" ]; then
  88. if [ "$Le_Deploy_ssh_certfile" = "$Le_Deploy_ssh_keyfile" ]; then
  89. # if filename is same as previous file then append.
  90. _pipe=">>"
  91. else
  92. # backup file we are about to overwrite.
  93. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_certfile $_backupdir ;"
  94. _pipe=">"
  95. fi
  96. # copy new certificate into file.
  97. _cmdstr="$_cmdstr echo \"$(cat "$_ccert")\" $_pipe $Le_Deploy_ssh_certfile ;"
  98. _info "will copy certificate to remote file $Le_Deploy_ssh_certfile"
  99. fi
  100. # CAFILE is optional.
  101. # If provided then CA intermediate certificate will be copied or appended to provided filename.
  102. if [ -n "$ACME_DEPLOY_SSH_CAFILE" ]; then
  103. Le_Deploy_ssh_cafile="$ACME_DEPLOY_SSH_CAFILE"
  104. _savedomainconf Le_Deploy_ssh_cafile "$Le_Deploy_ssh_cafile"
  105. fi
  106. if [ -n "$Le_Deploy_ssh_cafile" ]; then
  107. if [ "$Le_Deploy_ssh_cafile" = "$Le_Deploy_ssh_keyfile" ] ||
  108. [ "$Le_Deploy_ssh_cafile" = "$Le_Deploy_ssh_certfile" ]; then
  109. # if filename is same as previous file then append.
  110. _pipe=">>"
  111. else
  112. # backup file we are about to overwrite.
  113. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_cafile $_backupdir ;"
  114. _pipe=">"
  115. fi
  116. # copy new certificate into file.
  117. _cmdstr="$_cmdstr echo \"$(cat "$_cca")\" $_pipe $Le_Deploy_ssh_cafile ;"
  118. _info "will copy CA file to remote file $Le_Deploy_ssh_cafile"
  119. fi
  120. # FULLCHAIN is optional.
  121. # If provided then fullchain certificate will be copied or appended to provided filename.
  122. if [ -n "$ACME_DEPLOY_SSH_FULLCHAIN" ]; then
  123. Le_Deploy_ssh_fullchain="$ACME_DEPLOY_SSH_FULLCHAIN"
  124. _savedomainconf Le_Deploy_ssh_fullchain "$Le_Deploy_ssh_fullchain"
  125. fi
  126. if [ -n "$Le_Deploy_ssh_fullchain" ]; then
  127. if [ "$Le_Deploy_ssh_fullchain" = "$Le_Deploy_ssh_keyfile" ] ||
  128. [ "$Le_Deploy_ssh_fullchain" = "$Le_Deploy_ssh_certfile" ] ||
  129. [ "$Le_Deploy_ssh_fullchain" = "$Le_Deploy_ssh_cafile" ]; then
  130. # if filename is same as previous file then append.
  131. _pipe=">>"
  132. else
  133. # backup file we are about to overwrite.
  134. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_fullchain $_backupdir ;"
  135. _pipe=">"
  136. fi
  137. # copy new certificate into file.
  138. _cmdstr="$_cmdstr echo \"$(cat "$_cfullchain")\" $_pipe $Le_Deploy_ssh_fullchain ;"
  139. _info "will copy fullchain to remote file $Le_Deploy_ssh_fullchain"
  140. fi
  141. # REMOTE_CMD is optional.
  142. # If provided then this command will be executed on remote host.
  143. if [ -n "$ACME_DEPLOY_SSH_REMOTE_CMD" ]; then
  144. Le_Deploy_ssh_remote_cmd="$ACME_DEPLOY_SSH_REMOTE_CMD"
  145. _savedomainconf Le_Deploy_ssh_remote_cmd "$Le_Deploy_ssh_remote_cmd"
  146. fi
  147. if [ -n "$Le_Deploy_ssh_remote_cmd" ]; then
  148. _cmdstr="$_cmdstr $Le_Deploy_ssh_remote_cmd ;"
  149. _info "Will execute remote command $Le_Deploy_ssh_remote_cmd"
  150. fi
  151. if [ -z "$_cmdstr" ]; then
  152. _err "No remote commands to excute. Failed to deploy certificates to remote server"
  153. return 1
  154. else
  155. # something to execute.
  156. # run cleanup on the backup directory, erase all older than 180 days.
  157. _cmdstr="find $_backupprefix* -type d -mtime +180 2>/dev/null | xargs rm -rf ; $_cmdstr"
  158. # Create our backup directory for overwritten cert files.
  159. _cmdstr="mkdir -p $_backupdir ; $_cmdstr"
  160. _info "Backup of old certificate files will be placed in remote directory $_backupdir"
  161. _info "Backup directories erased after 180 days."
  162. fi
  163. _debug "Remote commands to execute: $_cmdstr"
  164. _info "Submitting sequence of commands to remote server by ssh"
  165. # quotations in bash cmd below intended. Squash travis spellcheck error
  166. # shellcheck disable=SC2029
  167. $Le_Deploy_ssh_cmd -T "$Le_Deploy_ssh_user@$Le_Deploy_ssh_server" sh -c "'$_cmdstr'"
  168. _ret="$?"
  169. if [ "$_ret" != "0" ]; then
  170. _err "Error code $_ret returned from $Le_Deploy_ssh_cmd"
  171. fi
  172. return $_ret
  173. }