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