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.

215 lines
8.1 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
  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 DEPLOY_SSH_CMD="" # defaults to "ssh -T"
  15. # export DEPLOY_SSH_USER="admin" # required
  16. # export DEPLOY_SSH_SERVER="qnap" # defaults to domain name
  17. # export DEPLOY_SSH_KEYFILE="/etc/stunnel/stunnel.pem"
  18. # export DEPLOY_SSH_CERTFILE="/etc/stunnel/stunnel.pem"
  19. # export DEPLOY_SSH_CAFILE="/etc/stunnel/uca.pem"
  20. # export DEPLOY_SSH_FULLCHAIN=""
  21. # export DEPLOY_SSH_REMOTE_CMD="/etc/init.d/stunnel.sh restart"
  22. # export DEPLOY_SSH_BACKUP="" # yes or no, default to yes
  23. #
  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. _err_code=0
  33. _cmdstr=""
  34. _homedir='~'
  35. _backupprefix="$_homedir/.acme_ssh_deploy/$_cdomain-backup"
  36. _backupdir="$_backupprefix-$(_utc_date | tr ' ' '-')"
  37. if [ -f "$DOMAIN_CONF" ]; then
  38. # shellcheck disable=SC1090
  39. . "$DOMAIN_CONF"
  40. fi
  41. _debug _cdomain "$_cdomain"
  42. _debug _ckey "$_ckey"
  43. _debug _ccert "$_ccert"
  44. _debug _cca "$_cca"
  45. _debug _cfullchain "$_cfullchain"
  46. # USER is required to login by SSH to remote host.
  47. if [ -z "$DEPLOY_SSH_USER" ]; then
  48. if [ -z "$Le_Deploy_ssh_user" ]; then
  49. _err "DEPLOY_SSH_USER not defined."
  50. return 1
  51. fi
  52. else
  53. Le_Deploy_ssh_user="$DEPLOY_SSH_USER"
  54. _savedomainconf Le_Deploy_ssh_user "$Le_Deploy_ssh_user"
  55. fi
  56. # SERVER is optional. If not provided then use _cdomain
  57. if [ -n "$DEPLOY_SSH_SERVER" ]; then
  58. Le_Deploy_ssh_server="$DEPLOY_SSH_SERVER"
  59. _savedomainconf Le_Deploy_ssh_server "$Le_Deploy_ssh_server"
  60. elif [ -z "$Le_Deploy_ssh_server" ]; then
  61. Le_Deploy_ssh_server="$_cdomain"
  62. fi
  63. # CMD is optional. If not provided then use ssh
  64. if [ -n "$DEPLOY_SSH_CMD" ]; then
  65. Le_Deploy_ssh_cmd="$DEPLOY_SSH_CMD"
  66. _savedomainconf Le_Deploy_ssh_cmd "$Le_Deploy_ssh_cmd"
  67. elif [ -z "$Le_Deploy_ssh_cmd" ]; then
  68. Le_Deploy_ssh_cmd="ssh -T"
  69. fi
  70. # BACKUP is optional. If not provided then default to yes
  71. if [ "$DEPLOY_SSH_BACKUP" = "no" ]; then
  72. Le_Deploy_ssh_backup="no"
  73. elif [ -z "$Le_Deploy_ssh_backup" ]; then
  74. Le_Deploy_ssh_backup="yes"
  75. fi
  76. _savedomainconf Le_Deploy_ssh_backup "$Le_Deploy_ssh_backup"
  77. _info "Deploy certificates to remote server $Le_Deploy_ssh_user@$Le_Deploy_ssh_server"
  78. # KEYFILE is optional.
  79. # If provided then private key will be copied to provided filename.
  80. if [ -n "$DEPLOY_SSH_KEYFILE" ]; then
  81. Le_Deploy_ssh_keyfile="$DEPLOY_SSH_KEYFILE"
  82. _savedomainconf Le_Deploy_ssh_keyfile "$Le_Deploy_ssh_keyfile"
  83. fi
  84. if [ -n "$Le_Deploy_ssh_keyfile" ]; then
  85. if [ "$Le_Deploy_ssh_backup" = "yes" ]; then
  86. # backup file we are about to overwrite.
  87. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_keyfile $_backupdir >/dev/null;"
  88. fi
  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 certificate will be copied or appended to provided filename.
  95. if [ -n "$DEPLOY_SSH_CERTFILE" ]; then
  96. Le_Deploy_ssh_certfile="$DEPLOY_SSH_CERTFILE"
  97. _savedomainconf Le_Deploy_ssh_certfile "$Le_Deploy_ssh_certfile"
  98. fi
  99. if [ -n "$Le_Deploy_ssh_certfile" ]; then
  100. _pipe=">"
  101. if [ "$Le_Deploy_ssh_certfile" = "$Le_Deploy_ssh_keyfile" ]; then
  102. # if filename is same as previous file then append.
  103. _pipe=">>"
  104. elif [ "$Le_Deploy_ssh_backup" = "yes" ]; then
  105. # backup file we are about to overwrite.
  106. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_certfile $_backupdir >/dev/null;"
  107. fi
  108. # copy new certificate into file.
  109. _cmdstr="$_cmdstr echo \"$(cat "$_ccert")\" $_pipe $Le_Deploy_ssh_certfile;"
  110. _info "will copy certificate to remote file $Le_Deploy_ssh_certfile"
  111. fi
  112. # CAFILE is optional.
  113. # If provided then CA intermediate certificate will be copied or appended to provided filename.
  114. if [ -n "$DEPLOY_SSH_CAFILE" ]; then
  115. Le_Deploy_ssh_cafile="$DEPLOY_SSH_CAFILE"
  116. _savedomainconf Le_Deploy_ssh_cafile "$Le_Deploy_ssh_cafile"
  117. fi
  118. if [ -n "$Le_Deploy_ssh_cafile" ]; then
  119. _pipe=">"
  120. if [ "$Le_Deploy_ssh_cafile" = "$Le_Deploy_ssh_keyfile" ] \
  121. || [ "$Le_Deploy_ssh_cafile" = "$Le_Deploy_ssh_certfile" ]; then
  122. # if filename is same as previous file then append.
  123. _pipe=">>"
  124. elif [ "$Le_Deploy_ssh_backup" = "yes" ]; then
  125. # backup file we are about to overwrite.
  126. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_cafile $_backupdir >/dev/null;"
  127. fi
  128. # copy new certificate into file.
  129. _cmdstr="$_cmdstr echo \"$(cat "$_cca")\" $_pipe $Le_Deploy_ssh_cafile;"
  130. _info "will copy CA file to remote file $Le_Deploy_ssh_cafile"
  131. fi
  132. # FULLCHAIN is optional.
  133. # If provided then fullchain certificate will be copied or appended to provided filename.
  134. if [ -n "$DEPLOY_SSH_FULLCHAIN" ]; then
  135. Le_Deploy_ssh_fullchain="$DEPLOY_SSH_FULLCHAIN"
  136. _savedomainconf Le_Deploy_ssh_fullchain "$Le_Deploy_ssh_fullchain"
  137. fi
  138. if [ -n "$Le_Deploy_ssh_fullchain" ]; then
  139. _pipe=">"
  140. if [ "$Le_Deploy_ssh_fullchain" = "$Le_Deploy_ssh_keyfile" ] \
  141. || [ "$Le_Deploy_ssh_fullchain" = "$Le_Deploy_ssh_certfile" ] \
  142. || [ "$Le_Deploy_ssh_fullchain" = "$Le_Deploy_ssh_cafile" ]; then
  143. # if filename is same as previous file then append.
  144. _pipe=">>"
  145. elif [ "$Le_Deploy_ssh_backup" = "yes" ]; then
  146. # backup file we are about to overwrite.
  147. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_fullchain $_backupdir >/dev/null;"
  148. fi
  149. # copy new certificate into file.
  150. _cmdstr="$_cmdstr echo \"$(cat "$_cfullchain")\" $_pipe $Le_Deploy_ssh_fullchain;"
  151. _info "will copy fullchain to remote file $Le_Deploy_ssh_fullchain"
  152. fi
  153. # REMOTE_CMD is optional.
  154. # If provided then this command will be executed on remote host.
  155. if [ -n "$DEPLOY_SSH_REMOTE_CMD" ]; then
  156. Le_Deploy_ssh_remote_cmd="$DEPLOY_SSH_REMOTE_CMD"
  157. _savedomainconf Le_Deploy_ssh_remote_cmd "$Le_Deploy_ssh_remote_cmd"
  158. fi
  159. if [ -n "$Le_Deploy_ssh_remote_cmd" ]; then
  160. _cmdstr="$_cmdstr $Le_Deploy_ssh_remote_cmd;"
  161. _info "Will execute remote command $Le_Deploy_ssh_remote_cmd"
  162. fi
  163. if [ -z "$_cmdstr" ]; then
  164. _err "No remote commands to excute. Failed to deploy certificates to remote server"
  165. return 1
  166. elif [ "$Le_Deploy_ssh_backup" = "yes" ]; then
  167. # run cleanup on the backup directory, erase all older
  168. # than 180 days (15552000 seconds).
  169. _cmdstr="{ now=\"\$(date -u +%s)\"; for fn in $_backupprefix*; \
  170. do if [ -d \"\$fn\" ] && [ \"\$(expr \$now - \$(date -ur \$fn +%s) )\" -ge \"15552000\" ]; \
  171. then rm -rf \"\$fn\"; echo \"Backup \$fn deleted as older than 180 days\"; fi; done; }; $_cmdstr"
  172. # Alternate version of above... _cmdstr="find $_backupprefix* -type d -mtime +180 2>/dev/null | xargs rm -rf; $_cmdstr"
  173. # Create our backup directory for overwritten cert files.
  174. _cmdstr="mkdir -p $_backupdir; $_cmdstr"
  175. _info "Backup of old certificate files will be placed in remote directory $_backupdir"
  176. _info "Backup directories erased after 180 days."
  177. fi
  178. if ! _ssh_remote_cmd "$_cmdstr"; then
  179. return $_err_code
  180. fi
  181. return 0
  182. }
  183. #cmd
  184. _ssh_remote_cmd() {
  185. _secure_debug "Remote commands to execute: $_cmd"
  186. _info "Submitting sequence of commands to remote server by $Le_Deploy_ssh_cmd"
  187. # quotations in bash cmd below intended. Squash travis spellcheck error
  188. # shellcheck disable=SC2029
  189. $Le_Deploy_ssh_cmd "$Le_Deploy_ssh_user@$Le_Deploy_ssh_server" sh -c "'$_cmd'"
  190. _err_code="$?"
  191. if [ "$_err_code" != "0" ]; then
  192. _err "Error code $_err_code returned from $Le_Deploy_ssh_cmd"
  193. fi
  194. return $_err_code
  195. }