3 # ----------------------------------------------------------------------
4 # AlternC - Web Hosting System
5 # Copyright (C) 2000-2012 by the AlternC Development Team.
7 # ----------------------------------------------------------------------
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License (GPL)
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # To read the license please visit http://www.gnu.org/copyleft/gpl.html
21 # ----------------------------------------------------------------------
22 # Purpose of file: Main install script, launch it anytime ;)
23 # ----------------------------------------------------------------------
25 # Somes check before start operations
26 if [ `id -u` -ne 0 ]; then
27 echo "must be launched as root"
36 export force=1; shift;;
38 export slave=1; shift;;
42 echo "unknown option $i"; shift;;
46 . /usr/lib/alternc/functions.sh
49 run-parts --arg=startup /usr/lib/alternc/install.d
51 #######################################################################
52 # Script configuration
55 # Configuration template location
56 TEMPLATE_DIR="/etc/alternc/templates"
58 # Find needed configuration files (without the initial '/')
59 # replace this one unconditionnally
60 CONFIG_FILES="etc/alternc/bureau.conf etc/incron.d/alternc etc/apache2/envvars etc/alternc/apache2.conf etc/alternc/apache_logformat.conf etc/alternc/phpmyadmin.inc.php"
62 if [ -e /etc/bind/named.conf ]; then
63 CONFIG_FILES="$CONFIG_FILES etc/bind/named.conf.options"
65 if [ -d /etc/postfix ]; then
66 CONFIG_FILES="$CONFIG_FILES etc/postfix/master.cf etc/postfix/myalias.cf etc/postfix/myrelay.cf
67 etc/postfix/mydomain.cf etc/postfix/myrelay-domain.cf etc/postfix/mygid.cf etc/postfix/myquota.cf
68 etc/postfix/myvirtual.cf etc/postfix/mytransport.cf etc/postfix/sasl/smtpd.conf
69 etc/alternc/postfix/postfix.cf etc/alternc/postfix/postfix-slave.cf"
71 if [ -e /etc/proftpd/proftpd.conf ]; then
72 CONFIG_FILES="$CONFIG_FILES etc/proftpd/proftpd.conf etc/proftpd/welcome.msg etc/proftpd/modules.conf"
75 if [ -e /etc/default/saslauthd ]; then
76 CONFIG_FILES="$CONFIG_FILES etc/default/saslauthd"
79 if [ -e /etc/dovecot/dovecot.conf ]; then
80 CONFIG_FILES="$CONFIG_FILES etc/dovecot/alternc-sql.conf etc/dovecot/alternc-dict-quota.conf etc/dovecot/conf.d/95_alternc.conf"
83 INSTALLED_CONFIG_TAR="/var/lib/alternc/backups/etc-installed.tar.gz"
85 #######################################################################
86 # Look for modified configuration files
88 if [ -f "$INSTALLED_CONFIG_TAR" ]; then
89 CHANGED="`env LANG=C tar -zdf "$INSTALLED_CONFIG_TAR" -C / 2> /dev/null |
90 grep -v 'postfix/main.cf' | grep -v 'Uid differs'|grep -v 'Gid differs' |grep -v 'Mode differs' |
91 sed -e 's/^\([^:]*\).*/ \1/' | sort -u`"
93 if [ ! -z "$CHANGED" ]; then
94 echo "The following configuration files has changed since last AlternC"
98 if [ "$force" = "1" ]; then
99 echo "Replacing them as you requested."
101 echo "These configuration files should normally be modified by"
102 echo "changing the template in $TEMPLATE_DIR and then calling"
103 echo "$0 to perform the update."
105 echo "Please examine the situation closely and call '$0 -f'"
106 echo "if you still want to actually overwrite these files."
112 #######################################################################
113 # Prepare template expansions
116 chown :alterncpanel /etc/alternc/local.sh
117 . /etc/alternc/local.sh
119 # Create the target directory
120 for i in "$ALTERNC_HTML" "$ALTERNC_MAIL" "$ALTERNC_LOGS" ; do
121 test -d "$i" || mkdir -p "$i"
124 find $ALTERNC_LOGS -maxdepth 1 -type d -exec chown alterncpanel:adm {} \;
125 find $ALTERNC_HTML -maxdepth 1 -type d -exec chown alterncpanel:alterncpanel {} \;
126 find $ALTERNC_MAIL -maxdepth 1 -type d -exec chown vmail:vmail {} \;
128 for i in a b c d e f g h i j k l m n o p q r s t u v w x y z _ 0 1 2 3 4 5 6 7 8 9; do
129 test -d "$ALTERNC_HTML/$i" || ( mkdir -p "$ALTERNC_HTML/$i" && chown www-data:www-data "$ALTERNC_HTML/$i" )
130 test -d "$ALTERNC_MAIL/$i" || ( mkdir -p "$ALTERNC_MAIL/$i" && chown vmail:vmail "$ALTERNC_MAIL/$i" && chmod 775 "$ALTERNC_MAIL/$i" )
135 aclcheckfile="$ALTERNC_HTML/test-acl"
136 touch "$aclcheckfile"
137 setfacl -m u:root:rwx "$aclcheckfile" 2>/dev/null || ( echo "Error : ACL aren't activated on $ALTERNC_HTML . AlternC can't work without it." ; test -e "$aclcheckfile" && rm -f "$aclcheckfile" ; exit 2)
138 test -e "$aclcheckfile" && rm -f "$aclcheckfile"
140 # XXX: copy-paste from debian/config
141 if [ -r /etc/alternc/my.cnf ]; then
142 # make mysql configuration available as shell variables
143 # to convert from .cnf to shell syntax, we:
144 # * match only lines with "equal" in them (/=/)
145 # * remove whitespace around the = and add a left quote operator ' (;s)
146 # * add a right quote operator at the end of line (;s)
147 # * convert mysql variables into our MYSQL_ naming convention (;s)
148 # * print the result (;p)
149 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`
150 chown root:alterncpanel /etc/alternc/my.cnf
151 chmod 640 /etc/alternc/my.cnf
154 if [ -r /etc/alternc/my_mail.cnf ]; then
155 # make mysql configuration available as shell variables
156 # to convert from .cnf to shell syntax, we:
157 # * match only lines with "equal" in them (/=/)
158 # * remove whitespace around the = and add a left quote operator ' (;s)
159 # * add a right quote operator at the end of line (;s)
160 # * convert mysql variables into our MYSQL_ naming convention (;s)
161 # * print the result (;p)
162 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`
163 chown root:alterncpanel /etc/alternc/my_mail.cnf
164 chmod 640 /etc/alternc/my_mail.cnf
167 WARNING="WARNING: Do not edit this file, edit the one in /etc/alternc/templates and launch alternc.install again."
169 if [ "$slave" = "1" ]; then
170 VERSION="`dpkg -s alternc-slave | sed -n -e 's/^Version: \(.*\)/\1/p'`"
172 VERSION="`dpkg -s alternc | sed -n -e 's/^Version: \(.*\)/\1/p'`"
175 # /var/ alternc/dns/d/www.example.com
176 FQDN_LETTER="`echo $FQDN | sed -e 's/.*\.\([^\.]\)[^\.]*\.[^\.]*$/\1/'`"
177 if [ "$FQDN_LETTER" = "$FQDN" ]
182 NS2_IP=`perl -e "\\$h = (gethostbyname(\"$NS2_HOSTNAME\"))[4];
183 @ip = unpack('C4', \\$h);
184 print join (\".\", @ip);"`
186 if [ -z "$MONITOR_IP" ]; then
187 MONITOR_IP="127.0.0.1"
190 PUBLIC_IP_BEGIN=$(echo $PUBLIC_IP|cut -c 1)
192 # Secret for PhpMyAdmin sessions
193 PHPMYADMIN_BLOWFISH="$(generate_string 24)"
195 # XXX: I assume this is secure if /tmp is sticky (+t)
196 # we should have a better way to deal with templating, of course.
197 SED_SCRIPT="/tmp/alternc.install.sedscript"
198 cat > $SED_SCRIPT <<EOF
199 s\\%%hosting%%\\$HOSTING\\;
200 s\\%%fqdn%%\\$FQDN\\;
201 s\\%%public_ip%%\\$PUBLIC_IP\\;
202 s\\%%public_ip_begin%%\\$PUBLIC_IP_BEGIN\\;
203 s\\%%internal_ip%%\\$INTERNAL_IP\\;
204 s\\%%monitor_ip%%\\$MONITOR_IP\\;
205 s\\%%ns1%%\\$NS1_HOSTNAME\\;
206 s\\%%ns2%%\\$NS2_HOSTNAME\\;
207 s\\%%mx%%\\$DEFAULT_MX\\;
208 s\\%%dbhost%%\\$MYSQL_HOST\\;
209 s\\%%dbname%%\\$MYSQL_DATABASE\\;
210 s\\%%dbuser%%\\$MYSQL_USER\\;
211 s\\%%dbpwd%%\\$MYSQL_PASS\\;
212 s\\%%db_mail_user%%\\$MYSQL_MAIL_USER\\;
213 s\\%%db_mail_pwd%%\\$MYSQL_MAIL_PASS\\;
214 s\\%%warning_message%%\\$WARNING\\;
215 s\\%%fqdn_lettre%%\\$FQDN_LETTER\\;
216 s\\%%version%%\\$VERSION\\;
217 s\\%%ns2_ip%%\\$NS2_IP\\;
218 s\\%%ALTERNC_HTML%%\\$ALTERNC_HTML\\;
219 s\\%%ALTERNC_MAIL%%\\$ALTERNC_MAIL\\;
220 s\\%%ALTERNC_LOGS%%\\$ALTERNC_LOGS\\;
221 s\\%%PHPMYADMIN_BLOWFISH%%\\$PHPMYADMIN_BLOWFISH\\;
225 test -d /usr/lib/alternc/install.d || mkdir -p /usr/lib/alternc/install.d
226 run-parts --arg=templates /usr/lib/alternc/install.d
229 ######################################################################
230 # Backup the Main database
231 DB_BACKUP="/var/lib/alternc/backups/${MYSQL_DATABASE}-db-`date +%Y%m%d-%H:%M:%S`.gz"
232 db_dump="mysqldump --defaults-file=/etc/alternc/my.cnf --add-drop-table --allow-keywords --quote-names --force --quick --add-locks --lock-tables --extended-insert ${MYSQL_DATABASE}"
233 $db_dump | /bin/gzip -c > $DB_BACKUP || echo "backup of the main database failed"
235 #######################################################################
236 # Backup configuration files
238 BACKUP_FILE="/var/lib/alternc/backups/etc-original-`date +%Y%m%d-%H%M`.tar.gz"
240 # Only backup what we are really going to replace
242 for file in $CONFIG_FILES; do
243 TEMPLATE="$TEMPLATE_DIR/${file##etc/}"
244 if [ -f "$TEMPLATE" ]; then
245 BACKUPS="$BACKUPS $file"
249 # also backup main.cf since we're doing major changes to it
250 BACKUPS="$BACKUPS etc/postfix/main.cf"
252 tar -zcf "$BACKUP_FILE" -C / $BACKUPS 2>/dev/null || true
254 #######################################################################
255 # Expand templates in the right place
257 echo -n "Expanding variables in configuration files:"
258 for file in $CONFIG_FILES; do
259 TEMPLATE="$TEMPLATE_DIR/${file##etc/}"
261 if [ -f "$TEMPLATE" ]; then
262 sed -f "$SED_SCRIPT" < $TEMPLATE > /$file
268 ########################################################################
272 php="`ls /usr/lib/apache*/*/*php*.so | sed -e 's/^.*libphp\(.\)\.so$/php\1/' | tail -1`"
273 ln -fs /etc/alternc/alternc.ini /etc/$php/conf.d/alternc.ini || true
274 if [ -x /usr/sbin/apache2 ]; then
276 run-parts --arg=apache2 /usr/lib/alternc/install.d
279 # unused from AlternC 1.0, FIXME: remove it later
280 if [ -L /etc/apache2/mods-enabled/vhost_alias.load ]
285 if ! [ -L /etc/apache2/mods-enabled/$php.load ]
289 if ! [ -L /etc/apache2/mods-enabled/rewrite.load ]
293 if [ -e /etc/alternc/apache.pem ]; then
294 # We enable proftpd tls module
295 cat /etc/proftpd/modules.conf | sed -e 's/^#LoadModule mod_tls.c/LoadModule mod_tls.c/' > /etc/proftpd/modules.conf.alternc-new
296 mv /etc/proftpd/modules.conf.alternc-new /etc/proftpd/modules.conf
297 cp /etc/proftpd/modules.conf /etc/alternc/templates/proftpd/
298 # We enable apache2 SSL :
299 if [ ! -L /etc/apache2/mods-enabled/ssl.load ] ; then
303 if [ ! -h /etc/apache2/conf.d/alternc-ssl.conf ] && [ -e /etc/apache2/conf.d/ ]; then
304 ln -sf /etc/alternc/apache2-ssl.conf /etc/apache2/conf.d/alternc-ssl.conf
308 # We enable dovecot SSL certificate instructions: (on wheezy we should use a new file in /etc/dovecot/conf.d/ )
309 ( echo "ssl_cert = </etc/alternc/apache.pem" ; echo "ssl_key = </etc/alternc/apache.pem" ) >/etc/dovecot/conf.d/96_ssl.conf
312 # We disable proftpd tls module
313 cat /etc/proftpd/modules.conf | sed -e 's/^LoadModule mod_tls.c/#LoadModule mod_tls.c/' > /etc/proftpd/modules.conf.alternc-new
314 mv /etc/proftpd/modules.conf.alternc-new /etc/proftpd/modules.conf
315 cp /etc/proftpd/modules.conf /etc/alternc/templates/proftpd/
317 # We disable dovecot SSL certificate instructions: (on wheezy we should remove a file in /etc/dovecot/conf.d/ )
318 ( echo "ssl_cert = </etc/ssl/certs/ssl-cert-snakeoil.pem" ; echo "ssl_key = </etc/ssl/private/ssl-cert-snakeoil.key" ) >/etc/dovecot/conf.d/96_ssl.conf
320 echo "SSL not configured"
321 echo "create a certificate in /etc/alternc/apache.pem and rerun alternc.install"
323 if [ ! -h /etc/apache2/conf.d/alternc.conf ] && [ -e /etc/apache2/conf.d/ ]; then
324 ln -sf /etc/alternc/apache2.conf /etc/apache2/conf.d/alternc.conf
327 if [ -e /etc/apache2/sites-enabled/000-default ]; then
331 SERVICES="$SERVICES $s"
334 # Copy postfix *_checks if they do not exist
335 for file in body_checks header_checks; do
336 if [ ! -e "/etc/postfix/$file" ]; then
337 cp /usr/share/alternc/install/$file /etc/postfix
341 # Attribute the correct rights to critical postfix files
342 if [ -e /etc/postfix/myalias.cf -o -e /etc/postfix/mydomain.cf -o -e /etc/postfix/mygid.cf -o -e /etc/postfix/myrelay-domain.sh -o -e /etc/postfix/myvirtual.cf -o -e /etc/postfix/myrelay.cf -o -e /etc/postfix/myquota.cf ]; then
343 chown root:postfix /etc/postfix/my*
344 chmod 640 /etc/postfix/my*
347 if [ ! -f /etc/postfix/main.cf ]
349 echo "****************************************"
350 echo "POSTFIX is NOT configured properly"
351 echo "please launch dpkg-reconfigure -plow postfix"
352 echo "and choose 'Internet Site'"
353 echo "then launch alternc.install again"
354 echo "****************************************"
358 # configure Postfix appropriatly for our needs
359 if [ "$slave" = "1" ]; then
360 postfix_conf=/etc/alternc/postfix/postfix-slave.cf
362 postfix_conf=/etc/alternc/postfix/postfix.cf
364 grep -v '^\ *#' $postfix_conf |while read line ; do
365 if echo "$line" | grep -qi '^smtpd_tls_cert_file' ;then
366 line_strip=`echo "$line"|tr -d '[:blank:]'`
368 cert_file=${line_strip#$pattern}
369 if [ -e $cert_file ];then
372 echo "****************************************"
373 echo "The certificate file : $cert_file does not exists"
374 echo "please go to https://alternc.org/wiki/SSL to get information on how to create a certificate"
375 echo "Then edit the file /etc/alternc/postfix/postfix.cf to add"
376 echo "the cert file path name to the key :smtpd_tls_cert_file"
377 echo "Finally relaunch alternc.install"
378 echo "****************************************"
385 # Conviguring delivery used by Postfix
386 /usr/lib/alternc/alternc_add_policy_dovecot
388 # Bug #1215: configure mydestination when $FQDN is not in
389 OLDDESTINATION=`postconf mydestination | awk -F '=' '{print $2}'`
390 echo "$OLDDESTINATION" | grep -q -v "$FQDN" && postconf -e "mydestination = $FQDN, $OLDDESTINATION"
392 # Configure PHPMyAdmin
393 include_str='include("/etc/alternc/phpmyadmin.inc.php")'
394 pma_config='/etc/phpmyadmin/config.inc.php'
396 # Sur une configuration vierge, inclure la configuration alternc
397 if ! grep -e "${include_str/\"/\\\"}" $pma_config > /dev/null 2>&1; then
398 echo "$include_str;" >> $pma_config
401 # Le template de /etc/alternc/phpmyadmin.inc.php viens d'être réappliqué, on
402 # regénére la liste des serveurs MySQL disponible dedans.
403 mysql_query "select id,host,name from db_servers;" | while read id host name ; do
405 // Server #$id in db_servers
407 \$cfg['Servers'][\$i]['connect_type'] = 'tcp'; // How to connect to MySQL server ('tcp' or 'socket')
408 \$cfg['Servers'][\$i]['auth_type'] = 'cookie'; // Authentication method (config, http or cookie based)?
409 \$cfg['Servers'][\$i]['hide_db'] = 'information_schema';
410 \$cfg['Servers'][\$i]['verbose'] = '$name'; // human name
411 \$cfg['Servers'][\$i]['host'] = '$host'; // MySQL hostname or IP address
412 " >> '/etc/alternc/phpmyadmin.inc.php'
415 # Reload incron. Useless, but who know?
416 SERVICES="$SERVICES incron"
418 if [ -e /etc/proftpd.conf ] ; then
419 chmod 640 /etc/proftpd/proftpd.conf
422 if [ -x /usr/sbin/locale-gen ] ; then
423 touch /etc/locale.gen
425 # Add de_DE ISO-8859-1, en_US ISO-8859-1, es_ES ISO-8859-1, fr_FR ISO-8859-1 to the locales :
426 if ! grep -q "^de_DE ISO-8859-1$" /etc/locale.gen ; then
427 echo "de_DE ISO-8859-1" >>/etc/locale.gen
430 if ! grep -q "^en_US ISO-8859-1$" /etc/locale.gen ; then
431 echo "en_US ISO-8859-1" >>/etc/locale.gen
434 if ! grep -q "^es_ES ISO-8859-1$" /etc/locale.gen ; then
435 echo "es_ES ISO-8859-1" >>/etc/locale.gen
438 if ! grep -q "^fr_FR ISO-8859-1$" /etc/locale.gen ; then
439 echo "fr_FR ISO-8859-1" >>/etc/locale.gen
442 if ! grep -q "^de_DE.UTF-8 UTF-8$" /etc/locale.gen ; then
443 echo "de_DE.UTF-8 UTF-8" >>/etc/locale.gen
446 if ! grep -q "^fr_FR.UTF-8 UTF-8$" /etc/locale.gen ; then
447 echo "fr_FR.UTF-8 UTF-8" >>/etc/locale.gen
450 if ! grep -q "^es_ES.UTF-8 UTF-8$" /etc/locale.gen ; then
451 echo "es_ES.UTF-8 UTF-8" >>/etc/locale.gen
454 if ! grep -q "^en_US.UTF-8 UTF-8$" /etc/locale.gen ; then
455 echo "en_US.UTF-8 UTF-8" >>/etc/locale.gen
458 if ! grep -q "^it_IT.UTF-8 UTF-8$" /etc/locale.gen ; then
459 echo "it_IT.UTF-8 UTF-8" >>/etc/locale.gen
462 if ! grep -q "^nl_NL.UTF-8 UTF-8$" /etc/locale.gen ; then
463 echo "nl_NL.UTF-8 UTF-8" >>/etc/locale.gen
466 if [ "$LOCALECHANGED" ] ; then
471 # remaining steps are only for the master
472 if [ "$slave" = "1" ]; then
476 #######################################################################
477 # populate alternc database with the mailname used by postfix to send mail for each vhost
479 mailname=`cat /etc/mailname`
480 mysql_query "update variable SET value='"$mailname"' where name='mailname_bounce';"
481 # Allow for all the users to view /etc/mailname
482 chmod +r "/etc/mailname"
484 #######################################################################
485 # Save installed files to check them during next install
487 tar -zcf "$INSTALLED_CONFIG_TAR" -C / $CONFIG_FILES
489 #######################################################################
493 find $ALTERNC_HTML -maxdepth 1 -type d -exec setfacl -b -k -m d:g:alterncpanel:-wx -m g:alterncpanel:-wx {} \;
496 if [ ! -e "/var/log/alternc/bureau.log" ]; then
497 test -d "/var/log/alternc/" || mkdir -p "/var/log/alternc/"
498 touch "/var/log/alternc/bureau.log"
501 if [ ! -e "/var/log/alternc/update_domains.log" ]; then
502 test -d "/var/log/alternc/" || mkdir -p "/var/log/alternc/"
503 touch "/var/log/alternc/update_domains.log"
506 # Be sure of the owner of the logs files
507 chmod 640 /var/log/alternc/bureau.log /var/log/alternc/update_domains.log
508 chown alterncpanel:adm /var/log/alternc/bureau.log /var/log/alternc/update_domains.log
510 # Creating admin user if needed
511 HAS_ROOT=`mysql --defaults-file=/etc/alternc/my.cnf -e "SELECT COUNT(*) FROM membres WHERE login = 'admin' OR login = 'root' and su = 1" | tail -1`
513 if [ "$HAS_ROOT" != "1" ]; then
514 echo "Creating admin user..."
517 if su - alterncpanel -s /bin/bash -c /usr/share/alternc/install/newone.php
519 echo "*******************************************"
521 echo "* Admin account *"
522 echo "* ------------ *"
524 echo "* user: admin password: admin *"
526 echo "* Please change this as soon as possible! *"
528 echo "*******************************************"
530 echo "Unable to create the first AlternC account (named 'admin'). newone.php returned $?. Check your MySQL database, PHP, and the /etc/alternc/local.sh file. Also check for any error above during install."
534 #giving vmail user read access on dovecot sql file
535 chgrp vmail /etc/dovecot/dovecot.conf
536 chmod g+r /etc/dovecot/dovecot.conf
538 # Changing owner of web panel's files
539 chown -R alterncpanel:alterncpanel "/usr/share/alternc/panel/"
541 # We force the re-computing of the DNS zones, since we may have changed the IP address (see #460)
542 /usr/bin/mysql --defaults-file="/etc/alternc/my.cnf" -B -e "update domaines set dns_action='update';"
545 run-parts --arg=before-reload /usr/lib/alternc/install.d
547 #######################################################################
550 for service in postfix bind9 apache2 dovecot cron proftpd; do
551 invoke-rc.d $service force-reload || true
554 # We should restart apaches after all configuration stuff ...
555 for service in $SERVICES; do
556 test -x /etc/init.d/$service && invoke-rc.d $service stop || true
558 for service in $SERVICES; do
559 test -x /etc/init.d/$service && invoke-rc.d $service start || true
563 echo "Fix all the permission. May be quite long..."
564 /usr/lib/alternc/fixperms.sh
567 run-parts --arg=end /usr/lib/alternc/install.d