Skip to content

Commit

Permalink
Feature: Add MAC address lookup widget
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryAllan committed Mar 27, 2022
1 parent d3503d3 commit 2104e44
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 22 deletions.
59 changes: 59 additions & 0 deletions app/dashboard/widgets/mac-lookup.php
@@ -0,0 +1,59 @@
<?php
# required functions
if (!isset($User)) {
require_once(dirname(__FILE__) . '/../../../functions/functions.php');
# classes
$Database = new Database_PDO;
$User = new User ($Database);
}

# user must be authenticated
$User->check_user_session();

# if direct request that redirect to tools page
if ($_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest") {
header("Location: " . create_link("tools", "mac-lookup"));
}
?>

<script>
$(document).ready(function() {
if ($("[rel=tooltip]").length) {
$("[rel=tooltip]").tooltip();
}

//submit form
$('form#mac_lookup').submit(function() {
var macvendorData = $(this).serialize();
$.post('app/tools/mac-lookup/results.php', macvendorData, function(data) {
$('div.macvendorResult').html(data).fadeIn('fast');
}).fail(function(jqxhr, textStatus, errorThrown) {
showError(jqxhr.statusText + "<br>Status: " + textStatus + "<br>Error: " + errorThrown);
});
return false;
});
});
</script>

<div class="container-fluid" style='padding-top:5px'>

<!-- search form -->
<form id="mac_lookup" name="search" class='form-inline' role="form" style="margin-bottom:20px;" method="post">
<div class='input-group'>
<div class='form-group'>
<input class="search input-md form-control" name="mac" placeholder="<?php print _('MAC address'); ?>" value='<?php print @escape_input($_POST['mac']); ?>' type="text" autofocus="autofocus" style='width:250px;'>
<span class="input-group-btn">
<button type="submit" class="btn btn-md btn-default"><?php print _('search'); ?></button>
</span>
</div>
</div>
</form>

<hr>


<div class="macvendorResult">
<span class="text-muted"><?php print _('Please enter valid MAC address'); ?></span>
</div>

</div>
38 changes: 22 additions & 16 deletions app/tools/mac-lookup/results.php
@@ -1,28 +1,34 @@
<?php
if (!isset($User)) {
require_once(dirname(__FILE__) . '/../../../functions/functions.php');
# classes
$Database = new Database_PDO;
$User = new User ($Database);
$Result = new Result();
}

# verify that user is logged in
$User->check_user_session();

// trim and escape
$mac = escape_input(trim($_POST['mac']));
$mac = isset($_POST['mac']) ? escape_input(trim($_POST['mac'])) : "";

