tbrehm
2008-05-28 43ce66dc8b573dd117b5e9e47b1ade46ace52982
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;
09e141 37
P 38
32814e 39     public function __construct()
5133cc 40     {
b0a1cc 41         global $conf; //TODO: maybe $conf  should be passed to constructor
P 42         $this->conf = $conf;
5133cc 43     }
9200ad 44     
1b063e 45     //: TODO  Implement the translation function and langauge files for the installer.
32814e 46     public function lng($text)
P 47     {
ce9544 48         return $text;
9200ad 49     }
T 50     
32814e 51     public function error($msg)
P 52     {
9200ad 53         die("ERROR: ".$msg."\n");
T 54     }
55     
32814e 56     public function simple_query($query, $answers, $default)
P 57     {        
ce9544 58         $finished = false;
T 59         do {
32814e 60             $answers_str = implode(',', $answers);
239ce8 61             swrite($this->lng($query).' ('.$answers_str.') ['.$default.']: ');
ce9544 62             $input = sread();
T 63             
32814e 64             //* Stop the installation
ce9544 65             if($input == 'quit') {
32814e 66                 swriteln($this->lng("Installation terminated by user.\n"));
239ce8 67                 die();
ce9544 68             }
T 69             
32814e 70             //* Select the default
ce9544 71             if($input == '') {
T 72                 $answer = $default;
73                 $finished = true;
74             }
75             
32814e 76             //* Set answer id valid
P 77             if(in_array($input, $answers)) {
ce9544 78                 $answer = $input;
T 79                 $finished = true;
80             }
81             
82         } while ($finished == false);
239ce8 83         swriteln();
ce9544 84         return $answer;
T 85     }
86     
32814e 87     public function free_query($query,$default)
P 88     {        
239ce8 89         swrite($this->lng($query).' ['.$default.']: ');
ce9544 90         $input = sread();
T 91             
32814e 92         //* Stop the installation
ce9544 93         if($input == 'quit') {
32814e 94             swriteln($this->lng("Installation terminated by user.\n"));
P 95             die();
ce9544 96         }
T 97             
32814e 98         $answer =  ($input == '') ? $default : $input;
239ce8 99         swriteln();
ce9544 100         return $answer;
T 101     }
102     
1b063e 103     /*
32814e 104     // TODO: this function is not used atmo I think - pedro
P 105     function request_language(){
9200ad 106         
T 107         swriteln(lng('Enter your language'));
108         swriteln(lng('de, en'));
109         
110     }
1b063e 111     */
9200ad 112     
facccb 113     /** Create the database for ISPConfig */ 
P 114     public function configure_database()
115     {
9200ad 116         global $conf;
facccb 117         $cf = $conf['mysql']; // make $conf['mysql'] more accessible
P 118         //** Create the database
119         if(!$this->db->query('CREATE DATABASE IF NOT EXISTS '.$cf['database'])) {
120             $this->error('Unable to create MySQL database: '.$cf['database'].'.');
9200ad 121         }
T 122         
facccb 123         //* Create the ISPConfig database user
P 124         $query = 'GRANT SELECT, INSERT, UPDATE, DELETE ON '.$cf['database'].".* "
125                 ."TO '".$cf['ispconfig_user']."'@'".$cf['host']."' "
126                 ."IDENTIFIED BY '".$cf['ispconfig_password']."';";
127         if(!$this->db->query($query)) {
128             $this->error('Unable to create database user: '.$cf['ispconfig_user']);
9200ad 129         }
T 130         
facccb 131         //* Reload database privelages
9200ad 132         $this->db->query('FLUSH PRIVILEGES;');
T 133         
facccb 134         //* Set the database name in the DB library
P 135         $this->db->dbName = $cf['database'];
9200ad 136         
facccb 137         //* Load the database dump into the database, if database contains no tables
9200ad 138         $db_tables = $this->db->getTables();
T 139         if(count($db_tables) > 0) {
66b4f9 140             $this->error('Stopped: Database already contains some tables.');
9200ad 141         } else {
facccb 142             if($cf['admin_password'] == '') {
P 143                 caselog("mysql -h '".$cf['host']."' -u '".$cf['admin_user']."' '".$cf['database']."' < 'sql/ispconfig3.sql' &> /dev/null", 
66b4f9 144                         __FILE__, __LINE__, 'read in ispconfig3.sql', 'could not read in ispconfig3.sql');
9200ad 145             } else {
facccb 146                 caselog("mysql -h '".$cf['host']."' -u '".$cf['admin_user']."' -p'".$cf['admin_password']."' '".$cf['database']."' < 'sql/ispconfig3.sql' &> /dev/null", 
66b4f9 147                         __FILE__, __LINE__, 'read in ispconfig3.sql', 'could not read in ispconfig3.sql');
9200ad 148             }
T 149             $db_tables = $this->db->getTables();
150             if(count($db_tables) == 0) {
151                 $this->error('Unable to load SQL-Dump into database table.');
152             }
153         }
154     }
155     
94411b 156     //** Create a recors in the
T 157     public function add_database_server_record() {
158         
d4c9b3 159         $server_ini_content = rf("tpl/server.ini.master");
T 160         $server_ini_content = addslashes($server_ini_content);
94411b 161         
3341c7 162         $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', 'Server', 1, 1, 1, 1, 1, 1, '$server_ini_content', 0, 1);";
94411b 163         $this->db->query($sql);
T 164         $this->conf['server_id'] = $this->db->insertID();
165     }
166     
b41692 167
09e141 168     //** writes postfix configuration files
b41692 169     private function process_postfix_config($configfile)
P 170     {
cc3fb3 171         $config_dir = $this->conf['postfix']['config_dir'].'/';
b41692 172         $full_file_name = $config_dir.$configfile; 
P 173         //* Backup exiting file
174         if(is_file($full_file_name)){
175             copy($full_file_name, $config_dir.$configfile.'~');
176         }
177         $content = rf('tpl/'.$configfile.'.master');
178         $content = str_replace('{mysql_server_ispconfig_user}', $this->conf['mysql']['ispconfig_user'], $content);
179         $content = str_replace('{mysql_server_ispconfig_password}', $this->conf['mysql']['ispconfig_password'], $content);
180         $content = str_replace('{mysql_server_database}', $this->conf['mysql']['database'], $content);
181         $content = str_replace('{mysql_server_ip}', $this->conf['mysql']['ip'], $content);
182         $content = str_replace('{server_id}', $this->conf['server_id'], $content);
183         wf($full_file_name, $content);
184     }
185
09e141 186
b41692 187     public function configure_postfix($options = '')
P 188     {
cc3fb3 189         $cf = $this->conf['postfix'];
77ab0a 190         $config_dir = $cf['config_dir'];
P 191         
b41692 192         if(!is_dir($config_dir)){
P 193             $this->error("The postfix configuration directory '$config_dir' does not exist.");
194         }
195         
196         //* mysql-virtual_domains.cf
197         $this->process_postfix_config('mysql-virtual_domains.cf');
198
199         //* mysql-virtual_forwardings.cf
200         $this->process_postfix_config('mysql-virtual_forwardings.cf');
201
202         //* mysql-virtual_mailboxes.cf
203         $this->process_postfix_config('mysql-virtual_mailboxes.cf');
204
205         //* mysql-virtual_email2email.cf
206         $this->process_postfix_config('mysql-virtual_email2email.cf');
207
208         //* mysql-virtual_transports.cf
209         $this->process_postfix_config('mysql-virtual_transports.cf');
210
211         //* mysql-virtual_recipient.cf
212         $this->process_postfix_config('mysql-virtual_recipient.cf');
213
214         //* mysql-virtual_sender.cf
215         $this->process_postfix_config('mysql-virtual_sender.cf');
216
217         //* mysql-virtual_client.cf
218         $this->process_postfix_config('mysql-virtual_client.cf');
5bf366 219         
T 220         //* mysql-virtual_relaydomains.cf
221         $this->process_postfix_config('mysql-virtual_relaydomains.cf');
b41692 222
P 223         //* Changing mode and group of the new created config files.
77ab0a 224         caselog('chmod o= '.$config_dir.'/mysql-virtual_*.cf* &> /dev/null',
P 225                  __FILE__, __LINE__, 'chmod on mysql-virtual_*.cf*', 'chmod on mysql-virtual_*.cf* failed');
226         caselog('chgrp '.$cf['group'].' '.$config_dir.'/mysql-virtual_*.cf* &> /dev/null', 
227                 __FILE__, __LINE__, 'chgrp on mysql-virtual_*.cf*', 'chgrp on mysql-virtual_*.cf* failed');
9200ad 228         
77ab0a 229         //* Creating virtual mail user and group
P 230         $command = 'groupadd -g '.$cf['vmail_groupid'].' '.$cf['vmail_groupname'];
231         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
232
233         $command = 'useradd -g '.$cf['vmail_groupname'].' -u '.$cf['vmail_userid'].' '.$cf['vmail_username'].' -d '.$cf['vmail_mailbox_base'].' -m';
234         caselog("$command &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");        
9200ad 235
T 236         $postconf_commands = array (
77ab0a 237             'myhostname = '.$this->conf['hostname'],
P 238             'mydestination = '.$this->conf['hostname'].', localhost, localhost.localdomain',
9200ad 239             'mynetworks = 127.0.0.0/8',
T 240             'virtual_alias_domains =',
438d9f 241             'virtual_alias_maps = proxy:mysql:'.$config_dir.'/mysql-virtual_forwardings.cf, mysql:'.$config_dir.'/mysql-virtual_email2email.cf',
P 242             'virtual_mailbox_domains = proxy:mysql:'.$config_dir.'/mysql-virtual_domains.cf',
77ab0a 243             'virtual_mailbox_maps = proxy:mysql:'.$config_dir.'/mysql-virtual_mailboxes.cf',
P 244             'virtual_mailbox_base = '.$cf['vmail_mailbox_base'],
245             'virtual_uid_maps = static:'.$cf['vmail_userid'],
246             'virtual_gid_maps = static:'.$cf['vmail_groupid'],
9200ad 247             'smtpd_sasl_auth_enable = yes',
T 248             'broken_sasl_auth_clients = yes',
438d9f 249             'smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, check_recipient_access mysql:'.$config_dir.'/mysql-virtual_recipient.cf, reject_unauth_destination',
9200ad 250             'smtpd_use_tls = yes',
438d9f 251             'smtpd_tls_cert_file = '.$config_dir.'/smtpd.cert',
P 252             'smtpd_tls_key_file = '.$config_dir.'/smtpd.key',
253             'transport_maps = proxy:mysql:'.$config_dir.'/mysql-virtual_transports.cf',
5bf366 254             'relay_domains = mysql:'.$config_dir.'/mysql-virtual_relaydomains.cf',
9200ad 255             'virtual_create_maildirsize = yes',
T 256             'virtual_mailbox_extended = yes',
438d9f 257             'virtual_mailbox_limit_maps = proxy:mysql:'.$config_dir.'/mysql-virtual_mailbox_limit_maps.cf',
9200ad 258             'virtual_mailbox_limit_override = yes',
T 259             'virtual_maildir_limit_message = "The user you are trying to reach is over quota."',
260             'virtual_overquota_bounce = yes',
261             '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 262             'smtpd_sender_restrictions = check_sender_access mysql:'.$config_dir.'/mysql-virtual_sender.cf',
P 263             'smtpd_client_restrictions = check_client_access mysql:'.$config_dir.'/mysql-virtual_client.cf',
9200ad 264             'maildrop_destination_concurrency_limit = 1',
T 265             'maildrop_destination_recipient_limit   = 1',
54fb59 266             'virtual_transport = maildrop',
438d9f 267             'header_checks = regexp:'.$config_dir.'/header_checks',
P 268             'mime_header_checks = regexp:'.$config_dir.'/mime_header_checks',
269             'nested_header_checks = regexp:'.$config_dir.'/nested_header_checks',
270             'body_checks = regexp:'.$config_dir.'/body_checks'
9200ad 271         );
T 272         
438d9f 273         //* Create the header and body check files
P 274         touch($config_dir.'/header_checks');
275         touch($config_dir.'/mime_header_checks');
276         touch($config_dir.'/nested_header_checks');
277         touch($config_dir.'/body_checks');
54fb59 278         
T 279         
438d9f 280         //* Make a backup copy of the main.cf file
P 281         copy($config_dir.'/main.cf', $config_dir.'/main.cf~');
9200ad 282         
438d9f 283         //* Executing the postconf commands
9200ad 284         foreach($postconf_commands as $cmd) {
T 285             $command = "postconf -e '$cmd'";
438d9f 286             caselog($command." &> /dev/null", __FILE__, __LINE__, 'EXECUTED: '.$command, 'Failed to execute the command '.$command);
9200ad 287         }
T 288         
289         // TODO: Change the master.cf file
290         /*
291         Add:
77ab0a 292         maildrop  unix  -       n       n       -       -       pipe
P 293         flags=R user=vmail argv=/usr/bin/maildrop -d ${recipient} ${extension} ${recipient} ${user} ${nexthop} ${sender}
9200ad 294         */
fb7155 295         if(!stristr($options,'dont-create-certs')) {
438d9f 296             //* Create the SSL certificate
P 297             $command = 'cd '.$config_dir.'; '
298                       .'openssl req -new -outform PEM -out smtpd.cert -newkey rsa:2048 -nodes -keyout '
299                       .'smtpd.key -keyform PEM -days 365 -x509';
c58e3f 300             exec($command);
9200ad 301         
438d9f 302             $command = 'chmod o= '.$config_dir.'/smtpd.key';
P 303             caselog($command.' &> /dev/null', __FILE__, __LINE__, 'EXECUTED: '.$command, 'Failed to execute the command '.$command);
c58e3f 304         }
9200ad 305         
77ab0a 306         //** We have to change the permissions of the courier authdaemon directory to make it accessible for maildrop.
438d9f 307         $command = 'chmod 755  /var/run/courier/authdaemon/';
P 308         caselog($command.' &> /dev/null', __FILE__, __LINE__, 'EXECUTED: '.$command, 'Failed to execute the command '.$command);
9200ad 309         
438d9f 310         //* Changing maildrop lines in posfix master.cf
P 311         if(is_file($config_dir.'/master.cf')){
312             copy($config_dir.'/master.cf', $config_dir.'/master.cf~');
313         }
314         if(is_file($config_dir.'/master.cf~')){
315             exec('chmod 400 '.$config_dir.'/master.cf~');
316         }
317         $configfile = $config_dir.'/master.cf';
20e642 318         $content = rf($configfile);
77ab0a 319         $content = str_replace('  flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}', 
P 320                    '  flags=R user='.$cf['vmail_username'].' argv=/usr/bin/maildrop -d ${recipient} ${extension} ${recipient} ${user} ${nexthop} ${sender}',
321                      $content);
322         wf($configfile, $content);
20e642 323         
438d9f 324         //* Writing the Maildrop mailfilter file
9200ad 325         $configfile = 'mailfilter';
77ab0a 326         if(is_file($cf['vmail_mailbox_base'].'/.'.$configfile)){
P 327             copy($cf['vmail_mailbox_base'].'/.'.$configfile, $cf['vmail_mailbox_base'].'/.'.$configfile.'~');
328         }
329         $content = rf("tpl/$configfile.master");
330         $content = str_replace('{dist_postfix_vmail_mailbox_base}', $cf['vmail_mailbox_base'], $content);
331         wf($cf['vmail_mailbox_base'].'/.'.$configfile, $content);
9200ad 332         
77ab0a 333         //* Create the directory for the custom mailfilters
P 334         $command = 'mkdir '.$cf['vmail_mailbox_base'].'/mailfilters';
335         caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
9200ad 336         
77ab0a 337         //* Chmod and chown the .mailfilter file
P 338         $command = 'chown -R '.$cf['vmail_username'].':'.$cf['vmail_groupname'].' '.$cf['vmail_mailbox_base'].'/.mailfilter';
339         caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
9200ad 340         
77ab0a 341         $command = 'chmod -R 600 '.$cf['vmail_mailbox_base'].'/.mailfilter';
P 342         caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
9200ad 343         
T 344     }
345     
346     function configure_saslauthd() {
347         global $conf;
348         
20e642 349     
9200ad 350         $configfile = 'sasl_smtpd.conf';
cc3fb3 351         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 352         if(is_file($conf["postfix"]["config_dir"].'/sasl/smtpd.conf~')) exec('chmod 400 '.$conf["postfix"]["config_dir"].'/sasl/smtpd.conf~');
9200ad 353         $content = rf("tpl/".$configfile.".master");
09e141 354         $content = str_replace('{mysql_server_ispconfig_user}',$this->conf['mysql']['ispconfig_user'],$content);
P 355         $content = str_replace('{mysql_server_ispconfig_password}',$this->conf['mysql']['ispconfig_password'], $content);
356         $content = str_replace('{mysql_server_database}',$this->conf['mysql']['database'],$content);
357         $content = str_replace('{mysql_server_ip}',$this->conf['mysql']['ip'],$content);
cc3fb3 358         wf($conf["postfix"]["config_dir"].'/sasl/smtpd.conf',$content);
9200ad 359         
T 360         // TODO: Chmod and chown on the config file
20e642 361         
T 362         
363         
364         // Create the spool directory
20218c 365         exec('mkdir -p /var/spool/postfix/var/run/saslauthd');
20e642 366         
T 367         // Edit the file /etc/default/saslauthd
cc3fb3 368         $configfile = $conf["saslauthd"]["config"];
20e642 369         if(is_file($configfile)) copy($configfile,$configfile.'~');
e32699 370         if(is_file($configfile.'~')) exec('chmod 400 '.$configfile.'~');
20e642 371         $content = rf($configfile);
T 372         $content = str_replace('START=no','START=yes',$content);
373         $content = str_replace('OPTIONS="-c"','OPTIONS="-m /var/spool/postfix/var/run/saslauthd -r"',$content);
374         wf($configfile,$content);
375         
20218c 376         // Edit the file /etc/init.d/saslauthd
cc3fb3 377         $configfile = $conf["init_scripts"].'/'.$conf["saslauthd"]["init_script"];
20e642 378         $content = rf($configfile);
T 379         $content = str_replace('PIDFILE=$RUN_DIR/saslauthd.pid','PIDFILE="/var/spool/postfix/var/run/${NAME}/saslauthd.pid"',$content);
380         wf($configfile,$content);
9200ad 381         
T 382         
383     }
384     
09e141 385     public function configure_pam()
P 386     {
cc3fb3 387         $pam = $this->conf['pam'];
09e141 388         //* configure pam for SMTP authentication agains the ispconfig database
9200ad 389         $configfile = 'pamd_smtp';
09e141 390         if(is_file("$pam/smtp"))    copy("$pam/smtp", "$pam/smtp~");
P 391         if(is_file("$pam/smtp~"))   exec("chmod 400 $pam/smtp~");
392
393         $content = rf("tpl/$configfile.master");
394         $content = str_replace('{mysql_server_ispconfig_user}', $this->conf['mysql']['ispconfig_user'], $content);
395         $content = str_replace('{mysql_server_ispconfig_password}', $this->conf['mysql']['ispconfig_password'], $content);
396         $content = str_replace('{mysql_server_database}', $this->conf['mysql']['database'], $content);
397         $content = str_replace('{mysql_server_ip}', $this->conf['mysql']['ip'], $content);
398         wf("$pam/smtp", $content);
399         exec("chmod 660 $pam/smtp");
400         exec("chown daemon:daemon $pam/smtp");
9200ad 401     
T 402     }
403     
09e141 404     public function configure_courier()
P 405     {
cc3fb3 406         $config_dir = $this->conf['courier']['config_dir'];
09e141 407         //* authmysqlrc
9200ad 408         $configfile = 'authmysqlrc';
09e141 409         if(is_file("$config_dir/$configfile")){
P 410             copy("$config_dir/$configfile", "$config_dir/$configfile~");
411         }
412         exec("chmod 400 $config_dir/$configfile~");
413         $content = rf("tpl/$configfile.master");
414         $content = str_replace('{mysql_server_ispconfig_user}',$this->conf['mysql']['ispconfig_user'],$content);
415         $content = str_replace('{mysql_server_ispconfig_password}',$this->conf['mysql']['ispconfig_password'], $content);
416         $content = str_replace('{mysql_server_database}',$this->conf['mysql']['database'],$content);
417         $content = str_replace('{mysql_server_host}',$this->conf['mysql']['host'],$content);
418         wf("$config_dir/$configfile", $content);
9200ad 419         
09e141 420         exec("chmod 660 $config_dir/$configfile");
P 421         exec("chown daemon:daemon $config_dir/$configfile");
20e642 422         
09e141 423         //* authdaemonrc
cc3fb3 424         $configfile = $this->conf['courier']['config_dir'].'/authdaemonrc';
09e141 425         if(is_file($configfile)){
P 426             copy($configfile, $configfile.'~');
427         }
428         if(is_file($configfile.'~')){
429             exec('chmod 400 '.$configfile.'~');
430         }
20e642 431         $content = rf($configfile);
09e141 432         $content = str_replace('authmodulelist="authpam"', 'authmodulelist="authmysql"', $content);
P 433         wf($configfile, $content);
9200ad 434     }
T 435     
436     function configure_amavis() {
437         global $conf;
438         
439         // amavisd user config file
440         $configfile = 'amavisd_user_config';
cc3fb3 441         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 442         if(is_file($conf["amavis"]["config_dir"].'/conf.d/50-user~')) exec('chmod 400 '.$conf["amavis"]["config_dir"].'/conf.d/50-user~');
9200ad 443         $content = rf("tpl/".$configfile.".master");
09e141 444         $content = str_replace('{mysql_server_ispconfig_user}',$this->conf['mysql']['ispconfig_user'],$content);
P 445         $content = str_replace('{mysql_server_ispconfig_password}',$this->conf['mysql']['ispconfig_password'], $content);
446         $content = str_replace('{mysql_server_database}',$this->conf['mysql']['database'],$content);
20218c 447         $content = str_replace('{mysql_server_port}',$conf["mysql"]["port"],$content);
09e141 448         $content = str_replace('{mysql_server_ip}',$this->conf['mysql']['ip'],$content);
cc3fb3 449         wf($conf["amavis"]["config_dir"].'/conf.d/50-user',$content);
9200ad 450         
T 451         // TODO: chmod and chown on the config file
452         
453         
454         // Adding the amavisd commands to the postfix configuration
455         $postconf_commands = array (
456             'content_filter = amavis:[127.0.0.1]:10024',
457             'receive_override_options = no_address_mappings'
458         );
459         
460         // Make a backup copy of the main.cf file
cc3fb3 461         copy($conf["postfix"]["config_dir"].'/main.cf',$conf["postfix"]["config_dir"].'/main.cf~2');
9200ad 462         
T 463         // Executing the postconf commands
464         foreach($postconf_commands as $cmd) {
465             $command = "postconf -e '$cmd'";
76b6b6 466             caselog($command." &> /dev/null", __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
9200ad 467         }
T 468         
469         // Append the configuration for amavisd to the master.cf file
cc3fb3 470         if(is_file($conf["postfix"]["config_dir"].'/master.cf')) copy($conf["postfix"]["config_dir"].'/master.cf',$conf["postfix"]["config_dir"].'/master.cf~');
9200ad 471         $content = rf("tpl/master_cf_amavis.master");
T 472         // Only add the content if we had not addded it before
473         if(!stristr("127.0.0.1:10025 inet n - - - - smtpd",$content)) {
cc3fb3 474             af($conf["postfix"]["config_dir"].'/master.cf',$content);
9200ad 475         }
T 476         
477         // Add the clamav user to the amavis group
478         exec('adduser clamav amavis');
479         
480         
481     }
482     
77ab0a 483     public function configure_spamassassin()
P 484     {
09e141 485         //* Enable spamasasssin on debian and ubuntu
9200ad 486         $configfile = '/etc/default/spamassassin';
77ab0a 487         if(is_file($configfile)){
09e141 488             copy($configfile, $configfile.'~');
77ab0a 489         }
9200ad 490         $content = rf($configfile);
77ab0a 491         $content = str_replace('ENABLED=0', 'ENABLED=1', $content);
P 492         wf($configfile, $content);
9200ad 493     }
T 494     
09e141 495     public function configure_getmail()
P 496     {
cc3fb3 497         $config_dir = $this->conf['getmail']['config_dir'];
03ade5 498         
T 499         if(!is_dir($config_dir)) exec("mkdir -p ".escapeshellcmd($config_dir));
09e141 500
P 501         $command = "useradd -d $config_dir getmail";
502         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
ba747c 503         
09e141 504         $command = "chown -R getmail $config_dir";
P 505         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
3edf9d 506         
09e141 507         $command = "chmod -R 700 $config_dir";
P 508         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
ba747c 509     }
T 510     
9200ad 511     
09e141 512     public function configure_pureftpd()
P 513     {
cc3fb3 514         $config_dir = $this->conf['pureftpd']['config_dir'];
09e141 515
P 516         //* configure pam for SMTP authentication agains the ispconfig database
20218c 517         $configfile = 'db/mysql.conf';
09e141 518         if(is_file("$config_dir/$configfile")){
P 519             copy("$config_dir/$configfile", "$config_dir/$configfile~");
520         }
521         if(is_file("$config_dir/$configfile~")){
522             exec("chmod 400 $config_dir/$configfile~");
523         }
524         $content = rf('tpl/pureftpd_mysql.conf.master');
525         $content = str_replace('{mysql_server_ispconfig_user}', $this->conf["mysql"]["ispconfig_user"], $content);
526         $content = str_replace('{mysql_server_ispconfig_password}', $this->conf["mysql"]["ispconfig_password"], $content);
527         $content = str_replace('{mysql_server_database}', $this->conf["mysql"]["database"], $content);
528         $content = str_replace('{mysql_server_ip}', $this->conf["mysql"]["ip"], $content);
529         $content = str_replace('{server_id}', $this->conf["server_id"], $content);
530         wf("$config_dir/$configfile", $content);
531         exec("chmod 600 $config_dir/$configfile");
532         exec("chown root:root $config_dir/$configfile");
533         // **enable chrooting
af8f1b 534         //exec('mkdir -p '.$config_dir.'/conf/ChrootEveryone');
09e141 535         exec('echo "yes" > '.$config_dir.'/conf/ChrootEveryone');
b4c750 536     }
T 537     
09e141 538     public function configure_mydns()
P 539     {
99d85e 540         global $conf;
T 541         
542         // configure pam for SMTP authentication agains the ispconfig database
543         $configfile = 'mydns.conf';
cc3fb3 544         if(is_file($conf["mydns"]["config_dir"].'/'.$configfile)) copy($conf["mydns"]["config_dir"].'/'.$configfile,$conf["mydns"]["config_dir"].'/'.$configfile.'~');
O 545         if(is_file($conf["mydns"]["config_dir"].'/'.$configfile.'~')) exec('chmod 400 '.$conf["mydns"]["config_dir"].'/'.$configfile.'~');
99d85e 546         $content = rf("tpl/".$configfile.".master");
09e141 547         $content = str_replace('{mysql_server_ispconfig_user}',$this->conf['mysql']['ispconfig_user'],$content);
P 548         $content = str_replace('{mysql_server_ispconfig_password}',$this->conf['mysql']['ispconfig_password'], $content);
549         $content = str_replace('{mysql_server_database}',$this->conf['mysql']['database'],$content);
20218c 550         $content = str_replace('{mysql_server_host}',$conf["mysql"]["host"],$content);
99d85e 551         $content = str_replace('{server_id}',$conf["server_id"],$content);
cc3fb3 552         wf($conf["mydns"]["config_dir"].'/'.$configfile,$content);
O 553         exec('chmod 600 '.$conf["mydns"]["config_dir"].'/'.$configfile);
554         exec('chown root:root '.$conf["mydns"]["config_dir"].'/'.$configfile);
99d85e 555     
T 556     }
557     
76b6b6 558     public function configure_apache()
P 559     {    
560         //* Create the logging directory for the vhost logfiles
561         exec('mkdir -p /var/log/ispconfig/httpd');
313e33 562         
T 563     }
564     
b4c750 565     
76b6b6 566     public function install_ispconfig()
P 567     {
568         $install_dir = $this->conf['ispconfig_install_dir'];
9200ad 569
76b6b6 570         //* Create the ISPConfig installation directory
P 571         $command = "mkdir $install_dir";
572         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
9200ad 573         
76b6b6 574         //* Create a ISPConfig user and group
P 575         $command = 'groupadd ispconfig';
576         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
577         
578         $command = "useradd -g ispconfig -d $install_dir ispconfig";
579         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
580         
581         //* copy the ISPConfig interface part
582         $command = "cp -rf ../interface $install_dir";
583         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
584         
585         //* copy the ISPConfig server part
586         $command = "cp -rf ../server $install_dir";
587         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
588         
589         //* Create a symlink, so ISPConfig is accessible via web
b722a1 590         // Replaced by a separate vhost definition for port 8080
T 591         // $command = "ln -s $install_dir/interface/web/ /var/www/ispconfig";
592         // caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
76b6b6 593         
P 594         //* Create the config file for ISPConfig interface
595         $configfile = 'config.inc.php';
596         if(is_file($install_dir.'/interface/lib/'.$configfile)){
597             copy("$install_dir/interface/lib/$configfile", "$install_dir/interface/lib/$configfile~");
598         }
599         $content = rf("tpl/$configfile.master");
600         $content = str_replace('{mysql_server_ispconfig_user}', $this->conf['mysql']['ispconfig_user'], $content);
601         $content = str_replace('{mysql_server_ispconfig_password}',$this->conf['mysql']['ispconfig_password'], $content);
602         $content = str_replace('{mysql_server_database}', $this->conf['mysql']['database'], $content);
603         $content = str_replace('{mysql_server_host}', $this->conf['mysql']['host'], $content);
604         wf("$install_dir/interface/lib/$configfile", $content);
605         
606         //* Create the config file for ISPConfig server
607         $configfile = 'config.inc.php';
608         if(is_file($install_dir.'/server/lib/'.$configfile)){
609             copy("$install_dir/server/lib/$configfile", "$install_dir/interface/lib/$configfile~");
610         }
611         $content = rf("tpl/$configfile.master");
612         $content = str_replace('{mysql_server_ispconfig_user}', $this->conf['mysql']['ispconfig_user'], $content);
613         $content = str_replace('{mysql_server_ispconfig_password}', $this->conf['mysql']['ispconfig_password'], $content);
614         $content = str_replace('{mysql_server_database}', $this->conf['mysql']['database'], $content);
615         $content = str_replace('{mysql_server_host}', $this->conf['mysql']['host'], $content);
616         $content = str_replace('{server_id}', $this->conf['server_id'], $content);
617         wf("$install_dir/server/lib/$configfile", $content);
618         
710f35 619         
1b063e 620         //* Enable the server modules and plugins.
T 621         // TODO: Implement a selector which modules and plugins shall be enabled.
622         $dir = $install_dir.'/server/mods-available/';
623         if (is_dir($dir)) {
624             if ($dh = opendir($dir)) {
625                 while (($file = readdir($dh)) !== false) {
626                     if($file != '.' && $file != '..') {
7ea3df 627                         if(!is_link($install_dir.'/server/mods-enabled/'.$file)) symlink($install_dir.'/server/mods-available/'.$file, $install_dir.'/server/mods-enabled/'.$file);
1b063e 628                     }
T 629                 }
630                 closedir($dh);
631             }
632         }
633         
634         $dir = $install_dir.'/server/plugins-available/';
635         if (is_dir($dir)) {
636             if ($dh = opendir($dir)) {
637                 while (($file = readdir($dh)) !== false) {
638                     if($file != '.' && $file != '..') {
4585cf 639                         if(!is_link($install_dir.'/server/plugins-enabled/'.$file)) symlink($install_dir.'/server/plugins-available/'.$file, $install_dir.'/server/plugins-enabled/'.$file);
1b063e 640                     }
T 641                 }
642                 closedir($dh);
643             }
644         }
76b6b6 645         
P 646         //* Chmod the files
647         $command = "chmod -R 750 $install_dir";
648         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
649
650         //* chown the files to the ispconfig user and group
651         $command = "chown -R ispconfig:ispconfig $install_dir";
652         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
653         
710f35 654         //* Make the global language file directory group writable
f3e2f0 655         exec("chmod -R 770 $install_dir/interface/lib/lang");
710f35 656         
1e2414 657         //* Make the temp directory for language file exports writable
T 658         exec("chmod -R 770 $install_dir/interface/web/temp");
659         
710f35 660         //* Make all interface language file directories group writable
T 661         $handle = @opendir($install_dir.'/interface/web');
662         while ($file = @readdir ($handle)) { 
663                if ($file != '.' && $file != '..') {
664                 if(@is_dir($install_dir.'/interface/web'.'/'.$file.'/lib/lang')) {
665                     $handle2 = opendir($install_dir.'/interface/web'.'/'.$file.'/lib/lang');
f3e2f0 666                     chmod($install_dir.'/interface/web'.'/'.$file.'/lib/lang',0770);
710f35 667                     while ($lang_file = @readdir ($handle2)) {
T 668                         if ($lang_file != '.' && $lang_file != '..') {
f3e2f0 669                             chmod($install_dir.'/interface/web'.'/'.$file.'/lib/lang/'.$lang_file,0770);
710f35 670                         }
T 671                     }
672                 }
673             }
674         }
675         
76b6b6 676         //* make sure that the server config file (not the interface one) is only readable by the root user
P 677         exec("chmod 600 $install_dir/server/lib/$configfile");
678         exec("chown root:root $install_dir/server/lib/$configfile");
2e1086 679         
9200ad 680         // TODO: FIXME: add the www-data user to the ispconfig group. This is just for testing
T 681         // and must be fixed as this will allow the apache user to read the ispconfig files.
682         // Later this must run as own apache server or via suexec!
76b6b6 683         $command = 'adduser www-data ispconfig';
P 684         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
9200ad 685         
76b6b6 686         //* Make the shell scripts executable
P 687         $command = "chmod +x $install_dir/server/scripts/*.sh";
688         caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
9200ad 689         
76b6b6 690         //* Copy the ISPConfig vhost for the controlpanel
1b063e 691         // TODO: These are missing! should they be "vhost_dist_*_dir" ?
cc3fb3 692         $vhost_conf_dir = $this->conf['apache']['vhost_conf_dir'];
O 693         $vhost_conf_enabled_dir = $this->conf['apache']['vhost_conf_enabled_dir'];
76b6b6 694         copy('tpl/apache_ispconfig.vhost.master', "$vhost_conf_dir/ispconfig.vhost");
P 695         //* and create the symlink
696         if(!is_link("$vhost_conf_enabled_dir/ispconfig.vhost")) {
697             exec("ln -s $vhost_conf_dir/ispconfig.vhost $vhost_conf_enabled_dir/ispconfig.vhost");
a35764 698         }
bc41bb 699         
T 700         // Make the Clamav log files readable by ISPConfig
701         exec('chmod +r /var/log/clamav/clamav.log');
702         exec('chmod +r /var/log/clamav/freshclam.log');
83994b 703         
T 704         //* Install the SVN update script
705         exec('cp ../helper_scripts/update_from_svn.sh /usr/local/bin/ispconfig_update_from_svn.sh');
706         exec('chown root /usr/local/bin/ispconfig_update_from_svn.sh');
707         exec('chmod 700 /usr/local/bin/ispconfig_update_from_svn.sh');
708         
9200ad 709     }
T 710     
76b6b6 711     public function install_crontab()
5d54be 712     {        
P 713         //* Root Crontab
714         exec('crontab -u root -l > crontab.txt');
daff5c 715         $existing_root_cron_jobs = file('crontab.txt');
T 716         
30aa08 717         $root_cron_jobs = array(
T 718             '* * * * * /usr/local/ispconfig/server/server.sh &> /dev/null',
719             '30 00 * * * /usr/local/ispconfig/server/cron_daily.sh &> /dev/null'
720         );
daff5c 721         foreach($root_cron_jobs as $cron_job) {
5d54be 722             if(!in_array($cron_job."\n", $existing_root_cron_jobs)) {
daff5c 723                 $existing_root_cron_jobs[] = $cron_job."\n";
T 724             }
725         }
5d54be 726         file_put_contents('crontab.txt', $existing_root_cron_jobs);
P 727         exec('crontab -u root crontab.txt &> /dev/null');
daff5c 728         unlink('crontab.txt');
T 729         
5d54be 730         //* Getmail crontab
cc3fb3 731         $cf = $this->conf['getmail'];
5d54be 732         exec('crontab -u getmail -l > crontab.txt');
daff5c 733         $existing_cron_jobs = file('crontab.txt');
T 734         
5d54be 735         $cron_jobs = array('*/5 * * * * '.$cf['program'].' -g '.$cf['config_dir'].' -r '.$cf['config_dir'].'/*.conf &> /dev/null');
daff5c 736         foreach($cron_jobs as $cron_job) {
5d54be 737             if(!in_array($cron_job."\n", $existing_cron_jobs)) {
daff5c 738                 $existing_cron_jobs[] = $cron_job."\n";
T 739             }
740         }
5d54be 741         file_put_contents('crontab.txt', $existing_cron_jobs);
P 742         exec('crontab -u getmail crontab.txt &> /dev/null');
daff5c 743         unlink('crontab.txt');
T 744     }
9200ad 745     
T 746 }
747
5d54be 748 ?>