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