Skip to content
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@
* issue#246: Fix Cisco device collection
* issue#248: Cacti process control, terminate dns resolvet when is nothing to do
* issue#250: Disable automatic MIBs load
* issue#285: Reject DNS server settings that are not empty or an IP address
* issue#288: Bind the device and site id in device and site removal
* issue#290: Report when none of the selected MAC addresses is valid
* issue#292: Compare the interfaces report site filter with '-1'
* issue#293: Look up a MAC revoke selection in one query
* issue#295: Remove stale scanner process entries in one delete keyed on the device
* issue#317: Leave the SNMP community string, auth password and privacy passphrase out of the device export; an import keeps the stored values, and a new install needs them entered
* issue#349: Keep punctuation in the port name filter
* issue#350: Leave an empty hardware address empty
* issue#353: Stop re-querying addresses that have no PTR record
* issue#355: Keep compressed IPv6 text intact instead of reading it as hexadecimal IPv4
* issue#356: Convert binary IP addresses before trimming, so a final whitespace byte is kept
* issue#357: Prevent repeated database setup from duplicating the Default site
* issue: Convert plugin bootstrap include/include_once calls to fail-fast require/require_once across the web, CLI, and hook entry points
* issue: Relocate the per-theme override stylesheets from themes/<theme>/ to css/<theme>/ alongside the base css/mactrack.css
Expand Down
4 changes: 4 additions & 0 deletions locales/po/cacti.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2088,6 +2088,10 @@ msgstr ""
msgid "Revoke"
msgstr ""

#: mactrack_view_macs.php
msgid "None of the selected MAC Addresses were valid, so no action was taken."
msgstr ""

