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.

128 lines
4.5 KiB

8 years ago
  1. #!/usr/bin/env sh
  2. #Here is a script to deploy certificates to remote server by ssh
  3. #This file name is "sshdeploy.sh"
  4. #So, here must be a method sshdeploy_deploy()
  5. #Which will be called by acme.sh to deploy the cert
  6. #returns 0 means success, otherwise error.
  7. # The following variables exported from environment will be used.
  8. # If not set then values previously saved in domain.conf file are used.
  9. #
  10. # export ACME_DEPLOY_SSH_URL="admin@qnap"
  11. # export ACME_DEPLOY_SSH_SERVICE_STOP="/etc/init.d/stunnel.sh stop"
  12. # export ACME_DEPLOY_SSH_KEYFILE="/etc/stunnel/stunnel.pem"
  13. # export ACME_DEPLOY_SSH_CERTFILE="/etc/stunnel/stunnel.pem"
  14. # export ACME_DEPLOY_SSH_CAFILE="/etc/stunnel/uca.pem"
  15. # export ACME_DEPLOY_SSH_FULLCHAIN=""
  16. # export ACME_DEPLOY_SSH_REMOTE_CMD="/etc/init.d/stunnel.sh restart"
  17. # export ACME_DEPLOY_SSH_SERVICE_START="/etc/init.d/stunnel.sh stop"
  18. . "$DOMAIN_CONF"
  19. ######## Public functions #####################
  20. #domain keyfile certfile cafile fullchain
  21. sshdeploy_deploy() {
  22. _cdomain="$1"
  23. _ckey="$2"
  24. _ccert="$3"
  25. _cca="$4"
  26. _cfullchain="$5"
  27. _cmdstr="{"
  28. _debug _cdomain "$_cdomain"
  29. _debug _ckey "$_ckey"
  30. _debug _ccert "$_ccert"
  31. _debug _cca "$_cca"
  32. _debug _cfullchain "$_cfullchain"
  33. if [ -z "$ACME_DEPLOY_SSH_URL" ]; then
  34. if [ -z "$Le_Deploy_ssh_url" ]; then
  35. _err "ACME_DEPLOY_SSH_URL not defined."
  36. return 1
  37. fi
  38. else
  39. Le_Deploy_ssh_url="$ACME_DEPLOY_SSH_URL"
  40. _savedomainconf Le_Deploy_ssh_url "$Le_Deploy_ssh_url"
  41. fi
  42. _info "Deploy certificates to remote server $Le_Deploy_ssh_url"
  43. if [ -n "$ACME_DEPLOY_SSH_SERVICE_STOP" ]; then
  44. Le_Deploy_ssh_service_stop="$ACME_DEPLOY_SSH_SERVICE_STOP"
  45. _savedomainconf Le_Deploy_ssh_service_stop "$Le_Deploy_ssh_service_stop"
  46. fi
  47. if [ -n "$Le_Deploy_ssh_service_stop" ]; then
  48. _cmdstr="$_cmdstr $Le_Deploy_ssh_service_stop ;"
  49. _info "Will stop remote service with command $Le_Deploy_ssh_service_stop"
  50. fi
  51. if [ -n "$ACME_DEPLOY_SSH_KEYFILE" ]; then
  52. Le_Deploy_ssh_keyfile="$ACME_DEPLOY_SSH_KEYFILE"
  53. _savedomainconf Le_Deploy_ssh_keyfile "$Le_Deploy_ssh_keyfile"
  54. fi
  55. if [ -n "$Le_Deploy_ssh_keyfile" ]; then
  56. _cmdstr="$_cmdstr echo \"$(cat $_ckey)\" > $Le_Deploy_ssh_keyfile ;"
  57. _info "will copy private key to remote file $Le_Deploy_ssh_keyfile"
  58. fi
  59. if [ -n "$ACME_DEPLOY_SSH_CERTFILE" ]; then
  60. Le_Deploy_ssh_certfile="$ACME_DEPLOY_SSH_CERTFILE"
  61. _savedomainconf Le_Deploy_ssh_certfile "$Le_Deploy_ssh_certfile"
  62. fi
  63. if [ -n "$Le_Deploy_ssh_certfile" ]; then
  64. if [ "$Le_Deploy_ssh_certfile" = "$Le_Deploy_ssh_keyfile" ]; then
  65. _cmdstr="$_cmdstr echo \"$(cat $_ccert)\" >> $Le_Deploy_ssh_certfile ;"
  66. _info "will append certificate to same file"
  67. else
  68. _cmdstr="$_cmdstr echo \"$(cat $_ccert)\" > $Le_Deploy_ssh_certfile ;"
  69. _info "will copy certificate to remote file $Le_Deploy_ssh_certfile"
  70. fi
  71. fi
  72. if [ -n "$ACME_DEPLOY_SSH_CAFILE" ]; then
  73. Le_Deploy_ssh_cafile="$ACME_DEPLOY_SSH_CAFILE"
  74. _savedomainconf Le_Deploy_ssh_cafile "$Le_Deploy_ssh_cafile"
  75. fi
  76. if [ -n "$Le_Deploy_ssh_cafile" ]; then
  77. _cmdstr="$_cmdstr echo \"$(cat $_cca)\" > $Le_Deploy_ssh_cafile ;"
  78. _info "will copy CA file to remote file $Le_Deploy_ssh_cafile"
  79. fi
  80. if [ -n "$ACME_DEPLOY_SSH_FULLCHAIN" ]; then
  81. Le_Deploy_ssh_fullchain="$ACME_DEPLOY_SSH_FULLCHAIN"
  82. _savedomainconf Le_Deploy_ssh_fullchain "$Le_Deploy_ssh_fullchain"
  83. fi
  84. if [ -n "$Le_Deploy_ssh_fullchain" ]; then
  85. _cmdstr="$_cmdstr echo \"$(cat $_cfullchain)\" > $Le_Deploy_ssh_fullchain ;"
  86. _info "will copy full chain to remote file $Le_Deploy_ssh_fullchain"
  87. fi
  88. if [ -n "$ACME_DEPLOY_SSH_REMOTE_CMD" ]; then
  89. Le_Deploy_ssh_remote_cmd="$ACME_DEPLOY_SSH_REMOTE_CMD"
  90. _savedomainconf Le_Deploy_ssh_remote_cmd "$Le_Deploy_ssh_remote_cmd"
  91. fi
  92. if [ -n "$Le_Deploy_ssh_remote_cmd" ]; then
  93. _cmdstr="$_cmdstr sleep 2 ; $Le_Deploy_ssh_remote_cmd ;"
  94. _info "Will sleep 2 seconds then execute remote command $Le_Deploy_ssh_remote_cmd"
  95. fi
  96. if [ -n "$ACME_DEPLOY_SSH_SERVICE_START" ]; then
  97. Le_Deploy_ssh_service_start="$ACME_DEPLOY_SSH_SERVICE_START"
  98. _savedomainconf Le_Deploy_ssh_service_start "$Le_Deploy_ssh_service_start"
  99. fi
  100. if [ -n "$Le_Deploy_ssh_service_start" ]; then
  101. _cmdstr="$_cmdstr sleep 2 ; $Le_Deploy_ssh_service_start ;"
  102. _info "Will sleep 2 seconds then start remote service with command $Le_Deploy_ssh_remote_cmd"
  103. fi
  104. _cmdstr="$_cmdstr }"
  105. _debug "Remote command to execute: $_cmdstr"
  106. _info "Submitting sequence of commands to remote server by ssh"
  107. ssh -T "$Le_Deploy_ssh_url" bash -c "'$_cmdstr'"
  108. return 0
  109. }