tbrehm
2009-03-12 608a8c7012e1947b020cb751f4ffda2acac40690
commit | author | age
9200ad 1 <?php
T 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
31 class installer_base {
32     
33     var $wb = array();
34     var $language = 'en';
35     var $db;
cc3fb3 36     public $conf;
ca5291 37     public $install_ispconfig_interface = true;
4f68a7 38     public $is_update = false; // true if it is an update, falsi if it is a new install
09e141 39
P 40
32814e 41     public function __construct()
5133cc 42     {
b0a1cc 43         global $conf; //TODO: maybe $conf  should be passed to constructor
cd972d 44         //$this->conf = $conf;
5133cc 45     }
9200ad 46     
4f7028 47     //: TODO  Implement the translation function and language files for the installer.
32814e 48     public function lng($text)
P 49     {
ce9544 50         return $text;
9200ad 51     }
T 52     
32814e 53     public function error($msg)
P 54     {
9200ad 55         die("ERROR: ".$msg."\n");
T 56     }
57     
32814e 58     public function simple_query($query, $answers, $default)
P 59     {        
ce9544 60         $finished = false;
T 61         do {
32814e 62             $answers_str = implode(',', $answers);
239ce8 63             swrite($this->lng($query).' ('.$answers_str.') ['.$default.']: ');
ce9544 64             $input = sread();
T 65             
32814e 66             //* Stop the installation
ce9544 67             if($input == 'quit') {
32814e 68                 swriteln($this->lng("Installation terminated by user.\n"));
239ce8 69                 die();
ce9544 70             }
T 71             
32814e 72             //* Select the default
ce9544 73             if($input == '') {
T 74                 $answer = $default;
75                 $finished = true;
76             }
77             
32814e 78             //* Set answer id valid
P 79             if(in_array($input, $answers)) {
ce9544 80                 $answer = $input;
T 81                 $finished = true;
82             }
83             
84         } while ($finished == false);
239ce8 85         swriteln();
ce9544 86         return $answer;
T 87     }
88     
32814e 89     public function free_query($query,$default)
P 90     {        
239ce8 91         swrite($this->lng($query).' ['.$default.']: ');
ce9544 92         $input = sread();
T 93             
32814e 94         //* Stop the installation
ce9544 95         if($input == 'quit') {
32814e 96             swriteln($this->lng("Installation terminated by user.\n"));
P 97             die();
ce9544 98         }
T 99             
32814e 100         $answer =  ($input == '') ? $default : $input;
239ce8 101         swriteln();
ce9544 102         return $answer;
T 103     }
104     
1b063e 105     /*
32814e 106     // TODO: this function is not used atmo I think - pedro
P 107     function request_language(){
9200ad 108         
T 109         swriteln(lng('Enter your language'));
110         swriteln(lng('de, en'));
111         
112     }
1b063e 113     */
9200ad 114     
0a1f02 115     //** Detect installed applications
T 116     public function find_installed_apps() {
117         global $conf;
118         
119         if(is_installed('mysql') || is_installed('mysqld')) $conf['mysql']['installed'] = true;
120         if(is_installed('postfix')) $conf['postfix']['installed'] = true;
121         if(is_installed('apache') || is_installed('apache2') || is_installed('httpd')) $conf['apache']['installed'] = true;
122         if(is_installed('getmail')) $conf['getmail']['installed'] = true;
123         if(is_installed('couriertcpd')) $conf['courier']['installed'] = true;
124         if(is_installed('saslsauthd')) $conf['saslauthd']['installed'] = true;
125         if(is_installed('amavisd-new')) $conf['amavis']['installed'] = true;
126         if(is_installed('clamdscan')) $conf['clamav']['installed'] = true;
127         if(is_installed('pure-ftpd') || is_installed('pure-ftpd-wrapper')) $conf['pureftpd']['installed'] = true;
128         if(is_installed('mydns') || is_installed('mydns-ng')) $conf['mydns']['installed'] = true;
129         if(is_installed('jk_chrootsh')) $conf['jailkit']['installed'] = true;
130         
131         
132     }
133     
facccb 134     /** Create the database for ISPConfig */ 
0a1f02 135     public function configure_database() {
9200ad 136         global $conf;
01074a 137         
facccb 138         //** Create the database
12e3ba 139         if(!$this->db->query('CREATE DATABASE IF NOT EXISTS '.$conf['mysql']['database'].' DEFAULT CHARACTER SET '.$conf['mysql']['charset'])) {
T 140             $this->error('Unable to create MySQL database: '.$conf['mysql']['database'].'.');
9200ad 141         }
T 142         
facccb 143         //* Set the database name in the DB library
12e3ba 144         $this->db->dbName = $conf['mysql']['database'];
9200ad 145         
facccb 146         //* Load the database dump into the database, if database contains no tables
9200ad 147         $db_tables = $this->db->getTables();
T 148         if(count($db_tables) > 0) {
66b4f9 149             $this->error('Stopped: Database already contains some tables.');
9200ad 150         } else {
12e3ba 151             if($conf['mysql']['admin_password'] == '') {
T 152                 caselog("mysql --default-character-set=".$conf['mysql']['charset']." -h '".$conf['mysql']['host']."' -u '".$conf['mysql']['admin_user']."' '".$conf['mysql']['database']."' < '".ISPC_INSTALL_ROOT."/install/sql/ispconfig3.sql' &> /dev/null", 
66b4f9 153                         __FILE__, __LINE__, 'read in ispconfig3.sql', 'could not read in ispconfig3.sql');
9200ad 154             } else {
12e3ba 155                 caselog("mysql --default-character-set=".$conf['mysql']['charset']." -h '".$conf['mysql']['host']."' -u '".$conf['mysql']['admin_user']."' -p'".$conf['mysql']['admin_password']."' '".$conf['mysql']['database']."' < '".ISPC_INSTALL_ROOT."/install/sql/ispconfig3.sql' &> /dev/null", 
66b4f9 156                         __FILE__, __LINE__, 'read in ispconfig3.sql', 'could not read in ispconfig3.sql');
9200ad 157             }
T 158             $db_tables = $this->db->getTables();
159             if(count($db_tables) == 0) {
160                 $this->error('Unable to load SQL-Dump into database table.');
161             }
db5aa6 162             
T 163             //* Load system.ini into the sys_ini table
164             $system_ini = $this->db->quote(rf('tpl/system.ini.master'));
165             $this->db->query("UPDATE sys_ini SET config = '$system_ini' WHERE sysini_id = 1");
166             
9200ad 167         }
T 168     }
169     
4f7028 170     //** Create the server record in the database
94411b 171     public function add_database_server_record() {
7bd4b4 172         
T 173         global $conf;
01074a 174         
12e3ba 175         if($conf['mysql']['host'] == 'localhost') {
ac4a37 176             $from_host = 'localhost';
T 177         } else {
cd972d 178             $from_host = $conf['hostname'];
ac4a37 179         }
T 180         
12e3ba 181         // Delete ISPConfig user in the local database, in case that it exists
T 182         $this->db->query("DELETE FROM mysql.user WHERE User = '".$conf['mysql']['ispconfig_user']."' AND Host = '".$from_host."';");
183         $this->db->query("DELETE FROM mysql.db WHERE Db = '".$conf['mysql']['database']."' AND Host = '".$from_host."';");
f1b989 184         $this->db->query('FLUSH PRIVILEGES;');
T 185         
12e3ba 186         //* Create the ISPConfig database user in the local database
T 187         $query = 'GRANT SELECT, INSERT, UPDATE, DELETE ON '.$conf['mysql']['database'].".* "
188                 ."TO '".$conf['mysql']['ispconfig_user']."'@'".$from_host."' "
189                 ."IDENTIFIED BY '".$conf['mysql']['ispconfig_password']."';";
7bd4b4 190         if(!$this->db->query($query)) {
12e3ba 191             $this->error('Unable to create database user: '.$conf['mysql']['ispconfig_user'].' Error: '.$this->db->errorMessage);
7bd4b4 192         }
T 193         
194         //* Reload database privelages
195         $this->db->query('FLUSH PRIVILEGES;');
196         
a768b9 197         //* Set the database name in the DB library
12e3ba 198         $this->db->dbName = $conf['mysql']['database'];
94411b 199         
0a1f02 200         $tpl_ini_array = ini_to_array(rf('tpl/server.ini.master'));
T 201         
202         // TODO: Update further distribution specific parameters for server config here
203         $tpl_ini_array['web']['vhost_conf_dir'] = $conf['apache']['vhost_conf_dir'];
204         $tpl_ini_array['web']['vhost_conf_enabled_dir'] = $conf['apache']['vhost_conf_enabled_dir'];
0f76eb 205         $tpl_ini_array['jailkit']['jailkit_chroot_app_programs'] = $conf['jailkit']['jailkit_chroot_app_programs'];
T 206         $tpl_ini_array['fastcgi']['fastcgi_phpini_path'] = $conf['fastcgi']['fastcgi_phpini_path'];
8b2f57 207         $tpl_ini_array['server']['hostname'] = $conf['hostname'];
T 208         $tpl_ini_array['server']['ip_address'] = @gethostbyname($conf['hostname']);
fb3749 209         $tpl_ini_array['web']['website_basedir'] = $conf['web']['website_basedir'];
T 210         $tpl_ini_array['web']['website_path'] = $conf['web']['website_path'];
211         $tpl_ini_array['web']['website_symlinks'] = $conf['web']['website_symlinks'];
0a1f02 212         
T 213         $server_ini_content = array_to_ini($tpl_ini_array);
8500be 214         $server_ini_content = mysql_real_escape_string($server_ini_content);
0a1f02 215         
T 216         $mail_server_enabled = ($conf['services']['mail'])?1:0;
217         $web_server_enabled = ($conf['services']['web'])?1:0;
218         $dns_server_enabled = ($conf['services']['dns'])?1:0;
219         $file_server_enabled = ($conf['services']['file'])?1:0;
220         $db_server_enabled = ($conf['services']['db'])?1:0;
221         $vserver_server_enabled = ($conf['services']['vserver'])?1:0;
94411b 222         
12e3ba 223         if($conf['mysql']['master_slave_setup'] == 'y') {
T 224             
225             //* Insert the server record in master DB
0a1f02 226             $sql = "INSERT INTO `server` (`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_name`, `mail_server`, `web_server`, `dns_server`, `file_server`, `db_server`, `vserver_server`, `config`, `updated`, `active`) VALUES (1, 1, 'riud', 'riud', 'r', '".$conf['hostname']."', '$mail_server_enabled', '$web_server_enabled', '$dns_server_enabled', '$file_server_enabled', '$db_server_enabled', '$vserver_server_enabled', '$server_ini_content', 0, 1);";
12e3ba 227             $this->dbmaster->query($sql);
T 228             $conf['server_id'] = $this->dbmaster->insertID();
229             $conf['server_id'] = $conf['server_id'];
230             
231             //* Insert the same record in the local DB
0a1f02 232             $sql = "INSERT INTO `server` (`server_id`, `sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_name`, `mail_server`, `web_server`, `dns_server`, `file_server`, `db_server`, `vserver_server`, `config`, `updated`, `active`) VALUES ('".$conf['server_id']."',1, 1, 'riud', 'riud', 'r', '".$conf['hostname']."', '$mail_server_enabled', '$web_server_enabled', '$dns_server_enabled', '$file_server_enabled', '$db_server_enabled', '$vserver_server_enabled', '$server_ini_content', 0, 1);";
12e3ba 233             $this->db->query($sql);
T 234             
235             //* insert the ispconfig user in the remote server
236             $from_host = $conf['hostname'];
fb15d6 237             $from_ip = gethostbyname($conf['hostname']);
12e3ba 238             
T 239             //* username for the ispconfig user
6cecfc 240             $conf['mysql']['master_ispconfig_user'] = 'ispcsrv'.$conf['server_id'];
12e3ba 241         
fb15d6 242             //* Delete ISPConfig user in the master database, in case that it exists
12e3ba 243             $this->dbmaster->query("DELETE FROM mysql.user WHERE User = '".$conf['mysql']['master_ispconfig_user']."' AND Host = '".$from_host."';");
T 244             $this->dbmaster->query("DELETE FROM mysql.db WHERE Db = '".$conf['mysql']['master_database']."' AND Host = '".$from_host."';");
fb15d6 245             $this->dbmaster->query("DELETE FROM mysql.user WHERE User = '".$conf['mysql']['master_ispconfig_user']."' AND Host = '".$from_ip."';");
T 246             $this->dbmaster->query("DELETE FROM mysql.db WHERE Db = '".$conf['mysql']['master_database']."' AND Host = '".$from_ip."';");
12e3ba 247             $this->dbmaster->query('FLUSH PRIVILEGES;');
T 248         
249             //* Create the ISPConfig database user in the local database
399f82 250             /*$query = 'GRANT SELECT, INSERT, UPDATE, DELETE ON '.$conf['mysql']['master_database'].".* "
12e3ba 251                     ."TO '".$conf['mysql']['master_ispconfig_user']."'@'".$from_host."' "
399f82 252                     ."IDENTIFIED BY '".$conf['mysql']['master_ispconfig_password']."';";*/
fb15d6 253             $query = 'GRANT SELECT, INSERT, UPDATE, DELETE ON '.$conf['mysql']['master_database'].".* "
T 254                     ."TO '".$conf['mysql']['master_ispconfig_user']."'@'".$from_ip."' "
255                     ."IDENTIFIED BY '".$conf['mysql']['master_ispconfig_password']."';";
12e3ba 256             if(!$this->dbmaster->query($query)) {
T 257                 $this->error('Unable to create database user in master database: '.$conf['mysql']['master_ispconfig_user'].' Error: '.$this->dbmaster->errorMessage);
258             }
259         
260         } else {
261             //* Insert the server, if its not a mster / slave setup
0a1f02 262             $sql = "INSERT INTO `server` (`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_name`, `mail_server`, `web_server`, `dns_server`, `file_server`, `db_server`, `vserver_server`, `config`, `updated`, `active`) VALUES (1, 1, 'riud', 'riud', 'r', '".$conf['hostname']."', '$mail_server_enabled', '$web_server_enabled', '$dns_server_enabled', '$file_server_enabled', '$db_server_enabled', '$vserver_server_enabled', '$server_ini_content', 0, 1);";
12e3ba 263             $this->db->query($sql);
T 264             $conf['server_id'] = $this->db->insertID();
265             $conf['server_id'] = $conf['server_id'];
266         }
267         
268         
94411b 269     }
T 270     
b41692 271
09e141 272     //** writes postfix configuration files
7d89f5 273     public function process_postfix_config($configfile)
01074a 274     {    
T 275         global $conf;
276         
cd972d 277         $config_dir = $conf['postfix']['config_dir'].'/';
b41692 278         $full_file_name = $config_dir.$configfile; 
P 279         //* Backup exiting file
280         if(is_file($full_file_name)){
281             copy($full_file_name, $config_dir.$configfile.'~');
282         }
283         $content = rf('tpl/'.$configfile.'.master');
cd972d 284         $content = str_replace('{mysql_server_ispconfig_user}', $conf['mysql']['ispconfig_user'], $content);
T 285         $content = str_replace('{mysql_server_ispconfig_password}', $conf['mysql']['ispconfig_password'], $content);
286         $content = str_replace('{mysql_server_database}', $conf['mysql']['database'], $content);
287         $content = str_replace('{mysql_server_ip}', $conf['mysql']['ip'], $content);
288         $content = str_replace('{server_id}', $conf['server_id'], $content);
b41692 289         wf($full_file_name, $content);
P 290     }
291
61d290 292     public function configure_jailkit()
D 293     {
01074a 294         global $conf;
T 295         
296         $cf = $conf['jailkit'];
61d290 297         $config_dir = $cf['config_dir'];
D 298         $jk_init = $cf['jk_init'];
299         $jk_chrootsh = $cf['jk_chrootsh'];
300         
3e8065 301         if (is_dir($config_dir))
61d290 302         {
fad1df 303             if(is_file($config_dir.'/'.$jk_init)) copy($config_dir.'/'.$jk_init, $config_dir.'/'.$jk_init.'~');
T 304             if(is_file($config_dir.'/'.$jk_chrootsh.".master")) copy($config_dir.'/'.$jk_chrootsh.".master", $config_dir.'/'.$jk_chrootsh.'~');
61d290 305             
D 306             copy('tpl/'.$jk_init.".master", $config_dir.'/'.$jk_init);
307             copy('tpl/'.$jk_chrootsh.".master", $config_dir.'/'.$jk_chrootsh);
308         }
309         
310     }
311         
b41692 312     public function configure_postfix($options = '')
P 313     {
cd972d 314         global $conf;
T 315         $cf = $conf['postfix'];
77ab0a 316         $config_dir = $cf['config_dir'];
P 317         
b41692 318         if(!is_dir($config_dir)){
P 319             $this->error("The postfix configuration directory '$config_dir' does not exist.");
320         }
321         
322         //* mysql-virtual_domains.cf
323         $this->process_postfix_config('mysql-virtual_domains.cf');
324
325         //* mysql-virtual_forwardings.cf
326         $this->process_postfix_config('mysql-virtual_forwardings.cf');
327
328         //* mysql-virtual_mailboxes.cf
329         $this->process_postfix_config('mysql-virtual_mailboxes.cf');
330
331         //* mysql-virtual_email2email.cf
332         $this->process_postfix_config('mysql-virtual_email2email.cf');
333
334         //* mysql-virtual_transports.cf
335         $this->process_postfix_config('mysql-virtual_transports.cf');
336
337         //* mysql-virtual_recipient.cf
338         $this->process_postfix_config('mysql-virtual_recipient.cf');
339
340         //* mysql-virtual_sender.cf
341         $this->process_postfix_config('mysql-virtual_sender.cf');
342
343         //* mysql-virtual_client.cf
344         $this->process_postfix_config('mysql-virtual_client.cf');
5bf366 345         
T 346         //* mysql-virtual_relaydomains.cf
347         $this->process_postfix_config('mysql-virtual_relaydomains.cf');
b41692 348
P 349         //* Changing mode and group of the new created config files.
77ab0a 350         caselog('chmod o= '.$config_dir.'/mysql-virtual_*.cf* &> /dev/null',
P 351                  __FILE__, __LINE__, 'chmod on mysql-virtual_*.cf*', 'chmod on mysql-virtual_*.cf* failed');
352         caselog('chgrp '.$cf['group'].' '.$config_dir.'/mysql-virtual_*.cf* &> /dev/null', 
353                 __FILE__, __LINE__, 'chgrp on mysql-virtual_*.cf*', 'chgrp on mysql-virtual_*.cf* failed');
9200ad 354         
77ab0a 355         //* Creating virtual mail user and group
P 356         $command = 'groupadd -g '.$cf['vmail_groupid'].' '.$cf['vmail_groupname'];
392450 357         if(!is_group($cf['vmail_groupname'])) caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
77ab0a 358
P 359         $command = 'useradd -g '.$cf['vmail_groupname'].' -u '.$cf['vmail_userid'].' '.$cf['vmail_username'].' -d '.$cf['vmail_mailbox_base'].' -m';
392450 360         if(!is_user($cf['vmail_username'])) caselog("$command &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");        
9200ad 361
T 362         $postconf_commands = array (
cd972d 363             'myhostname = '.$conf['hostname'],
T 364             'mydestination = '.$conf['hostname'].', localhost, localhost.localdomain',
9200ad 365             'mynetworks = 127.0.0.0/8',
T 366             'virtual_alias_domains =',
438d9f 367             'virtual_alias_maps = proxy:mysql:'.$config_dir.'/mysql-virtual_forwardings.cf, mysql:'.$config_dir.'/mysql-virtual_email2email.cf',
P 368             'virtual_mailbox_domains = proxy:mysql:'.$config_dir.'/mysql-virtual_domains.cf',
77ab0a 369             'virtual_mailbox_maps = proxy:mysql:'.$config_dir.'/mysql-virtual_mailboxes.cf',
P 370             'virtual_mailbox_base = '.$cf['vmail_mailbox_base'],
371             'virtual_uid_maps = static:'.$cf['vmail_userid'],
372             'virtual_gid_maps = static:'.$cf['vmail_groupid'],
9200ad 373             'smtpd_sasl_auth_enable = yes',
T 374             'broken_sasl_auth_clients = yes',
7b26e8 375             'smtpd_sasl_authenticated_header = yes',
438d9f 376             'smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, check_recipient_access mysql:'.$config_dir.'/mysql-virtual_recipient.cf, reject_unauth_destination',
9200ad 377             'smtpd_use_tls = yes',
e77915 378             'smtpd_tls_security_level = may',
01423f 379             'smtpd_tls_cert_file = '.$config_dir.'/smtpd.cert',
R 380             'smtpd_tls_key_file = '.$config_dir.'/smtpd.key',
438d9f 381             'transport_maps = proxy:mysql:'.$config_dir.'/mysql-virtual_transports.cf',
5bf366 382             'relay_domains = mysql:'.$config_dir.'/mysql-virtual_relaydomains.cf',
9200ad 383             'virtual_create_maildirsize = yes',
766a45 384             'virtual_maildir_extended = yes',
438d9f 385             'virtual_mailbox_limit_maps = proxy:mysql:'.$config_dir.'/mysql-virtual_mailbox_limit_maps.cf',
9200ad 386             'virtual_mailbox_limit_override = yes',
T 387             'virtual_maildir_limit_message = "The user you are trying to reach is over quota."',
388             'virtual_overquota_bounce = yes',
389             'proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_domains $canonical_maps $sender_canonical_maps $recipient_canonical_maps $relocated_maps $transport_maps $mynetworks $virtual_mailbox_limit_maps',
438d9f 390             'smtpd_sender_restrictions = check_sender_access mysql:'.$config_dir.'/mysql-virtual_sender.cf',
P 391             'smtpd_client_restrictions = check_client_access mysql:'.$config_dir.'/mysql-virtual_client.cf',
9200ad 392             'maildrop_destination_concurrency_limit = 1',
T 393             'maildrop_destination_recipient_limit   = 1',
54fb59 394             'virtual_transport = maildrop',
438d9f 395             'header_checks = regexp:'.$config_dir.'/header_checks',
P 396             'mime_header_checks = regexp:'.$config_dir.'/mime_header_checks',
397             'nested_header_checks = regexp:'.$config_dir.'/nested_header_checks',
398             'body_checks = regexp:'.$config_dir.'/body_checks'
9200ad 399         );
T 400         
438d9f 401         //* Create the header and body check files
P 402         touch($config_dir.'/header_checks');
403         touch($config_dir.'/mime_header_checks');
404         touch($config_dir.'/nested_header_checks');
405         touch($config_dir.'/body_checks');
54fb59 406         
T 407         
438d9f 408         //* Make a backup copy of the main.cf file
P 409         copy($config_dir.'/main.cf', $config_dir.'/main.cf~');
9200ad 410         
438d9f 411         //* Executing the postconf commands
9200ad 412         foreach($postconf_commands as $cmd) {
T 413             $command = "postconf -e '$cmd'";
438d9f 414             caselog($command." &> /dev/null", __FILE__, __LINE__, 'EXECUTED: '.$command, 'Failed to execute the command '.$command);
9200ad 415         }
T 416         
fb7155 417         if(!stristr($options,'dont-create-certs')) {
438d9f 418             //* Create the SSL certificate
01423f 419             $command = 'cd '.$config_dir.'; '
e77915 420                       .'openssl req -new -outform PEM -out smtpd.cert -newkey rsa:2048 -nodes -keyout smtpd.key -keyform PEM -days 365 -x509';
c58e3f 421             exec($command);
9200ad 422         
01423f 423             $command = 'chmod o= '.$config_dir.'/smtpd.key';
438d9f 424             caselog($command.' &> /dev/null', __FILE__, __LINE__, 'EXECUTED: '.$command, 'Failed to execute the command '.$command);
c58e3f 425         }
9200ad 426         
77ab0a 427         //** We have to change the permissions of the courier authdaemon directory to make it accessible for maildrop.
438d9f 428         $command = 'chmod 755  /var/run/courier/authdaemon/';
P 429         caselog($command.' &> /dev/null', __FILE__, __LINE__, 'EXECUTED: '.$command, 'Failed to execute the command '.$command);
9200ad 430         
438d9f 431         //* Changing maildrop lines in posfix master.cf
P 432         if(is_file($config_dir.'/master.cf')){
433             copy($config_dir.'/master.cf', $config_dir.'/master.cf~');
434         }
435         if(is_file($config_dir.'/master.cf~')){
436             exec('chmod 400 '.$config_dir.'/master.cf~');
437         }
438         $configfile = $config_dir.'/master.cf';
20e642 439         $content = rf($configfile);
73b93e 440         $content = str_replace('flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}', 
V 441                    'flags=R user='.$cf['vmail_username'].' argv=/usr/bin/maildrop -d ${recipient} ${extension} ${recipient} ${user} ${nexthop} ${sender}',
77ab0a 442                      $content);
P 443         wf($configfile, $content);
20e642 444         
438d9f 445         //* Writing the Maildrop mailfilter file
9200ad 446         $configfile = 'mailfilter';
77ab0a 447         if(is_file($cf['vmail_mailbox_base'].'/.'.$configfile)){
P 448             copy($cf['vmail_mailbox_base'].'/.'.$configfile, $cf['vmail_mailbox_base'].'/.'.$configfile.'~');
449         }
450         $content = rf("tpl/$configfile.master");
451         $content = str_replace('{dist_postfix_vmail_mailbox_base}', $cf['vmail_mailbox_base'], $content);
452         wf($cf['vmail_mailbox_base'].'/.'.$configfile, $content);
9200ad 453         
77ab0a 454         //* Create the directory for the custom mailfilters
a3ec01 455         if(!is_dir($cf['vmail_mailbox_base'].'/mailfilters')) {
T 456             $command = 'mkdir '.$cf['vmail_mailbox_base'].'/mailfilters';
457             caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
458         }
9200ad 459         
77ab0a 460         //* Chmod and chown the .mailfilter file
P 461         $command = 'chown -R '.$cf['vmail_username'].':'.$cf['vmail_groupname'].' '.$cf['vmail_mailbox_base'].'/.mailfilter';
462         caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
9200ad 463         
77ab0a 464         $command = 'chmod -R 600 '.$cf['vmail_mailbox_base'].'/.mailfilter';
P 465         caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
9200ad 466         
T 467     }
468     
7d89f5 469     public function configure_saslauthd() {
9200ad 470         global $conf;
T 471         
20e642 472     
9200ad 473         $configfile = 'sasl_smtpd.conf';
cc3fb3 474         if(is_file($conf["postfix"]["config_dir"].'/sasl/smtpd.conf')) copy($conf["postfix"]["config_dir"].'/sasl/smtpd.conf',$conf["postfix"]["config_dir"].'/sasl/smtpd.conf~');
O 475         if(is_file($conf["postfix"]["config_dir"].'/sasl/smtpd.conf~')) exec('chmod 400 '.$conf["postfix"]["config_dir"].'/sasl/smtpd.conf~');
9200ad 476         $content = rf("tpl/".$configfile.".master");
cd972d 477         $content = str_replace('{mysql_server_ispconfig_user}',$conf['mysql']['ispconfig_user'],$content);
T 478         $content = str_replace('{mysql_server_ispconfig_password}',$conf['mysql']['ispconfig_password'], $content);
479         $content = str_replace('{mysql_server_database}',$conf['mysql']['database'],$content);
480         $content = str_replace('{mysql_server_ip}',$conf['mysql']['ip'],$content);
cc3fb3 481         wf($conf["postfix"]["config_dir"].'/sasl/smtpd.conf',$content);
9200ad 482         
T 483         // TODO: Chmod and chown on the config file
20e642 484         
T 485         
486         
487         // Create the spool directory
20218c 488         exec('mkdir -p /var/spool/postfix/var/run/saslauthd');
20e642 489         
T 490         // Edit the file /etc/default/saslauthd
cc3fb3 491         $configfile = $conf["saslauthd"]["config"];
20e642 492         if(is_file($configfile)) copy($configfile,$configfile.'~');
e32699 493         if(is_file($configfile.'~')) exec('chmod 400 '.$configfile.'~');
20e642 494         $content = rf($configfile);
T 495         $content = str_replace('START=no','START=yes',$content);
20aa6b 496         // Debian
20e642 497         $content = str_replace('OPTIONS="-c"','OPTIONS="-m /var/spool/postfix/var/run/saslauthd -r"',$content);
20aa6b 498         // Ubuntu
T 499         $content = str_replace('OPTIONS="-c -m /var/run/saslauthd"','OPTIONS="-c -m /var/spool/postfix/var/run/saslauthd -r"',$content);
20e642 500         wf($configfile,$content);
T 501         
20218c 502         // Edit the file /etc/init.d/saslauthd
cc3fb3 503         $configfile = $conf["init_scripts"].'/'.$conf["saslauthd"]["init_script"];
20e642 504         $content = rf($configfile);
T 505         $content = str_replace('PIDFILE=$RUN_DIR/saslauthd.pid','PIDFILE="/var/spool/postfix/var/run/${NAME}/saslauthd.pid"',$content);
506         wf($configfile,$content);
20aa6b 507         
T 508         // add the postfix user to the sasl group (at least nescessary for ubuntu 8.04 and most likely debian lenny too.
509         exec('adduser postfix sasl');
9200ad 510         
T 511         
512     }
513     
09e141 514     public function configure_pam()
P 515     {
cd972d 516         global $conf;
T 517         $pam = $conf['pam'];
09e141 518         //* configure pam for SMTP authentication agains the ispconfig database
9200ad 519         $configfile = 'pamd_smtp';
09e141 520         if(is_file("$pam/smtp"))    copy("$pam/smtp", "$pam/smtp~");
P 521         if(is_file("$pam/smtp~"))   exec("chmod 400 $pam/smtp~");
522
523         $content = rf("tpl/$configfile.master");
cd972d 524         $content = str_replace('{mysql_server_ispconfig_user}', $conf['mysql']['ispconfig_user'], $content);
T 525         $content = str_replace('{mysql_server_ispconfig_password}', $conf['mysql']['ispconfig_password'], $content);
526         $content = str_replace('{mysql_server_database}', $conf['mysql']['database'], $content);
527         $content = str_replace('{mysql_server_ip}', $conf['mysql']['ip'], $content);
09e141 528         wf("$pam/smtp", $content);
P 529         exec("chmod 660 $pam/smtp");
530         exec("chown daemon:daemon $pam/smtp");
9200ad 531     
T 532     }
533     
09e141 534     public function configure_courier()
P 535     {
cd972d 536         global $conf;
T 537         $config_dir = $conf['courier']['config_dir'];
09e141 538         //* authmysqlrc
9200ad 539         $configfile = 'authmysqlrc';
09e141 540         if(is_file("$config_dir/$configfile")){
P 541             copy("$config_dir/$configfile", "$config_dir/$configfile~");
542         }
543         exec("chmod 400 $config_dir/$configfile~");
544         $content = rf("tpl/$configfile.master");
cd972d 545         $content = str_replace('{mysql_server_ispconfig_user}',$conf['mysql']['ispconfig_user'],$content);
T 546         $content = str_replace('{mysql_server_ispconfig_password}',$conf['mysql']['ispconfig_password'], $content);
547         $content = str_replace('{mysql_server_database}',$conf['mysql']['database'],$content);
548         $content = str_replace('{mysql_server_host}',$conf['mysql']['host'],$content);
09e141 549         wf("$config_dir/$configfile", $content);
9200ad 550         
09e141 551         exec("chmod 660 $config_dir/$configfile");
P 552         exec("chown daemon:daemon $config_dir/$configfile");
20e642 553         
09e141 554         //* authdaemonrc
cd972d 555         $configfile = $conf['courier']['config_dir'].'/authdaemonrc';
09e141 556         if(is_file($configfile)){
P 557             copy($configfile, $configfile.'~');
558         }
559         if(is_file($configfile.'~')){
560             exec('chmod 400 '.$configfile.'~');
561         }
20e642 562         $content = rf($configfile);
09e141 563         $content = str_replace('authmodulelist="authpam"', 'authmodulelist="authmysql"', $content);
P 564         wf($configfile, $content);
9200ad 565     }
T 566     
7d89f5 567     public function configure_amavis() {
9200ad 568         global $conf;
T 569         
570         // amavisd user config file
571         $configfile = 'amavisd_user_config';
cc3fb3 572         if(is_file($conf["amavis"]["config_dir"].'/conf.d/50-user')) copy($conf["amavis"]["config_dir"].'/conf.d/50-user',$conf["courier"]["config_dir"].'/50-user~');
O 573         if(is_file($conf["amavis"]["config_dir"].'/conf.d/50-user~')) exec('chmod 400 '.$conf["amavis"]["config_dir"].'/conf.d/50-user~');
9200ad 574         $content = rf("tpl/".$configfile.".master");
cd972d 575         $content = str_replace('{mysql_server_ispconfig_user}',$conf['mysql']['ispconfig_user'],$content);
T 576         $content = str_replace('{mysql_server_ispconfig_password}',$conf['mysql']['ispconfig_password'], $content);
577         $content = str_replace('{mysql_server_database}',$conf['mysql']['database'],$content);
20218c 578         $content = str_replace('{mysql_server_port}',$conf["mysql"]["port"],$content);
cd972d 579         $content = str_replace('{mysql_server_ip}',$conf['mysql']['ip'],$content);
cc3fb3 580         wf($conf["amavis"]["config_dir"].'/conf.d/50-user',$content);
9200ad 581         
T 582         // TODO: chmod and chown on the config file
583         
584         
585         // Adding the amavisd commands to the postfix configuration
586         $postconf_commands = array (
587             'content_filter = amavis:[127.0.0.1]:10024',
588             'receive_override_options = no_address_mappings'
589         );
590         
591         // Make a backup copy of the main.cf file
cc3fb3 592         copy($conf["postfix"]["config_dir"].'/main.cf',$conf["postfix"]["config_dir"].'/main.cf~2');
9200ad 593         
T 594         // Executing the postconf commands
595         foreach($postconf_commands as $cmd) {
596             $command = "postconf -e '$cmd'";
76b6b6 597             caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
9200ad 598         }
T 599         
600         // Append the configuration for amavisd to the master.cf file
cc3fb3 601         if(is_file($conf["postfix"]["config_dir"].'/master.cf')) copy($conf["postfix"]["config_dir"].'/master.cf',$conf["postfix"]["config_dir"].'/master.cf~');
7d89f5 602         $content = rf($conf["postfix"]["config_dir"].'/master.cf');
9200ad 603         // Only add the content if we had not addded it before
4d4495 604         if(!stristr($content,"127.0.0.1:10025")) {
7d89f5 605             unset($content);
T 606             $content = rf("tpl/master_cf_amavis.master");
cc3fb3 607             af($conf["postfix"]["config_dir"].'/master.cf',$content);
9200ad 608         }
7d89f5 609         unset($content);
9200ad 610         
T 611         // Add the clamav user to the amavis group
612         exec('adduser clamav amavis');
613         
614         
615     }
616     
77ab0a 617     public function configure_spamassassin()
P 618     {
cd972d 619         global $conf;
T 620         
09e141 621         //* Enable spamasasssin on debian and ubuntu
9200ad 622         $configfile = '/etc/default/spamassassin';
77ab0a 623         if(is_file($configfile)){
09e141 624             copy($configfile, $configfile.'~');
77ab0a 625         }
9200ad 626         $content = rf($configfile);
77ab0a 627         $content = str_replace('ENABLED=0', 'ENABLED=1', $content);
P 628         wf($configfile, $content);
9200ad 629     }
T 630     
09e141 631     public function configure_getmail()
P 632     {
af5c9b 633         global $conf;
T 634         
cd972d 635         $config_dir = $conf['getmail']['config_dir'];
03ade5 636         
T 637         if(!is_dir($config_dir)) exec("mkdir -p ".escapeshellcmd($config_dir));
09e141 638
P 639         $command = "useradd -d $config_dir getmail";
392450 640         if(!is_user('getmail')) caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
ba747c 641         
09e141 642         $command = "chown -R getmail $config_dir";
P 643         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
3edf9d 644         
09e141 645         $command = "chmod -R 700 $config_dir";
P 646         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
ba747c 647     }
T 648     
9200ad 649     
09e141 650     public function configure_pureftpd()
P 651     {
7bd4b4 652         global $conf;
T 653         
cd972d 654         $config_dir = $conf['pureftpd']['config_dir'];
09e141 655
P 656         //* configure pam for SMTP authentication agains the ispconfig database
20218c 657         $configfile = 'db/mysql.conf';
09e141 658         if(is_file("$config_dir/$configfile")){
P 659             copy("$config_dir/$configfile", "$config_dir/$configfile~");
660         }
661         if(is_file("$config_dir/$configfile~")){
662             exec("chmod 400 $config_dir/$configfile~");
663         }
664         $content = rf('tpl/pureftpd_mysql.conf.master');
7bd4b4 665         $content = str_replace('{mysql_server_ispconfig_user}', $conf["mysql"]["ispconfig_user"], $content);
T 666         $content = str_replace('{mysql_server_ispconfig_password}', $conf["mysql"]["ispconfig_password"], $content);
667         $content = str_replace('{mysql_server_database}', $conf["mysql"]["database"], $content);
668         $content = str_replace('{mysql_server_ip}', $conf["mysql"]["ip"], $content);
669         $content = str_replace('{server_id}', $conf["server_id"], $content);
09e141 670         wf("$config_dir/$configfile", $content);
P 671         exec("chmod 600 $config_dir/$configfile");
672         exec("chown root:root $config_dir/$configfile");
673         // **enable chrooting
af8f1b 674         //exec('mkdir -p '.$config_dir.'/conf/ChrootEveryone');
09e141 675         exec('echo "yes" > '.$config_dir.'/conf/ChrootEveryone');
2549b2 676         exec('echo "yes" > '.$config_dir.'/conf/BrokenClientsCompatibility');
b4c750 677     }
T 678     
09e141 679     public function configure_mydns()
P 680     {
99d85e 681         global $conf;
T 682         
683         // configure pam for SMTP authentication agains the ispconfig database
684         $configfile = 'mydns.conf';
cc3fb3 685         if(is_file($conf["mydns"]["config_dir"].'/'.$configfile)) copy($conf["mydns"]["config_dir"].'/'.$configfile,$conf["mydns"]["config_dir"].'/'.$configfile.'~');
O 686         if(is_file($conf["mydns"]["config_dir"].'/'.$configfile.'~')) exec('chmod 400 '.$conf["mydns"]["config_dir"].'/'.$configfile.'~');
99d85e 687         $content = rf("tpl/".$configfile.".master");
7bd4b4 688         $content = str_replace('{mysql_server_ispconfig_user}',$conf['mysql']['ispconfig_user'],$content);
T 689         $content = str_replace('{mysql_server_ispconfig_password}',$conf['mysql']['ispconfig_password'], $content);
690         $content = str_replace('{mysql_server_database}',$conf['mysql']['database'],$content);
20218c 691         $content = str_replace('{mysql_server_host}',$conf["mysql"]["host"],$content);
99d85e 692         $content = str_replace('{server_id}',$conf["server_id"],$content);
cc3fb3 693         wf($conf["mydns"]["config_dir"].'/'.$configfile,$content);
O 694         exec('chmod 600 '.$conf["mydns"]["config_dir"].'/'.$configfile);
695         exec('chown root:root '.$conf["mydns"]["config_dir"].'/'.$configfile);
99d85e 696     
T 697     }
698     
76b6b6 699     public function configure_apache()
P 700     {    
af5c9b 701         global $conf;
T 702         
76b6b6 703         //* Create the logging directory for the vhost logfiles
P 704         exec('mkdir -p /var/log/ispconfig/httpd');
313e33 705         
871b33 706         if(is_file('/etc/suphp/suphp.conf')) {
20aa6b 707             replaceLine('/etc/suphp/suphp.conf','php=php:/usr/bin','x-httpd-suphp=php:/usr/bin/php-cgi',0);
ce6dcf 708             //replaceLine('/etc/suphp/suphp.conf','docroot=','docroot=/var/clients',0);
871b33 709         }
T 710         
98cfe5 711         if(is_file('/etc/apache2/sites-enabled/000-default')) {
bde98e 712             replaceLine('/etc/apache2/sites-available/000-default','NameVirtualHost *','NameVirtualHost *:80',1,0);
T 713             replaceLine('/etc/apache2/sites-available/000-default','<VirtualHost *>','<VirtualHost *:80>',1,0);
98cfe5 714         }
T 715         
b9dbe7 716         if(is_file('/etc/apache2/ports.conf')) {
T 717             // add a line "Listen 443" to ports conf if line does not exist
718             replaceLine('/etc/apache2/ports.conf','Listen 443','Listen 443',1);
719         }
720         
98cfe5 721         
db81c7 722         //* Copy the ISPConfig configuration include
T 723         $vhost_conf_dir = $conf['apache']['vhost_conf_dir'];
724         $vhost_conf_enabled_dir = $conf['apache']['vhost_conf_enabled_dir'];
725         
86e3bb 726         // copy('tpl/apache_ispconfig.conf.master',$vhost_conf_dir.'/ispconfig.conf');
T 727         
728         $content = rf("tpl/apache_ispconfig.conf.master");
729         $records = $this->db->queryAllRecords("SELECT * FROM server_ip WHERE server_id = ".$conf["server_id"]." AND virtualhost = 'y'");
730         if(count($records) > 0) {
731             foreach($records as $rec) {
732                 $content .= "NameVirtualHost ".$rec["ip_address"].":80\n";
733                 $content .= "NameVirtualHost ".$rec["ip_address"].":443\n";
734             }
735         }
736         $content .= "\n";
737         wf($vhost_conf_dir.'/ispconfig.conf',$content);
738         
8804ba 739         if(!@is_link($vhost_conf_enabled_dir."/000-ispconfig.conf")) {
T 740             exec("ln -s ".$vhost_conf_dir."/ispconfig.conf ".$vhost_conf_enabled_dir."/000-ispconfig.conf");
db81c7 741         }
T 742         
313e33 743     }
T 744     
7c99ef 745     public function configure_firewall()
T 746     {
747         global $conf;
748         
749         $dist_init_scripts = $conf['init_scripts'];
750           
af5c9b 751         if(is_dir("/etc/Bastille.backup")) caselog("rm -rf /etc/Bastille.backup", __FILE__, __LINE__);
dce071 752         if(is_dir("/etc/Bastille")) caselog("mv -f /etc/Bastille /etc/Bastille.backup", __FILE__, __LINE__);
7c99ef 753           @mkdir("/etc/Bastille", octdec($directory_mode));
dce071 754           if(is_dir("/etc/Bastille.backup/firewall.d")) caselog("cp -pfr /etc/Bastille.backup/firewall.d /etc/Bastille/", __FILE__, __LINE__);
T 755           caselog("cp -f tpl/bastille-firewall.cfg.master /etc/Bastille/bastille-firewall.cfg", __FILE__, __LINE__);
756           caselog("chmod 644 /etc/Bastille/bastille-firewall.cfg", __FILE__, __LINE__);
7c99ef 757           $content = rf("/etc/Bastille/bastille-firewall.cfg");
T 758           $content = str_replace("{DNS_SERVERS}", "", $content);
759
760           $tcp_public_services = '';
761           $udp_public_services = '';
762         
cd972d 763         $row = $this->db->queryOneRecord("SELECT * FROM firewall WHERE server_id = ".intval($conf['server_id']));
7c99ef 764         
T 765           if(trim($row["tcp_port"]) != '' || trim($row["udp_port"]) != ''){
766             $tcp_public_services = trim(str_replace(',',' ',$row["tcp_port"]));
767             $udp_public_services = trim(str_replace(',',' ',$row["udp_port"]));
768           } else {
e8878f 769             $tcp_public_services = '21 22 25 53 80 110 143 443 3306 8080 10000';
7c99ef 770             $udp_public_services = '53';
T 771           }
9ce725 772         
T 773         if(!stristr($tcp_public_services, $conf['apache']['vhost_port'])) {
4a5f92 774             $tcp_public_services .= ' '.intval($conf['apache']['vhost_port']);
57299d 775             if($row["tcp_port"] != '') $this->db->query("UPDATE firewall SET tcp_port = tcp_port + ',".intval($conf['apache']['vhost_port'])."' WHERE server_id = ".intval($conf['server_id']));
87fb65 776         }
T 777         
7c99ef 778           $content = str_replace("{TCP_PUBLIC_SERVICES}", $tcp_public_services, $content);
T 779           $content = str_replace("{UDP_PUBLIC_SERVICES}", $udp_public_services, $content);
780
781           wf("/etc/Bastille/bastille-firewall.cfg", $content);
782
6912b2 783           if(is_file($dist_init_scripts."/bastille-firewall")) caselog("mv -f $dist_init_scripts/bastille-firewall $dist_init_scripts/bastille-firewall.backup", __FILE__, __LINE__);
dce071 784           caselog("cp -f apps/bastille-firewall $dist_init_scripts", __FILE__, __LINE__);
T 785           caselog("chmod 700 $dist_init_scripts/bastille-firewall", __FILE__, __LINE__);
7c99ef 786
dce071 787           if(is_file("/sbin/bastille-ipchains")) caselog("mv -f /sbin/bastille-ipchains /sbin/bastille-ipchains.backup", __FILE__, __LINE__);
T 788           caselog("cp -f apps/bastille-ipchains /sbin", __FILE__, __LINE__);
789           caselog("chmod 700 /sbin/bastille-ipchains", __FILE__, __LINE__);
7c99ef 790
dce071 791           if(is_file("/sbin/bastille-netfilter")) caselog("mv -f /sbin/bastille-netfilter /sbin/bastille-netfilter.backup", __FILE__, __LINE__);
T 792           caselog("cp -f apps/bastille-netfilter /sbin", __FILE__, __LINE__);
793           caselog("chmod 700 /sbin/bastille-netfilter", __FILE__, __LINE__);
794         
795         if(!@is_dir('/var/lock/subsys')) caselog("mkdir /var/lock/subsys", __FILE__, __LINE__);
7c99ef 796
T 797           exec("which ipchains &> /dev/null", $ipchains_location, $ret_val);
dce071 798           if(!is_file("/sbin/ipchains") && !is_link("/sbin/ipchains") && $ret_val == 0) phpcaselog(@symlink(shell_exec("which ipchains"), "/sbin/ipchains"), 'create symlink', __FILE__, __LINE__);
7c99ef 799           unset($ipchains_location);
T 800           exec("which iptables &> /dev/null", $iptables_location, $ret_val);
dce071 801           if(!is_file("/sbin/iptables") && !is_link("/sbin/iptables") && $ret_val == 0) phpcaselog(@symlink(trim(shell_exec("which iptables")), "/sbin/iptables"), 'create symlink', __FILE__, __LINE__);
7c99ef 802           unset($iptables_location);
T 803
804     }
805     
b4c750 806     
76b6b6 807     public function install_ispconfig()
P 808     {
7bd4b4 809         global $conf;
T 810         
cd972d 811         $install_dir = $conf['ispconfig_install_dir'];
9200ad 812
76b6b6 813         //* Create the ISPConfig installation directory
613744 814         if(!@is_dir("$install_dir")) {
88d899 815             $command = "mkdir $install_dir";
T 816             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
817         }
9200ad 818         
76b6b6 819         //* Create a ISPConfig user and group
P 820         $command = 'groupadd ispconfig';
392450 821         if(!is_group('ispconfig')) caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
76b6b6 822         
P 823         $command = "useradd -g ispconfig -d $install_dir ispconfig";
392450 824         if(!is_user('ispconfig')) caselog($command.' &> /dev/null 2> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
76b6b6 825         
P 826         //* copy the ISPConfig interface part
827         $command = "cp -rf ../interface $install_dir";
828         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
829         
830         //* copy the ISPConfig server part
831         $command = "cp -rf ../server $install_dir";
832         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
833         
834         //* Create a symlink, so ISPConfig is accessible via web
b722a1 835         // Replaced by a separate vhost definition for port 8080
T 836         // $command = "ln -s $install_dir/interface/web/ /var/www/ispconfig";
837         // caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
76b6b6 838         
P 839         //* Create the config file for ISPConfig interface
840         $configfile = 'config.inc.php';
841         if(is_file($install_dir.'/interface/lib/'.$configfile)){
842             copy("$install_dir/interface/lib/$configfile", "$install_dir/interface/lib/$configfile~");
843         }
844         $content = rf("tpl/$configfile.master");
7bd4b4 845         $content = str_replace('{mysql_server_ispconfig_user}', $conf['mysql']['ispconfig_user'], $content);
T 846         $content = str_replace('{mysql_server_ispconfig_password}',$conf['mysql']['ispconfig_password'], $content);
847         $content = str_replace('{mysql_server_database}', $conf['mysql']['database'], $content);
848         $content = str_replace('{mysql_server_host}', $conf['mysql']['host'], $content);
12e3ba 849         
T 850         $content = str_replace('{mysql_master_server_ispconfig_user}', $conf['mysql']['master_ispconfig_user'], $content);
851         $content = str_replace('{mysql_master_server_ispconfig_password}', $conf['mysql']['master_ispconfig_password'], $content);
852         $content = str_replace('{mysql_master_server_database}', $conf['mysql']['master_database'], $content);
853         $content = str_replace('{mysql_master_server_host}', $conf['mysql']['master_host'], $content);
854         
fa7958 855         $content = str_replace('{ispconfig_log_priority}', $conf['ispconfig_log_priority'], $content);
76b6b6 856         wf("$install_dir/interface/lib/$configfile", $content);
P 857         
858         //* Create the config file for ISPConfig server
859         $configfile = 'config.inc.php';
860         if(is_file($install_dir.'/server/lib/'.$configfile)){
861             copy("$install_dir/server/lib/$configfile", "$install_dir/interface/lib/$configfile~");
862         }
863         $content = rf("tpl/$configfile.master");
7bd4b4 864         $content = str_replace('{mysql_server_ispconfig_user}', $conf['mysql']['ispconfig_user'], $content);
T 865         $content = str_replace('{mysql_server_ispconfig_password}', $conf['mysql']['ispconfig_password'], $content);
866         $content = str_replace('{mysql_server_database}', $conf['mysql']['database'], $content);
867         $content = str_replace('{mysql_server_host}', $conf['mysql']['host'], $content);
12e3ba 868         
T 869         $content = str_replace('{mysql_master_server_ispconfig_user}', $conf['mysql']['master_ispconfig_user'], $content);
870         $content = str_replace('{mysql_master_server_ispconfig_password}', $conf['mysql']['master_ispconfig_password'], $content);
871         $content = str_replace('{mysql_master_server_database}', $conf['mysql']['master_database'], $content);
872         $content = str_replace('{mysql_master_server_host}', $conf['mysql']['master_host'], $content);
873         
7bd4b4 874         $content = str_replace('{server_id}', $conf['server_id'], $content);
614365 875         $content = str_replace('{ispconfig_log_priority}', $conf['ispconfig_log_priority'], $content);
76b6b6 876         wf("$install_dir/server/lib/$configfile", $content);
P 877         
710f35 878         
1b063e 879         //* Enable the server modules and plugins.
T 880         // TODO: Implement a selector which modules and plugins shall be enabled.
881         $dir = $install_dir.'/server/mods-available/';
882         if (is_dir($dir)) {
883             if ($dh = opendir($dir)) {
884                 while (($file = readdir($dh)) !== false) {
392450 885                     if($file != '.' && $file != '..' && substr($file,-8,8) == '.inc.php') {
T 886                         include_once($install_dir.'/server/mods-available/'.$file);
887                         $module_name = substr($file,0,-8);
888                         $tmp = new $module_name;
889                         if($tmp->onInstall()) {
890                             if(!@is_link($install_dir.'/server/mods-enabled/'.$file)) @symlink($install_dir.'/server/mods-available/'.$file, $install_dir.'/server/mods-enabled/'.$file);
891                             if (strpos($file, '_core_module') !== false) {
892                                 if(!@is_link($install_dir.'/server/mods-core/'.$file)) @symlink($install_dir.'/server/mods-available/'.$file, $install_dir.'/server/mods-core/'.$file);
893                             }
0d0cd9 894                         }
392450 895                         unset($tmp);
1b063e 896                     }
T 897                 }
898                 closedir($dh);
899             }
900         }
901         
902         $dir = $install_dir.'/server/plugins-available/';
903         if (is_dir($dir)) {
904             if ($dh = opendir($dir)) {
905                 while (($file = readdir($dh)) !== false) {
392450 906                     if($file != '.' && $file != '..' && substr($file,-8,8) == '.inc.php') {
T 907                         include_once($install_dir.'/server/plugins-available/'.$file);
908                         $plugin_name = substr($file,0,-8);
909                         $tmp = new $plugin_name;
dd2bf5 910                         if(method_exists($tmp,'onInstall') && $tmp->onInstall()) {
392450 911                             if(!@is_link($install_dir.'/server/plugins-enabled/'.$file)) @symlink($install_dir.'/server/plugins-available/'.$file, $install_dir.'/server/plugins-enabled/'.$file);
T 912                             if (strpos($file, '_core_plugin') !== false) {
913                                 if(!@is_link($install_dir.'/server/plugins-core/'.$file)) @symlink($install_dir.'/server/plugins-available/'.$file, $install_dir.'/server/plugins-core/'.$file);
914                             }
0d0cd9 915                         }
392450 916                         unset($tmp);
1b063e 917                     }
T 918                 }
919                 closedir($dh);
920             }
921         }
76b6b6 922         
392450 923         // Update the server config
T 924         $mail_server_enabled = ($conf['services']['mail'])?1:0;
925         $web_server_enabled = ($conf['services']['web'])?1:0;
926         $dns_server_enabled = ($conf['services']['dns'])?1:0;
927         $file_server_enabled = ($conf['services']['file'])?1:0;
928         $db_server_enabled = ($conf['services']['db'])?1:0;
929         $vserver_server_enabled = ($conf['services']['vserver'])?1:0;
838c87 930         
T 931         
932         
933         
934         
935         
392450 936         $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']);
T 937         
938         if($conf['mysql']['master_slave_setup'] == 'y') {
939             $this->dbmaster->query($sql);
f66929 940             $this->db->query($sql);
392450 941         } else {
T 942             $this->db->query($sql);
943         }
944         
945         
76b6b6 946         //* Chmod the files
P 947         $command = "chmod -R 750 $install_dir";
948         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
949
950         //* chown the files to the ispconfig user and group
951         $command = "chown -R ispconfig:ispconfig $install_dir";
952         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
953         
710f35 954         //* Make the global language file directory group writable
f3e2f0 955         exec("chmod -R 770 $install_dir/interface/lib/lang");
710f35 956         
1e2414 957         //* Make the temp directory for language file exports writable
T 958         exec("chmod -R 770 $install_dir/interface/web/temp");
959         
710f35 960         //* Make all interface language file directories group writable
T 961         $handle = @opendir($install_dir.'/interface/web');
962         while ($file = @readdir ($handle)) { 
963                if ($file != '.' && $file != '..') {
964                 if(@is_dir($install_dir.'/interface/web'.'/'.$file.'/lib/lang')) {
965                     $handle2 = opendir($install_dir.'/interface/web'.'/'.$file.'/lib/lang');
f3e2f0 966                     chmod($install_dir.'/interface/web'.'/'.$file.'/lib/lang',0770);
710f35 967                     while ($lang_file = @readdir ($handle2)) {
T 968                         if ($lang_file != '.' && $lang_file != '..') {
f3e2f0 969                             chmod($install_dir.'/interface/web'.'/'.$file.'/lib/lang/'.$lang_file,0770);
710f35 970                         }
T 971                     }
972                 }
973             }
974         }
975         
76b6b6 976         //* make sure that the server config file (not the interface one) is only readable by the root user
P 977         exec("chmod 600 $install_dir/server/lib/$configfile");
978         exec("chown root:root $install_dir/server/lib/$configfile");
7c99ef 979         if(@is_file("$install_dir/server/lib/mysql_clientdb.conf")) {
a61345 980             exec("chmod 600 $install_dir/server/lib/mysql_clientdb.conf");
T 981             exec("chown root:root $install_dir/server/lib/mysql_clientdb.conf");
982         }
2e1086 983         
9200ad 984         // TODO: FIXME: add the www-data user to the ispconfig group. This is just for testing
T 985         // and must be fixed as this will allow the apache user to read the ispconfig files.
986         // Later this must run as own apache server or via suexec!
76b6b6 987         $command = 'adduser www-data ispconfig';
P 988         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
9200ad 989         
76b6b6 990         //* Make the shell scripts executable
P 991         $command = "chmod +x $install_dir/server/scripts/*.sh";
992         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
9200ad 993         
76b6b6 994         //* Copy the ISPConfig vhost for the controlpanel
cd972d 995         $vhost_conf_dir = $conf['apache']['vhost_conf_dir'];
T 996         $vhost_conf_enabled_dir = $conf['apache']['vhost_conf_enabled_dir'];
9b9ba4 997         
D 998         
999         // Dont just copy over the virtualhost template but add some custom settings
1000         $content = rf("tpl/apache_ispconfig.vhost.master");
cd972d 1001         $content = str_replace('{vhost_port}', $conf['apache']['vhost_port'], $content);
f6d745 1002         
T 1003         // comment out the listen directive if port is 80 or 443
1004         if($conf['apache']['vhost_port'] == 80 or $conf['apache']['vhost_port'] == 443) {
1005             $content = str_replace('{vhost_port_listen}', '#', $content);
1006         } else {
1007             $content = str_replace('{vhost_port_listen}', '', $content);
1008         }
1009         
9b9ba4 1010         wf("$vhost_conf_dir/ispconfig.vhost", $content);
D 1011         
1012         //copy('tpl/apache_ispconfig.vhost.master', "$vhost_conf_dir/ispconfig.vhost");
76b6b6 1013         //* and create the symlink
4f68a7 1014         if($this->install_ispconfig_interface == true && $this->is_update == false) {
0c78af 1015             if(@is_link("$vhost_conf_enabled_dir/ispconfig.vhost")) unlink("$vhost_conf_enabled_dir/ispconfig.vhost");
8804ba 1016             if(!@is_link("$vhost_conf_enabled_dir/000-ispconfig.vhost")) {
T 1017                 exec("ln -s $vhost_conf_dir/ispconfig.vhost $vhost_conf_enabled_dir/000-ispconfig.vhost");
ac4a37 1018             }
ea31fd 1019         }
T 1020         if(!is_file('/var/www/php-fcgi-scripts/ispconfig/.php-fcgi-starter')) {
0731a3 1021             exec('mkdir -p /var/www/php-fcgi-scripts/ispconfig');
T 1022             exec('cp tpl/apache_ispconfig_fcgi_starter.master /var/www/php-fcgi-scripts/ispconfig/.php-fcgi-starter');
1023             exec('chmod +x /var/www/php-fcgi-scripts/ispconfig/.php-fcgi-starter');
1024             exec('ln -s /usr/local/ispconfig/interface/web /var/www/ispconfig');
9787a0 1025             exec('chown -R ispconfig:ispconfig /var/www/php-fcgi-scripts/ispconfig');
0731a3 1026             
a35764 1027         }
bc41bb 1028         
66768a 1029         //* Install the update script
T 1030         if(is_file('/usr/local/bin/ispconfig_update_from_svn.sh')) unlink('/usr/local/bin/ispconfig_update_from_svn.sh');
1031         exec('chown root /usr/local/ispconfig/server/scripts/update_from_svn.sh');
1032         exec('chmod 700 /usr/local/ispconfig/server/scripts/update_from_svn.sh');
1033         exec('chown root /usr/local/ispconfig/server/scripts/update_from_tgz.sh');
1034         exec('chmod 700 /usr/local/ispconfig/server/scripts/update_from_tgz.sh');
1035         exec('chown root /usr/local/ispconfig/server/scripts/ispconfig_update.sh');
1036         exec('chmod 700 /usr/local/ispconfig/server/scripts/ispconfig_update.sh');
608a8c 1037         if(!is_link('/usr/local/bin/ispconfig_update_from_svn.sh')) exec('ln -s /usr/local/ispconfig/server/scripts/ispconfig_update.sh /usr/local/bin/ispconfig_update_from_svn.sh');
T 1038         if(!is_link('/usr/local/bin/ispconfig_update.sh')) exec('ln -s /usr/local/ispconfig/server/scripts/ispconfig_update.sh /usr/local/bin/ispconfig_update.sh');
3c5c2d 1039         
T 1040         //* Make the logs readable for the ispconfig user
1041         if(@is_file('/var/log/mail.log')) exec('chmod +r /var/log/mail.log');
1042         if(@is_file('/var/log/mail.warn')) exec('chmod +r /var/log/mail.warn');
1043         if(@is_file('/var/log/mail.err')) exec('chmod +r /var/log/mail.err');
1044         if(@is_file('/var/log/messages')) exec('chmod +r /var/log/messages');
5518df 1045         if(@is_file('/var/log/clamav/clamav.log')) exec('chmod +r /var/log/clamav/clamav.log');
T 1046         if(@is_file('/var/log/clamav/freshclam.log')) exec('chmod +r /var/log/clamav/freshclam.log');
3c5c2d 1047         
d9c8a7 1048         //* Create the ispconfig log directory
T 1049         if(!is_dir('/var/log/ispconfig')) mkdir('/var/log/ispconfig');
77d8a9 1050         if(!is_file('/var/log/ispconfig/ispconfig.log')) exec('touch /var/log/ispconfig/ispconfig.log');
d9c8a7 1051         
27c3a6 1052         exec('chown getmail /usr/local/ispconfig/server/scripts/run-getmail.sh');
T 1053         exec('chmod 744 /usr/local/ispconfig/server/scripts/run-getmail.sh');
1054         
3c5c2d 1055         
9200ad 1056     }
T 1057     
d83fcf 1058     public function configure_dbserver()
T 1059     {
1060         global $conf;
1061         
1062         //* If this server shall act as database server for client DB's, we configure this here
cd972d 1063         $install_dir = $conf['ispconfig_install_dir'];
d83fcf 1064         
T 1065         // Create a file with the database login details which 
1066         // are used to create the client databases.
1067         
1068         if(!is_dir("$install_dir/server/lib")) {
1069             $command = "mkdir $install_dir/server/lib";
1070             caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
1071         }
1072         
1073         $content = rf("tpl/mysql_clientdb.conf.master");
1074         $content = str_replace('{username}',$conf['mysql']['admin_user'],$content);
1075         $content = str_replace('{password}',$conf['mysql']['admin_password'], $content);
1076         wf("$install_dir/server/lib/mysql_clientdb.conf",$content);
1077         exec('chmod 600 '."$install_dir/server/lib/mysql_clientdb.conf");
1078         exec('chown root:root '."$install_dir/server/lib/mysql_clientdb.conf");
1079         
1080     }
1081     
76b6b6 1082     public function install_crontab()
5d54be 1083     {        
cd972d 1084         global $conf;
T 1085         
5d54be 1086         //* Root Crontab
P 1087         exec('crontab -u root -l > crontab.txt');
daff5c 1088         $existing_root_cron_jobs = file('crontab.txt');
T 1089         
79bc50 1090         // remove existing ispconfig cronjobs, in case the syntax has changed
T 1091         foreach($existing_root_cron_jobs as $key => $val) {
1092             if(stristr($val,'/usr/local/ispconfig')) unset($existing_root_cron_jobs[$key]);
1093         }
1094         
30aa08 1095         $root_cron_jobs = array(
986f1c 1096             '* * * * * /usr/local/ispconfig/server/server.sh > /dev/null 2>> /var/log/ispconfig/cron.log',
R 1097             '30 00 * * * /usr/local/ispconfig/server/cron_daily.sh > /dev/null 2>> /var/log/ispconfig/cron.log'
30aa08 1098         );
daff5c 1099         foreach($root_cron_jobs as $cron_job) {
5d54be 1100             if(!in_array($cron_job."\n", $existing_root_cron_jobs)) {
daff5c 1101                 $existing_root_cron_jobs[] = $cron_job."\n";
T 1102             }
1103         }
5d54be 1104         file_put_contents('crontab.txt', $existing_root_cron_jobs);
P 1105         exec('crontab -u root crontab.txt &> /dev/null');
daff5c 1106         unlink('crontab.txt');
T 1107         
5d54be 1108         //* Getmail crontab
a8a328 1109         if(is_user('getmail')) {
T 1110             $cf = $conf['getmail'];
1111             exec('crontab -u getmail -l > crontab.txt');
1112             $existing_cron_jobs = file('crontab.txt');
daff5c 1113         
641cb3 1114             $cron_jobs = array(
27c3a6 1115                 '*/5 * * * * /usr/local/ispconfig/server/scripts/run-getmail.sh > /dev/null 2>> /var/log/ispconfig/cron.log'
641cb3 1116             );
79bc50 1117         
a8a328 1118             // remove existing ispconfig cronjobs, in case the syntax has changed
9b30b0 1119             foreach($existing_cron_jobs as $key => $val) {
27c3a6 1120                 if(stristr($val,'getmail')) unset($existing_cron_jobs[$key]);
daff5c 1121             }
a8a328 1122         
T 1123             foreach($cron_jobs as $cron_job) {
1124                 if(!in_array($cron_job."\n", $existing_cron_jobs)) {
1125                     $existing_cron_jobs[] = $cron_job."\n";
1126                 }
1127             }
1128             file_put_contents('crontab.txt', $existing_cron_jobs);
1129             exec('crontab -u getmail crontab.txt &> /dev/null');
1130             unlink('crontab.txt');
daff5c 1131         }
37e661 1132         
T 1133         exec('touch /var/log/ispconfig/cron.log');
9e2319 1134         exec('chmod 666 /var/log/ispconfig/cron.log');
37e661 1135         
daff5c 1136     }
9200ad 1137     
T 1138 }
1139
5d54be 1140 ?>