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