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.

103 lines
2.3 KiB

  1. #!/usr/bin/bash
  2. #
  3. # Deploy cert to localhost similar to certbot behavior
  4. #
  5. # export DEPLOY_LOCALHOST_ROOT_PATH="/path/to/certs"
  6. #
  7. # Deploys as:
  8. # /path/to/certs/domain.tld/privkey.pem
  9. # /path/to/certs/domain.tld/cert.pem
  10. # /path/to/certs/domain.tld/ca.pem
  11. # /path/to/certs/domain.tld/fullchain.pem
  12. #
  13. # $1=domain $2=keyfile $3=certfile $4=cafile $5=fullchain
  14. #
  15. localhost_deploy() {
  16. _cdomain="$1"
  17. _ckey="$2"
  18. _ccert="$3"
  19. _cca="$4"
  20. _cfullchain="$5"
  21. _debug _cdomain "$_cdomain"
  22. _debug _ckey "$_ckey"
  23. _debug _ccert "$_ccert"
  24. _debug _cca "$_cca"
  25. _debug _cfullchain "$_cfullchain"
  26. _getdeployconf DEPLOY_LOCALHOST_ROOT_PATH
  27. _debug DEPLOY_LOCALHOST_ROOT_PATH "$DEPLOY_LOCALHOST_ROOT_PATH"
  28. if [ -z "$_cdomain" ]; then
  29. _err "Domain not defined"
  30. return 1
  31. fi
  32. if [ -z "$DEPLOY_LOCALHOST_ROOT_PATH" ]; then
  33. _err "DEPLOY_LOCALHOST_ROOT_PATH not defined"
  34. return 1
  35. fi
  36. _ssl_path="$DEPLOY_LOCALHOST_ROOT_PATH"
  37. if [ ! -d "$_ssl_path" ]; then
  38. _err "Path not found: $_ssl_path"
  39. return 1
  40. fi
  41. _savedeployconf DEPLOY_LOCALHOST_ROOT_PATH "$DEPLOY_LOCALHOST_ROOT_PATH"
  42. _ssl_path="$_ssl_path/$_cdomain"
  43. mkdir -p "$_ssl_path"
  44. # ECC or RSA
  45. length=$(_readdomainconf Le_Keylength)
  46. if _isEccKey "$length"; then
  47. _info "ECC key type detected"
  48. _file_prefix="ecdsa-"
  49. else
  50. _info "RSA key type detected"
  51. _file_prefix=""
  52. fi
  53. _info "Copying cert files..."
  54. # {$2} _ckey
  55. _filename="$_ssl_path/${_file_prefix}privkey.pem"
  56. if ! cat "$_ckey" > "$_filename"; then
  57. err "Error: Can't write $_filename"
  58. return 1
  59. fi
  60. if ! chmod 600 "$_filename"; then
  61. err "Error: Can't set protected 600 permission on privkey.pem"
  62. rm -f "$_filename"
  63. return 1
  64. fi
  65. # {$3} _ccert
  66. _filename="$_ssl_path/${_file_prefix}cert.pem"
  67. if ! cat "$_ccert" > "$_filename"; then
  68. err "Error: Can't write $_filename"
  69. return 1
  70. fi
  71. # {$4} _cca
  72. _filename="$_ssl_path/${_file_prefix}ca.pem"
  73. if ! cat "$_cca" > "$_filename"; then
  74. err "Error: Can't write $_filename"
  75. return 1
  76. fi
  77. # {$5} _cfullchain
  78. _filename="$_ssl_path/${_file_prefix}fullchain.pem"
  79. if ! cat "$_cfullchain" > "$_filename"; then
  80. err "Error: Can't write $_filename"
  81. return 1
  82. fi
  83. _info "Done: Cert files copied to $_ssl_path/"
  84. return 0
  85. }