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