v-add-letsencrypt-domain 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/bash
  2. # info: adding letsencrypt ssl cetificate for domain
  3. # options: USER DOMAIN [ALIASES] [RESTART]
  4. #
  5. # The function turns on SSL support for a domain. Parameter ssl_dir is a path
  6. # to directory where 2 or 3 ssl files can be found. Certificate file
  7. # domain.tld.crt and its key domain.tld.key are mandatory. Certificate
  8. # authority domain.tld.ca file is optional. If home directory parameter
  9. # (ssl_home) is not set, https domain uses public_shtml as separate
  10. # documentroot directory.
  11. #----------------------------------------------------------#
  12. # Variable&Function #
  13. #----------------------------------------------------------#
  14. # Argument definition
  15. user=$1
  16. domain=$2
  17. aliases=$3
  18. restart=$4
  19. # Includes
  20. source $VESTA/func/main.sh
  21. source $VESTA/func/domain.sh
  22. source $VESTA/conf/vesta.conf
  23. #----------------------------------------------------------#
  24. # Verifications #
  25. #----------------------------------------------------------#
  26. check_args '2' "$#" 'USER DOMAIN [ALIASES] [RESTART]'
  27. is_format_valid 'user' 'domain'
  28. is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
  29. is_system_enabled "$WEB_SSL" 'SSL_SUPPORT'
  30. is_object_valid 'user' 'USER' "$user"
  31. is_object_unsuspended 'user' 'USER' "$user"
  32. is_object_valid 'web' 'DOMAIN' "$domain"
  33. is_object_unsuspended 'web' 'DOMAIN' "$domain"
  34. #----------------------------------------------------------#
  35. # Action #
  36. #----------------------------------------------------------#
  37. # Registering LetsEncrypt user account
  38. $BIN/v-add-letsencrypt-user $user
  39. check_result $? "LE account registration" >/dev/null
  40. source $USER_DATA/ssl/le.conf
  41. email=$EMAIL
  42. # Validating domain and aliases
  43. i=1
  44. for alias in $(echo $domain,$aliases |tr ',' '\n' |sort -u); do
  45. $BIN/v-check-letsencrypt-domain $user $alias
  46. check_result $? "LE domain validation" >/dev/null
  47. if [ "$i" -gt 6 ]; then
  48. check_result $E_LIMIT "LE can't sign more than 6 domains"
  49. fi
  50. i=$((i++))
  51. done
  52. # Generating CSR
  53. ssl_dir=$($BIN/v-generate-ssl-cert "$domain" "$email" "US" "California" \
  54. "San Francisco" "Vesta" "IT" "$aliases" |tail -n1 |awk '{print $2}')
  55. # Signing CSR
  56. crt=$($BIN/v-sign-letsencrypt-csr $user $domain $ssl_dir)
  57. check_result $? "$crt"
  58. echo "$crt" > $ssl_dir/$domain.crt
  59. # Dowloading CA certificate
  60. le_certs='https://letsencrypt.org/certs'
  61. x1='lets-encrypt-x1-cross-signed.pem.txt'
  62. x3='lets-encrypt-x3-cross-signed.pem.txt'
  63. issuer=$(openssl x509 -text -in $ssl_dir/$domain.crt |grep "Issuer:")
  64. if [ -z "$(echo $issuer|grep X3)" ]; then
  65. curl -s $le_certs/$x1 > $ssl_dir/$domain.ca
  66. else
  67. curl -s $le_certs/$x3 > $ssl_dir/$domain.ca
  68. fi
  69. # Adding SSL
  70. $BIN/v-delete-web-domain-ssl $user $domain >/dev/null 2>&1
  71. $BIN/v-add-web-domain-ssl $user $domain $ssl_dir
  72. check_result $? "SSL install" >/dev/null
  73. #----------------------------------------------------------#
  74. # Vesta #
  75. #----------------------------------------------------------#
  76. # Logging
  77. log_event "$OK" "$ARGUMENTS"
  78. exit