logicsw.php 14 KB

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