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.

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