3 . /usr/lib/alternc/functions.sh
5 TEMPLATE_DIR="/etc/alternc/templates/apache2"
6 HOSTING_DIR="/etc/alternc/functions_hosting"
8 HTML_HOME="$ALTERNC_HTML"
9 VHOST_DIR="/var/lib/alternc/apache-vhost"
15 # If no VTYPE specified
21 if [ -x "$HOSTING_DIR/hosting_$VTYPE.sh" ] ; then
22 # If a specific script exist for this VTYPE,
23 # we launch it, and return his return code
24 "$HOSTING_DIR/hosting_$VTYPE.sh" "$1" "$2" "$3" "$4"
28 # No specific script, return 0
33 # Return the absolute path of a conf file for a FQDN
35 local U_ID=$(get_uid_by_domain "$FQDN")
36 local CONFFILE="$VHOST_DIR/${U_ID:(-1)}/$U_ID/$FQDN.conf"
42 # Function to create a vhost for a website
43 # First, it look if there is a special file for
45 # If there isn't, it use the default function
46 # and the template file provided
50 launch_hooks "create" "$1" "$2" "$3" "$4"
51 if [ $? -gt 10 ] ; then
52 # If the hooks return a value > 10
53 # it's mean we do not continue the
58 # There is no special script, I use the standart template
59 # If I do not found template manualy define, I look
60 # If there is an existing template with the good name
62 # First, usefull vars. Some may be empty or false, it's
63 # OK, it will be solve in the "case" below
66 local REDIRECT=$4 # Yes, TARGET_DIR and REDIRECT are the same
67 local TARGET_DIR=$4 # It's used by different template
68 local U_ID=$(get_uid_by_domain "$FQDN")
70 local USER=$(get_account_by_domain $FQDN)
71 local user_letter=`print_user_letter "$USER"`
72 local DOCUMENT_ROOT="${HTML_HOME}/${user_letter}/${USER}$TARGET_DIR"
73 local ACCOUNT_ROOT="${HTML_HOME}/${user_letter}/${USER}/"
74 local FILE_TARGET=$(host_conffile "$FQDN")
76 # In case VTYPE don't have the same name as the template file,
77 # here we can define it
81 # TEMPLATE="$TEMPLATE_DIR/an-example.conf"
84 # No template found, look if there is some in the
86 [ -r "$TEMPLATE_DIR/$VTYPE" ] && TEMPLATE="$TEMPLATE_DIR/$VTYPE"
87 [ ! "$TEMPLATE" ] && [ -r "$TEMPLATE_DIR/$VTYPE.conf" ] && TEMPLATE="$TEMPLATE_DIR/$VTYPE.conf"
91 # If TEMPLATE is empty, stop right here
92 [ ! "$TEMPLATE" ] && return 6
94 # Forbid generation for website with UID/GID == 0
95 if [[ $U_ID == 0 || $G_ID == 0 ]] ; then
96 log_error "Fatal error: update_domains/function_dns/host_create : FQDN = $FQDN - TYPE = $VTYPE - UID = $U_ID - GID = $G_ID . Stopping generation"
100 # Create a new conf file
101 local TMP_FILE=$(mktemp "/tmp/alternc_host.XXXXXX")
102 cp "$TEMPLATE" "$TMP_FILE"
104 # Substitute special characters :
105 FQDN2="`echo $FQDN | sed -e 's/\\\\/\\\\\\\\/g' -e 's/#/\\\\#/g' -e 's/&/\\\\\\&/g'`"
106 DOCUMENT_ROOT2="`echo $DOCUMENT_ROOT | sed -e 's/\\\\/\\\\\\\\/g' -e 's/#/\\\\#/g' -e 's/&/\\\\\\&/g'`"
107 ACCOUNT_ROOT2="`echo $ACCOUNT_ROOT | sed -e 's/\\\\/\\\\\\\\/g' -e 's/#/\\\\#/g' -e 's/&/\\\\\\&/g'`"
108 REDIRECT2="`echo $REDIRECT | sed -e 's/\\\\/\\\\\\\\/g' -e 's/#/\\\\#/g' -e 's/&/\\\\\\&/g'`"
109 USER2="`echo $USER | sed -e 's/\\\\/\\\\\\\\/g' -e 's/#/\\\\#/g' -e 's/&/\\\\\\&/g'`"
111 # Put the good value in the conf file
113 -e "s#%%LOGIN%%#$USER#g" \
114 -e "s#%%fqdn%%#$FQDN2#g" \
115 -e "s#%%document_root%%#$DOCUMENT_ROOT2#g" \
116 -e "s#%%account_root%%#$ACCOUNT_ROOT2#g" \
117 -e "s#%%redirect%%#$REDIRECT2#g" \
118 -e "s#%%UID%%#$U_ID#g" \
119 -e "s#%%GID%%#$G_ID#g" \
120 -e "s#%%mail_account%%#$MAIL_ACCOUNT#g" \
121 -e "s#%%user%%#$USER2#g" \
125 if [[ "$FQDN2" == "*."* ]]; then
126 sed -i "s/ServerName/ServerAlias/" $TMP_FILE
129 # Check if all is right in the conf file
130 # If not, put a debug message
131 # NO : redirect and document_root COULD contains legitimate %% expressions (...)
132 # local ISNOTGOOD=$(grep "%%" "$TMP_FILE")
133 # [ "$ISNOTGOOD" ] && (echo "# There was a probleme in the generation : $ISNOTGOOD" > "$TMP_FILE" ; return 44 )
135 # Put the conf file in prod
136 mkdir -p "$(dirname "$FILE_TARGET")"
137 mv -f "$TMP_FILE" "$FILE_TARGET"
139 # Execute post-install hooks
140 launch_hooks "postinst" "$1" "$2" "$3" "$4"
141 if [ $? -gt 10 ] ; then
142 # If the hooks return a value > 10
143 # it's mean we do not continue the
148 # All is quit, we return 0
153 host_change_enable "disable" "$1" "$2" "$3" "$4"
157 host_change_enable "enable" "$1" "$2" "$3" "$4"
160 host_change_enable() {
161 # Function to enable or disable a host
165 launch_hooks "$1" "$2" "$3" "$4"
166 if [ $? -gt 10 ] ; then
167 # If the hooks return a value > 10
168 # it's mean we do not continue the
173 local TYPE=$2 # no use here, but one day, maybe... So here he is
175 local FENABLED=$(host_conffile "$FQDN")
176 local FDISABLED="$FENABLED-disabled"
180 local SOURCE="$FDISABLED"
181 local TARGET="$FENABLED"
184 local TARGET="$FDISABLED"
185 local SOURCE="$FENABLED"
192 if [ ! -e "$TARGET" ] && [ -e "$SOURCE" ] ; then
193 # If the "target" file do not exist and the "source" file exist
194 mv -f "$SOURCE" "$TARGET"
203 # Execute post-install hooks
204 launch_hooks "delete" "$1" "$2" "$3" "$4"
205 if [ $? -gt 10 ] ; then
206 # If the hooks return a value > 10
207 # it's mean we do not continue the
212 # Fix of a longstanding BUG: we only DELETE the vhost file if the type is a vhost one !
213 if [ -f "${TEMPLATE_DIR}/${VTYPE}.conf" ]
215 local FENABLED=$(host_conffile "$FQDN")
216 local FDISABLED="$FENABLED-disabled"
218 [ -w "$FENABLED" ] && rm -f "$FENABLED"
219 [ -w "$FDISABLED" ] && rm -f "$FDISABLED"