diff --git a/.gitattributes b/.gitattributes index c40af3bd..9fd9f61f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -419,6 +419,11 @@ src/basedir_prot.sh -text src/du.pl -text src/fixperms.sh -text src/functions.sh -text +src/hosting_functions.sh -text +src/hosting_functions/hosting_panel.sh -text +src/hosting_functions/hosting_redirect.sh -text +src/hosting_functions/hosting_vhost.sh -text +src/hosting_functions/hosting_webmail.sh -text src/mail_add -text src/mail_del -text src/mem_add -text diff --git a/src/functions.sh b/src/functions.sh index e28f0356..e100bc77 100644 --- a/src/functions.sh +++ b/src/functions.sh @@ -127,7 +127,7 @@ change_host_ip() { if [ ! -f "$zone_file" ]; then echo "Should change $host.$domain, but can't find $zone_file." return 1 - fi + fi if grep -q "$pattern" "$zone_file"; then cp -a -f "$zone_file" "$zone_file.$$" sed "s/$pattern/$a_line/" < "$zone_file" > "$zone_file.$$" @@ -149,7 +149,7 @@ add_host() { local ip local fqdn local vhost_directory - + delete_host "$domain" "$host" "$host_type" if [ "$host" = "@" -o -z "$host" ]; then @@ -193,12 +193,11 @@ add_host() { case "$host_type" in $TYPE_LOCAL) - ln -snf "${HTML_HOME}/${user_letter}/${user}${value}" \ - "$vhost_directory" + host_create_vhost $user $fqdn ${value} ;; $TYPE_WEBMAIL) - ln -snf "${WEBMAIL_DIR}" "$vhost_directory" + host_create_webmail $user $fqdn ;; $TYPE_URL) @@ -207,12 +206,9 @@ add_host() { # followed by at least / value=`echo $value | sed -e 's#\([^/:]*://\)\?\([^/]*\)/*\(.*\)#\1\2/\3#'` - (echo "RewriteEngine on" - echo "RewriteRule (.*) ${value}\$1 [R=permanent,L]" - ) > "$htaccess_directory/.htaccess" - ln -snf "$htaccess_directory" "$vhost_directory" + host_create_redirect $user $fqdn $value ;; - + $TYPE_IP) rm -f "$vhost_directory" rm -rf "$htaccess_directory/.htaccess" @@ -222,6 +218,7 @@ add_host() { echo "Unknow type code: $type" >> "$DOMAIN_LOG_FILE" ;; esac + host_enable_host $user $fqdn } delete_host() { @@ -233,7 +230,7 @@ delete_host() { local escaped_host local escaped_fqdn local pattern - + if [ "$host" = "@" -o -z "$host" ]; then fqdn="$domain" escaped_host="" @@ -276,6 +273,7 @@ delete_host() { rm -f "$HTTP_DNS/$domain_letter/$fqdn" rm -rf "$HTTP_DNS/redir/$domain_letter/$fqdn" + host_disable_host $fqdn } diff --git a/src/hosting_functions.sh b/src/hosting_functions.sh new file mode 100644 index 00000000..b6c5a1c4 --- /dev/null +++ b/src/hosting_functions.sh @@ -0,0 +1,62 @@ +HOST_DIR="/etc/apache2/sites-available" +TEMPLATE_DIR="/etc/alternc/templates/apache2" +DATA_ROOT="/var/alternc" + +HTML_HOME="$DATA_ROOT/html" + +HOSTING_DIR="/usr/lib/alternc/hosting_functions" + + +. /usr/lib/alternc/functions.sh + +if [ -d $HOSTING_DIR ]; then + for i in $HOSTING_DIR/*.sh; do + if [ -r $i ]; then + . $i + fi + done + unset i +fi + + + +host_prepare_conf() { + local TEMPLATE=$1 + HOST_TMP=`mktemp` + + cp $TEMPLATE_DIR"/"$TEMPLATE $HOST_TMP +} + +host_save_conf() { + + local SOURCE=$1 + local TARGET=$2 + + TARGET_DIR=`dirname $TARGET` + mkdir -p $TARGET_DIR + mv $SOURCE $TARGET +} + + +host_enable_host() { + local USER=$1 + local FQDN=$2 + + local FILE_TARGET="/etc/apache2/sites-enabled/"$FQDN + local FILE_SOURCE=$HOST_DIR"/"$USER"/"$FQDN + + if [ -L "$FILE_TARGET" ]; then + rm $FILE_TARGET + fi + + ln -s $FILE_SOURCE $FILE_TARGET +} + +host_disable_host() { + local FQDN=$1 + local CONF_FILE="/etc/apache2/sites-enabled/"$FQDN + + if [ -e "$CONF_FILE" ]; then + rm $CONF_FILE + fi +} diff --git a/src/hosting_functions/hosting_panel.sh b/src/hosting_functions/hosting_panel.sh new file mode 100644 index 00000000..dc9e6cd9 --- /dev/null +++ b/src/hosting_functions/hosting_panel.sh @@ -0,0 +1,20 @@ +host_create_panel() { + + if [[ ( -z $1 ) || (-z $2 ) ]]; then + exit 1; + fi + + echo "création panel $1" + + local USER=$1 + local FQDN=$2 + local TEMPLATE="panel.conf" + local TARGET=$HOST_DIR"/"$USER"/"$FQDN + host_prepare_conf $TEMPLATE #Return #HOST_TMP + + sed -i \ + -e "s#%%fqdn%%#$FQDN#g" \ + $HOST_TMP + + host_save_conf $HOST_TMP $TARGET +} diff --git a/src/hosting_functions/hosting_redirect.sh b/src/hosting_functions/hosting_redirect.sh new file mode 100644 index 00000000..03af374a --- /dev/null +++ b/src/hosting_functions/hosting_redirect.sh @@ -0,0 +1,23 @@ +host_create_redirect() { + + if [[ ( -z $1 ) || (-z $2 ) || ( -z $3) ]]; then + exit 1; + fi + + echo "création redirection pour $1 de $2 vers $3" + + local USER=$1 + local FQDN=$2 + local REDIRECT=$3 + local TEMPLATE="redirect.conf" + local TARGET=$HOST_DIR"/"$USER"/"$FQDN + host_prepare_conf $TEMPLATE #Return #HOST_TMP + + sed -i \ + -e "s#%%fqdn%%#${FQDN}#g" \ + -e "s#%%redirect%%#${REDIRECT}#g" \ + $HOST_TMP + + host_save_conf $HOST_TMP $TARGET + +} diff --git a/src/hosting_functions/hosting_vhost.sh b/src/hosting_functions/hosting_vhost.sh new file mode 100644 index 00000000..b6340672 --- /dev/null +++ b/src/hosting_functions/hosting_vhost.sh @@ -0,0 +1,26 @@ +host_create_vhost() { + + if [[ ( -z $1 ) || (-z $2 ) || ( -z $3) ]]; then + exit 1; + fi + + echo "création vhost $1 pour $2, repertoire $3" + + local USER=$1 + local FQDN=$2 + local TEMPLATE="vhost.conf" + local TARGET=$HOST_DIR"/"$USER"/"$FQDN + + local domain_letter=`print_domain_letter "$domain"` + local user_letter=`print_user_letter "$user"` + local DIRECTORY=${HTML_HOME}/${user_letter}/${user}$3 + + host_prepare_conf $TEMPLATE #Return #HOST_TMP + + sed -i \ + -e "s#%%fqdn%%#$FQDN#g" \ + -e "s#%%document_root%%#$DIRECTORY#g" \ + $HOST_TMP + + host_save_conf $HOST_TMP $TARGET +} diff --git a/src/hosting_functions/hosting_webmail.sh b/src/hosting_functions/hosting_webmail.sh new file mode 100644 index 00000000..190c4f7b --- /dev/null +++ b/src/hosting_functions/hosting_webmail.sh @@ -0,0 +1,20 @@ +host_create_webmail() { + + if [[ ( -z $1 ) || (-z $2 ) ]]; then + exit 1; + fi + + echo "création webmail $1 pour $2" + + local USER=$1 + local FQDN=$2 + local TEMPLATE="webmail.conf" + local TARGET=$HOST_DIR"/"$USER"/"$FQDN + host_prepare_conf $TEMPLATE #Return #HOST_TMP + + sed -i \ + -e "s#%%fqdn%%#$FQDN#g" \ + $HOST_TMP + + host_save_conf $HOST_TMP $TARGET +} diff --git a/src/update_domains.sh b/src/update_domains.sh index 4acad0e4..39462b2f 100755 --- a/src/update_domains.sh +++ b/src/update_domains.sh @@ -109,7 +109,7 @@ MYSQL_DELETE="mysql --defaults-file=/etc/alternc/my.cnf " ######################################################################## # Functions # -. /usr/lib/alternc/functions.sh +. /usr/lib/alternc/hosting_functions.sh ######################################################################## # Main