// 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'; require_once 'Log.php'; require_once 'functions.php'; $dsn = "$db_driver:host=$db_host;dbname=$db_name"; $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; try { $dbh = new PDO($dsn, $db_user, $db_pass, $options); } catch (PDOException $e) { die($e->getMessage()); } $logger = Log::singleton('file', LOGFILE, TOOL_NAME . ' : Logical Switches'); if ($logger === false) { die("Failed to open logfile.\n"); } $mask = Log::MAX(LOG_LEVEL); $logger->setMask($mask); $base = get_base($_SERVER['SCRIPT_NAME']); $ruser = ''; if (isset($_SERVER['REMOTE_USER'])) { $ruser = $_SERVER['REMOTE_USER']; } $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']; $pexp = (isset($_REQUEST['prev_exception'])) ? $_REQUEST['prev_exception'] : array(); $ia = $_REQUEST['address']; $pid = $_REQUEST['pid']; $loc = $_REQUEST['location']; $exp = $_REQUEST['exception']; $idf = (isset($_REQUEST['is_idf'])) ? $_REQUEST['is_idf'] : array(); $d = (isset($_REQUEST['delete'])) ? $_REQUEST['delete'] : array(); $made_change = false; foreach ($pia as $sname => $value) { $curr_del = (isset($d[$sname])) ? $d[$sname] : 0; if ($curr_del == 1) { if ($ia[$sname] !== null && $ia[$sname] != '') { $sql = 'UPDATE ADDRESSES SET used=? WHERE address=?'; try { $sth = $dbh->prepare($sql); $sth->execute(array('0', $ia[$sname])); $sth->closeCursor(); } catch (PDOException $e) { array_push($errors, "Failed to free IP address {$ia[$sname]}: {$e->getMessage()}"); } } $sql = "DELETE FROM DEVICE_MAP WHERE assigned_switch='{$sname}'"; try { $dbh->query($sql); } catch (PDOException $e) { array_push($errors, "Failed to delete {$sname}: {$e->getMessage()}"); } $sql = "DELETE FROM SWITCHES WHERE name='{$sname}'"; try { $dbh->query($sql); $logger->info("User {$ruser} deleted logical switch {$sname}"); } catch (PDOException $e) { array_push($errors, "Failed to delete {$sname}: {$e->getMessage()}"); } if (count($errors) == 0) { $made_change = true; @unlink(PORT_TMPL_DIR . '/devices/' . "{$sname}-ports.tmpl"); } continue; } $curr_idf = (isset($idf[$sname])) ? $idf[$sname] : 0; if (!isset($ppid[$sname]) || $ppid[$sname] == '') { $ppid[$sname] = '__BOGUS__'; } if (!isset($pexp[$sname]) || $pexp[$sname] == '') { $pexp[$sname] = '__BOGUS__'; } if ($value != $ia[$sname] || $ploc[$sname] != $loc[$sname] || $ppid[$sname] != $pid[$sname] || $pidf[$sname] != $curr_idf) { 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 = null; try { $res = $dbh->query($sql); } catch (PDOException $e) { } $row = $res->fetch(); $newa = "'{$ia[$sname]}'"; if ($ia[$sname] != '' && !$row['address']) { array_push($errors, "IP address {$ia[$sname]} is not valid for switch {$sname}"); } elseif ((ADDRESS_SCHEME == 'IDF' && $row['reserved'] == 1 && $curr_idf != 1) || (ADDRESS_SCHEME != 'IDF' && ADDRESS_SCHEME != 'DNS' && $row['reserved'] == 1)) { array_push($errors, "IP address {$ia[$sname]} is reserved and cannot be used for this switch"); } elseif ($ia[$sname] == '') { $newa = 'NULL'; } if (count($errors) == 0) { $sql = "UPDATE SWITCHES SET ip_address = $newa, location = '{$loc[$sname]}', pid = '{$pid[$sname]}', is_idf = '{$curr_idf}' WHERE name='{$sname}'"; try { $res = $dbh->query($sql); $logger->info("User {$ruser} updated logical switches: location={$loc[$sname]}, pid={$pid[$sname]}, is_idf={$curr_idf} for switch {$sname}"); if ($ia[$sname] != '') { $sql = 'UPDATE ADDRESSES SET used=? WHERE address=?'; try { $sth = $dbh->prepare($sql); $sth->execute(array('0', $ia[$sname])); $sth->closeCursor(); $logger->info("User {$ruser} released address {$ia[$sname]}"); } catch (PDOException $ie) { array_push($errors, "Failed to update address usage for $sname: '{$ie->getMessage()}'"); } } if ($newa != 'NULL') { $sql = 'UPDATE ADDRESSES SET used=? WHERE address=?'; try { $sth = $dbh->prepare($sql); $sth->execute(array('1', $newa)); $sth->closeCursor(); $logger->info("User {$ruser} allocated address {$newa} for {$sname}"); } catch (PDOException $ie) { array_push($errors, "Failed to update address usage for $sname: '{$ie->getMessage()}'"); } } } catch (PDOException $e) { array_push($errors, "Failed to update $sname: '{$e->getMessage()}'"); } } } if (count($errors) == 0) { $made_change = true; if ($pexp[$sname] != $exp[$sname]) { 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"); } } } } if ($made_change) { $now = time(); $sql = 'UPDATE SYS_PROPERTIES SET value=? WHERE name=?'; try { $sth = $dbh->prepare($sql); $sth->execute([$now, 'last_logic_update']); $sth->closeCursor(); $logger->info("User {$ruser} updated the last logic date"); } catch (PDOException $e) { array_push($errors, "Failed to update the last logic update date: '{$e->getMessage()}'"); } } } $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($match[1], $ZTP_PIDS) === false) { $wc = ''; } else { $wc = " WHERE pid = '{$match[1]}'"; } } elseif (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; } } $res = null; if (isset($_REQUEST['switch_name'])) { $sql = 'SELECT * FROM SWITCHES WHERE name LIKE :swname1 OR ip_address LIKE :swname2 ORDER BY name'; try { $res = $dbh->prepare($sql); $res->execute(['swname1' => "%{$_REQUEST['switch_name']}%", 'swname2' => "%{$_REQUEST['switch_name']}%"]); } catch (PDOException $e) { echo "

