v-add-web-domain-ssl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 definition
  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/$WEB_BACKEND/$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/$WEB_BACKEND/$template.sh ]; then
  73. $WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$template.sh \
  74. $user $domain $ip $HOMEDIR $sdocroot
  75. fi
  76. # Checking web config
  77. web_conf="/etc/$WEB_SYSTEM/conf.d/vesta.conf"
  78. if [ -z "$(grep "$conf" $web_conf)" ]; then
  79. echo "Include $conf" >> $web_conf
  80. fi
  81. # Checking proxy
  82. if [ ! -z "$PROXY_SYSTEM" ] && [ ! -z "$PROXY" ]; then
  83. conf="$HOMEDIR/$user/conf/web/s$PROXY_SYSTEM.conf"
  84. tpl_file="$WEBTPL/$PROXY_SYSTEM/$PROXY.stpl"
  85. add_web_config
  86. chown root:$user $conf
  87. chmod 640 $conf
  88. # Checking proxy config
  89. proxy_conf="/etc/$PROXY_SYSTEM/conf.d/vesta.conf"
  90. if [ -z "$(grep "$conf" $proxy_conf )" ]; then
  91. echo "include $conf;" >> $proxy_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. # Restarting web server
  103. if [ "$restart" != 'no' ]; then
  104. $BIN/v-restart-web
  105. check_result $? "Web restart failed" >/dev/null
  106. if [ ! -z "$PROXY_SYSTEM" ]; then
  107. $BIN/v-restart-proxy
  108. check_result $? "Proxy restart failed" >/dev/null
  109. fi
  110. fi
  111. # Logging
  112. log_history "enabled ssl support for $domain"
  113. log_event "$OK" "$EVENT"
  114. exit