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