#!/bin/bash
# info: adding ssl for domain
# options: user domain ssl_dir [ssl_home] [restart]
#
# The function turns on SSL support for a domain. Parameter ssl_dir is a path
# to directory where 2 or 3 ssl files can be found. Certificate file 
# domain.tld.crt and its key domain.tld.key  are mandatory. Certificate
# authority domain.tld.ca file is optional. If home directory  parameter
# (ssl_home) is not set, https domain uses public_shtml as separate
# documentroot directory.


#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

# Argument defenition
user=$1
domain=$(idn -t --quiet -u "$2" )
domain_idn=$(idn -t --quiet -a "$domain")
ssl_dir=$3
ssl_home=${4-same}
restart="$5"

# Includes
source $VESTA/conf/vesta.conf
source $VESTA/func/main.sh
source $VESTA/func/domain.sh
source $VESTA/func/ip.sh


#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

check_args '3' "$#" 'user domain ssl_dir [ssl_home] [restart]'
validate_format 'user' 'domain' 'ssl_dir'
is_system_enabled "$WEB_SYSTEM"
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_object_valid 'web' 'DOMAIN' "$domain"
is_object_unsuspended 'web' 'DOMAIN' "$domain"
is_object_value_empty 'web' 'DOMAIN' "$domain" '$SSL'
is_ip_owner
is_web_domain_cert_valid


#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Adding certificate to user data directory
cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/$domain.crt
cp -f $ssl_dir/$domain.key $USER_DATA/ssl/$domain.key
cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/$domain.pem
if [ -e "$ssl_dir/$domain.ca" ]; then
    cp -f $ssl_dir/$domain.ca $USER_DATA/ssl/$domain.ca
    cat $USER_DATA/ssl/$domain.ca >> $USER_DATA/ssl/$domain.pem
fi
chmod 660 $USER_DATA/ssl/$domain.*

# Parsing domain values
get_domain_values 'web'
conf="$HOMEDIR/$user/conf/web/shttpd.conf"
tpl_file="$WEBTPL/apache_$TPL.stpl"
SSL_HOME="$ssl_home"

# Preparing domain values for the template substitution
upd_web_domain_values

# Adding domain to the shttpd.conf
add_web_config

chown root:apache $conf
chmod 640 $conf

# Adding certificate to user dir
cp -f $USER_DATA/ssl/$domain.crt $HOMEDIR/$user/conf/web/ssl.$domain.crt
cp -f $USER_DATA/ssl/$domain.key $HOMEDIR/$user/conf/web/ssl.$domain.key
cp -f $USER_DATA/ssl/$domain.pem $HOMEDIR/$user/conf/web/ssl.$domain.pem
if [ -e "$USER_DATA/ssl/$domain.ca" ]; then
    cp -f $USER_DATA/ssl/$domain.ca $HOMEDIR/$user/conf/web/ssl.$domain.ca
fi

# Running template trigger
if [ -x $WEBTPL/apache_$template.sh ]; then
    $WEBTPL/apache_$template.sh $user $domain $ip $HOMEDIR $sdocroot
fi

# Checking main vesta httpd config
main_conf='/etc/httpd/conf.d/vesta.conf'
main_conf_check=$(grep "$conf" $main_conf )
if [ -z "$main_conf_check" ]; then
    echo "Include $conf" >> $main_conf
fi

# Checking  nginx
if [ ! -z "$NGINX" ]; then
    # Adding domain to the snginx.conf
    conf="$HOMEDIR/$user/conf/web/snginx.conf"
    tpl_file="$WEBTPL/nginx_$NGINX.stpl"
    add_web_config

    chown root:nginx $conf
    chmod 640 $conf

    # Checking vesta nginx config
    main_conf='/etc/nginx/conf.d/vesta_users.conf'
    main_conf_check=$(grep "$conf" $main_conf )
    if [ -z "$main_conf_check" ]; then
        echo "include $conf;" >>$main_conf
    fi
fi


#----------------------------------------------------------#
#                       Vesta                              #
#----------------------------------------------------------#

# Increasing domain value
increase_user_value "$user" '$U_WEB_SSL'

# Adding ssl values
update_object_value 'web' 'DOMAIN' "$domain" '$SSL_HOME' "$SSL_HOME"
update_object_value 'web' 'DOMAIN' "$domain" '$SSL' "yes"

# Restart web server
if [ "$restart" != 'no' ]; then
    $BIN/v_restart_web "$EVENT"
fi

# Logging
log_history "enabled ssl support for $domain"
log_event "$OK" "$EVENT"

exit
