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