Till Brehm
2014-08-14 a13af29287c5f3c72f21d104651ece92b4a9e52e
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         
TB 752         //* copy the ISPConfig security part
753         $command = 'cp -rf ../security '.$install_dir;
754         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
7fe908 755
MC 756
ff1d9a 757         //* Create the config file for ISPConfig interface
cb8c86 758         $configfile = 'config.inc.php';
ff1d9a 759         $content = $this->get_template_file($configfile, true, true); //* get contents & insert db cred
7fe908 760
cb8c86 761         $content = str_replace('{mysql_master_server_ispconfig_user}', $conf['mysql']['master_ispconfig_user'], $content);
W 762         $content = str_replace('{mysql_master_server_ispconfig_password}', $conf['mysql']['master_ispconfig_password'], $content);
763         $content = str_replace('{mysql_master_server_database}', $conf['mysql']['master_database'], $content);
764         $content = str_replace('{mysql_master_server_host}', $conf['mysql']['master_host'], $content);
7fe908 765
cb8c86 766         $content = str_replace('{server_id}', $conf['server_id'], $content);
W 767         $content = str_replace('{ispconfig_log_priority}', $conf['ispconfig_log_priority'], $content);
768         $content = str_replace('{language}', $conf['language'], $content);
8cf78b 769         $content = str_replace('{timezone}', $conf['timezone'], $content);
41eaa8 770         $content = str_replace('{theme}', $conf['theme'], $content);
992797 771         $content = str_replace('{language_file_import_enabled}', ($conf['language_file_import_enabled'] == true)?'true':'false', $content);
7fe908 772
cb8c86 773         $this->write_config_file("$install_dir/interface/lib/$configfile", $content);
7fe908 774
ff1d9a 775         //* Create the config file for ISPConfig server
cb8c86 776         $this->write_config_file("$install_dir/server/lib/$configfile", $content);
7fe908 777
fb3a98 778         //* Create the config file for remote-actions (but only, if it does not exist, because
T 779         //  the value is a autoinc-value and so changed by the remoteaction_core_module
ff1d9a 780         if (!file_exists($install_dir.'/server/lib/remote_action.inc.php')) {
W 781             $content = '<?php' . "\n" . '$maxid_remote_action = 0;' . "\n" . '?>';
fb3a98 782             wf($install_dir.'/server/lib/remote_action.inc.php', $content);
ff1d9a 783         }
7fe908 784
MC 785         // Enable the server modules and plugins.
cb8c86 786         // TODO: Implement a selector which modules and plugins shall be enabled.
W 787         $dir = $install_dir.'/server/mods-available/';
788         if (is_dir($dir)) {
789             if ($dh = opendir($dir)) {
790                 while (($file = readdir($dh)) !== false) {
7fe908 791                     if($file != '.' && $file != '..' && substr($file, -8, 8) == '.inc.php') {
MC 792                         include_once $install_dir.'/server/mods-available/'.$file;
793                         $module_name = substr($file, 0, -8);
cb8c86 794                         $tmp = new $module_name;
W 795                         if($tmp->onInstall()) {
ff1d9a 796                             if(!@is_link($install_dir.'/server/mods-enabled/'.$file)) {
W 797                                 @symlink($install_dir.'/server/mods-available/'.$file, $install_dir.'/server/mods-enabled/'.$file);
798                                 // @symlink($install_dir.'/server/mods-available/'.$file, '../mods-enabled/'.$file);
799                             }
cb8c86 800                             if (strpos($file, '_core_module') !== false) {
ff1d9a 801                                 if(!@is_link($install_dir.'/server/mods-core/'.$file)) {
W 802                                     @symlink($install_dir.'/server/mods-available/'.$file, $install_dir.'/server/mods-core/'.$file);
803                                     // @symlink($install_dir.'/server/mods-available/'.$file, '../mods-core/'.$file);
804                                 }
cb8c86 805                             }
W 806                         }
807                         unset($tmp);
808                     }
809                 }
810                 closedir($dh);
811             }
812         }
ff1d9a 813
cb8c86 814         $dir = $install_dir.'/server/plugins-available/';
W 815         if (is_dir($dir)) {
816             if ($dh = opendir($dir)) {
817                 while (($file = readdir($dh)) !== false) {
91324a 818                     if($conf['apache']['installed'] == true && $file == 'nginx_plugin.inc.php') continue;
F 819                     if($conf['nginx']['installed'] == true && $file == 'apache2_plugin.inc.php') continue;
7fe908 820                     if($file != '.' && $file != '..' && substr($file, -8, 8) == '.inc.php') {
MC 821                         include_once $install_dir.'/server/plugins-available/'.$file;
822                         $plugin_name = substr($file, 0, -8);
cb8c86 823                         $tmp = new $plugin_name;
7fe908 824                         if(method_exists($tmp, 'onInstall') && $tmp->onInstall()) {
ff1d9a 825                             if(!@is_link($install_dir.'/server/plugins-enabled/'.$file)) {
W 826                                 @symlink($install_dir.'/server/plugins-available/'.$file, $install_dir.'/server/plugins-enabled/'.$file);
827                             }
cb8c86 828                             if (strpos($file, '_core_plugin') !== false) {
ff1d9a 829                                 if(!@is_link($install_dir.'/server/plugins-core/'.$file)) {
W 830                                     @symlink($install_dir.'/server/plugins-available/'.$file, $install_dir.'/server/plugins-core/'.$file);
831                                 }
cb8c86 832                             }
W 833                         }
834                         unset($tmp);
835                     }
836                 }
837                 closedir($dh);
838             }
839         }
7fe908 840
ff1d9a 841         //* Update the server config
cb8c86 842         $mail_server_enabled = ($conf['services']['mail'])?1:0;
W 843         $web_server_enabled = ($conf['services']['web'])?1:0;
844         $dns_server_enabled = ($conf['services']['dns'])?1:0;
845         $file_server_enabled = ($conf['services']['file'])?1:0;
846         $db_server_enabled = ($conf['services']['db'])?1:0;
847         $vserver_server_enabled = ($conf['services']['vserver'])?1:0;
7fe908 848
MC 849         $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']);
850
cb8c86 851         if($conf['mysql']['master_slave_setup'] == 'y') {
W 852             $this->dbmaster->query($sql);
853             $this->db->query($sql);
854         } else {
855             $this->db->query($sql);
856         }
7fe908 857
3e0fc8 858         // chown install dir to root and chmod 755
TB 859         $command = 'chown root:root '.$install_dir;
860         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
861         $command = 'chmod 755 '.$install_dir;
cb8c86 862         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
W 863
fa029b 864         //* Chmod the files and directories in the install dir
3e0fc8 865         $command = 'chmod -R 750 '.$install_dir.'/*';
TB 866         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
867
868         //* chown the interface files to the ispconfig user and group
869         $command = 'chown -R ispconfig:ispconfig '.$install_dir.'/interface';
870         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
871         
872         //* chown the server files to the root user and group
873         $command = 'chown -R root:root '.$install_dir.'/server';
cb8c86 874         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
fa029b 875         
TB 876         //* chown the security files to the root user and group
877         $command = 'chown -R root:root '.$install_dir.'/security';
878         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
879         
880         //* chown the security directory and security_settings.ini to root:ispconfig
881         $command = 'chown root:ispconfig '.$install_dir.'/security/security_settings.ini';
882         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
883         $command = 'chown root:ispconfig '.$install_dir.'/security';
884         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
7fe908 885
ff1d9a 886         //* Make the global language file directory group writable
cb8c86 887         exec("chmod -R 770 $install_dir/interface/lib/lang");
7fe908 888
ff1d9a 889         //* Make the temp directory for language file exports writable
W 890         if(is_dir($install_dir.'/interface/web/temp')) {
891             exec("chmod -R 770 $install_dir/interface/web/temp");
892         }
7fe908 893
MC 894         //* Make all interface language file directories group writable
cb8c86 895         $handle = @opendir($install_dir.'/interface/web');
7fe908 896         while ($file = @readdir($handle)) {
MC 897             if ($file != '.' && $file != '..') {
898                 if(@is_dir($install_dir.'/interface/web'.'/'.$file.'/lib/lang')) {
cb8c86 899                     $handle2 = opendir($install_dir.'/interface/web'.'/'.$file.'/lib/lang');
7fe908 900                     chmod($install_dir.'/interface/web'.'/'.$file.'/lib/lang', 0770);
MC 901                     while ($lang_file = @readdir($handle2)) {
cb8c86 902                         if ($lang_file != '.' && $lang_file != '..') {
7fe908 903                             chmod($install_dir.'/interface/web'.'/'.$file.'/lib/lang/'.$lang_file, 0770);
cb8c86 904                         }
W 905                     }
906                 }
907             }
908         }
7fe908 909
477d4e 910         //* Make the APS directories group writable
T 911         exec("chmod -R 770 $install_dir/interface/web/sites/aps_meta_packages");
912         exec("chmod -R 770 $install_dir/server/aps_packages");
7fe908 913
MC 914         //* make sure that the server config file (not the interface one) is only readable by the root user
bfcdef 915         chmod($install_dir.'/server/lib/config.inc.php', 0600);
T 916         chown($install_dir.'/server/lib/config.inc.php', 'root');
917         chgrp($install_dir.'/server/lib/config.inc.php', 'root');
7fe908 918
bfcdef 919         //* Make sure thet the interface config file is readable by user ispconfig only
T 920         chmod($install_dir.'/interface/lib/config.inc.php', 0600);
921         chown($install_dir.'/interface/lib/config.inc.php', 'ispconfig');
922         chgrp($install_dir.'/interface/lib/config.inc.php', 'ispconfig');
ff1d9a 923
W 924         chmod($install_dir.'/server/lib/remote_action.inc.php', 0600);
925         chown($install_dir.'/server/lib/remote_action.inc.php', 'root');
926         chgrp($install_dir.'/server/lib/remote_action.inc.php', 'root');
927
928         if(@is_file($install_dir.'/server/lib/mysql_clientdb.conf')) {
929             chmod($install_dir.'/server/lib/mysql_clientdb.conf', 0600);
930             chown($install_dir.'/server/lib/mysql_clientdb.conf', 'root');
931             chgrp($install_dir.'/server/lib/mysql_clientdb.conf', 'root');
cb8c86 932         }
7fe908 933
8cf78b 934         if(is_dir($install_dir.'/interface/invoices')) {
e94a9f 935             exec('chmod -R 770 '.escapeshellarg($install_dir.'/interface/invoices'));
T 936             exec('chown -R ispconfig:ispconfig '.escapeshellarg($install_dir.'/interface/invoices'));
edf806 937         }
7fe908 938
cb8c86 939         // TODO: FIXME: add the www-data user to the ispconfig group. This is just for testing
W 940         // and must be fixed as this will allow the apache user to read the ispconfig files.
941         // Later this must run as own apache server or via suexec!
63b369 942         if($conf['apache']['installed'] == true){
F 943             $command = 'usermod -a -G ispconfig '.$conf['apache']['user'];
944             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
272aec 945             if(is_group('ispapps')){
F 946                 $command = 'usermod -a -G ispapps '.$conf['apache']['user'];
947                 caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
948             }
63b369 949         }
F 950         if($conf['nginx']['installed'] == true){
951             $command = 'usermod -a -G ispconfig '.$conf['nginx']['user'];
952             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
272aec 953             if(is_group('ispapps')){
F 954                 $command = 'usermod -a -G ispapps '.$conf['nginx']['user'];
955                 caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
956             }
63b369 957         }
7fe908 958
ff1d9a 959         //* Make the shell scripts executable
cb8c86 960         $command = "chmod +x $install_dir/server/scripts/*.sh";
W 961         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
7fe908 962
7e1cfb 963         if($conf['apache']['installed'] == true && $this->install_ispconfig_interface == true){
91324a 964             //* Copy the ISPConfig vhost for the controlpanel
F 965             $content = $this->get_template_file("apache_ispconfig.vhost", true);
966             $content = str_replace('{vhost_port}', $conf['apache']['vhost_port'], $content);
7fe908 967
91324a 968             //* comment out the listen directive if port is 80 or 443
F 969             if ($conf['apache']['vhost_port'] == 80 or $conf['apache']['vhost_port'] == 443) {
970                 $content = str_replace('{vhost_port_listen}', '#', $content);
971             } else {
972                 $content = str_replace('{vhost_port_listen}', '', $content);
973             }
7fe908 974
91324a 975             if(is_file($install_dir.'/interface/ssl/ispserver.crt') && is_file($install_dir.'/interface/ssl/ispserver.key')) {
F 976                 $content = str_replace('{ssl_comment}', '', $content);
977             } else {
978                 $content = str_replace('{ssl_comment}', '#', $content);
979             }
10b4c8 980             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 981                 $content = str_replace('{ssl_bundle_comment}', '', $content);
982             } else {
983                 $content = str_replace('{ssl_bundle_comment}', '#', $content);
984             }
7fe908 985
91324a 986             $vhost_path = $conf['apache']['vhost_conf_dir'].'/ispconfig.vhost';
F 987             $this->write_config_file($vhost_path, $content);
7fe908 988
526b99 989             if(!is_file('/var/www/php-fcgi-scripts/ispconfig/.php-fcgi-starter')) {
615a0a 990                 $content = rfsel($conf['ispconfig_install_dir'].'/server/conf-custom/install/apache_ispconfig_fcgi_starter.master', 'tpl/apache_ispconfig_fcgi_starter.master');
526b99 991                 $content = str_replace('{fastcgi_bin}', $conf['fastcgi']['fastcgi_bin'], $content);
T 992                 $content = str_replace('{fastcgi_phpini_path}', $conf['fastcgi']['fastcgi_phpini_path'], $content);
cc6568 993                 @mkdir('/var/www/php-fcgi-scripts/ispconfig', 0755, true);
526b99 994                 wf('/var/www/php-fcgi-scripts/ispconfig/.php-fcgi-starter', $content);
91324a 995                 exec('chmod +x /var/www/php-fcgi-scripts/ispconfig/.php-fcgi-starter');
F 996                 chmod('/var/www/php-fcgi-scripts/ispconfig/.php-fcgi-starter', 0755);
7fe908 997                 @symlink($install_dir.'/interface/web', '/var/www/ispconfig');
91324a 998                 exec('chown -R ispconfig:ispconfig /var/www/php-fcgi-scripts/ispconfig');
F 999             }
cb8c86 1000         }
91324a 1001
7e1cfb 1002         if($conf['nginx']['installed'] == true && $this->install_ispconfig_interface == true){
91324a 1003             //* Copy the ISPConfig vhost for the controlpanel
F 1004             $vhost_conf_dir = $conf['nginx']['vhost_conf_dir'];
1005             $vhost_conf_enabled_dir = $conf['nginx']['vhost_conf_enabled_dir'];
1006
1007             // Dont just copy over the virtualhost template but add some custom settings
615a0a 1008             $content = rfsel($conf['ispconfig_install_dir'].'/server/conf-custom/install/nginx_ispconfig.vhost.master', 'tpl/nginx_ispconfig.vhost.master');
91324a 1009             $content = str_replace('{vhost_port}', $conf['nginx']['vhost_port'], $content);
7fe908 1010
91324a 1011             if(is_file($install_dir.'/interface/ssl/ispserver.crt') && is_file($install_dir.'/interface/ssl/ispserver.key')) {
10b4c8 1012                 $content = str_replace('{ssl_on}', ' on', $content);
91324a 1013                 $content = str_replace('{ssl_comment}', '', $content);
F 1014                 $content = str_replace('{fastcgi_ssl}', 'on', $content);
1015             } else {
10b4c8 1016                 $content = str_replace('{ssl_on}', ' off', $content);
91324a 1017                 $content = str_replace('{ssl_comment}', '#', $content);
F 1018                 $content = str_replace('{fastcgi_ssl}', 'off', $content);
1019             }
7fe908 1020
ca0b77 1021             $socket_dir = escapeshellcmd($conf['nginx']['php_fpm_socket_dir']);
7fe908 1022             if(substr($socket_dir, -1) != '/') $socket_dir .= '/';
ca0b77 1023             if(!is_dir($socket_dir)) exec('mkdir -p '.$socket_dir);
F 1024             $fpm_socket = $socket_dir.'ispconfig.sock';
7fe908 1025
ca0b77 1026             //$content = str_replace('{fpm_port}', $conf['nginx']['php_fpm_start_port'], $content);
F 1027             $content = str_replace('{fpm_socket}', $fpm_socket, $content);
91324a 1028
F 1029             wf($vhost_conf_dir.'/ispconfig.vhost', $content);
7fe908 1030
91324a 1031             unset($content);
7fe908 1032
91324a 1033             // PHP-FPM
F 1034             // Dont just copy over the php-fpm pool template but add some custom settings
615a0a 1035             $content = rfsel($conf['ispconfig_install_dir'].'/server/conf-custom/install/php_fpm_pool.conf.master', 'tpl/php_fpm_pool.conf.master');
91324a 1036             $content = str_replace('{fpm_pool}', 'ispconfig', $content);
ca0b77 1037             //$content = str_replace('{fpm_port}', $conf['nginx']['php_fpm_start_port'], $content);
F 1038             $content = str_replace('{fpm_socket}', $fpm_socket, $content);
91324a 1039             $content = str_replace('{fpm_user}', 'ispconfig', $content);
F 1040             $content = str_replace('{fpm_group}', 'ispconfig', $content);
1041             wf($conf['nginx']['php_fpm_pool_dir'].'/ispconfig.conf', $content);
1042
1043             //copy('tpl/nginx_ispconfig.vhost.master', $vhost_conf_dir.'/ispconfig.vhost');
1044             //* and create the symlink
7e1cfb 1045             if($this->is_update == false) {
91324a 1046                 if(@is_link($vhost_conf_enabled_dir.'/ispconfig.vhost')) unlink($vhost_conf_enabled_dir.'/ispconfig.vhost');
F 1047                 if(!@is_link($vhost_conf_enabled_dir.'/000-ispconfig.vhost')) {
7fe908 1048                     symlink($vhost_conf_dir.'/ispconfig.vhost', $vhost_conf_enabled_dir.'/000-ispconfig.vhost');
91324a 1049                 }
F 1050             }
cb8c86 1051         }
7fe908 1052
ff1d9a 1053         //* Install the update script
b34f99 1054         if (is_file('/usr/local/bin/ispconfig_update_from_dev.sh')) {
MC 1055             unlink('/usr/local/bin/ispconfig_update_from_dev.sh');
cb8c86 1056         }
7fe908 1057
b34f99 1058         chown($install_dir.'/server/scripts/update_from_dev.sh', 'root');
MC 1059         chmod($install_dir.'/server/scripts/update_from_dev.sh', 0700);
ff1d9a 1060         chown($install_dir.'/server/scripts/update_from_tgz.sh', 'root');
W 1061         chmod($install_dir.'/server/scripts/update_from_tgz.sh', 0700);
1062         chown($install_dir.'/server/scripts/ispconfig_update.sh', 'root');
1063         chmod($install_dir.'/server/scripts/ispconfig_update.sh', 0700);
7fe908 1064
b34f99 1065         if (!is_link('/usr/local/bin/ispconfig_update_from_dev.sh')) {
MC 1066             symlink($install_dir.'/server/scripts/ispconfig_update.sh', '/usr/local/bin/ispconfig_update_from_dev.sh');
cb8c86 1067         }
7fe908 1068
cb8c86 1069         if (!is_link('/usr/local/bin/ispconfig_update.sh')) {
ff1d9a 1070             symlink($install_dir.'/server/scripts/ispconfig_update.sh', '/usr/local/bin/ispconfig_update.sh');
cb8c86 1071         }
7fe908 1072
ff1d9a 1073         //* Make the logs readable for the ispconfig user
cb8c86 1074         if (is_file('/var/log/maillog')) {
W 1075             exec('chmod +r /var/log/maillog');
1076         }
1077         if (is_file('/var/log/messages')) {
1078             exec('chmod +r /var/log/messages');
1079         }
1080         if (is_file('/var/log/clamav/clamav.log')) {
1081             exec('chmod +r /var/log/clamav/clamav.log');
1082         }
1083         if (is_file('/var/log/clamav/freshclam.log')) {
1084             exec('chmod +r /var/log/clamav/freshclam.log');
1085         }
7fe908 1086
ff1d9a 1087         //* Create the ispconfig log directory
e38d14 1088         if (!is_dir($conf['ispconfig_log_dir'])) {
J 1089             mkdir($conf['ispconfig_log_dir']);
cb8c86 1090         }
e38d14 1091         if (!is_file($conf['ispconfig_log_dir'].'/ispconfig.log')) {
ff1d9a 1092             touch($conf['ispconfig_log_dir'].'/ispconfig.log');
cb8c86 1093         }
7fe908 1094
0799f8 1095         //* Create the ispconfig auth log file and set uid/gid
T 1096         if(!is_file($conf['ispconfig_log_dir'].'/auth.log')) {
1097             touch($conf['ispconfig_log_dir'].'/auth.log');
1098         }
1099         exec('chown ispconfig:ispconfig '. $conf['ispconfig_log_dir'].'/auth.log');
1100         exec('chmod 660 '. $conf['ispconfig_log_dir'].'/auth.log');
7fe908 1101
ff1d9a 1102         rename($install_dir.'/server/scripts/run-getmail.sh', '/usr/local/bin/run-getmail.sh');
7fe908 1103
ff1d9a 1104         if (is_user('getmail')) {
W 1105             chown('/usr/local/bin/run-getmail.sh', 'getmail');
1106         }
1107         chmod('/usr/local/bin/run-getmail.sh', 0744);
7fe908 1108
d71bae 1109         //* Remove Domain module as its functions are available in the client module now
T 1110         if(@is_dir('/usr/local/ispconfig/interface/web/domain')) exec('rm -rf /usr/local/ispconfig/interface/web/domain');
021aec 1111         
TB 1112         // Add symlink for patch tool
1113         if(!is_link('/usr/local/bin/ispconfig_patch')) exec('ln -s /usr/local/ispconfig/server/scripts/ispconfig_patch /usr/local/bin/ispconfig_patch');
7fe908 1114
MC 1115     }
1116
20218c 1117 }
M 1118
e38d14 1119 ?>