r3056 - add constraints for: AttributeValue, RackObject, IPv4NAT, IPv4LB, IPv4VS
[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',
9e51318b 37 '0.17.3',
958ac06d 38 '0.17.4',
a6f83a72 39 );
120e9ddd
DO
40 if (!in_array ($v1, $versionhistory) or !in_array ($v2, $versionhistory))
41 return NULL;
fbbb74fb 42 $skip = TRUE;
4114697d 43 $path = NULL;
fbbb74fb
DO
44 // Now collect all versions > $v1 and <= $v2
45 foreach ($versionhistory as $v)
46 {
4114697d 47 if ($skip and $v == $v1)
fbbb74fb
DO
48 {
49 $skip = FALSE;
4114697d 50 $path = array();
fbbb74fb
DO
51 continue;
52 }
53 if ($skip)
54 continue;
55 $path[] = $v;
56 if ($v == $v2)
57 break;
58 }
59 return $path;
60}
61
90b96ff6
DO
62// Upgrade batches are named exactly as the release where they first appear.
63// That is simple, but seems sufficient for beginning.
fbbb74fb
DO
64function executeUpgradeBatch ($batchid)
65{
66 $query = array();
ca3d68bd 67 global $dbxlink;
fbbb74fb
DO
68 switch ($batchid)
69 {
64347dcf
DO
70 case '0.16.5':
71 $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')";
72 $query[] = "update Config set varvalue = '0.16.5' where varname = 'DB_VERSION'";
73 break;
90b96ff6
DO
74 case '0.16.6':
75 $query[] = "update Config set varvalue = '0.16.6' where varname = 'DB_VERSION'";
76 break;
30d0a2a3 77 case '0.17.0':
e1ae3fb4
AD
78 // create tables for storing files (requires InnoDB support)
79 if (!isInnoDBSupported ())
80 {
5d168fde 81 showFailure ("Cannot upgrade because InnoDB tables are not supported by your MySQL server. See the README for details.", __FILE__);
f3c50166 82 die;
e1ae3fb4 83 }
f76c4197
DY
84
85 $query[] = "alter table Chapter change chapter_no id int(10) unsigned NOT NULL auto_increment";
86 $query[] = "alter table Chapter change chapter_name name char(128) NOT NULL";
87 $query[] = "alter table Chapter drop key chapter_name";
88 $query[] = "alter table Chapter add UNIQUE KEY name (name)";
89 $query[] = "alter table Attribute change attr_id id int(10) unsigned NOT NULL auto_increment";
90 $query[] = "alter table Attribute change attr_type type enum('string','uint','float','dict') default NULL";
91 $query[] = "alter table Attribute change attr_name name char(64) default NULL";
92 $query[] = "alter table Attribute drop key attr_name";
93 $query[] = "alter table Attribute add UNIQUE KEY name (name)";
94 $query[] = "alter table AttributeMap change chapter_no chapter_id int(10) unsigned NOT NULL";
95 $query[] = "alter table Dictionary change chapter_no chapter_id int(10) unsigned NOT NULL";
3fb336f6 96 // Only after the above call it is Ok to use reloadDictionary()
ca3d68bd 97 $query = array_merge ($query, reloadDictionary ($batchid));
f3c50166 98 // schema changes for file management
e1ae3fb4
AD
99 $query[] = "
100CREATE TABLE `File` (
101 `id` int(10) unsigned NOT NULL auto_increment,
102 `name` char(255) NOT NULL,
103 `type` char(255) NOT NULL,
104 `size` int(10) unsigned NOT NULL,
105 `ctime` datetime NOT NULL,
106 `mtime` datetime NOT NULL,
107 `atime` datetime NOT NULL,
108 `contents` longblob NOT NULL,
109 `comment` text,
13edfa1c
AD
110 PRIMARY KEY (`id`),
111 UNIQUE KEY `name` (`name`)
e1ae3fb4
AD
112) ENGINE=InnoDB";
113 $query[] = "
114CREATE TABLE `FileLink` (
115 `id` int(10) unsigned NOT NULL auto_increment,
116 `file_id` int(10) unsigned NOT NULL,
117 `entity_type` enum('ipv4net','ipv4rspool','ipv4vs','object','rack','user') NOT NULL default 'object',
118 `entity_id` int(10) NOT NULL,
119 PRIMARY KEY (`id`),
af721881 120 UNIQUE KEY `FileLink-unique` (`file_id`,`entity_type`,`entity_id`),
e1ae3fb4
AD
121 KEY `FileLink-file_id` (`file_id`),
122 CONSTRAINT `FileLink-File_fkey` FOREIGN KEY (`file_id`) REFERENCES `File` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
123) ENGINE=InnoDB";
124 $query[] = "ALTER TABLE TagStorage MODIFY COLUMN target_realm enum('file','ipv4net','ipv4rspool','ipv4vs','object','rack','user') NOT NULL default 'object'";
f3c50166 125
f76c4197 126 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (24,'no','network security models')";
9730d09f 127 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (25,'no','wireless models')";
f76c4197
DY
128 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,1,0)";
129 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,2,24)";
130 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,3,0)";
131 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,5,0)";
132 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,14,0)";
133 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,16,0)";
134 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,17,0)";
135 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,18,0)";
136 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,20,0)";
137 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,21,0)";
138 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,22,0)";
139 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,24,0)";
9730d09f
DO
140 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (965,1,0)";
141 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (965,3,0)";
142 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (965,2,25)";
706ce117
DO
143 $query[] = 'alter table IPBonds rename to IPv4Allocation';
144 $query[] = 'alter table PortForwarding rename to IPv4NAT';
145 $query[] = 'alter table IPRanges rename to IPv4Network';
146 $query[] = 'alter table IPAddress rename to IPv4Address';
147 $query[] = 'alter table IPLoadBalancer rename to IPv4LB';
4114697d 148 $query[] = 'alter table IPRSPool rename to IPv4RSPool';
706ce117 149 $query[] = 'alter table IPRealServer rename to IPv4RS';
4114697d 150 $query[] = 'alter table IPVirtualService rename to IPv4VS';
120e9ddd
DO
151 $query[] = "alter table TagStorage change column target_realm entity_realm enum('file','ipv4net','ipv4vs','ipv4rspool','object','rack','user') NOT NULL default 'object'";
152 $query[] = 'alter table TagStorage change column target_id entity_id int(10) unsigned NOT NULL';
153 $query[] = 'alter table TagStorage drop key entity_tag';
154 $query[] = 'alter table TagStorage drop key target_id';
155 $query[] = 'alter table TagStorage add UNIQUE KEY `entity_tag` (`entity_realm`,`entity_id`,`tag_id`)';
156 $query[] = 'alter table TagStorage add KEY `entity_id` (`entity_id`)';
37e59768
DO
157 $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')";
158 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_TEXT_ROWS','25','uint','yes','no','Rows for text file preview')";
159 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_TEXT_COLS','80','uint','yes','no','Columns for text file preview')";
160 $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 161 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('VENDOR_SIEVE','','string','yes','no','Vendor sieve configuration')";
073ed463
DO
162 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4LB_LISTSRC','{\$typeid_4}','string','yes','no','List source: IPv4 load balancers')";
163 $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')";
164 $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')";
165 $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')";
166 $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 167 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('RACKS_PER_ROW','12','unit','yes','no','Racks per row')";
590e1281 168 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_PREDICATE_SIEVE','','string','yes','no','Predicate sieve regex(7)')";
5496c89f
DO
169 $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)')";
170 $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')";
171 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_TAGS','yes','string','no','no','Suggest tags in list filter')";
172 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_PREDICATES','yes','string','no','no','Suggest predicates in list filter')";
173 $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
174 $query[] = "delete from Config where varname = 'USER_AUTH_SRC'";
175 $query[] = "delete from Config where varname = 'COOKIE_TTL'";
176 $query[] = "delete from Config where varname = 'rtwidth_0'";
177 $query[] = "delete from Config where varname = 'rtwidth_1'";
178 $query[] = "delete from Config where varname = 'rtwidth_2'";
c6bc0ac5
DO
179 $query[] = "delete from Config where varname = 'NAMEFUL_OBJTYPES'";
180 $query[] = "delete from Config where varname = 'REQUIRE_ASSET_TAG_FOR'";
181 $query[] = "delete from Config where varname = 'IPV4_PERFORMERS'";
182 $query[] = "delete from Config where varname = 'NATV4_PERFORMERS'";
dbb33805 183 $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
184 $result = $dbxlink->query ("select user_id, user_name, user_realname from UserAccount where user_enabled = 'no'");
185 while ($row = $result->fetch (PDO::FETCH_ASSOC))
186 $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'";
187 $query[] = "update Script set script_text = NULL where script_name = 'RackCodeCache'";
188 unset ($result);
79b8ad1e 189 $query[] = "alter table UserAccount drop column user_enabled";
f76c4197 190
10bac82a
DY
191 $query[] = "CREATE TABLE RackRow ( id int(10) unsigned NOT NULL auto_increment, name char(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM";
192
9f14a7ef
DY
193 $result = $dbxlink->query ("select dict_key, dict_value from Dictionary where chapter_no = 3");
194 while($row = $result->fetch(PDO::FETCH_NUM))
10bac82a 195 $query[] = "insert into RackRow set id=${row[0]}, name='${row[1]}'";
ee286837 196 unset ($result);
f76c4197 197 $query[] = "delete from Dictionary where chapter_id = 3";
c4d0dc30 198 $query[] = "delete from Chapter where id = 3";
9133d2c5
DO
199 $query[] = "
200CREATE TABLE `LDAPCache` (
201 `presented_username` char(64) NOT NULL,
202 `successful_hash` char(40) NOT NULL,
203 `first_success` timestamp NOT NULL default CURRENT_TIMESTAMP,
204 `last_retry` timestamp NOT NULL default '0000-00-00 00:00:00',
205 `displayed_name` char(128) default NULL,
206 `memberof` text,
207 UNIQUE KEY `presented_username` (`presented_username`),
208 KEY `scanidx` (`presented_username`,`successful_hash`)
209) ENGINE=InnoDB;";
3827da34 210 $query[] = "alter table UserAccount modify column user_password_hash char(40) NULL";
aa9a0fb4
DO
211 $query[] = 'ALTER TABLE Rack DROP COLUMN deleted';
212 $query[] = 'ALTER TABLE RackHistory DROP COLUMN deleted';
213 $query[] = 'ALTER TABLE RackObject DROP COLUMN deleted';
214 $query[] = 'ALTER TABLE RackObjectHistory DROP COLUMN deleted';
2fb9d280
DO
215 // Can't be added straight due to many duplicates, even in "dictbase" data.
216 $result = $dbxlink->query ('SELECT type1, type2, count(*) - 1 as excess FROM PortCompat GROUP BY type1, type2 HAVING excess > 0');
217 while ($row = $result->fetch (PDO::FETCH_ASSOC))
218 $query[] = "DELETE FROM PortCompat WHERE type1 = ${row['type1']} AND type2 = ${row['type2']} limit ${row['excess']}";
219 unset ($result);
220 $query[] = 'ALTER TABLE PortCompat DROP KEY type1';
67d8a969 221 $query[] = 'ALTER TABLE PortCompat ADD UNIQUE `type1_2` (type1, type2)';
f76c4197
DY
222 $query[] = "UPDATE Config SET varvalue = '0.17.0' WHERE varname = 'DB_VERSION'";
223
b3f866fc 224 break;
4563cecb 225 case '0.17.1':
8b200a9c 226 $query[] = "ALTER TABLE Dictionary DROP KEY `chap_to_key`";
ca3d68bd 227 $query = array_merge ($query, reloadDictionary ($batchid));
4563cecb
DO
228 // Token set has changed, so the cache isn't valid any more.
229 $query[] = "UPDATE Script SET script_text = NULL WHERE script_name = 'RackCodeCache'";
230 $query[] = "UPDATE Config SET varvalue = '0.17.1' WHERE varname = 'DB_VERSION'";
7d4ea62b 231 break;
7b1a3a72 232 case '0.17.2':
7b1a3a72 233 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (26,'no','fibre channel switch models')";
ca3d68bd 234 $query = array_merge ($query, reloadDictionary ($batchid));
7c537f33 235 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (1055,2,26)";
49b605d9 236 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('DEFAULT_SNMP_COMMUNITY','public','string','no','no','Default SNMP Community string')";
29c3a4d8
DO
237 // wipe irrelevant records (ticket:250)
238 $query[] = "DELETE FROM TagStorage WHERE entity_realm = 'file' AND entity_id NOT IN (SELECT id FROM File)";
f06fe423 239 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4_ENABLE_KNIGHT','yes','string','no','no','Enable IPv4 knight feature')";
99ab184f 240 $query[] = "ALTER TABLE IPv4Network ADD COLUMN comment text AFTER name";
5163cd3a 241 $query[] = "ALTER TABLE Port ADD INDEX comment (reservation_comment)";
029a14bc
DO
242 $query[] = "ALTER TABLE Port DROP KEY l2address"; // UNIQUE
243 $query[] = "ALTER TABLE Port ADD KEY (l2address)"; // not UNIQUE
948666cc
DO
244 $query[] = "ALTER TABLE Port DROP KEY object_id";
245 $query[] = "ALTER TABLE Port ADD UNIQUE KEY per_object (object_id, name, type)";
74aee2dc
DO
246 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (20,1083)";
247 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (21,1083)";
248 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1077,1077)";
249 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1083,20)";
250 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1083,21)";
251 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1083,1083)";
1c4830dc 252 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1087,1087)";
50e02490
DO
253 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (27,'no','PDU models')";
254 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (2,2,27)";
7b1a3a72
AD
255 $query[] = "UPDATE Config SET varvalue = '0.17.2' WHERE varname = 'DB_VERSION'";
256 break;
9e51318b
DO
257 case '0.17.3':
258 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('TAGS_TOPLIST_SIZE','50','uint','yes','no','Tags top list size')";
259 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('TAGS_QUICKLIST_SIZE','20','uint','no','no','Tags quick list size')";
260 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('TAGS_QUICKLIST_THRESHOLD','50','uint','yes','no','Tags quick list threshold')";
87601bbc 261 $query[] = "ALTER TABLE AttributeMap MODIFY COLUMN chapter_id int(10) unsigned NULL";
7028a42c 262 $query[] = "UPDATE AttributeMap SET chapter_id = NULL WHERE attr_id IN (SELECT id FROM Attribute WHERE type != 'dict')";
a013838b
DO
263 // ticket:239
264 $query[] = 'UPDATE AttributeValue SET uint_value = 1018 WHERE uint_value = 731 AND attr_id IN (SELECT attr_id FROM AttributeMap WHERE chapter_id = 12)';
265 $query[] = 'DELETE FROM Dictionary WHERE dict_key = 731';
084aca6c
DO
266 $query = array_merge ($query, reloadDictionary ($batchid));
267 $query[] = "UPDATE Config SET vartype='uint' WHERE varname='RACKS_PER_ROW'";
f44fdef9 268 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('ENABLE_MULTIPORT_FORM','no','string','no','no','Enable \"Add/update multiple ports\" form')";
9e51318b
DO
269 $query[] = "UPDATE Config SET varvalue = '0.17.3' WHERE varname = 'DB_VERSION'";
270 break;
958ac06d
DO
271 case '0.17.4':
272 $query[] = "ALTER TABLE Link ENGINE=InnoDB";
273 $query[] = "ALTER TABLE Port ENGINE=InnoDB";
4d87feaf
DO
274 $query[] = "ALTER TABLE IPv4RS ENGINE=InnoDB";
275 $query[] = "ALTER TABLE IPv4RSPool ENGINE=InnoDB";
cafd4cf3
DO
276 $query[] = "ALTER TABLE AttributeValue ENGINE=InnoDB";
277 $query[] = "ALTER TABLE RackObject ENGINE=InnoDB";
278 $query[] = "ALTER TABLE IPv4NAT ENGINE=InnoDB";
279 $query[] = "ALTER TABLE IPv4LB ENGINE=InnoDB";
280 $query[] = "ALTER TABLE IPv4VS ENGINE=InnoDB";
4d87feaf 281 $query[] = "DELETE FROM IPv4RS WHERE rspool_id NOT IN (SELECT id FROM IPv4RSPool)";
958ac06d 282 $query[] = "ALTER TABLE Link ADD CONSTRAINT `Link-FK-b` FOREIGN KEY (portb) REFERENCES Port (id)";
cafd4cf3 283 $query[] = "ALTER TABLE Link ADD CONSTRAINT `Link-FK-a` FOREIGN KEY (porta) REFERENCES Port (id)";
4d87feaf 284 $query[] = "ALTER TABLE IPv4RS ADD CONSTRAINT `IPv4RS-FK` FOREIGN KEY (rspool_id) REFERENCES IPv4RSPool (id) ON DELETE CASCADE";
cafd4cf3
DO
285 $query[] = "ALTER TABLE AttributeValue ADD CONSTRAINT `AttributeValue-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
286 $query[] = "ALTER TABLE IPv4NAT ADD CONSTRAINT `IPv4NAT-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
287 $query[] = "ALTER TABLE Port ADD CONSTRAINT `Port-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
288 $query[] = "ALTER TABLE IPv4LB ADD CONSTRAINT `IPv4LB-FK-rspool_id` FOREIGN KEY (rspool_id) REFERENCES IPv4RSPool (id)";
289 $query[] = "ALTER TABLE IPv4LB ADD CONSTRAINT `IPv4LB-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
290 $query[] = "ALTER TABLE IPv4LB ADD CONSTRAINT `IPv4LB-FK-vs_id` FOREIGN KEY (vs_id) REFERENCES IPv4VS (id)";
958ac06d
DO
291 $query[] = "UPDATE Config SET varvalue = '0.17.4' WHERE varname = 'DB_VERSION'";
292 break;
fbbb74fb 293 default:
5d168fde 294 showFailure ("executeUpgradeBatch () failed, because batch '${batchid}' isn't defined", __FILE__);
fbbb74fb
DO
295 die;
296 break;
297 }
fbbb74fb 298 $failures = array();
4114697d 299 echo "<tr><th>Executing batch '${batchid}'</th><td>";
fbbb74fb
DO
300 foreach ($query as $q)
301 {
302 $result = $dbxlink->query ($q);
4114697d 303 if ($result == NULL)
758fe24c 304 {
758fe24c
DO
305 $errorInfo = $dbxlink->errorInfo();
306 $failures[] = array ($q, $errorInfo[2]);
307 }
fbbb74fb 308 }
fbbb74fb 309 if (!count ($failures))
4114697d 310 echo "<strong><font color=green>done</font></strong>";
fbbb74fb
DO
311 else
312 {
4114697d 313 echo "<strong><font color=red>The following queries failed:</font></strong><br><pre>";
fbbb74fb
DO
314 foreach ($failures as $f)
315 {
316 list ($q, $i) = $f;
4114697d 317 echo "${q} -- ${i}\n";
fbbb74fb 318 }
4114697d 319 echo "</pre>";
fbbb74fb 320 }
4114697d 321 echo '</td></tr>';
fbbb74fb
DO
322}
323
324// ******************************************************************
325//
326// Execution starts here
327//
328// ******************************************************************
329
fff18256
DO
330$root = (empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off') ? 'http://' : 'https://';
331$root .= isset ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'].($_SERVER['SERVER_PORT']=='80'?'':$_SERVER['SERVER_PORT']));
332$root .= strtr (dirname ($_SERVER['PHP_SELF']), '\\', '/');
54c2a7a8
DO
333if (substr ($root, -1) != '/')
334 $root .= '/';
fbbb74fb 335
5d168fde
DO
336// a clone of showError() to drop dependency on interface.php
337function showFailure ($info = '', $location = 'N/A')
338{
339 global $root;
340 if (preg_match ('/\.php$/', $location))
341 $location = basename ($location);
342 elseif ($location != 'N/A')
343 $location = $location . '()';
344 echo "<div class=msg_error>An error has occured in [${location}]. ";
345 if (empty ($info))
346 echo 'No additional information is available.';
347 else
348 echo "Additional information:<br><p>\n<pre>\n${info}\n</pre></p>";
349 echo "This failure is most probably fatal.<br></div>\n";
350}
351
352require_once 'inc/config.php'; // for CODE_VERSION
353require_once 'inc/database.php'; // for getDatabaseVersion()
3fb336f6 354require_once 'inc/dictionary.php';
5d168fde
DO
355// Enforce default value for now, releases prior to 0.17.0 didn't support 'httpd' auth source.
356$user_auth_src = 'database';
fbbb74fb 357
80138748
DO
358if (file_exists ('inc/secret.php'))
359 require_once 'inc/secret.php';
fbbb74fb 360else
80138748
DO
361 die ("Database connection parameters are read from inc/secret.php file, " .
362 "which cannot be found.\nCopy provided inc/secret-sample.php to " .
363 "inc/secret.php and modify to your setup.\n\nThen reload the page.");
fbbb74fb
DO
364
365try
366{
367 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
368}
369catch (PDOException $e)
370{
371 die ("Database connection failed:\n\n" . $e->getMessage());
372}
373
fbbb74fb
DO
374// Now we need to be sure that the current user is the administrator.
375// The rest doesn't matter within this context.
fbbb74fb 376
120e9ddd 377function authenticate_admin ($username, $password)
a1f3710a 378{
43c7895d 379 global $dbxlink;
93bdb7ba 380 $hash = sha1 ($password);
120e9ddd 381 $query = "select count(*) from UserAccount where user_id = 1 and user_name = '${username}' and user_password_hash = '${hash}'";
a1f3710a
DO
382 if (($result = $dbxlink->query ($query)) == NULL)
383 die ('SQL query failed in ' . __FUNCTION__);
120e9ddd 384 $rows = $result->fetchAll (PDO::FETCH_NUM);
43c7895d 385 return $rows[0][0] == 1;
a1f3710a
DO
386}
387
204284ba 388switch ($user_auth_src)
99ee5479 389{
120e9ddd
DO
390 case 'database':
391 case 'ldap': // authenticate against DB as well
392 if
393 (
394 !isset ($_SERVER['PHP_AUTH_USER']) or
395 !strlen ($_SERVER['PHP_AUTH_USER']) or
396 !isset ($_SERVER['PHP_AUTH_PW']) or
397 !strlen ($_SERVER['PHP_AUTH_PW']) or
398 !authenticate_admin (escapeString ($_SERVER['PHP_AUTH_USER']), escapeString ($_SERVER['PHP_AUTH_PW']))
399 )
400 {
401 header ('WWW-Authenticate: Basic realm="RackTables upgrade"');
402 header ('HTTP/1.0 401 Unauthorized');
33bbd712 403 showFailure ('You must be authenticated as an administrator to complete the upgrade.', __FILE__);
120e9ddd
DO
404 die;
405 }
406 break; // cleared
407 case 'httpd':
408 if
409 (
410 !isset ($_SERVER['REMOTE_USER']) or
411 !strlen ($_SERVER['REMOTE_USER'])
412 )
413 {
33bbd712 414 showFailure ('System misconfiguration. The web-server didn\'t authenticate the user, although ought to do.');
120e9ddd
DO
415 die;
416 }
417 break; // cleared
418 default:
33bbd712 419 showFailure ('authentication source misconfiguration', __FILE__);
120e9ddd 420 die;
99ee5479 421}
fbbb74fb
DO
422
423$dbver = getDatabaseVersion();
4114697d
DO
424echo '<table border=1>';
425echo "<tr><th>Current status</th><td>Data version: ${dbver}<br>Code version: " . CODE_VERSION . "</td></tr>\n";
426
427$path = getDBUpgradePath ($dbver, CODE_VERSION);
428if ($path === NULL)
758fe24c 429{
4114697d 430 echo "<tr><th>Upgrade path</th><td><font color=red>not found</font></td></tr>\n";
d74ae24c
DO
431 echo "<tr><th>Summary</th><td>Check README for more information. RackTables releases prior to 0.16.4 ";
432 echo "must be upgraded to 0.16.4 first.</td></tr>\n";
758fe24c 433}
4114697d 434else
5f4027b8 435{
4114697d
DO
436 if (!count ($path))
437 echo "<tr><th>Summary</th><td>Come back later.</td></tr>\n";
438 else
439 {
440 echo "<tr><th>Upgrade path</th><td>${dbver} &rarr; " . implode (' &rarr; ', $path) . "</td></tr>\n";
441 foreach ($path as $batchid)
442 {
443 executeUpgradeBatch ($batchid);
444 if (isset ($relnotes[$batchid]))
445 echo "<tr><th>Release notes for ${batchid}</th><td>" . $relnotes[$batchid] . "</td></tr>\n";
446 }
447 echo "<tr><th>Summary</th><td>Upgrade complete, it is Ok to <a href='${root}'>enter</a> the system.</td></tr>\n";
448 }
5f4027b8 449}
4114697d 450echo '</table>';
fbbb74fb
DO
451
452?>