v-add-web-domain-ssl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_web_domain_cert_valid
  38. #----------------------------------------------------------#
  39. # Action #
  40. #----------------------------------------------------------#
  41. # Adding certificate to user data directory
  42. cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/$domain.crt
  43. cp -f $ssl_dir/$domain.key $USER_DATA/ssl/$domain.key
  44. cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/$domain.pem
  45. if [ -e "$ssl_dir/$domain.ca" ]; then
  46. cp -f $ssl_dir/$domain.ca $USER_DATA/ssl/$domain.ca
  47. echo >> $USER_DATA/ssl/$domain.pem
  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/s$WEB_SYSTEM.conf"
  54. tpl_file="$WEBTPL/$WEB_SYSTEM/$TPL.stpl"
  55. SSL_HOME="$ssl_home"
  56. ip=$(get_real_ip $IP)
  57. # Preparing domain values for the template substitution
  58. upd_web_domain_values
  59. # Adding domain to the web config
  60. add_web_config
  61. chown root:$user $conf
  62. chmod 640 $conf
  63. # Adding certificate to user dir
  64. cp -f $USER_DATA/ssl/$domain.crt $HOMEDIR/$user/conf/web/ssl.$domain.crt
  65. cp -f $USER_DATA/ssl/$domain.key $HOMEDIR/$user/conf/web/ssl.$domain.key
  66. cp -f $USER_DATA/ssl/$domain.pem $HOMEDIR/$user/conf/web/ssl.$domain.pem
  67. if [ -e "$USER_DATA/ssl/$domain.ca" ]; then
  68. cp -f $USER_DATA/ssl/$domain.ca $HOMEDIR/$user/conf/web/ssl.$domain.ca
  69. fi
  70. # Running template trigger
  71. if [ -x $WEBTPL/$WEB_SYSTEM/$template.sh ]; then
  72. $WEBTPL/$WEB_SYSTEM/$template.sh $user $domain $ip $HOMEDIR $sdocroot
  73. fi
  74. # Checking web config
  75. web_conf="/etc/$WEB_SYSTEM/conf.d/vesta.conf"
  76. if [ -z "$(grep "$conf" $web_conf)" ]; then
  77. echo "Include $conf" >> $web_conf
  78. fi
  79. # Checking proxy
  80. if [ ! -z "$PROXY" ]; then
  81. conf="$HOMEDIR/$user/conf/web/s$PROXY_SYSTEM.conf"
  82. tpl_file="$WEBTPL/$PROXY_SYSTEM/$PROXY.stpl"
  83. add_web_config
  84. chown root:$user $conf
  85. chmod 640 $conf
  86. # Checking proxy config
  87. proxy_conf="/etc/$PROXY_SYSTEM/conf.d/vesta.conf"
  88. if [ -z "$(grep "$conf" $proxy_conf )" ]; then
  89. echo "include $conf;" >> $proxy_conf
  90. fi
  91. fi
  92. #----------------------------------------------------------#
  93. # Vesta #
  94. #----------------------------------------------------------#
  95. # Increasing domain value
  96. increase_user_value "$user" '$U_WEB_SSL'
  97. # Adding ssl values
  98. update_object_value 'web' 'DOMAIN' "$domain" '$SSL_HOME' "$SSL_HOME"
  99. update_object_value 'web' 'DOMAIN' "$domain" '$SSL' "yes"
  100. # Restart web server
  101. if [ "$restart" != 'no' ]; then
  102. $BIN/v-restart-web "$EVENT"
  103. fi
  104. # Logging
  105. log_history "enabled ssl support for $domain"
  106. log_event "$OK" "$EVENT"
  107. exit