tbrehm
2009-10-14 19057519ba6cf51f53ecc1aeb2011bb380799bd1
commit | author | age
e2d6ed 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
00dfba 31 /*
R 32     ISPConfig 3 updater.
33 */
098b2f 34
00dfba 35 error_reporting(E_ALL|E_STRICT);
R 36
4585cf 37 //** The banner on the command line
T 38 echo "\n\n".str_repeat('-',80)."\n";
39 echo " _____ ___________   _____              __ _       
40 |_   _/  ___| ___ \ /  __ \            / _(_)      
41   | | \ `--.| |_/ / | /  \/ ___  _ __ | |_ _  __ _ 
42   | |  `--. \  __/  | |    / _ \| '_ \|  _| |/ _` |
43  _| |_/\__/ / |     | \__/\ (_) | | | | | | | (_| |
44  \___/\____/\_|      \____/\___/|_| |_|_| |_|\__, |
45                                               __/ |
46                                              |___/ ";
47 echo "\n".str_repeat('-',80)."\n";
48 echo "\n\n>> Update  \n\n";
e2d6ed 49
098b2f 50 //** Include the library with the basic installer functions
e2d6ed 51 require_once('lib/install.lib.php');
T 52
098b2f 53 //** Include the base class of the installer class
e2d6ed 54 require_once('lib/installer_base.lib.php');
T 55
8c4aa3 56 //** Ensure that current working directory is install directory
T 57 $cur_dir = getcwd();
58 if(realpath(dirname(__FILE__)) != $cur_dir) die("Please run installation/update from _inside_ the install directory!\n");
59
00dfba 60 //** Install logfile
cc3fb3 61 define('ISPC_LOG_FILE', '/var/log/ispconfig_install.log');
faf3f5 62 define('ISPC_INSTALL_ROOT', realpath(dirname(__FILE__).'/../'));
cc3fb3 63
0c0d28 64 //** Check for ISPConfig 2.x versions
T 65 if(is_dir('/root/ispconfig') || is_dir('/home/admispconfig')) {
66     die('This software can not be installed on a server wich runs ISPConfig 2.x.');
67 }
68
cc3fb3 69 //** Get distribution identifier
90511b 70 $dist = get_distname();
e2d6ed 71
T 72 include_once("/usr/local/ispconfig/server/lib/config.inc.php");
73 $conf_old = $conf;
b8d8d3 74 unset($conf);
e2d6ed 75
90511b 76 if($dist['id'] == '') die('Linux Dustribution or Version not recognized.');
T 77
098b2f 78 //** Include the distribution specific installer class library and configuration
90511b 79 if(is_file('dist/lib/'.$dist['baseid'].'.lib.php')) include_once('dist/lib/'.$dist['baseid'].'.lib.php');
T 80 include_once('dist/lib/'.$dist['id'].'.lib.php');
81 include_once('dist/conf/'.$dist['id'].'.conf.php');
e2d6ed 82
35bcf2 83 //** Get hostname
T 84 exec('hostname -f', $tmp_out);
85 $conf['hostname'] = $tmp_out[0];
86 unset($tmp_out);
87
88
098b2f 89 //** Set the mysql login information
20218c 90 $conf["mysql"]["host"] = $conf_old["db_host"];
M 91 $conf["mysql"]["database"] = $conf_old["db_database"];
00dfba 92 $conf['mysql']['charset'] = 'utf8';
20218c 93 $conf["mysql"]["ispconfig_user"] = $conf_old["db_user"];
M 94 $conf["mysql"]["ispconfig_password"] = $conf_old["db_password"];
24854c 95 $conf['language'] = $conf_old['language'];
949e7e 96 if($conf['language'] == '{language}') $conf['language'] = 'en';
e2d6ed 97
72d7c3 98 if(isset($conf_old["dbmaster_host"])) $conf["mysql"]["master_host"] = $conf_old["dbmaster_host"];
T 99 if(isset($conf_old["dbmaster_database"])) $conf["mysql"]["master_database"] = $conf_old["dbmaster_database"];
190575 100 //if(isset($conf_old["dbmaster_user"])) $conf["mysql"]["master_ispconfig_user"] = $conf_old["dbmaster_user"];
T 101 //if(isset($conf_old["dbmaster_password"])) $conf["mysql"]["master_ispconfig_password"] = $conf_old["dbmaster_password"];
00e597 102
8ba08e 103 //* Check if this is a master / slave setup
T 104 if($conf["mysql"]["master_host"] != '' && $conf["mysql"]["host"] != $conf["mysql"]["master_host"]) {
105     $conf['mysql']['master_slave_setup'] = 'y';
106 }
107
cd972d 108 // Resolve the IP address of the mysql hostname.
T 109 if(!$conf['mysql']['ip'] = gethostbyname($conf['mysql']['host'])) die('Unable to resolve hostname'.$conf['mysql']['host']);
110
392450 111 $conf['server_id'] = intval($conf_old["server_id"]);
53ffea 112 $conf['ispconfig_log_priority'] = $conf_old["log_priority"];
4f7028 113
e2d6ed 114 $inst = new installer();
4f68a7 115 $inst->is_update = true;
e2d6ed 116
0a1f02 117 //** Detect the installed applications
T 118 $inst->find_installed_apps();
119
e2d6ed 120 echo "This application will update ISPConfig 3 on your server.\n";
T 121
098b2f 122 //** Initialize the MySQL server connection
e2d6ed 123 include_once('lib/mysql.lib.php');
T 124
098b2f 125 //** Database update is a bit brute force and should be rebuild later ;)
e2d6ed 126
098b2f 127 //** Ask user for mysql admin_password if empty
cc3fb3 128 if( empty($conf["mysql"]["admin_password"]) ) {
O 129
130     $conf["mysql"]["admin_password"] = $inst->free_query('MySQL root password', $conf['mysql']['admin_password']);
131 }
132
098b2f 133 //** export the current database data
cc3fb3 134 if( !empty($conf["mysql"]["admin_password"]) ) {
O 135
9855c7 136     system("mysqldump -h '".$conf['mysql']['host']."' -u '".$conf['mysql']['admin_user']."' -p'".$conf['mysql']['admin_password']."' -c -t --add-drop-table --all --quick ".$conf['mysql']['database']." > existing_db.sql");
cc3fb3 137 }
O 138 else {
139
9855c7 140     system("mysqldump -h '".$conf['mysql']['host']."' -u '".$conf['mysql']['admin_user']."' -c -t --add-drop-table --all --quick ".$conf['mysql']['database']." > existing_db.sql");
cc3fb3 141 }
O 142
190575 143 // create a backup copy of the ispconfig database in the root folder
T 144 $backup_db_name = '/root/ispconfig_db_backup_'.@date('Y-m-d_h-i').'.sql';
145 copy('existing_db.sql',$backup_db_name);
146 exec("chmod 700 $backup_db_name");
147 exec("chown root:root $backup_db_name");
148
392450 149
T 150 //* initialize the database
cc3fb3 151 $inst->db = new db();
O 152
fc918d 153 //* initialize the master DB, if we have a multiserver setup
T 154 if($conf['mysql']['master_slave_setup'] == 'y') {
190575 155         //** Get MySQL root credentials
T 156         $finished = false;
157         do {
158             $tmp_mysql_server_host = $inst->free_query('MySQL master server hostname', $conf['mysql']['master_host']);
159             $tmp_mysql_server_admin_user = $inst->free_query('MySQL master server root username', $conf['mysql']['master_admin_user']);
160             $tmp_mysql_server_admin_password = $inst->free_query('MySQL master server root password', $conf['mysql']['master_admin_password']);
161             $tmp_mysql_server_database = $inst->free_query('MySQL master server database name', $conf['mysql']['master_database']);
162     
163             //* Initialize the MySQL server connection
164             if(@mysql_connect($tmp_mysql_server_host, $tmp_mysql_server_admin_user, $tmp_mysql_server_admin_password)) {
165                 $conf['mysql']['master_host'] = $tmp_mysql_server_host;
166                 $conf['mysql']['master_admin_user'] = $tmp_mysql_server_admin_user;
167                 $conf['mysql']['master_admin_password'] = $tmp_mysql_server_admin_password;
168                 $conf['mysql']['master_database'] = $tmp_mysql_server_database;
169                 $finished = true;
170             } else {
171                 swriteln($inst->lng('Unable to connect to mysql server').' '.mysql_error());
172             }
173         } while ($finished == false);
174         unset($finished);
175         
176         // initialize the connection to the master database
177         $inst->dbmaster = new db();
178         if($inst->dbmaster->linkId) $inst->dbmaster->closeConn();
179         $inst->dbmaster->dbHost = $conf['mysql']["master_host"];
180         $inst->dbmaster->dbName = $conf['mysql']["master_database"];
181         $inst->dbmaster->dbUser = $conf['mysql']["master_admin_user"];
182         $inst->dbmaster->dbPass = $conf['mysql']["master_admin_password"];
fc918d 183 } else {
T 184     $inst->dbmaster = $inst->db;
185 }
186
392450 187 //* Update $conf array with values from the server.ini that shall be preserved
T 188 $tmp = $inst->db->queryOneRecord("SELECT * FROM ".$conf["mysql"]["database"].".server WHERE server_id = ".$conf['server_id']);
9eff6c 189 $ini_array = ini_to_array(stripslashes($tmp['config']));
392450 190
fc918d 191 if(count($ini_array) == 0) die('Unable to read server configuration from database.');
T 192
392450 193 $conf['services']['mail'] = ($tmp['mail_server'] == 1)?true:false;
T 194 $conf['services']['web'] = ($tmp['web_server'] == 1)?true:false;
195 $conf['services']['dns'] = ($tmp['dns_server'] == 1)?true:false;
196 $conf['services']['file'] = ($tmp['file_server'] == 1)?true:false;
197 $conf['services']['db'] = ($tmp['db_server'] == 1)?true:false;
198 $conf['services']['vserver'] = ($tmp['vserver_server'] == 1)?true:false;
9eff6c 199 $conf['postfix']['vmail_mailbox_base'] = $ini_array['mail']['homedir_path'];
392450 200
T 201 //** Delete the old database
cc3fb3 202 if( !$inst->db->query('DROP DATABASE IF EXISTS '.$conf['mysql']['database']) ) {
O 203
204     $inst->error('Unable to drop MySQL database: '.$conf['mysql']['database'].'.');
cb1d8b 205 }
T 206
098b2f 207 //** Create the mysql database
e2d6ed 208 $inst->configure_database();
8c4aa3 209
T 210 //** Update master database rights
211 $inst->grant_master_database_rights();
e2d6ed 212
098b2f 213 //** empty all databases
e2d6ed 214 $db_tables = $inst->db->getTables();
098b2f 215
e2d6ed 216 foreach($db_tables as $table) {
098b2f 217
e2d6ed 218     $inst->db->query("TRUNCATE $table");
T 219 }
220
098b2f 221 //** load old data back into database
O 222 if( !empty($conf["mysql"]["admin_password"]) ) {
223
9855c7 224     system("mysql --default-character-set=".$conf['mysql']['charset']." -h '".$conf['mysql']['host']."' -u '".$conf['mysql']['admin_user']."' -p'".$conf['mysql']['admin_password']."' ".$conf['mysql']['database']." < existing_db.sql");
cb1d8b 225 } else {
098b2f 226
9855c7 227     system("mysql --default-character-set=".$conf['mysql']['charset']." -h '".$conf['mysql']['host']."' -u '".$conf['mysql']['admin_user']."' ".$conf['mysql']['database']." < existing_db.sql");
cb1d8b 228 }
7f702a 229
T 230
4f7028 231 //** Update server ini
T 232 $tmp_server_rec = $inst->db->queryOneRecord("SELECT config FROM server WHERE server_id = ".$conf['server_id']);
233 $old_ini_array = ini_to_array(stripslashes($tmp_server_rec['config']));
234 unset($tmp_server_rec);
235 $tpl_ini_array = ini_to_array(rf('tpl/server.ini.master'));
236
237 // update the new template with the old values
faf3f5 238 if(is_array($old_ini_array)) {
T 239     foreach($old_ini_array as $tmp_section_name => $tmp_section_content) {
240         foreach($tmp_section_content as $tmp_var_name => $tmp_var_content) {
241             $tpl_ini_array[$tmp_section_name][$tmp_var_name] = $tmp_var_content;
242         }
4f7028 243     }
T 244 }
245
246 $new_ini = array_to_ini($tpl_ini_array);
8500be 247 $inst->db->query("UPDATE server SET config = '".mysql_real_escape_string($new_ini)."' WHERE server_id = ".$conf['server_id']);
4f7028 248 unset($old_ini_array);
T 249 unset($tpl_ini_array);
250 unset($new_ini);
251
252
db5aa6 253 //** Update system ini
T 254 $tmp_server_rec = $inst->db->queryOneRecord("SELECT config FROM sys_ini WHERE sysini_id = 1");
255 $old_ini_array = ini_to_array(stripslashes($tmp_server_rec['config']));
256 unset($tmp_server_rec);
257 $tpl_ini_array = ini_to_array(rf('tpl/system.ini.master'));
258
259 // update the new template with the old values
260 if(is_array($old_ini_array)) {
261     foreach($old_ini_array as $tmp_section_name => $tmp_section_content) {
262         foreach($tmp_section_content as $tmp_var_name => $tmp_var_content) {
263             $tpl_ini_array[$tmp_section_name][$tmp_var_name] = $tmp_var_content;
264         }
265     }
266 }
267
268 $new_ini = array_to_ini($tpl_ini_array);
78f1a6 269 $tmp = $inst->db->queryOneRecord('SELECT count(sysini_id) as number FROM sys_ini WHERE 1');
T 270 if($tmp['number'] == 0) {
271     $inst->db->query("INSERT INTO sys_ini (sysini_id, config) VALUES (1,'".mysql_real_escape_string($new_ini)."')");
272 } else {
273     $inst->db->query("UPDATE sys_ini SET config = '".mysql_real_escape_string($new_ini)."' WHERE sysini_id = 1");
274 }
db5aa6 275 unset($old_ini_array);
T 276 unset($tpl_ini_array);
277 unset($new_ini);
278
279
00d96b 280 //** Shall the services be reconfigured during update
e2ef16 281 $reconfigure_services_answer = $inst->simple_query('Reconfigure Services?', array('yes','no'),'yes');
e2d6ed 282
00d96b 283 if($reconfigure_services_answer == 'yes') {
61d290 284     
392450 285     if($conf['services']['mail']) {
T 286         //** Configure postfix
132223 287         swriteln('Configuring Postfix');
392450 288         $inst->configure_postfix('dont-create-certs');
61d290 289     
132223 290         //* Configure Jailkit
392450 291         swriteln('Configuring Jailkit');
T 292         $inst->configure_jailkit();
00d96b 293     
392450 294         //** Configure saslauthd
T 295         swriteln('Configuring SASL');
296         $inst->configure_saslauthd();
d83fcf 297     
392450 298         //** Configure PAM
T 299         swriteln('Configuring PAM');
300         $inst->configure_pam();
301
302         //** Configure courier
303         swriteln('Configuring Courier');
304         $inst->configure_courier();
305
306         //** Configure Spamasassin
307         swriteln('Configuring Spamassassin');
308         $inst->configure_spamassassin();
309
310         //** Configure Amavis
311         swriteln('Configuring Amavisd');
312         $inst->configure_amavis();
313
314         //** Configure Getmail
315         swriteln('Configuring Getmail');
316         $inst->configure_getmail();
317     }
318     
319     if($conf['services']['web']) {
320         //** Configure Pureftpd
321         swriteln('Configuring Pureftpd');
322         $inst->configure_pureftpd();
323     }
324     
325     if($conf['services']['dns']) {
326         //** Configure MyDNS
327         swriteln('Configuring MyDNS');
328         $inst->configure_mydns();
329     }
330     
331     if($conf['services']['web']) {
332         //** Configure Apache
333         swriteln('Configuring Apache');
334         $inst->configure_apache();
8c4aa3 335         
T 336         //** Configure vlogger
337         swriteln('Configuring vlogger');
338         $inst->configure_vlogger();
392450 339     }
T 340     
341
d83fcf 342     //* Configure DBServer
392450 343     swriteln('Configuring Database');
d83fcf 344     $inst->configure_dbserver();
392450 345
7c99ef 346     
69db55 347     //if(@is_dir('/etc/Bastille')) {
392450 348     //* Configure Firewall
T 349     swriteln('Configuring Firewall');
350     $inst->configure_firewall();
69db55 351     //}
00d96b 352 }
313e33 353
098b2f 354 //** Configure ISPConfig
00d96b 355 swriteln('Updating ISPConfig');
9b9ba4 356
D 357
358 //** Customise the port ISPConfig runs on
cd972d 359 $conf['apache']['vhost_port'] = $inst->free_query('ISPConfig Port', '8080');
9b9ba4 360
e2d6ed 361 $inst->install_ispconfig();
T 362
00d96b 363 //** Configure Crontab
6eb2af 364 $update_crontab_answer = $inst->simple_query('Reconfigure Crontab?', array('yes','no'),'yes');
00d96b 365 if($update_crontab_answer == 'yes') {
T 366     swriteln('Updating Crontab');
367     $inst->install_crontab();
368 }
e2d6ed 369
098b2f 370 //** Restart services:
00d96b 371 if($reconfigure_services_answer == 'yes') {
T 372     swriteln('Restarting services ...');
2ce158 373     if($conf['mysql']['init_script'] != '' && is_file($conf['init_scripts'].'/'.$conf['mysql']['init_script']))                    system($conf['init_scripts'].'/'.$conf['mysql']['init_script'].' restart');
392450 374     if($conf['services']['mail']) {
2ce158 375         if($conf['postfix']['init_script'] != '' && is_file($conf['init_scripts'].'/'.$conf['postfix']['init_script']))                system($conf['init_scripts'].'/'.$conf['postfix']['init_script'].' restart');
T 376         if($conf['saslauthd']['init_script'] != '' && is_file($conf['init_scripts'].'/'.$conf['saslauthd']['init_script']))            system($conf['init_scripts'].'/'.$conf['saslauthd']['init_script'].' restart');
377         if($conf['amavis']['init_script'] != '' && is_file($conf['init_scripts'].'/'.$conf['amavis']['init_script']))                    system($conf['init_scripts'].'/'.$conf['amavis']['init_script'].' restart');
378         if($conf['clamav']['init_script'] != '' && is_file($conf['init_scripts'].'/'.$conf['clamav']['init_script']))                    system($conf['init_scripts'].'/'.$conf['clamav']['init_script'].' restart');
379         if($conf['courier']['courier-authdaemon'] != '' && is_file($conf['init_scripts'].'/'.$conf['courier']['courier-authdaemon'])) system($conf['init_scripts'].'/'.$conf['courier']['courier-authdaemon'].' restart');
380         if($conf['courier']['courier-imap'] != '' && is_file($conf['init_scripts'].'/'.$conf['courier']['courier-imap']))             system($conf['init_scripts'].'/'.$conf['courier']['courier-imap'].' restart');
381         if($conf['courier']['courier-imap-ssl'] != '' && is_file($conf['init_scripts'].'/'.$conf['courier']['courier-imap-ssl']))     system($conf['init_scripts'].'/'.$conf['courier']['courier-imap-ssl'].' restart');
382         if($conf['courier']['courier-pop'] != '' && is_file($conf['init_scripts'].'/'.$conf['courier']['courier-pop']))                 system($conf['init_scripts'].'/'.$conf['courier']['courier-pop'].' restart');
383         if($conf['courier']['courier-pop-ssl'] != '' && is_file($conf['init_scripts'].'/'.$conf['courier']['courier-pop-ssl']))         system($conf['init_scripts'].'/'.$conf['courier']['courier-pop-ssl'].' restart');
392450 384     }
T 385     if($conf['services']['web']) {
2ce158 386         if($conf['apache']['init_script'] != '' && is_file($conf['init_scripts'].'/'.$conf['apache']['init_script']))                 system($conf['init_scripts'].'/'.$conf['apache']['init_script'].' restart');
T 387         if($conf['pureftpd']['init_script'] != '' && is_file($conf['init_scripts'].'/'.$conf['pureftpd']['init_script']))                system($conf['init_scripts'].'/'.$conf['pureftpd']['init_script'].' restart');
392450 388     }
T 389     if($conf['services']['dns']) {
2ce158 390         if($conf['mydns']['init_script'] != '' && is_file($conf['init_scripts'].'/'.$conf['mydns']['init_script']))                    system($conf['init_scripts'].'/'.$conf['mydns']['init_script'].' restart &> /dev/null');
392450 391     }
00d96b 392 }
e2d6ed 393
T 394 echo "Update finished.\n";
395
20218c 396 ?>