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