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.

267 lines
9.8 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
  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 or previously saved value
  23. # export DEPLOY_SSH_MULTI_CALL="" # yes or no, default to no or previously saved value
  24. #
  25. ######## Public functions #####################
  26. #domain keyfile certfile cafile fullchain
  27. ssh_deploy() {
  28. _cdomain="$1"
  29. _ckey="$2"
  30. _ccert="$3"
  31. _cca="$4"
  32. _cfullchain="$5"
  33. _err_code=0
  34. _cmdstr=""
  35. _homedir='~'
  36. _backupprefix="$_homedir/.acme_ssh_deploy/$_cdomain-backup"
  37. _backupdir="$_backupprefix-$(_utc_date | tr ' ' '-')"
  38. if [ -f "$DOMAIN_CONF" ]; then
  39. # shellcheck disable=SC1090
  40. . "$DOMAIN_CONF"
  41. fi
  42. _debug _cdomain "$_cdomain"
  43. _debug _ckey "$_ckey"
  44. _debug _ccert "$_ccert"
  45. _debug _cca "$_cca"
  46. _debug _cfullchain "$_cfullchain"
  47. # USER is required to login by SSH to remote host.
  48. if [ -z "$DEPLOY_SSH_USER" ]; then
  49. if [ -z "$Le_Deploy_ssh_user" ]; then
  50. _err "DEPLOY_SSH_USER not defined."
  51. return 1
  52. fi
  53. else
  54. Le_Deploy_ssh_user="$DEPLOY_SSH_USER"
  55. _savedomainconf Le_Deploy_ssh_user "$Le_Deploy_ssh_user"
  56. fi
  57. # SERVER is optional. If not provided then use _cdomain
  58. if [ -n "$DEPLOY_SSH_SERVER" ]; then
  59. Le_Deploy_ssh_server="$DEPLOY_SSH_SERVER"
  60. _savedomainconf Le_Deploy_ssh_server "$Le_Deploy_ssh_server"
  61. elif [ -z "$Le_Deploy_ssh_server" ]; then
  62. Le_Deploy_ssh_server="$_cdomain"
  63. fi
  64. # CMD is optional. If not provided then use ssh
  65. if [ -n "$DEPLOY_SSH_CMD" ]; then
  66. Le_Deploy_ssh_cmd="$DEPLOY_SSH_CMD"
  67. _savedomainconf Le_Deploy_ssh_cmd "$Le_Deploy_ssh_cmd"
  68. elif [ -z "$Le_Deploy_ssh_cmd" ]; then
  69. Le_Deploy_ssh_cmd="ssh -T"
  70. fi
  71. # BACKUP is optional. If not provided then default to previously saved value or yes.
  72. if [ "$DEPLOY_SSH_BACKUP" = "no" ]; then
  73. Le_Deploy_ssh_backup="no"
  74. elif [ -z "$Le_Deploy_ssh_backup" ] || [ "$DEPLOY_SSH_BACKUP" = "yes" ]; then
  75. Le_Deploy_ssh_backup="yes"
  76. fi
  77. _savedomainconf Le_Deploy_ssh_backup "$Le_Deploy_ssh_backup"
  78. # MULTI_CALL is optional. If not provided then default to previously saved
  79. # value (which may be undefined... equivalent to "no").
  80. if [ "$DEPLOY_SSH_MULTI_CALL" = "yes" ]; then
  81. Le_Deploy_ssh_multi_call="yes"
  82. _savedomainconf Le_Deploy_ssh_multi_call "$Le_Deploy_ssh_multi_call"
  83. elif [ "$DEPLOY_SSH_MULTI_CALL" = "no" ]; then
  84. Le_Deploy_ssh_multi_call=""
  85. _cleardomainconf Le_Deploy_ssh_multi_call
  86. fi
  87. _info "Deploy certificates to remote server $Le_Deploy_ssh_user@$Le_Deploy_ssh_server"
  88. if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
  89. _info "Using MULTI_CALL mode... Required commands sent in multiple calls to remote host"
  90. else
  91. _info "Required commands batched and sent in single call to remote host"
  92. fi
  93. if [ "$Le_Deploy_ssh_backup" = "yes" ]; then
  94. # run cleanup on the backup directory, erase all older
  95. # than 180 days (15552000 seconds).
  96. _cmdstr="{ now=\"\$(date -u +%s)\"; for fn in $_backupprefix*; \
  97. do if [ -d \"\$fn\" ] && [ \"\$(expr \$now - \$(date -ur \$fn +%s) )\" -ge \"15552000\" ]; \
  98. then rm -rf \"\$fn\"; echo \"Backup \$fn deleted as older than 180 days\"; fi; done; }; $_cmdstr"
  99. # Alternate version of above... _cmdstr="find $_backupprefix* -type d -mtime +180 2>/dev/null | xargs rm -rf; $_cmdstr"
  100. # Create our backup directory for overwritten cert files.
  101. _cmdstr="mkdir -p $_backupdir; $_cmdstr"
  102. _info "Backup of old certificate files will be placed in remote directory $_backupdir"
  103. _info "Backup directories erased after 180 days."
  104. if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
  105. if ! _ssh_remote_cmd "$_cmdstr"; then
  106. return $_err_code
  107. fi
  108. _cmdstr=""
  109. fi
  110. fi
  111. # KEYFILE is optional.
  112. # If provided then private key will be copied to provided filename.
  113. if [ -n "$DEPLOY_SSH_KEYFILE" ]; then
  114. Le_Deploy_ssh_keyfile="$DEPLOY_SSH_KEYFILE"
  115. _savedomainconf Le_Deploy_ssh_keyfile "$Le_Deploy_ssh_keyfile"
  116. fi
  117. if [ -n "$Le_Deploy_ssh_keyfile" ]; then
  118. if [ "$Le_Deploy_ssh_backup" = "yes" ]; then
  119. # backup file we are about to overwrite.
  120. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_keyfile $_backupdir >/dev/null;"
  121. fi
  122. # copy new certificate into file.
  123. _cmdstr="$_cmdstr echo \"$(cat "$_ckey")\" > $Le_Deploy_ssh_keyfile;"
  124. _info "will copy private key to remote file $Le_Deploy_ssh_keyfile"
  125. if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
  126. if ! _ssh_remote_cmd "$_cmdstr"; then
  127. return $_err_code
  128. fi
  129. _cmdstr=""
  130. fi
  131. fi
  132. # CERTFILE is optional.
  133. # If provided then certificate will be copied or appended to provided filename.
  134. if [ -n "$DEPLOY_SSH_CERTFILE" ]; then
  135. Le_Deploy_ssh_certfile="$DEPLOY_SSH_CERTFILE"
  136. _savedomainconf Le_Deploy_ssh_certfile "$Le_Deploy_ssh_certfile"
  137. fi
  138. if [ -n "$Le_Deploy_ssh_certfile" ]; then
  139. _pipe=">"
  140. if [ "$Le_Deploy_ssh_certfile" = "$Le_Deploy_ssh_keyfile" ]; then
  141. # if filename is same as previous file then append.
  142. _pipe=">>"
  143. elif [ "$Le_Deploy_ssh_backup" = "yes" ]; then
  144. # backup file we are about to overwrite.
  145. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_certfile $_backupdir >/dev/null;"
  146. fi
  147. # copy new certificate into file.
  148. _cmdstr="$_cmdstr echo \"$(cat "$_ccert")\" $_pipe $Le_Deploy_ssh_certfile;"
  149. _info "will copy certificate to remote file $Le_Deploy_ssh_certfile"
  150. if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
  151. if ! _ssh_remote_cmd "$_cmdstr"; then
  152. return $_err_code
  153. fi
  154. _cmdstr=""
  155. fi
  156. fi
  157. # CAFILE is optional.
  158. # If provided then CA intermediate certificate will be copied or appended to provided filename.
  159. if [ -n "$DEPLOY_SSH_CAFILE" ]; then
  160. Le_Deploy_ssh_cafile="$DEPLOY_SSH_CAFILE"
  161. _savedomainconf Le_Deploy_ssh_cafile "$Le_Deploy_ssh_cafile"
  162. fi
  163. if [ -n "$Le_Deploy_ssh_cafile" ]; then
  164. _pipe=">"
  165. if [ "$Le_Deploy_ssh_cafile" = "$Le_Deploy_ssh_keyfile" ] \
  166. || [ "$Le_Deploy_ssh_cafile" = "$Le_Deploy_ssh_certfile" ]; then
  167. # if filename is same as previous file then append.
  168. _pipe=">>"
  169. elif [ "$Le_Deploy_ssh_backup" = "yes" ]; then
  170. # backup file we are about to overwrite.
  171. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_cafile $_backupdir >/dev/null;"
  172. fi
  173. # copy new certificate into file.
  174. _cmdstr="$_cmdstr echo \"$(cat "$_cca")\" $_pipe $Le_Deploy_ssh_cafile;"
  175. _info "will copy CA file to remote file $Le_Deploy_ssh_cafile"
  176. if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
  177. if ! _ssh_remote_cmd "$_cmdstr"; then
  178. return $_err_code
  179. fi
  180. _cmdstr=""
  181. fi
  182. fi
  183. # FULLCHAIN is optional.
  184. # If provided then fullchain certificate will be copied or appended to provided filename.
  185. if [ -n "$DEPLOY_SSH_FULLCHAIN" ]; then
  186. Le_Deploy_ssh_fullchain="$DEPLOY_SSH_FULLCHAIN"
  187. _savedomainconf Le_Deploy_ssh_fullchain "$Le_Deploy_ssh_fullchain"
  188. fi
  189. if [ -n "$Le_Deploy_ssh_fullchain" ]; then
  190. _pipe=">"
  191. if [ "$Le_Deploy_ssh_fullchain" = "$Le_Deploy_ssh_keyfile" ] \
  192. || [ "$Le_Deploy_ssh_fullchain" = "$Le_Deploy_ssh_certfile" ] \
  193. || [ "$Le_Deploy_ssh_fullchain" = "$Le_Deploy_ssh_cafile" ]; then
  194. # if filename is same as previous file then append.
  195. _pipe=">>"
  196. elif [ "$Le_Deploy_ssh_backup" = "yes" ]; then
  197. # backup file we are about to overwrite.
  198. _cmdstr="$_cmdstr cp $Le_Deploy_ssh_fullchain $_backupdir >/dev/null;"
  199. fi
  200. # copy new certificate into file.
  201. _cmdstr="$_cmdstr echo \"$(cat "$_cfullchain")\" $_pipe $Le_Deploy_ssh_fullchain;"
  202. _info "will copy fullchain to remote file $Le_Deploy_ssh_fullchain"
  203. if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
  204. if ! _ssh_remote_cmd "$_cmdstr"; then
  205. return $_err_code
  206. fi
  207. _cmdstr=""
  208. fi
  209. fi
  210. # REMOTE_CMD is optional.
  211. # If provided then this command will be executed on remote host.
  212. if [ -n "$DEPLOY_SSH_REMOTE_CMD" ]; then
  213. Le_Deploy_ssh_remote_cmd="$DEPLOY_SSH_REMOTE_CMD"
  214. _savedomainconf Le_Deploy_ssh_remote_cmd "$Le_Deploy_ssh_remote_cmd"
  215. fi
  216. if [ -n "$Le_Deploy_ssh_remote_cmd" ]; then
  217. _cmdstr="$_cmdstr $Le_Deploy_ssh_remote_cmd;"
  218. _info "Will execute remote command $Le_Deploy_ssh_remote_cmd"
  219. if [ "$Le_Deploy_ssh_multi_call" = "yes" ]; then
  220. if ! _ssh_remote_cmd "$_cmdstr"; then
  221. return $_err_code
  222. fi
  223. _cmdstr=""
  224. fi
  225. fi
  226. # if commands not all sent in multiple calls then all commands sent in a single SSH call now...
  227. if [ -n "$_cmdstr" ]; then
  228. if ! _ssh_remote_cmd "$_cmdstr"; then
  229. return $_err_code
  230. fi
  231. fi
  232. return 0
  233. }
  234. #cmd
  235. _ssh_remote_cmd() {
  236. _cmd="$1"
  237. _secure_debug "Remote commands to execute: $_cmd"
  238. _info "Submitting sequence of commands to remote server by ssh"
  239. # quotations in bash cmd below intended. Squash travis spellcheck error
  240. # shellcheck disable=SC2029
  241. $Le_Deploy_ssh_cmd "$Le_Deploy_ssh_user@$Le_Deploy_ssh_server" sh -c "'$_cmd'"
  242. _err_code="$?"
  243. if [ "$_err_code" != "0" ]; then
  244. _err "Error code $_err_code returned from ssh"
  245. fi
  246. return $_err_code
  247. }