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.

196 lines
7.6 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
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@URL 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_USER="admin"
  15. # export ACME_DEPLOY_SSH_URL="qnap"
  16. # export ACME_DEPLOY_SSH_SERVICE_STOP=""
  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. # export ACME_DEPLOY_SSH_SERVICE_START=""
  23. ######## Public functions #####################
  24. #domain keyfile certfile cafile fullchain
  25. sshdeploy_deploy() {
  26. _cdomain="$1"
  27. _ckey="$2"
  28. _ccert="$3"
  29. _cca="$4"
  30. _cfullchain="$5"
  31. _cmdstr=""
  32. _homedir='~'
  33. _homedir="$_homedir/.acme_ssh_deploy"
  34. _backupdir="$_homedir/certs-backup-$(date +%Y%m%d%H%M%S)"
  35. if [ -f "$DOMAIN_CONF" ]; then
  36. # shellcheck disable=SC1090
  37. . "$DOMAIN_CONF"
  38. fi
  39. _debug _cdomain "$_cdomain"
  40. _debug _ckey "$_ckey"
  41. _debug _ccert "$_ccert"
  42. _debug _cca "$_cca"
  43. _debug _cfullchain "$_cfullchain"
  44. # USER is required to login by SSH to remote host.
  45. if [ -z "$ACME_DEPLOY_SSH_USER" ]; then
  46. if [ -z "$Le_Deploy_ssh_user" ]; then
  47. _err "ACME_DEPLOY_SSH_USER not defined."
  48. return 1
  49. fi
  50. else
  51. Le_Deploy_ssh_user="$ACME_DEPLOY_SSH_USER"
  52. _savedomainconf Le_Deploy_ssh_user "$Le_Deploy_ssh_user"
  53. fi
  54. # URL is optional. If not provided then use _cdomain
  55. if [ -n "$ACME_DEPLOY_SSH_URL" ]; then
  56. Le_Deploy_ssh_url="$ACME_DEPLOY_SSH_URL"
  57. _savedomainconf Le_Deploy_ssh_url "$Le_Deploy_ssh_url"
  58. elif [ -z "$Le_Deploy_ssh_url" ]; then
  59. Le_Deploy_ssh_url="$_cdomain"
  60. fi
  61. _info "Deploy certificates to remote server $Le_Deploy_ssh_user@$Le_Deploy_ssh_url"
  62. # SERVICE_STOP is optional.
  63. # If provided then this command will be executed on remote host.
  64. if [ -n "$ACME_DEPLOY_SSH_SERVICE_STOP" ]; then
  65. Le_Deploy_ssh_service_stop="$ACME_DEPLOY_SSH_SERVICE_STOP"
  66. _savedomainconf Le_Deploy_ssh_service_stop "$Le_Deploy_ssh_service_stop"
  67. fi
  68. if [ -n "$Le_Deploy_ssh_service_stop" ]; then
  69. _cmdstr="$_cmdstr $Le_Deploy_ssh_service_stop ;"
  70. _info "Will stop remote service with command $Le_Deploy_ssh_service_stop"
  71. fi
  72. # KEYFILE is optional.
  73. # If provided then private key will be copied to provided filename.
  74. if [ -n "$ACME_DEPLOY_SSH_KEYFILE" ]; then
  75. Le_Deploy_ssh_keyfile="$ACME_DEPLOY_SSH_KEYFILE"
  76. _savedomainconf Le_Deploy_ssh_keyfile "$Le_Deploy_ssh_keyfile"
  77. fi
  78. if [ -n "$Le_Deploy_ssh_keyfile" ]; then
  79. # backup file we are about to overwrite.
  80. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_keyfile $_backupdir ;"
  81. # copy new certificate into file.
  82. _cmdstr="$_cmdstr echo \"$(cat "$_ckey")\" > $Le_Deploy_ssh_keyfile ;"
  83. _info "will copy private key to remote file $Le_Deploy_ssh_keyfile"
  84. fi
  85. # CERTFILE is optional.
  86. # If provided then private key will be copied or appended to provided filename.
  87. if [ -n "$ACME_DEPLOY_SSH_CERTFILE" ]; then
  88. Le_Deploy_ssh_certfile="$ACME_DEPLOY_SSH_CERTFILE"
  89. _savedomainconf Le_Deploy_ssh_certfile "$Le_Deploy_ssh_certfile"
  90. fi
  91. if [ -n "$Le_Deploy_ssh_certfile" ]; then
  92. if [ "$Le_Deploy_ssh_certfile" = "$Le_Deploy_ssh_keyfile" ]; then
  93. # if filename is same as that provided for private key then append.
  94. _cmdstr="$_cmdstr echo \"$(cat "$_ccert")\" >> $Le_Deploy_ssh_certfile ;"
  95. _info "will append certificate to same file"
  96. else
  97. # backup file we are about to overwrite.
  98. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_certfile $_backupdir ;"
  99. # copy new certificate into file.
  100. _cmdstr="$_cmdstr echo \"$(cat "$_ccert")\" > $Le_Deploy_ssh_certfile ;"
  101. _info "will copy certificate to remote file $Le_Deploy_ssh_certfile"
  102. fi
  103. fi
  104. # CAFILE is optional.
  105. # If provided then CA intermediate certificate will be copied to provided filename.
  106. if [ -n "$ACME_DEPLOY_SSH_CAFILE" ]; then
  107. Le_Deploy_ssh_cafile="$ACME_DEPLOY_SSH_CAFILE"
  108. _savedomainconf Le_Deploy_ssh_cafile "$Le_Deploy_ssh_cafile"
  109. fi
  110. if [ -n "$Le_Deploy_ssh_cafile" ]; then
  111. # backup file we are about to overwrite.
  112. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_cafile $_backupdir ;"
  113. # copy new certificate into file.
  114. _cmdstr="$_cmdstr echo \"$(cat "$_cca")\" > $Le_Deploy_ssh_cafile ;"
  115. _info "will copy CA file to remote file $Le_Deploy_ssh_cafile"
  116. fi
  117. # FULLCHAIN is optional.
  118. # If provided then fullchain certificate will be copied to provided filename.
  119. if [ -n "$ACME_DEPLOY_SSH_FULLCHAIN" ]; then
  120. Le_Deploy_ssh_fullchain="$ACME_DEPLOY_SSH_FULLCHAIN"
  121. _savedomainconf Le_Deploy_ssh_fullchain "$Le_Deploy_ssh_fullchain"
  122. fi
  123. if [ -n "$Le_Deploy_ssh_fullchain" ]; then
  124. # backup file we are about to overwrite.
  125. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_fullchain $_backupdir ;"
  126. # copy new certificate into file.
  127. _cmdstr="$_cmdstr echo \"$(cat "$_cfullchain")\" > $Le_Deploy_ssh_fullchain ;"
  128. _info "will copy full chain to remote file $Le_Deploy_ssh_fullchain"
  129. fi
  130. # REMOTE_CMD is optional.
  131. # If provided then this command will be executed on remote host.
  132. # A 2 second delay is inserted to allow system to stabalize after
  133. # executing a service stop.
  134. if [ -n "$ACME_DEPLOY_SSH_REMOTE_CMD" ]; then
  135. Le_Deploy_ssh_remote_cmd="$ACME_DEPLOY_SSH_REMOTE_CMD"
  136. _savedomainconf Le_Deploy_ssh_remote_cmd "$Le_Deploy_ssh_remote_cmd"
  137. fi
  138. if [ -n "$Le_Deploy_ssh_remote_cmd" ]; then
  139. if [ -n "$Le_Deploy_ssh_service_stop" ]; then
  140. _cmdstr="$_cmdstr sleep 2 ;"
  141. fi
  142. _cmdstr="$_cmdstr $Le_Deploy_ssh_remote_cmd ;"
  143. _info "Will execute remote command $Le_Deploy_ssh_remote_cmd"
  144. fi
  145. # SERVICE_START is optional.
  146. # If provided then this command will be executed on remote host.
  147. # A 2 second delay is inserted to allow system to stabalize after
  148. # executing a service stop or previous command.
  149. if [ -n "$ACME_DEPLOY_SSH_SERVICE_START" ]; then
  150. Le_Deploy_ssh_service_start="$ACME_DEPLOY_SSH_SERVICE_START"
  151. _savedomainconf Le_Deploy_ssh_service_start "$Le_Deploy_ssh_service_start"
  152. fi
  153. if [ -n "$Le_Deploy_ssh_service_start" ]; then
  154. if [ -n "$Le_Deploy_ssh_service_stop" ] || [ -n "$Le_Deploy_ssh_remote_cmd" ]; then
  155. _cmdstr="$_cmdstr sleep 2 ;"
  156. fi
  157. _cmdstr="$_cmdstr $Le_Deploy_ssh_service_start ;"
  158. _info "Will start remote service with command $Le_Deploy_ssh_remote_cmd"
  159. fi
  160. if [ -z "$_cmdstr" ]; then
  161. _err "No remote commands to excute. Failed to deploy certificates to remote server"
  162. return 1
  163. else
  164. # something to execute.
  165. # run cleanup on the backup directory, erase all older than 180 days.
  166. _cmdstr="find $_homedir/* -type d -mtime +180 2>/dev/null | xargs rm -rf ; $_cmdstr"
  167. # Create our backup directory for overwritten cert files.
  168. _cmdstr="mkdir -p $_backupdir ; $_cmdstr"
  169. _info "Backup of old certificate files will be placed in remote directory $_backupdir"
  170. _info "Backup directories erased after 180 days."
  171. fi
  172. _debug "Remote commands to execute: $_cmdstr"
  173. _info "Submitting sequence of commands to remote server by ssh"
  174. # quotations in bash cmd below intended. Squash travis spellcheck error
  175. # shellcheck disable=SC2029
  176. ssh -T "$Le_Deploy_ssh_user@$Le_Deploy_ssh_url" bash -c "'$_cmdstr'"
  177. return 0
  178. }