Alternc  latest
Alternc logiel libre pour l'hébergement
alternc.config
Go to the documentation of this file.
1 #!/bin/bash -e
2 
3 # Source debconf library.
4 . /usr/share/debconf/confmodule
5 
6 db_capb backup
7 
8 # Validate an IPv4 address.
9 function valid_ip()
10 {
11  local ip=$1
12  local stat=1
13 
14  if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
15  OIFS=$IFS
16  IFS='.'
17  ip=($ip)
18  IFS=$OIFS
19  [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
20  && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
21  stat=$?
22  fi
23  return $stat
24 }
25 
26 
27 # Checking mysql connectivity and updating local.sh environment variables accordingly
28 check_mysql()
29 {
30  STATE=0
31  while [ "$STATE" -eq 0 ]; do
32  db_input high alternc/mysql/host || true
33  db_go
34  db_get alternc/mysql/host || true
35  MYSQL_HOST="$RET"
36  db_input high alternc/mysql/remote_user || true
37  db_go
38  db_get alternc/mysql/remote_user || true
39  MYSQL_USER="$RET"
40  db_input high alternc/mysql/remote_password || true
41  db_go
42  db_get alternc/mysql/remote_password || true
43  MYSQL_PASS="$RET"
44  db_input high alternc/mysql/client || true
45  db_go
46  db_get alternc/mysql/client || true
47  MYSQL_CLIENT="$RET"
48  if [ "`mysql -u"$MYSQL_USER" -p"$MYSQL_PASS" -h"$MYSQL_HOST" -Bse 'SELECT "OK";' 2>/dev/null`" = "OK" ]; then
49  STATE=`expr $STATE + 1`
50  else
51  db_input high alternc/retry_remote_mysql || true
52  if [ $? -eq 30 ]; then
53  db_input high alternc/remote_mysql_error || true
54  db_go || true
55  exit 4
56  fi
57  db_go
58  db_get alternc/retry_remote_mysql || true
59  if [ "$RET" = "false" ]; then
60  db_input high alternc/remote_mysql_error || true
61  db_go || true
62  exit 4
63  fi
64  fi
65  done
66 }
67 
68 # Return the deepest existing directory in a path
69 function get_first_existing_dir()
70 {
71  dir="$1"
72  if [ -z "$dir" ] ; then
73  return 0
74  fi
75  if ! test -d "$dir" ; then
76  get_first_existing_dir "$(dirname "$dir")"
77  else
78  echo "$dir"
79  fi
80 }
81 
82 
83 # Compute default values for local.sh
84 MYSQL_HOST=127.0.0.1
85 MYSQL_DATABASE=alternc
86 MYSQL_USER=sysusr
87 MYSQL_PASS="`perl -e 'print map{("a".."z","A".."Z",0..9)[int(rand(62))]}(1..10)' `"
88 MYSQL_MAIL_USER=alternc_user
89 MYSQL_MAIL_PASS="`perl -e 'print map{("a".."z","A".."Z",0..9)[int(rand(62))]}(1..10)' `"
90 MYSQL_CLIENT=localhost
91 FQDN="`(cat /etc/mailname 2>/dev/null || hostname -f)|tr '[:upper:]' '[:lower:]'`"
92 INTERNAL_IP="`env LANG=C ip addr show|grep 'inet ' | grep -v 127.0.0.1| head -1 | sed -e 's/^.*inet \‍([0-9\.]*\‍).*$/\1/' 2>/dev/null || hostname -f`"
93 PUBLIC_IP="$INTERNAL_IP"
94 DEFAULT_MX="`cat /etc/mailname 2>/dev/null || hostname -f`"
95 ALTERNC_HTML="/var/www/alternc/"
96 ALTERNC_MAIL="/var/mail/alternc"
97 ALTERNC_LOGS="/var/log/alternc/sites/"
98 NS1_HOSTNAME="$FQDN"
99 NS2_HOSTNAME="$FQDN"
100 HOSTING="AlternC"
101 SQLBACKUP_TYPE="rotate"
102 SQLBACKUP_OVERWRITE="no"
103 QUEST_STATE=1
104 
105 while [ "$QUEST_STATE" != 0 -a "$QUEST_STATE" != 14 ]; do
106  case "$QUEST_STATE" in
107  1)
108  if [ -r /etc/alternc/my.cnf ]; then
109  MYSQL_USER=$(cat /etc/alternc/my.cnf |grep "^user"|sed -r 's/user="(.*)"/\1/');
110  MYSQL_PASS=$(cat /etc/alternc/my.cnf |grep "^password"|sed -r 's/password="(.*)"/\1/');
111  MYSQL_DATABASE=$(cat /etc/alternc/my.cnf |grep "^database"|sed -r 's/database="(.*)"/\1/');
112  MYSQL_HOST=$(cat /etc/alternc/my.cnf |grep "^host"|sed -r 's/host="(.*)"/\1/');
113  db_set alternc/mysql/host "$MYSQL_HOST"
114  db_set alternc/mysql/db "$MYSQL_DATABASE"
115  db_set alternc/mysql/user "$MYSQL_USER"
116  db_set alternc/mysql/password "$MYSQL_PASS"
117  fi
118 
119  if [ -r /etc/alternc/local.sh ]; then
120  # source the current config
121  . /etc/alternc/local.sh
122  # and push it into debconf (its values have priority over anything!)
123  db_set alternc/hostingname "$HOSTING"
124  db_set alternc/desktopname "`echo $FQDN | tr '[:upper:]' '[:lower:]'`"
125  db_set alternc/public_ip "$PUBLIC_IP"
126  db_set alternc/internal_ip "$INTERNAL_IP"
127  db_set alternc/ns1 "$NS1_HOSTNAME"
128  db_set alternc/ns2 "$NS2_HOSTNAME"
129  db_set alternc/default_mx "$DEFAULT_MX"
130  db_set alternc/alternc_html "$ALTERNC_HTML"
131  db_set alternc/alternc_mail "$ALTERNC_MAIL"
132  db_set alternc/alternc_logs "$ALTERNC_LOGS"
133  db_set alternc/monitor_ip "$MONITOR_IP"
134  db_set alternc/default_mx2 "$DEFAULT_SECONDARY_MX"
135  db_set alternc/mysql/client "$MYSQL_CLIENT"
136  db_set alternc/sql/backup_type "$SQLBACKUP_TYPE"
137  db_set alternc/sql/backup_overwrite "$SQLBACKUP_OVERWRITE"
138  db_set alternc/mysql/alternc_mail_user "$MYSQL_MAIL_USER"
139  db_set alternc/mysql/alternc_mail_password "$MYSQL_MAIL_PASS"
140  fi
141  # upgrade <= 3.0 to >= 3.1
142  if [ "x$ALTERNC_LOC" != "x" ]; then
143  ALTERNC_HTML="$ALTERNC_LOC/html"
144  ALTERNC_MAIL="$ALTERNC_LOC/mail"
145  db_set alternc/alternc_html "$ALTERNC_HTML"
146  db_set alternc/alternc_mail "$ALTERNC_MAIL"
147  fi
148 
149  # We ask for the hosting name and the FQDN
150  db_get alternc/hostingname
151  if [ -z "$RET" ]; then
152  db_set alternc/hostingname "$HOSTING"
153  db_input high alternc/hostingname || true
154  fi
155  ;;
156  2)
157  # Get the FQDN
158  db_get alternc/desktopname
159  if [ -z "$RET" ]; then
160  db_set alternc/desktopname "$FQDN"
161  db_input high alternc/desktopname || true
162  fi
163  # Ensure that the FQDN is lowercase (Fixes #1405)
164  db_get alternc/desktopname
165  db_set alternc/desktopname "`echo $RET | tr '[:upper:]' '[:lower:]'`"
166  ;;
167  3)
168  # Ask for the public and private ip
169  db_get alternc/public_ip
170  if [ -z "$RET" ]; then
171  db_set alternc/public_ip "$PUBLIC_IP"
172  fi
173  STATE=0
174  while [ $STATE -eq 0 ]; do
175  db_input high alternc/public_ip || true
176  db_go
177  db_get alternc/public_ip
178  ip="$RET"
179  if valid_ip $ip ; then
180  if [ $(echo $ip |egrep '(^127\.)|(^192\.168\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^::1)$') ]; then
181  db_input high alternc/use_private_ip || true
182  db_go
183  db_get alternc/use_private_ip
184  if [ "$RET" = "true" ]; then
185  STATE=1
186  else
187  db_reset alternc/use_private_ip || true
188  db_fset alternc/use_private_ip "seen" "false" || true
189  fi
190  else
191  STATE=1
192  fi
193  else
194  STATE=1
195  fi
196  done
197  ;;
198  4)
199  # Private IP
200  db_get alternc/internal_ip
201  if [ -z "$RET" ]; then
202  db_set alternc/internal_ip "$INTERNAL_IP"
203  fi
204 
205  STATE=0
206  while [ $STATE -eq 0 ]; do
207  db_input high alternc/internal_ip || true
208  db_go
209  db_get alternc/internal_ip
210  ip="$RET"
211  if valid_ip $ip ; then
212  STATE=1
213  fi
214  done
215  ;;
216  5)
217  # Ask for the DNS servers
218  db_get alternc/ns1
219  if [ -z "$RET" ]; then
220  db_set alternc/ns1 "$NS1_HOSTNAME"
221  db_input high alternc/ns1 || true
222  fi
223  ;;
224  6)
225  db_get alternc/ns2
226  if [ -z "$RET" ]; then
227  db_set alternc/ns2 "$NS2_HOSTNAME"
228  db_input high alternc/ns2 || true
229  fi
230  ;;
231  7)
232  db_get alternc/default_mx
233  if [ -z "$RET" ]; then
234  db_set alternc/default_mx "$DEFAULT_MX"
235  db_input high alternc/default_mx || true
236  fi
237  ;;
238  8)
239  if [ "`mysql --defaults-file=/etc/mysql/debian.cnf -Bse 'SELECT "OK";' 2>/dev/null`" = "OK" ]; then
240  db_input critical alternc/use_local_mysql || true
241  fi
242  ;;
243  9)
244  db_get alternc/use_local_mysql
245  if [ "$RET" != "true" ]; then
246  db_input critical alternc/use_remote_mysql || true
247  fi
248  ;;
249  10)
250  db_get alternc/use_remote_mysql
251  if [ "$RET" == "true" ]; then
252  # User want to use a remote server
253  check_mysql
254  fi
255  ;;
256  11)
257  # We need to set the alternc_location here because we have to test for quotas and acls
258 
259  db_get alternc/alternc_html
260  if [ -z "$RET" ]; then
261  db_set alternc/alternc_html $ALTERNC_HTML
262  db_input high alternc/alternc_html || true
263  fi
264  db_get alternc/alternc_html
265 
266  ALTERNC_HTML="$RET"
267  # Checking acl and quota activation.
268  basedir=`get_first_existing_dir "$ALTERNC_HTML"`;
269  MOUNT_POINT=$(df -P ${basedir} | tail -n 1 | awk '{print $6}')
270  # Get the first existing dir
271  aclcheckfile="$basedir/test-acl"
272  touch "$aclcheckfile"
273  setfacl -m u:root:rwx "$aclcheckfile" || (
274  test -e "$aclcheckfile" && rm -f "$aclcheckfile" || true
275  db_input high alternc/acluninstalled || true
276  db_go || true
277  exit 2
278  )
279  test -e "$aclcheckfile" && rm -f "$aclcheckfile" || true
280 
281  quota -gA -f $MOUNT_POINT || (
282  db_get alternc/quotauninstalled
283  if [ -z "$RET" ]; then
284  db_input critical alternc/quotauninstalled || true
285  db_go
286  db_set alternc/quotauninstalled "false" || true
287  fi
288  )
289  ;;
290  12)
291  db_get alternc/alternc_mail
292  if [ -z "$RET" ]; then
293  db_set alternc/alternc_mail $ALTERNC_MAIL
294  db_input high alternc/alternc_mail || true
295  fi
296  ;;
297  13)
298  db_get alternc/alternc_logs
299  if [ -z "$RET" ];then
300  db_set alternc/alternc_logs $ALTERNC_LOGS
301  db_input high alternc/alternc_logs || true
302  fi
303  ;;
304  esac
305  if db_go; then
306  QUEST_STATE=$(($QUEST_STATE + 1))
307  else
308  QUEST_STATE=$(($QUEST_STATE - 1))
309  fi
310 done
311 
312 if [ -r /etc/alternc/my.cnf ]; then
313  # make mysql configuration available as shell variables
314  # to convert from .cnf to shell syntax, we:
315  # * match only lines with "equal" in them (/=/)
316  # * remove whitespace around the = and add a left quote operator ' (;s)
317  # * add a right quote operator at the end of line (;s)
318  # * convert mysql variables into our MYSQL_ naming convention (;s)
319  # * print the result (;p)
320  eval `sed -n -e "/=/{s/ *= *\"\?/='/;s/\"\?\$/'/;s/host/MYSQL_HOST/;s/user/MYSQL_USER/;s/password/MYSQL_PASS/;s/database/MYSQL_DATABASE/;p}" /etc/alternc/my.cnf`
321 fi
322 
323 if [ -r /etc/alternc/my_mail.cnf ]; then
324  # make mysql configuration available as shell variables
325  # to convert from .cnf to shell syntax, we:
326  # * match only lines with "equal" in them (/=/)
327  # * remove whitespace around the = and add a left quote operator ' (;s)
328  # * add a right quote operator at the end of line (;s)
329  # * convert mysql variables into our MYSQL_ naming convention (;s)
330  # * print the result (;p)
331  eval `sed -n -e "/=/{s/ *= *\"\?/='/;s/\"\?\$/'/;s/host/MYSQL_HOST/;s/user/MYSQL_MAIL_USER/;s/password/MYSQL_MAIL_PASS/;s/database/MYSQL_DATABASE/;p}" /etc/alternc/my_mail.cnf`
332 fi
333 
334 db_get alternc/monitor_ip
335 if [ -z "$RET" ]; then
336  db_set alternc/monitor_ip "$MONITOR_IP"
337 fi
338 
339 db_get alternc/default_mx2
340 if [ -z "$RET" ]; then
341  db_set alternc/default_mx2 "$DEFAULT_SECONDARY_MX"
342 fi
343 
344 db_get alternc/mysql/host
345 if [ -z "$RET" ]; then
346  db_set alternc/mysql/host "$MYSQL_HOST"
347 fi
348 
349 # Even if we asked the question concerning the database earlier in the process
350 # Those calls are needed to pass the variable of remote sql server to AlternC
351 db_get alternc/mysql/db
352 if [ -z "$RET" ]; then
353  db_set alternc/mysql/db "$MYSQL_DATABASE"
354 fi
355 
356 db_get alternc/mysql/user
357 if [ -z "$RET" ]; then
358  db_get alternc/mysql/remote_user
359  if [ -z "$RET" ]; then
360  db_set alternc/mysql/user "$MYSQL_USER"
361  else
362  db_set alternc/mysql/user "$RET"
363  fi
364 fi
365 
366 db_get alternc/mysql/password
367 if [ -z "$RET" ]; then
368  db_get alternc/mysql/remote_password
369  if [ -z "$RET" ]; then
370  db_set alternc/mysql/password "$MYSQL_PASS"
371  else
372  db_set alternc/mysql/password "$RET"
373  fi
374 fi
375 
376 db_get alternc/mysql/client
377 if [ -z "$RET" ]; then
378  db_set alternc/mysql/client "$MYSQL_CLIENT"
379 fi
380 
381 db_get alternc/sql/backup_type
382 if [ -z "$RET" ]; then
383  db_set alternc/sql/backup_type "$SQLBACKUP_TYPE"
384 fi
385 
386 db_get alternc/sql/backup_overwrite
387 if [ -z "$RET" ]; then
388  db_set alternc/sql/backup_overwrite "$SQLBACKUP_OVERWRITE"
389 fi
390 
391 db_get alternc/mysql/alternc_mail_user
392 if [ -z "$RET" ]; then
393  db_set alternc/mysql/alternc_mail_user "$MYSQL_MAIL_USER"
394 fi
395 
396 db_get alternc/mysql/alternc_mail_password
397 if [ -z "$RET" ]; then
398  db_set alternc/mysql/alternc_mail_password "$MYSQL_MAIL_PASS"
399 fi
400 
401 db_input low alternc/default_mx2 || true
402 db_input low alternc/mysql/host || true
403 db_input low alternc/mysql/db || true
404 db_input low alternc/mysql/user || true
405 db_input low alternc/mysql/password || true
406 db_input low alternc/mysql/alternc_mail_password || true
407 db_input low alternc/mysql/alternc_mail_user || true
408 db_input low alternc/mysql/client || true
409 db_input low alternc/sql/backup_type || true
410 db_input low alternc/sql/overwrite || true
411 db_input low alternc/monitor_ip || true
412 db_input low alternc/slaves || true
413 db_go
414 
415 # vim: et sw=4