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.

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