v-add-web-domain-ssl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/bash
  2. # info: adding ssl for domain
  3. # options: user domain ssl_dir [ssl_home] [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 defenition
  15. user=$1
  16. domain=$(idn -t --quiet -u "$2" )
  17. domain_idn=$(idn -t --quiet -a "$domain")
  18. ssl_dir=$3
  19. ssl_home=${4-same}
  20. restart="$5"
  21. # Includes
  22. source $VESTA/conf/vesta.conf
  23. source $VESTA/func/main.sh
  24. source $VESTA/func/domain.sh
  25. source $VESTA/func/ip.sh
  26. #----------------------------------------------------------#
  27. # Verifications #
  28. #----------------------------------------------------------#
  29. check_args '3' "$#" 'user domain ssl_dir [ssl_home] [restart]'
  30. validate_format 'user' 'domain' 'ssl_dir'
  31. is_system_enabled "$WEB_SYSTEM"
  32. is_object_valid 'user' 'USER' "$user"
  33. is_object_unsuspended 'user' 'USER' "$user"
  34. is_object_valid 'web' 'DOMAIN' "$domain"
  35. is_object_unsuspended 'web' 'DOMAIN' "$domain"
  36. is_object_value_empty 'web' 'DOMAIN' "$domain" '$SSL'
  37. is_ip_owner
  38. is_web_domain_cert_valid
  39. #----------------------------------------------------------#
  40. # Action #
  41. #----------------------------------------------------------#
  42. # Adding certificate to user data directory
  43. cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/$domain.crt
  44. cp -f $ssl_dir/$domain.key $USER_DATA/ssl/$domain.key
  45. cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/$domain.pem
  46. if [ -e "$ssl_dir/$domain.ca" ]; then
  47. cp -f $ssl_dir/$domain.ca $USER_DATA/ssl/$domain.ca
  48. cat $USER_DATA/ssl/$domain.ca >> $USER_DATA/ssl/$domain.pem
  49. fi
  50. chmod 660 $USER_DATA/ssl/$domain.*
  51. # Parsing domain values
  52. get_domain_values 'web'
  53. conf="$HOMEDIR/$user/conf/web/shttpd.conf"
  54. tpl_file="$WEBTPL/apache_$TPL.stpl"
  55. SSL_HOME="$ssl_home"
  56. # Preparing domain values for the template substitution
  57. upd_web_domain_values
  58. # Adding domain to the shttpd.conf
  59. add_web_config
  60. chown root:apache $conf
  61. chmod 640 $conf
  62. # Adding certificate to user dir
  63. cp -f $USER_DATA/ssl/$domain.crt $HOMEDIR/$user/conf/web/ssl.$domain.crt
  64. cp -f $USER_DATA/ssl/$domain.key $HOMEDIR/$user/conf/web/ssl.$domain.key
  65. cp -f $USER_DATA/ssl/$domain.pem $HOMEDIR/$user/conf/web/ssl.$domain.pem
  66. if [ -e "$USER_DATA/ssl/$domain.ca" ]; then
  67. cp -f $USER_DATA/ssl/$domain.ca $HOMEDIR/$user/conf/web/ssl.$domain.ca
  68. fi
  69. # Running template trigger
  70. if [ -x $WEBTPL/apache_$template.sh ]; then
  71. $WEBTPL/apache_$template.sh $user $domain $ip $HOMEDIR $sdocroot
  72. fi
  73. # Checking main vesta httpd config
  74. main_conf='/etc/httpd/conf.d/vesta.conf'
  75. main_conf_check=$(grep "$conf" $main_conf )
  76. if [ -z "$main_conf_check" ]; then
  77. echo "Include $conf" >> $main_conf
  78. fi
  79. # Checking nginx
  80. if [ ! -z "$NGINX" ]; then
  81. # Adding domain to the snginx.conf
  82. conf="$HOMEDIR/$user/conf/web/snginx.conf"
  83. tpl_file="$WEBTPL/nginx_$NGINX.stpl"
  84. add_web_config
  85. chown root:nginx $conf
  86. chmod 640 $conf
  87. # Checking vesta nginx config
  88. main_conf='/etc/nginx/conf.d/vesta_users.conf'
  89. main_conf_check=$(grep "$conf" $main_conf )
  90. if [ -z "$main_conf_check" ]; then
  91. echo "include $conf;" >>$main_conf
  92. fi
  93. fi
  94. #----------------------------------------------------------#
  95. # Vesta #
  96. #----------------------------------------------------------#
  97. # Increasing domain value
  98. increase_user_value "$user" '$U_WEB_SSL'
  99. # Adding ssl values
  100. update_object_value 'web' 'DOMAIN' "$domain" '$SSL_HOME' "$SSL_HOME"
  101. update_object_value 'web' 'DOMAIN' "$domain" '$SSL' "yes"
  102. # Restart web server
  103. if [ "$restart" != 'no' ]; then
  104. $BIN/v-restart-web "$EVENT"
  105. fi
  106. # Logging
  107. log_history "enabled ssl support for $domain"
  108. log_event "$OK" "$EVENT"
  109. exit