Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 60 additions & 4 deletions lib/ConfigRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,67 @@
}

/**
* Devices Cacti lists, which is what get_allowed_devices() returns for a
* user with unrestricted device access.
* Sorted ids of the devices a device permission condition allows, or null
* when the query fails or a row has no numeric id. $sqlWhere is the WHERE
* clause smokeping_allowed_device_ids() builds with Cacti's
* get_policy_where(); the joins are those of get_allowed_devices() in
* Cacti 1.2.31 and 1.2.x lib/auth.php, which the condition refers to.
* The clause is SQL and is never built from request input.
*
* @return list<int>|null
*/
public function allowedDeviceIds(string $sqlWhere): ?array {

Check warning on line 66 in lib/ConfigRepository.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move this open curly brace to the beginning of the next line.

See more on https://sonarcloud.io/project/issues?id=somethingwithproof_plugin_smokeping&issues=AaH3tLOE2otzqtpBDiYs&open=AaH3tLOE2otzqtpBDiYs&pullRequest=115
$rows = db_fetch_assoc_prepared("SELECT DISTINCT h.id
FROM host AS h
LEFT JOIN graph_local AS gl
ON h.id = gl.host_id
LEFT JOIN graph_templates AS gt
ON gt.id = gl.graph_template_id
LEFT JOIN host_template AS ht
ON h.host_template_id = ht.id
$sqlWhere", [], true, $this->connection);

Check warning on line 75 in lib/ConfigRepository.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Either split this list into multiple lines, aligned at column "6" or put all arguments on line "67".

See more on https://sonarcloud.io/project/issues?id=somethingwithproof_plugin_smokeping&issues=AaH3tLOE2otzqtpBDiYt&open=AaH3tLOE2otzqtpBDiYt&pullRequest=115

Check warning on line 75 in lib/ConfigRepository.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move the closing parenthesis on the next line.

See more on https://sonarcloud.io/project/issues?id=somethingwithproof_plugin_smokeping&issues=AaH3tLOE2otzqtpBDiYu&open=AaH3tLOE2otzqtpBDiYu&pullRequest=115

return self::sortedIds($rows);
}

/**
* Ids of every device Cacti lists, disabled ones included. These are the
* devices smokeping_allowed_device_ids() returns for a user with
* unrestricted device access. Null when the query fails or a row has no
* numeric id, so a failure never reads as "no devices".
*
* @return list<int>|null
*/
public function deviceCount(): int {
return (int) db_fetch_cell_prepared("SELECT COUNT(*) FROM host WHERE id > 0 AND deleted = ''", [], '', true, $this->connection);
public function deviceIds(): ?array {

Check warning on line 88 in lib/ConfigRepository.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move this open curly brace to the beginning of the next line.

See more on https://sonarcloud.io/project/issues?id=somethingwithproof_plugin_smokeping&issues=AaH3wX5fW4xpQtUEDQky&open=AaH3wX5fW4xpQtUEDQky&pullRequest=115
$rows = db_fetch_assoc_prepared("SELECT id FROM host WHERE id > 0 AND deleted = ''", [], true, $this->connection);

return self::sortedIds($rows);
}

/**
* The sorted ids of a device query's rows, or null when the query failed
* (Cacti returns false) or a row has no numeric id.
*
* @return list<int>|null
*/
private static function sortedIds(mixed $rows): ?array {

Check warning on line 100 in lib/ConfigRepository.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move this open curly brace to the beginning of the next line.

See more on https://sonarcloud.io/project/issues?id=somethingwithproof_plugin_smokeping&issues=AaH3wX5fW4xpQtUEDQkz&open=AaH3wX5fW4xpQtUEDQkz&pullRequest=115
if (!is_array($rows)) {
return null;
}

$ids = [];

foreach ($rows as $row) {
if (!is_array($row) || !is_numeric($row['id'] ?? null)) {
return null;
}

$ids[] = (int) $row['id'];
}

sort($ids);

return $ids;
}

/**
Expand Down
96 changes: 91 additions & 5 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,100 @@
}

/**
* Whether the user's device permissions cover every device: only when
* get_allowed_devices() returns every device Cacti lists. A user limited to
* some devices, however many, is not unrestricted.
* Whether the user's device permissions cover every device: only when the
* devices they allow, disabled ones included, are exactly the devices Cacti
* lists. A user limited to some devices, however many, is not unrestricted,
* and a failure to read either set counts as limited.
*
* With no devices at all, both sets are empty and every user with the realm
* passes: there is no device name to export or to hide, and refusing would
* block the import that first populates a new Cacti.
*/
function smokeping_user_sees_every_device(\Somethingwithproof\Smokeping\ConfigRepository $repository): bool {
$allowed = get_allowed_devices();
$allowed = smokeping_allowed_device_ids($repository);
$every = $repository->deviceIds();

return $allowed !== null && $every !== null && $allowed === $every;
}

/**
* Sorted ids of the devices the logged-in user's permissions allow, disabled
* devices included, or null when they cannot be worked out.
*
* This is Cacti's get_allowed_devices() without its Hide Disabled filter.
* get_allowed_devices() drops disabled devices while the user's Hide Disabled
* setting is on, the default, and reads that setting for the session user
* from settings_user (read_user_setting() with $user > 0 never consults the
* session), so it cannot be turned off for one call. The query is rebuilt
* from Cacti's own permission helpers instead. It mirrors get_allowed_devices()
* in lib/auth.php (Cacti 1.2.31 lines 2768-2870; the 1.2.x branch, as of
* this change, lines 2810-2912): the user from the session when
* auth_method > 0, get_simple_device_perms(), get_policy_where() over
* get_policies() unless the permissions are simple or authentication is off,
* and the same host, graph_local, graph_templates and host_template joins
* (ConfigRepository::allowedDeviceIds()). The helpers are called, not copied;
* review this function and that query when get_allowed_devices() changes.
*
* It refuses where Cacti would not: a missing helper, a session user that
* does not exist, a graph permission method other than 1-4 (for which
* get_policy_where() adds no condition), a policy row of the wrong shape, a
* failed query, or a non-numeric id gives null.
*
* @return list<int>|null
*/
function smokeping_allowed_device_ids(\Somethingwithproof\Smokeping\ConfigRepository $repository): ?array {

Check warning on line 444 in setup.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move this open curly brace to the beginning of the next line.

See more on https://sonarcloud.io/project/issues?id=somethingwithproof_plugin_smokeping&issues=AaH3tLOV2otzqtpBDiYx&open=AaH3tLOV2otzqtpBDiYx&pullRequest=115

Check warning on line 444 in setup.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename function "smokeping_allowed_device_ids" to match the regular expression ^[a-z][a-zA-Z0-9]*$.

See more on https://sonarcloud.io/project/issues?id=somethingwithproof_plugin_smokeping&issues=AaH3tLOV2otzqtpBDiYy&open=AaH3tLOV2otzqtpBDiYy&pullRequest=115
foreach (['read_config_option', 'auth_valid_user', 'get_simple_device_perms', 'get_policies', 'get_policy_where'] as $helper) {

Check warning on line 445 in setup.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this 128 characters long line (which is greater than 120 authorized).

See more on https://sonarcloud.io/project/issues?id=somethingwithproof_plugin_smokeping&issues=AaH3tLOV2otzqtpBDiYv&open=AaH3tLOV2otzqtpBDiYv&pullRequest=115
if (!function_exists($helper)) {
return null;
}
}

$authMethod = read_config_option('auth_method');

if (!is_numeric($authMethod)) {
return null;
}

$sqlWhere = "WHERE ((h.id > 0 AND h.deleted = '') OR h.id IS NULL)";

if ((int) $authMethod > 0) {
$userId = $_SESSION['sess_user_id'] ?? null;

if (!is_numeric($userId) || (int) $userId <= 0 || !auth_valid_user((int) $userId)) {
return null;
}

$userId = (int) $userId;

if (!get_simple_device_perms($userId)) {
$graphAuthMethod = read_config_option('graph_auth_method');
$policies = get_policies($userId);

if (!is_numeric($graphAuthMethod) || !in_array((int) $graphAuthMethod, [1, 2, 3, 4], true) || !is_array($policies) || $policies === []) {

Check warning on line 472 in setup.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this 140 characters long line (which is greater than 120 authorized).

See more on https://sonarcloud.io/project/issues?id=somethingwithproof_plugin_smokeping&issues=AaH3tLOV2otzqtpBDiYw&open=AaH3tLOV2otzqtpBDiYw&pullRequest=115
return null;
}

foreach ($policies as $policy) {
if (!is_array($policy) || !is_numeric($policy['id'] ?? null) || !isset($policy['type'])) {
return null;
}

foreach (['policy_graphs', 'policy_hosts', 'policy_graph_templates'] as $column) {
if (!array_key_exists($column, $policy)) {
return null;
}
}
}

$sqlWhere = get_policy_where((int) $graphAuthMethod, $policies, $sqlWhere);

if (!is_string($sqlWhere)) {
return null;
}
}
}

return is_array($allowed) && count($allowed) === $repository->deviceCount();
return $repository->allowedDeviceIds($sqlWhere);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/Support/OperatorStubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ function api_plugin_user_realm_auth($filename = '') {
}

function get_allowed_devices($sql_where = '', $sql_order = 'description', $sql_limit = '', &$total_rows = 0, $user_id = 0, $device_id = 0) {
return $GLOBALS['__operator_devices'] ?? [];
$devices = $GLOBALS['__operator_devices'] ?? [];

// A closure stands in for Cacti's own filtering, such as Hide Disabled.
return $devices instanceof Closure ? $devices() : $devices;
}

function form_start($action, $id = '', $multipart = false) {
Expand Down
Loading