Files
cacti/remote_agent.php
T
Thomas VincentandTheWitness fcbcbdd1d7 refactor: replace strpos() boolean checks with str_contains/str_starts_with (#6645)
* refactor: replace strpos() boolean checks with str_contains/str_starts_with

Completes the PHP 8.x string function migration started in lib/.
Converts remaining legacy patterns across 81 files:

- strpos($x, $y) !== false → str_contains($x, $y)
- strpos($x, $y) === false → !str_contains($x, $y)
- strpos($x, $y) != false  → str_contains($x, $y)
- bare if (strpos($x, $y)) → if (str_contains($x, $y))
- substr($x, 0, strlen($y)) == $y → str_starts_with($x, $y)

Intentionally preserves strpos() calls that use the return value as
an integer offset:
- user_admin.php / user_group_admin.php (4 calls, ternary for position)
- auth_profile.php (2 calls, offset for substr); guards fixed from
  bare if ($pos) to if ($pos !== false)

Behavior fix: ss_esxi_vhosts.php:55 bare strpos() missed position-0
matches; str_contains() now correctly detects 'not installed' at any
position. Adjacent line already used str_contains().

Ref: https://github.com/Cacti/cacti/issues/6641

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix: correct dead-code elseif in plugin_display_compat() version comparison

The if/elseif chain in plugin_display_compat() checked >=, >=, >, <
— the second >= was unreachable dead code. Corrected to <= to complete
the operator set (>=, <=, >, <).

Without this fix, <= constraints (e.g. "<=1.2") fell through to the <
branch and were rendered as "< =1.2" instead of "<= 1.2".

This pre-existing bug was exposed when converting strpos() to
str_contains(), which made PHPStan detect the always-false condition.

Ref: https://github.com/Cacti/cacti/issues/6641

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* fix: add limit to explode('=') in CLI argument parsers

17 CLI scripts used explode('=', $parameter) without a limit of 2,
silently truncating values containing = (e.g. --note=a=b parsed as
$value='a' instead of 'a=b'). The other 40 CLI scripts already had
the correct limit.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Co-authored-by: TheWitness <thewitness@cacti.net>
2026-02-25 11:38:59 -05:00

485 lines
14 KiB
PHP

<?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. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
require(__DIR__ . '/include/global.php');
require_once(CACTI_PATH_LIBRARY . '/api_device.php');
require_once(CACTI_PATH_LIBRARY . '/api_data_source.php');
require_once(CACTI_PATH_LIBRARY . '/data_query.php');
require_once(CACTI_PATH_LIBRARY . '/api_graph.php');
require_once(CACTI_PATH_LIBRARY . '/api_tree.php');
require_once(CACTI_PATH_LIBRARY . '/data_query.php');
require_once(CACTI_PATH_LIBRARY . '/html_form_template.php');
require_once(CACTI_PATH_LIBRARY . '/ping.php');
require_once(CACTI_PATH_LIBRARY . '/poller.php');
require_once(CACTI_PATH_LIBRARY . '/rrd.php');
require_once(CACTI_PATH_LIBRARY . '/snmp.php');
require_once(CACTI_PATH_LIBRARY . '/sort.php');
require_once(CACTI_PATH_LIBRARY . '/template.php');
require_once(CACTI_PATH_LIBRARY . '/utility.php');
global $remote_db_cnn_id;
$debug = false;
$poller_db_cnn_id = false;
if (POLLER_ID > 1) {
if (CACTI_CONNECTION == 'online') {
if (gnrv('action') == 'runquery') {
db_force_remote_cnn();
}
$poller_db_cnn_id = $remote_db_cnn_id;
}
}
if (!remote_client_authorized()) {
print 'FATAL: You are not authorized to use this service';
exit;
}
set_default_action();
switch (grv('action')) {
case 'polldata':
// Only let realtime polling run for a short time
ini_set('max_execution_time', read_config_option('script_timeout'));
debug('Start: Poling Data for Realtime');
poll_for_data();
debug('End: Poling Data for Realtime');
break;
case 'runquery':
debug('Start: Running Data Query');
run_remote_data_query();
debug('End: Running Data Query');
break;
case 'ping':
debug('Start: Pinging Device');
ping_device();
debug('End: Pinging Device');
break;
case 'snmpget':
debug('Start: Performing SNMP Get Request');
get_snmp_data();
debug('End: Performing SNMP Get Request');
break;
case 'snmpwalk':
debug('Start: Performing SNMP Walk Request');
get_snmp_data_walk();
debug('End: Performing SNMP Walk Request');
break;
case 'graph_json':
debug('Start: Performing Graph Request');
get_graph_data();
debug('End: Performing Graph Request');
break;
case 'discover':
debug('Start:Performing Network Discovery Request');
run_remote_discovery();
debug('End:Performing Network Discovery Request');
break;
default:
if (!api_plugin_hook_function('remote_agent', grv('action'))) {
debug('WARNING: Unknown Agent Request');
print 'Unknown Agent Request';
}
}
exit;
function debug(string $message) : void {
global $debug;
if ($debug) {
cacti_log('REMOTE DEBUG: ' . trim($message), false, 'WEBSVCS');
}
}
function remote_agent_strip_domain(string $host) : string {
if (str_contains($host, '.')) {
$parts = explode('.', $host);
return $parts[0];
} else {
return $host;
}
}
function remote_client_authorized() : bool {
global $poller_db_cnn_id, $remote_agent_whitelist;
// don't allow to run from the command line
$client_addr = get_client_addr();
if ($client_addr === false) {
return false;
}
if (!filter_var($client_addr, FILTER_VALIDATE_IP)) {
cacti_log('ERROR: Invalid remote agent client IP Address. Exiting');
return false;
}
$client_name = gethostbyaddr($client_addr);
if ($client_name == $client_addr) {
cacti_log('NOTE: Unable to resolve hostname from address ' . $client_addr, false, 'WEBUI', POLLER_VERBOSITY_MEDIUM);
} else {
$client_name = remote_agent_strip_domain($client_name);
}
$pollers = db_fetch_assoc('SELECT * FROM poller WHERE disabled = ""', true, $poller_db_cnn_id);
if (cacti_sizeof($pollers) > 1) {
foreach ($pollers as $poller) {
if (remote_agent_strip_domain($poller['hostname']) == $client_name) {
return true;
}
if ($poller['hostname'] == $client_addr) {
return true;
}
if (in_array($client_addr,$remote_agent_whitelist, true)) {
return true;
}
}
}
cacti_log("Unauthorized remote agent access attempt from $client_name ($client_addr)");
return false;
}
function get_graph_data() : bool {
gfrv('graph_start');
gfrv('graph_end');
gfrv('graph_height');
gfrv('graph_width');
gfrv('local_graph_id');
gfrv('rra_id');
gfrv('graph_theme', FILTER_CALLBACK, ['options' => 'sanitize_search_string']);
gfrv('graph_nolegend', FILTER_CALLBACK, ['options' => 'sanitize_search_string']);
gfrv('effective_user');
$local_graph_id = gfrv('local_graph_id');
$rra_id = gfrv('rra_id');
$graph_data_array = [];
// override: graph start time (unix time)
if (!ierv('graph_start') && grv('graph_start') < FILTER_VALIDATE_MAX_DATE_AS_INT) {
$graph_data_array['graph_start'] = grv('graph_start');
}
// override: graph end time (unix time)
if (!ierv('graph_end') && grv('graph_end') < FILTER_VALIDATE_MAX_DATE_AS_INT) {
$graph_data_array['graph_end'] = grv('graph_end');
}
// override: graph height (in pixels)
if (!ierv('graph_height') && grv('graph_height') < 3000) {
$graph_data_array['graph_height'] = grv('graph_height');
}
// override: graph width (in pixels)
if (!ierv('graph_width') && grv('graph_width') < 3000) {
$graph_data_array['graph_width'] = grv('graph_width');
}
// override: skip drawing the legend?
if (!ierv('graph_nolegend')) {
$graph_data_array['graph_nolegend'] = grv('graph_nolegend');
}
// print RRDtool graph source?
if (!ierv('show_source')) {
$graph_data_array['print_source'] = grv('show_source');
}
// disable cache check
if (isrv('disable_cache')) {
$graph_data_array['disable_cache'] = true;
}
// set the theme
if (isrv('graph_theme')) {
$graph_data_array['graph_theme'] = grv('graph_theme');
}
// set the theme
if (isrv('effective_user')) {
$user = grv('effective_user');
} else {
$user = 0;
}
$graph_data_array['graphv'] = true;
$xport_options = [];
print @rrdtool_function_graph($local_graph_id, $rra_id, $graph_data_array, null, $xport_options, $user);
return true;
}
function get_snmp_data() : void {
$host_id = gfrv('host_id');
$oid = gnrv('oid');
$output = '';
if (!empty($host_id)) {
$host = db_fetch_row_prepared('SELECT * FROM host WHERE id = ?', [$host_id]);
$session = cacti_snmp_session($host['hostname'], $host['snmp_community'], $host['snmp_version'],
$host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'], $host['snmp_priv_passphrase'],
$host['snmp_priv_protocol'], $host['snmp_context'], $host['snmp_engine_id'], $host['snmp_port'],
$host['snmp_timeout'], $host['ping_retries'], $host['max_oids']);
if ($session === false) {
$output = 'U';
} else {
$output = cacti_snmp_session_get($session, $oid);
$session->close();
}
}
print $output;
}
function get_snmp_data_walk() : void {
$host_id = gfrv('host_id');
$oid = gnrv('oid');
$output = '';
if (!empty($host_id)) {
$host = db_fetch_row_prepared('SELECT * FROM host WHERE id = ?', [$host_id]);
$session = cacti_snmp_session($host['hostname'], $host['snmp_community'], $host['snmp_version'],
$host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'], $host['snmp_priv_passphrase'],
$host['snmp_priv_protocol'], $host['snmp_context'], $host['snmp_engine_id'], $host['snmp_port'],
$host['snmp_timeout'], $host['ping_retries'], $host['max_oids']);
if ($session === false) {
$output = 'U';
} else {
$output = cacti_snmp_session_walk($session, $oid);
$session->close();
}
}
if (cacti_sizeof($output)) {
print json_encode($output);
} else {
print 'U';
}
}
function ping_device() : void {
$host_id = gfrv('host_id');
api_device_ping_device($host_id, true);
}
function poll_for_data() : mixed {
$local_data_ids = gnrv('local_data_ids');
$host_id = gfrv('host_id');
$poller_id = gnrv('poller_id');
$return = [];
// ensure we have a valid poller_id
if (!preg_match('/^[a-z0-9]+$/i', $poller_id)) {
return [];
}
$i = 0;
if (cacti_sizeof($local_data_ids)) {
foreach ($local_data_ids as $local_data_id) {
input_validate_input_number($local_data_id, 'local_data_id');
$items = db_fetch_assoc_prepared('SELECT *
FROM poller_item
WHERE host_id = ?
AND local_data_id = ?',
[$host_id, $local_data_id]);
$script_server_calls = db_fetch_cell_prepared('SELECT COUNT(*)
FROM poller_item
WHERE host_id = ?
AND local_data_id = ?
AND action = 2',
[$host_id, $local_data_id]);
if (cacti_sizeof($items)) {
foreach ($items as $item) {
switch ($item['action']) {
case POLLER_ACTION_SNMP: // snmp
if (($item['snmp_version'] == 0) || (($item['snmp_community'] == '') && ($item['snmp_version'] != 3))) {
$output = 'U';
} else {
$host = db_fetch_row_prepared('SELECT ping_retries, max_oids FROM host WHERE hostname = ?', [$item['hostname']]);
$session = cacti_snmp_session($item['hostname'], $item['snmp_community'], $item['snmp_version'],
$item['snmp_username'], $item['snmp_password'], $item['snmp_auth_protocol'], $item['snmp_priv_passphrase'],
$item['snmp_priv_protocol'], $item['snmp_context'], $item['snmp_engine_id'], $item['snmp_port'],
$item['snmp_timeout'], $host['ping_retries'], $host['max_oids']);
if ($session === false) {
$output = 'U';
} else {
$output = cacti_snmp_session_get($session, $item['arg1']);
$session->close();
}
if (prepare_validate_result($output) === false) {
if (strlen($output) > 20) {
$strout = 20;
} else {
$strout = strlen($output);
}
$output = 'U';
}
}
$return[$i]['value'] = $output;
$return[$i]['rrd_name'] = $item['rrd_name'];
$return[$i]['local_data_id'] = $local_data_id;
break;
case POLLER_ACTION_SCRIPT: // script (popen)
$output = trim(exec_poll($item['arg1']));
if (prepare_validate_result($output) === false) {
if (strlen($output) > 20) {
$strout = 20;
} else {
$strout = strlen($output);
}
$output = 'U';
}
$return[$i]['value'] = $output;
$return[$i]['rrd_name'] = $item['rrd_name'];
$return[$i]['local_data_id'] = $local_data_id;
break;
case POLLER_ACTION_SCRIPT_PHP: // script (php script server)
$cactides = [
0 => ['pipe', 'r'], // stdin is a pipe that the child will read from
1 => ['pipe', 'w'], // stdout is a pipe that the child will write to
2 => ['pipe', 'w'] // stderr is a pipe to write to
];
$cactiphp = false;
$pipes = false;
if (function_exists('proc_open')) {
$cactiphp = proc_open(read_config_option('path_php_binary') . ' -q ' . CACTI_PATH_BASE . '/script_server.php realtime ' . cacti_escapeshellarg($poller_id), $cactides, $pipes);
$output = fgets($pipes[1], 1024);
$using_proc_function = true;
} else {
$using_proc_function = false;
}
if ($using_proc_function == true) {
$output = trim(str_replace("\n", '', exec_poll_php($item['arg1'], $using_proc_function, $pipes, $cactiphp)));
if (prepare_validate_result($output) === false) {
if (strlen($output) > 20) {
$strout = 20;
} else {
$strout = strlen($output);
}
$output = 'U';
}
} else {
$output = 'U';
}
$return[$i]['value'] = $output;
$return[$i]['rrd_name'] = $item['rrd_name'];
$return[$i]['local_data_id'] = $local_data_id;
if (($using_proc_function == true) && ($script_server_calls > 0)) {
// close php server process
fwrite($pipes[0], "quit\r\n");
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
$return_value = proc_close($cactiphp);
}
break;
}
$i++;
}
}
}
}
print json_encode($return);
return false;
}
function run_remote_data_query() : void {
$host_id = gfrv('host_id');
$data_query_id = gfrv('data_query_id');
if ($host_id > 0 && $data_query_id > 0) {
run_data_query($host_id, $data_query_id);
}
}
function run_remote_discovery() : void {
$poller_id = cacti_escapeshellarg((string) POLLER_ID);
$network = cacti_escapeshellarg(gfrv('network'));
$php = cacti_escapeshellcmd(read_config_option('path_php_binary'));
$path = cacti_escapeshellarg(read_config_option('path_webroot') . '/poller_automation.php');
$options = ' --poller=' . $poller_id . ' --network=' . $network . ' --force';
if (isrv('debug')) {
$options .= ' --debug';
}
exec_background($php, '-q ' . $path . $options);
sleep(2);
}