Error querying for logical switches: {$e->getMessage()}

\r\n"; exit(1); } } else { $sql = 'SELECT SWITCHES.* FROM SWITCHES ' . $wc . ' ORDER BY name'; try { $res = $dbh->query($sql); } catch (PDOException $e) { echo "

Error querying for logical switches: {$e->getMessage()}

\r\n"; exit(1); } } $switches = array(); while ($row = $res->fetch()) { array_push($switches, $row); } $sql = 'SELECT ip_address FROM SWITCHES WHERE ip_address IS NOT NULL'; try { $res = $dbh->query($sql); } catch (PDOException $e) { echo "

Error querying for used IP addresses: {$e->getMessage()}

\r\n"; exit(1); } $used = array(); while ($row = $res->fetch()) { $used[$row['ip_address']] = true; } $sql = 'SELECT * FROM EXCEPTIONS'; try { $res = $dbh->query($sql); } catch (PDOException $e) { echo "

Error querying for exceptions: {$e->getMessage()}

\r\n"; exit(1); } $exceptions = array(); while ($row = $res->fetch()) { $exceptions[$row['name']] = array($row['pid'], $row['path']); } $filter = ''; if (isset($_REQUEST['filter'])) { $filter = $_REQUEST['filter']; } print_header(TOOL_NAME . ': Logical Switches'); ?>

Show Only Switches In: Switch Search:

Add Logical Switch | Physical Switches | Port Profiles
$value) { if (!is_array($value)) { ?>

Row No. Delete? Name Is IDF? IP Address Product ID SNMP Location Port Profile
. >