123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- <?php
- #-
- # Copyright (c) 2011-2015 Joe Clarke <jclarke@cisco.com>
- # All rights reserved.
- #
- # Redistribution and use in source and binary forms, with or without
- # modification, are permitted provided that the following conditions
- # are met:
- # 1. Redistributions of source code must retain the above copyright
- # notice, this list of conditions and the following disclaimer.
- # 2. Redistributions in binary form must reproduce the above copyright
- # notice, this list of conditions and the following disclaimer in the
- # documentation and/or other materials provided with the distribution.
- #
- # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- # SUCH DAMAGE.
- #
- #
- include_once 'db.inc.php';
- include_once 'swreg/swreg.inc.php';
- include_once 'swreg_web.inc.php';
- require_once 'MDB2.php';
- require_once 'Log.php';
- require_once 'functions.php';
- $dsn = "mysql://$db_user:$db_pass@$db_host/$db_name";
- $options = array('result_buffering', false);
- $dbh = MDB2::factory($dsn, $options);
- if (PEAR::isError($dbh)) {
- die($dbh->getMessage());
- }
- $dbh->setFetchMode(MDB2_FETCHMODE_ASSOC);
- $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : Logical Switches');
- if ($logger === FALSE) {
- die("Failed to open logfile.\n");
- }
- $mask = Log::UPTO(LOG_LEVEL);
- $logger->setMask($mask);
- $base = get_base($_SERVER['SCRIPT_NAME']);
- $errors = array();
- if (isset($_REQUEST['submit_top']) || isset($_REQUEST['submit_bottom'])) {
- $pia = $_REQUEST['prev_ip_address'];
- $ppid = $_REQUEST['prev_pid'];
- $ploc = $_REQUEST['prev_location'];
- $pidf = $_REQUEST['prev_is_idf'];
- $ia = $_REQUEST['address'];
- $pid = $_REQUEST['pid'];
- $loc = $_REQUEST['location'];
- $exp = $_REQUEST['exception'];
- $idf = $_REQUEST['is_idf'];
- $d = $_REQUEST['delete'];
- foreach ($pia as $sname => $value) {
- if ($d[$sname] == 1) {
- if ($ia[$sname] !== NULL && $ia[$sname] != '') {
- $sql = 'UPDATE ADDRESSES SET used=? WHERE address=?';
- $sth = $dbh->prepare($sql);
- if (PEAR::isError($sth)) {
- array_push($errors, "Failed to free IP address {$ia[$sname]}: {$sth->getUserInfo()}");
- } else {
- $res = $sth->execute(array('0', $ia[$sname]));
- $sth->free();
- if (PEAR::isError($res)) {
- array_push($errors, "Failed to free IP address {$ia[$sname]}: {$res->getUserInfo()}");
- }
- }
- }
- $sql = "DELETE FROM DEVICE_MAP WHERE assigned_switch='{$sname}'";
- $res = $dbh->query($sql);
- if (PEAR::isError($res)) {
- array_push($errors, "Failed to delete {$sname}: {$res->getUserInfo()}");
- }
- $sql = "DELETE FROM SWITCHES WHERE name='{$sname}'";
- $res = $dbh->query($sql);
- if (PEAR::isError($res)) {
- array_push($errors, "Failed to delete {$sname}: {$res->getUserInfo()}");
- } else {
- $logger->info("User {$_SERVER['REMOTE_USER']} deleted logical switch {$sname}");
- }
- if (count($errors) == 0) {
- @unlink(TFTPBOOT . "/" . "{$sname}-ports.tmpl");
- }
- continue;
- }
- if ($value != $ia[$sname] || $ploc[$sname] != $loc[$sname] || $ppid[$sname] != $pid[$sname] || $pidf[$sname] != $idf[$sname]) {
- if ($pid[$sname] == '__BOGUS__') {
- array_push($errors, "You must select a Product ID for {$sname}");
- }
- $sql = "SELECT address, reserved FROM ADDRESSES WHERE address='{$ia[$sname]}'";
- $res = $dbh->query($sql);
- $row = $res->fetchRow();
- $newa = "'{$ia[$sname]}'";
- if ($ia[$sname] != '' && !$row['address']) {
- array_push($errors, "IP address {$ia[$sname]} is not valid for switch {$sname}");
- } else if ($row['reserved'] == 1 && $idf[$sname] != 1) {
- array_push($errors, "IP address {$ia[$sname]} is reserved for IDF switches only");
- } else if ($ia[$sname] == '') {
- $newa = "NULL";
- }
- if (count($errors) == 0) {
- $sql = "UPDATE SWITCHES SET ip_address = $newa, location = '{$loc[$sname]}', pid = '{$pid[$sname]}', is_idf = '{$idf[$sname]}' WHERE name='{$sname}'";
- $res = $dbh->query($sql);
- if (PEAR::isError($res)) {
- array_push($errors, "Failed to update $sname: '{$res->getUserInfo()}'");
- } else {
- $logger->info("User {$_SERVER['REMOTE_USER']} updated logical switches: location={$loc[$sname]}, pid={$pid[$sname]}, is_idf={$idf[$sname]} for switch {$sname}");
- if ($is[$sname] != '') {
- $sql = 'UPDATE ADDRESSES SET used=? WHERE address=?';
- $sth = $dbh->prepare($sql);
- if (PEAR::isError($sth)) {
- array_push($errors, "Failed to update address usage for $sname: '{$sth->getUserInfo()}'");
- } else {
- $res = $sth->execute(array('0', $ia[$sname]));
- $sth->free();
- if (PEAR::isError($res)) {
- array_push($errors, "Failed to update address usage for $sname: '{$res->getUserInfo()}'");
- } else {
- $logger->info("User {$_SERVER['REMOTE_USER']} released address {$ia[$sname]}");
- }
- }
- }
- if ($newa != 'NULL') {
- $sql = 'UPDATE ADDRESSES SET used=? WHERE address=?';
- $sth = $dbh->prepare($sql);
- if (PEAR::isError($sth)) {
- array_push($errors, "Failed to update address usage for $sname: '{$sth->getUserInfo()}'");
- } else {
- $res = $sth->execute(array('1', $newa));
- $sth->free();
- if (PEAR::isError($res)) {
- array_push($errors, "Failed to update address usage for $sname: '{$res->getUserInfo()}'");
- } else {
- $logger->info("User {$_SERVER['REMOTE_USER']} allocated address {$newa} for {$sname}");
- }
- }
- }
- }
- }
- }
- if (count($errors) == 0) {
- if ($exp[$sname] != '__BOGUS__') {
- @symlink(PORT_PROFILE_DIR . "/" . $exp[$sname], PORT_TMPL_DIR . '/devices/' . "{$sname}-ports.tmpl");
- } else {
- @unlink(PORT_TMPL_DIR . '/devices/' . "{$sname}-ports.tmpl");
- }
- }
- }
- }
- $wc = '';
- if (isset($_REQUEST['filter'])) {
- switch ($_REQUEST['filter']) {
- case 'a':
- $wc = '';
- break;
- case 'u':
- $wc = " WHERE ip_address IS NULL";
- break;
- default:
- if (preg_match("/^pid:([\w\d-]+)/", $_REQUEST['filter'], $match)) {
- if (array_search($ZTP_PIDS, $match[1]) === FALSE) {
- $wc = '';
- } else {
- $wc = " WHERE pid = '{$match[1]}'";
- }
- } else if (preg_match("/^mdf:([\w\d-_]+/", $_REQUEST['filter'], $match)) {
- if (array_search($match[1], $MDFS) === FALSE) {
- $wc = '';
- } else {
- $wc = ", ADDRESSES WHERE SWITCHES.ip_address = ADDRESSES.address AND ADDRESSES.location = '{$match[1]}'";
- }
- } else {
- $wc = '';
- }
- break;
- }
- }
- if (isset($_REQUEST['switch_name'])) {
- $sql = "SELECT * FROM SWITCHES WHERE name LIKE '%{$_REQUEST['switch_name']}%' OR ip_address LIKE '%{$_REQUEST['switch_name']}%'";
- $res = $dbh->query($sql);
- if (PEAR::isError($res)) {
- echo "<p><font color=\"red\">Error querying for logical switches: {$res->getUserInfo()}</font></p>\r\n";
- exit(1);
- }
- } else {
- $sql = 'SELECT SWITCHES.* FROM SWITCHES ' . $wc . ' ORDER BY name';
- $res = $dbh->query($sql);
- if (PEAR::isError($res)) {
- echo "<p><font color=\"red\">Error querying for logical switches: {$res->getUserInfo()}</font></p>\r\n";
- exit(1);
- }
- }
- $switches = array();
- while ($row = $res->fetchRow()) {
- array_push($switches, $row);
- }
- $sql = 'SELECT ip_address FROM SWITCHES WHERE ip_address IS NOT NULL';
- $res = $dbh->query($sql);
- if (PEAR::isError($res)) {
- echo "<p><font color=\"red\">Error querying for used IP addresses: {$res->getUserInfo()}</font></p>\r\n";
- exit(1);
- }
- $used = array();
- while ($row = $res->fetchRow()) {
- $used[$row['ip_address']] = TRUE;
- }
- $sql = "SELECT * FROM EXCEPTIONS";
- $res = $dbh->query($sql);
- if (PEAR::isError($res)) {
- echo "<p><font color=\"red\">Error querying for exceptions: {$res->getUserInfo()}</font></p>\r\n";
- exit(1);
- }
- while ($row = $res->fetchRow()) {
- $exceptions[$row['name']] = array($row['pid'], $row['path']);
- }
- print_header(TOOL_NAME . ": Logical Switches");
- ?>
- <script language="javascript">
- var deletes = 0;
- </script>
- <body class="flex">
- <div id="headswreg">
- <div class="apage">
- <div id="header">
- <h1><?=TOOL_NAME?>: Logical Switches</h1>
- </div>
- <br/>
- <div>
- Total Logical Switches Displayed: <b><?=count($switches)?></b>
- </div>
- </div>
- </div>
- <div class="apage">
- <table class="noborder" summary="Filter Table">
- <tr>
- <td class="left_act">Show Only Switches In:
- <select name="filter" onChange="MM_jumpMenu('parent', this, 0)">
- <option value="<?=$_SERVER['PHP_SELF']?>?filter=a" <?=($_REQUEST['filter'] == 'a') ? 'selected' : ''?>>All</option>
- <?php
- foreach ($MDFS as $m) {
- ?>
- <option value="<?=$_SERVER['PHP_SELF']?>?filter=<?=$m?>" <?=($_REQUEST['filter'] == $m) ? 'selected' : ''?>><?=$m?></option>
- <?php
- }
- ?>
- <option value="<?=$_SERVER['PHP_SELF']?>?filter=u" <?=($_REQUEST['filter'] == 'u') ? 'selected' : ''?>>No IP Assigned</option>
- <?php
- foreach ($ZTP_PIDS as $spid) {
- $selected = ($_REQUEST['filter'] == "pid:$spid") ? 'selected' : '';
- ?>
- <option value="<?=$_SERVER['PHP_SELF']?>?filter=pid:<?=$spid?>" <?=$selected?>><?=$spid?></option>
- <?php
- }
- ?>
- </select></td>
- <form method="POST" name="search_form" action="<?=$_SERVER['PHP_SELF']?>">
- <td class="right">Switch Search:
- <input type="text" size="16" name="switch_name" value="<?=$_REQUEST['switch_name']?>">
- <input type="image" value="Submit" name="search" src="/images/submit_button.png">
- </td>
- </form>
- </tr>
- </table>
- <br/>
- <table class="noborder" summary="Control Table">
- <tr>
- <td class="left"><a href="<?=$base?>/add_logic.php">Add Logical Switch</a> |
- <a href="<?=$base?>/index.php">Physical Switches</a> |
- <a href="<?=$base?>/port_profiles.php">Port Profiles</a></td>
- </tr>
- </table>
- <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)?"); }'>
- <table class="swreg" summary="Button Table" class="full">
- <tr>
- <td><input type="reset" value="Reset" name="reset">
- <input type="submit" value="Submit" name="submit_top"></td>
- </tr>
- </table>
- <br/>
- <div align="center">
- <?php
- foreach ($errors as $error) {
- ?>
- <p class="error"><?=$error?></p>
- <?php
- }
- ?>
- </div>
- <table class="swreg" summary="Logical Switch Table" class="full">
- <tr>
- <th class="delete">Row No.</th>
- <th class="delete">Delete?</td>
- <th class="headlink">Name</th>
- <th class="headlink">Is IDF?</th>
- <th class="headlink">IP Address</th>
- <th class="headlink">Product ID</th>
- <th class="headlink">SNMP Location</th>
- <th class="headlink">Ports Required</th>
- <th class="headlink">Port Profile</th>
- </tr>
- <?php
- foreach (array_merge($_GET, $_POST) as $name => $value) {
- if (!is_array($value)) {
- ?>
- <input type="hidden" name="<?=$name?>" value="<?=$value?>">
- <?php
- }
- }
- $i = 0;
- foreach ($switches as $row) {
- $name = $row['name'];
- ?>
- <input type="hidden" name="prev_ip_address[<?=$name?>]" value="<?=$row['ip_address']?>">
- <input type="hidden" name="prev_pid[<?=$name?>]" value="<?=$row['pid']?>">
- <input type="hidden" name="prev_location[<?=$name?>]" value="<?=$row['location']?>">
- <input type="hidden" name="prev_is_idf[<?=$name?>]" value="<?=$row['is_idf']?>">
- <tr <?=get_row_color($i)?>>
- <td><?=$i + 1?>.</td>
- <td><input type="checkbox" name="delete[<?=$name?>]" value="1" onClick="if (this.checked == true) { deletes++; } else { deletes--; }"></td>
- <td class="wide"><?=$name?></td>
- <td><input type="checkbox" name="is_idf[<?=$name?>]" value="1" <?=($row['is_idf'] == 1) ? 'checked' : ''?>></td>
- <td><input type="text" name="address[<?=$name?>]" size="16" value="<?=$row['ip_address']?>"></td>
- <td><select name="pid[<?=$name?>]">
- <?php
- $pidarr = array_merge($ZTP_PIDS, array('__BOGUS__'));
- foreach ($ZTP_PIDS as $p) {
- $selected = ($row['pid'] == $p) ? "selected" : "";
- if ($p == '__BOGUS__') {
- ?>
- <option value="__BOGUS__" <?=$selected?>>--Please Select--</option>
- <?php
- } else {
- ?>
- <option value="<?=$p?>" <?=$selected?>><?=$p?></option>
- <?php
- }
- }
- ?>
- </select></td>
- <td><input type="text" name="location[<?=$name?>]" size="32" value="<?=$row['location']?>"></td>
- <td><?=$row['ports_required']?></td>
- <td><select id="exception_<?=$name?>" name="exception[<?=$name?>]">
- <option value="__BOGUS__">--Please Select--</option>
- <?php
- foreach ($exceptions as $exname => $exarr) {
- $path = $exarr[1];
- $expid = $exarr[0];
- $selected = "";
- if (file_exists(TFTPBOOT . "/{$name}-ports.tmpl")) {
- $targ = readlink(TFTPBOOT . "/{$name}-ports.tmpl");
- $tbase = basename($targ);
- if ($path == $tbase) {
- $selected = "selected";
- }
- }
- if ($selected == "" && $expid != $row['pid'] && $expid != '__ANY__') {
- continue;
- }
- ?>
- <option value="<?=$path?>" <?=$selected?>><?=$exname?></option>
- <?php
- }
- ?>
- </select> <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>
- </tr>
- <?php
- $i++;
- }
- ?>
- </table>
- <br/>
- <table class="swreg" summary="Button Table" class="full">
- <tr>
- <td><input type="reset" value="Reset" name="reset">
- <input type="submit" value="Submit" name="submit_bottom"></td>
- </tr>
- </table>
- </form>
- </div>
- </body>
- </html>
- <?php
- cleanup();
- ?>
|