#: mactrack_view_macs.php
#, php-format
msgid "The following MAC Addresses Could not be revoked because they are members of Group Authorizations %s"
Expand Down
37 changes: 18 additions & 19 deletions mactrack_actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,15 @@ function api_mactrack_device_save($device_id, $host_id, $site_id, $hostname,
* @return void
*/
function api_mactrack_device_remove($device_id): void {
db_execute('DELETE FROM mac_track_devices WHERE device_id=' . $device_id);
db_execute('DELETE FROM mac_track_aggregated_ports WHERE device_id=' . $device_id);
db_execute('DELETE FROM mac_track_interfaces WHERE device_id=' . $device_id);
db_execute('DELETE FROM mac_track_ips WHERE device_id=' . $device_id);
db_execute('DELETE FROM mac_track_ports WHERE device_id=' . $device_id);
db_execute('DELETE FROM mac_track_processes WHERE device_id=' . $device_id);
db_execute('DELETE FROM mac_track_processes WHERE device_id=' . $device_id);
db_execute('DELETE FROM mac_track_temp_ports WHERE device_id=' . $device_id);
db_execute('DELETE FROM mac_track_vlans WHERE device_id=' . $device_id);
db_execute('DELETE FROM mac_track_interface_graphs WHERE device_id=' . $device_id);
db_execute_prepared('DELETE FROM mac_track_devices WHERE device_id = ?', [$device_id]);
db_execute_prepared('DELETE FROM mac_track_aggregated_ports WHERE device_id = ?', [$device_id]);
db_execute_prepared('DELETE FROM mac_track_interfaces WHERE device_id = ?', [$device_id]);
db_execute_prepared('DELETE FROM mac_track_ips WHERE device_id = ?', [$device_id]);
db_execute_prepared('DELETE FROM mac_track_ports WHERE device_id = ?', [$device_id]);
db_execute_prepared('DELETE FROM mac_track_processes WHERE device_id = ?', [$device_id]);
db_execute_prepared('DELETE FROM mac_track_temp_ports WHERE device_id = ?', [$device_id]);
db_execute_prepared('DELETE FROM mac_track_vlans WHERE device_id = ?', [$device_id]);
db_execute_prepared('DELETE FROM mac_track_interface_graphs WHERE device_id = ?', [$device_id]);
}

/**
Expand Down Expand Up @@ -203,15 +202,15 @@ function api_mactrack_site_save($site_id, $site_name, $customer_contact, $netops
* @return void
*/
function api_mactrack_site_remove($site_id): void {
db_execute('DELETE FROM mac_track_sites WHERE site_id=' . $site_id);
db_execute('DELETE FROM mac_track_devices WHERE site_id=' . $site_id);
db_execute('DELETE FROM mac_track_aggregated_ports WHERE site_id=' . $site_id);
db_execute('DELETE FROM mac_track_interfaces WHERE site_id=' . $site_id);
db_execute('DELETE FROM mac_track_ips WHERE site_id=' . $site_id);
db_execute('DELETE FROM mac_track_ip_ranges WHERE site_id=' . $site_id);
db_execute('DELETE FROM mac_track_ports WHERE site_id=' . $site_id);
db_execute('DELETE FROM mac_track_temp_ports WHERE site_id=' . $site_id);
db_execute('DELETE FROM mac_track_vlans WHERE site_id=' . $site_id);
db_execute_prepared('DELETE FROM mac_track_sites WHERE site_id = ?', [$site_id]);
db_execute_prepared('DELETE FROM mac_track_devices WHERE site_id = ?', [$site_id]);
db_execute_prepared('DELETE FROM mac_track_aggregated_ports WHERE site_id = ?', [$site_id]);
db_execute_prepared('DELETE FROM mac_track_interfaces WHERE site_id = ?', [$site_id]);
db_execute_prepared('DELETE FROM mac_track_ips WHERE site_id = ?', [$site_id]);
db_execute_prepared('DELETE FROM mac_track_ip_ranges WHERE site_id = ?', [$site_id]);
db_execute_prepared('DELETE FROM mac_track_ports WHERE site_id = ?', [$site_id]);
db_execute_prepared('DELETE FROM mac_track_temp_ports WHERE site_id = ?', [$site_id]);
db_execute_prepared('DELETE FROM mac_track_vlans WHERE site_id = ?', [$site_id]);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions mactrack_view_interfaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ function mactrack_filter_table(): void {
<?php
$sql_where = '';

if (get_request_var('site_id') != -1) {
if (get_request_var('site_id') != '-1') {
$sql_where .= ' WHERE mac_track_devices.site_id=' . get_filter_request_var('site_id');
} else {
$sql_where = '';
Expand Down Expand Up @@ -754,7 +754,7 @@ function mactrack_filter_table(): void {
<?php
$sql_where = '';

if (get_request_var('site_id') != -1) {
if (get_request_var('site_id') != '-1') {
$sql_where .= ($sql_where != '' ? ' AND ' : 'WHERE ') . 'site_id=' . get_filter_request_var('site_id');
}

Expand Down
48 changes: 25 additions & 23 deletions mactrack_view_macs.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,31 +123,33 @@ function form_actions(): void {
}
}

if (cacti_sizeof($selected_items)) {
if (get_request_var('drp_action') == '1') { // Authorize
if (cacti_sizeof($selected_items)) {
foreach ($selected_items as $mac=>$ip) {
api_mactrack_authorize_mac_addresses($mac, $ip);
}
}
} elseif (get_request_var('drp_action') == '2') { // Revoke
$errors = '';

if (cacti_sizeof($selected_items)) {
foreach ($selected_items as $mac=>$ip) {
$mac_found = db_fetch_cell_prepared('SELECT mac_address FROM mac_track_macauth WHERE mac_address = ?', [$mac]);

if ($mac_found) {
api_mactrack_revoke_mac_addresses($mac);
} else {
$errors .= ', ' . mactrack_format_mac($mac);
}
}
if (!cacti_sizeof($selected_items)) {
raise_message('mactrack_macs_invalid', __('None of the selected MAC Addresses were valid, so no action was taken.', 'mactrack'), MESSAGE_LEVEL_ERROR);
} elseif (get_request_var('drp_action') == '1') { // Authorize
foreach ($selected_items as $mac=>$ip) {
api_mactrack_authorize_mac_addresses($mac, $ip);
}
} elseif (get_request_var('drp_action') == '2') { // Revoke
// one lookup for the whole selection instead of one per MAC; the
// column compares case-insensitively, so the lookup keys do too
$macs = array_map('strval', array_keys($selected_items));
$authorized = array_flip(array_map('strtoupper', array_column(db_fetch_assoc_prepared('SELECT mac_address
FROM mac_track_macauth
WHERE mac_address IN (' . implode(',', array_fill(0, count($macs), '?')) . ')',
$macs), 'mac_address')));

$errors = [];

foreach ($macs as $mac) {
if (isset($authorized[strtoupper($mac)])) {
api_mactrack_revoke_mac_addresses($mac);
} else {
$errors[] = mactrack_format_mac($mac);
}
}

if ($errors) {
$_SESSION['sess_messages'] = __('The following MAC Addresses Could not be revoked because they are members of Group Authorizations %s', $errors, 'mactrack');
}
if (cacti_sizeof($errors)) {
raise_message('mactrack_macs_revoke', __('The following MAC Addresses Could not be revoked because they are members of Group Authorizations %s', implode(', ', $errors), 'mactrack'), MESSAGE_LEVEL_WARN);
}
}

Expand Down
16 changes: 11 additions & 5 deletions poller_mactrack.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,11 @@ function clear_old_processes($site_id): void {
$old_procs = db_fetch_assoc_prepared($sql, $params);

if (cacti_sizeof($old_procs)) {
$device_ids = [];

foreach ($old_procs as $p) {
$process_id = intval($p['process_id']);
$process_id = intval($p['process_id']);
$device_ids[] = intval($p['device_id']);

if ($process_id > 0) {
if (strstr(PHP_OS, 'WIN')) {
Expand All @@ -474,11 +477,14 @@ function clear_old_processes($site_id): void {
} else {
cacti_log("WARNING: Removing Hung Mactrack Process Entry for Device '" . $p['device_id'] . "' With Status '" . $p['status'] . "'");
}

db_execute_prepared('DELETE FROM mac_track_processes
WHERE process_id = ?',
[$process_id]);
}

// device_id is the key; a PID of 0 would also match every entry still
// waiting for one, and the age check spares an entry re-queued meanwhile
db_execute_prepared('DELETE FROM mac_track_processes
WHERE device_id IN (' . implode(',', array_fill(0, count($device_ids), '?')) . ')
AND start_date < ?',
array_merge($device_ids, [$delete_time]));
}
}

Expand Down
31 changes: 27 additions & 4 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,25 @@ function mactrack_poller_bottom() {
exec_background($command_string, $extra_args);
}

/**
* Settings filter for the DNS server fields: an empty value (no server) or a
* valid IPv4 or IPv6 address is accepted, anything else fails the save.
*
* @param mixed $value The submitted value.
*
* @return false|string The value, or false to reject it.
*/
function mactrack_validate_dns_address($value): false|string {
// settings.php stores the raw request value, so validate it untrimmed
$value = (string) $value;

if ($value === '' || filter_var($value, FILTER_VALIDATE_IP) !== false) {
return $value;
}

return false;
}

/**
* Builds this plugin's Console "Settings > Mactrack" configuration
* tab's field definitions (scanning frequency, concurrent processes,
Expand Down Expand Up @@ -771,16 +790,20 @@ function mactrack_config_settings() {
'description' => __('Enter the primary DNS IP Address to utilize for reverse lookups.', 'mactrack'),
'method' => 'textbox',
'default' => '',
'max_length' => '30',
'size' => '18'
'max_length' => '45',
'size' => '18',
'filter' => FILTER_CALLBACK,
'options' => ['options' => 'mactrack_validate_dns_address']
],
'mt_dns_secondary' => [
'friendly_name' => __('Secondary DNS IP Address', 'mactrack'),
'description' => __('Enter the secondary DNS IP Address to utilize for reverse lookups.', 'mactrack'),
'method' => 'textbox',
'default' => '',
'max_length' => '30',
'size' => '18'
'max_length' => '45',
'size' => '18',
'filter' => FILTER_CALLBACK,
'options' => ['options' => 'mactrack_validate_dns_address']
],
'mt_dns_timeout' => [
'friendly_name' => __('DNS Timeout', 'mactrack'),
Expand Down
2 changes: 1 addition & 1 deletion tests/Security/PreparedStatementConsistencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
'lib/mactrack_extreme.php' => 1,
'lib/mactrack_functions.php' => 28,
'lib/mactrack_h3c_3com.php' => 1,
'mactrack_actions.php' => 19,
'mactrack_actions.php' => 0,
'mactrack_convert.php' => 9,
'mactrack_device_types.php' => 11,
'mactrack_devices.php' => 7,
Expand Down
51 changes: 51 additions & 0 deletions tests/Support/load_function.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/

/*
* Some plugin scripts handle the request or run the poller as soon as they
* are included, so a test cannot require them. This copies one function out
* of such a script into a temporary file and loads that instead.
*/

/**
* Load a single top-level function from a plugin script.
*
* @param string $file Path relative to the plugin root.
* @param string $function Function name.
*
* @return void
*/
function mactrack_test_load_function(string $file, string $function): void {
if (function_exists($function)) {
return;
}

$source = (string) file_get_contents(dirname(__DIR__, 2) . '/' . $file);
$start = strpos($source, "\nfunction $function(");

if ($start === false) {
throw new RuntimeException("$function() not found in $file");
}

$end = strpos($source, "\n}\n", $start);
$path = tempnam(sys_get_temp_dir(), 'mactrack_fn_');

file_put_contents($path, "<?php\n" . substr($source, $start, $end - $start + 3));

require $path;

unlink($path);
}
87 changes: 87 additions & 0 deletions tests/Unit/DeviceRemoveAndStaleProcessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/

/*
* Device and site removal bind the id instead of concatenating it (#288),
* and the stale-process sweep removes the rows it found in one statement
* keyed on device_id (#295).
*/

require_once dirname(__DIR__, 2) . '/mactrack_actions.php';

/*
* poller_mactrack.php runs the poller when it is included, so load only the
* function under test from its source.
*/
require_once dirname(__DIR__) . '/Support/load_function.php';

mactrack_test_load_function('poller_mactrack.php', 'clear_old_processes');

beforeEach(function () {
$GLOBALS['__test_db_calls'] = [];
$GLOBALS['__test_next_returns'] = [];
$GLOBALS['__test_config_options'] = ['mt_script_runtime' => '10'];
});

test('removing a device binds the device id in every delete', function () {
api_mactrack_device_remove('7 OR 1=1');

$calls = $GLOBALS['__test_db_calls'];

expect($calls)->toHaveCount(9);

foreach ($calls as $call) {
expect($call['fn'])->toBe('db_execute_prepared')
->and($call['sql'])->toEndWith('WHERE device_id = ?')
->and($call['params'])->toBe(['7 OR 1=1']);
}
});

test('removing a site binds the site id in every delete', function () {
api_mactrack_site_remove(3);

$calls = $GLOBALS['__test_db_calls'];

expect($calls)->toHaveCount(9)
->and(array_column($calls, 'fn'))->each->toBe('db_execute_prepared')
->and(array_column($calls, 'params'))->each->toBe([3]);
});

test('stale process entries are removed in one delete keyed on device_id', function () {
mactrack_test_queue_return('db_fetch_assoc_prepared', [
['device_id' => '4', 'process_id' => null, 'status' => 'Queued'],
['device_id' => '9', 'process_id' => '0', 'status' => 'Running'],
]);

clear_old_processes(0);

$deletes = array_values(array_filter($GLOBALS['__test_db_calls'], function ($call) {
return stripos($call['sql'], 'DELETE') !== false;
}));

expect($deletes)->toHaveCount(1)
->and(preg_replace('/\s+/', ' ', $deletes[0]['sql']))->toBe('DELETE FROM mac_track_processes WHERE device_id IN (?,?) AND start_date < ?')
->and(array_slice($deletes[0]['params'], 0, 2))->toBe([4, 9])
->and($deletes[0]['params'][2])->toBe($GLOBALS['__test_db_calls'][0]['params'][0]);
});

test('no delete runs when there are no stale processes', function () {
clear_old_processes(2);

$fns = array_column($GLOBALS['__test_db_calls'], 'fn');

expect($fns)->toBe(['db_fetch_assoc_prepared']);
});
Loading
Loading