ftimme
2011-09-28 ca0b77f95a10f7d81714667557d4946ce5b50a9e
commit | author | age
32d8e9 1 <?php
T 2
3 /*
4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 class installer_dist extends installer_base {
32     
33     function configure_postfix($options = '')
34     {
35         global $conf;
36         $cf = $conf['postfix'];
37         $config_dir = $cf['config_dir'];
38         
39         if(!is_dir($config_dir)){
40             $this->error("The postfix configuration directory '$config_dir' does not exist.");
41         }
42         
43         //* mysql-virtual_domains.cf
44         $this->process_postfix_config('mysql-virtual_domains.cf');
45
46         //* mysql-virtual_forwardings.cf
47         $this->process_postfix_config('mysql-virtual_forwardings.cf');
48
49         //* mysql-virtual_mailboxes.cf
50         $this->process_postfix_config('mysql-virtual_mailboxes.cf');
51
52         //* mysql-virtual_email2email.cf
53         $this->process_postfix_config('mysql-virtual_email2email.cf');
54
55         //* mysql-virtual_transports.cf
56         $this->process_postfix_config('mysql-virtual_transports.cf');
57
58         //* mysql-virtual_recipient.cf
59         $this->process_postfix_config('mysql-virtual_recipient.cf');
60
61         //* mysql-virtual_sender.cf
62         $this->process_postfix_config('mysql-virtual_sender.cf');
63
64         //* mysql-virtual_client.cf
65         $this->process_postfix_config('mysql-virtual_client.cf');
66         
67         //* mysql-virtual_relaydomains.cf
68         $this->process_postfix_config('mysql-virtual_relaydomains.cf');
429dcf 69         
T 70         //* mysql-virtual_relayrecipientmaps.cf
71         $this->process_postfix_config('mysql-virtual_relayrecipientmaps.cf');
32d8e9 72
T 73         //* Changing mode and group of the new created config files.
74         caselog('chmod o= '.$config_dir.'/mysql-virtual_*.cf* &> /dev/null',
75                  __FILE__, __LINE__, 'chmod on mysql-virtual_*.cf*', 'chmod on mysql-virtual_*.cf* failed');
76         caselog('chgrp '.$cf['group'].' '.$config_dir.'/mysql-virtual_*.cf* &> /dev/null', 
77                 __FILE__, __LINE__, 'chgrp on mysql-virtual_*.cf*', 'chgrp on mysql-virtual_*.cf* failed');
78         
1ca823 79         if(!is_dir($cf['vmail_mailbox_base'])) mkdir($cf['vmail_mailbox_base']);
T 80         
32d8e9 81         //* Creating virtual mail user and group
fdb514 82         if(is_group($cf['vmail_groupname'])) {
T 83             $command = 'groupmod -g '.$cf['vmail_groupid'].' '.$cf['vmail_groupname'];
84             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
85         } else {
86             $command = 'groupadd -g '.$cf['vmail_groupid'].' '.$cf['vmail_groupname'];
87             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
88         }
89         
90         if(is_user($cf['vmail_username'])) {
91             $command = 'usermod -g '.$cf['vmail_groupname'].' -u '.$cf['vmail_userid'].' -d '.$cf['vmail_mailbox_base'].' -s /bin/bash '.$cf['vmail_username'];
92             caselog("$command &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
93         } else {
94             $command = 'useradd -g '.$cf['vmail_groupname'].' -u '.$cf['vmail_userid'].' '.$cf['vmail_username'].' -d '.$cf['vmail_mailbox_base'].' -m';
95             caselog("$command &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
8f898a 96         }
T 97         
d2e848 98         if($cf['vmail_mailbox_base'] != '' && strlen($cf['vmail_mailbox_base']) >= 10 && $this->is_update === false) exec('chown -R '.$cf['vmail_username'].':'.$cf['vmail_groupname'].' '.$cf['vmail_mailbox_base']);
32d8e9 99
T 100         $postconf_commands = array (
101             'myhostname = '.$conf['hostname'],
102             'mydestination = '.$conf['hostname'].', localhost, localhost.localdomain',
103             'mynetworks = 127.0.0.0/8 [::1]/128',
104             'virtual_alias_domains =',
105             'virtual_alias_maps = proxy:mysql:'.$config_dir.'/mysql-virtual_forwardings.cf, mysql:'.$config_dir.'/mysql-virtual_email2email.cf',
106             'virtual_mailbox_domains = proxy:mysql:'.$config_dir.'/mysql-virtual_domains.cf',
107             'virtual_mailbox_maps = proxy:mysql:'.$config_dir.'/mysql-virtual_mailboxes.cf',
108             'virtual_mailbox_base = '.$cf['vmail_mailbox_base'],
109             'virtual_uid_maps = static:'.$cf['vmail_userid'],
110             'virtual_gid_maps = static:'.$cf['vmail_groupid'],
111             'smtpd_sasl_auth_enable = yes',
112             'broken_sasl_auth_clients = yes',
113             'smtpd_sasl_authenticated_header = yes',
114             'smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, check_recipient_access mysql:'.$config_dir.'/mysql-virtual_recipient.cf, reject_unauth_destination',
115             'smtpd_use_tls = yes',
116             'smtpd_tls_security_level = may',
117             'smtpd_tls_cert_file = '.$config_dir.'/smtpd.cert',
118             'smtpd_tls_key_file = '.$config_dir.'/smtpd.key',
119             'transport_maps = proxy:mysql:'.$config_dir.'/mysql-virtual_transports.cf',
120             'relay_domains = mysql:'.$config_dir.'/mysql-virtual_relaydomains.cf',
429dcf 121             'relay_recipient_maps = mysql:'.$config_dir.'/mysql-virtual_relayrecipientmaps.cf',
32d8e9 122             'proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_domains $canonical_maps $sender_canonical_maps $recipient_canonical_maps $relocated_maps $transport_maps $mynetworks $virtual_mailbox_limit_maps',
T 123             'smtpd_sender_restrictions = check_sender_access mysql:'.$config_dir.'/mysql-virtual_sender.cf',
124             'smtpd_client_restrictions = check_client_access mysql:'.$config_dir.'/mysql-virtual_client.cf',
125             'maildrop_destination_concurrency_limit = 1',
126             'maildrop_destination_recipient_limit   = 1',
127             'virtual_transport = maildrop',
128             'header_checks = regexp:'.$config_dir.'/header_checks',
129             'mime_header_checks = regexp:'.$config_dir.'/mime_header_checks',
130             'nested_header_checks = regexp:'.$config_dir.'/nested_header_checks',
131             'body_checks = regexp:'.$config_dir.'/body_checks',
132             'inet_interfaces = all'
133         );
134         
135         //* Create the header and body check files
136         touch($config_dir.'/header_checks');
137         touch($config_dir.'/mime_header_checks');
138         touch($config_dir.'/nested_header_checks');
139         touch($config_dir.'/body_checks');
140         
141         
142         //* Make a backup copy of the main.cf file
143         copy($config_dir.'/main.cf', $config_dir.'/main.cf~');
144         
145         //* Executing the postconf commands
146         foreach($postconf_commands as $cmd) {
147             $command = "postconf -e '$cmd'";
148             caselog($command." &> /dev/null", __FILE__, __LINE__, 'EXECUTED: '.$command, 'Failed to execute the command '.$command);
149         }
150         
151         if(!stristr($options,'dont-create-certs')) {
152             //* Create the SSL certificate
153             $command = 'cd '.$config_dir.'; '
154                       .'openssl req -new -outform PEM -out smtpd.cert -newkey rsa:2048 -nodes -keyout smtpd.key -keyform PEM -days 365 -x509';
155             exec($command);
156         
157             $command = 'chmod o= '.$config_dir.'/smtpd.key';
158             caselog($command.' &> /dev/null', __FILE__, __LINE__, 'EXECUTED: '.$command, 'Failed to execute the command '.$command);
159         }
160         
161         //** We have to change the permissions of the courier authdaemon directory to make it accessible for maildrop.
162         $command = 'chmod 755  /var/run/authdaemon.courier-imap';
163         caselog($command.' &> /dev/null', __FILE__, __LINE__, 'EXECUTED: '.$command, 'Failed to execute the command '.$command);
164         
165         //* Changing maildrop lines in posfix master.cf
166         if(is_file($config_dir.'/master.cf')){
167             copy($config_dir.'/master.cf', $config_dir.'/master.cf~');
168         }
169         if(is_file($config_dir.'/master.cf~')){
170             exec('chmod 400 '.$config_dir.'/master.cf~');
171         }
172         $configfile = $config_dir.'/master.cf';
173         $content = rf($configfile);
174         
175         $content = str_replace('  flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}', 
b2b3b1 176                    '  flags=DRhu user='.$cf['vmail_username'].' argv=/usr/bin/maildrop -d ${recipient} ${extension} ${recipient} ${user} ${nexthop} ${sender}',
32d8e9 177                      $content);
T 178         
179         $content = str_replace('  flags=DRhu user=vmail argv=/usr/local/bin/maildrop -d ${recipient}', 
b2b3b1 180                    '  flags=DRhu user='.$cf['vmail_username'].' argv=/usr/bin/maildrop -d ${recipient} ${extension} ${recipient} ${user} ${nexthop} ${sender}',
32d8e9 181                      $content);
T 182         
fdb514 183         // enable tlsmanager
T 184         $content = str_replace('#tlsmgr    unix  -       -       n       1000?   1       tlsmgr','tlsmgr    unix  -       -       n       1000?   1       tlsmgr',$content);
185         
32d8e9 186         wf($configfile, $content);
T 187         
188         //* Writing the Maildrop mailfilter file
189         $configfile = 'mailfilter';
190         if(is_file($cf['vmail_mailbox_base'].'/.'.$configfile)){
191             copy($cf['vmail_mailbox_base'].'/.'.$configfile, $cf['vmail_mailbox_base'].'/.'.$configfile.'~');
192         }
193         $content = rf("tpl/$configfile.master");
194         $content = str_replace('{dist_postfix_vmail_mailbox_base}', $cf['vmail_mailbox_base'], $content);
195         wf($cf['vmail_mailbox_base'].'/.'.$configfile, $content);
196         
197         //* Create the directory for the custom mailfilters
198         $command = 'mkdir '.$cf['vmail_mailbox_base'].'/mailfilters';
199         caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
200         
201         //* Chmod and chown the .mailfilter file
202         $command = 'chown -R '.$cf['vmail_username'].':'.$cf['vmail_groupname'].' '.$cf['vmail_mailbox_base'].'/.mailfilter';
203         caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
204         
205         $command = 'chmod -R 600 '.$cf['vmail_mailbox_base'].'/.mailfilter';
206         caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
207         
208     }
209     
210     public function configure_saslauthd() {
211         global $conf;
212         
213         /*
214         $configfile = 'sasl_smtpd.conf';
215         if(is_file('/etc/sasl2/smtpd.conf')) copy('/etc/sasl2/smtpd.conf','/etc/sasl2/smtpd.conf~');
216         if(is_file('/etc/sasl2/smtpd.conf~')) exec('chmod 400 '.'/etc/sasl2/smtpd.conf~');
217         $content = rf("tpl/".$configfile.".master");
218         $content = str_replace('{mysql_server_ispconfig_user}',$conf['mysql']['ispconfig_user'],$content);
219         $content = str_replace('{mysql_server_ispconfig_password}',$conf['mysql']['ispconfig_password'], $content);
220         $content = str_replace('{mysql_server_database}',$conf['mysql']['database'],$content);
221         $content = str_replace('{mysql_server_ip}',$conf['mysql']['ip'],$content);
222         wf('/etc/sasl2/smtpd.conf',$content);
223         */
224         
225         // TODO: Chmod and chown on the config file
226         
227         
228         /*
229         // Create the spool directory
230         exec('mkdir -p /var/spool/postfix/var/run/saslauthd');
231         
232         // Edit the file /etc/default/saslauthd
233         $configfile = $conf["saslauthd"]["config"];
234         if(is_file($configfile)) copy($configfile,$configfile.'~');
235         if(is_file($configfile.'~')) exec('chmod 400 '.$configfile.'~');
236         $content = rf($configfile);
237         $content = str_replace('START=no','START=yes',$content);
238         $content = str_replace('OPTIONS="-c"','OPTIONS="-m /var/spool/postfix/var/run/saslauthd -r"',$content);
239         wf($configfile,$content);
240         */
241         
242         // Edit the file /etc/init.d/saslauthd
243         $configfile = $conf["init_scripts"].'/'.$conf["saslauthd"]["init_script"];
244         $content = rf($configfile);
245         $content = str_replace('/sbin/startproc $AUTHD_BIN -a $SASLAUTHD_AUTHMECH -n $SASLAUTHD_THREADS > /dev/null 2>&1','/sbin/startproc $AUTHD_BIN -r -a $SASLAUTHD_AUTHMECH -n $SASLAUTHD_THREADS > /dev/null 2>&1',$content);
246         $content = str_replace('/sbin/startproc $AUTHD_BIN $SASLAUTHD_PARAMS -a $SASLAUTHD_AUTHMECH -n $SASLAUTHD_THREADS > /dev/null 2>&1','/sbin/startproc $AUTHD_BIN $SASLAUTHD_PARAMS -r -a $SASLAUTHD_AUTHMECH -n $SASLAUTHD_THREADS > /dev/null 2>&1',$content);
247         
248         
fdb514 249         if(is_file($configfile)) wf($configfile,$content);
32d8e9 250         
T 251         
252         
253     }
254     
255     public function configure_pam()
256     {
257         global $conf;
258         $pam = $conf['pam'];
259         //* configure pam for SMTP authentication agains the ispconfig database
260         $configfile = 'pamd_smtp';
261         if(is_file("$pam/smtp"))    copy("$pam/smtp", "$pam/smtp~");
262         if(is_file("$pam/smtp~"))   exec("chmod 400 $pam/smtp~");
263
264         $content = rf("tpl/$configfile.master");
265         $content = str_replace('{mysql_server_ispconfig_user}', $conf['mysql']['ispconfig_user'], $content);
266         $content = str_replace('{mysql_server_ispconfig_password}', $conf['mysql']['ispconfig_password'], $content);
267         $content = str_replace('{mysql_server_database}', $conf['mysql']['database'], $content);
268         $content = str_replace('{mysql_server_ip}', $conf['mysql']['ip'], $content);
269         wf("$pam/smtp", $content);
03bff7 270         // On some OSes smtp is world readable which allows for reading database information.  Removing world readable rights should have no effect.
T 271         if(is_file("$pam/smtp"))    exec("chmod o= $pam/smtp");
32d8e9 272         //exec("chmod 660 $pam/smtp");
T 273         //exec("chown root:root $pam/smtp");
274     
275     }
276     
277     public function configure_courier()
278     {
279         global $conf;
280         $config_dir = $conf['courier']['config_dir'];
281         //* authmysqlrc
282         $configfile = 'authmysqlrc';
283         if(is_file("$config_dir/$configfile")){
284             copy("$config_dir/$configfile", "$config_dir/$configfile~");
285         }
286         exec("chmod 400 $config_dir/$configfile~");
287         $content = rf("tpl/$configfile.master");
288         $content = str_replace('{mysql_server_ispconfig_user}',$conf['mysql']['ispconfig_user'],$content);
289         $content = str_replace('{mysql_server_ispconfig_password}',$conf['mysql']['ispconfig_password'], $content);
290         $content = str_replace('{mysql_server_database}',$conf['mysql']['database'],$content);
291         $content = str_replace('{mysql_server_host}',$conf['mysql']['host'],$content);
292         wf("$config_dir/$configfile", $content);
293         
294         exec("chmod 660 $config_dir/$configfile");
295         exec("chown root:root $config_dir/$configfile");
296         
297         //* authdaemonrc
298         $configfile = $conf['courier']['config_dir'].'/authdaemonrc';
299         if(is_file($configfile)){
300             copy($configfile, $configfile.'~');
301         }
302         if(is_file($configfile.'~')){
303             exec('chmod 400 '.$configfile.'~');
304         }
305         $content = rf($configfile);
306         $content = str_replace('authmodulelist=', 'authmodulelist="authmysql"', $content);
307         wf($configfile, $content);
308     }
309     
fdb514 310     public function configure_dovecot()
T 311     {
312         global $conf;
313         
314         $config_dir = $conf['dovecot']['config_dir'];
315         
316         //* Configure master.cf and add a line for deliver
317         if(is_file($config_dir.'/master.cf')){
318             copy($config_dir.'/master.cf', $config_dir.'/master.cf~2');
319         }
320         if(is_file($config_dir.'/master.cf~')){
321             exec('chmod 400 '.$config_dir.'/master.cf~2');
322         }
323         $content = rf($conf["postfix"]["config_dir"].'/master.cf');
324         // Only add the content if we had not addded it before
325         if(!stristr($content,"dovecot/deliver")) {
326             $deliver_content = 'dovecot   unix  -       n       n       -       -       pipe'."\n".'  flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -f ${sender} -d ${user}@${nexthop}';
327             af($conf["postfix"]["config_dir"].'/master.cf',$deliver_content);
328         }
329         unset($content);
330         unset($deliver_content);
331         
332         
333         //* Reconfigure postfix to use dovecot authentication
334         // Adding the amavisd commands to the postfix configuration
335         $postconf_commands = array (
336             'dovecot_destination_recipient_limit = 1',
337             'virtual_transport = dovecot',
338             'smtpd_sasl_type = dovecot',
339             'smtpd_sasl_path = private/auth',
340             'receive_override_options = no_address_mappings'
341         );
342         
343         // Make a backup copy of the main.cf file
344         copy($conf["postfix"]["config_dir"].'/main.cf',$conf["postfix"]["config_dir"].'/main.cf~3');
345         
346         // Executing the postconf commands
347         foreach($postconf_commands as $cmd) {
348             $command = "postconf -e '$cmd'";
349             caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
350         }
351         
352         //* copy dovecot.conf
353         $configfile = 'dovecot.conf';
354         if(is_file("$config_dir/$configfile")){
355             copy("$config_dir/$configfile", "$config_dir/$configfile~");
356         }
357         copy('tpl/opensuse_dovecot.conf.master',"$config_dir/$configfile");
358         
359         //* dovecot-sql.conf
360         $configfile = 'dovecot-sql.conf';
361         if(is_file("$config_dir/$configfile")){
362             copy("$config_dir/$configfile", "$config_dir/$configfile~");
363         }
364         exec("chmod 400 $config_dir/$configfile~");
365         $content = rf("tpl/opensuse_dovecot-sql.conf.master");
366         $content = str_replace('{mysql_server_ispconfig_user}',$conf['mysql']['ispconfig_user'],$content);
367         $content = str_replace('{mysql_server_ispconfig_password}',$conf['mysql']['ispconfig_password'], $content);
368         $content = str_replace('{mysql_server_database}',$conf['mysql']['database'],$content);
369         $content = str_replace('{mysql_server_host}',$conf['mysql']['host'],$content);
370         wf("$config_dir/$configfile", $content);
371         
372         exec("chmod 600 $config_dir/$configfile");
373         exec("chown root:root $config_dir/$configfile");
374
375     }
376     
32d8e9 377     public function configure_amavis() {
T 378         global $conf;
379         
380         // amavisd user config file
381         $configfile = 'opensuse_amavisd_conf';
df7e6d 382         if(is_file($conf["amavis"]["config_dir"].'/amavisd.conf')) @copy($conf["amavis"]["config_dir"].'/amavisd.conf',$conf["amavis"]["config_dir"].'/amavisd.conf~');
32d8e9 383         if(is_file($conf["amavis"]["config_dir"].'/amavisd.conf~')) exec('chmod 400 '.$conf["amavis"]["config_dir"].'/amavisd.conf~');
T 384         $content = rf("tpl/".$configfile.".master");
385         $content = str_replace('{mysql_server_ispconfig_user}',$conf['mysql']['ispconfig_user'],$content);
386         $content = str_replace('{mysql_server_ispconfig_password}',$conf['mysql']['ispconfig_password'], $content);
387         $content = str_replace('{mysql_server_database}',$conf['mysql']['database'],$content);
388         $content = str_replace('{mysql_server_port}',$conf["mysql"]["port"],$content);
389         $content = str_replace('{mysql_server_ip}',$conf['mysql']['ip'],$content);
390         wf($conf["amavis"]["config_dir"].'/amavisd.conf',$content);
391         
392         
393         // Adding the amavisd commands to the postfix configuration
394         $postconf_commands = array (
395             'content_filter = amavis:[127.0.0.1]:10024',
396             'receive_override_options = no_address_mappings'
397         );
398         
399         // Make a backup copy of the main.cf file
400         copy($conf["postfix"]["config_dir"].'/main.cf',$conf["postfix"]["config_dir"].'/main.cf~2');
401         
402         // Executing the postconf commands
403         foreach($postconf_commands as $cmd) {
404             $command = "postconf -e '$cmd'";
405             caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
406         }
407         
408         // Append the configuration for amavisd to the master.cf file
409         if(is_file($conf["postfix"]["config_dir"].'/master.cf')) copy($conf["postfix"]["config_dir"].'/master.cf',$conf["postfix"]["config_dir"].'/master.cf~');
410         $content = rf($conf["postfix"]["config_dir"].'/master.cf');
411         // Only add the content if we had not addded it before
412         if(!stristr($content,"127.0.0.1:10025")) {
413             unset($content);
414             $content = rf("tpl/master_cf_amavis.master");
415             af($conf["postfix"]["config_dir"].'/master.cf',$content);
416         }
417         unset($content);
418         
419         // Add the clamav user to the vscan group
420         exec('groupmod --add-user clamav vscan');
421         
422         
423     }
424     
425     public function configure_spamassassin()
426     {
427         global $conf;
428         
429         //* Enable spamasasssin on debian and ubuntu
430         /*
431         $configfile = '/etc/default/spamassassin';
432         if(is_file($configfile)){
433             copy($configfile, $configfile.'~');
434         }
435         $content = rf($configfile);
436         $content = str_replace('ENABLED=0', 'ENABLED=1', $content);
437         wf($configfile, $content);
438         */
439     }
440     
441     public function configure_getmail()
442     {
443         global $conf;
444         
445         $config_dir = $conf['getmail']['config_dir'];
446         
447         if(!is_dir($config_dir)) exec("mkdir -p ".escapeshellcmd($config_dir));
448
449         $command = "useradd -d $config_dir getmail";
450         if(!is_user('getmail')) caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
451         
452         $command = "chown -R getmail $config_dir";
453         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
454         
455         $command = "chmod -R 700 $config_dir";
456         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
457     }
458     
459     
460     public function configure_pureftpd()
461     {
462         global $conf;
463         
464         $config_dir = $conf['pureftpd']['config_dir'];
465
466         //* configure pam for SMTP authentication agains the ispconfig database
467         $configfile = 'db/mysql.conf';
468         if(is_file("$config_dir/$configfile")){
469             copy("$config_dir/$configfile", "$config_dir/$configfile~");
470         }
471         if(is_file("$config_dir/$configfile~")){
472             exec("chmod 400 $config_dir/$configfile~");
473         }
474         $content = rf('tpl/pureftpd_mysql.conf.master');
475         $content = str_replace('{mysql_server_ispconfig_user}', $conf["mysql"]["ispconfig_user"], $content);
476         $content = str_replace('{mysql_server_ispconfig_password}', $conf["mysql"]["ispconfig_password"], $content);
477         $content = str_replace('{mysql_server_database}', $conf["mysql"]["database"], $content);
478         $content = str_replace('{mysql_server_ip}', $conf["mysql"]["ip"], $content);
479         $content = str_replace('{server_id}', $conf["server_id"], $content);
480         wf("$config_dir/$configfile", $content);
481         exec("chmod 600 $config_dir/$configfile");
482         exec("chown root:root $config_dir/$configfile");
483         
484         // copy our customized copy of pureftpd.conf to the pure-ftpd config directory
485         exec("cp tpl/opensuse_pureftpd_conf.master $config_dir/pure-ftpd.conf");
486         
487     }
488     
489     public function configure_mydns()
490     {
491         global $conf;
492         
493         // configure pam for SMTP authentication agains the ispconfig database
494         $configfile = 'mydns.conf';
495         if(is_file($conf["mydns"]["config_dir"].'/'.$configfile)) copy($conf["mydns"]["config_dir"].'/'.$configfile,$conf["mydns"]["config_dir"].'/'.$configfile.'~');
496         if(is_file($conf["mydns"]["config_dir"].'/'.$configfile.'~')) exec('chmod 400 '.$conf["mydns"]["config_dir"].'/'.$configfile.'~');
497         $content = rf("tpl/".$configfile.".master");
498         $content = str_replace('{mysql_server_ispconfig_user}',$conf['mysql']['ispconfig_user'],$content);
499         $content = str_replace('{mysql_server_ispconfig_password}',$conf['mysql']['ispconfig_password'], $content);
500         $content = str_replace('{mysql_server_database}',$conf['mysql']['database'],$content);
501         $content = str_replace('{mysql_server_host}',$conf["mysql"]["host"],$content);
502         $content = str_replace('{server_id}',$conf["server_id"],$content);
503         wf($conf["mydns"]["config_dir"].'/'.$configfile,$content);
504         exec('chmod 600 '.$conf["mydns"]["config_dir"].'/'.$configfile);
505         exec('chown root:root '.$conf["mydns"]["config_dir"].'/'.$configfile);
506     
507     }
508     
509     public function configure_apache()
510     {    
511         global $conf;
512         
1bd269 513         if($conf['apache']['installed'] == false) return;
32d8e9 514         //* Create the logging directory for the vhost logfiles
T 515         exec('mkdir -p /var/log/ispconfig/httpd');
516         
517         //if(is_file('/etc/suphp.conf')) {
44c74c 518         replaceLine('/etc/suphp.conf','php=php','x-httpd-suphp="php:/srv/www/cgi-bin/php5"',0,0);
T 519         replaceLine('/etc/suphp.conf','php="php','x-httpd-suphp="php:/srv/www/cgi-bin/php5"',0,0);
32d8e9 520         replaceLine('/etc/suphp.conf','docroot=','docroot=/srv/www',0,0);
a4f044 521         replaceLine('/etc/suphp.conf','umask=0077','umask=0022',0);
32d8e9 522         //}
T 523         
dbe5b0 524         // Sites enabled and available dirs
32d8e9 525         exec('mkdir -p '.$conf['apache']['vhost_conf_enabled_dir']);
T 526         exec('mkdir -p '.$conf['apache']['vhost_conf_dir']);
527         
528         $content = rf('/etc/apache2/httpd.conf');
529         if(!stristr($content,'Include /etc/apache2/sites-enabled/')) {
530             af('/etc/apache2/httpd.conf',"\n<Directory /srv/www>\n    Options FollowSymlinks\n</Directory>\n\nInclude /etc/apache2/sites-enabled/\n\n");
531         }
532         unset($content);
533         
534         //* Copy the ISPConfig configuration include
535         $vhost_conf_dir = $conf['apache']['vhost_conf_dir'];
536         $vhost_conf_enabled_dir = $conf['apache']['vhost_conf_enabled_dir'];
537         
538         //copy('tpl/apache_ispconfig.conf.master',$vhost_conf_dir.'/ispconfig.conf');
a2156e 539         $content = rf('tpl/apache_ispconfig.conf.master');
T 540         $records = $this->db->queryAllRecords('SELECT * FROM '.$conf['mysql']['master_database'].'.server_ip WHERE server_id = '.$conf['server_id']." AND virtualhost = 'y'");
541
fb3a98 542         if(is_array($records) && count($records) > 0) {
32d8e9 543             foreach($records as $rec) {
a2156e 544                 if($rec['ip_type'] == 'IPv6') {
T 545                     $ip_address = '['.$rec['ip_address'].']';
546                 } else {
547                     $ip_address = $rec['ip_address'];
548                 }
549                 $ports = explode(',',$rec['virtualhost_port']);
550                 if(is_array($ports)) {
551                     foreach($ports as $port) {
552                         $port = intval($port);
553                         if($port > 0 && $port < 65536 && $ip_address != '') {
554                             $content .= 'NameVirtualHost '.$ip_address.":".$port."\n";
555                         }
556                     }
557                 }
32d8e9 558             }
T 559         }
a2156e 560         
32d8e9 561         $content .= "\n";
T 562         wf($vhost_conf_dir.'/ispconfig.conf',$content);
563         
564         if(!@is_link($vhost_conf_enabled_dir."/000-ispconfig.conf")) {
565             exec("ln -s ".$vhost_conf_dir."/ispconfig.conf ".$vhost_conf_enabled_dir."/000-ispconfig.conf");
566         }
567         
99d5dc 568         //* make sure that webalizer finds its config file when it is directly in /etc
T 569         if(@is_file('/etc/webalizer.conf') && !@is_dir('/etc/webalizer')) {
570             exec('mkdir /etc/webalizer');
571             exec('ln -s /etc/webalizer.conf /etc/webalizer/webalizer.conf');
572         }
573         
574         if(is_file('/etc/webalizer/webalizer.conf')) {
575             // Change webalizer mode to incremental
7da533 576             replaceLine('/etc/webalizer/webalizer.conf','#IncrementalName','IncrementalName webalizer.current',0,0);
T 577             replaceLine('/etc/webalizer/webalizer.conf','#Incremental','Incremental     yes',0,0);
578             replaceLine('/etc/webalizer/webalizer.conf','#HistoryName','HistoryName     webalizer.hist',0,0);
99d5dc 579         }
T 580         
6b029a 581         //* add a sshusers group
T 582         $command = 'groupadd sshusers';
583         if(!is_group('sshusers')) caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
584         
32d8e9 585     }
T 586     
2d86e1 587     public function configure_nginx(){
F 588         global $conf;
589         
590         if($conf['nginx']['installed'] == false) return;
591         //* Create the logging directory for the vhost logfiles
592         if(!@is_dir($conf['ispconfig_log_dir'].'/httpd')) mkdir($conf['ispconfig_log_dir'].'/httpd', 0755, true);
593         
dbe5b0 594         // Sites enabled and available dirs
2d86e1 595         exec('mkdir -p '.$conf['nginx']['vhost_conf_enabled_dir']);
F 596         exec('mkdir -p '.$conf['nginx']['vhost_conf_dir']);
597         
dbe5b0 598         $content = rf('/etc/nginx/nginx.conf');
F 599         if(stripos($content, 'include /etc/nginx/sites-enabled/*.vhost;') === false){
600             $content = trim($content);
601             $content = substr($content,0,-1)."\n    include /etc/nginx/sites-enabled/*.vhost;\n}";
602             wf('/etc/nginx/nginx.conf',$content);
603         }
2d86e1 604         unset($content);
F 605         
606         // create PHP-FPM pool dir
607         exec('mkdir -p '.$conf['nginx']['php_fpm_pool_dir']);
608         
609         $content = rf('/etc/php5/fpm/php-fpm.conf');
610         if(stripos($content, 'include=/etc/php5/fpm/pool.d/*.conf') === false){
611             af('/etc/php5/fpm/php-fpm.conf',"\ninclude=/etc/php5/fpm/pool.d/*.conf");
612         }
613         unset($content);
59feb7 614         if(!@is_file($conf['nginx']['php_fpm_ini_path'])){
F 615             if(@is_file('/etc/php5/cli/php.ini')){
616                 exec('cp -f /etc/php5/cli/php.ini '.$conf['nginx']['php_fpm_ini_path']);
617             } elseif(@is_file('/etc/php5/fastcgi/php.ini')){
618                 exec('cp -f /etc/php5/fastcgi/php.ini '.$conf['nginx']['php_fpm_ini_path']);
619             } elseif(@is_file('/etc/php5/apache2/php.ini')){
620                 exec('cp -f /etc/php5/apache2/php.ini '.$conf['nginx']['php_fpm_ini_path']);
621             }
622         }
2d86e1 623
F 624         //* make sure that webalizer finds its config file when it is directly in /etc
625         if(@is_file('/etc/webalizer.conf') && !@is_dir('/etc/webalizer')) {
626             mkdir('/etc/webalizer');
627             symlink('/etc/webalizer.conf','/etc/webalizer/webalizer.conf');
628         }
629
630         if(is_file('/etc/webalizer/webalizer.conf')) {
631             // Change webalizer mode to incremental
632             replaceLine('/etc/webalizer/webalizer.conf','#IncrementalName','IncrementalName webalizer.current',0,0);
633             replaceLine('/etc/webalizer/webalizer.conf','#Incremental','Incremental     yes',0,0);
634             replaceLine('/etc/webalizer/webalizer.conf','#HistoryName','HistoryName     webalizer.hist',0,0);
635         }
636         
637         // Check the awsatst script
638         if(!is_dir('/usr/share/awstats/tools')) exec('mkdir -p /usr/share/awstats/tools');
639         if(!file_exists('/usr/share/awstats/tools/awstats_buildstaticpages.pl') && file_exists('/usr/share/doc/awstats/examples/awstats_buildstaticpages.pl')) symlink('/usr/share/doc/awstats/examples/awstats_buildstaticpages.pl','/usr/share/awstats/tools/awstats_buildstaticpages.pl');
640         if(file_exists('/etc/awstats/awstats.conf.local')) replaceLine('/etc/awstats/awstats.conf.local','LogFormat=4','LogFormat=1',0,1);
641         
642         //* add a sshusers group
643         $command = 'groupadd sshusers';
644         if(!is_group('sshusers')) caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
645     }
646     
32d8e9 647     public function configure_firewall()
T 648     {
649         global $conf;
650         
651         $dist_init_scripts = $conf['init_scripts'];
652           
653         if(is_dir("/etc/Bastille.backup")) caselog("rm -rf /etc/Bastille.backup", __FILE__, __LINE__);
654         if(is_dir("/etc/Bastille")) caselog("mv -f /etc/Bastille /etc/Bastille.backup", __FILE__, __LINE__);
655           @mkdir("/etc/Bastille", octdec($directory_mode));
656           if(is_dir("/etc/Bastille.backup/firewall.d")) caselog("cp -pfr /etc/Bastille.backup/firewall.d /etc/Bastille/", __FILE__, __LINE__);
657           caselog("cp -f tpl/bastille-firewall.cfg.master /etc/Bastille/bastille-firewall.cfg", __FILE__, __LINE__);
658           caselog("chmod 644 /etc/Bastille/bastille-firewall.cfg", __FILE__, __LINE__);
659           $content = rf("/etc/Bastille/bastille-firewall.cfg");
660           $content = str_replace("{DNS_SERVERS}", "", $content);
661
662           $tcp_public_services = '';
663           $udp_public_services = '';
664         
fb3a98 665         $row = $this->db->queryOneRecord('SELECT * FROM '.$conf["mysql"]["database"].'.firewall WHERE server_id = '.intval($conf['server_id']));
32d8e9 666         
T 667           if(trim($row["tcp_port"]) != '' || trim($row["udp_port"]) != ''){
668             $tcp_public_services = trim(str_replace(',',' ',$row["tcp_port"]));
669             $udp_public_services = trim(str_replace(',',' ',$row["udp_port"]));
670           } else {
671             $tcp_public_services = '21 22 25 53 80 110 443 3306 8080 10000';
672             $udp_public_services = '53';
673           }
674         
675         if(!stristr($tcp_public_services, $conf['apache']['vhost_port'])) {
676             $tcp_public_services .= ' '.intval($conf['apache']['vhost_port']);
677             if($row["tcp_port"] != '') $this->db->query("UPDATE firewall SET tcp_port = tcp_port + ',".intval($conf['apache']['vhost_port'])."' WHERE server_id = ".intval($conf['server_id']));
678         }
679
680           $content = str_replace("{TCP_PUBLIC_SERVICES}", $tcp_public_services, $content);
681           $content = str_replace("{UDP_PUBLIC_SERVICES}", $udp_public_services, $content);
682         
683           wf("/etc/Bastille/bastille-firewall.cfg", $content);
684
685           if(is_file($dist_init_scripts."/bastille-firewall")) caselog("mv -f $dist_init_scripts/bastille-firewall $dist_init_scripts/bastille-firewall.backup", __FILE__, __LINE__);
686           caselog("cp -f apps/bastille-firewall $dist_init_scripts", __FILE__, __LINE__);
687           caselog("chmod 700 $dist_init_scripts/bastille-firewall", __FILE__, __LINE__);
688
689           if(is_file("/sbin/bastille-ipchains")) caselog("mv -f /sbin/bastille-ipchains /sbin/bastille-ipchains.backup", __FILE__, __LINE__);
690           caselog("cp -f apps/bastille-ipchains /sbin", __FILE__, __LINE__);
691           caselog("chmod 700 /sbin/bastille-ipchains", __FILE__, __LINE__);
692
693           if(is_file("/sbin/bastille-netfilter")) caselog("mv -f /sbin/bastille-netfilter /sbin/bastille-netfilter.backup", __FILE__, __LINE__);
694           caselog("cp -f apps/bastille-netfilter /sbin", __FILE__, __LINE__);
695           caselog("chmod 700 /sbin/bastille-netfilter", __FILE__, __LINE__);
696         
697         if(!@is_dir('/var/lock/subsys')) caselog("mkdir /var/lock/subsys", __FILE__, __LINE__);
698
699           exec("which ipchains &> /dev/null", $ipchains_location, $ret_val);
700           if(!is_file("/sbin/ipchains") && !is_link("/sbin/ipchains") && $ret_val == 0) phpcaselog(@symlink(shell_exec("which ipchains"), "/sbin/ipchains"), 'create symlink', __FILE__, __LINE__);
701           unset($ipchains_location);
702           exec("which iptables &> /dev/null", $iptables_location, $ret_val);
703           if(!is_file("/sbin/iptables") && !is_link("/sbin/iptables") && $ret_val == 0) phpcaselog(@symlink(trim(shell_exec("which iptables")), "/sbin/iptables"), 'create symlink', __FILE__, __LINE__);
704           unset($iptables_location);
705
706     }
4ae2a0 707
32d8e9 708     public function install_ispconfig()
T 709     {
710         global $conf;
711         
712         $install_dir = $conf['ispconfig_install_dir'];
713
714         //* Create the ISPConfig installation directory
715         if(!@is_dir("$install_dir")) {
716             $command = "mkdir $install_dir";
717             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
718         }
719         
720         //* Create a ISPConfig user and group
721         $command = 'groupadd ispconfig';
722         if(!is_group('ispconfig')) caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
723         
724         $command = "useradd -g ispconfig -d $install_dir ispconfig";
725         if(!is_user('ispconfig')) caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
726         
727         //* copy the ISPConfig interface part
728         $command = "cp -rf ../interface $install_dir";
729         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
730         
731         //* copy the ISPConfig server part
732         $command = "cp -rf ../server $install_dir";
733         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
734         
735         //* Create a symlink, so ISPConfig is accessible via web
736         // Replaced by a separate vhost definition for port 8080
737         // $command = "ln -s $install_dir/interface/web/ /var/www/ispconfig";
738         // caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
739         
740         //* Create the config file for ISPConfig interface
741         $configfile = 'config.inc.php';
742         if(is_file($install_dir.'/interface/lib/'.$configfile)){
743             copy("$install_dir/interface/lib/$configfile", "$install_dir/interface/lib/$configfile~");
744         }
745         $content = rf("tpl/$configfile.master");
746         $content = str_replace('{mysql_server_ispconfig_user}', $conf['mysql']['ispconfig_user'], $content);
747         $content = str_replace('{mysql_server_ispconfig_password}',$conf['mysql']['ispconfig_password'], $content);
748         $content = str_replace('{mysql_server_database}', $conf['mysql']['database'], $content);
749         $content = str_replace('{mysql_server_host}', $conf['mysql']['host'], $content);
750         
751         $content = str_replace('{mysql_master_server_ispconfig_user}', $conf['mysql']['master_ispconfig_user'], $content);
752         $content = str_replace('{mysql_master_server_ispconfig_password}', $conf['mysql']['master_ispconfig_password'], $content);
753         $content = str_replace('{mysql_master_server_database}', $conf['mysql']['master_database'], $content);
754         $content = str_replace('{mysql_master_server_host}', $conf['mysql']['master_host'], $content);
755         
7c3b60 756         $content = str_replace('{server_id}', $conf['server_id'], $content);
32d8e9 757         $content = str_replace('{ispconfig_log_priority}', $conf['ispconfig_log_priority'], $content);
56f1f4 758         $content = str_replace('{language}', $conf['language'], $content);
T 759         
32d8e9 760         wf("$install_dir/interface/lib/$configfile", $content);
T 761         
762         //* Create the config file for ISPConfig server
763         $configfile = 'config.inc.php';
764         if(is_file($install_dir.'/server/lib/'.$configfile)){
765             copy("$install_dir/server/lib/$configfile", "$install_dir/interface/lib/$configfile~");
766         }
767         $content = rf("tpl/$configfile.master");
768         $content = str_replace('{mysql_server_ispconfig_user}', $conf['mysql']['ispconfig_user'], $content);
769         $content = str_replace('{mysql_server_ispconfig_password}', $conf['mysql']['ispconfig_password'], $content);
770         $content = str_replace('{mysql_server_database}', $conf['mysql']['database'], $content);
771         $content = str_replace('{mysql_server_host}', $conf['mysql']['host'], $content);
772         
773         $content = str_replace('{mysql_master_server_ispconfig_user}', $conf['mysql']['master_ispconfig_user'], $content);
774         $content = str_replace('{mysql_master_server_ispconfig_password}', $conf['mysql']['master_ispconfig_password'], $content);
775         $content = str_replace('{mysql_master_server_database}', $conf['mysql']['master_database'], $content);
776         $content = str_replace('{mysql_master_server_host}', $conf['mysql']['master_host'], $content);
777         
778         $content = str_replace('{server_id}', $conf['server_id'], $content);
779         $content = str_replace('{ispconfig_log_priority}', $conf['ispconfig_log_priority'], $content);
5898e6 780         $content = str_replace('{language}', $conf['language'], $content);
T 781         
32d8e9 782         wf("$install_dir/server/lib/$configfile", $content);
T 783         
fb3a98 784         //* Create the config file for remote-actions (but only, if it does not exist, because
T 785         //  the value is a autoinc-value and so changed by the remoteaction_core_module
786         if (!file_exists($install_dir.'/server/lib/remote_action.inc.php')) {
787             $content = '<?php' . "\n" . '$maxid_remote_action = 0;' . "\n" . '?>';
788             wf($install_dir.'/server/lib/remote_action.inc.php', $content);
789         }
32d8e9 790         
T 791         //* Enable the server modules and plugins.
792         // TODO: Implement a selector which modules and plugins shall be enabled.
793         $dir = $install_dir.'/server/mods-available/';
794         if (is_dir($dir)) {
795             if ($dh = opendir($dir)) {
796                 while (($file = readdir($dh)) !== false) {
797                     if($file != '.' && $file != '..' && substr($file,-8,8) == '.inc.php') {
798                         include_once($install_dir.'/server/mods-available/'.$file);
799                         $module_name = substr($file,0,-8);
800                         $tmp = new $module_name;
801                         if($tmp->onInstall()) {
802                             if(!@is_link($install_dir.'/server/mods-enabled/'.$file)) @symlink($install_dir.'/server/mods-available/'.$file, $install_dir.'/server/mods-enabled/'.$file);
803                             if (strpos($file, '_core_module') !== false) {
804                                 if(!@is_link($install_dir.'/server/mods-core/'.$file)) @symlink($install_dir.'/server/mods-available/'.$file, $install_dir.'/server/mods-core/'.$file);
805                             }
806                         }
807                         unset($tmp);
808                     }
809                 }
810                 closedir($dh);
811             }
812         }
813         
814         $dir = $install_dir.'/server/plugins-available/';
815         if (is_dir($dir)) {
816             if ($dh = opendir($dir)) {
817                 while (($file = readdir($dh)) !== false) {
1bd269 818                     if($conf['apache']['installed'] == true && $file == 'nginx_plugin.inc.php') continue;
F 819                     if($conf['nginx']['installed'] == true && $file == 'apache2_plugin.inc.php') continue;
32d8e9 820                     if($file != '.' && $file != '..' && substr($file,-8,8) == '.inc.php') {
T 821                         include_once($install_dir.'/server/plugins-available/'.$file);
822                         $plugin_name = substr($file,0,-8);
823                         $tmp = new $plugin_name;
824                         if($tmp->onInstall()) {
825                             if(!@is_link($install_dir.'/server/plugins-enabled/'.$file)) @symlink($install_dir.'/server/plugins-available/'.$file, $install_dir.'/server/plugins-enabled/'.$file);
826                             if (strpos($file, '_core_plugin') !== false) {
827                                 if(!@is_link($install_dir.'/server/plugins-core/'.$file)) @symlink($install_dir.'/server/plugins-available/'.$file, $install_dir.'/server/plugins-core/'.$file);
828                             }
829                         }
830                         unset($tmp);
831                     }
832                 }
833                 closedir($dh);
834             }
835         }
836         
837         // Update the server config
838         $mail_server_enabled = ($conf['services']['mail'])?1:0;
839         $web_server_enabled = ($conf['services']['web'])?1:0;
840         $dns_server_enabled = ($conf['services']['dns'])?1:0;
841         $file_server_enabled = ($conf['services']['file'])?1:0;
842         $db_server_enabled = ($conf['services']['db'])?1:0;
843         $vserver_server_enabled = ($conf['services']['vserver'])?1:0;
844         $sql = "UPDATE `server` SET mail_server = '$mail_server_enabled', web_server = '$web_server_enabled', dns_server = '$dns_server_enabled', file_server = '$file_server_enabled', db_server = '$db_server_enabled', vserver_server = '$vserver_server_enabled' WHERE server_id = ".intval($conf['server_id']);
845         
846         if($conf['mysql']['master_slave_setup'] == 'y') {
847             $this->dbmaster->query($sql);
848             $this->db->query($sql);
849         } else {
850             $this->db->query($sql);
851         }
852         
853         //* Chmod the files
854         $command = "chmod -R 750 $install_dir";
855         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
856
857         //* chown the files to the ispconfig user and group
858         $command = "chown -R ispconfig:ispconfig $install_dir";
859         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
860         
861         //* Make the global language file directory group writable
862         exec("chmod -R 770 $install_dir/interface/lib/lang");
863         
864         //* Make the temp directory for language file exports writable
865         exec("chmod -R 770 $install_dir/interface/web/temp");
866         
867         //* Make all interface language file directories group writable
868         $handle = @opendir($install_dir.'/interface/web');
869         while ($file = @readdir ($handle)) { 
870                if ($file != '.' && $file != '..') {
871                 if(@is_dir($install_dir.'/interface/web'.'/'.$file.'/lib/lang')) {
872                     $handle2 = opendir($install_dir.'/interface/web'.'/'.$file.'/lib/lang');
873                     chmod($install_dir.'/interface/web'.'/'.$file.'/lib/lang',0770);
874                     while ($lang_file = @readdir ($handle2)) {
875                         if ($lang_file != '.' && $lang_file != '..') {
876                             chmod($install_dir.'/interface/web'.'/'.$file.'/lib/lang/'.$lang_file,0770);
877                         }
878                     }
879                 }
880             }
881         }
882         
883         //* make sure that the server config file (not the interface one) is only readable by the root user
884         exec("chmod 600 $install_dir/server/lib/$configfile");
885         exec("chown root:root $install_dir/server/lib/$configfile");
886         if(@is_file("$install_dir/server/lib/mysql_clientdb.conf")) {
887             exec("chmod 600 $install_dir/server/lib/mysql_clientdb.conf");
888             exec("chown root:root $install_dir/server/lib/mysql_clientdb.conf");
889         }
890         
891         // TODO: FIXME: add the www-data user to the ispconfig group. This is just for testing
892         // and must be fixed as this will allow the apache user to read the ispconfig files.
893         // Later this must run as own apache server or via suexec!
63b369 894         if($conf['apache']['installed'] == true){
F 895             $command = 'groupmod --add-user '.$conf['apache']['user'].' ispconfig';
896             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
897         }
898         if($conf['nginx']['installed'] == true){
899             $command = 'groupmod --add-user '.$conf['nginx']['user'].' ispconfig';
900             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
901         }
32d8e9 902         
T 903         //* Make the shell scripts executable
904         $command = "chmod +x $install_dir/server/scripts/*.sh";
905         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
906         
1bd269 907         
F 908         if($conf['apache']['installed'] == true){
909             //* Copy the ISPConfig vhost for the controlpanel
910             // TODO: These are missing! should they be "vhost_dist_*_dir" ?
911             $vhost_conf_dir = $conf['apache']['vhost_conf_dir'];
912             $vhost_conf_enabled_dir = $conf['apache']['vhost_conf_enabled_dir'];
32d8e9 913         
T 914         
1bd269 915             // Dont just copy over the virtualhost template but add some custom settings
32d8e9 916          
1bd269 917             $content = rf("tpl/apache_ispconfig.vhost.master");
F 918             $content = str_replace('{vhost_port}', $conf['apache']['vhost_port'], $content);
32d8e9 919         
1bd269 920             // comment out the listen directive if port is 80 or 443
F 921             if($conf['apache']['vhost_port'] == 80 or $conf['apache']['vhost_port'] == 443) {
922                 $content = str_replace('{vhost_port_listen}', '#', $content);
923             } else {
924                 $content = str_replace('{vhost_port_listen}', '', $content);
925             }
32d8e9 926         
1bd269 927             if(is_file('/usr/local/ispconfig/interface/ssl/ispserver.crt') && is_file('/usr/local/ispconfig/interface/ssl/ispserver.key')) {
F 928                 $content = str_replace('{ssl_comment}', '', $content);
929             } else {
930                 $content = str_replace('{ssl_comment}', '#', $content);
931             }
4ae2a0 932         
1bd269 933             $content = str_replace('/var/www/', '/srv/www/', $content);
32d8e9 934         
1bd269 935             wf("$vhost_conf_dir/ispconfig.vhost", $content);
32d8e9 936         
1bd269 937             if(!is_file('/srv/www/php-fcgi-scripts/ispconfig/.php-fcgi-starter')) {
F 938                 exec('mkdir -p /srv/www/php-fcgi-scripts/ispconfig');
939                 exec('cp tpl/apache_ispconfig_fcgi_starter.master /srv/www/php-fcgi-scripts/ispconfig/.php-fcgi-starter');
940                 exec('chmod +x /srv/www/php-fcgi-scripts/ispconfig/.php-fcgi-starter');
941                 exec('ln -s /usr/local/ispconfig/interface/web /srv/www/ispconfig');
942                 exec('chown -R ispconfig:ispconfig /srv/www/php-fcgi-scripts/ispconfig');
32d8e9 943             
1bd269 944             }
32d8e9 945         
1bd269 946             //copy('tpl/apache_ispconfig.vhost.master', "$vhost_conf_dir/ispconfig.vhost");
F 947             //* and create the symlink
948             if($this->install_ispconfig_interface == true && $this->is_update == false) {
949                 if(@is_link("$vhost_conf_enabled_dir/ispconfig.vhost")) unlink("$vhost_conf_enabled_dir/ispconfig.vhost");
950                 if(!@is_link("$vhost_conf_enabled_dir/000-ispconfig.vhost")) {
951                     exec("ln -s $vhost_conf_dir/ispconfig.vhost $vhost_conf_enabled_dir/000-ispconfig.vhost");
952                 }
953             
954             }
955         
956             // Fix a setting in vhost master file for suse
957             replaceLine('/usr/local/ispconfig/server/conf/vhost.conf.master',"suPHP_UserGroup","        suPHP_UserGroup <tmpl_var name='system_user'> <tmpl_var name='system_group'>",0);
958         }
959
960         if($conf['nginx']['installed'] == true){
961             //* Copy the ISPConfig vhost for the controlpanel
962             $vhost_conf_dir = $conf['nginx']['vhost_conf_dir'];
963             $vhost_conf_enabled_dir = $conf['nginx']['vhost_conf_enabled_dir'];
964
965             // Dont just copy over the virtualhost template but add some custom settings
966             $content = rf('tpl/nginx_ispconfig.vhost.master');
967             $content = str_replace('{vhost_port}', $conf['nginx']['vhost_port'], $content);
968         
969             if(is_file($install_dir.'/interface/ssl/ispserver.crt') && is_file($install_dir.'/interface/ssl/ispserver.key')) {
970                 $content = str_replace('{ssl_on}', ' ssl', $content);
971                 $content = str_replace('{ssl_comment}', '', $content);
972                 $content = str_replace('{fastcgi_ssl}', 'on', $content);
973             } else {
974                 $content = str_replace('{ssl_on}', '', $content);
975                 $content = str_replace('{ssl_comment}', '#', $content);
976                 $content = str_replace('{fastcgi_ssl}', 'off', $content);
32d8e9 977             }
T 978             
ca0b77 979             $socket_dir = escapeshellcmd($conf['nginx']['php_fpm_socket_dir']);
F 980             if(substr($socket_dir,-1) != '/') $socket_dir .= '/';
981             if(!is_dir($socket_dir)) exec('mkdir -p '.$socket_dir);
982             $fpm_socket = $socket_dir.'ispconfig.sock';
983             
984             //$content = str_replace('{fpm_port}', $conf['nginx']['php_fpm_start_port'], $content);
985             $content = str_replace('{fpm_socket}', $fpm_socket, $content);
1bd269 986
F 987             wf($vhost_conf_dir.'/ispconfig.vhost', $content);
988             
989             unset($content);
990             
991             // PHP-FPM
992             // Dont just copy over the php-fpm pool template but add some custom settings
993             $content = rf('tpl/php_fpm_pool.conf.master');
994             $content = str_replace('{fpm_pool}', 'ispconfig', $content);
ca0b77 995             //$content = str_replace('{fpm_port}', $conf['nginx']['php_fpm_start_port'], $content);
F 996             $content = str_replace('{fpm_socket}', $fpm_socket, $content);
1bd269 997             $content = str_replace('{fpm_user}', 'ispconfig', $content);
F 998             $content = str_replace('{fpm_group}', 'ispconfig', $content);
999             wf($conf['nginx']['php_fpm_pool_dir'].'/ispconfig.conf', $content);
1000
1001             //copy('tpl/nginx_ispconfig.vhost.master', $vhost_conf_dir.'/ispconfig.vhost');
1002             //* and create the symlink
1003             if($this->install_ispconfig_interface == true && $this->is_update == false) {
1004                 if(@is_link($vhost_conf_enabled_dir.'/ispconfig.vhost')) unlink($vhost_conf_enabled_dir.'/ispconfig.vhost');
1005                 if(!@is_link($vhost_conf_enabled_dir.'/000-ispconfig.vhost')) {
1006                     symlink($vhost_conf_dir.'/ispconfig.vhost',$vhost_conf_enabled_dir.'/000-ispconfig.vhost');
1007                 }
1008             }
9aec3d 1009             
F 1010             // create symlinks from /usr/share to phpMyAdmin and SquirrelMail, if they are installed
1011             if(!@file_exists('/usr/share/phpmyadmin') && @is_dir('/srv/www/htdocs/phpMyAdmin')) symlink('/srv/www/htdocs/phpMyAdmin/','/usr/share/phpmyadmin');
1012             if(!@file_exists('/usr/share/squirrelmail') && @is_dir('/srv/www/htdocs/squirrelmail')) symlink('/srv/www/htdocs/squirrelmail/','/usr/share/squirrelmail');
32d8e9 1013         }
T 1014         
1015         
1016         // Make the Clamav log files readable by ISPConfig
1017         //exec('chmod +r /var/log/clamav/clamav.log');
1018         //exec('chmod +r /var/log/clamav/freshclam.log');
1019         
1020         //* Install the update script
1021         if(is_file('/usr/local/bin/ispconfig_update_from_svn.sh')) unlink('/usr/local/bin/ispconfig_update_from_svn.sh');
1022         exec('chown root /usr/local/ispconfig/server/scripts/update_from_svn.sh');
1023         exec('chmod 700 /usr/local/ispconfig/server/scripts/update_from_svn.sh');
1024         exec('chown root /usr/local/ispconfig/server/scripts/update_from_tgz.sh');
1025         exec('chmod 700 /usr/local/ispconfig/server/scripts/update_from_tgz.sh');
1026         exec('chown root /usr/local/ispconfig/server/scripts/ispconfig_update.sh');
1027         exec('chmod 700 /usr/local/ispconfig/server/scripts/ispconfig_update.sh');
1028         if(!is_link('/usr/local/bin/ispconfig_update_from_svn.sh')) exec('ln -s /usr/local/ispconfig/server/scripts/ispconfig_update.sh /usr/local/bin/ispconfig_update_from_svn.sh');
1029         if(!is_link('/usr/local/bin/ispconfig_update.sh')) exec('ln -s /usr/local/ispconfig/server/scripts/ispconfig_update.sh /usr/local/bin/ispconfig_update.sh');
1030         
1031         //set the fast cgi starter script to executable
1032         //exec('chmod 755 '.$install_dir.'/interface/bin/php-fcgi');
1033         
1034         //* Make the logs readable for the ispconfig user
1035         if(@is_file('/var/log/mail.log')) exec('chmod +r /var/log/mail.log');
1036         if(@is_file('/var/log/mail.warn')) exec('chmod +r /var/log/mail.warn');
1037         if(@is_file('/var/log/mail.err')) exec('chmod +r /var/log/mail.err');
1038         if(@is_file('/var/log/messages')) exec('chmod +r /var/log/messages');
1039         
1040         //To enable apache to read the directories
1041         exec('chmod a+rx /usr/local/ispconfig');
1042         exec('chmod -R 751 /usr/local/ispconfig/interface');
1043         exec('chmod a+rx /usr/local/ispconfig/interface/web');
1044         
1045         //* Create the ispconfig log directory
e38d14 1046         if(!is_dir($conf['ispconfig_log_dir'])) mkdir($conf['ispconfig_log_dir']);
J 1047         if(!is_file($conf['ispconfig_log_dir'].'/ispconfig.log')) exec('touch '.$conf['ispconfig_log_dir'].'/ispconfig.log');
32d8e9 1048         
0c5b42 1049         if(is_user('getmail')) {
T 1050             exec('mv /usr/local/ispconfig/server/scripts/run-getmail.sh /usr/local/bin/run-getmail.sh');
1051             exec('chown getmail /usr/local/bin/run-getmail.sh');
1052             exec('chmod 744 /usr/local/bin/run-getmail.sh');
1053         }
32d8e9 1054         
T 1055         
1056     }
1057     
1058     public function configure_dbserver()
1059     {
1060         global $conf;
1061         
1062         //* If this server shall act as database server for client DB's, we configure this here
1063         $install_dir = $conf['ispconfig_install_dir'];
1064         
1065         // Create a file with the database login details which 
1066         // are used to create the client databases.
1067         
1068         if(!is_dir("$install_dir/server/lib")) {
1069             $command = "mkdir $install_dir/server/lib";
1070             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
1071         }
1072         
1073         $content = rf("tpl/mysql_clientdb.conf.master");
1074         $content = str_replace('{username}',$conf['mysql']['admin_user'],$content);
1075         $content = str_replace('{password}',$conf['mysql']['admin_password'], $content);
1076         wf("$install_dir/server/lib/mysql_clientdb.conf",$content);
1077         exec('chmod 600 '."$install_dir/server/lib/mysql_clientdb.conf");
1078         exec('chown root:root '."$install_dir/server/lib/mysql_clientdb.conf");
1079         
1080     }
1081     
1082     public function install_crontab()
1083     {        
1084         global $conf;
1085         
1086         //* Root Crontab
1087         exec('crontab -u root -l > crontab.txt');
1088         $existing_root_cron_jobs = file('crontab.txt');
1089         
1090         // remove existing ispconfig cronjobs, in case the syntax has changed
1091         foreach($existing_root_cron_jobs as $key => $val) {
1092             if(stristr($val,'/usr/local/ispconfig')) unset($existing_root_cron_jobs[$key]);
1093         }
1094         
1095         $root_cron_jobs = array(
1096             '* * * * * /usr/local/ispconfig/server/server.sh &> /dev/null',
1097             '30 00 * * * /usr/local/ispconfig/server/cron_daily.sh &> /dev/null'
1098         );
b6a10a 1099         
F 1100         if ($conf['nginx']['installed'] == true) {
1101             $root_cron_jobs[] = "0 0 * * * /usr/local/ispconfig/server/scripts/create_daily_nginx_access_logs.sh &> /dev/null";
1102         }
1103         
32d8e9 1104         foreach($root_cron_jobs as $cron_job) {
T 1105             if(!in_array($cron_job."\n", $existing_root_cron_jobs)) {
1106                 $existing_root_cron_jobs[] = $cron_job."\n";
1107             }
1108         }
1109         file_put_contents('crontab.txt', $existing_root_cron_jobs);
1110         exec('crontab -u root crontab.txt &> /dev/null');
1111         unlink('crontab.txt');
1112         
1113         //* Getmail crontab
1114         if(is_user('getmail')) {
1115             $cf = $conf['getmail'];
1116             exec('crontab -u getmail -l > crontab.txt');
1117             $existing_cron_jobs = file('crontab.txt');
1118         
1119             $cron_jobs = array(
1120                 '*/5 * * * * /usr/local/bin/run-getmail.sh > /dev/null 2>> /var/log/ispconfig/cron.log'
1121             );
1122         
1123             // remove existing ispconfig cronjobs, in case the syntax has changed
1124             foreach($existing_cron_jobs as $key => $val) {
1125                 if(stristr($val,'getmail')) unset($existing_cron_jobs[$key]);
1126             }
1127         
1128             foreach($cron_jobs as $cron_job) {
1129                 if(!in_array($cron_job."\n", $existing_cron_jobs)) {
1130                     $existing_cron_jobs[] = $cron_job."\n";
1131                 }
1132             }
1133             file_put_contents('crontab.txt', $existing_cron_jobs);
1134             exec('crontab -u getmail crontab.txt &> /dev/null');
1135             unlink('crontab.txt');
1136         }
1137         
1138         exec('touch /var/log/ispconfig/cron.log');
1139         exec('chmod 666 /var/log/ispconfig/cron.log');
1140     }
1141
1142 }
1143
e38d14 1144 ?>