From ca1491d23802782441c122c8625af143b965d7c4 Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Wed, 11 Nov 2026 19:31:15 -0800 Subject: [PATCH 1/7] fix(mactrack): bind the id in device and site removal api_mactrack_device_remove() and api_mactrack_site_remove() concatenated the id into each DELETE; bind it instead and drop the repeated mac_track_processes delete. Fixes #288 Signed-off-by: Thomas Vincent --- mactrack_actions.php | 37 +++++++++---------- .../PreparedStatementConsistencyTest.php | 2 +- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/mactrack_actions.php b/mactrack_actions.php index 5d7186a1..01612b5e 100644 --- a/mactrack_actions.php +++ b/mactrack_actions.php @@ -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]); } /** @@ -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]); } /** diff --git a/tests/Security/PreparedStatementConsistencyTest.php b/tests/Security/PreparedStatementConsistencyTest.php index 20a9bf2a..dcd6aab5 100644 --- a/tests/Security/PreparedStatementConsistencyTest.php +++ b/tests/Security/PreparedStatementConsistencyTest.php @@ -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, From 9debdd7139d55dc8f6f70329b95d74509e11e8cd Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Wed, 11 Nov 2026 19:31:16 -0800 Subject: [PATCH 2/7] fix(mactrack): remove stale process entries in one delete clear_old_processes() deleted each entry by process_id, so an entry with no PID also removed every other entry still waiting for one. Delete the rows it found by device_id, the table key, in one statement. Fixes #295 Signed-off-by: Thomas Vincent --- poller_mactrack.php | 16 +++- .../Unit/DeviceRemoveAndStaleProcessTest.php | 91 +++++++++++++++++++ 2 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 tests/Unit/DeviceRemoveAndStaleProcessTest.php diff --git a/poller_mactrack.php b/poller_mactrack.php index 4bb1d13d..5ed2adfe 100644 --- a/poller_mactrack.php +++ b/poller_mactrack.php @@ -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')) { @@ -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])); } } diff --git a/tests/Unit/DeviceRemoveAndStaleProcessTest.php b/tests/Unit/DeviceRemoveAndStaleProcessTest.php new file mode 100644 index 00000000..9e08fc70 --- /dev/null +++ b/tests/Unit/DeviceRemoveAndStaleProcessTest.php @@ -0,0 +1,91 @@ + '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']); +}); From 25b2b8ec12c935c5cb365e18c47be3a9bea97cbe Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Wed, 11 Nov 2026 19:31:17 -0800 Subject: [PATCH 3/7] fix(mactrack): report invalid MAC selections and batch the revoke lookup Tell the user when none of the selected MAC addresses was valid, look up the whole revoke selection in one query, and raise the group-authorization warning through raise_message() so it is shown. Fixes #290 Fixes #293 Signed-off-by: Thomas Vincent --- locales/po/cacti.pot | 4 ++++ mactrack_view_macs.php | 48 ++++++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/locales/po/cacti.pot b/locales/po/cacti.pot index a6a99e74..497f54aa 100644 --- a/locales/po/cacti.pot +++ b/locales/po/cacti.pot @@ -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" diff --git a/mactrack_view_macs.php b/mactrack_view_macs.php index 6b919cb5..5bd50bea 100644 --- a/mactrack_view_macs.php +++ b/mactrack_view_macs.php @@ -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); } } From d2c267bb8576a6085b7587c386bfe88f338dd8ed Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Wed, 11 Nov 2026 19:31:17 -0800 Subject: [PATCH 4/7] fix(mactrack): compare site_id with '-1' in the interfaces report Fixes #292 Signed-off-by: Thomas Vincent --- mactrack_view_interfaces.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mactrack_view_interfaces.php b/mactrack_view_interfaces.php index 950ec580..26183805 100644 --- a/mactrack_view_interfaces.php +++ b/mactrack_view_interfaces.php @@ -719,7 +719,7 @@ function mactrack_filter_table(): void { Date: Wed, 11 Nov 2026 19:31:18 -0800 Subject: [PATCH 5/7] feat(mactrack): validate the DNS server settings Reject a primary or secondary DNS server that is not empty or an IP address when the settings are saved, and allow IPv6-length values. Fixes #285 Signed-off-by: Thomas Vincent --- setup.php | 31 +++++++++++++++--- tests/Unit/DnsServerSettingTest.php | 50 +++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 tests/Unit/DnsServerSettingTest.php diff --git a/setup.php b/setup.php index 89eac6c3..9876c424 100644 --- a/setup.php +++ b/setup.php @@ -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, @@ -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'), diff --git a/tests/Unit/DnsServerSettingTest.php b/tests/Unit/DnsServerSettingTest.php new file mode 100644 index 00000000..a6c0aa32 --- /dev/null +++ b/tests/Unit/DnsServerSettingTest.php @@ -0,0 +1,50 @@ +toBe($value); +})->with(['', '192.0.2.53', '2001:db8::53']); + +test('the DNS server filter rejects anything that is not an IP address', function ($value) { + expect(mactrack_validate_dns_address($value))->toBeFalse(); +})->with(['dns.example.com', '192.0.2.300', ' 192.0.2.53', '192.0.2.53; rm']); + +test('both DNS server settings use the filter', function () { + global $settings, $settings_user, $tabs, $tabs_graphs; + + $settings = ['path' => [], 'mail' => []]; + $settings_user = []; + $tabs = []; + $tabs_graphs = []; + + mactrack_config_settings(); + + foreach (['mt_dns_primary', 'mt_dns_secondary'] as $name) { + $field = $settings['mactrack'][$name]; + + expect($field['filter'])->toBe(FILTER_CALLBACK) + ->and($field['options'])->toBe(['options' => 'mactrack_validate_dns_address']) + ->and(filter_var('not-an-ip', $field['filter'], $field['options']))->toBeFalse() + ->and(filter_var('', $field['filter'], $field['options']))->toBe(''); + } +}); From 087caf037772c36f15b05bbe60f13b8771be0986 Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Wed, 11 Nov 2026 19:34:22 -0800 Subject: [PATCH 6/7] docs(mactrack): add CHANGELOG entries for this batch Covers this branch and the xform_net_address() and device export branches, so only one pull request in the batch edits the CHANGELOG. Signed-off-by: Thomas Vincent --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bd91ccd..40a96583 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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// to css// alongside the base css/mactrack.css From 5e913e858218ece8baa3b2627a644bdf91653b0b Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Wed, 11 Nov 2026 19:45:01 -0800 Subject: [PATCH 7/7] test(mactrack): load single script functions without eval() Copy the function into a temporary file and require it, so the tests no longer run code through eval(). Signed-off-by: Thomas Vincent --- tests/Support/load_function.php | 51 +++++++++++++++++++ .../Unit/DeviceRemoveAndStaleProcessTest.php | 8 +-- 2 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 tests/Support/load_function.php diff --git a/tests/Support/load_function.php b/tests/Support/load_function.php new file mode 100644 index 00000000..027eb616 --- /dev/null +++ b/tests/Support/load_function.php @@ -0,0 +1,51 @@ +