From 77e83a6ad3e23157b1ca7e459e6cd8be98ce6de5 Mon Sep 17 00:00:00 2001 From: Alan Garcia Date: Tue, 1 Feb 2011 22:44:18 +0000 Subject: [PATCH] Ajout des hooks pour la gestion du mass virtual hosting --- .gitattributes | 1 + etc/alternc/apache2.conf | 2 +- .../functions_hosting/hosting_massvhost.sh | 56 +++++++++++++++++++ install/upgrades/1.0.sql | 3 +- src/functions.sh | 3 +- src/functions_hosting.sh | 22 ++++---- src/update_domains.sh | 55 +++++++++--------- 7 files changed, 101 insertions(+), 41 deletions(-) create mode 100755 etc/alternc/functions_hosting/hosting_massvhost.sh diff --git a/.gitattributes b/.gitattributes index 57a2148f..23509dac 100644 --- a/.gitattributes +++ b/.gitattributes @@ -353,6 +353,7 @@ etc/alternc/apache-ssl.conf -text etc/alternc/apache.conf -text etc/alternc/apache2-ssl.conf -text etc/alternc/apache2.conf -text +etc/alternc/functions_hosting/hosting_massvhost.sh -text etc/alternc/menulist.txt -text etc/alternc/phpmyadmin.inc.php -text etc/alternc/postfix-slave.cf -text diff --git a/etc/alternc/apache2.conf b/etc/alternc/apache2.conf index 602032cf..71c7603b 100644 --- a/etc/alternc/apache2.conf +++ b/etc/alternc/apache2.conf @@ -11,7 +11,6 @@ ScriptAlias /cgi-bin/ /var/alternc/cgi-bin/ AddHandler cgi-script .cgi -Include /var/alternc/apache-vhost/vhosts_all.conf # ***ALTERNC_ALIASES*** @@ -47,4 +46,5 @@ Include /var/alternc/apache-vhost/vhosts_all.conf Allow from all +Include /var/alternc/apache-vhost/vhosts_all.conf Include /etc/alternc/bureau.conf diff --git a/etc/alternc/functions_hosting/hosting_massvhost.sh b/etc/alternc/functions_hosting/hosting_massvhost.sh new file mode 100755 index 00000000..2a59ce81 --- /dev/null +++ b/etc/alternc/functions_hosting/hosting_massvhost.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +ACTION=$1 +# $2 is the type +DOMAIN=$3 +TARGET=$4 + +# Load some librairies +. /etc/alternc/local.sh +. /usr/lib/alternc/functions.sh + +# To not be case-sensitive +ACTION="`echo $ACTION|tr '[:upper:]' '[:lower:]'`" +DOMAIN="`echo $DOMAIN|tr '[:upper:]' '[:lower:]'`" + +if [ -z $ACTION ] || [ -z $DOMAIN ] ; then + echo "Need at least 2 parameters ( action - fqdn )" +fi + +YOP="$ALTERNC_LOC/dns/$(print_domain_letter $DOMAIN)/$DOMAIN" + +case $ACTION in +"disable"|"delete") + if [ ! -e "$YOP" ] ; then + echo "Link do not exist. Nothing done" + exit 15 + fi + if [ ! -L "$YOP" ] ; then + echo "Seem not to be a link ($YOP). Abord" + exit 16 + fi + rm -f "$YOP" + ;; +"enable"|"create") + if [ -z "$TARGET" ] ; then + echo "Parameters target $TARGET missing" + exit 13 + fi + USER=$(get_account_by_domain "$DOMAIN") + if [ -z $USER ] ; then + echo "Unable to find account of $DOMAIN" + exit 17 + fi + TARGET="$ALTERNC_LOC/html/$(print_user_letter $USER)/$USER/$TARGET" + if [ ! -d "$TARGET" ] ; then + echo "Directory $TARGET missing" + exit 14 + fi + ln -snf "$TARGET" "$YOP" + ;; +*) + echo Error : $ACTION not an recognised action + exit 11 + ;; +esac + diff --git a/install/upgrades/1.0.sql b/install/upgrades/1.0.sql index a7b7bbce..bac0c61d 100644 --- a/install/upgrades/1.0.sql +++ b/install/upgrades/1.0.sql @@ -19,6 +19,7 @@ PRIMARY KEY ( `name` ) ) COMMENT = 'Type of domains allowed'; INSERT IGNORE INTO `domaines_type` (name, description, target, entry, compatibility, only_dns, need_dns) values +('massvhost','Locally managed with Mass Virtual Hosting technologie', 'DIRECTORY', '%SUB% IN A @@PUBLIC_IP@@', 'txt', false, false), ('local','Locally managed', 'DIRECTORY', '%SUB% IN A @@PUBLIC_IP@@', 'txt', false, false), ('url','URL redirection', 'URL', '%SUB% IN A @@PUBLIC_IP@@','txt', true, true), ('ip','IP redirection', 'IP', '%SUB% IN A %TARGET%','url,ip,ipv6,txt', false, true), @@ -40,7 +41,7 @@ alter table sub_domaines add column enable enum ('ENABLED', 'ENABLE', 'DISABLED' drop table sub_domaines_standby; drop table domaines_standby; -update sub_domaines set type='LOCAL' where type='0'; +update sub_domaines set type='MASSVHOST' where type='0'; update sub_domaines set type='URL' where type='1'; update sub_domaines set type='IP' where type='2'; update sub_domaines set type='WEBMAIL' where type='3'; diff --git a/src/functions.sh b/src/functions.sh index 95dc6e30..5047a7f2 100755 --- a/src/functions.sh +++ b/src/functions.sh @@ -6,6 +6,7 @@ # Init some other vars MYSQL_DO="/usr/bin/mysql --defaults-file=/etc/alternc/my.cnf -Bs -e " +mysql_query() { /usr/bin/mysql --defaults-file=/etc/alternc/my.cnf -Bs -e "$@" ; } DOMAIN_LOG_FILE="/var/log/alternc/update_domains.log" VHOST_FILE="$VHOST_DIR/vhosts_all.conf" @@ -35,7 +36,7 @@ get_account_by_domain() { # else # implantons localement ce que nous avons besoin, puisque admintools # n'est pas là - $MYSQL_DO 'SELECT a.login FROM membres a, sub_domaines b WHERE a.uid = b.compte AND \ + mysql_query 'SELECT a.login FROM membres a, sub_domaines b WHERE a.uid = b.compte AND \ CONCAT(IF(sub="", "", CONCAT(sub, ".")), domaine) = "'"$1"'" LIMIT 1;' # fi } diff --git a/src/functions_hosting.sh b/src/functions_hosting.sh index afbad0ec..a484df38 100644 --- a/src/functions_hosting.sh +++ b/src/functions_hosting.sh @@ -3,7 +3,7 @@ . /usr/lib/alternc/functions.sh TEMPLATE_DIR="/etc/alternc/templates/apache2" -HOSTING_DIR="/usr/lib/alternc/hosting_functions" +HOSTING_DIR="/etc/alternc/functions_hosting" HTML_HOME="$ALTERNC_LOC/html" VHOST_DIR="$ALTERNC_LOC/apache-vhost" @@ -21,7 +21,7 @@ launch_hooks() { if [ -x "$HOSTING_DIR/hosting_$VTYPE.sh" ] ; then # If a specific script exist for this VTYPE, # we launch it, and return his return code - "$HOSTING_DIR/hosting_$VTYPE.sh" $@ + "$HOSTING_DIR/hosting_$VTYPE.sh" "$1" "$2" "$3" "$4" return $? fi @@ -37,8 +37,8 @@ host_create() { # and the template file provided local VTYPE="$1" - - launch_hooks "create" $@ + + launch_hooks "create" "$1" "$2" "$3" "$4" if [ $? -gt 10 ] ; then # If the hooks return a value > 10 # it's mean we do not continue the @@ -99,7 +99,7 @@ host_create() { mv -f "$TMP_FILE" "$FILE_TARGET" # Execute post-install hooks - launch_hooks "postinst" $@ + launch_hooks "postinst" "$1" "$2" "$3" "$4" if [ $? -gt 10 ] ; then # If the hooks return a value > 10 # it's mean we do not continue the @@ -112,11 +112,11 @@ host_create() { } host_disable() { - host_change_enable "disable" $@ + host_change_enable "disable" "$1" "$2" "$3" "$4" } host_enable() { - host_change_enable "enable" $@ + host_change_enable "enable" "$1" "$2" "$3" "$4" } host_change_enable() { @@ -124,7 +124,7 @@ host_change_enable() { local STATE=$1 # Execute hooks - launch_hooks $@ + launch_hooks "$1" "$2" "$3" "$4" if [ $? -gt 10 ] ; then # If the hooks return a value > 10 # it's mean we do not continue the @@ -132,8 +132,8 @@ host_change_enable() { return $? fi - local FQDN=$2 - local TYPE=$3 # no use here, but one day, maybe... So here he is + local TYPE=$2 # no use here, but one day, maybe... So here he is + local FQDN=$3 local USER=$(get_account_by_domain $FQDN) local user_letter=`print_user_letter "$USER"` local FENABLED="$VHOST_DIR/${user_letter}/$USER/$FQDN.conf" @@ -164,7 +164,7 @@ host_change_enable() { host_delete() { local FQDN=$1 # Execute post-install hooks - launch_hooks "delete" $@ + launch_hooks "delete" "$1" "$2" "$3" "$4" if [ $? -gt 10 ] ; then # If the hooks return a value > 10 # it's mean we do not continue the diff --git a/src/update_domains.sh b/src/update_domains.sh index 168b9647..c91606d3 100644 --- a/src/update_domains.sh +++ b/src/update_domains.sh @@ -17,8 +17,9 @@ done # Some vars umask 022 LOCK_FILE="$ALTERNC_LOC/bureau/cron.lock" -B='µµ' # Strange letters to make split in query - +OLDIFS="$IFS" +NEWIFS=" " +B="µµ§§" # Strange letters to make split in query # Somes check before start operations if [ `id -u` -ne 0 ]; then @@ -39,64 +40,64 @@ touch "$LOCK_FILE" # For domains we want to delete completely, make sure all the tags are all right # set sub_domaines.web_action = delete where domaines.dns_action = DELETE -$MYSQL_DO "update sub_domaines sd, domaines d set sd.web_action = 'DELETE' where sd.domaine = d.domaine and sd.compte=d.compte and d.dns_action = 'DELETE';" +mysql_query "update sub_domaines sd, domaines d set sd.web_action = 'DELETE' where sd.domaine = d.domaine and sd.compte=d.compte and d.dns_action = 'DELETE';" # Sub_domaines we want to delete # sub_domaines.web_action = delete -for sub in $( $MYSQL_DO "select concat_ws('$B',if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine),sd.type) from sub_domaines sd where web_action ='DELETE';") ; do - host_delete $(echo $sub|tr "$B" ' ') - $MYSQL_DO "delete from sub_domaines where concat_ws('$B',if(length(sub)>0,concat_ws('.',sub,domaine),domaine),type) = '$sub' and web_action ='DELETE';" +for sub in $( mysql_query "select concat_ws('$B',lower(sd.type), if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine)) from sub_domaines sd where web_action ='DELETE';") ; do + host_delete ${sub/$B/ } + mysql_query "delete from sub_domaines where concat_ws('$B',if(length(sub)>0,concat_ws('.',sub,domaine),domaine),type) = '$sub' and web_action ='DELETE';" done # Sub domaines we want to update # sub_domaines.web_action = update and sub_domains.only_dns = false -params=$( $MYSQL_DO " - select concat_ws('$B',lower(sd.type), if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine), valeur) - from sub_domaines sd - where sd.web_action ='UPDATE' - ;") -for sub in $params;do - host_create $(echo $sub|tr "$B" ' ') - $MYSQL_DO "update sub_domaines sd set web_action='OK',web_result='$?' where concat_ws('$B',lower(sd.type),if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine),valeur)='$sub'" +IFS="$NEWIFS" +mysql_query " +select concat_ws('$IFS',lower(sd.type), if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine), sd.valeur ) +from sub_domaines sd +where sd.web_action ='UPDATE' +;" | while read type domain valeur ; do + host_create "$type" "$domain" "$valeur" + mysql_query "update sub_domaines sd set web_action='OK',web_result='$?' where lower(sd.type)='$type' and if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine)='$domain' and sd.valeur='$valeur'; " done # Domaine to enable -for sub in $( $MYSQL_DO "select concat_ws('$B',if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine),lower(sd.type)) from sub_domaines sd where sd.enable ='ENABLE' ;");do - host_enable $(echo $sub|tr "$B" ' ') - $MYSQL_DO "update sub_domaines sd set enable='ENABLED' where concat_ws('$B',if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine),lower(sd.type)) = '$sub';" +mysql_query "select concat_ws('$IFS',lower(sd.type),if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine),sd.valeur) from sub_domaines sd where sd.enable ='ENABLE' ;"|while read type domain valeur ; do + host_enable "$type" "$domain" "$valeur" + mysql_query "update sub_domaines sd set enable='ENABLED' where lower(sd.type)='$type' and if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine)='$domain' and sd.valeur='$valeur';" done # Domains to disable -for sub in $( $MYSQL_DO "select concat_ws('$B',if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine),lower(sd.type)) from sub_domaines sd where sd.enable ='DISABLE' ;");do - host_disable $(echo $sub|tr "$B" ' ') - $MYSQL_DO "update sub_domaines sd set enable='DISABLED' where concat_ws('$B',if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine),lower(sd.type)) = '$sub';" +mysql_query "select concat_ws('$IFS',lower(sd.type),if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine),sd.valeur) from sub_domaines sd where sd.enable ='DISABLE' ;"|while read type domain valeur ; do + host_disable "$type" "$domain" "$valeur" + mysql_query "update sub_domaines sd set enable='DISABLED' where lower(sd.type)='$type' and if(length(sd.sub)>0,concat_ws('.',sd.sub,sd.domaine),sd.domaine)='$domain' and sd.valeur='$valeur';" done # Domains we do not want to be the DNS serveur anymore : # domaines.dns_action = UPDATE and domaines.gesdns = 0 -for dom in $( $MYSQL_DO "select domaine from domaines where dns_action = 'UPDATE' and gesdns = 0;") ; do +for dom in $( mysql_query "select domaine from domaines where dns_action = 'UPDATE' and gesdns = 0;") ; do dns_delete $dom - $MYSQL_DO "update domaines set dns_action = 'OK', dns_result = '$?' where domaine = '$dom'" + mysql_query "update domaines set dns_action = 'OK', dns_result = '$?' where domaine = '$dom'" done # Domains we have to update the dns : # domaines.dns_action = UPDATE -for dom in $( $MYSQL_DO "select domaine from domaines where dns_action = 'UPDATE';") ; do +for dom in $( mysql_query "select domaine from domaines where dns_action = 'UPDATE';") ; do dns_regenerate $dom - $MYSQL_DO "update domaines set dns_action = 'OK', dns_result = '$?' where domaine = '$dom'" + mysql_query "update domaines set dns_action = 'OK', dns_result = '$?' where domaine = '$dom'" done # Domains we want to delete completely, now we do it # domaines.dns_action = DELETE -for dom in $( $MYSQL_DO "select domaine from domaines where dns_action = 'DELETE';") ; do +for dom in $( mysql_query "select domaine from domaines where dns_action = 'DELETE';") ; do dns_delete $dom # Web configurations have already bean cleaned previously - $MYSQL_DO "delete sub_domaines where domaine='$dom'; delete domaines where domaine='$dom';" + mysql_query "delete sub_domaines where domaine='$dom'; delete domaines where domaine='$dom';" done # Concat the apaches files -tempo=$(mktemp /tmp/alternc-vhost.XXXXX) +tempo=$(mktemp "$VHOST_FILE.XXXXX") find "$VHOST_DIR" -mindepth 2 -type f -iname "*.conf" -exec cat '{}' > "$tempo" \; if [ $? -ne 0 ] ; then log_error " web file concatenation failed"