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.

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