source: install/alternc.install @ 794

Revision 794, 18.5 KB checked in by anarcat, 7 years ago (diff)

commit skipping to 20060221201309-67438-1ad69475725370c754a58452453d0693db92624a.gz .

Line 
1#!/bin/sh
2#
3# AlternC Main install script.
4# This script should be launched only once, when installing AlternC
5# on a new server. THIS SCRIPT ERASE ALL DATA ON THE AlternC SYSTEM !!
6# YOU HAVE BEEN WARNED !
7
8set -e
9
10#######################################################################
11# Script configuration
12#
13
14# Configuration template location
15TEMPLATE_DIR="/etc/alternc/templates"
16
17# Find needed configuration files (without the initial '/')
18CONFIG_FILES=""
19
20if [ -x /etc/init.d/apache ]; then
21    CONFIG_FILES="$CONFIG_FILES etc/apache/httpd.conf etc/php4/apache/php.ini"
22fi
23if [ -x /etc/init.d/apache-ssl ]; then
24    CONFIG_FILES="$CONFIG_FILES etc/apache-ssl/httpd.conf"
25fi
26if [ -x /usr/bin/php4-cgi ]; then
27    CONFIG_FILES="$CONFIG_FILES etc/php4/cgi/php.ini"
28fi
29if [ -x /etc/init.d/bind9 ]; then
30    CONFIG_FILES="$CONFIG_FILES etc/bind/templates/zone.template
31                  etc/bind/templates/named.template etc/bind/named.conf"
32fi
33if [ -x /etc/init.d/courier-pop ]; then
34    CONFIG_FILES="$CONFIG_FILES etc/courier/authdaemonrc
35                  etc/courier/authmysqlrc"
36fi
37if [ -x /etc/init.d/mysql ]; then
38    CONFIG_FILES="$CONFIG_FILES etc/mysql/my.cnf"
39fi
40if [ -x /etc/init.d/postfix ]; then
41    CONFIG_FILES="$CONFIG_FILES etc/postfix/main.cf etc/postfix/myalias.cf
42                  etc/postfix/mydomain.cf etc/postfix/mygid.cf
43                  etc/postfix/myvirtual.cf etc/postfix/sasl/smtpd.conf"
44fi
45if [ -x /etc/init.d/proftpd ]; then
46    CONFIG_FILES="$CONFIG_FILES etc/proftpd.conf etc/welcome.msg"
47fi
48if [ -d /usr/share/squirrelmail ]; then
49    CONFIG_FILES="$CONFIG_FILES etc/squirrelmail/apache.conf"
50fi
51
52INSTALLED_CONFIG_TAR="/var/backups/alternc/etc-installed.tar.gz"
53
54#######################################################################
55# Look for modified configuration files
56#
57if [ -f "$INSTALLED_CONFIG_TAR" ]; then
58    CHANGED="`tar -zdf "$INSTALLED_CONFIG_TAR" -C / 2> /dev/null |
59              sed -e 's/^\([^:]*\).*/    \1/' | sort -u`"
60    if [ ! -z "$CHANGED" ]; then
61        echo "The following configuration files has changed since last AlternC"
62        echo "installation :"
63        echo "$CHANGED"
64        echo ""
65        if [ "$1" = "force" ]; then
66            echo "Replacing them as you requested."
67        else
68            echo "These configuration files should normally be modified by"
69            echo "changing the template in $TEMPLATE_DIR and then calling"
70            echo "$0 to perform the update."
71            echo ""
72            echo "Please examine the situation closely and call '$0 force'"
73            echo "if you still want to actually overwrite these files."
74            exit 1
75        fi
76    fi
77fi
78
79#######################################################################
80# Prepare template expansions
81#
82
83. /etc/alternc/local.sh
84
85WARNING="WARNING: Do not edit this file, edit the one in /etc/alternc/templates and launch alternc.install again."
86
87VERSION="`dpkg -s alternc | sed -n -e 's/^Version: \(.*\)/\1/p'`"
88
89# /var/alternc/dns/d/www.example.com
90FQDN_LETTER="`echo $FQDN | sed -e 's/.*\.\([^\.]\)[^\.]*\.[^\.]*$/\1/'`"
91if [ "$FQDN_LETTER" = "$FQDN" ] 
92then
93       FQDN_LETTER="_" 
94fi
95
96NS2_IP=`perl -e "\\$h = (gethostbyname(\"$NS2_HOSTNAME\"))[4];
97                 @ip = unpack('C4', \\$h);
98                 print join (\".\", @ip);"`
99
100if [ ! -z "$BIND_INTERNAL" ]; then
101    BIND_INTERNAL="$BIND_INTERNAL;"
102fi
103
104if [ -z "$MONITOR_IP" ]; then
105    MONITOR_IP="127.0.0.1"
106fi
107
108SED_SCRIPT="
109s\\%%hosting%%\\$HOSTING\\;
110s\\%%fqdn%%\\$FQDN\\;
111s\\%%public_ip%%\\$PUBLIC_IP\\;
112s\\%%internal_ip%%\\$INTERNAL_IP\\;
113s\\%%monitor_ip%%\\$MONITOR_IP\\;
114s\\%%ns1%%\\$NS1_HOSTNAME\\;
115s\\%%ns2%%\\$NS2_HOSTNAME\\;
116s\\%%bind_internal%%\\$BIND_INTERNAL\\;
117s\\%%mx%%\\$DEFAULT_MX\\;
118s\\%%dbhost%%\\$MYSQL_HOST\\;
119s\\%%dbname%%\\$MYSQL_DATABASE\\;
120s\\%%dbuser%%\\$MYSQL_USER\\;
121s\\%%dbpwd%%\\$MYSQL_PASS\\;
122s\\%%ALTERNC_LOC%%\\$ALTERNC_LOC\\;
123s\\%%mynetwork%%\\$SMTP_RELAY_NETWORKS\\;
124s\\%%warning_message%%\\$WARNING\\;
125s\\%%fqdn_lettre%%\\$FQDN_LETTER\\;
126s\\%%version%%\\$VERSION\\;
127s\\%%ns2_ip%%\\$NS2_IP\\;
128"
129
130#######################################################################
131# Backup configuration files
132#
133BACKUP_FILE="/var/backups/alternc/etc-original-`date +%Y%m%d-%H%M`.tar.gz"
134
135# Only backup what we are really going to replace
136BACKUPS=""
137for file in $CONFIG_FILES; do
138    TEMPLATE="$TEMPLATE_DIR/${file##etc/}"
139    if [ -f "$TEMPLATE" ]; then
140        BACKUPS="$BACKUPS $file"
141    fi
142done
143
144tar -zcf "$BACKUP_FILE" -C / $BACKUPS || true
145
146#######################################################################
147# Expand templates in the right place
148#
149for file in $CONFIG_FILES; do
150    TEMPLATE="$TEMPLATE_DIR/${file##etc/}"
151    if [ -f "$TEMPLATE" ]; then
152        sed -e "$SED_SCRIPT" < $TEMPLATE > /$file
153    fi
154done
155
156v v v v v v v
157#######################################################################
158# Save installed files to check them during next install
159#
160tar -zcf "$INSTALLED_CONFIG_TAR" -C / $CONFIG_FILES
161*************
162v v v v v v v
163#######################################################################
164# Save installed files to check them during next install
165#
166tar -zcf "$INSTALLED_CONFIG_TAR" -C / $CONFIG_FILES
167*************
168%vars = (
169         "hosting" => "Nom du service d'hebergement ",
170         "mx" => "Nom du serveur de mail ",
171         "ns1" => "Nom du serveur DNS primaire ",
172         "ns2" => "Nom du serveur DNS secondaire ",
173         "internal_ip" => "Adresse IP de l'interface réseau",
174         "mynetwork" => "Liste des réseaux autorisés en SMTP",
175         "fqdn" => "Nom de domaine du serveur hébergé ",
176         "bind_internal" => "Adresse IP ou classe d'IP des machines à l'intérieur de notre réseau de confiance DNS, séparés par ';', facultatif ",
177         "public_ip" => "Adresse IP annoncee par le serveur DNS pour ce serveur (vaut souvent internal_ip)",
178         "data_part" => "Nom de la partition contenant /%DATA/, pour la gestion des quotas (ex: /dev/hda3)",
179         "dbhost" => "Serveur où la base de données systeme de MySQL sera créée",
180         "dbname" => "Base de données systeme de MySQL à créer",
181         "dbuser" => "Nom d'utilisateur mysql réservé pour le Bureau",
182         "dbpwd" => "Mot de passe de l'utilisateur mysql du Bureau",
183         "monitor_ip" => "Adresse IP (ou ip/prefixe) du(des) serveur(s) dont on veut autoriser le ping vers nous et le status apache (pour le monitoring de serveur.) 127.0.0.1 par défaut",
184         "myrandom" => "Debian : Mot de passe de l'utilisateur 'debian-maint' de mysql.",
185         "ALTERNC_LOC" => "General : chemin où AlternC est installé (pour les quotas)",
186         "version" => "General : Version d'AlternC"
187         );
188^ ^ ^ ^ ^ ^ ^
189^ ^ ^ ^ ^ ^ ^
190
191######################################################################
192# Initialize database
193#
194if [ -x /etc/init.d/mysql -a ! -d /var/alternc/db/mysql ]; then
195    if /etc/init.d/mysql status | grep -v "MySQL is stopped." > /dev/null; then
196        /etc/init.d/mysql stop
197    fi
198
199    echo "Setup MySQL and database..."
200    /usr/share/alternc/install/mysql.sh $MYSQL_USER $MYSQL_PASS $MYSQL_DATABASE
201
202    /etc/init.d/mysql start
203fi
204
205########################################################################
206# Ad-hoc fixes
207#
208# Add access to the management panel
209ln -nsf /var/alternc/bureau /var/alternc/dns/$FQDN_LETTER/$FQDN
210
211# Update l18n files
212/usr/share/alternc/install/dopo.sh
213
214# Bind stuff
215touch /var/alternc/bind/automatic.conf /var/alternc/bind/slaveip.conf
216chown root:bind /var/alternc/bind/automatic.conf /var/alternc/bind/slaveip.conf
217chmod 640 /var/alternc/bind/automatic.conf /var/alternc/bind/slaveip.conf
218touch /var/run/alternc/refresh_slave
219/usr/lib/alternc/slave_dns
220
221# Apache will not start without this file
222touch /var/alternc/apacheconf/override_php.conf
223
224# Copy postfix *_checks if they do not exist
225for file in body_checks header_checks; do
226    if [ ! -e "/etc/postfix/$file" ]; then
227        cp /usr/share/alternc/install/$file /etc/postfix
228    fi
229done
230
231#######################################################################
232# Reload services
233#
234for service in apache apache-ssl postfix bind9 courier-authdaemon \
235               courier-imap courier-imap-ssl courier-pop courier-pop-ssl \
236               cron proftpd; do
237    test -x /etc/init.d/$service && /etc/init.d/$service force-reload || true
238done
239
240#######################################################################
241# Last touches
242#
243
244# Add basedir protection
245/usr/lib/alternc/basedir_prot.sh
246
247# Creating root user if needed
248HAS_ROOT=`echo "SELECT COUNT(*) FROM membres WHERE login = 'root'" |
249          mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $MYSQL_DATABASE |
250          tail -1`
251if [ "$HAS_ROOT" -eq 0 ]; then
252    echo "Creating root user..."
253    su - www-data -c /usr/share/alternc/install/newone.php
254    echo ""
255    echo "*******************************************"
256    echo "*                                         *"
257    echo "*               Root account              *"
258    echo "*               ------------              *"
259    echo "*                                         *"
260    echo "* user: root               password: root *"
261    echo "*                                         *"
262    echo "* Please change this as soon as possible! *"
263    echo "*                                         *"
264    echo "*******************************************"
265fi
266
267v v v v v v v
268*************
269v v v v v v v
270         # Postfix (serveur SMTP)
271         "etc/postfix/main.cf" => "[Postfix] Fichier de configuration principal de postfix",
272         "etc/postfix/sasl/smtpd.conf" => "[Postfix] Fichier de configuration SASL de postfix",
273         "etc/postfix/myalias.cf" => "[Postfix] Table des alias dans MySQL",
274         "etc/postfix/mydomain.cf" => "[Postfix] Table des mails en domaine dans MySQL",
275        "etc/postfix/myvirtual.cf" => "[Postfix] Table des wrappers / comptes pop dans MySQL",
276         "etc/postfix/mygid.cf" => "[Postfix] Table des mapping GID unix dans MySQL",
277
278         "redir.htaccess" => "[WebMail] Redirection url interne",
279         "etc/squirrelmail/apache.conf" => "[Squirrelmail] Fichier de conf apache",
280
281         );
282
283# Those files are copied ONLY if they don't already exist
284%onetime_vars = (
285         "etc/bind/slaveip.conf" => "[Bind9] Liste des esclaves",
286         "etc/postfix/body_checks" => "[Postfix] Regles de filtrage des mails",
287         "etc/postfix/header_checks" => "[Postfix] Regles de filtrage des en-tetes",
288         "etc/bind/automatic.conf" => "[Bind9] Fichier (vide) dynamiquement genere par PHP",
289         );
290
291
292@etcdir = (
293        "etc/alternc", "etc/bind", "etc/bind/master", "etc/mysql", "etc/courier",
294        "etc/php4",     "etc/php4/apache", "etc/apache-ssl", "etc/apache",
295        "etc/postfix", "etc/squirrelmail", "etc/php4/cgi", "etc/phpmyadmin", "usr/lib/alternc"
296        );
297
298
299#########################################################################
300# 6. Compute other variables depending on the variables setup in 4. and
301# setup some defaults
302#########################################################################
303
304%varscalc = (
305             "fqdn_lettre" => "Premiere lettre de l'avant dernier membre du fqdn",
306             "warning_message" => "Message d'avertissement insere en commentaire dans tous les fichiers de conf ...",
307             "random_hash" => "Chaine aléatoire utilisée par phpmyadmin ..."
308             );
309
310@fq=split /\./, $conf{fqdn};
311
312$conf{fqdn_lettre} = substr($fq[(scalar @fq)-2],0,1);
313$conf{warning_message} = "WARNING : Do not edit this file, edit the one in /usr/share/alternc/install/ and launch alternc.install again";
314
315if ($conf{bind_internal}) {
316    $conf{bind_internal} .= ";";
317}
318if (!$conf{monitor_ip}) {
319    $conf{monitor_ip} = "127.0.0.1";
320}
321
322$conf{random_hash} = map{("a".."z","A".."Z",0..9)[int(rand(62))]}(1..10);
323
324#########################################################################
325# 7. Create the temporary etc files, and parse them, replacing the
326# variables set in 4 and 6
327#########################################################################
328
329$TMP=`mktemp -d /tmp/alternc.install.XXXXXX`; chomp($TMP);
330mkdir("$TMP/etc",0777);
331mkdir("$TMP/usr",0777);
332mkdir("$TMP/usr/lib",0777);
333
334for ($i=0;$i<scalar @etcdir;$i++) {
335    print "creating directory /".$etcdir[$i]."\n";
336    mkdir("$TMP/".$etcdir[$i]."",0777);
337}
338
339while (($key,$val) = each(%conf_vars)) {
340    print "parsing configuration file /$key    $val  ";
341    open F,"</usr/share/alternc/install/$key" || die "Erreur Ouverture R";
342    open G,">$TMP/$key" || die "Erreur Ouverture W";
343    while ($buffer = <F>) {
344        # Replace user variables :
345        while (($kv,$vv)=each(%vars)) {
346            $buffer=~ s/%%$kv%%/$conf{$kv}/;
347        }
348        # Replace computed variables :
349        while (($kv,$vv)=each(%varscalc)) {
350        # printf $kv." : ".$conf{$kv}."\n";
351            $buffer=~ s/%%$kv%%/$conf{$kv}/;
352        }
353        print G $buffer;
354    }
355    close(F) || warn("cannot close F /usr/share/alternc/install/$key: $!");
356    close(G) || warn("cannot close G $TMP/$key: $!");
357    print "\n";
358}
359# Chmod the shell scripts (a+x)
360system("find $TMP/ -name \"*.sh\" -exec chmod a+x '{}' \\;");
361
362
363#########################################################################
364# 8. Backup current conf files
365#########################################################################
366
367print "backup current system files (/etc)\n";
368
369# Creation des dossiers dans /tmp/system.DATEDUJOUR
370chop($TS=`date +%Y%m%d-%H%M`);
371mkdir("/usr/lib/alternc/backups");
372# XXX: this should change name instead of just overwriting
373unlink("/usr/lib/alternc/backups/system.$TS.tgz");
374# prepend a slash before each etcdir: TODO : is it still necessary ?
375map { $_ =~ s/^/\//; } @etcdir;
376$cmd = "tar -czf /usr/lib/alternc/backups/system.$TS.tgz /etc  >/dev/null 2>&1";
377system($cmd) == 0 || die ("error tar $cmd: $!");
378system("/usr/share/alternc/install/dopo.sh") == 0 || die ("error dodpo");
379
380#########################################################################
381# 9. Stop services
382#########################################################################
383
384system("/etc/init.d/apache stop >&2");
385system("/etc/init.d/apache-ssl stop >&2");
386system("/etc/init.d/postfix stop >&2");
387system("/etc/init.d/bind9 stop >&2");
388system("/etc/init.d/courier-authdaemon stop >&2");
389system("/etc/init.d/courier-imap stop >&2");
390system("/etc/init.d/courier-imap-ssl stop >&2");
391system("/etc/init.d/courier-pop stop >&2");
392system("/etc/init.d/courier-pop-ssl stop >&2");
393system("/etc/init.d/cron stop >&2");
394system("/etc/init.d/proftpd stop >&2");
395system("/etc/init.d/mysql stop >&2");
396
397
398#########################################################################
399# 10. Create dir structure and copy panel files
400#########################################################################
401
402print "Creating directory structure in /var/alternc\n";
403system("/usr/share/alternc/install/initrep.sh");
404
405
406#########################################################################
407# 11. Send etc files to /etc :)
408#########################################################################
409
410print "Copying files to /etc\n";
411# 11.1 copy all the files setup in 7 in /etc, plus other places
412system("rsync -a $TMP/etc/* /etc/");
413# 11.2 setup part of the webmail
414system("cp $TMP/redir.htaccess /var/alternc/bureau/admin/webmail/.htaccess");
415# 11.3 setup part of bind
416system("cp $TMP/domaines.template /usr/lib/alternc/system/bind2/");
417# 11.4 setup part of the panel
418system("cp $TMP/local.php /var/alternc/bureau/class");
419# 11.5 setup permissions for scripts and configs
420system("chown -R www-data.www-data /var/alternc/bureau/admin/webmail/.htaccess /var/alternc/dns /var/run/alternc /var/log/alternc");
421system("chown -R root.www-data /var/alternc/bureau");
422system("chmod -R 0640 /var/alternc/bureau");
423system("chmod -R ug+X /var/alternc/bureau");
424system("chown www-data.www-data /var/alternc/html/* /var/alternc/mail/* /var/alternc/html/ /var/alternc/mail/ ");
425system("install -o root -g www-data -m0750 $TMP/usr/lib/alternc/sendmail /usr/lib/alternc/sendmail");
426
427# 11.6 symlink the bureau in dns/
428@fq=split /\./, $conf{fqdn};
429$fl=substr($fq[(scalar @fq)-2],0,1);
430symlink("/var/alternc/bureau","/var/alternc/dns/$fl/$conf{fqdn}");
431
432# 11.7 Copy 'one time' files IF REQUIRED
433while (($key,$val) = each(%onetime_vars)) {
434    if (! (-e "/$key") ) {
435      print "Copying one time file /$key    $val  ";
436      open F,"</usr/share/alternc/install/$key" || die "Erreur Ouverture R";
437      open G,">/$key" || die "Erreur Ouverture W";
438      while ($buffer = <F>) {
439          # Replace user variables :
440          while (($kv,$vv)=each(%vars)) {
441              $buffer=~ s/%%$kv%%/$conf{$kv}/;
442          }
443          # Replace computed variables :
444          while (($kv,$vv)=each(%varscalc)) {
445          # printf $kv." : ".$conf{$kv}."\n";
446              $buffer=~ s/%%$kv%%/$conf{$kv}/;
447          }
448          print G $buffer;
449      }
450      close(F);
451      close(G);
452      print "\n";
453    }
454}
455
456#########################################################################
457# 14. Create MySQL database, restart mysql and add default users
458#########################################################################
459
460print "Creating MySQL db, restarting mysql.\n";
461system("/usr/share/alternc/install/mysql.sh '".$conf{dbuser}."' '".$conf{dbpwd}."' '".$conf{dbname}."' >&2");
462
463
464#########################################################################
465# 15. Start services
466#########################################################################
467
468system("/etc/init.d/apache start >&2");
469system("/etc/init.d/apache-ssl start >&2");
470system("/etc/init.d/postfix start >&2");
471system("/etc/init.d/bind9 start >&2");
472system("/etc/init.d/courier-authdaemon start >&2");
473system("/etc/init.d/courier-imap start >&2");
474system("/etc/init.d/courier-imap-ssl start >&2");
475system("/etc/init.d/courier-pop start >&2");
476system("/etc/init.d/courier-pop-ssl start >&2");
477system("/etc/init.d/cron start >&2");
478system("/etc/init.d/proftpd start >&2");
479
480#########################################################################
481# 16. Create the first user
482#########################################################################
483
484print "Creating user root, with password root (change it as soon as possible)\n";
485
486system("su - www-data -c /usr/share/alternc/install/newone.php");
487system("/usr/lib/alternc/basedir_prot.sh");
488
489system("rm -rf $TMP");
490
491#########################################################################
492# 17. Job's done ! Ready to serve...
493#########################################################################
494
495print "
496  AlternC Install complete.
497  You may use you server now. Don't forget to change your root password
498  as soon as possible.
499  If you use this software, please send a mail to tech\@alternc.org
500  so that we know who use it :) and build some kind of statistics. Thanks.
501
502";
503
504exit(0);
505*************
506# vim: et sw=4
507^ ^ ^ ^ ^ ^ ^
508
509^ ^ ^ ^ ^ ^ ^
Note: See TracBrowser for help on using the repository browser.