9a3f8df39787c71b1c1d6a5de9132e4229156562
[racktables] / upgrade.php
1 <?php
2
3 $relnotes = array
4 (
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>" .
7 "upgrade to 0.17.0. To get things working again, adjust existing secret.php file<br>" .
8 "according to secret-sample.php file provided with 0.17.0 release.<br><br>" .
9 "Another change is the addition of support for file uploads. Files are stored<br>" .
10 "in the database. There are several settings in php.ini which you may need to modify:<br>" .
11 "<ul><li>file_uploads - needs to be On</li>" .
12 "<li>upload_max_filesize - max size for uploaded files</li>" .
13 "<li>post_max_size - max size of all form data submitted via POST (including files)</li></ul><br>" .
14 "Local user accounts used to have 'enabled' flag, which allowed individual blocking and<br>" .
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>",
18 );
19
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.
26 // Returning NULL indicates an error.
27 function getDBUpgradePath ($v1, $v2)
28 {
29 $versionhistory = array
30 (
31 '0.16.4',
32 '0.16.5',
33 '0.16.6',
34 '0.17.0',
35 '0.17.1',
36 '0.17.2',
37 '0.17.3',
38 '0.17.4',
39 '0.17.5',
40 '0.17.6',
41 '0.17.7',
42 '0.17.8',
43 '0.17.9',
44 '0.17.10',
45 '0.18.0',
46 );
47 if (!in_array ($v1, $versionhistory) or !in_array ($v2, $versionhistory))
48 return NULL;
49 $skip = TRUE;
50 $path = NULL;
51 // Now collect all versions > $v1 and <= $v2
52 foreach ($versionhistory as $v)
53 {
54 if ($skip and $v == $v1)
55 {
56 $skip = FALSE;
57 $path = array();
58 continue;
59 }
60 if ($skip)
61 continue;
62 $path[] = $v;
63 if ($v == $v2)
64 break;
65 }
66 return $path;
67 }
68
69 // Upgrade batches are named exactly as the release where they first appear.
70 // That is simple, but seems sufficient for beginning.
71 function executeUpgradeBatch ($batchid)
72 {
73 $query = array();
74 global $dbxlink;
75 switch ($batchid)
76 {
77 case '0.16.5':
78 $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')";
79 $query[] = "update Config set varvalue = '0.16.5' where varname = 'DB_VERSION'";
80 break;
81 case '0.16.6':
82 $query[] = "update Config set varvalue = '0.16.6' where varname = 'DB_VERSION'";
83 break;
84 case '0.17.0':
85 // create tables for storing files (requires InnoDB support)
86 if (!isInnoDBSupported ())
87 {
88 showFailure ("Cannot upgrade because InnoDB tables are not supported by your MySQL server. See the README for details.", __FILE__);
89 die;
90 }
91
92 $query[] = "alter table Chapter change chapter_no id int(10) unsigned NOT NULL auto_increment";
93 $query[] = "alter table Chapter change chapter_name name char(128) NOT NULL";
94 $query[] = "alter table Chapter drop key chapter_name";
95 $query[] = "alter table Chapter add UNIQUE KEY name (name)";
96 $query[] = "alter table Attribute change attr_id id int(10) unsigned NOT NULL auto_increment";
97 $query[] = "alter table Attribute change attr_type type enum('string','uint','float','dict') default NULL";
98 $query[] = "alter table Attribute change attr_name name char(64) default NULL";
99 $query[] = "alter table Attribute drop key attr_name";
100 $query[] = "alter table Attribute add UNIQUE KEY name (name)";
101 $query[] = "alter table AttributeMap change chapter_no chapter_id int(10) unsigned NOT NULL";
102 $query[] = "alter table Dictionary change chapter_no chapter_id int(10) unsigned NOT NULL";
103 // Only after the above call it is Ok to use reloadDictionary()
104 $query = array_merge ($query, reloadDictionary ($batchid));
105 // schema changes for file management
106 $query[] = "
107 CREATE TABLE `File` (
108 `id` int(10) unsigned NOT NULL auto_increment,
109 `name` char(255) NOT NULL,
110 `type` char(255) NOT NULL,
111 `size` int(10) unsigned NOT NULL,
112 `ctime` datetime NOT NULL,
113 `mtime` datetime NOT NULL,
114 `atime` datetime NOT NULL,
115 `contents` longblob NOT NULL,
116 `comment` text,
117 PRIMARY KEY (`id`),
118 UNIQUE KEY `name` (`name`)
119 ) ENGINE=InnoDB";
120 $query[] = "
121 CREATE TABLE `FileLink` (
122 `id` int(10) unsigned NOT NULL auto_increment,
123 `file_id` int(10) unsigned NOT NULL,
124 `entity_type` enum('ipv4net','ipv4rspool','ipv4vs','object','rack','user') NOT NULL default 'object',
125 `entity_id` int(10) NOT NULL,
126 PRIMARY KEY (`id`),
127 UNIQUE KEY `FileLink-unique` (`file_id`,`entity_type`,`entity_id`),
128 KEY `FileLink-file_id` (`file_id`),
129 CONSTRAINT `FileLink-File_fkey` FOREIGN KEY (`file_id`) REFERENCES `File` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
130 ) ENGINE=InnoDB";
131 $query[] = "ALTER TABLE TagStorage MODIFY COLUMN target_realm enum('file','ipv4net','ipv4rspool','ipv4vs','object','rack','user') NOT NULL default 'object'";
132
133 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (24,'no','network security models')";
134 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (25,'no','wireless models')";
135 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,1,0)";
136 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,2,24)";
137 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,3,0)";
138 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,5,0)";
139 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,14,0)";
140 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,16,0)";
141 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,17,0)";
142 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,18,0)";
143 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,20,0)";
144 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,21,0)";
145 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,22,0)";
146 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (798,24,0)";
147 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (965,1,0)";
148 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (965,3,0)";
149 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (965,2,25)";
150 $query[] = 'alter table IPBonds rename to IPv4Allocation';
151 $query[] = 'alter table PortForwarding rename to IPv4NAT';
152 $query[] = 'alter table IPRanges rename to IPv4Network';
153 $query[] = 'alter table IPAddress rename to IPv4Address';
154 $query[] = 'alter table IPLoadBalancer rename to IPv4LB';
155 $query[] = 'alter table IPRSPool rename to IPv4RSPool';
156 $query[] = 'alter table IPRealServer rename to IPv4RS';
157 $query[] = 'alter table IPVirtualService rename to IPv4VS';
158 $query[] = "alter table TagStorage change column target_realm entity_realm enum('file','ipv4net','ipv4vs','ipv4rspool','object','rack','user') NOT NULL default 'object'";
159 $query[] = 'alter table TagStorage change column target_id entity_id int(10) unsigned NOT NULL';
160 $query[] = 'alter table TagStorage drop key entity_tag';
161 $query[] = 'alter table TagStorage drop key target_id';
162 $query[] = 'alter table TagStorage add UNIQUE KEY `entity_tag` (`entity_realm`,`entity_id`,`tag_id`)';
163 $query[] = 'alter table TagStorage add KEY `entity_id` (`entity_id`)';
164 $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')";
165 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_TEXT_ROWS','25','uint','yes','no','Rows for text file preview')";
166 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PREVIEW_TEXT_COLS','80','uint','yes','no','Columns for text file preview')";
167 $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')";
168 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('VENDOR_SIEVE','','string','yes','no','Vendor sieve configuration')";
169 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4LB_LISTSRC','{\$typeid_4}','string','yes','no','List source: IPv4 load balancers')";
170 $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')";
171 $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')";
172 $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')";
173 $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')";
174 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('RACKS_PER_ROW','12','unit','yes','no','Racks per row')";
175 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_PREDICATE_SIEVE','','string','yes','no','Predicate sieve regex(7)')";
176 $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)')";
177 $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')";
178 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_TAGS','yes','string','no','no','Suggest tags in list filter')";
179 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('FILTER_SUGGEST_PREDICATES','yes','string','no','no','Suggest predicates in list filter')";
180 $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')";
181 $query[] = "delete from Config where varname = 'USER_AUTH_SRC'";
182 $query[] = "delete from Config where varname = 'COOKIE_TTL'";
183 $query[] = "delete from Config where varname = 'rtwidth_0'";
184 $query[] = "delete from Config where varname = 'rtwidth_1'";
185 $query[] = "delete from Config where varname = 'rtwidth_2'";
186 $query[] = "delete from Config where varname = 'NAMEFUL_OBJTYPES'";
187 $query[] = "delete from Config where varname = 'REQUIRE_ASSET_TAG_FOR'";
188 $query[] = "delete from Config where varname = 'IPV4_PERFORMERS'";
189 $query[] = "delete from Config where varname = 'NATV4_PERFORMERS'";
190 $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";
191 $result = $dbxlink->query ("select user_id, user_name, user_realname from UserAccount where user_enabled = 'no'");
192 while ($row = $result->fetch (PDO::FETCH_ASSOC))
193 $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'";
194 $query[] = "update Script set script_text = NULL where script_name = 'RackCodeCache'";
195 unset ($result);
196 $query[] = "alter table UserAccount drop column user_enabled";
197
198 $query[] = "CREATE TABLE RackRow ( id int(10) unsigned NOT NULL auto_increment, name char(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM";
199
200 $result = $dbxlink->query ("select dict_key, dict_value from Dictionary where chapter_no = 3");
201 while($row = $result->fetch(PDO::FETCH_NUM))
202 $query[] = "insert into RackRow set id=${row[0]}, name='${row[1]}'";
203 unset ($result);
204 $query[] = "delete from Dictionary where chapter_id = 3";
205 $query[] = "delete from Chapter where id = 3";
206 $query[] = "
207 CREATE TABLE `LDAPCache` (
208 `presented_username` char(64) NOT NULL,
209 `successful_hash` char(40) NOT NULL,
210 `first_success` timestamp NOT NULL default CURRENT_TIMESTAMP,
211 `last_retry` timestamp NOT NULL default '0000-00-00 00:00:00',
212 `displayed_name` char(128) default NULL,
213 `memberof` text,
214 UNIQUE KEY `presented_username` (`presented_username`),
215 KEY `scanidx` (`presented_username`,`successful_hash`)
216 ) ENGINE=InnoDB;";
217 $query[] = "alter table UserAccount modify column user_password_hash char(40) NULL";
218 $query[] = 'ALTER TABLE Rack DROP COLUMN deleted';
219 $query[] = 'ALTER TABLE RackHistory DROP COLUMN deleted';
220 $query[] = 'ALTER TABLE RackObject DROP COLUMN deleted';
221 $query[] = 'ALTER TABLE RackObjectHistory DROP COLUMN deleted';
222 // Can't be added straight due to many duplicates, even in "dictbase" data.
223 $result = $dbxlink->query ('SELECT type1, type2, count(*) - 1 as excess FROM PortCompat GROUP BY type1, type2 HAVING excess > 0');
224 while ($row = $result->fetch (PDO::FETCH_ASSOC))
225 $query[] = "DELETE FROM PortCompat WHERE type1 = ${row['type1']} AND type2 = ${row['type2']} limit ${row['excess']}";
226 unset ($result);
227 $query[] = 'ALTER TABLE PortCompat DROP KEY type1';
228 $query[] = 'ALTER TABLE PortCompat ADD UNIQUE `type1_2` (type1, type2)';
229 $query[] = "UPDATE Config SET varvalue = '0.17.0' WHERE varname = 'DB_VERSION'";
230
231 break;
232 case '0.17.1':
233 $query[] = "ALTER TABLE Dictionary DROP KEY `chap_to_key`";
234 $query = array_merge ($query, reloadDictionary ($batchid));
235 // Token set has changed, so the cache isn't valid any more.
236 $query[] = "UPDATE Script SET script_text = NULL WHERE script_name = 'RackCodeCache'";
237 $query[] = "UPDATE Config SET varvalue = '0.17.1' WHERE varname = 'DB_VERSION'";
238 break;
239 case '0.17.2':
240 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (26,'no','fibre channel switch models')";
241 $query = array_merge ($query, reloadDictionary ($batchid));
242 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (1055,2,26)";
243 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('DEFAULT_SNMP_COMMUNITY','public','string','no','no','Default SNMP Community string')";
244 // wipe irrelevant records (ticket:250)
245 $query[] = "DELETE FROM TagStorage WHERE entity_realm = 'file' AND entity_id NOT IN (SELECT id FROM File)";
246 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('IPV4_ENABLE_KNIGHT','yes','string','no','no','Enable IPv4 knight feature')";
247 $query[] = "ALTER TABLE IPv4Network ADD COLUMN comment text AFTER name";
248 $query[] = "ALTER TABLE Port ADD INDEX comment (reservation_comment)";
249 $query[] = "ALTER TABLE Port DROP KEY l2address"; // UNIQUE
250 $query[] = "ALTER TABLE Port ADD KEY (l2address)"; // not UNIQUE
251 $query[] = "ALTER TABLE Port DROP KEY object_id";
252 $query[] = "ALTER TABLE Port ADD UNIQUE KEY per_object (object_id, name, type)";
253 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (20,1083)";
254 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (21,1083)";
255 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1077,1077)";
256 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1083,20)";
257 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1083,21)";
258 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1083,1083)";
259 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (1087,1087)";
260 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (27,'no','PDU models')";
261 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (2,2,27)";
262 $query[] = "UPDATE Config SET varvalue = '0.17.2' WHERE varname = 'DB_VERSION'";
263 break;
264 case '0.17.3':
265 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('TAGS_TOPLIST_SIZE','50','uint','yes','no','Tags top list size')";
266 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('TAGS_QUICKLIST_SIZE','20','uint','no','no','Tags quick list size')";
267 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('TAGS_QUICKLIST_THRESHOLD','50','uint','yes','no','Tags quick list threshold')";
268 $query[] = "ALTER TABLE AttributeMap MODIFY COLUMN chapter_id int(10) unsigned NULL";
269 $query[] = "UPDATE AttributeMap SET chapter_id = NULL WHERE attr_id IN (SELECT id FROM Attribute WHERE type != 'dict')";
270 // ticket:239
271 $query[] = 'UPDATE AttributeValue SET uint_value = 1018 WHERE uint_value = 731 AND attr_id IN (SELECT attr_id FROM AttributeMap WHERE chapter_id = 12)';
272 $query[] = 'DELETE FROM Dictionary WHERE dict_key = 731';
273 $query = array_merge ($query, reloadDictionary ($batchid));
274 $query[] = "UPDATE Config SET vartype='uint' WHERE varname='RACKS_PER_ROW'";
275 $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')";
276 $query[] = "UPDATE Config SET varvalue = '0.17.3' WHERE varname = 'DB_VERSION'";
277 break;
278 case '0.17.4':
279 $query[] = "ALTER TABLE Link ENGINE=InnoDB";
280 $query[] = "ALTER TABLE Port ENGINE=InnoDB";
281 $query[] = "ALTER TABLE IPv4RS ENGINE=InnoDB";
282 $query[] = "ALTER TABLE IPv4RSPool ENGINE=InnoDB";
283 $query[] = "ALTER TABLE AttributeValue ENGINE=InnoDB";
284 $query[] = "ALTER TABLE RackObject ENGINE=InnoDB";
285 $query[] = "ALTER TABLE IPv4NAT ENGINE=InnoDB";
286 $query[] = "ALTER TABLE IPv4LB ENGINE=InnoDB";
287 $query[] = "ALTER TABLE IPv4VS ENGINE=InnoDB";
288 $query[] = "DELETE FROM IPv4RS WHERE rspool_id NOT IN (SELECT id FROM IPv4RSPool)";
289 $query[] = "ALTER TABLE Link ADD CONSTRAINT `Link-FK-b` FOREIGN KEY (portb) REFERENCES Port (id)";
290 $query[] = "ALTER TABLE Link ADD CONSTRAINT `Link-FK-a` FOREIGN KEY (porta) REFERENCES Port (id)";
291 $query[] = "ALTER TABLE IPv4RS ADD CONSTRAINT `IPv4RS-FK` FOREIGN KEY (rspool_id) REFERENCES IPv4RSPool (id) ON DELETE CASCADE";
292 $query[] = "ALTER TABLE AttributeValue ADD CONSTRAINT `AttributeValue-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
293 $query[] = "ALTER TABLE IPv4NAT ADD CONSTRAINT `IPv4NAT-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
294 $query[] = "ALTER TABLE Port ADD CONSTRAINT `Port-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
295 $query[] = "ALTER TABLE IPv4LB ADD CONSTRAINT `IPv4LB-FK-rspool_id` FOREIGN KEY (rspool_id) REFERENCES IPv4RSPool (id)";
296 $query[] = "ALTER TABLE IPv4LB ADD CONSTRAINT `IPv4LB-FK-object_id` FOREIGN KEY (object_id) REFERENCES RackObject (id)";
297 $query[] = "ALTER TABLE IPv4LB ADD CONSTRAINT `IPv4LB-FK-vs_id` FOREIGN KEY (vs_id) REFERENCES IPv4VS (id)";
298 $query = array_merge ($query, reloadDictionary ($batchid));
299 $query[] = "UPDATE Config SET varvalue = '0.17.4' WHERE varname = 'DB_VERSION'";
300 break;
301 case '0.17.5':
302 $query[] = "ALTER TABLE TagTree ENGINE=InnoDB";
303 $query[] = "ALTER TABLE TagStorage ENGINE=InnoDB";
304 $query[] = "ALTER TABLE TagStorage ADD CONSTRAINT `TagStorage-FK-tag_id` FOREIGN KEY (tag_id) REFERENCES TagTree (id)";
305 $query[] = "ALTER TABLE TagTree ADD CONSTRAINT `TagTree-K-parent_id` FOREIGN KEY (parent_id) REFERENCES TagTree (id)";
306 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (21,1195)';
307 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (22,1196)';
308 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (23,1196)';
309 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (20,1195)';
310 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (25,1202)';
311 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (26,1202)';
312 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (27,1204)';
313 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (28,1204)';
314 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1083,1195)';
315 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1084,1084)';
316 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1195,20)';
317 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1195,21)';
318 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1195,1083)';
319 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1195,1195)';
320 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1196,22)';
321 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1196,23)';
322 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1196,1196)';
323 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1197,1197)';
324 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1198,1199)';
325 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1199,1198)';
326 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1200,1200)';
327 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1201,1201)';
328 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1202,25)';
329 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1202,26)';
330 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1202,1202)';
331 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1203,1203)';
332 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1204,27)';
333 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1204,28)';
334 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1204,1204)';
335 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1205,1205)';
336 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1206,1207)';
337 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1207,1206)';
338 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1316,1316)';
339 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (16, 1322)';
340 $query[] = 'INSERT INTO PortCompat (type1, type2) VALUES (1322, 16)';
341 $query[] = 'DELETE FROM PortCompat WHERE type1 = 16 AND type2 = 16';
342 for ($i = 1209; $i <= 1300; $i++)
343 $query[] = "INSERT INTO PortCompat (type1, type2) VALUES (${i}, ${i})";
344 $query[] = "
345 CREATE TABLE `PortInnerInterface` (
346 `id` int(10) unsigned NOT NULL,
347 `iif_name` char(16) NOT NULL,
348 PRIMARY KEY (`id`),
349 UNIQUE KEY `iif_name` (`iif_name`)
350 ) ENGINE=InnoDB";
351 $query[] = "INSERT INTO `PortInnerInterface` VALUES (1,'hardwired')";
352 $query[] = "INSERT INTO `PortInnerInterface` VALUES (2,'SFP-100')";
353 $query[] = "INSERT INTO `PortInnerInterface` VALUES (3,'GBIC')";
354 $query[] = "INSERT INTO `PortInnerInterface` VALUES (4,'SFP-1000')";
355 $query[] = "INSERT INTO `PortInnerInterface` VALUES (5,'XENPAK')";
356 $query[] = "INSERT INTO `PortInnerInterface` VALUES (6,'X2')";
357 $query[] = "INSERT INTO `PortInnerInterface` VALUES (7,'XPAK')";
358 $query[] = "INSERT INTO `PortInnerInterface` VALUES (8,'XFP')";
359 $query[] = "INSERT INTO `PortInnerInterface` VALUES (9,'SFP+')";
360 $query[] = "
361 CREATE TABLE `PortInterfaceCompat` (
362 `iif_id` int(10) unsigned NOT NULL,
363 `oif_id` int(10) unsigned NOT NULL,
364 UNIQUE KEY `pair` (`iif_id`,`oif_id`),
365 CONSTRAINT `PortInterfaceCompat-FK-iif_id` FOREIGN KEY (`iif_id`) REFERENCES `PortInnerInterface` (`id`)
366 ) ENGINE=InnoDB";
367 $query[] = "ALTER TABLE Port ADD COLUMN iif_id int unsigned NOT NULL AFTER name"; // will set iif_id to 0
368 $query[] = "UPDATE Port SET iif_id = 2 WHERE type = 1208";
369 $query[] = "UPDATE Port SET iif_id = 3 WHERE type = 1078";
370 $query[] = "UPDATE Port SET iif_id = 4 WHERE type = 1077";
371 $query[] = "UPDATE Port SET iif_id = 5 WHERE type = 1079";
372 $query[] = "UPDATE Port SET iif_id = 6 WHERE type = 1080";
373 $query[] = "UPDATE Port SET iif_id = 7 WHERE type = 1081";
374 $query[] = "UPDATE Port SET iif_id = 8 WHERE type = 1082";
375 $query[] = "UPDATE Port SET iif_id = 9 WHERE type = 1084";
376 $query[] = "UPDATE Port SET iif_id = 1 WHERE iif_id = 0";
377 $query[] = 'ALTER TABLE Port ADD UNIQUE `object_iif_oif_name` (object_id, iif_id, type, name)';
378 $query[] = 'ALTER TABLE Port DROP KEY `per_object`';
379 $base1000 = array (24, 34, 1202, 1203, 1204, 1205, 1206, 1207);
380 $base10000 = array (30, 35, 36, 37, 38, 39, 40);
381 $PICdata = array
382 (
383 1 => array (16, 19, 24, 29, 31, 33, 446, 681, 682, 1322),
384 2 => array (1208, 1195, 1196, 1197, 1198, 1199, 1200, 1201),
385 3 => array_merge (array (1078), $base1000),
386 4 => array_merge (array (1077), $base1000),
387 5 => array_merge (array (1079), $base10000),
388 6 => array_merge (array (1080), $base10000),
389 7 => array_merge (array (1081), $base10000),
390 8 => array_merge (array (1082), $base10000),
391 9 => array_merge (array (1084), $base10000),
392 );
393 // make sure all IIF/OIF pairs referenced from Port exist in PortInterfaceCompat before enabling FK
394 // iif_id doesn't exist at this point
395 $result = $dbxlink->query ('SELECT DISTINCT type FROM Port WHERE type NOT IN (1208, 1078, 1077, 1079, 1080, 1081, 1082, 1084)');
396 while ($row = $result->fetch (PDO::FETCH_ASSOC))
397 if (FALSE === array_search ($row['type'], $PICdata[1]))
398 array_push ($PICdata[1], $row['type']);
399 unset ($result);
400 foreach ($PICdata as $iif_id => $oif_ids)
401 foreach ($oif_ids as $oif_id)
402 $query[] = "INSERT INTO PortInterfaceCompat (iif_id, oif_id) VALUES (${iif_id}, ${oif_id})";
403 $query[] = "ALTER TABLE Port ADD CONSTRAINT `Port-FK-iif-oif` FOREIGN KEY (`iif_id`, `type`) REFERENCES `PortInterfaceCompat` (`iif_id`, `oif_id`)";
404 $query[] = 'UPDATE Port SET type = 1322 WHERE type = 16 AND (SELECT objtype_id FROM RackObject WHERE id = object_id) IN (2, 12)';
405 $query = array_merge ($query, reloadDictionary ($batchid));
406 $query[] = "DELETE FROM Config WHERE varname = 'default_port_type'";
407 $query[] = "INSERT INTO Config VALUES ('DEFAULT_PORT_IIF_ID','1','uint','no','no','Default port inner interface ID')";
408 $query[] = "INSERT INTO Config VALUES ('DEFAULT_PORT_OIF_IDS','1=24; 3=1078; 4=1077; 5=1079; 6=1080; 8=1082; 9=1084','string','no','no','Default port outer interface IDs')";
409 $query[] = "INSERT INTO Config VALUES ('IPV4_TREE_RTR_AS_CELL','yes','string','no','no','Show full router info for each network in IPv4 tree view')";
410 $query[] = "UPDATE Chapter SET name = 'PortOuterInterface' WHERE id = 2";
411 // remap refs to duplicate records, which will be discarded (ticket:286)
412 $query[] = 'UPDATE AttributeValue SET uint_value = 147 WHERE uint_value = 1020 AND attr_id = 2';
413 $query[] = 'UPDATE AttributeValue SET uint_value = 377 WHERE uint_value = 1021 AND attr_id = 2';
414 $query[] = 'INSERT INTO AttributeMap (objtype_id, attr_id) VALUES (2, 1), (2, 3), (2, 5)';
415 $query[] = "UPDATE Config SET varvalue = '0.17.5' WHERE varname = 'DB_VERSION'";
416 break;
417 case '0.17.6':
418 $query[] = "INSERT INTO `Chapter` (`id`, `sticky`, `name`) VALUES (28,'no','Voice/video hardware')";
419 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (1323,1,NULL)";
420 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (1323,2,28)";
421 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (1323,3,NULL)";
422 $query[] = "INSERT INTO `AttributeMap` (`objtype_id`, `attr_id`, `chapter_id`) VALUES (1323,5,NULL)";
423 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, description) VALUES ('PROXIMITY_RANGE','0','uint','yes','no','Proximity range (0 is current rack only)')";
424 $query = array_merge ($query, reloadDictionary ($batchid));
425 $query[] = "UPDATE Config SET varvalue = '0.17.6' WHERE varname = 'DB_VERSION'";
426 break;
427 case '0.17.7':
428 $query[] = "UPDATE Config SET varvalue = '0.17.7' WHERE varname = 'DB_VERSION'";
429 break;
430 case '0.17.8':
431 $query = array_merge ($query, reloadDictionary ($batchid));
432 $query[] = "ALTER TABLE TagTree DROP COLUMN valid_realm";
433 $query[] = "UPDATE Config SET varvalue = '0.17.8' WHERE varname = 'DB_VERSION'";
434 break;
435 case '0.17.9':
436 $query[] = "ALTER table Config add `is_userdefined` enum('yes','no') NOT NULL default 'no' AFTER `is_hidden`";
437 $query[] = "
438 CREATE TABLE `UserConfig` (
439 `varname` char(32) NOT NULL,
440 `varvalue` char(255) NOT NULL,
441 `user` char(64) NOT NULL,
442 UNIQUE KEY `user_varname` (`user`,`varname`)
443 ) TYPE=InnoDB";
444 $query[] = "UPDATE Config SET is_userdefined = 'yes' WHERE varname IN
445 (
446 'MASSCOUNT',
447 'MAXSELSIZE',
448 'ROW_SCALE',
449 'PORTS_PER_ROW',
450 'IPV4_ADDRS_PER_PAGE',
451 'DEFAULT_RACK_HEIGHT',
452 'DEFAULT_SLB_VS_PORT',
453 'DEFAULT_SLB_RS_PORT',
454 'DETECT_URLS',
455 'RACK_PRESELECT_THRESHOLD',
456 'DEFAULT_IPV4_RS_INSERVICE',
457 'DEFAULT_OBJECT_TYPE',
458 'SHOW_EXPLICIT_TAGS',
459 'SHOW_IMPLICIT_TAGS',
460 'SHOW_AUTOMATIC_TAGS',
461 'IPV4_AUTO_RELEASE',
462 'SHOW_LAST_TAB',
463 'EXT_IPV4_VIEW',
464 'TREE_THRESHOLD',
465 'ADDNEW_AT_TOP',
466 'IPV4_TREE_SHOW_USAGE',
467 'PREVIEW_TEXT_MAXCHARS',
468 'PREVIEW_TEXT_ROWS',
469 'PREVIEW_TEXT_COLS',
470 'PREVIEW_IMAGE_MAXPXS',
471 'VENDOR_SIEVE',
472 'RACKS_PER_ROW'
473 )";
474 $query[] = "INSERT INTO Config VLAUES ('IPV4_TREE_SHOW_VLAN','yes','string','no','no','yes','Show VLAN for each network in IPv4 tree')";
475 $query[] = "UPDATE Config SET varvalue = '0.17.9' WHERE varname = 'DB_VERSION'";
476 break;
477 case '0.17.10':
478 $query = array_merge ($query, reloadDictionary ($batchid));
479 $query[] = "ALTER TABLE MountOperation ADD KEY (object_id)";
480 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('STATIC_FILTER','yes','string','no','no','yes','Enable Filter Caching');";
481 $query[] = "UPDATE Config SET varvalue = '0.17.10' WHERE varname = 'DB_VERSION'";
482 case '0.17.11':
483 $query = array_merge ($query, reloadDictionary ($batchid));
484 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('ENABLE_BULKPORT_FORM','yes','string','no','no','yes','Enable \"Bulk Port\" form');";
485 $query[] = "UPDATE Config SET varvalue = '0.17.11' WHERE varname = 'DB_VERSION'";
486 $query[] = "DELETE attributevalue FROM attributevalue join attribute where attributevalue.attr_id = attribute.id and attribute.`type` = 'dict' and attributevalue.uint_value = 0;";
487 case '0.18.0':
488 $query = array_merge ($query, reloadDictionary ($batchid));
489 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('VLANSWITCH_LISTSRC', '', 'string', 'yes', 'no', 'yes', 'List of VLAN running switches')";
490 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('VLANIPV4NET_LISTSRC', '', 'string', 'yes', 'no', 'yes', 'List of VLAN-based IPv4 networks')";
491 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('DEFAULT_VDOM_ID','','uint','yes','no','yes','Default VLAN domain ID')";
492 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('DEFAULT_VST_ID','','uint','yes','no','yes','Default VLAN switch template ID')";
493 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('8021Q_DEPLOY_MINAGE','300','uint','no','no','no','802.1Q deploy minimum age')";
494 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('8021Q_DEPLOY_MAXAGE','3600','uint','no','no','no','802.1Q deploy maximum age')";
495 $query[] = "INSERT INTO `Config` (varname, varvalue, vartype, emptyok, is_hidden, is_userdefined, description) VALUES ('8021Q_DEPLOY_RETRY','10800','uint','no','no','no','802.1Q deploy retry timer')";
496 $query[] = "ALTER TABLE IPv4Network ENGINE=InnoDB";
497 for ($i = 1; $i <= 4094; $i++)
498 usePreparedInsertBlade ('VLANValidID', array ('vlan_id' => $i));
499 $query[] = "UPDATE Config SET varvalue = '0.18.0' WHERE varname = 'DB_VERSION'";
500 default:
501 showFailure ("executeUpgradeBatch () failed, because batch '${batchid}' isn't defined", __FILE__);
502 die;
503 break;
504 }
505 $failures = array();
506 echo "<tr><th>Executing batch '${batchid}'</th><td>";
507 foreach ($query as $q)
508 {
509 $result = $dbxlink->query ($q);
510 if ($result == NULL)
511 {
512 $errorInfo = $dbxlink->errorInfo();
513 $failures[] = array ($q, $errorInfo[2]);
514 }
515 }
516 if (!count ($failures))
517 echo "<strong><font color=green>done</font></strong>";
518 else
519 {
520 echo "<strong><font color=red>The following queries failed:</font></strong><br><pre>";
521 foreach ($failures as $f)
522 {
523 list ($q, $i) = $f;
524 echo "${q} -- ${i}\n";
525 }
526 echo "</pre>";
527 }
528 echo '</td></tr>';
529 }
530
531 // ******************************************************************
532 //
533 // Execution starts here
534 //
535 // ******************************************************************
536
537 // a clone of showError() to drop dependency on interface.php
538 function showFailure ($info = '', $location = 'N/A')
539 {
540 if (preg_match ('/\.php$/', $location))
541 $location = basename ($location);
542 elseif ($location != 'N/A')
543 $location = $location . '()';
544 echo "<div class=msg_error>An error has occured in [${location}]. ";
545 if (empty ($info))
546 echo 'No additional information is available.';
547 else
548 echo "Additional information:<br><p>\n<pre>\n${info}\n</pre></p>";
549 echo "This failure is most probably fatal.<br></div>\n";
550 }
551
552 require_once 'inc/config.php'; // for CODE_VERSION
553 require_once 'inc/database.php'; // for getDatabaseVersion()
554 require_once 'inc/dictionary.php';
555 // Enforce default value for now, releases prior to 0.17.0 didn't support 'httpd' auth source.
556 $user_auth_src = 'database';
557
558 if (file_exists ('inc/secret.php'))
559 require_once 'inc/secret.php';
560 else
561 die ("Database connection parameters are read from inc/secret.php file, " .
562 "which cannot be found.\nCopy provided inc/secret-sample.php to " .
563 "inc/secret.php and modify to your setup.\n\nThen reload the page.");
564
565 try
566 {
567 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
568 }
569 catch (PDOException $e)
570 {
571 die ("Database connection failed:\n\n" . $e->getMessage());
572 }
573
574 // Now we need to be sure that the current user is the administrator.
575 // The rest doesn't matter within this context.
576
577 function authenticate_admin ($username, $password)
578 {
579 global $dbxlink;
580 $hash = sha1 ($password);
581 $query = "select count(*) from UserAccount where user_id = 1 and user_name = '${username}' and user_password_hash = '${hash}'";
582 if (($result = $dbxlink->query ($query)) == NULL)
583 die ('SQL query failed in ' . __FUNCTION__);
584 $rows = $result->fetchAll (PDO::FETCH_NUM);
585 return $rows[0][0] == 1;
586 }
587
588 switch ($user_auth_src)
589 {
590 case 'database':
591 case 'ldap': // authenticate against DB as well
592 if
593 (
594 !isset ($_SERVER['PHP_AUTH_USER']) or
595 !strlen ($_SERVER['PHP_AUTH_USER']) or
596 !isset ($_SERVER['PHP_AUTH_PW']) or
597 !strlen ($_SERVER['PHP_AUTH_PW']) or
598 !authenticate_admin (escapeString ($_SERVER['PHP_AUTH_USER']), escapeString ($_SERVER['PHP_AUTH_PW']))
599 )
600 {
601 header ('WWW-Authenticate: Basic realm="RackTables upgrade"');
602 header ('HTTP/1.0 401 Unauthorized');
603 showFailure ('You must be authenticated as an administrator to complete the upgrade.', __FILE__);
604 die;
605 }
606 break; // cleared
607 case 'httpd':
608 if
609 (
610 !isset ($_SERVER['REMOTE_USER']) or
611 !strlen ($_SERVER['REMOTE_USER'])
612 )
613 {
614 showFailure ('System misconfiguration. The web-server didn\'t authenticate the user, although ought to do.');
615 die;
616 }
617 break; // cleared
618 default:
619 showFailure ('authentication source misconfiguration', __FILE__);
620 die;
621 }
622
623 $dbver = getDatabaseVersion();
624 echo '<table border=1>';
625 echo "<tr><th>Current status</th><td>Data version: ${dbver}<br>Code version: " . CODE_VERSION . "</td></tr>\n";
626
627 $path = getDBUpgradePath ($dbver, CODE_VERSION);
628 if ($path === NULL)
629 {
630 echo "<tr><th>Upgrade path</th><td><font color=red>not found</font></td></tr>\n";
631 echo "<tr><th>Summary</th><td>Check README for more information. RackTables releases prior to 0.16.4 ";
632 echo "must be upgraded to 0.16.4 first.</td></tr>\n";
633 }
634 else
635 {
636 if (!count ($path))
637 echo "<tr><th>Summary</th><td>Come back later.</td></tr>\n";
638 else
639 {
640 echo "<tr><th>Upgrade path</th><td>${dbver} &rarr; " . implode (' &rarr; ', $path) . "</td></tr>\n";
641 foreach ($path as $batchid)
642 {
643 executeUpgradeBatch ($batchid);
644 if (isset ($relnotes[$batchid]))
645 echo "<tr><th>Release notes for ${batchid}</th><td>" . $relnotes[$batchid] . "</td></tr>\n";
646 }
647 echo "<tr><th>Summary</th><td>Upgrade complete, it is Ok to <a href='index.php'>enter</a> the system.</td></tr>\n";
648 }
649 }
650 echo '</table>';
651
652 ?>