v-add-web-domain-ssl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/func/main.sh
  23. source $VESTA/func/domain.sh
  24. source $VESTA/func/ip.sh
  25. source $VESTA/conf/vesta.conf
  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" 'WEB_SYSTEM'
  32. is_system_enabled "$WEB_SSL" 'SSL_SUPPORT'
  33. is_object_valid 'user' 'USER' "$user"
  34. is_object_unsuspended 'user' 'USER' "$user"
  35. is_object_valid 'web' 'DOMAIN' "$domain"
  36. is_object_unsuspended 'web' 'DOMAIN' "$domain"
  37. is_object_value_empty 'web' 'DOMAIN' "$domain" '$SSL'
  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. echo >> $USER_DATA/ssl/$domain.pem
  49. cat $USER_DATA/ssl/$domain.ca >> $USER_DATA/ssl/$domain.pem
  50. fi
  51. chmod 660 $USER_DATA/ssl/$domain.*
  52. # Parsing domain values
  53. get_domain_values 'web'
  54. conf="$HOMEDIR/$user/conf/web/s$WEB_SYSTEM.conf"
  55. tpl_file="$WEBTPL/$WEB_SYSTEM/$TPL.stpl"
  56. SSL_HOME="$ssl_home"
  57. ip=$(get_real_ip $IP)
  58. # Preparing domain values for the template substitution
  59. upd_web_domain_values
  60. # Adding domain to the web config
  61. add_web_config
  62. chown root:$user $conf
  63. chmod 640 $conf
  64. # Adding certificate to user dir
  65. cp -f $USER_DATA/ssl/$domain.crt $HOMEDIR/$user/conf/web/ssl.$domain.crt
  66. cp -f $USER_DATA/ssl/$domain.key $HOMEDIR/$user/conf/web/ssl.$domain.key
  67. cp -f $USER_DATA/ssl/$domain.pem $HOMEDIR/$user/conf/web/ssl.$domain.pem
  68. if [ -e "$USER_DATA/ssl/$domain.ca" ]; then
  69. cp -f $USER_DATA/ssl/$domain.ca $HOMEDIR/$user/conf/web/ssl.$domain.ca
  70. fi
  71. # Running template trigger
  72. if [ -x $WEBTPL/$WEB_SYSTEM/$template.sh ]; then
  73. $WEBTPL/$WEB_SYSTEM/$template.sh $user $domain $ip $HOMEDIR $sdocroot
  74. fi
  75. # Checking web config
  76. web_conf="/etc/$WEB_SYSTEM/conf.d/vesta.conf"
  77. if [ -z "$(grep "$conf" $web_conf)" ]; then
  78. echo "Include $conf" >> $web_conf
  79. fi
  80. # Checking proxy
  81. if [ ! -z "$PROXY" ]; then
  82. conf="$HOMEDIR/$user/conf/web/s$PROXY_SYSTEM.conf"
  83. tpl_file="$WEBTPL/$PROXY_SYSTEM/$PROXY.stpl"
  84. add_web_config
  85. chown root:$user $conf
  86. chmod 640 $conf
  87. # Checking proxy config
  88. proxy_conf="/etc/$PROXY_SYSTEM/conf.d/vesta.conf"
  89. if [ -z "$(grep "$conf" $proxy_conf )" ]; then
  90. echo "include $conf;" >> $proxy_conf
  91. fi
  92. fi
  93. #----------------------------------------------------------#
  94. # Vesta #
  95. #----------------------------------------------------------#
  96. # Increasing domain value
  97. increase_user_value "$user" '$U_WEB_SSL'
  98. # Adding ssl values
  99. update_object_value 'web' 'DOMAIN' "$domain" '$SSL_HOME' "$SSL_HOME"
  100. update_object_value 'web' 'DOMAIN' "$domain" '$SSL' "yes"
  101. # Restart web server
  102. if [ "$restart" != 'no' ]; then
  103. $BIN/v-restart-web
  104. if [ $? -ne 0 ]; then
  105. exit E_RESTART
  106. fi
  107. $BIN/v-restart-proxy
  108. if [ $? -ne 0 ]; then
  109. exit E_RESTART
  110. fi
  111. fi
  112. # Logging
  113. log_history "enabled ssl support for $domain"
  114. log_event "$OK" "$EVENT"
  115. exit