Alternc  latest
Alternc logiel libre pour l'hébergement
functions_hosting.sh
Go to the documentation of this file.
1 #!/bin/bash
2 
3 . /usr/lib/alternc/functions.sh
4 
5 TEMPLATE_DIR="/etc/alternc/templates/apache2"
6 HOSTING_DIR="/etc/alternc/functions_hosting"
7 
8 HTML_HOME="$ALTERNC_HTML"
9 VHOST_DIR="/var/lib/alternc/apache-vhost"
10 
11 launch_hooks() {
12  local ACTION=$1
13 
14  if [ ! $2 ] ; then
15  # If no VTYPE specified
16  return 0
17  fi
18 
19  local VTYPE=$2
20 
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"
25  return $?
26  fi
27 
28  # No specific script, return 0
29  return 0
30 }
31 
32 host_conffile() {
33  # Return the absolute path of a conf file for a FQDN
34  local FQDN="$1"
35  local U_ID=$(get_uid_by_domain "$FQDN")
36  local CONFFILE="$VHOST_DIR/${U_ID:(-1)}/$U_ID/$FQDN.conf"
37  echo $CONFFILE
38  return 0
39 }
40 
41 host_create() {
42  # Function to create a vhost for a website
43  # First, it look if there is a special file for
44  # this type of vhost
45  # If there isn't, it use the default function
46  # and the template file provided
47 
48  local VTYPE="$1"
49 
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
54  # "default" actions
55  return $?
56  fi
57 
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
61 
62  # First, usefull vars. Some may be empty or false, it's
63  # OK, it will be solve in the "case" below
64  local FQDN=$2
65  local MAIL_ACCOUNT=$3
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")
69  local G_ID="$U_ID"
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")
75 
76  # In case VTYPE don't have the same name as the template file,
77  # here we can define it
78  local TEMPLATE=''
79  case $VTYPE in
80 # "example")
81 # TEMPLATE="$TEMPLATE_DIR/an-example.conf"
82 # ;;
83  *)
84  # No template found, look if there is some in the
85  # template dir
86  [ -r "$TEMPLATE_DIR/$VTYPE" ] && TEMPLATE="$TEMPLATE_DIR/$VTYPE"
87  [ ! "$TEMPLATE" ] && [ -r "$TEMPLATE_DIR/$VTYPE.conf" ] && TEMPLATE="$TEMPLATE_DIR/$VTYPE.conf"
88  ;;
89  esac
90 
91  # If TEMPLATE is empty, stop right here
92  [ ! "$TEMPLATE" ] && return 6
93 
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"
97  return 7
98  fi
99 
100  # Create a new conf file
101  local TMP_FILE=$(mktemp "/tmp/alternc_host.XXXXXX")
102  cp "$TEMPLATE" "$TMP_FILE"
103 
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'`"
110 
111  # Put the good value in the conf file
112  sed -i \
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" \
122  $TMP_FILE
123 
124  ## Fix for wildcard
125  if [[ "$FQDN2" == "*."* ]]; then
126  sed -i "s/ServerName/ServerAlias/" $TMP_FILE
127  fi
128 
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 )
134 
135  # Put the conf file in prod
136  mkdir -p "$(dirname "$FILE_TARGET")"
137  mv -f "$TMP_FILE" "$FILE_TARGET"
138 
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
144  # "default" actions
145  return $?
146  fi
147 
148  # All is quit, we return 0
149  return 0
150 }
151 
152 host_disable() {
153  host_change_enable "disable" "$1" "$2" "$3" "$4"
154 }
155 
156 host_enable() {
157  host_change_enable "enable" "$1" "$2" "$3" "$4"
158 }
159 
160 host_change_enable() {
161  # Function to enable or disable a host
162  local STATE=$1
163 
164  # Execute hooks
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
169  # "default" actions
170  return $?
171  fi
172 
173  local TYPE=$2 # no use here, but one day, maybe... So here he is
174  local FQDN=$3
175  local FENABLED=$(host_conffile "$FQDN")
176  local FDISABLED="$FENABLED-disabled"
177 
178  case $STATE in
179  "enable")
180  local SOURCE="$FDISABLED"
181  local TARGET="$FENABLED"
182  ;;
183  "disable")
184  local TARGET="$FDISABLED"
185  local SOURCE="$FENABLED"
186  ;;
187  *)
188  return 1
189  ;;
190  esac
191 
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"
195  else
196  return 2
197  fi
198 }
199 
200 host_delete() {
201  local VTYPE=$1
202  local FQDN=$2
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
208  # "default" actions
209  return $?
210  fi
211 
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" ]
214  then
215  local FENABLED=$(host_conffile "$FQDN")
216  local FDISABLED="$FENABLED-disabled"
217 
218  [ -w "$FENABLED" ] && rm -f "$FENABLED"
219  [ -w "$FDISABLED" ] && rm -f "$FDISABLED"
220  fi
221 }
222