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.

118 lines
4.0 KiB

  1. #!/usr/bin/env sh
  2. # Script to deploy certificates to remote cPanel server by SSH
  3. # This is a rough mashup of deploy/ssh.sh and deploy/cpanel_uapi.sh
  4. # Note that SSH must be able to login to remote host without a password...
  5. # SSH Keys must have been exchanged with the remote host. Validate and
  6. # test that you can login to USER@SERVER from the host running acme.sh before
  7. # using this script.
  8. #
  9. # The following variables exported from environment will be used.
  10. # If not set then values previously saved in domain.conf file are used.
  11. #
  12. # Only a username is required. All others are optional.
  13. #
  14. # export DEPLOY_SSH_CPANEL_USER="admin" # required
  15. # export DEPLOY_SSH_CPANEL_CMD="ssh -i /path/to/key" # defaults to ssh
  16. # export DEPLOY_SSH_CPANEL_SERVER="server.example.com" # defaults to domain name
  17. # export DEPLOY_SSH_CPANEL_UAPIUSER="cPanelUserName" # defaults to DEPLOY_SSH_CPANEL_USER
  18. ######## Public functions #####################
  19. #domain keyfile certfile cafile fullchain
  20. ssh_cpanel_deploy() {
  21. _cdomain="$1"
  22. _ckey="$2"
  23. _ccert="$3"
  24. _cca="$4"
  25. _cfullchain="$5"
  26. _cmdstr=""
  27. if [ -f "$DOMAIN_CONF" ]; then
  28. # shellcheck disable=SC1090
  29. . "$DOMAIN_CONF"
  30. fi
  31. _debug _cdomain "$_cdomain"
  32. _debug _ckey "$_ckey"
  33. _debug _ccert "$_ccert"
  34. _debug _cca "$_cca"
  35. _debug _cfullchain "$_cfullchain"
  36. # USER is required to login by SSH to remote host.
  37. if [ -z "$DEPLOY_SSH_CPANEL_USER" ]; then
  38. if [ -z "$Le_Deploy_ssh_cpanel_user" ]; then
  39. _err "DEPLOY_SSH_CPANEL_USER not defined."
  40. return 1
  41. fi
  42. else
  43. Le_Deploy_ssh_cpanel_user="$DEPLOY_SSH_CPANEL_USER"
  44. _savedomainconf Le_Deploy_ssh_cpanel_user "$Le_Deploy_ssh_cpanel_user"
  45. fi
  46. # UAPIUSER is optional. If not provided then use DEPLOY_SSH_CPANEL_USER
  47. if [ -z "$DEPLOY_SSH_CPANEL_UAPIUSER" ]; then
  48. if [ -z "$Le_Deploy_ssh_cpanel_uapiuser" ]; then
  49. Le_Deploy_ssh_cpanel_uapiuser="$Le_Deploy_ssh_cpanel_user"
  50. fi
  51. else
  52. Le_Deploy_ssh_cpanel_uapiuser="$DEPLOY_SSH_CPANEL_UAPIUSER"
  53. _savedomainconf Le_Deploy_ssh_cpanel_uapiuser "$Le_Deploy_ssh_cpanel_uapiuser"
  54. fi
  55. # SERVER is optional. If not provided then use _cdomain
  56. if [ -n "$DEPLOY_SSH_CPANEL_SERVER" ]; then
  57. Le_Deploy_ssh_cpanel_server="$DEPLOY_SSH_CPANEL_SERVER"
  58. _savedomainconf Le_Deploy_ssh_cpanel_server "$Le_Deploy_ssh_cpanel_server"
  59. elif [ -z "$Le_Deploy_ssh_cpanel_server" ]; then
  60. Le_Deploy_ssh_cpanel_server="$_cdomain"
  61. fi
  62. # CMD is optional. If not provided then use ssh
  63. if [ -n "$DEPLOY_SSH_CPANEL_CMD" ]; then
  64. Le_Deploy_ssh_cpanel_cmd="$DEPLOY_SSH_CPANEL_CMD"
  65. _savedomainconf Le_Deploy_ssh_cpanel_cmd "$Le_Deploy_ssh_cpanel_cmd"
  66. elif [ -z "$Le_Deploy_ssh_cpanel_cmd" ]; then
  67. Le_Deploy_ssh_cpanel_cmd="ssh"
  68. fi
  69. _info "Deploy certificates to remote server $Le_Deploy_ssh_cpanel_user@$Le_Deploy_ssh_cpanel_server"
  70. # read cert and key files and urlencode both
  71. _info "URL Encode Certificate..."
  72. _cert=$(_url_encode <"$_ccert")
  73. _info "URL Encode Key..."
  74. _key=$(_url_encode <"$_ckey")
  75. _secure_debug _cert "$_cert"
  76. _secure_debug _key "$_key"
  77. if [ "$Le_Deploy_ssh_cpanel_uapiuser" = "$Le_Deploy_ssh_cpanel_user" ]; then
  78. _cmdstr="uapi SSL install_ssl domain=\"$_cdomain\" cert=\"$_cert\" key=\"$_key\""
  79. else
  80. _cmdstr="uapi --user=\"$Le_Deploy_ssh_cpanel_uapiuser\" SSL install_ssl domain=\"$_cdomain\" cert=\"$_cert\" key=\"$_key\""
  81. fi
  82. _secure_debug "Remote commands to execute: " "$_cmdstr"
  83. _info "Submitting sequence of commands to remote server by ssh"
  84. # quotations in bash cmd below intended. Squash travis spellcheck error
  85. # shellcheck disable=SC2029
  86. $Le_Deploy_ssh_cpanel_cmd -T "$Le_Deploy_ssh_cpanel_user@$Le_Deploy_ssh_cpanel_server" sh -c "'$_cmdstr'"
  87. _ret="$?"
  88. if [ "$_ret" != "0" ]; then
  89. _err "Error code $_ret returned from $Le_Deploy_ssh_cpanel_cmd"
  90. fi
  91. _error_response="status: 0"
  92. if test "${_ret#*$_error_response}" != "$_ret"; then
  93. _err "Error in deploying certificate:"
  94. _err "$_ret"
  95. return 1
  96. fi
  97. _debug ret "$_ret"
  98. _info "Certificate successfully deployed"
  99. return 0
  100. }