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.

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