// validate
if($User->validate_mac ($mac)===false) {
$Result->show("warning", _("Invalid MAC address provided")." - ".$mac, false);
}
else {
if ($User->validate_mac($mac) === false) {
$Result->show("warning", _("Invalid MAC address provided") . " - " . $mac, false);
} else {
// check
$mac_vendor = $User->get_mac_address_vendor_details ($mac);
$mac_vendor = $User->get_mac_address_vendor_details($mac, $prefix);

// print
if($mac_vendor=="") {
$Result->show("info", _("No matches found for prefix")." ".$mac, false);
}
else {
$mac = strtoupper($User->reformat_mac_address ($mac, 1));
$mac_partial = explode(":", $mac);
if ($mac_vendor == "") {
$Result->show("info", _("No matches found for prefix") . " " . $mac, false);
} else {
$mac = strtoupper($User->reformat_mac_address($mac, 1));

// print
print "<div style='font-size:16px;'>Vendor: <strong>".$mac_vendor."</strong></div><hr>";
print "Prefix: ".$mac_partial[0].":".$mac_partial[1].":".$mac_partial[2]."<br>";
print "MAC: ".$mac;
print "<div style='font-size:16px;'>Vendor: <strong class='clipboard'>" . escape_input($mac_vendor) . "</strong></div><hr>";
print "Prefix: " . escape_input($prefix) . "<br>";
print "MAC: " . $mac;
}
}
}
5 changes: 3 additions & 2 deletions db/SCHEMA.sql
Expand Up @@ -588,7 +588,8 @@ VALUES
(15, 'Locations', 'Shows map of locations', 'locations', NULL, 'yes', '6', 'no', 'yes'),
(16, 'Bandwidth calculator', 'Calculate bandwidth', 'bw_calculator', NULL, 'no', '6', 'no', 'yes'),
(17, 'Customers', 'Shows customer list', 'customers', NULL, 'yes', '6', 'no', 'yes'),
(18, 'User Instructions', 'Shows user instructions', 'instructions', NULL, 'yes', '6', 'no', 'yes');
(18, 'User Instructions', 'Shows user instructions', 'instructions', NULL, 'yes', '6', 'no', 'yes'),
(19, 'MAC lookup', 'Shows MAC address vendor', 'mac-lookup', NULL, 'yes', '6', 'no', 'yes');



Expand Down Expand Up @@ -1058,4 +1059,4 @@ CREATE TABLE `nominatim_cache` (
# ------------------------------------------------------------

UPDATE `settings` SET `version` = "1.5";
UPDATE `settings` SET `dbversion` = 38;
UPDATE `settings` SET `dbversion` = 39;
4 changes: 3 additions & 1 deletion functions/classes/class.Common.php
Expand Up @@ -1798,9 +1798,10 @@ public function show_mac_and_vendor($mac) {
*
* @method get_mac_address_vendor
* @param string $mac
* @param string &$prefix
* @return string
*/
public function get_mac_address_vendor_details($mac) {
public function get_mac_address_vendor_details($mac, &$prefix=null) {
if (strlen($mac) < 4 || !$this->validate_mac($mac)) {
return "";
}
Expand All @@ -1818,6 +1819,7 @@ public function get_mac_address_vendor_details($mac) {

while (strlen($search_mac) > 0) {
if (isset($this->mac_address_vendors[$search_mac])) {
$prefix = implode(":", str_split(strtoupper($search_mac), 2));
return $this->mac_address_vendors[$search_mac];
}

Expand Down
10 changes: 7 additions & 3 deletions functions/upgrade_queries/upgrade_queries_1.5.php
Expand Up @@ -170,7 +170,6 @@

// Truncated change log errors, module defaults, MariaDB Galera compatibility
$upgrade_queries["1.5.38"] = [];

$upgrade_queries["1.5.38"][] = "ALTER TABLE `logs` CHANGE `command` `command` text DEFAULT NULL;";
$upgrade_queries["1.5.38"][] = "ALTER TABLE `logs` CHANGE `details` `details` text DEFAULT NULL;";
$upgrade_queries["1.5.38"][] = "ALTER TABLE `changelog` CHANGE `caction` `caction` ENUM('add','edit','delete','truncate','resize','perm_change') NOT NULL DEFAULT 'edit';";
Expand All @@ -179,6 +178,11 @@
$upgrade_queries["1.5.38"][] = "ALTER TABLE `users` CHANGE `module_permissions` `module_permissions` varchar(255) DEFAULT '{\"vlan\":\"1\",\"l2dom\":\"1\",\"vrf\":\"1\",\"pdns\":\"1\",\"circuits\":\"1\",\"racks\":\"1\",\"nat\":\"1\",\"pstn\":\"1\",\"customers\":\"1\",\"locations\":\"1\",\"devices\":\"1\",\"routing\":\"1\",\"vaults\":\"1\"}';";
$upgrade_queries["1.5.38"][] = "ALTER TABLE `firewallZoneSubnet` ADD PRIMARY KEY (`zoneId`,`subnetId`);";
$upgrade_queries["1.5.38"][] = "ALTER TABLE `circuitsLogicalMapping` ADD PRIMARY KEY (`logicalCircuit_id`, `circuit_id`);";

$upgrade_queries["1.5.38"][] = "-- Database version bump";
$upgrade_queries["1.5.38"][] = "UPDATE `settings` set `dbversion` = '38';";
$upgrade_queries["1.5.38"][] = "UPDATE `settings` set `dbversion` = '38';";

// MAC lookup widget
$upgrade_queries["1.5.39"] = [];
$upgrade_queries["1.5.39"][] = "INSERT INTO `widgets` (`wtitle`, `wdescription`, `wfile`, `wparams`, `whref`, `wsize`, `wadminonly`, `wactive`) VALUES ('MAC lookup', 'Shows MAC address vendor', 'mac-lookup', NULL, 'yes', '6', 'no', 'yes');";
$upgrade_queries["1.5.39"][] = "-- Database version bump";
$upgrade_queries["1.5.39"][] = "UPDATE `settings` set `dbversion` = '39';";
1 change: 1 addition & 0 deletions misc/CHANGELOG
Expand Up @@ -11,6 +11,7 @@
+ Added Vaults (Certificate andf password storing);
+ Added Tools->Duplicate subnets & IP page;
+ Added config.php offline_mode to disable server-side Internet lookups (#3462);
+ Added MAC vendor lookup widget;

Enhancements, changes:
----------------------------
Expand Down

0 comments on commit 2104e44

Please sign in to comment.