r2843 - renderIPv4Address(): don't invent a wheel, but use renderCell() and renderLB...
[racktables] / upgrade.php
CommitLineData
6dc745d2 1<?php
fbbb74fb 2
90b96ff6
DO
3$relnotes = array
4(
2c9fde3a
DO
5 '0.17.0' => "LDAP options have been moved to LDAP_options array. This means, that if you were<br>" .
6 "using LDAP authentication for users in version 0.16.x, it will break right after<br>" .
aca5a846 7 "upgrade to 0.17.0. To get things working again, adjust existing secret.php file<br>" .
2c9fde3a 8 "according to secret-sample.php file provided with 0.17.0 release.<br><br>" .
204284ba 9 "Another change is the addition of support for file uploads. Files are stored<br>" .
90b96ff6 10 "in the database. There are several settings in php.ini which you may need to modify:<br>" .
4114697d
DO
11 "<ul><li>file_uploads - needs to be On</li>" .
12 "<li>upload_max_filesize - max size for uploaded files</li>" .
a476909e 13 "<li>post_max_size - max size of all form data submitted via POST (including files)</li></ul><br>" .
b82cce3f 14 "Local user accounts used to have 'enabled' flag, which allowed individual blocking and<br>" .
a476909e
DO
15 "unblocking of each. This flag was dropped in favor of existing mean of access<br>" .
16 "setup (RackCode). An unconditional denying rule is automatically added into RackCode<br>" .
17 "for such blocked account, so the effective security policy remains the same.<br>",
90b96ff6
DO
18);
19
fbbb74fb
DO
20// At the moment we assume, that for any two releases we can
21// sequentally execute all batches, that separate them, and
22// nothing will break. If this changes one day, the function
23// below will have to generate smarter upgrade paths, while
24// the upper layer will remain the same.
25// Returning an empty array means that no upgrade is necessary.
4114697d 26// Returning NULL indicates an error.
fbbb74fb
DO
27function getDBUpgradePath ($v1, $v2)
28{
a6f83a72
DO
29 $versionhistory = array
30 (
b3f866fc 31 '0.16.4',
64347dcf 32 '0.16.5',
90b96ff6 33 '0.16.6',
30d0a2a3 34 '0.17.0',
4563cecb 35 '0.17.1',
7b1a3a72 36 '0.17.2',
a6f83a72 37 );
120e9ddd
DO
38 if (!in_array ($v1, $versionhistory) or !in_array ($v2, $versionhistory))
39 return NULL;
fbbb74fb 40 $skip = TRUE;
4114697d 41 $path = NULL;
fbbb74fb
DO
42 // Now collect all versions > $v1 and <= $v2
43 foreach ($versionhistory as $v)
44 {
4114697d 45 if ($skip and $v == $v1)
fbbb74fb
DO
46 {
47 $skip = FALSE;
4114697d 48 $path = array();
fbbb74fb
DO
49 continue;
50 }
51 if ($skip)
52 continue;
53 $path[] = $v;
54 if ($v == $v2)
55 break;
56 }
57 return $path;
58}
59
90b96ff6
DO
60// Upgrade batches are named exactly as the release where they first appear.
61// That is simple, but seems sufficient for beginning.
fbbb74fb
DO
62function executeUpgradeBatch ($batchid)
63{
64 $query = array();
ce109ff2 65 global $dbxlink;
fbbb74fb
DO
66 switch ($batchid)
67 {
64347dcf
DO
68 case '0.16.5':
69 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4_TREE_SHOW_USAGE','yes','string','no','no','Show address usage in IPv4 tree')";
70 $query[] = "update Config set varvalue = '0.16.5' where varname = 'DB_VERSION'";
71 break;
90b96ff6
DO
72 case '0.16.6':
73 $query[] = "update Config set varvalue = '0.16.6' where varname = 'DB_VERSION'";
74 break;
30d0a2a3 75 case '0.17.0':
e1ae3fb4
AD
76 // create tables for storing files (requires InnoDB support)
77 if (!isInnoDBSupported ())
78 {
5d168fde 79 showFailure ("Cannot upgrade because InnoDB tables are not supported by your MySQL server. See the README for details.", __FILE__);
f3c50166 80 die;
e1ae3fb4 81 }
f76c4197
DY
82
83 $query[] = "alter table Chapter change chapter_no id int(10) unsigned NOT NULL auto_increment";
84 $query[] = "alter table Chapter change chapter_name name char(128) NOT NULL";
85 $query[] = "alter table Chapter drop key chapter_name";
86 $query[] = "alter table Chapter add UNIQUE KEY name (name)";
87 $query[] = "alter table Attribute change attr_id id int(10) unsigned NOT NULL auto_increment";
88 $query[] = "alter table Attribute change attr_type type enum('string','uint','float','dict') default NULL";
89 $query[] = "alter table Attribute change attr_name name char(64) default NULL";
90 $query[] = "alter table Attribute drop key attr_name";
91 $query[] = "alter table Attribute add UNIQUE KEY name (name)";
92 $query[] = "alter table AttributeMap change chapter_no chapter_id int(10) unsigned NOT NULL";
93 $query[] = "alter table Dictionary change chapter_no chapter_id int(10) unsigned NOT NULL";
90b96ff6
DO
94 // Many dictionary changes were made... remove all dictvendor entries and install fresh.
95 // Take care not to erase locally added records. 0.16.x ends with max key 797
f76c4197
DY
96 $query[] = 'DELETE FROM Dictionary WHERE ((chapter_id BETWEEN 11 AND 14) or (chapter_id BETWEEN 16 AND 19) ' .
97 'or (chapter_id BETWEEN 21 AND 24)) and dict_key <= 797';
f3c50166
AD
98 $f = fopen ("install/init-dictvendors.sql", 'r');
99 if ($f === FALSE)
100 {
5d168fde 101 showFailure ("Failed to open install/init-dictvendors.sql for reading");
f3c50166
AD
102 die;
103 }
104 $longq = '';
105 while (!feof ($f))
106 {
107 $line = fgets ($f);
108 if (ereg ('^--', $line))
109 continue;
110 $longq .= $line;
111 }
112 fclose ($f);
113 foreach (explode (";\n", $longq) as $dict_query)
114 {
115 if (empty ($dict_query))
116 continue;
117 $query[] = $dict_query;
118 }
119
120 // schema changes for file management
e1ae3fb4
AD
121 $query[] = "
122CREATE TABLE `File` (
123 `id` int(10) unsigned NOT NULL auto_increment,
124 `name` char(255) NOT NULL,
125 `type` char(255) NOT NULL,
126 `size` int(10) unsigned NOT NULL,
127 `ctime` datetime NOT NULL,
128 `mtime` datetime NOT NULL,
129 `atime` datetime NOT NULL,
130 `contents` longblob NOT NULL,
131 `comment` text,
13edfa1c
AD
132 PRIMARY KEY (`id`),
133 UNIQUE KEY `name` (`name`)
e1ae3fb4
AD
134) ENGINE=InnoDB";
135 $query[] = "
136CREATE TABLE `FileLink` (
137 `id` int(10) unsigned NOT NULL auto_increment,
138 `file_id` int(10) unsigned NOT NULL,
139 `entity_type` enum('ipv4net','ipv4rspool','ipv4vs','object','rack','user') NOT NULL default 'object',
140 `entity_id` int(10) NOT NULL,
141 PRIMARY KEY (`id`),
af721881 142 UNIQUE KEY `FileLink-unique` (`file_id`,`entity_type`,`entity_id`),
e1ae3fb4
AD
143 KEY `FileLink-file_id` (`file_id`),
144 CONSTRAINT `FileLink-File_fkey` FOREIGN KEY (`file_id`) REFERENCES `File` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
145) ENGINE=InnoDB";
146 $query[] = "ALTER TABLE TagStorage MODIFY COLUMN target_realm enum('file','ipv4net','ipv4rspool','ipv4vs','object','rack','user') NOT NULL default 'object'";
f3c50166 147
9730d09f
DO
148 $query[] = "INSERT INTO `Dictionary` (`chapter_id`, `dict_key`, `dict_value`) VALUES (1,798,'Network security')";
149 $query[] = "INSERT INTO `Dictionary` (`chapter_id`, `dict_key`, `dict_value`) VALUES (1,965,'Wireless')";
f76c4197 150 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (24,'no','network security models')";
9730d09f 151 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (25,'no','wireless models')";
f76c4197
DY
152 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,1,0)";
153 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,2,24)";
154 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,3,0)";
155 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,5,0)";
156 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,14,0)";
157 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,16,0)";
158 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,17,0)";
159 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,18,0)";
160 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,20,0)";
161 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,21,0)";
162 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,22,0)";
163 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,24,0)";
9730d09f
DO
164 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (965,1,0)";
165 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (965,3,0)";
166 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (965,2,25)";
d4739002 167 $query[] = "UPDATE Dictionary SET dict_value = 'Network switch' WHERE dict_key = 8";
706ce117
DO
168 $query[] = 'alter table IPBonds rename to IPv4Allocation';
169 $query[] = 'alter table PortForwarding rename to IPv4NAT';
170 $query[] = 'alter table IPRanges rename to IPv4Network';
171 $query[] = 'alter table IPAddress rename to IPv4Address';
172 $query[] = 'alter table IPLoadBalancer rename to IPv4LB';
4114697d 173 $query[] = 'alter table IPRSPool rename to IPv4RSPool';
706ce117 174 $query[] = 'alter table IPRealServer rename to IPv4RS';
4114697d 175 $query[] = 'alter table IPVirtualService rename to IPv4VS';
120e9ddd
DO
176 $query[] = "alter table TagStorage change column target_realm entity_realm enum('file','ipv4net','ipv4vs','ipv4rspool','object','rack','user') NOT NULL default 'object'";
177 $query[] = 'alter table TagStorage change column target_id entity_id int(10) unsigned NOT NULL';
178 $query[] = 'alter table TagStorage drop key entity_tag';
179 $query[] = 'alter table TagStorage drop key target_id';
180 $query[] = 'alter table TagStorage add UNIQUE KEY `entity_tag` (`entity_realm`,`entity_id`,`tag_id`)';
181 $query[] = 'alter table TagStorage add KEY `entity_id` (`entity_id`)';
37e59768
DO
182 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_TEXT_MAXCHARS','10240','uint','yes','no','Max chars for text file preview')";
183 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_TEXT_ROWS','25','uint','yes','no','Rows for text file preview')";
184 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_TEXT_COLS','80','uint','yes','no','Columns for text file preview')";
185 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_IMAGE_MAXPXS','320','uint','yes','no','Max pixels per axis for image file preview')";
f3d274bf 186 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('VENDOR_SIEVE','','string','yes','no','Vendor sieve configuration')";
073ed463
DO
187 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4LB_LISTSRC','{\$typeid_4}','string','yes','no','List source: IPv4 load balancers')";
188 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4OBJ_LISTSRC','{\$typeid_4} or {\$typeid_7} or {\$typeid_8} or {\$typeid_12} or {\$typeid_445} or {\$typeid_447}','string','yes','no','List source: IPv4-enabled objects')";
189 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4NAT_LISTSRC','{\$typeid_4} or {\$typeid_7} or {\$typeid_8}','string','yes','no','List source: IPv4 NAT performers')";
190 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('ASSETWARN_LISTSRC','{\$typeid_4} or {\$typeid_7} or {\$typeid_8}','string','yes','no','List source: object, for which asset tag should be set')";
191 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('NAMEWARN_LISTSRC','{\$typeid_4} or {\$typeid_7} or {\$typeid_8}','string','yes','no','List source: object, for which common name should be set')";
f5883ec1 192 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('RACKS_PER_ROW','12','unit','yes','no','Racks per row')";
590e1281 193 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_PREDICATE_SIEVE','','string','yes','no','Predicate sieve regex(7)')";
5496c89f
DO
194 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_DEFAULT_ANDOR','or','string','no','no','Default list filter boolean operation (or/and)')";
195 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_ANDOR','yes','string','no','no','Suggest and/or selector in list filter')";
196 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_TAGS','yes','string','no','no','Suggest tags in list filter')";
197 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_PREDICATES','yes','string','no','no','Suggest predicates in list filter')";
198 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_EXTRA','no','string','no','no','Suggest extra expression in list filter')";
529eac25
DO
199 $query[] = "delete from Config where varname = 'USER_AUTH_SRC'";
200 $query[] = "delete from Config where varname = 'COOKIE_TTL'";
201 $query[] = "delete from Config where varname = 'rtwidth_0'";
202 $query[] = "delete from Config where varname = 'rtwidth_1'";
203 $query[] = "delete from Config where varname = 'rtwidth_2'";
c6bc0ac5
DO
204 $query[] = "delete from Config where varname = 'NAMEFUL_OBJTYPES'";
205 $query[] = "delete from Config where varname = 'REQUIRE_ASSET_TAG_FOR'";
206 $query[] = "delete from Config where varname = 'IPV4_PERFORMERS'";
207 $query[] = "delete from Config where varname = 'NATV4_PERFORMERS'";
dbb33805 208 $query[] = "alter table TagTree add column valid_realm set('file','ipv4net','ipv4vs','ipv4rspool','object','rack','user') not null default 'file,ipv4net,ipv4vs,ipv4rspool,object,rack,user' after parent_id";
a476909e
DO
209 $result = $dbxlink->query ("select user_id, user_name, user_realname from UserAccount where user_enabled = 'no'");
210 while ($row = $result->fetch (PDO::FETCH_ASSOC))
211 $query[] = "update Script set script_text = concat('deny {\$userid_${row['user_id']}} # ${row['user_name']} (${row['user_realname']})\n', script_text) where script_name = 'RackCode'";
212 $query[] = "update Script set script_text = NULL where script_name = 'RackCodeCache'";
213 unset ($result);
79b8ad1e 214 $query[] = "alter table UserAccount drop column user_enabled";
f76c4197 215
10bac82a
DY
216 $query[] = "CREATE TABLE RackRow ( id int(10) unsigned NOT NULL auto_increment, name char(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM";
217
9f14a7ef
DY
218 $result = $dbxlink->query ("select dict_key, dict_value from Dictionary where chapter_no = 3");
219 while($row = $result->fetch(PDO::FETCH_NUM))
10bac82a 220 $query[] = "insert into RackRow set id=${row[0]}, name='${row[1]}'";
ee286837 221 unset ($result);
f76c4197 222 $query[] = "delete from Dictionary where chapter_id = 3";
c4d0dc30 223 $query[] = "delete from Chapter where id = 3";
9133d2c5
DO
224 $query[] = "
225CREATE TABLE `LDAPCache` (
226 `presented_username` char(64) NOT NULL,
227 `successful_hash` char(40) NOT NULL,
228 `first_success` timestamp NOT NULL default CURRENT_TIMESTAMP,
229 `last_retry` timestamp NOT NULL default '0000-00-00 00:00:00',
230 `displayed_name` char(128) default NULL,
231 `memberof` text,
232 UNIQUE KEY `presented_username` (`presented_username`),
233 KEY `scanidx` (`presented_username`,`successful_hash`)
234) ENGINE=InnoDB;";
3827da34 235 $query[] = "alter table UserAccount modify column user_password_hash char(40) NULL";
aa9a0fb4
DO
236 $query[] = 'ALTER TABLE Rack DROP COLUMN deleted';
237 $query[] = 'ALTER TABLE RackHistory DROP COLUMN deleted';
238 $query[] = 'ALTER TABLE RackObject DROP COLUMN deleted';
239 $query[] = 'ALTER TABLE RackObjectHistory DROP COLUMN deleted';
2fb9d280
DO
240 // Can't be added straight due to many duplicates, even in "dictbase" data.
241 $result = $dbxlink->query ('SELECT type1, type2, count(*) - 1 as excess FROM PortCompat GROUP BY type1, type2 HAVING excess > 0');
242 while ($row = $result->fetch (PDO::FETCH_ASSOC))
243 $query[] = "DELETE FROM PortCompat WHERE type1 = ${row['type1']} AND type2 = ${row['type2']} limit ${row['excess']}";
244 unset ($result);
245 $query[] = 'ALTER TABLE PortCompat DROP KEY type1';
67d8a969 246 $query[] = 'ALTER TABLE PortCompat ADD UNIQUE `type1_2` (type1, type2)';
f76c4197
DY
247 $query[] = "UPDATE Config SET varvalue = '0.17.0' WHERE varname = 'DB_VERSION'";
248
b3f866fc 249 break;
4563cecb 250 case '0.17.1':
8b200a9c 251 $query[] = "ALTER TABLE Dictionary DROP KEY `chap_to_key`";
4563cecb
DO
252 // Token set has changed, so the cache isn't valid any more.
253 $query[] = "UPDATE Script SET script_text = NULL WHERE script_name = 'RackCodeCache'";
254 $query[] = "UPDATE Config SET varvalue = '0.17.1' WHERE varname = 'DB_VERSION'";
7d4ea62b 255 break;
7b1a3a72
AD
256 case '0.17.2':
257 // Many dictionary changes were made... remove all dictvendor entries and install fresh.
258 // Take care not to erase locally added records. 0.17.1 ends with max key 988
259 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (26,'no','fibre channel switch models')";
260 $query[] = 'DELETE FROM Dictionary WHERE ((chapter_id BETWEEN 11 AND 14) or (chapter_id BETWEEN 16 AND 19) ' .
261 'or (chapter_id BETWEEN 21 AND 25)) and dict_key <= 988';
262 $f = fopen ("install/init-dictvendors.sql", 'r');
263 if ($f === FALSE)
264 {
265 showFailure ("Failed to open install/init-dictvendors.sql for reading");
266 die;
267 }
268 $longq = '';
269 while (!feof ($f))
270 {
271 $line = fgets ($f);
272 if (ereg ('^--', $line))
273 continue;
274 $longq .= $line;
275 }
276 fclose ($f);
277 foreach (explode (";\n", $longq) as $dict_query)
278 {
279 if (empty ($dict_query))
280 continue;
281 $query[] = $dict_query;
282 }
283 $query[] = "UPDATE Config SET varvalue = '0.17.2' WHERE varname = 'DB_VERSION'";
284 break;
fbbb74fb 285 default:
5d168fde 286 showFailure ("executeUpgradeBatch () failed, because batch '${batchid}' isn't defined", __FILE__);
fbbb74fb
DO
287 die;
288 break;
289 }
fbbb74fb 290 $failures = array();
4114697d 291 echo "<tr><th>Executing batch '${batchid}'</th><td>";
fbbb74fb
DO
292 foreach ($query as $q)
293 {
294 $result = $dbxlink->query ($q);
4114697d 295 if ($result == NULL)
758fe24c 296 {
758fe24c
DO
297 $errorInfo = $dbxlink->errorInfo();
298 $failures[] = array ($q, $errorInfo[2]);
299 }
fbbb74fb 300 }
fbbb74fb 301 if (!count ($failures))
4114697d 302 echo "<strong><font color=green>done</font></strong>";
fbbb74fb
DO
303 else
304 {
4114697d 305 echo "<strong><font color=red>The following queries failed:</font></strong><br><pre>";
fbbb74fb
DO
306 foreach ($failures as $f)
307 {
308 list ($q, $i) = $f;
4114697d 309 echo "${q} -- ${i}\n";
fbbb74fb 310 }
4114697d 311 echo "</pre>";
fbbb74fb 312 }
4114697d 313 echo '</td></tr>';
fbbb74fb
DO
314}
315
316// ******************************************************************
317//
318// Execution starts here
319//
320// ******************************************************************
321
fff18256
DO
322$root = (empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off') ? 'http://' : 'https://';
323$root .= isset ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'].($_SERVER['SERVER_PORT']=='80'?'':$_SERVER['SERVER_PORT']));
324$root .= strtr (dirname ($_SERVER['PHP_SELF']), '\\', '/');
54c2a7a8
DO
325if (substr ($root, -1) != '/')
326 $root .= '/';
fbbb74fb 327
5d168fde
DO
328// a clone of showError() to drop dependency on interface.php
329function showFailure ($info = '', $location = 'N/A')
330{
331 global $root;
332 if (preg_match ('/\.php$/', $location))
333 $location = basename ($location);
334 elseif ($location != 'N/A')
335 $location = $location . '()';
336 echo "<div class=msg_error>An error has occured in [${location}]. ";
337 if (empty ($info))
338 echo 'No additional information is available.';
339 else
340 echo "Additional information:<br><p>\n<pre>\n${info}\n</pre></p>";
341 echo "This failure is most probably fatal.<br></div>\n";
342}
343
344require_once 'inc/config.php'; // for CODE_VERSION
345require_once 'inc/database.php'; // for getDatabaseVersion()
346// Enforce default value for now, releases prior to 0.17.0 didn't support 'httpd' auth source.
347$user_auth_src = 'database';
fbbb74fb 348
80138748
DO
349if (file_exists ('inc/secret.php'))
350 require_once 'inc/secret.php';
fbbb74fb 351else
80138748
DO
352 die ("Database connection parameters are read from inc/secret.php file, " .
353 "which cannot be found.\nCopy provided inc/secret-sample.php to " .
354 "inc/secret.php and modify to your setup.\n\nThen reload the page.");
fbbb74fb
DO
355
356try
357{
358 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
359}
360catch (PDOException $e)
361{
362 die ("Database connection failed:\n\n" . $e->getMessage());
363}
364
fbbb74fb
DO
365// Now we need to be sure that the current user is the administrator.
366// The rest doesn't matter within this context.
fbbb74fb 367
120e9ddd 368function authenticate_admin ($username, $password)
a1f3710a 369{
43c7895d 370 global $dbxlink;
93bdb7ba 371 $hash = sha1 ($password);
120e9ddd 372 $query = "select count(*) from UserAccount where user_id = 1 and user_name = '${username}' and user_password_hash = '${hash}'";
a1f3710a
DO
373 if (($result = $dbxlink->query ($query)) == NULL)
374 die ('SQL query failed in ' . __FUNCTION__);
120e9ddd 375 $rows = $result->fetchAll (PDO::FETCH_NUM);
43c7895d 376 return $rows[0][0] == 1;
a1f3710a
DO
377}
378
204284ba 379switch ($user_auth_src)
99ee5479 380{
120e9ddd
DO
381 case 'database':
382 case 'ldap': // authenticate against DB as well
383 if
384 (
385 !isset ($_SERVER['PHP_AUTH_USER']) or
386 !strlen ($_SERVER['PHP_AUTH_USER']) or
387 !isset ($_SERVER['PHP_AUTH_PW']) or
388 !strlen ($_SERVER['PHP_AUTH_PW']) or
389 !authenticate_admin (escapeString ($_SERVER['PHP_AUTH_USER']), escapeString ($_SERVER['PHP_AUTH_PW']))
390 )
391 {
392 header ('WWW-Authenticate: Basic realm="RackTables upgrade"');
393 header ('HTTP/1.0 401 Unauthorized');
33bbd712 394 showFailure ('You must be authenticated as an administrator to complete the upgrade.', __FILE__);
120e9ddd
DO
395 die;
396 }
397 break; // cleared
398 case 'httpd':
399 if
400 (
401 !isset ($_SERVER['REMOTE_USER']) or
402 !strlen ($_SERVER['REMOTE_USER'])
403 )
404 {
33bbd712 405 showFailure ('System misconfiguration. The web-server didn\'t authenticate the user, although ought to do.');
120e9ddd
DO
406 die;
407 }
408 break; // cleared
409 default:
33bbd712 410 showFailure ('authentication source misconfiguration', __FILE__);
120e9ddd 411 die;
99ee5479 412}
fbbb74fb
DO
413
414$dbver = getDatabaseVersion();
4114697d
DO
415echo '<table border=1>';
416echo "<tr><th>Current status</th><td>Data version: ${dbver}<br>Code version: " . CODE_VERSION . "</td></tr>\n";
417
418$path = getDBUpgradePath ($dbver, CODE_VERSION);
419if ($path === NULL)
758fe24c 420{
4114697d
DO
421 echo "<tr><th>Upgrade path</th><td><font color=red>not found</font></td></tr>\n";
422 echo "<tr><th>Summary</th><td>Check README for more information.</td></tr>\n";
758fe24c 423}
4114697d 424else
5f4027b8 425{
4114697d
DO
426 if (!count ($path))
427 echo "<tr><th>Summary</th><td>Come back later.</td></tr>\n";
428 else
429 {
430 echo "<tr><th>Upgrade path</th><td>${dbver} &rarr; " . implode (' &rarr; ', $path) . "</td></tr>\n";
431 foreach ($path as $batchid)
432 {
433 executeUpgradeBatch ($batchid);
434 if (isset ($relnotes[$batchid]))
435 echo "<tr><th>Release notes for ${batchid}</th><td>" . $relnotes[$batchid] . "</td></tr>\n";
436 }
437 echo "<tr><th>Summary</th><td>Upgrade complete, it is Ok to <a href='${root}'>enter</a> the system.</td></tr>\n";
438 }
5f4027b8 439}
4114697d 440echo '</table>';
fbbb74fb
DO
441
442?>