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.

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