logicsw.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. //-
  3. // Copyright (c) 2011-2015 Joe Clarke <jclarke@cisco.com>
  4. // All rights reserved.
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions
  7. // are met:
  8. // 1. Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // 2. Redistributions in binary form must reproduce the above copyright
  11. // notice, this list of conditions and the following disclaimer in the
  12. // documentation and/or other materials provided with the distribution.
  13. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. // SUCH DAMAGE.
  24. include_once 'db.inc.php';
  25. include_once 'swreg/swreg.inc.php';
  26. require_once 'Log.php';
  27. require_once 'functions.php';
  28. $dsn = "$db_driver:host=$db_host;dbname=$db_name";
  29. $options = [
  30. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  31. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  32. PDO::ATTR_EMULATE_PREPARES => false,
  33. ];
  34. try {
  35. $dbh = new PDO($dsn, $db_user, $db_pass, $options);
  36. } catch (PDOException $e) {
  37. die($e->getMessage());
  38. }
  39. $logger = Log::singleton('file', LOGFILE, TOOL_NAME.' : Logical Switches');
  40. if ($logger === false) {
  41. die("Failed to open logfile.\n");
  42. }
  43. $mask = Log::UPTO(LOG_LEVEL);
  44. $logger->setMask($mask);
  45. $base = get_base($_SERVER['SCRIPT_NAME']);
  46. $ruser = '<AUTHENTICATION NOT ENABLED>';
  47. if (isset($_SERVER['REMOTE_USER'])) {
  48. $ruser = $_SERVER['REMOTE_USER'];
  49. }
  50. $errors = array();
  51. if (isset($_REQUEST['submit_top']) || isset($_REQUEST['submit_bottom'])) {
  52. $pia = $_REQUEST['prev_ip_address'];
  53. $ppid = $_REQUEST['prev_pid'];
  54. $ploc = $_REQUEST['prev_location'];
  55. $pidf = $_REQUEST['prev_is_idf'];
  56. $ia = $_REQUEST['address'];
  57. $pid = $_REQUEST['pid'];
  58. $loc = $_REQUEST['location'];
  59. $exp = $_REQUEST['exception'];
  60. $idf = (isset($_REQUEST['is_idf'])) ? $_REQUEST['is_idf'] : array();
  61. $d = (isset($_REQUEST['delete'])) ? $_REQUEST['delete'] : array();
  62. foreach ($pia as $sname => $value) {
  63. $curr_del = (isset($d[$sname])) ? $d[$sname] : 0;
  64. if ($curr_del == 1) {
  65. if ($ia[$sname] !== null && $ia[$sname] != '') {
  66. $sql = 'UPDATE ADDRESSES SET used=? WHERE address=?';
  67. try {
  68. $sth = $dbh->prepare($sql);
  69. $sth->execute(array('0', $ia[$sname]));
  70. $sth->closeCursor();
  71. } catch (PDOException $e) {
  72. array_push($errors, "Failed to free IP address {$ia[$sname]}: {$e->getMessage()}");
  73. }
  74. }
  75. $sql = "DELETE FROM DEVICE_MAP WHERE assigned_switch='{$sname}'";
  76. try {
  77. $dbh->query($sql);
  78. } catch (PDOException $e) {
  79. array_push($errors, "Failed to delete {$sname}: {$e->getMessage()}");
  80. }
  81. $sql = "DELETE FROM SWITCHES WHERE name='{$sname}'";
  82. try {
  83. $dbh->query($sql);
  84. $logger->info("User {$ruser} deleted logical switch {$sname}");
  85. } catch (PDOException $e) {
  86. array_push($errors, "Failed to delete {$sname}: {$e->getMessage()}");
  87. }
  88. if (count($errors) == 0) {
  89. @unlink(PORT_TMPL_DIR.'/devices/'."{$sname}-ports.tmpl");
  90. }
  91. continue;
  92. }
  93. $curr_idf = (isset($idf[$sname])) ? $idf[$sname] : 0;
  94. if ($value != $ia[$sname] || $ploc[$sname] != $loc[$sname] || $ppid[$sname] != $pid[$sname] || $pidf[$sname] != $curr_idf) {
  95. if ($pid[$sname] == '__BOGUS__') {
  96. array_push($errors, "You must select a Product ID for {$sname}");
  97. }
  98. $sql = "SELECT address, reserved FROM ADDRESSES WHERE address='{$ia[$sname]}'";
  99. $res = null;
  100. try {
  101. $res = $dbh->query($sql);
  102. } catch (PDOException $e) {
  103. }
  104. $row = $res->fetch();
  105. $newa = "'{$ia[$sname]}'";
  106. if ($ia[$sname] != '' && !$row['address']) {
  107. array_push($errors, "IP address {$ia[$sname]} is not valid for switch {$sname}");
  108. } elseif ((ADDRESS_SCHEME == 'IDF' && $row['reserved'] == 1 && $curr_idf != 1) || (ADDRESS_SCHEME != 'IDF' && ADDRESS_SCHEME != 'DNS' && $row['reserved'] == 1)) {
  109. array_push($errors, "IP address {$ia[$sname]} is reserved and cannot be used for this switch");
  110. } elseif ($ia[$sname] == '') {
  111. $newa = 'NULL';
  112. }
  113. if (count($errors) == 0) {
  114. $sql = "UPDATE SWITCHES SET ip_address = $newa, location = '{$loc[$sname]}', pid = '{$pid[$sname]}', is_idf = '{$curr_idf}' WHERE name='{$sname}'";
  115. try {
  116. $res = $dbh->query($sql);
  117. $logger->info("User {$ruser} updated logical switches: location={$loc[$sname]}, pid={$pid[$sname]}, is_idf={$curr_idf} for switch {$sname}");
  118. if ($ia[$sname] != '') {
  119. $sql = 'UPDATE ADDRESSES SET used=? WHERE address=?';
  120. try {
  121. $sth = $dbh->prepare($sql);
  122. $sth->execute(array('0', $ia[$sname]));
  123. $sth->closeCursor();
  124. $logger->info("User {$ruser} released address {$ia[$sname]}");
  125. } catch (PDOException $ie) {
  126. array_push($errors, "Failed to update address usage for $sname: '{$ie->getMessage()}'");
  127. }
  128. }
  129. if ($newa != 'NULL') {
  130. $sql = 'UPDATE ADDRESSES SET used=? WHERE address=?';
  131. try {
  132. $sth = $dbh->prepare($sql);
  133. $sth->execute(array('1', $newa));
  134. $sth->closeCursor();
  135. $logger->info("User {$ruser} allocated address {$newa} for {$sname}");
  136. } catch (PDOException $ie) {
  137. array_push($errors, "Failed to update address usage for $sname: '{$ie->getMessage()}'");
  138. }
  139. }
  140. } catch (PDOException $e) {
  141. array_push($errors, "Failed to update $sname: '{$e->getMessage()}'");
  142. }
  143. }
  144. }
  145. if (count($errors) == 0) {
  146. if ($exp[$sname] != '__BOGUS__') {
  147. @symlink(PORT_PROFILE_DIR.'/'.$exp[$sname], PORT_TMPL_DIR.'/devices/'."{$sname}-ports.tmpl");
  148. } else {
  149. @unlink(PORT_TMPL_DIR.'/devices/'."{$sname}-ports.tmpl");
  150. }
  151. }
  152. }
  153. }
  154. $wc = '';
  155. if (isset($_REQUEST['filter'])) {
  156. switch ($_REQUEST['filter']) {
  157. case 'a':
  158. $wc = '';
  159. break;
  160. case 'u':
  161. $wc = ' WHERE ip_address IS NULL';
  162. break;
  163. default:
  164. if (preg_match("/^pid:([\w\d-]+)/", $_REQUEST['filter'], $match)) {
  165. if (array_search($match[1], $ZTP_PIDS) === false) {
  166. $wc = '';
  167. } else {
  168. $wc = " WHERE pid = '{$match[1]}'";
  169. }
  170. } elseif (preg_match("/^mdf:([\w\d-_]+)/", $_REQUEST['filter'], $match)) {
  171. if (array_search($match[1], $MDFS) === false) {
  172. $wc = '';
  173. } else {
  174. $wc = ", ADDRESSES WHERE SWITCHES.ip_address = ADDRESSES.address AND ADDRESSES.location = '{$match[1]}'";
  175. }
  176. } else {
  177. $wc = '';
  178. }
  179. break;
  180. }
  181. }
  182. $res = null;
  183. if (isset($_REQUEST['switch_name'])) {
  184. $sql = "SELECT * FROM SWITCHES WHERE name LIKE '%{$_REQUEST['switch_name']}%' OR ip_address LIKE '%{$_REQUEST['switch_name']}%'";
  185. try {
  186. $res = $dbh->query($sql);
  187. } catch (PDOException $e) {
  188. echo "<p><font color=\"red\">Error querying for logical switches: {$e->getMessage()}</font></p>\r\n";
  189. exit(1);
  190. }
  191. } else {
  192. $sql = 'SELECT SWITCHES.* FROM SWITCHES '.$wc.' ORDER BY name';
  193. try {
  194. $res = $dbh->query($sql);
  195. } catch (PDOException $e) {
  196. echo "<p><font color=\"red\">Error querying for logical switches: {$e->getMessage()}</font></p>\r\n";
  197. exit(1);
  198. }
  199. }
  200. $switches = array();
  201. while ($row = $res->fetch()) {
  202. array_push($switches, $row);
  203. }
  204. $sql = 'SELECT ip_address FROM SWITCHES WHERE ip_address IS NOT NULL';
  205. try {
  206. $res = $dbh->query($sql);
  207. } catch (PDOException $e) {
  208. echo "<p><font color=\"red\">Error querying for used IP addresses: {$e->getMessage()}</font></p>\r\n";
  209. exit(1);
  210. }
  211. $used = array();
  212. while ($row = $res->fetch()) {
  213. $used[$row['ip_address']] = true;
  214. }
  215. $sql = 'SELECT * FROM EXCEPTIONS';
  216. try {
  217. $res = $dbh->query($sql);
  218. } catch (PDOException $e) {
  219. echo "<p><font color=\"red\">Error querying for exceptions: {$e->getMessage()}</font></p>\r\n";
  220. exit(1);
  221. }
  222. $exceptions = array();
  223. while ($row = $res->fetch()) {
  224. $exceptions[$row['name']] = array($row['pid'], $row['path']);
  225. }
  226. $filter = '';
  227. if (isset($_REQUEST['filter'])) {
  228. $filter = $_REQUEST['filter'];
  229. }
  230. print_header(TOOL_NAME.': Logical Switches');
  231. ?>
  232. <body class="flex">
  233. <script language="javascript">
  234. var deletes = 0;
  235. $(document).ready(function() {
  236. $('#devtable').DataTable( {
  237. "scrollY": "400px",
  238. "scrollCollapse": true,
  239. "paging":false
  240. } );
  241. } );
  242. </script>
  243. <div id="headswreg">
  244. <div class="apage">
  245. <div id="header">
  246. <h1><?=TOOL_NAME?>: Logical Switches</h1>
  247. </div>
  248. <br/>
  249. </div>
  250. </div>
  251. <div class="apage">
  252. <form method="POST" name="search_form" action="<?=$_SERVER['PHP_SELF']?>">
  253. <table class="noborder" summary="Filter Table">
  254. <tr>
  255. <td class="left_act">Show Only Switches In:
  256. <select name="filter" onChange="MM_jumpMenu('parent', this, 0)">
  257. <option value="<?=$_SERVER['PHP_SELF']?>?filter=a" <?=($filter == 'a') ? 'selected' : ''?>>All</option>
  258. <?php
  259. foreach ($MDFS as $m) {
  260. ?>
  261. <option value="<?=$_SERVER['PHP_SELF']?>?filter=<?=$m?>" <?=($filter == $m) ? 'selected' : ''?>><?=$m?></option>
  262. <?php
  263. }
  264. ?>
  265. <option value="<?=$_SERVER['PHP_SELF']?>?filter=u" <?=($filter == 'u') ? 'selected' : ''?>>No IP Assigned</option>
  266. <?php
  267. foreach ($ZTP_PIDS as $spid) {
  268. $selected = ($filter == "pid:$spid") ? 'selected' : ''; ?>
  269. <option value="<?=$_SERVER['PHP_SELF']?>?filter=pid:<?=$spid?>" <?=$selected?>><?=$spid?></option>
  270. <?php
  271. }
  272. ?>
  273. </select></td>
  274. <td class="right">Switch Search:
  275. <input type="text" size="16" name="switch_name" value="<?=(isset($_REQUEST['switch_name'])) ? $_REQUEST['switch_name'] : ''?>">
  276. <input type="image" value="Submit" name="search" src="/images/submit_button.png">
  277. </td>
  278. </tr>
  279. </table>
  280. </form>
  281. <br/>
  282. <table class="noborder" summary="Control Table">
  283. <tr>
  284. <td class="left"><a href="<?=$base?>/add_logic.php">Add Logical Switch</a> |
  285. <a href="<?=$base?>/index.php">Physical Switches</a> |
  286. <a href="<?=$base?>/port_profiles.php">Port Profiles</a></td>
  287. </tr>
  288. </table>
  289. <form name="mod_logic_switch_form" method="POST" action="<?=$_SERVER['PHP_SELF']?>" onSubmit='if (deletes > 0) { return confirm("Are you sure you want to delete these " + deletes + " logical switch(es)?"); }'>
  290. <?php
  291. foreach (array_merge($_GET, $_POST) as $name => $value) {
  292. if (!is_array($value)) {
  293. ?>
  294. <input type="hidden" name="<?=$name?>" value="<?=$value?>">
  295. <?php
  296. }
  297. }
  298. ?>
  299. <div class="fbtable">
  300. <table summary="Button Table" width="100%" cellspacing="0">
  301. <tr>
  302. <td><input type="reset" value="Reset" name="reset">
  303. <input type="submit" value="Submit" name="submit_top"></td>
  304. </tr>
  305. </table>
  306. </div>
  307. <br/>
  308. <div align="center">
  309. <?php
  310. foreach ($errors as $error) {
  311. ?>
  312. <p class="error"><?=$error?></p>
  313. <?php
  314. }
  315. ?>
  316. </div>
  317. <div class="fulltable">
  318. <table id="devtable" class="display compact" width="100%" cellspacing="0" summary="Logical Switch Table">
  319. <thead>
  320. <tr>
  321. <th class="headlink">Row No.</th>
  322. <th class="headlink">Delete?</th>
  323. <th class="headlink">Name</th>
  324. <th class="headlink">Is IDF?</th>
  325. <th class="headlink">IP Address</th>
  326. <th class="headlink">Product ID</th>
  327. <th class="headlink">SNMP Location</th>
  328. <th class="headlink">Ports Required</th>
  329. <th class="headlink">Port Profile</th>
  330. </tr>
  331. </thead>
  332. <tbody>
  333. <?php
  334. $i = 0;
  335. foreach ($switches as $row) {
  336. $name = $row['name']; ?>
  337. <tr>
  338. <td><?=$i + 1?>.</td>
  339. <td><input type="checkbox" name="delete[<?=$name?>]" value="1" onClick="if (this.checked == true) { deletes++; } else { deletes--; }"></td>
  340. <td><?=$name?></td>
  341. <td><input type="hidden" name="prev_is_idf[<?=$name?>]" value="<?=$row['is_idf']?>">
  342. <input type="checkbox" name="is_idf[<?=$name?>]" value="1" <?=($row['is_idf'] == 1) ? 'checked' : ''?>></td>
  343. <td><input type="hidden" name="prev_ip_address[<?=$name?>]" value="<?=$row['ip_address']?>">
  344. <input type="text" name="address[<?=$name?>]" size="16" value="<?=$row['ip_address']?>"></td>
  345. <td><input type="hidden" name="prev_pid[<?=$name?>]" value="<?=$row['pid']?>">
  346. <select name="pid[<?=$name?>]">
  347. <?php
  348. $pidarr = array_merge($ZTP_PIDS, array('__BOGUS__'));
  349. foreach ($ZTP_PIDS as $p) {
  350. $selected = ($row['pid'] == $p) ? 'selected' : '';
  351. if ($p == '__BOGUS__') {
  352. ?>
  353. <option value="__BOGUS__" <?=$selected?>>--Please Select--</option>
  354. <?php
  355. } else {
  356. ?>
  357. <option value="<?=$p?>" <?=$selected?>><?=$p?></option>
  358. <?php
  359. }
  360. } ?>
  361. </select></td>
  362. <td><input type="hidden" name="prev_location[<?=$name?>]" value="<?=$row['location']?>">
  363. <input type="text" name="location[<?=$name?>]" size="32" value="<?=$row['location']?>"></td>
  364. <td><?=$row['ports_required']?></td>
  365. <td><select id="exception_<?=$name?>" name="exception[<?=$name?>]">
  366. <option value="__BOGUS__">--Please Select--</option>
  367. <?php
  368. foreach ($exceptions as $exname => $exarr) {
  369. $path = $exarr[1];
  370. $expid = $exarr[0];
  371. $selected = '';
  372. if (file_exists(PORT_TMPL_DIR.'/devices/'."{$name}-ports.tmpl")) {
  373. $targ = readlink(PORT_TMPL_DIR.'/devices/'."{$name}-ports.tmpl");
  374. $tbase = basename($targ);
  375. if ($path == $tbase) {
  376. $selected = 'selected';
  377. }
  378. }
  379. if ($selected == '' && $expid != $row['pid'] && $expid != '__ANY__') {
  380. continue;
  381. } ?>
  382. <option value="<?=$path?>" <?=$selected?>><?=$exname?></option>
  383. <?php
  384. } ?>
  385. </select>&nbsp;<a href="#" onClick='var pp = document.getElementById("exception_<?=$name?>"); if (pp.value == "__BOGUS__") { alert("There is no port profile associated to this logical switch."); return false; } else { window.open("<?=$base?>/show_config.php?type=profile&cfg=" + pp.value + "&dname=<?=$name?>", "Config for profile " + pp.value, "height=650,width=980"); return false; }'><img src="/images/mag.gif" border="0" title="View port profile"></td>
  386. </tr>
  387. <?php
  388. ++$i;
  389. }
  390. ?>
  391. </tbody>
  392. </table>
  393. </div>
  394. <br/>
  395. <div class="fbtable">
  396. <table summary="Button Table" width="100%" cellspacing="0">
  397. <tr>
  398. <td><input type="reset" value="Reset" name="reset">
  399. <input type="submit" value="Submit" name="submit_bottom"></td>
  400. </tr>
  401. </table>
  402. </div>
  403. </form>
  404. </div>
  405. </body>
  406. </html>
  407. <?php
  408. cleanup();
  409. ?>