r2039 + fix unescaped string
[racktables] / inc / ophandlers.php
CommitLineData
b325120a 1<?php
e673ee24
DO
2/*
3*
4* This file is a library of operation handlers for RackTables.
5*
6*/
7
17c32f4c
DO
8// This function assures that specified argument was passed
9// and is a number greater than zero.
2c6c7645 10function assertUIntArg ($argname, $caller = 'N/A', $allow_zero = FALSE)
17c32f4c
DO
11{
12 if (!isset ($_REQUEST[$argname]))
13 {
2c6c7645 14 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
15 die();
16 }
17 if (!is_numeric ($_REQUEST[$argname]))
18 {
2c6c7645 19 showError ("Parameter '${argname}' is not a number (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
20 die();
21 }
22 if ($_REQUEST[$argname] < 0)
23 {
2c6c7645 24 showError ("Parameter '${argname}' is less than zero (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
25 die();
26 }
27 if (!$allow_zero and $_REQUEST[$argname] == 0)
28 {
2c6c7645 29 showError ("Parameter '${argname}' is equal to zero (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
30 die();
31 }
32}
33
34// This function assures that specified argument was passed
35// and is a non-empty string.
2c6c7645 36function assertStringArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
17c32f4c
DO
37{
38 if (!isset ($_REQUEST[$argname]))
39 {
2c6c7645 40 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
41 die();
42 }
43 if (!is_string ($_REQUEST[$argname]))
44 {
2c6c7645 45 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
46 die();
47 }
48 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
49 {
2c6c7645 50 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
51 die();
52 }
53}
54
2c6c7645 55function assertBoolArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
17c32f4c
DO
56{
57 if (!isset ($_REQUEST[$argname]))
58 {
2c6c7645 59 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
60 die();
61 }
62 if (!is_string ($_REQUEST[$argname]) or $_REQUEST[$argname] != 'on')
63 {
2c6c7645 64 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
65 die();
66 }
67 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
68 {
2c6c7645 69 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
70 die();
71 }
72}
73
2c6c7645 74function assertIPv4Arg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
17c32f4c 75{
2c6c7645 76 assertStringArg ($argname, $caller, $ok_if_empty);
17c32f4c
DO
77 if (!empty ($_REQUEST[$argname]) and long2ip (ip2long ($_REQUEST[$argname])) !== $_REQUEST[$argname])
78 {
2c6c7645 79 showError ("IPv4 address validation failed for value '" . $_REQUEST[$argname] . "' (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
80 die();
81 }
82}
83
e673ee24
DO
84function addPortForwarding ()
85{
86 global $root, $pageno, $tabno;
87
2c6c7645
DO
88 assertUIntArg ('object_id', __FUNCTION__);
89 assertIPv4Arg ('localip', __FUNCTION__);
90 assertIPv4Arg ('remoteip', __FUNCTION__);
91 assertUIntArg ('localport', __FUNCTION__);
9bea3a8b 92 assertStringArg ('proto', __FUNCTION__);
2c6c7645 93 assertStringArg ('description', __FUNCTION__, TRUE);
e673ee24 94 $object_id = $_REQUEST['object_id'];
e673ee24 95 $remoteport = $_REQUEST['remoteport'];
31fc4157 96 if (empty ($remoteport))
f75eb878 97 $remoteport = $_REQUEST['localport'];
e673ee24
DO
98
99 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
100
9bea3a8b
DO
101 $error = newPortForwarding
102 (
103 $object_id,
104 $_REQUEST['localip'],
105 $_REQUEST['localport'],
106 $_REQUEST['remoteip'],
107 $remoteport,
108 $_REQUEST['proto'],
109 $_REQUEST['description']
110 );
e673ee24
DO
111
112 if ($error == '')
113 return "${retpage}&message=".urlencode('Port forwarding successfully added.');
114 else
115 {
116 return "${retpage}&error=".urlencode($error);
117 }
118
119}
120
121function delPortForwarding ()
122{
123 global $root, $pageno, $tabno;
124
125 $object_id = $_REQUEST['object_id'];
126 $localip = $_REQUEST['localip'];
127 $remoteip = $_REQUEST['remoteip'];
128 $localport = $_REQUEST['localport'];
129 $remoteport = $_REQUEST['remoteport'];
130 $proto = $_REQUEST['proto'];
e673ee24
DO
131
132 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
133
134 $error=deletePortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto);
135 if ($error == '')
136 return "${retpage}&message=".urlencode('Port forwarding successfully deleted.');
137 else
138 {
139 return "${retpage}&error=".urlencode($error);
140 }
141
142}
143
144function updPortForwarding ()
145{
146 global $root, $pageno, $tabno;
147
148 $object_id = $_REQUEST['object_id'];
149 $localip = $_REQUEST['localip'];
150 $remoteip = $_REQUEST['remoteip'];
151 $localport = $_REQUEST['localport'];
152 $remoteport = $_REQUEST['remoteport'];
153 $proto = $_REQUEST['proto'];
154 $description = $_REQUEST['description'];
155
156 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
157
158 $error=updatePortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto, $description);
159 if ($error == '')
160 return "${retpage}&message=".urlencode('Port forwarding successfully updated.');
161 else
162 {
163 return "${retpage}&error=".urlencode($error);
164 }
165
166}
167
e673ee24
DO
168function addPortForObject ()
169{
170 global $root, $pageno, $tabno;
171
172 $object_id = $_REQUEST['object_id'];
173 $port_name = $_REQUEST['port_name'];
174 $port_l2address = $_REQUEST['port_l2address'];
175 $port_label = $_REQUEST['port_label'];
176 $port_type_id = $_REQUEST['port_type_id'];
177
178
179 if ($port_name == '')
180 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode('Port name cannot be empty');
181 else
182 {
183 $error = commitAddPort ($object_id, $port_name, $port_type_id, $port_label, $port_l2address);
184 if ($error != '')
185 {
186 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
187 }
188 else
189 {
190 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name added successfully");
191 }
192 }
193
194}
195
196function editPortForObject ()
197{
198 global $root, $pageno, $tabno;
199
2c6c7645
DO
200 assertUIntArg ('port_id', __FUNCTION__);
201 assertUIntArg ('object_id', __FUNCTION__);
202 assertStringArg ('name', __FUNCTION__);
e673ee24
DO
203 $port_id = $_REQUEST['port_id'];
204 $object_id = $_REQUEST['object_id'];
205 $port_name = $_REQUEST['name'];
206 $port_l2address = $_REQUEST['l2address'];
207 $port_label = $_REQUEST['label'];
208 if (isset ($_REQUEST['reservation_comment']) and !empty ($_REQUEST['reservation_comment']))
209 $port_rc = '"' . $_REQUEST['reservation_comment'] . '"';
210 else
211 $port_rc = 'NULL';
212
213 if ($port_name == '')
214 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode('Port name cannot be empty');
215 else
216 {
217 $error = commitUpdatePort ($port_id, $port_name, $port_label, $port_l2address, $port_rc);
218 if ($error != '')
219 {
220 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
221 }
222 else
223 {
224 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name updated successfully");
225 }
226 }
227
228}
229
230function delPortFromObject ()
231{
232 global $root, $pageno, $tabno;
233
234 $port_id = $_REQUEST['port_id'];
235 $port_name = $_REQUEST['port_name'];
236 $object_id = $_REQUEST['object_id'];
237 $error = delObjectPort($port_id);
238
239 if ($error != '')
240 {
241 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
242 }
243 else
244 {
245 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name deleted successfully");
246 }
247}
248
249function linkPortForObject ()
250{
251 global $root, $pageno, $tabno;
252
253 $port_id = $_REQUEST['port_id'];
254 $remote_port_id = $_REQUEST['remote_port_id'];
255 $object_id = $_REQUEST['object_id'];
256 $port_name = $_REQUEST['port_name'];
257 $remote_port_name = $_REQUEST['remote_port_name'];
258 $remote_object_name = $_REQUEST['remote_object_name'];
259
260 $error = linkPorts($port_id, $remote_port_id);
261 if ($error != '')
262 {
263 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
264 }
265 else
266 {
267 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name successfully linked with port $remote_port_name at object $remote_object_name");
268 }
269}
270
271function unlinkPortForObject ()
272{
273 global $root, $pageno, $tabno;
274
275 $port_id = $_REQUEST['port_id'];
276 $object_id = $_REQUEST['object_id'];
277 $port_name = $_REQUEST['port_name'];
278 $remote_port_name = $_REQUEST['remote_port_name'];
279 $remote_object_name = $_REQUEST['remote_object_name'];
280
281 $error = unlinkPort($port_id);
282 if ($error != '')
283 {
284 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
285 }
286 else
287 {
288 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name successfully unlinked from port $remote_port_name at object $remote_object_name");
289 }
290}
291
292function addMultiPorts ()
293{
294 global $root, $pageno, $tabno;
295 // Parse data.
2c6c7645
DO
296 assertStringArg ('format', __FUNCTION__);
297 assertStringArg ('input', __FUNCTION__);
298 assertUIntArg ('port_type', __FUNCTION__);
299 assertUIntArg ('object_id', __FUNCTION__);
e673ee24
DO
300 $format = $_REQUEST['format'];
301 $port_type = $_REQUEST['port_type'];
302 $object_id = $_REQUEST['object_id'];
303 // Input lines are escaped, so we have to explode and to chop by 2-char
304 // \n and \r respectively.
305 $lines1 = explode ('\n', $_REQUEST['input']);
306 foreach ($lines1 as $line)
307 {
308 $parts = explode ('\r', $line);
309 reset ($parts);
310 if (empty ($parts[0]))
311 continue;
312 else
313 $lines2[] = rtrim ($parts[0]);
314 }
315 $ports = array();
316 foreach ($lines2 as $line)
317 {
318 switch ($format)
319 {
320 case 'fisxii':
321 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
322 list ($slot, $port) = explode ('/', $words[0]);
323 $ports[] = array
324 (
325 'name' => "e ${slot}/${port}",
326 'l2address' => $words[8],
327 'label' => "slot ${slot} port ${port}"
328 );
329 break;
330 case 'c3600asy':
331 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', trim (substr ($line, 3))));
332/*
333How Async Lines are Numbered in Cisco 3600 Series Routers
334http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
335
336Understanding 16- and 32-Port Async Network Modules
337http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml
338*/
339 $async = $words[0];
340 $slot = floor (($async - 1) / 32);
341 $octalgroup = floor (($async - 1 - $slot * 32) / 8);
342 $cable = $async - $slot * 32 - $octalgroup * 8;
343 $og_label[0] = 'async 0-7';
344 $og_label[1] = 'async 8-15';
345 $og_label[2] = 'async 16-23';
346 $og_label[3] = 'async 24-31';
347 $ports[] = array
348 (
349 'name' => "async ${async}",
350 'l2address' => '',
351 'label' => "slot ${slot} " . $og_label[$octalgroup] . " cable ${cable}"
352 );
353 break;
354 case 'fiwg':
355 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
356 $ifnumber = $words[0] * 1;
357 $ports[] = array
358 (
359 'name' => "e ${ifnumber}",
360 'l2address' => "${words[8]}",
361 'label' => "${ifnumber}"
362 );
363 break;
351d4dbf 364 case 'ssv1':
5b585342 365 $words = explode (' ', $line);
351d4dbf
DO
366 if (empty ($words[0]) or empty ($words[1]))
367 continue;
368 $ports[] = array
369 (
370 'name' => $words[0],
371 'l2address' => $words[1],
372 'label' => ''
373 );
374 break;
e673ee24
DO
375 default:
376 return
377 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" .
378 urlencode('Cannot process submitted data: unknown format code.');
379 break;
380 }
381 }
382 // Create ports, if they don't exist.
383 $added_count = $updated_count = $error_count = 0;
384 foreach ($ports as $port)
385 {
386 $port_id = getPortID ($object_id, $port['name']);
387 if ($port_id === NULL)
388 {
389 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
390 if ($result == '')
391 $added_count++;
392 else
393 $error_count++;
394 }
395 else
396 {
397 $result = commitUpdatePort ($port_id, $port['name'], $port['label'], $port['l2address']);
398 if ($result == '')
399 $updated_count++;
400 else
401 $error_count++;
402 }
403 }
404 return
405 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" .
406 urlencode("Added ${added_count} ports, updated ${updated_count} ports, encountered ${error_count} errors.");
407}
408
b4c1ef87 409function updIPv4Allocation ()
e673ee24 410{
da958e52 411 global $pageno, $tabno;
b4c1ef87
DO
412 assertIPv4Arg ('ip', __FUNCTION__);
413 assertUIntArg ('object_id', __FUNCTION__);
414 assertStringArg ('bond_name', __FUNCTION__, TRUE);
415 assertStringArg ('bond_type', __FUNCTION__);
e673ee24 416
b4c1ef87 417 $error = updateBond ($_REQUEST['ip'], $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
e673ee24 418 if ($error != '')
46f92ff7 419 return buildRedirectURL_ERR ($error);
e673ee24 420 else
46f92ff7 421 return buildRedirectURL_OK ('allocation updated');
e673ee24
DO
422}
423
b4c1ef87 424function delIPv4Allocation ()
e673ee24 425{
da958e52 426 global $pageno, $tabno;
b4c1ef87
DO
427 assertIPv4Arg ('ip', __FUNCTION__);
428 assertUIntArg ('object_id', __FUNCTION__);
e673ee24 429
b4c1ef87 430 $error = unbindIpFromObject ($_REQUEST['ip'], $_REQUEST['object_id']);
e673ee24 431 if ($error != '')
46f92ff7 432 return buildRedirectURL_ERR ($error);
e673ee24 433 else
46f92ff7 434 return buildRedirectURL_OK ('deallocated');
e673ee24
DO
435}
436
b4c1ef87 437function addIPv4Allocation ()
e673ee24 438{
da958e52 439 global $pageno, $tabno;
b4c1ef87
DO
440 assertIPv4Arg ('ip', __FUNCTION__);
441 assertUIntArg ('object_id', __FUNCTION__);
442 assertStringArg ('bond_name', __FUNCTION__, TRUE);
443 assertStringArg ('bond_type', __FUNCTION__);
da958e52 444
b4c1ef87
DO
445 // Strip masklen.
446 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
447 $error = bindIpToObject ($ip, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
448 $address = getIPAddress ($ip);
449 if ($address['exists'] and ($address['reserved'] == 'yes' or strlen ($address['name']) > 0))
450 {
451 $release = getConfigVar ('IPV4_AUTO_RELEASE');
452 if ($release >= 1)
453 $address['reserved'] = 'no';
454 if ($release >= 2)
455 $address['name'] = '';
456 updateAddress ($ip, $address['name'], $address['reserved']);
457 }
e673ee24 458 if ($error != '')
46f92ff7 459 return buildRedirectURL_ERR ($error);
e673ee24 460 else
46f92ff7 461 return buildRedirectURL_OK ('allocated');
e673ee24
DO
462}
463
42023f03 464function addIPv4Prefix ()
e673ee24
DO
465{
466 global $root, $pageno, $tabno;
2c6c7645
DO
467 assertStringArg ('range', __FUNCTION__);
468 assertStringArg ('name', __FUNCTION__);
e673ee24 469
fa05e3de
DO
470 $is_bcast = isset ($_REQUEST['is_bcast']) ? $_REQUEST['is_bcast'] : 'off';
471 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42023f03 472 $error = createIPv4Prefix ($_REQUEST['range'], $_REQUEST['name'], $is_bcast == 'on', $taglist);
e673ee24 473 if ($error != '')
42023f03 474 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ($error);
e673ee24 475 else
42023f03 476 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("IPv4 prefix successfully added");
e673ee24
DO
477}
478
42023f03 479function delIPv4Prefix ()
e673ee24
DO
480{
481 global $root, $pageno, $tabno;
482
da958e52 483 assertUIntArg ('id', __FUNCTION__);
42023f03 484 $error = destroyIPv4Prefix ($_REQUEST['id']);
e673ee24 485 if ($error != '')
42023f03 486 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ($error);
e673ee24 487 else
42023f03 488 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("IPv4 prefix deleted");
e673ee24
DO
489}
490
42023f03 491function editRange ()
e673ee24 492{
da958e52
DO
493 global $pageno, $tabno;
494 assertUIntArg ('id', __FUNCTION__);
495 assertStringArg ('name', __FUNCTION__);
e673ee24 496
da958e52 497 $error = updateRange ($_REQUEST['id'], $_REQUEST['name']);
e673ee24 498 if ($error != '')
46f92ff7 499 return buildRedirectURL_ERR ($error);
e673ee24 500 else
46f92ff7 501 return buildRedirectURL_OK ('IPv4 prefix updated');
e673ee24
DO
502}
503
504function editAddress ()
505{
da958e52
DO
506 global $pageno, $tabno;
507 assertIPv4Arg ('ip', __FUNCTION__);
508 assertStringArg ('name', __FUNCTION__, TRUE);
e673ee24 509
e673ee24
DO
510 if (isset ($_REQUEST['reserved']))
511 $reserved = $_REQUEST['reserved'];
512 else
513 $reserved = 'off';
da958e52 514 $error = updateAddress ($_REQUEST['ip'], $_REQUEST['name'], $reserved == 'on' ? 'yes' : 'no');
e673ee24 515 if ($error != '')
46f92ff7 516 return buildRedirectURL_ERR ($error);
e673ee24 517 else
46f92ff7 518 return buildRedirectURL_OK ('IPv4 address updated');
e673ee24
DO
519}
520
cced6b7d 521function createUser ()
e673ee24
DO
522{
523 global $root, $pageno, $tabno;
2c6c7645
DO
524 assertStringArg ('username', __FUNCTION__);
525 assertStringArg ('realname', __FUNCTION__, TRUE);
526 assertStringArg ('password', __FUNCTION__);
e673ee24
DO
527 $username = $_REQUEST['username'];
528 $password = hash (PASSWORD_HASH, $_REQUEST['password']);
529 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
530 if ($result == TRUE)
531 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} created.");
532 else
533 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error creating user account ${username}.");
534}
535
cced6b7d 536function updateUser ()
e673ee24
DO
537{
538 global $root, $pageno, $tabno;
cced6b7d 539 assertUIntArg ('user_id', __FUNCTION__);
2c6c7645
DO
540 assertStringArg ('username', __FUNCTION__);
541 assertStringArg ('realname', __FUNCTION__, TRUE);
542 assertStringArg ('password', __FUNCTION__);
e673ee24 543 // We might be asked to change username, so use user ID only.
cced6b7d 544 $id = $_REQUEST['user_id'];
e673ee24
DO
545 $username = $_REQUEST['username'];
546 $new_password = $_REQUEST['password'];
547 $old_hash = getHashByID ($id);
548 if ($old_hash == NULL)
549 {
61e269b5 550 showError ('getHashByID() failed', __FUNCTION__);
e673ee24
DO
551 return;
552 }
553 // Update user password only if provided password is not the same as current password hash.
554 if ($new_password != $old_hash)
555 $new_password = hash (PASSWORD_HASH, $new_password);
cced6b7d 556 $result = commitUpdateUserAccount ($id, $username, $_REQUEST['realname'], $new_password);
e673ee24
DO
557 if ($result == TRUE)
558 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} updated.");
559 else
560 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error updating user account ${username}.");
561}
562
cced6b7d 563function enableUser ()
e673ee24
DO
564{
565 global $root, $pageno, $tabno;
cced6b7d
DO
566 assertUIntArg ('user_id', __FUNCTION__);
567 if (commitEnableUserAccount ($_REQUEST['user_id'], 'yes') == TRUE)
e673ee24
DO
568 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account enabled.");
569 else
570 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error enabling user account.");
571}
572
cced6b7d 573function disableUser ()
e673ee24
DO
574{
575 global $root, $pageno, $tabno;
cced6b7d
DO
576 assertUIntArg ('user_id', __FUNCTION__);
577 $id = $_REQUEST['user_id'];
e673ee24
DO
578 if ($id == 1)
579 $result = FALSE;
580 else
581 $result = commitEnableUserAccount ($id, 'no');
582 if ($result == TRUE)
583 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account disabled.");
584 else
585 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error disabling user account.");
586}
587
e673ee24
DO
588// This function find differences in users's submit and PortCompat table
589// and modifies database accordingly.
590function savePortMap ()
591{
592 global $root, $pageno, $tabno;
593 $ptlist = getPortTypes();
594 $oldCompat = getPortCompat();
595 $newCompat = array();
596 foreach (array_keys ($ptlist) as $leftKey)
597 foreach (array_keys ($ptlist) as $rightKey)
598 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
599 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
600 // Build the new matrix from user's submit and compare it to
601 // the old matrix. Those pairs which appear on
602 // new matrix only, have to be stored in PortCompat table. Those who appear
603 // on the old matrix only, should be removed from PortCompat table.
604 // Those present in both matrices should be skipped.
605 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
606 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
607 $error_count = $success_count = 0;
608 foreach (array_keys ($ptlist) as $type1)
609 foreach (array_keys ($ptlist) as $type2)
610 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
611 switch ($oldCompatTable[$type1][$type2])
612 {
613 case TRUE: // new value is FALSE
614 if (removePortCompat ($type1, $type2) === TRUE)
615 $success_count++;
616 else
617 $error_count++;
618 break;
619 case FALSE: // new value is TRUE
620 if (addPortCompat ($type1, $type2) === TRUE)
621 $success_count++;
622 else
623 $error_count++;
624 break;
625 default:
61e269b5 626 showError ('oldCompatTable is invalid', __FUNCTION__);
e673ee24
DO
627 break;
628 }
629 return
630 "${root}?page=${pageno}&tab=${tabno}&" .
631 ($error_count == 0 ? 'message' : 'error') .
632 "=" . urlencode ("${error_count} failures and ${success_count} successfull changes.");
633}
634
635function deleteDictWord ()
636{
637 global $root, $pageno, $tabno;
638 return
639 "${root}?page=${pageno}&tab=${tabno}&" .
640 "error=" . urlencode ('Dragon ate this word!');
641}
642
643function updateDictionary ()
644{
645 global $root, $pageno, $tabno;
2c6c7645
DO
646 assertUIntArg ('chapter_no', __FUNCTION__);
647 assertUIntArg ('dict_key', __FUNCTION__);
648 assertStringArg ('dict_value', __FUNCTION__);
e673ee24
DO
649 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
650 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Update succeeded.');
651 else
652 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed!");
653}
654
655function supplementDictionary ()
656{
657 global $root, $pageno, $tabno;
2c6c7645
DO
658 assertUIntArg ('chapter_no', __FUNCTION__);
659 assertStringArg ('dict_value', __FUNCTION__);
e673ee24
DO
660 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
661 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
662 else
663 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
664}
665
666function reduceDictionary ()
667{
668 global $root, $pageno, $tabno;
2c6c7645
DO
669 assertUIntArg ('chapter_no', __FUNCTION__);
670 assertUIntArg ('dict_key', __FUNCTION__);
e673ee24
DO
671 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
672 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
673 else
674 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
675}
676
677function addChapter ()
678{
679 global $root, $pageno, $tabno;
2c6c7645 680 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24
DO
681 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
682 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was added.');
683 else
684 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error adding chapter.');
685}
686
687function updateChapter ()
688{
689 global $root, $pageno, $tabno;
2c6c7645
DO
690 assertUIntArg ('chapter_no', __FUNCTION__);
691 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24
DO
692 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
693 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was updated.');
694 else
695 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error updating chapter.');
696}
697
698function delChapter ()
699{
700 global $root, $pageno, $tabno;
2c6c7645 701 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24
DO
702 if (commitDeleteChapter ($_REQUEST['chapter_no']))
703 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was deleted.');
704 else
705 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting chapter.');
706}
707
708function changeAttribute ()
709{
710 global $root, $pageno, $tabno;
2c6c7645
DO
711 assertUIntArg ('attr_id', __FUNCTION__);
712 assertStringArg ('attr_name', __FUNCTION__);
e673ee24
DO
713 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
714 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Rename successful.');
715 else
716 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error renaming attribute.');
717}
718
719function createAttribute ()
720{
721 global $root, $pageno, $tabno;
2c6c7645
DO
722 assertStringArg ('attr_name', __FUNCTION__);
723 assertStringArg ('attr_type', __FUNCTION__);
e673ee24
DO
724 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
725 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Attribute '${_REQUEST['attr_name']}' created.");
726 else
727 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error creating attribute.');
728}
729
730function deleteAttribute ()
731{
732 global $root, $pageno, $tabno;
2c6c7645 733 assertUIntArg ('attr_id', __FUNCTION__);
e673ee24
DO
734 if (commitDeleteAttribute ($_REQUEST['attr_id']))
735 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Attribute was deleted.');
736 else
737 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting attribute.');
738}
739
740function supplementAttrMap ()
741{
742 global $root, $pageno, $tabno;
2c6c7645
DO
743 assertUIntArg ('attr_id', __FUNCTION__);
744 assertUIntArg ('objtype_id', __FUNCTION__);
745 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24
DO
746 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
747 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
748 else
749 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
750}
751
752function reduceAttrMap ()
753{
754 global $root, $pageno, $tabno;
2c6c7645
DO
755 assertUIntArg ('attr_id', __FUNCTION__);
756 assertUIntArg ('objtype_id', __FUNCTION__);
e673ee24
DO
757 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
758 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
759 else
760 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
761}
762
763function resetAttrValue ()
764{
765 global $root, $pageno, $tabno;
2c6c7645
DO
766 assertUIntArg ('attr_id', __FUNCTION__);
767 assertUIntArg ('object_id', __FUNCTION__);
e673ee24
DO
768 $object_id = $_REQUEST['object_id'];
769 if (commitResetAttrValue ($object_id, $_REQUEST['attr_id']) === TRUE)
770 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reset succeeded.');
771 else
772 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Reset failed!");
773}
774
0dfb8a2a 775function updateAttrValues ()
e673ee24
DO
776{
777 global $root, $pageno, $tabno;
2c6c7645 778 assertUIntArg ('object_id', __FUNCTION__);
e673ee24 779 $object_id = $_REQUEST['object_id'];
0dfb8a2a
DO
780 $oldvalues = getAttrValues ($object_id);
781
2c6c7645 782 assertUIntArg ('num_attrs', __FUNCTION__);
0dfb8a2a
DO
783 $num_attrs = $_REQUEST['num_attrs'];
784 $result = array();
785
786 for ($i = 0; $i < $num_attrs; $i++)
787 {
2c6c7645 788 assertUIntArg ("${i}_attr_id", __FUNCTION__);
0dfb8a2a
DO
789 $attr_id = $_REQUEST["${i}_attr_id"];
790
791 // Field is empty, delete attribute and move on.
792 if (empty($_REQUEST["${i}_value"]))
793 {
794 commitResetAttrValue ($object_id, $attr_id);
795 continue;
796 }
797
798 // The value could be uint/float, but we don't know ATM. Let SQL
799 // server check this and complain.
2c6c7645 800 assertStringArg ("${i}_value", __FUNCTION__);
0dfb8a2a
DO
801 $value = $_REQUEST["${i}_value"];
802 switch ($oldvalues[$attr_id]['type'])
803 {
804 case 'uint':
805 case 'float':
806 case 'string':
807 $oldvalue = $oldvalues[$attr_id]['value'];
808 break;
809 case 'dict':
810 $oldvalue = $oldvalues[$attr_id]['key'];
811 break;
812 default:
61e269b5 813 showError ('Internal structure error', __FUNCTION__);
0dfb8a2a
DO
814 die;
815 }
816 if ($value == $oldvalue)
817 continue;
818
819 // Note if the queries succeed or not, it determines which page they see.
820 $result[] = commitUpdateAttrValue ($object_id, $attr_id, $value);
821 }
822
823 if (in_array(false, $result))
e673ee24 824 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Update failed!");
0dfb8a2a
DO
825
826 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Update succeeded.');
e673ee24
DO
827}
828
829function useupPort ()
830{
831 global $root, $pageno, $tabno;
2c6c7645
DO
832 assertUIntArg ('port_id', __FUNCTION__);
833 assertUIntArg ('object_id', __FUNCTION__);
e673ee24
DO
834 $object_id = $_REQUEST['object_id'];
835 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
836 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reservation removed.');
837 else
838 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Error removing reservation!");
839}
840
4fe32e78
DY
841function updateUI ()
842{
c461c579 843 global $root, $pageno, $tabno;
4fe32e78
DY
844 $oldvalues = loadConfigCache();
845
2c6c7645 846 assertUIntArg ('num_vars', __FUNCTION__);
4fe32e78 847 $num_vars = $_REQUEST['num_vars'];
c461c579 848 $error = '';
4fe32e78
DY
849
850 for ($i = 0; $i < $num_vars; $i++)
851 {
2c6c7645
DO
852 assertStringArg ("${i}_varname", __FUNCTION__);
853 assertStringArg ("${i}_varvalue", __FUNCTION__, TRUE);
4fe32e78 854 $varname = $_REQUEST["${i}_varname"];
4fe32e78 855 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 856
c461c579
DO
857 // If form value = value in DB, don't bother updating DB
858 if ($varvalue == getConfigVar ($varname))
4fe32e78
DY
859 continue;
860
861 // Note if the queries succeed or not, it determines which page they see.
c461c579
DO
862 $error = setConfigVar ($varname, $varvalue, TRUE);
863 if (!empty ($error))
864 break;
4fe32e78
DY
865 }
866
c461c579
DO
867 if ($error != '')
868 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed with error: " . $error);
4fe32e78 869
c461c579 870 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Update succeeded.");
4fe32e78
DY
871}
872
b07f617c
DO
873function resetUIConfig()
874{
6c12ef9b 875 global $root, $pageno, $tabno;
1f86e996 876 setConfigVar ('default_port_type','24');
b07f617c
DO
877 setConfigVar ('MASSCOUNT','15');
878 setConfigVar ('MAXSELSIZE','30');
879 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
880 setConfigVar ('ROW_SCALE','2');
881 setConfigVar ('PORTS_PER_ROW','12');
882 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
883 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
a6305acc 884 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
b5ce46aa
DO
885 setConfigVar ('USER_AUTH_SRC','database');
886 setConfigVar ('DEFAULT_SLB_VS_PORT','');
887 setConfigVar ('DEFAULT_SLB_RS_PORT','');
d48e0a20 888 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
b5ce46aa 889 setConfigVar ('NATV4_PERFORMERS','4,7,8');
72281138 890 setConfigVar ('DETECT_URLS','no');
4b8d413e 891 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
b108de49 892 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
8dfb997c 893 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
194e3748
DO
894 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
895 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
896 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
a477e405 897 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
2754cefc 898 setConfigVar ('IPV4_AUTO_RELEASE','1');
6c12ef9b 899 return "${root}?page=${pageno}&tab=default&message=" . urlencode ("Reset complete");
b07f617c
DO
900}
901
732e4578 902// Add single record.
ca461127
DO
903function addRealServer ()
904{
905 global $root, $pageno, $tabno;
906
2c6c7645
DO
907 assertUIntArg ('pool_id', __FUNCTION__);
908 assertIPv4Arg ('remoteip', __FUNCTION__);
909 assertUIntArg ('rsport', __FUNCTION__);
910 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
22a5b5ae 911 $pool_id = $_REQUEST['pool_id'];
2910c1d4 912 if (!addRStoRSPool ($pool_id, $_REQUEST['remoteip'], $_REQUEST['rsport'], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), $_REQUEST['rsconfig']))
841a7a7a 913 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('addRStoRSPool() failed');
d6517a21 914 else
841a7a7a 915 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully added");
d6517a21
DO
916}
917
732e4578
DO
918// Parse textarea submitted and try adding a real server for each line.
919function addRealServers ()
920{
921 global $root, $pageno, $tabno;
922
cbadbb38 923 assertUIntArg ('pool_id', __FUNCTION__);
2c6c7645
DO
924 assertStringArg ('format', __FUNCTION__);
925 assertStringArg ('rawtext', __FUNCTION__);
cbadbb38 926 $pool_id = $_REQUEST['pool_id'];
732e4578
DO
927 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
928 $ngood = $nbad = 0;
929 $rsconfig = '';
930 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
931 foreach (explode ('\n', $rawtext) as $line)
932 {
933 if (empty ($line))
934 continue;
935 $match = array ();
936 switch ($_REQUEST['format'])
937 {
938 case 'ipvs_2': // address and port only
939 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
940 continue;
7e7a8387 941 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
942 $ngood++;
943 else
944 $nbad++;
945 break;
946 case 'ipvs_3': // address, port and weight
947 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
948 continue;
7e7a8387 949 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
950 $ngood++;
951 else
952 $nbad++;
953 break;
e69c2aa3
DO
954 case 'ssv_2': // IP address and port
955 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
956 continue;
957 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
958 $ngood++;
959 else
960 $nbad++;
961 break;
732e4578
DO
962 default:
963 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode (__FUNCTION__ . ': invalid format requested');
964 break;
965 }
966 }
967 if ($nbad == 0 and $ngood > 0)
841a7a7a 968 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Successfully added ${ngood} real servers");
732e4578 969 else
841a7a7a 970 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ("Added ${ngood} real servers and encountered ${nbad} errors");
732e4578
DO
971}
972
d6517a21
DO
973function addVService ()
974{
975 global $root, $pageno, $tabno;
976
2c6c7645
DO
977 assertIPv4Arg ('vip', __FUNCTION__);
978 assertUIntArg ('vport', __FUNCTION__);
979 assertStringArg ('proto', __FUNCTION__);
d6517a21
DO
980 $proto = $_REQUEST['proto'];
981 if ($proto != 'TCP' and $proto != 'UDP')
982 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode (__FUNCTION__ . ': invalid protocol');
2c6c7645
DO
983 assertStringArg ('name', __FUNCTION__, TRUE);
984 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
985 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
d6517a21
DO
986 $pool_id = $_REQUEST['id'];
987 if (!commitCreateVS ($_REQUEST['vip'], $_REQUEST['vport'], $proto, $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
988 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateVS() failed');
ca461127 989 else
d6517a21 990 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully created");
fb1c4a54
DO
991}
992
993function deleteRealServer ()
994{
995 global $root, $pageno, $tabno;
996
2c6c7645
DO
997 assertUIntArg ('pool_id', __FUNCTION__);
998 assertUIntArg ('id', __FUNCTION__);
fb1c4a54 999 $pool_id = $_REQUEST['pool_id'];
d6517a21 1000 if (!commitDeleteRS ($_REQUEST['id']))
841a7a7a 1001 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitDeleteRS() failed');
fb1c4a54 1002 else
841a7a7a 1003 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully deleted");
fb1c4a54
DO
1004}
1005
3241551e
DO
1006function deleteLoadBalancer ()
1007{
1008 global $root, $pageno, $tabno;
1009
2c6c7645
DO
1010 assertUIntArg ('object_id', __FUNCTION__);
1011 assertUIntArg ('pool_id', __FUNCTION__);
1012 assertUIntArg ('vs_id', __FUNCTION__);
49fb2686
DO
1013 if (!commitDeleteLB ($_REQUEST['object_id'], $_REQUEST['pool_id'], $_REQUEST['vs_id']))
1014 return buildRedirectURL_ERR ('commitDeleteLB() failed');
d6517a21 1015 else
49fb2686 1016 return buildRedirectURL_OK ('Load balancer was successfully deleted');
d6517a21
DO
1017}
1018
1019function deleteVService ()
1020{
e02e1941
DO
1021 assertUIntArg ('vs_id', __FUNCTION__);
1022 if (!commitDeleteVS ($_REQUEST['vs_id']))
1023 return buildRedirectURL_ERR ('commitDeleteVS() failed');
3241551e 1024 else
e02e1941 1025 return buildRedirectURL_OK ('Virtual service was successfully deleted');
3241551e
DO
1026}
1027
fb1c4a54
DO
1028function updateRealServer ()
1029{
1030 global $root, $pageno, $tabno;
1031
3c52ff91 1032 assertUIntArg ('pool_id', __FUNCTION__);
2c6c7645
DO
1033 assertUIntArg ('rs_id', __FUNCTION__);
1034 assertIPv4Arg ('rsip', __FUNCTION__);
1035 assertUIntArg ('rsport', __FUNCTION__);
1036 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
3241551e 1037 // only necessary for generating next URL
3c52ff91 1038 $pool_id = $_REQUEST['pool_id'];
732e4578 1039 if (!commitUpdateRS ($_REQUEST['rs_id'], $_REQUEST['rsip'], $_REQUEST['rsport'], $_REQUEST['rsconfig']))
841a7a7a 1040 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitUpdateRS() failed');
fb1c4a54 1041 else
841a7a7a 1042 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully updated");
ca461127
DO
1043}
1044
c1ca768c 1045function updateLoadBalancer ()
3241551e
DO
1046{
1047 global $root, $pageno, $tabno;
1048
2c6c7645
DO
1049 assertUIntArg ('object_id', __FUNCTION__);
1050 assertUIntArg ('pool_id', __FUNCTION__);
1051 assertUIntArg ('vs_id', __FUNCTION__);
1052 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1053 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
49fb2686
DO
1054 if (!commitUpdateLB ($_REQUEST['object_id'], $_REQUEST['pool_id'], $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1055 return buildRedirectURL_ERR ('commitUpdateLB() failed');
d6517a21 1056 else
49fb2686 1057 return buildRedirectURL_OK ('Load balancer info was successfully updated');
d6517a21
DO
1058}
1059
1060function updateVService ()
1061{
e02e1941 1062 assertUIntArg ('vs_id', __FUNCTION__);
2c6c7645
DO
1063 assertIPv4Arg ('vip', __FUNCTION__);
1064 assertUIntArg ('vport', __FUNCTION__);
1065 assertStringArg ('proto', __FUNCTION__);
1066 assertStringArg ('name', __FUNCTION__, TRUE);
1067 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1068 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
e02e1941 1069 if (!commitUpdateVS ($_REQUEST['vs_id'], $_REQUEST['vip'], $_REQUEST['vport'], $_REQUEST['proto'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
c9890988 1070 return buildRedirectURL_ERR ('commitUpdateVS() failed');
3241551e 1071 else
c9890988 1072 return buildRedirectURL_OK ('Virtual service was successfully updated');
3241551e
DO
1073}
1074
1075function addLoadBalancer ()
1076{
1077 global $root, $pageno, $tabno;
1078
2c6c7645
DO
1079 assertUIntArg ('pool_id', __FUNCTION__);
1080 assertUIntArg ('object_id', __FUNCTION__);
1081 assertUIntArg ('vs_id', __FUNCTION__);
1082 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1083 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
49fb2686
DO
1084 if (!addLBtoRSPool ($_REQUEST['pool_id'], $_REQUEST['object_id'], $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1085 return buildRedirectURL_ERR ('addLBtoRSPool() failed');
3241551e 1086 else
49fb2686 1087 return buildRedirectURL_OK ('Load balancer was successfully added');
3241551e
DO
1088}
1089
5ad76f01
DO
1090function addRSPool ()
1091{
1092 global $root, $pageno, $tabno;
1093
2c6c7645
DO
1094 assertStringArg ('name', __FUNCTION__, TRUE);
1095 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1096 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
70c24883 1097 if (!commitCreateRSPool ($_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
5ad76f01
DO
1098 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateRSPool() failed');
1099 else
1100 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully created");
1101}
1102
1103function deleteRSPool ()
1104{
1105 global $root, $pageno, $tabno;
1106
2c6c7645 1107 assertUIntArg ('pool_id', __FUNCTION__);
841a7a7a 1108 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
5ad76f01
DO
1109 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteRSPool() failed');
1110 else
1111 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully deleted");
1112}
1113
1114function updateRSPool ()
1115{
1116 global $root, $pageno, $tabno;
1117
2c6c7645
DO
1118 assertUIntArg ('pool_id', __FUNCTION__);
1119 assertStringArg ('name', __FUNCTION__, TRUE);
1120 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1121 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
841a7a7a 1122 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
ea5a3250 1123 return buildRedirectURL_ERR ('commitUpdateRSPool() failed');
5ad76f01 1124 else
ea5a3250 1125 return buildRedirectURL_OK ('Real server pool was successfully updated');
5ad76f01
DO
1126}
1127
1f7d18fa
DO
1128function updateRSInService ()
1129{
1130 global $root, $pageno, $tabno;
2c6c7645
DO
1131 assertUIntArg ('pool_id', __FUNCTION__);
1132 assertUIntArg ('rscount', __FUNCTION__);
841a7a7a 1133 $pool_id = $_REQUEST['pool_id'];
1f7d18fa
DO
1134 $orig = getRSPoolInfo ($pool_id);
1135 $nbad = $ngood = 0;
1136 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1137 {
1138 $rs_id = $_REQUEST["rsid_${i}"];
1139 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1140 $newval = 'yes';
1141 else
1142 $newval = 'no';
1143 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1144 {
1145 if (commitSetInService ($rs_id, $newval))
1146 $ngood++;
1147 else
1148 $nbad++;
1149 }
1150 }
1151 if (!$nbad)
841a7a7a 1152 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ($ngood . " real server(s) were successfully (de)activated");
1f7d18fa 1153 else
841a7a7a 1154 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ("Encountered ${nbad} errors, (de)activated ${ngood} real servers");
1f7d18fa
DO
1155}
1156
03eb5209
DO
1157function importPTRData ()
1158{
1159 global $root, $pageno, $tabno;
2c6c7645
DO
1160 assertUIntArg ('id', __FUNCTION__);
1161 assertUIntArg ('addrcount', __FUNCTION__);
03eb5209
DO
1162 $nbad = $ngood = 0;
1163 $id = $_REQUEST['id'];
1164 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1165 {
3f3bd41e 1166 $inputname = "import_${i}";
03eb5209
DO
1167 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1168 continue;
2c6c7645
DO
1169 assertIPv4Arg ("addr_${i}", __FUNCTION__);
1170 assertStringArg ("descr_${i}", __FUNCTION__, TRUE);
1171 assertStringArg ("rsvd_${i}", __FUNCTION__);
3f3bd41e
DO
1172 // Non-existent addresses will not have this argument set in request.
1173 $rsvd = 'no';
03eb5209
DO
1174 if ($_REQUEST["rsvd_${i}"] == 'yes')
1175 $rsvd = 'yes';
3f3bd41e 1176 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1177 $ngood++;
1178 else
1179 $nbad++;
1180 }
1181 if (!$nbad)
0aa8633e 1182 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&message=" . urlencode ($ngood . " IP address(es) were successfully updated");
03eb5209 1183 else
0aa8633e 1184 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&error=" . urlencode ("Encountered ${nbad} errors, updated ${ngood} IP address(es)");
03eb5209
DO
1185}
1186
f3f0161f
DO
1187function generateAutoPorts ()
1188{
1189 global $root, $pageno, $tabno;
2c6c7645 1190 assertUIntArg ('object_id', __FUNCTION__);
f3f0161f
DO
1191 $object_id = $_REQUEST['object_id'];
1192 $info = getObjectInfo ($object_id);
1193 // Navigate away in case of success, stay at the place otherwise.
1194 if (executeAutoPorts ($object_id, $info['objtype_id']))
1195 return "${root}?page=${pageno}&tab=ports&object_id=${object_id}&message=" . urlencode ('Generation complete');
1196 else
1197 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ('executeAutoPorts() failed');
1198}
1199
24cbe8af 1200// Filter out implicit tags before storing the new tag set.
2034d968 1201function saveEntityTags ($realm, $bypass)
24cbe8af
DO
1202{
1203 global $root, $pageno, $tabno, $explicit_tags, $implicit_tags;
2c6c7645 1204 assertUIntArg ($bypass, __FUNCTION__);
2034d968 1205 $entity_id = $_REQUEST[$bypass];
e04931c8 1206 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1207 // Build a chain from the submitted data, minimize it,
ab379543 1208 // then wipe existing records and store the new set instead.
49fb1027 1209 deleteTagsForEntity ($realm, $entity_id);
6e49bd1f 1210 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
ab379543 1211 $n_succeeds = $n_errors = 0;
6e49bd1f 1212 foreach ($newchain as $taginfo)
eb6ea26f 1213 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1214 $n_succeeds++;
1215 else
1216 $n_errors++;
ab379543 1217 if ($n_errors)
6e49bd1f 1218 return buildRedirectURL_ERR ("Tried chaining ${n_succeeds} tags, but experienced ${n_errors} errors.");
ab379543 1219 else
6e49bd1f 1220 return buildRedirectURL_OK ("Chained ${n_succeeds} tags");
2034d968
DO
1221}
1222
1223function saveObjectTags ()
1224{
1225 return saveEntityTags ('object', 'object_id');
1226}
1227
1228function saveIPv4PrefixTags ()
1229{
1230 return saveEntityTags ('ipv4net', 'id');
24cbe8af
DO
1231}
1232
41bd8c87
DO
1233function saveRackTags ()
1234{
1235 return saveEntityTags ('rack', 'rack_id');
1236}
1237
d817ba03
DO
1238function saveIPv4VSTags ()
1239{
1a4beb9e 1240 return saveEntityTags ('ipv4vs', 'vs_id');
d817ba03
DO
1241}
1242
1243function saveIPv4RSPoolTags ()
1244{
2e4170aa 1245 return saveEntityTags ('ipv4rspool', 'pool_id');
d817ba03
DO
1246}
1247
cced6b7d
DO
1248function saveUserTags ()
1249{
1250 return saveEntityTags ('user', 'user_id');
1251}
1252
fe7044ad
DO
1253function destroyTag ()
1254{
1255 global $root, $pageno, $tabno;
49fb1027
DO
1256 assertUIntArg ('tag_id', __FUNCTION__);
1257 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
fe7044ad
DO
1258 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Successfully deleted tag.");
1259 else
1260 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error deleting tag: '${ret}'");
1261}
1262
1263function createTag ()
1264{
1265 global $root, $pageno, $tabno;
49fb1027 1266 assertStringArg ('tag_name', __FUNCTION__);
2c6c7645 1267 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
49fb1027 1268 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80
DO
1269 if (!validTagName ($tagname))
1270 return buildRedirectURL_ERR ("Invalid tag name '${tagname}'");
0f3c6639
DO
1271 if (tagExistsInDatabase ($tagname))
1272 return buildRedirectURL_ERR ("Tag '${tagname}' (or similar name) already exists");
fe7044ad
DO
1273 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1274 $parent_id = 'NULL';
1275 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
1276 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Created tag '${tagname}'.");
1277 else
1278 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Could not create tag '${tagname}' because of error '${ret}'");
1279}
1280
49fb1027
DO
1281function updateTag ()
1282{
1283 global $root, $pageno, $tabno;
1284 assertUIntArg ('tag_id', __FUNCTION__);
1285 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1286 assertStringArg ('tag_name', __FUNCTION__);
1287 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80
DO
1288 if (!validTagName ($tagname))
1289 return buildRedirectURL_ERR ("Invalid tag name '${tagname}'");
49fb1027
DO
1290 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1291 $parent_id = 'NULL';
1292 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
1293 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Updated tag '${tagname}'.");
1294 else
1295 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Could not update tag '${tagname}' because of error '${ret}'");
1296}
1297
eb6ea26f
DO
1298function rollTags ()
1299{
1300 global $root, $pageno, $tabno;
1301 assertUIntArg ('row_id', __FUNCTION__);
1302 assertStringArg ('sum', __FUNCTION__, TRUE);
1303 assertUIntArg ('realsum', __FUNCTION__);
1304 $row_id = $_REQUEST['row_id'];
1305 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
1306 return "${root}?page=${pageno}&tab=${tabno}&row_id=${row_id}&error=" . urlencode ("test failed");
1307 $racks = getRacksForRow ($row_id);
1308 // Each time addTagForEntity() fails we assume it was just because of already existing record in its way.
1309 $newtags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1310 $tagstack = getExplicitTagsOnly (buildTagChainFromIds ($newtags));
eb6ea26f
DO
1311 $ndupes = $nnew = 0;
1312 foreach ($tagstack as $taginfo)
1313 foreach ($racks as $rackInfo)
1314 {
1315 if (addTagForEntity ('rack', $rackInfo['id'], $taginfo['id']))
1316 $nnew++;
1317 else
1318 $ndupes++;
1319 // FIXME: do something likewise for all object inside current rack
1320 }
1321 return "${root}?page=${pageno}&tab=${tabno}&row_id=${row_id}&message=" . urlencode ("${nnew} new records done, ${ndupes} already existed");
1322}
1323
9457ca59 1324function changeMyPassword ()
cced6b7d 1325{
cced6b7d 1326 global $accounts, $root, $pageno, $tabno, $remote_username;
9457ca59
DO
1327 if (getConfigVar ('USER_AUTH_SRC') != 'database')
1328 return buildRedirectURL_ERR ('Can only change password under DB authentication.');
0ac7e4b3
DO
1329 assertStringArg ('oldpassword', __FUNCTION__);
1330 assertStringArg ('newpassword1', __FUNCTION__);
1331 assertStringArg ('newpassword2', __FUNCTION__);
1332 if ($accounts[$remote_username]['user_password_hash'] != hash (PASSWORD_HASH, $_REQUEST['oldpassword']))
9457ca59 1333 return buildRedirectURL_ERR ('Old password doesn\'t match.');
cced6b7d 1334 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
9457ca59 1335 return buildRedirectURL_ERR ('New passwords don\'t match.');
0ac7e4b3 1336 if (commitUpdateUserAccount ($accounts[$remote_username]['user_id'], $accounts[$remote_username]['user_name'], $accounts[$remote_username]['user_realname'], hash (PASSWORD_HASH, $_REQUEST['newpassword1'])))
9457ca59 1337 return buildRedirectURL_OK ('Password changed successfully.');
cced6b7d 1338 else
9457ca59 1339 return buildRedirectURL_ERR ('Password change failed.');
cced6b7d
DO
1340}
1341
1342function saveRackCode ()
1343{
1344 global $root, $pageno, $tabno;
1345 assertStringArg ('rackcode');
e6a4adb9
DO
1346 // For the test to succeed, unescape LFs, strip CRs.
1347 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
cf25e649
DO
1348 $parseTree = getRackCode ($newcode);
1349 if ($parseTree['result'] != 'ACK')
1350 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Verification failed: ' . $parseTree['load']);
1351 if (saveScript ('RackCode', $newcode))
cced6b7d
DO
1352 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Saved successfully.');
1353 else
1354 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Save failed.');
1355}
1356
46f92ff7
DO
1357// This handler's context is pre-built, but not authorized. It is assumed, that the
1358// handler will take existing context and before each commit check authorization
1359// on the base chain plus necessary context added.
1360function setPortVLAN ()
1361{
1362 assertUIntArg ('portcount', __FUNCTION__);
1363 $data = getSwitchVLANs ($_REQUEST['object_id']);
1364 if ($data === NULL)
1365 return buildRedirectURL_ERR ('getSwitchVLANs() failed');
1366 list ($vlanlist, $portlist) = $data;
1367 // Here we just build up 1 set command for the gateway with all of the ports
1368 // included. The gateway is expected to filter unnecessary changes silently
1369 // and to provide a list of responses with either error or success message
1370 // for each of the rest.
1371 $nports = $_REQUEST['portcount'];
1372 $prefix = 'set ';
1373 $log = array();
1374 $setcmd = '';
1375 for ($i = 0; $i < $nports; $i++)
1376 if
1377 (
1378 !isset ($_REQUEST['portname_' . $i]) ||
1379 !isset ($_REQUEST['vlanid_' . $i]) ||
1380 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1381 )
1382 $log[] = array ('code' => 'error', 'message' => "Ignoring malformed record #${i} in form submit");
1383 elseif
1384 (
1385 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1386 $portlist[$i]['vlaind'] == 'TRUNK'
1387 )
1388 continue;
1389 else
1390 {
1391 $portname = $_REQUEST['portname_' . $i];
1392 $oldvlanid = $portlist[$i]['vlanid'];
1393 $newvlanid = $_REQUEST['vlanid_' . $i];
1394 // Finish the security context and evaluate it.
1395 $annex = array();
1396 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1397 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1398 if (!permitted (NULL, NULL, NULL, $annex))
1399 {
1400 $log[] = array ('code' => 'error', 'message' => "Permission denied moving port ${portname} from VLAN${oldvlanid} to VLAN${newvlanid}");
1401 continue;
1402 }
1403 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1404 $prefix = ';';
1405 }
1406 // Feed the gateway and interpret its (non)response.
1407 if ($setcmd != '')
1408 $log = array_merge ($log, setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
1409 else
1410 $log[] = array ('code' => 'warning', 'message' => 'nothing happened...');
1411 return buildWideRedirectURL ($log);
1412}
1413
e673ee24 1414?>