tbrehm
2013-02-22 526b997c9891a796b152cdbab8e329b356b1f596
commit | author | age
20218c 1 <?php
M 2
3 /*
4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
cb8c86 31 class installer extends installer_base 
W 32 {
33     public function configure_jailkit()
34     {
35         global $conf;
36         
37         if (is_dir($conf['jailkit']['config_dir']))
38         {
ff1d9a 39             $jkinit_content = $this->get_template_file($conf['jailkit']['jk_init'], true); //* get contents
cb8c86 40             $this->write_config_file($conf['jailkit']['config_dir'] . '/' . $conf['jailkit']['jk_init'], $jkinit_content);
W 41             
ff1d9a 42             $jkchroot_content = $this->get_template_file($conf['jailkit']['jk_chrootsh'], true); //* get contents
cb8c86 43             $this->write_config_file($conf['jailkit']['config_dir'] . '/' . $conf['jailkit']['jk_chrootsh'], $jkchroot_content);
W 44         }
45         
ff1d9a 46         $command = 'chown root:root /var/www';
W 47         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
cb8c86 48     }
W 49     
50     public function configure_postfix($options = '')
51     {
52         global $conf;
53         
54         $cf = $conf['postfix'];
55         $config_dir = $cf['config_dir'];
56         
57         if(!is_dir($config_dir)){
58             $this->error("The postfix configuration directory '$config_dir' does not exist.");
59         }
60         
ff1d9a 61         //* Install virtual mappings
W 62         foreach (glob('tpl/mysql-virtual_*.master') as $filename) {
63             $this->process_postfix_config( basename($filename, '.master') );
cb8c86 64         }
W 65         
ff1d9a 66         //* Changing mode and group of the new created config files.
cb8c86 67         caselog('chmod o= '.$config_dir.'/mysql-virtual_*.cf* &> /dev/null',
W 68                  __FILE__, __LINE__, 'chmod on mysql-virtual_*.cf*', 'chmod on mysql-virtual_*.cf* failed');
69         caselog('chgrp '.$cf['group'].' '.$config_dir.'/mysql-virtual_*.cf* &> /dev/null', 
70                 __FILE__, __LINE__, 'chgrp on mysql-virtual_*.cf*', 'chgrp on mysql-virtual_*.cf* failed');
71         
ff1d9a 72         //* Creating virtual mail user and group
cb8c86 73         $command = 'groupadd -g '.$cf['vmail_groupid'].' '.$cf['vmail_groupname'];
W 74         if (!is_group($cf['vmail_groupname'])) {
75             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
76         }
20218c 77
cb8c86 78         $command = 'useradd -g '.$cf['vmail_groupname'].' -u '.$cf['vmail_userid'].' '.$cf['vmail_username'].' -d '.$cf['vmail_mailbox_base'].' -m';
W 79         if (!is_user($cf['vmail_username'])) {
80             caselog("$command &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");        
81         }
20218c 82
b67344 83         //* These postconf commands will be executed on installation and update
cb8c86 84         $postconf_commands = array (
W 85             'virtual_alias_domains =',
86             'virtual_alias_maps = proxy:mysql:'.$config_dir.'/mysql-virtual_forwardings.cf, mysql:'.$config_dir.'/mysql-virtual_email2email.cf',
87             'virtual_mailbox_domains = proxy:mysql:'.$config_dir.'/mysql-virtual_domains.cf',
88             'virtual_mailbox_maps = proxy:mysql:'.$config_dir.'/mysql-virtual_mailboxes.cf',
89             'virtual_mailbox_base = '.$cf['vmail_mailbox_base'],
90             'virtual_uid_maps = static:'.$cf['vmail_userid'],
91             'virtual_gid_maps = static:'.$cf['vmail_groupid'],
92             'smtpd_sasl_auth_enable = yes',
93             'broken_sasl_auth_clients = yes',
94             'smtpd_sasl_authenticated_header = yes',
95             'smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, check_recipient_access mysql:'.$config_dir.'/mysql-virtual_recipient.cf, reject_unauth_destination',
96             'smtpd_use_tls = yes',
97             'smtpd_tls_security_level = may',
98             'smtpd_tls_cert_file = '.$config_dir.'/smtpd.cert',
99             'smtpd_tls_key_file = '.$config_dir.'/smtpd.key',
72695f 100             'transport_maps = hash:/var/lib/mailman/data/transport-mailman, proxy:mysql:'.$config_dir.'/mysql-virtual_transports.cf',
cb8c86 101             'relay_domains = mysql:'.$config_dir.'/mysql-virtual_relaydomains.cf',
W 102             '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',
103             'smtpd_sender_restrictions = check_sender_access mysql:'.$config_dir.'/mysql-virtual_sender.cf',
104             'smtpd_client_restrictions = check_client_access mysql:'.$config_dir.'/mysql-virtual_client.cf',
105             'maildrop_destination_concurrency_limit = 1',
106             'maildrop_destination_recipient_limit   = 1',
107             'virtual_transport = maildrop',
108             'header_checks = regexp:'.$config_dir.'/header_checks',
109             'mime_header_checks = regexp:'.$config_dir.'/mime_header_checks',
110             'nested_header_checks = regexp:'.$config_dir.'/nested_header_checks',
111             'body_checks = regexp:'.$config_dir.'/body_checks'
112         );
113         
b67344 114         //* These postconf commands will be executed on installation only
T 115         if($this->is_update == false) {
116             $postconf_commands = array_merge($postconf_commands,array(
117                 'myhostname = '.$conf['hostname'],
118                 'mydestination = '.$conf['hostname'].', localhost, localhost.localdomain',
119                 'mynetworks = 127.0.0.0/8 [::1]/128'
120             ));
121         }
122         
ff1d9a 123         //* Create the header and body check files
cb8c86 124         touch($config_dir.'/header_checks');
W 125         touch($config_dir.'/mime_header_checks');
126         touch($config_dir.'/nested_header_checks');
127         touch($config_dir.'/body_checks');
128         
129         
ff1d9a 130         //* Make a backup copy of the main.cf file
cb8c86 131         copy($config_dir.'/main.cf', $config_dir.'/main.cf~');
W 132         
ff1d9a 133         //* Executing the postconf commands
cb8c86 134         foreach($postconf_commands as $cmd) {
W 135             $command = "postconf -e '$cmd'";
ff1d9a 136             caselog($command.' &> /dev/null', __FILE__, __LINE__, 'EXECUTED: '.$command, 'Failed to execute the command '.$command);
cb8c86 137         }
W 138         
ff1d9a 139         //* Create the SSL certificate
cb8c86 140         if (!stristr($options,'dont-create-certs'))  
W 141         {
142             $command = 'cd '.$config_dir.'; '
143                       .'openssl req -new -outform PEM -out smtpd.cert -newkey rsa:2048 -nodes -keyout smtpd.key -keyform PEM -days 365 -x509';
144             exec($command);
145         
146             $command = 'chmod o= '.$config_dir.'/smtpd.key';
147             caselog($command.' &> /dev/null', __FILE__, __LINE__, 'EXECUTED: '.$command, 'Failed to execute the command '.$command);
148         }
149         
ff1d9a 150         //* We have to change the permissions of the courier authdaemon directory to make it accessible for maildrop.
W 151         $command = 'chmod 755  /var/lib/courier/authdaemon/';
152         if (is_dir('/var/lib/courier/authdaemon')) {
153             caselog($command.' &> /dev/null', __FILE__, __LINE__, 'EXECUTED: '.$command, 'Failed to execute the command '.$command);
154         }
cb8c86 155         
ff1d9a 156         //* Changing maildrop lines in posfix master.cf
cb8c86 157         $configfile = $config_dir.'/master.cf';
W 158         $content = rf($configfile);
159
160         $content = preg_replace('/^#?maildrop/m', 'maildrop', $content);
161         $content = preg_replace('/^#?(\s+)flags=DRhu user=vmail argv=\/usr\/bin\/maildrop -d/m',
162                                 '$1flags=DRhu user=vmail argv=/usr/bin/maildrop -d vmail \${extension} \${recipient} \${user} \${nexthop} \${sender}',
163                                 $content);
164                                 
165         $this->write_config_file($configfile, $content);
166         
ff1d9a 167         //* Writing the Maildrop mailfilter file
W 168         $content = rf('tpl/mailfilter.master');
cb8c86 169         $content = str_replace('{dist_postfix_vmail_mailbox_base}', $cf['vmail_mailbox_base'], $content);
W 170         
171         $this->write_config_file($cf['vmail_mailbox_base'].'/.mailfilter', $content);
172         
ff1d9a 173         //* Create the directory for the custom mailfilters
cb8c86 174         if (!is_dir($cf['vmail_mailbox_base'].'/mailfilters')) 
W 175         {
176             $command = 'mkdir '.$cf['vmail_mailbox_base'].'/mailfilters';
ff1d9a 177             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
cb8c86 178         }
W 179         
ff1d9a 180         //* Chmod and chown the .mailfilter file
cb8c86 181         $command = 'chown -R '.$cf['vmail_username'].':'.$cf['vmail_groupname'].' '.$cf['vmail_mailbox_base'].'/.mailfilter';
W 182         caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
183         
184         $command = 'chmod -R 600 '.$cf['vmail_mailbox_base'].'/.mailfilter';
185         caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
186         
187     }
188     
189     public function configure_saslauthd() 
190     {
191         global $conf;
192     
ff1d9a 193         $content = $this->get_template_file('sasl_smtpd.conf', true, true); //* get contents & insert db cred
cb8c86 194         $this->write_config_file($conf['saslauthd']['config_dir'].'/smtpd.conf', $content);
W 195         
ff1d9a 196         //* Edit the file saslauthd config file
W 197         $content = rf($conf['saslauthd']['config_file']);
cb8c86 198         $content = preg_replace('/(?<=\n)SASLAUTHD_OPTS="\$\{SASLAUTHD_OPTS\}[^"]+"/', 'SASLAUTHD_OPTS="${SASLAUTHD_OPTS} -a pam -r -c -s 128 -t 30 -n 5"', $content);
W 199         
ff1d9a 200         $this->write_config_file($conf['saslauthd']['config_file'], $content);
cb8c86 201     }
W 202     
203     public function configure_courier()
204     {
205         global $conf;
206         
ff1d9a 207         //* authmysqlrc
W 208         $content = $this->get_template_file('authmysqlrc', true, true); //* get contents & insert db cred
cb8c86 209         $this->write_config_file($conf['courier']['config_dir'].'/authmysqlrc', $content);
W 210         
ff1d9a 211         //* authdaemonrc
cb8c86 212         $configfile = $conf['courier']['config_dir'].'/authdaemonrc';
W 213
214         $content = rf($configfile);
215         $content = preg_replace('/(?<=\n)authmodulelist="[^"]+"/', "authmodulelist=\"authmysql\"", $content);
216         $this->write_config_file($configfile, $content);
217         
ff1d9a 218         //* create certificates
W 219         $command = 'mkimapdcert';
220         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
cb8c86 221             
ff1d9a 222         $command = 'mkpop3dcert';
cb8c86 223         caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
ff1d9a 224     }
W 225     
226     public function configure_dovecot() 
227     {
228         global $conf;
229         
230         $config_dir = $conf['dovecot']['config_dir'];
231         
232         $configfile = $conf['postfix']['config_dir'].'/master.cf';
233         
234         if(is_file($configfile)) {
235             copy($configfile, $configfile.'~2');
236         }
237         if(is_file($configfile.'~2')) {
238             chmod($configfile.'~2', 0400);
239         }
240         
241         //* Configure master.cf and add a line for deliver
242         $content = rf($configfile);
243         
244         if(!stristr($content,'dovecot/deliver')) {
013ae4 245             $deliver_content = 'dovecot   unix  -       n       n       -       -       pipe'."\n".'  flags=DROhu user=vmail:vmail argv=/usr/libexec/dovecot/deliver -f ${sender} -d ${user}@${nexthop}';
ff1d9a 246             af($conf['postfix']['config_dir'].'/master.cf',$deliver_content);
W 247         }
248         unset($content);
249         unset($deliver_content);
250         unset($configfile);
251         
252         //* Reconfigure postfix to use dovecot authentication
253         $postconf_commands = array (
254                 'dovecot_destination_recipient_limit = 1',
255                 'virtual_transport = dovecot',
256                 'smtpd_sasl_type = dovecot',
257                 'smtpd_sasl_path = private/auth'
258         );
259         
260         //* Make a backup copy of the main.cf file
261         copy($conf['postfix']['config_dir'].'/main.cf',$conf['postfix']['config_dir'].'/main.cf~3');
262         
263         //* Executing the postconf commands
264         foreach($postconf_commands as $cmd) 
265         {
266             $command = "postconf -e '$cmd'";
267             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
268         }
269         
270         //* copy dovecot.conf
271         $configfile = $config_dir.'/dovecot.conf';
272         $content = $this->get_template_file('dovecot.conf', true);
273         $this->write_config_file($configfile, $content);
274         
275         //* dovecot-sql.conf
276         $configfile = $config_dir.'/dovecot-sql.conf';
277         $content = $this->get_template_file('debian_dovecot-sql.conf', true, true);
278         $this->write_config_file($configfile, $content);
cb8c86 279     }
W 280     
281     public function configure_spamassassin()
282     {
283         return true;
284     }
285     
286     public function configure_getmail()
287     {
288         global $conf;
289         
290         $config_dir = $conf['getmail']['config_dir'];
291         
292         if (!is_dir($config_dir)) {
ff1d9a 293             exec('mkdir -p '.escapeshellcmd($config_dir));
cb8c86 294         }
W 295
296         $command = "useradd -d $config_dir ".$conf['getmail']['user'];
297         if (!is_user('getmail')) {
298             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
299         }
300         
301         $command = "chown -R getmail $config_dir";
302         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
303         
304         $command = "chmod -R 700 $config_dir";
305         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
306         
ff1d9a 307         //* Getmail will be run from cron. In order to have access to cron the getmail user needs to be part of the cron group.
cb8c86 308         $command = "gpasswd -a getmail " . $conf['cron']['group'];
W 309         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
310     }
311     
312     public function configure_amavis() 
313     {
314         global $conf;
315         
ff1d9a 316         //* Amavisd-new user config file
cb8c86 317         $conf_file = 'amavisd-ispconfig.conf';
W 318         $conf_path = dirname($conf['amavis']['config_file']) . '/' . $conf_file;
319         
ff1d9a 320         $content = $this->get_template_file($conf_file, true, true); //* get contents & insert db cred
cb8c86 321         $this->write_config_file($conf_path, $content);
W 322         
ff1d9a 323         //* Activate config directory in default file
cb8c86 324         $amavis_conf = rf($conf['amavis']['config_file']);
ff1d9a 325         if (stripos($amavis_conf, $conf_path) === false) 
cb8c86 326         {
ff1d9a 327             $amavis_conf = preg_replace('/^(1;.*)$/m', "include_config_files('$conf_path');\n$1", $amavis_conf);
cb8c86 328             $this->write_config_file($conf['amavis']['config_file'], $amavis_conf);
W 329         }
330         
ff1d9a 331         //* Adding the amavisd commands to the postfix configuration
cb8c86 332         $postconf_commands = array (
W 333             'content_filter = amavis:[127.0.0.1]:10024',
334             'receive_override_options = no_address_mappings'
335         );
336         
337         foreach($postconf_commands as $cmd) {
338             $command = "postconf -e '$cmd'";
ff1d9a 339             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
cb8c86 340         }
W 341         
ff1d9a 342         //* Append the configuration for amavisd to the master.cf file
W 343         $content = rf($conf['postfix']['config_dir'].'/master.cf');
cb8c86 344         
ff1d9a 345         if(!stristr($content,'127.0.0.1:10025')) //* Only add the content if we had not addded it before 
cb8c86 346         { 
W 347             unset($content);
ff1d9a 348             $content = $this->get_template_file('master_cf_amavis', true);
W 349             af($conf['postfix']['config_dir'].'/master.cf', $content);
cb8c86 350         }
W 351         unset($content);
352         
ff1d9a 353         //* Add the clamav user to the amavis group
cb8c86 354         exec('usermod -a -G amavis clamav');
W 355     }
356     
357     public function configure_pureftpd()
358     {
359         global $conf;
360         
361         //* configure pure-ftpd for MySQL authentication against the ispconfig database
ff1d9a 362         $content = $this->get_template_file('pureftpd_mysql.conf', true, true); //* get contents & insert db cred
W 363         $content = str_replace('{server_id}', $conf['server_id'], $content);
cb8c86 364         
W 365         $this->write_config_file($conf['pureftpd']['mysql_config_file'], $content, 600, 'root', 'root');
366         
ff1d9a 367         //* enable pure-ftpd and server settings
cb8c86 368         $content = rf($conf["pureftpd"]["config_file"]);
W 369         
370         $content = preg_replace('/#?IS_CONFIGURED="(?:yes|no)"/', 'IS_CONFIGURED="yes"', $content);
371         $content = str_replace('AUTH="-l unix"', 'AUTH="-l mysql:'.$conf['pureftpd']['mysql_config_file'].'"', $content);
372         
ff1d9a 373         //* Logging defaults to syslog's ftp facility. Override this behaviour for better compatibility with debian/ubuntu
W 374         //* and specify the format.
cb8c86 375         $logdir = '/var/log/pure-ftpd';
W 376         if (!is_dir($logdir)) {
377             mkdir($logdir, 0755, true);
378         }
379         
ff1d9a 380         /**
cb8c86 381          * @link http://download.pureftpd.org/pub/pure-ftpd/doc/README
W 382          * -b brokenclientscompatibility
383          * -A chrooteveryone
384          * -E noanonymous
385          * -O altlog <format>:<log file>
386          * -Z customerproof (Add safe guards against common customer mistakes ie. like chmod 0 on their own files)
ff1d9a 387          * -D displaydotfiles 
W 388          * -H dontresolve
cb8c86 389          */
ff1d9a 390         $content = preg_replace('/MISC_OTHER="[^"]+"/', 'MISC_OTHER="-b -A -E -Z -D -H -O clf:'.$logdir.'/transfer.log"', $content);
cb8c86 391         
ff1d9a 392         $this->write_config_file($conf['pureftpd']['config_file'], $content);
cb8c86 393     }
W 394     
395     public function configure_powerdns() 
396     {
397         global $conf;
398         
399         //* Create the database
400         if(!$this->db->query('CREATE DATABASE IF NOT EXISTS '.$conf['powerdns']['database'].' DEFAULT CHARACTER SET '.$conf['mysql']['charset'])) {
401             $this->error('Unable to create MySQL database: '.$conf['powerdns']['database'].'.');
402         }
403         
404         //* Create the ISPConfig database user in the local database
ff1d9a 405         $query = 'GRANT ALL ON `'.$conf['powerdns']['database'].'` . * TO \''.$conf['mysql']['ispconfig_user'].'\'@\'localhost\';';
cb8c86 406         if(!$this->db->query($query)) {
W 407             $this->error('Unable to create user for powerdns database Error: '.$this->db->errorMessage);
408         }
409         
410         //* Reload database privelages
411         $this->db->query('FLUSH PRIVILEGES;');
412         
413         //* load the powerdns databse dump
414         if($conf['mysql']['admin_password'] == '') {
415             caselog("mysql --default-character-set=".$conf['mysql']['charset']." -h '".$conf['mysql']['host']."' -u '".$conf['mysql']['admin_user']."' '".$conf['powerdns']['database']."' < '".ISPC_INSTALL_ROOT."/install/sql/powerdns.sql' &> /dev/null", 
416                     __FILE__, __LINE__, 'read in ispconfig3.sql', 'could not read in powerdns.sql');
417         } else {
418             caselog("mysql --default-character-set=".$conf['mysql']['charset']." -h '".$conf['mysql']['host']."' -u '".$conf['mysql']['admin_user']."' -p'".$conf['mysql']['admin_password']."' '".$conf['powerdns']['database']."' < '".ISPC_INSTALL_ROOT."/install/sql/powerdns.sql' &> /dev/null", 
419                      __FILE__, __LINE__, 'read in ispconfig3.sql', 'could not read in powerdns.sql');
420         }
421         
422         //* Create the powerdns config file
ff1d9a 423         $content = $this->get_template_file('pdns.local', true, true); //* get contents & insert db cred
cb8c86 424         $content = str_replace('{powerdns_database}', $conf['powerdns']['database'], $content);
W 425         
426         $this->write_config_file($conf["powerdns"]["config_dir"].'/'.$conf["powerdns"]["config_file"], $content, 600, 'root', 'root');
427
ff1d9a 428         //* Create symlink to init script to start the correct config file
cb8c86 429         if( !is_link($conf['init_scripts'].'/'.$conf['powerdns']['init_script']) ) {
W 430             symlink($conf['init_scripts'].'/pdns', $conf['init_scripts'].'/'.$conf['powerdns']['init_script']);
431         }
432     }
433     
d090db 434     public function configure_bind() {
W 435         global $conf;
436
437         //* Check if the zonefile directory has a slash at the end
438         $content=$conf['bind']['bind_zonefiles_dir'];
439         if(substr($content,-1,1) != '/') {
440             $content .= '/';
441         }
442         
443         //* New default format of named.conf uses views. Check which version the system is using and include our zones file.
444         $named_conf = rf($conf['bind']['named_conf_path']);
445         if (stripos($named_conf, 'include "'.$conf['bind']['named_conf_local_path'].'";') === false) 
446         {
447             preg_match_all("/(?<=\n)view \"(?:public|internal)\" in \{.*\n\};/Us", $named_conf, $views);
448             if (count($views[0]) == 2) {
449                 foreach ($views[0] as $view) {
450                     $named_conf = str_replace($view, substr($view, 0, -2)."include \"{$conf['bind']['named_conf_local_path']}\";\n};", $named_conf);
451                 }
452                 
453                 wf($conf['bind']['named_conf_path'], $named_conf);
454             }
455             else {
456                 af($conf['bind']['named_conf_path'], 'include "'.$conf['bind']['named_conf_local_path'].'";');
457             }
458         }
459     }
460     
cb8c86 461     public function configure_apache()
W 462     {    
463         global $conf;
464         
91324a 465         if($conf['apache']['installed'] == false) return;
ff1d9a 466         //* Create the logging directory for the vhost logfiles
W 467         if (!is_dir($conf['ispconfig_log_dir'].'/httpd')) {
468             mkdir($conf['ispconfig_log_dir'].'/httpd', 0755, true);
cb8c86 469         }
W 470         
471         if (is_file($conf['suphp']['config_file'])) 
472         {
473             $content = rf($conf['suphp']['config_file']);
474             
ff1d9a 475             if (!preg_match('|^x-httpd-suphp=php:/usr/bin/php-cgi$|m', $content))
W 476             {
477                 $content = preg_replace('/;Handler for php-scripts/',";Handler for php-scripts\nx-httpd-suphp=php:/usr/bin/php-cgi", $content);
478                 $content = preg_replace('/;?umask=\d+/','umask=0022', $content);
479             }
cb8c86 480             
W 481             $this->write_config_file($conf['suphp']['config_file'], $content);
482         }
483         
ff1d9a 484         //* Enable ISPConfig default vhost settings
cb8c86 485         $default_vhost_path = $conf['apache']['vhost_conf_dir'].'/'.$conf['apache']['vhost_default'];
W 486         if (is_file($default_vhost_path)) 
487         {
488             $content = rf($default_vhost_path);
489             
490             $content = preg_replace('/^#?\s*NameVirtualHost.*$/m', 'NameVirtualHost *:80', $content);
491             $content = preg_replace('/<VirtualHost[^>]+>/', '<VirtualHost *:80>', $content);
492             
493             $this->write_config_file($default_vhost_path, $content);
494         }
495         
ff1d9a 496         //* Generate default ssl certificates
cb8c86 497         if (!is_dir($conf['apache']['ssl_dir'])) {
W 498             mkdir($conf['apache']['ssl_dir']);
499         }
500         
501         if ($conf['services']['mail'] == true) 
502         {
503             copy($conf['postfix']['config_dir']."/smtpd.key", $conf['apache']['ssl_dir']."/server.key");
504             copy($conf['postfix']['config_dir']."/smtpd.cert", $conf['apache']['ssl_dir']."/server.crt");
505         }
506         else
507         {
508             if (!is_file($conf['apache']['ssl_dir'] . '/server.crt')) {
509                 exec("openssl req -new -outform PEM -out {$conf['apache']['ssl_dir']}/server.crt -newkey rsa:2048 -nodes -keyout {$conf['apache']['ssl_dir']}/server.key -keyform PEM -days 365 -x509");
510             }
511         }
512         
513         
514         
ff1d9a 515         //* Copy the ISPConfig configuration include
cb8c86 516         $content = $this->get_template_file('apache_ispconfig.conf', true);
W 517         
518         $records = $this->db->queryAllRecords("SELECT * FROM server_ip WHERE server_id = ".$conf["server_id"]." AND virtualhost = 'y'");
519         if(is_array($records) && count($records) > 0) 
520         {
521             foreach($records as $rec) {
522                 $content .= "NameVirtualHost ".$rec["ip_address"].":80\n";
523                 $content .= "NameVirtualHost ".$rec["ip_address"].":443\n";
524             }
525         }
526         
527         $this->write_config_file($conf['apache']['vhost_conf_dir'].'/000-ispconfig.conf', $content);
528         
ff1d9a 529         //* Gentoo by default does not include .vhost files. Add include line to config file.
cb8c86 530         $content = rf($conf['apache']['config_file']);
W 531         if ( strpos($content, 'Include /etc/apache2/vhosts.d/*.vhost') === false ) {
532             $content = preg_replace('|(Include /etc/apache2/vhosts.d/\*.conf)|',"$1\nInclude /etc/apache2/vhosts.d/*.vhost", $content);
533         }
534         
535         $this->write_config_file($conf['apache']['config_file'], $content);
536         
ff1d9a 537         //* make sure that webalizer finds its config file when it is directly in /etc
cb8c86 538         if(is_file('/etc/webalizer.conf') && !is_dir('/etc/webalizer')) 
W 539         {
540             mkdir('/etc/webalizer', 0755);
541             symlink('/etc/webalizer.conf', '/etc/webalizer/webalizer.conf');
542         }
543         
ff1d9a 544         if(is_file('/etc/webalizer/webalizer.conf')) //* Change webalizer mode to incremental 
cb8c86 545         {
W 546             replaceLine('/etc/webalizer/webalizer.conf','#IncrementalName','IncrementalName webalizer.current',0,0);
547             replaceLine('/etc/webalizer/webalizer.conf','#Incremental','Incremental     yes',0,0);
548             replaceLine('/etc/webalizer/webalizer.conf','#HistoryName','HistoryName     webalizer.hist',0,0);
549         }
550         
ff1d9a 551         //* add a sshusers group
cb8c86 552         if (!is_group('sshusers')) 
W 553         {
554             $command = 'groupadd sshusers';
555             caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
556         }
557     }
558     
559     public function configure_apps_vhost()
560     {
561         global $conf;
562         
563         //* Create the ispconfig apps vhost user and group
165152 564         if($conf['apache']['installed'] == true){
91324a 565             $apps_vhost_user = escapeshellcmd($conf['web']['apps_vhost_user']);
F 566             $apps_vhost_group = escapeshellcmd($conf['web']['apps_vhost_group']);
567             $install_dir = escapeshellcmd($conf['web']['website_basedir'].'/apps');
cb8c86 568         
91324a 569             $command = 'groupadd '.$apps_vhost_user;
F 570             if ( !is_group($apps_vhost_group) ) {
571                 caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
572             }
cb8c86 573         
91324a 574             $command = "useradd -g '$apps_vhost_group' -d $install_dir $apps_vhost_group";
F 575             if ( !is_user($apps_vhost_user) ) {
576                 caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
577             }
cb8c86 578         
91324a 579             $command = 'adduser '.$conf['apache']['user'].' '.$apps_vhost_group;
F 580             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
cb8c86 581         
99b55b 582             if(!@is_dir($install_dir)){
91324a 583                 mkdir($install_dir, 0755, true);
99b55b 584             } else {
F 585                 chmod($install_dir, 0755);
91324a 586             }
F 587             chown($install_dir, $apps_vhost_user);
588             chgrp($install_dir, $apps_vhost_group);
cb8c86 589         
91324a 590             //* Copy the apps vhost file
F 591             $vhost_conf_dir = $conf['apache']['vhost_conf_dir'];
592             $vhost_conf_enabled_dir = $conf['apache']['vhost_conf_enabled_dir'];
593             $apps_vhost_servername = ($conf['web']['apps_vhost_servername'] == '') ? '' : 'ServerName '.$conf['web']['apps_vhost_servername'];
cb8c86 594         
91324a 595             //* Dont just copy over the virtualhost template but add some custom settings
F 596             $content = $this->get_template_file('apache_apps.vhost', true);
cb8c86 597         
91324a 598             $content = str_replace('{apps_vhost_ip}', $conf['web']['apps_vhost_ip'], $content);
F 599             $content = str_replace('{apps_vhost_port}', $conf['web']['apps_vhost_port'], $content);
600             $content = str_replace('{apps_vhost_dir}', $conf['web']['website_basedir'].'/apps', $content);
601             $content = str_replace('{website_basedir}', $conf['web']['website_basedir'], $content);
602             $content = str_replace('{apps_vhost_servername}', $apps_vhost_servername, $content);
cb8c86 603         
91324a 604             //* comment out the listen directive if port is 80 or 443
F 605             if($conf['web']['apps_vhost_ip'] == 80 or $conf['web']['apps_vhost_ip'] == 443) {
606                 $content = str_replace('{vhost_port_listen}', '#', $content);
607             } else {
608                 $content = str_replace('{vhost_port_listen}', '', $content);
609             }
cb8c86 610         
91324a 611             $this->write_config_file("$vhost_conf_dir/apps.vhost", $content);
cb8c86 612         
91324a 613             if ( !is_file($conf['web']['website_basedir'].'/php-fcgi-scripts/apps/.php-fcgi-starter') ) 
F 614             {
526b99 615                 $content = rf('tpl/apache_apps_fcgi_starter.master');
T 616                 $content = str_replace('{fastcgi_bin}', $conf['fastcgi']['fastcgi_bin'], $content);
617                 $content = str_replace('{fastcgi_phpini_path}', $conf['fastcgi']['fastcgi_phpini_path'], $content);
91324a 618                 mkdir($conf['web']['website_basedir'].'/php-fcgi-scripts/apps', 0755, true);
526b99 619                 //copy('tpl/apache_apps_fcgi_starter.master',$conf['web']['website_basedir'].'/php-fcgi-scripts/apps/.php-fcgi-starter');
T 620                 wf($conf['web']['website_basedir'].'/php-fcgi-scripts/apps/.php-fcgi-starter', $content);
91324a 621                 exec('chmod +x '.$conf['web']['website_basedir'].'/php-fcgi-scripts/apps/.php-fcgi-starter');
F 622                 exec('chown -R ispapps:ispapps '.$conf['web']['website_basedir'].'/php-fcgi-scripts/apps');
cb8c86 623             
91324a 624             }
F 625         }
165152 626         if($conf['nginx']['installed'] == true){
91324a 627             $apps_vhost_user = escapeshellcmd($conf['web']['apps_vhost_user']);
F 628             $apps_vhost_group = escapeshellcmd($conf['web']['apps_vhost_group']);
629             $install_dir = escapeshellcmd($conf['web']['website_basedir'].'/apps');
630
631             $command = 'groupadd '.$apps_vhost_user;
632             if(!is_group($apps_vhost_group)) caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
633
634             $command = 'useradd -g '.$apps_vhost_group.' -d '.$install_dir.' '.$apps_vhost_group;
635             if(!is_user($apps_vhost_user)) caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
636
637
638             $command = 'adduser '.$conf['nginx']['user'].' '.$apps_vhost_group;
639             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
640
6e2d48 641             if(!@is_dir($install_dir)){
F 642                 mkdir($install_dir, 0755, true);
643             } else {
644                 chmod($install_dir, 0755);
645             }
91324a 646             chown($install_dir, $apps_vhost_user);
F 647             chgrp($install_dir, $apps_vhost_group);
648
649             //* Copy the apps vhost file
650             $vhost_conf_dir = $conf['nginx']['vhost_conf_dir'];
651             $vhost_conf_enabled_dir = $conf['nginx']['vhost_conf_enabled_dir'];
652             $apps_vhost_servername = ($conf['web']['apps_vhost_servername'] == '')?'_':$conf['web']['apps_vhost_servername'];
653
654             // Dont just copy over the virtualhost template but add some custom settings
655             $content = rf('tpl/nginx_apps.vhost.master');
656             
657             if($conf['web']['apps_vhost_ip'] == '_default_'){
658                 $apps_vhost_ip = '';
659             } else {
660                 $apps_vhost_ip = $conf['web']['apps_vhost_ip'].':';
661             }
ca0b77 662             
F 663             $socket_dir = escapeshellcmd($conf['nginx']['php_fpm_socket_dir']);
664             if(substr($socket_dir,-1) != '/') $socket_dir .= '/';
665             if(!is_dir($socket_dir)) exec('mkdir -p '.$socket_dir);
666             $fpm_socket = $socket_dir.'apps.sock';
8ab3cd 667             $cgi_socket = escapeshellcmd($conf['nginx']['cgi_socket']);
91324a 668
F 669             $content = str_replace('{apps_vhost_ip}', $apps_vhost_ip, $content);
670             $content = str_replace('{apps_vhost_port}', $conf['web']['apps_vhost_port'], $content);
671             $content = str_replace('{apps_vhost_dir}', $conf['web']['website_basedir'].'/apps', $content);
672             $content = str_replace('{apps_vhost_servername}', $apps_vhost_servername, $content);
ca0b77 673             //$content = str_replace('{fpm_port}', ($conf['nginx']['php_fpm_start_port']+1), $content);
F 674             $content = str_replace('{fpm_socket}', $fpm_socket, $content);
8ab3cd 675             $content = str_replace('{cgi_socket}', $cgi_socket, $content);
91324a 676
F 677             wf($vhost_conf_dir.'/apps.vhost', $content);
678             
679             // PHP-FPM
680             // Dont just copy over the php-fpm pool template but add some custom settings
f19eb6 681             $content = rf('tpl/apps_php_fpm_pool.conf.master');
91324a 682             $content = str_replace('{fpm_pool}', 'apps', $content);
ca0b77 683             //$content = str_replace('{fpm_port}', ($conf['nginx']['php_fpm_start_port']+1), $content);
F 684             $content = str_replace('{fpm_socket}', $fpm_socket, $content);
91324a 685             $content = str_replace('{fpm_user}', $apps_vhost_user, $content);
F 686             $content = str_replace('{fpm_group}', $apps_vhost_group, $content);
687             wf($conf['nginx']['php_fpm_pool_dir'].'/apps.conf', $content);
688
689             //copy('tpl/nginx_ispconfig.vhost.master', "$vhost_conf_dir/ispconfig.vhost");
690             //* and create the symlink
7e1cfb 691             if(@is_link($vhost_conf_enabled_dir.'/apps.vhost')) unlink($vhost_conf_enabled_dir.'/apps.vhost');
F 692             if(!@is_link($vhost_conf_enabled_dir.'/000-apps.vhost')) {
693                 symlink($vhost_conf_dir.'/apps.vhost',$vhost_conf_enabled_dir.'/000-apps.vhost');
91324a 694             }
7e1cfb 695             
cb8c86 696         }
W 697     }
698     
699     public function install_ispconfig()
700     {
701         global $conf;
702         
703         $install_dir = $conf['ispconfig_install_dir'];
704         
ff1d9a 705         //* Create the ISPConfig installation directory
cb8c86 706         if(!is_dir($install_dir)) 
W 707         {
708             $command = "mkdir $install_dir";
709             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
710         }
711         
ff1d9a 712         //* Create a ISPConfig user and group
cb8c86 713         if (!is_group('ispconfig')) 
W 714         {
715             $command = 'groupadd ispconfig';
716             caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
717         }
718         
719         if (!is_user('ispconfig')) 
720         {
721             $command = "useradd -g ispconfig -d $install_dir ispconfig";
722             caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
723         }
724         
ff1d9a 725         //* copy the ISPConfig interface part
cb8c86 726         $command = "cp -rf ../interface $install_dir";
W 727         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
728         
ff1d9a 729         //* copy the ISPConfig server part
cb8c86 730         $command = "cp -rf ../server $install_dir";
W 731         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
732         
733         
ff1d9a 734         //* Create the config file for ISPConfig interface
cb8c86 735         $configfile = 'config.inc.php';
ff1d9a 736         $content = $this->get_template_file($configfile, true, true); //* get contents & insert db cred
cb8c86 737         
W 738         $content = str_replace('{mysql_master_server_ispconfig_user}', $conf['mysql']['master_ispconfig_user'], $content);
739         $content = str_replace('{mysql_master_server_ispconfig_password}', $conf['mysql']['master_ispconfig_password'], $content);
740         $content = str_replace('{mysql_master_server_database}', $conf['mysql']['master_database'], $content);
741         $content = str_replace('{mysql_master_server_host}', $conf['mysql']['master_host'], $content);
742         
743         $content = str_replace('{server_id}', $conf['server_id'], $content);
744         $content = str_replace('{ispconfig_log_priority}', $conf['ispconfig_log_priority'], $content);
745         $content = str_replace('{language}', $conf['language'], $content);
8cf78b 746         $content = str_replace('{timezone}', $conf['timezone'], $content);
41eaa8 747         $content = str_replace('{theme}', $conf['theme'], $content);
cb8c86 748         
W 749         $this->write_config_file("$install_dir/interface/lib/$configfile", $content);
750         
ff1d9a 751         //* Create the config file for ISPConfig server
cb8c86 752         $this->write_config_file("$install_dir/server/lib/$configfile", $content);
W 753         
fb3a98 754         //* Create the config file for remote-actions (but only, if it does not exist, because
T 755         //  the value is a autoinc-value and so changed by the remoteaction_core_module
ff1d9a 756         if (!file_exists($install_dir.'/server/lib/remote_action.inc.php')) {
W 757             $content = '<?php' . "\n" . '$maxid_remote_action = 0;' . "\n" . '?>';
fb3a98 758             wf($install_dir.'/server/lib/remote_action.inc.php', $content);
ff1d9a 759         }
W 760         
fb3a98 761         // Enable the server modules and plugins.
cb8c86 762         // TODO: Implement a selector which modules and plugins shall be enabled.
W 763         $dir = $install_dir.'/server/mods-available/';
764         if (is_dir($dir)) {
765             if ($dh = opendir($dir)) {
766                 while (($file = readdir($dh)) !== false) {
767                     if($file != '.' && $file != '..' && substr($file,-8,8) == '.inc.php') {
768                         include_once($install_dir.'/server/mods-available/'.$file);
769                         $module_name = substr($file,0,-8);
770                         $tmp = new $module_name;
771                         if($tmp->onInstall()) {
ff1d9a 772                             if(!@is_link($install_dir.'/server/mods-enabled/'.$file)) {
W 773                                 @symlink($install_dir.'/server/mods-available/'.$file, $install_dir.'/server/mods-enabled/'.$file);
774                                 // @symlink($install_dir.'/server/mods-available/'.$file, '../mods-enabled/'.$file);
775                             }
cb8c86 776                             if (strpos($file, '_core_module') !== false) {
ff1d9a 777                                 if(!@is_link($install_dir.'/server/mods-core/'.$file)) {
W 778                                     @symlink($install_dir.'/server/mods-available/'.$file, $install_dir.'/server/mods-core/'.$file);
779                                     // @symlink($install_dir.'/server/mods-available/'.$file, '../mods-core/'.$file);
780                                 }
cb8c86 781                             }
W 782                         }
783                         unset($tmp);
784                     }
785                 }
786                 closedir($dh);
787             }
788         }
ff1d9a 789
cb8c86 790         $dir = $install_dir.'/server/plugins-available/';
W 791         if (is_dir($dir)) {
792             if ($dh = opendir($dir)) {
793                 while (($file = readdir($dh)) !== false) {
91324a 794                     if($conf['apache']['installed'] == true && $file == 'nginx_plugin.inc.php') continue;
F 795                     if($conf['nginx']['installed'] == true && $file == 'apache2_plugin.inc.php') continue;
cb8c86 796                     if($file != '.' && $file != '..' && substr($file,-8,8) == '.inc.php') {
W 797                         include_once($install_dir.'/server/plugins-available/'.$file);
798                         $plugin_name = substr($file,0,-8);
799                         $tmp = new $plugin_name;
800                         if(method_exists($tmp,'onInstall') && $tmp->onInstall()) {
ff1d9a 801                             if(!@is_link($install_dir.'/server/plugins-enabled/'.$file)) {
W 802                                 @symlink($install_dir.'/server/plugins-available/'.$file, $install_dir.'/server/plugins-enabled/'.$file);
803                             }
cb8c86 804                             if (strpos($file, '_core_plugin') !== false) {
ff1d9a 805                                 if(!@is_link($install_dir.'/server/plugins-core/'.$file)) {
W 806                                     @symlink($install_dir.'/server/plugins-available/'.$file, $install_dir.'/server/plugins-core/'.$file);
807                                 }
cb8c86 808                             }
W 809                         }
810                         unset($tmp);
811                     }
812                 }
813                 closedir($dh);
814             }
815         }
816         
ff1d9a 817         //* Update the server config
cb8c86 818         $mail_server_enabled = ($conf['services']['mail'])?1:0;
W 819         $web_server_enabled = ($conf['services']['web'])?1:0;
820         $dns_server_enabled = ($conf['services']['dns'])?1:0;
821         $file_server_enabled = ($conf['services']['file'])?1:0;
822         $db_server_enabled = ($conf['services']['db'])?1:0;
823         $vserver_server_enabled = ($conf['services']['vserver'])?1:0;
824         
825         $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']);
826         
827         if($conf['mysql']['master_slave_setup'] == 'y') {
828             $this->dbmaster->query($sql);
829             $this->db->query($sql);
830         } else {
831             $this->db->query($sql);
832         }
833         
ff1d9a 834         //* Chmod the files
cb8c86 835         $command = "chmod -R 750 $install_dir";
W 836         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
837
ff1d9a 838         //* chown the files to the ispconfig user and group
cb8c86 839         $command = "chown -R ispconfig:ispconfig $install_dir";
W 840         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
841         
ff1d9a 842         //* Make the global language file directory group writable
cb8c86 843         exec("chmod -R 770 $install_dir/interface/lib/lang");
W 844         
ff1d9a 845         //* Make the temp directory for language file exports writable
W 846         if(is_dir($install_dir.'/interface/web/temp')) {
847             exec("chmod -R 770 $install_dir/interface/web/temp");
848         }
cb8c86 849         
ff1d9a 850         //* Make all interface language file directories group writable
cb8c86 851         $handle = @opendir($install_dir.'/interface/web');
W 852         while ($file = @readdir ($handle)) { 
853                if ($file != '.' && $file != '..') {
854                 if(@is_dir($install_dir.'/interface/web'.'/'.$file.'/lib/lang')) {
855                     $handle2 = opendir($install_dir.'/interface/web'.'/'.$file.'/lib/lang');
856                     chmod($install_dir.'/interface/web'.'/'.$file.'/lib/lang',0770);
857                     while ($lang_file = @readdir ($handle2)) {
858                         if ($lang_file != '.' && $lang_file != '..') {
859                             chmod($install_dir.'/interface/web'.'/'.$file.'/lib/lang/'.$lang_file,0770);
860                         }
861                     }
862                 }
863             }
864         }
865         
477d4e 866         //* Make the APS directories group writable
T 867         exec("chmod -R 770 $install_dir/interface/web/sites/aps_meta_packages");
868         exec("chmod -R 770 $install_dir/server/aps_packages");
869         
ff1d9a 870         //* make sure that the server config file (not the interface one) is only readable by the root user
bfcdef 871         chmod($install_dir.'/server/lib/config.inc.php', 0600);
T 872         chown($install_dir.'/server/lib/config.inc.php', 'root');
873         chgrp($install_dir.'/server/lib/config.inc.php', 'root');
874         
875         //* Make sure thet the interface config file is readable by user ispconfig only
876         chmod($install_dir.'/interface/lib/config.inc.php', 0600);
877         chown($install_dir.'/interface/lib/config.inc.php', 'ispconfig');
878         chgrp($install_dir.'/interface/lib/config.inc.php', 'ispconfig');
ff1d9a 879
W 880         chmod($install_dir.'/server/lib/remote_action.inc.php', 0600);
881         chown($install_dir.'/server/lib/remote_action.inc.php', 'root');
882         chgrp($install_dir.'/server/lib/remote_action.inc.php', 'root');
883
884         if(@is_file($install_dir.'/server/lib/mysql_clientdb.conf')) {
885             chmod($install_dir.'/server/lib/mysql_clientdb.conf', 0600);
886             chown($install_dir.'/server/lib/mysql_clientdb.conf', 'root');
887             chgrp($install_dir.'/server/lib/mysql_clientdb.conf', 'root');
cb8c86 888         }
W 889         
8cf78b 890         if(is_dir($install_dir.'/interface/invoices')) {
e94a9f 891             exec('chmod -R 770 '.escapeshellarg($install_dir.'/interface/invoices'));
T 892             exec('chown -R ispconfig:ispconfig '.escapeshellarg($install_dir.'/interface/invoices'));
edf806 893         }
T 894         
cb8c86 895         // TODO: FIXME: add the www-data user to the ispconfig group. This is just for testing
W 896         // and must be fixed as this will allow the apache user to read the ispconfig files.
897         // Later this must run as own apache server or via suexec!
63b369 898         if($conf['apache']['installed'] == true){
F 899             $command = 'usermod -a -G ispconfig '.$conf['apache']['user'];
900             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
272aec 901             if(is_group('ispapps')){
F 902                 $command = 'usermod -a -G ispapps '.$conf['apache']['user'];
903                 caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
904             }
63b369 905         }
F 906         if($conf['nginx']['installed'] == true){
907             $command = 'usermod -a -G ispconfig '.$conf['nginx']['user'];
908             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
272aec 909             if(is_group('ispapps')){
F 910                 $command = 'usermod -a -G ispapps '.$conf['nginx']['user'];
911                 caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
912             }
63b369 913         }
cb8c86 914         
ff1d9a 915         //* Make the shell scripts executable
cb8c86 916         $command = "chmod +x $install_dir/server/scripts/*.sh";
W 917         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
918         
7e1cfb 919         if($conf['apache']['installed'] == true && $this->install_ispconfig_interface == true){
91324a 920             //* Copy the ISPConfig vhost for the controlpanel
F 921             $content = $this->get_template_file("apache_ispconfig.vhost", true);
922             $content = str_replace('{vhost_port}', $conf['apache']['vhost_port'], $content);
cb8c86 923         
91324a 924             //* comment out the listen directive if port is 80 or 443
F 925             if ($conf['apache']['vhost_port'] == 80 or $conf['apache']['vhost_port'] == 443) {
926                 $content = str_replace('{vhost_port_listen}', '#', $content);
927             } else {
928                 $content = str_replace('{vhost_port_listen}', '', $content);
929             }
930         
931             if(is_file($install_dir.'/interface/ssl/ispserver.crt') && is_file($install_dir.'/interface/ssl/ispserver.key')) {
932                 $content = str_replace('{ssl_comment}', '', $content);
933             } else {
934                 $content = str_replace('{ssl_comment}', '#', $content);
935             }
10b4c8 936             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 937                 $content = str_replace('{ssl_bundle_comment}', '', $content);
938             } else {
939                 $content = str_replace('{ssl_bundle_comment}', '#', $content);
940             }
91324a 941         
F 942             $vhost_path = $conf['apache']['vhost_conf_dir'].'/ispconfig.vhost';
943             $this->write_config_file($vhost_path, $content);
944         
526b99 945             if(!is_file('/var/www/php-fcgi-scripts/ispconfig/.php-fcgi-starter')) {
T 946                 $content = rf('tpl/apache_ispconfig_fcgi_starter.master');
947                 $content = str_replace('{fastcgi_bin}', $conf['fastcgi']['fastcgi_bin'], $content);
948                 $content = str_replace('{fastcgi_phpini_path}', $conf['fastcgi']['fastcgi_phpini_path'], $content);
91324a 949                 mkdir('/var/www/php-fcgi-scripts/ispconfig', 0755, true);
526b99 950                 wf('/var/www/php-fcgi-scripts/ispconfig/.php-fcgi-starter', $content);
91324a 951                 exec('chmod +x /var/www/php-fcgi-scripts/ispconfig/.php-fcgi-starter');
F 952                 chmod('/var/www/php-fcgi-scripts/ispconfig/.php-fcgi-starter', 0755);
526b99 953                 symlink($install_dir.'/interface/web','/var/www/ispconfig');
91324a 954                 exec('chown -R ispconfig:ispconfig /var/www/php-fcgi-scripts/ispconfig');
F 955             }
cb8c86 956         }
91324a 957
7e1cfb 958         if($conf['nginx']['installed'] == true && $this->install_ispconfig_interface == true){
91324a 959             //* Copy the ISPConfig vhost for the controlpanel
F 960             $vhost_conf_dir = $conf['nginx']['vhost_conf_dir'];
961             $vhost_conf_enabled_dir = $conf['nginx']['vhost_conf_enabled_dir'];
962
963             // Dont just copy over the virtualhost template but add some custom settings
964             $content = rf('tpl/nginx_ispconfig.vhost.master');
965             $content = str_replace('{vhost_port}', $conf['nginx']['vhost_port'], $content);
cb8c86 966         
91324a 967             if(is_file($install_dir.'/interface/ssl/ispserver.crt') && is_file($install_dir.'/interface/ssl/ispserver.key')) {
10b4c8 968                 $content = str_replace('{ssl_on}', ' on', $content);
91324a 969                 $content = str_replace('{ssl_comment}', '', $content);
F 970                 $content = str_replace('{fastcgi_ssl}', 'on', $content);
971             } else {
10b4c8 972                 $content = str_replace('{ssl_on}', ' off', $content);
91324a 973                 $content = str_replace('{ssl_comment}', '#', $content);
F 974                 $content = str_replace('{fastcgi_ssl}', 'off', $content);
975             }
976             
ca0b77 977             $socket_dir = escapeshellcmd($conf['nginx']['php_fpm_socket_dir']);
F 978             if(substr($socket_dir,-1) != '/') $socket_dir .= '/';
979             if(!is_dir($socket_dir)) exec('mkdir -p '.$socket_dir);
980             $fpm_socket = $socket_dir.'ispconfig.sock';
981             
982             //$content = str_replace('{fpm_port}', $conf['nginx']['php_fpm_start_port'], $content);
983             $content = str_replace('{fpm_socket}', $fpm_socket, $content);
91324a 984
F 985             wf($vhost_conf_dir.'/ispconfig.vhost', $content);
986             
987             unset($content);
988             
989             // PHP-FPM
990             // Dont just copy over the php-fpm pool template but add some custom settings
991             $content = rf('tpl/php_fpm_pool.conf.master');
992             $content = str_replace('{fpm_pool}', 'ispconfig', $content);
ca0b77 993             //$content = str_replace('{fpm_port}', $conf['nginx']['php_fpm_start_port'], $content);
F 994             $content = str_replace('{fpm_socket}', $fpm_socket, $content);
91324a 995             $content = str_replace('{fpm_user}', 'ispconfig', $content);
F 996             $content = str_replace('{fpm_group}', 'ispconfig', $content);
997             wf($conf['nginx']['php_fpm_pool_dir'].'/ispconfig.conf', $content);
998
999             //copy('tpl/nginx_ispconfig.vhost.master', $vhost_conf_dir.'/ispconfig.vhost');
1000             //* and create the symlink
7e1cfb 1001             if($this->is_update == false) {
91324a 1002                 if(@is_link($vhost_conf_enabled_dir.'/ispconfig.vhost')) unlink($vhost_conf_enabled_dir.'/ispconfig.vhost');
F 1003                 if(!@is_link($vhost_conf_enabled_dir.'/000-ispconfig.vhost')) {
1004                     symlink($vhost_conf_dir.'/ispconfig.vhost',$vhost_conf_enabled_dir.'/000-ispconfig.vhost');
1005                 }
1006             }
cb8c86 1007         }
W 1008         
ff1d9a 1009         //* Install the update script
cb8c86 1010         if (is_file('/usr/local/bin/ispconfig_update_from_svn.sh')) {
W 1011             unlink('/usr/local/bin/ispconfig_update_from_svn.sh');
1012         }
1013         
ff1d9a 1014         chown($install_dir.'/server/scripts/update_from_svn.sh', 'root');
W 1015         chmod($install_dir.'/server/scripts/update_from_svn.sh', 0700);
1016         chown($install_dir.'/server/scripts/update_from_tgz.sh', 'root');
1017         chmod($install_dir.'/server/scripts/update_from_tgz.sh', 0700);
1018         chown($install_dir.'/server/scripts/ispconfig_update.sh', 'root');
1019         chmod($install_dir.'/server/scripts/ispconfig_update.sh', 0700);
cb8c86 1020         
W 1021         if (!is_link('/usr/local/bin/ispconfig_update_from_svn.sh')) {
ff1d9a 1022             symlink($install_dir.'/server/scripts/ispconfig_update.sh', '/usr/local/bin/ispconfig_update_from_svn.sh');
cb8c86 1023         }
W 1024         
1025         if (!is_link('/usr/local/bin/ispconfig_update.sh')) {
ff1d9a 1026             symlink($install_dir.'/server/scripts/ispconfig_update.sh', '/usr/local/bin/ispconfig_update.sh');
cb8c86 1027         }
W 1028         
ff1d9a 1029         //* Make the logs readable for the ispconfig user
cb8c86 1030         if (is_file('/var/log/maillog')) {
W 1031             exec('chmod +r /var/log/maillog');
1032         }
1033         if (is_file('/var/log/messages')) {
1034             exec('chmod +r /var/log/messages');
1035         }
1036         if (is_file('/var/log/clamav/clamav.log')) {
1037             exec('chmod +r /var/log/clamav/clamav.log');
1038         }
1039         if (is_file('/var/log/clamav/freshclam.log')) {
1040             exec('chmod +r /var/log/clamav/freshclam.log');
1041         }
1042         
ff1d9a 1043         //* Create the ispconfig log directory
e38d14 1044         if (!is_dir($conf['ispconfig_log_dir'])) {
J 1045             mkdir($conf['ispconfig_log_dir']);
cb8c86 1046         }
e38d14 1047         if (!is_file($conf['ispconfig_log_dir'].'/ispconfig.log')) {
ff1d9a 1048             touch($conf['ispconfig_log_dir'].'/ispconfig.log');
cb8c86 1049         }
W 1050         
0799f8 1051         //* Create the ispconfig auth log file and set uid/gid
T 1052         if(!is_file($conf['ispconfig_log_dir'].'/auth.log')) {
1053             touch($conf['ispconfig_log_dir'].'/auth.log');
1054         }
1055         exec('chown ispconfig:ispconfig '. $conf['ispconfig_log_dir'].'/auth.log');
1056         exec('chmod 660 '. $conf['ispconfig_log_dir'].'/auth.log');
1057         
ff1d9a 1058         rename($install_dir.'/server/scripts/run-getmail.sh', '/usr/local/bin/run-getmail.sh');
W 1059         
1060         if (is_user('getmail')) {
1061             chown('/usr/local/bin/run-getmail.sh', 'getmail');
1062         }
1063         chmod('/usr/local/bin/run-getmail.sh', 0744);
d71bae 1064         
T 1065         //* Remove Domain module as its functions are available in the client module now
1066         if(@is_dir('/usr/local/ispconfig/interface/web/domain')) exec('rm -rf /usr/local/ispconfig/interface/web/domain');
1067         
cb8c86 1068     }
20218c 1069 }
M 1070
e38d14 1071 ?>