mirror of
https://github.com/Cacti/cacti.git
synced 2026-11-30 04:09:20 +09:00
* fix(auth): bind Basic sessions to verified upstream identities Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test(auth): use behavioral proxy identity coverage Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(auth): keep Basic guest sessions bound to their upstream identity An upstream-verified user with no realm 2 row is logged in as the guest account by auth_login.php. The identity check then found no user row on the next request, destroyed the session and re-ran auth_login.php, so every request ended in another login redirect. Record the Basic username in the session when auth_login.php establishes it, and treat a guest session as valid while that recorded name matches the current upstream identity and the guest account is still enabled. A different identity, or a realm 2 row created later, still replaces the session. Extend the Basic e2e test with a guest-account case on a guest page. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test(auth): match session-user assignments, not comparisons, in the sweep The sweep that requires cacti_auth_transition() near every write of $_SESSION[SESS_USER_ID] matched '=' at the start of '===', so the guest session check in include/auth.php was reported as an uncovered write. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(auth): look up Basic sessions by the sanitized login name auth_login.php creates and resolves the realm 2 account from sanitize_search_string() of the upstream name, but the per-request check looked the row up with the raw name. A name that sanitization changes, such as o'brien, never matched its own row, so every request destroyed the session and ran auth_login.php again. Look the row up by the sanitized name and bind every Basic session, not only guest sessions, to the raw upstream name recorded at login, so a different upstream identity that maps to the same account still starts a new session. Also map $auth_basic_allow_php_auth_user into $config like $trusted_proxies, name the setting in the changelog fragment, and drop a doubled blank line in config.php.dist. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test(phpstan): match the $config shape that includes the Basic opt-in Mapping $auth_basic_allow_php_auth_user into $config changed the array shape PHPStan prints for include/global.php, so two existing baseline entries stopped matching and repo-wide analysis reported them as new. Update the two messages to the new shape; no entry is added. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(auth): log the PHP_AUTH_USER opt-in when Basic login finds no identity An upgraded site whose web server only sets PHP_AUTH_USER now gets the "no username was passed" page, because that value is no longer trusted by default. Log a hint naming $auth_basic_allow_php_auth_user so the administrator can find the setting. The page text is unchanged and the client-supplied value is never logged. The Basic e2e test asserts the hint for a decoded client credential. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> --------- Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
462 lines
17 KiB
PHP
462 lines
17 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/ |
|
|
+-------------------------------------------------------------------------+
|
|
*/
|
|
|
|
global $current_user;
|
|
|
|
require_once('global.php');
|
|
|
|
if (!isset($config['cacti_db_version'])) {
|
|
$version = get_cacti_db_version();
|
|
|
|
$config['cacti_db_version'] = $version;
|
|
|
|
if (!defined('CACTI_DB_VERSION')) {
|
|
define('CACTI_DB_VERSION', $version);
|
|
}
|
|
} else {
|
|
$version = $config['cacti_db_version'];
|
|
}
|
|
|
|
$auth_method = read_config_option('auth_method');
|
|
$cookie_user = false;
|
|
|
|
/**
|
|
* Check to see if Cacti authentication is disabled
|
|
* and force Local Authentication on if found
|
|
*/
|
|
check_reset_no_authentication($auth_method);
|
|
|
|
/**
|
|
* Check to see if the Database Cacti version is different
|
|
* from the installed Cacti version and start the install
|
|
* process if found to be different.
|
|
*/
|
|
if (is_install_needed() && !defined('IN_CACTI_INSTALL')) {
|
|
header('Location: ' . CACTI_PATH_URL . 'install/');
|
|
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* The logout page does not require authentication
|
|
* so, short cut the process. login_sso.php is the public SAML2/OpenID
|
|
* redirect+callback endpoint: every request to it is, by definition, still
|
|
* unauthenticated, so it must never hit the login-page redirect below.
|
|
*/
|
|
if (get_current_page() == 'logout.php' || get_current_page() == 'auth_changepassword.php' || get_current_page() == 'auth_resetpassword.php' || get_current_page() == 'login_sso.php') {
|
|
return true;
|
|
}
|
|
|
|
// handle alternate authentication realms
|
|
api_plugin_hook_function('auth_alternate_realms');
|
|
|
|
/**
|
|
* handle change password dialog and auth cookie if not using basic auth
|
|
*/
|
|
if ($auth_method != AUTH_METHOD_BASIC) {
|
|
if (isset($_SESSION[SESS_CHANGE_PASSWORD])) {
|
|
header('Location: ' . CACTI_PATH_URL . 'auth_changepassword.php?ref=' . rawurlencode(validate_redirect_url($_SERVER['HTTP_REFERER'] ?? '', 'index.php')));
|
|
exit;
|
|
}
|
|
|
|
// check for remember me functionality
|
|
if (!isset($_SESSION[SESS_USER_ID])) {
|
|
$cookie_user = check_auth_cookie();
|
|
|
|
if ($cookie_user !== false) {
|
|
if (cacti_auth_transition((int) $cookie_user, 'cookie_restore')) {
|
|
$_SESSION[SESS_USER_ID] = $cookie_user;
|
|
$_SESSION[SESS_USER_AGENT] = $_SERVER['HTTP_USER_AGENT'];
|
|
$_SESSION[SESS_CLIENT_ADDR] = get_client_addr();
|
|
} else {
|
|
// The account may have been locked after cookie validation.
|
|
clear_auth_cookie();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check for basic auth, and if the user has been logged in via the server
|
|
* but their user_id is not set, include the auth_login.php script to
|
|
* process their log in.
|
|
*/
|
|
if ($auth_method == AUTH_METHOD_BASIC) {
|
|
$username = get_basic_auth_username();
|
|
|
|
// auth_login.php resolves and creates the realm 2 account from the
|
|
// sanitize_search_string() form of the upstream name (auth_get_username()),
|
|
// so look the row up by that same form. A raw lookup misses names such as
|
|
// o'brien and would rebuild the session on every request.
|
|
$login_username = $username !== false ? sanitize_search_string($username) : false;
|
|
$current_user = $login_username !== false ? db_fetch_row_prepared('SELECT *
|
|
FROM user_auth
|
|
WHERE realm = 2
|
|
AND username = ?',
|
|
[$login_username]) : [];
|
|
$current_user = is_array($current_user) ? $current_user : [];
|
|
$basic_error = '';
|
|
|
|
if ($username === false) {
|
|
$basic_error = __('Web Basic Authentication configured, but no username was passed from the web server. Please make sure you have authentication enabled on the web server.');
|
|
|
|
// Upgraded sites whose web server only sets PHP_AUTH_USER land here, because
|
|
// that value is no longer trusted by default. Point the administrator at the
|
|
// opt-in in the log rather than on the page, and never log the client value.
|
|
if (!empty($_SERVER['PHP_AUTH_USER']) && ($config['auth_basic_allow_php_auth_user'] ?? false) !== true) {
|
|
cacti_log('AUTH: Web Basic Authentication ignored PHP_AUTH_USER. If the web server verifies the password and sets only PHP_AUTH_USER, set $auth_basic_allow_php_auth_user = true in include/config.php', false, 'AUTH');
|
|
}
|
|
} elseif (cacti_sizeof($current_user)) {
|
|
if ($current_user['enabled'] != 'on') {
|
|
$basic_error = __('Access Denied! User account disabled.');
|
|
} elseif (($current_user['locked'] ?? '') === 'on') {
|
|
$basic_error = __('Access Denied! User account locked.');
|
|
} elseif (!auth_user_has_access($current_user)) {
|
|
$basic_error = __('You do not have access to any area of Cacti. Contact your administrator.');
|
|
}
|
|
}
|
|
|
|
// Sanitization maps several upstream names to one account, so every session
|
|
// is also bound to the raw upstream name recorded when it was created. A
|
|
// different upstream identity on the same browser session starts over.
|
|
$same_identity = isset($_SESSION[SESS_BASIC_AUTH_USER]) && $_SESSION[SESS_BASIC_AUTH_USER] === $username;
|
|
|
|
// An upstream identity with no realm 2 row is logged in as the guest account
|
|
// by auth_login.php, so the session row id cannot be compared with a user row.
|
|
// Bind that session to the upstream username recorded at login instead;
|
|
// otherwise every request would destroy the guest session and loop through
|
|
// auth_login.php's redirect.
|
|
$guest_session = $basic_error === '' && !cacti_sizeof($current_user)
|
|
&& isset($_SESSION[SESS_USER_ID])
|
|
&& $same_identity
|
|
&& get_guest_account() > 0
|
|
&& (int) $_SESSION[SESS_USER_ID] === get_guest_account();
|
|
|
|
$user_session = cacti_sizeof($current_user)
|
|
&& isset($_SESSION[SESS_USER_ID])
|
|
&& $same_identity
|
|
&& (int) $_SESSION[SESS_USER_ID] === (int) $current_user['id'];
|
|
|
|
// A Cacti session cannot outlive or replace the verified upstream identity.
|
|
if (isset($_SESSION[SESS_USER_ID]) && ($basic_error !== '' || (!$guest_session && !$user_session))) {
|
|
cacti_session_destroy();
|
|
cacti_session_start();
|
|
}
|
|
|
|
if ($basic_error !== '') {
|
|
auth_display_custom_error_message($basic_error);
|
|
|
|
exit;
|
|
}
|
|
|
|
if (cacti_sizeof($current_user)) {
|
|
if (!isset($_SESSION[SESS_USER_ID])) {
|
|
if (!cacti_auth_transition((int) $current_user['id'], 'basic_auth')) {
|
|
auth_display_custom_error_message(__('Access Denied! User account locked.'));
|
|
|
|
exit;
|
|
}
|
|
|
|
$_SESSION[SESS_USER_ID] = $current_user['id'];
|
|
$_SESSION[SESS_USER_AGENT] = $_SERVER['HTTP_USER_AGENT'];
|
|
$_SESSION[SESS_CLIENT_ADDR] = get_client_addr();
|
|
|
|
// Bind the session to the raw upstream name, as auth_login.php does.
|
|
$_SESSION[SESS_BASIC_AUTH_USER] = $username;
|
|
|
|
$client_addr = get_client_addr();
|
|
|
|
cacti_log("LOGIN: User '" . $current_user['username'] . "' authenticated via Basic Authentication from IP Address '" . $client_addr . "'", false, 'AUTH');
|
|
|
|
db_execute_prepared('INSERT IGNORE INTO user_log
|
|
(username, user_id, result, ip, time)
|
|
VALUES (?, ?, 1, ?, NOW())',
|
|
[$current_user['username'], $current_user['id'], $_SESSION[SESS_CLIENT_ADDR]]);
|
|
}
|
|
} elseif (!$guest_session) {
|
|
require_once(CACTI_PATH_BASE . '/auth_login.php');
|
|
}
|
|
|
|
// GHSA-4rmr-wvjq-qxc2: retain the per-page realm check below.
|
|
}
|
|
|
|
/**
|
|
* Check to see if Cacti is locked out and if it is, block
|
|
* users from accessing the site until lockout is cleared.
|
|
*/
|
|
if (CACTI_WEB && isset($_SESSION[SESS_USER_ID])) {
|
|
$lockout = read_config_option('cacti_lockout_status', true);
|
|
|
|
if ($lockout != '') {
|
|
$lockout = json_decode($lockout, true);
|
|
$admin_user = read_config_option('admin_user');
|
|
$enabled = read_config_option('auth_maint_lockout_type', true);
|
|
|
|
if ($lockout['session'] != session_id()) {
|
|
if (($enabled == 0 && $admin_user != $_SESSION[SESS_USER_ID]) || $enabled == 1) {
|
|
general_header();
|
|
print '<h1>' . __('Cacti is Locked Out for Maintenance') . '</h1>';
|
|
print '<p>' . __('The primary Cacti administrator has locked out the Cacti user interface for maintenance for activities such as upgrades. If you feel this is in error, please contact your Cacti adminnistrator. This lockout will stay in place for approximately 30 minutes.') . '</p>';
|
|
bottom_footer();
|
|
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* If the special boolean $guest_account is set for a page, then the guest
|
|
* account can be used. Where this may not be the case is with basic auth
|
|
* where to enter the Cacti website, you must first have a valid account.
|
|
* if that is the case, then use that valid accounts permissions and not
|
|
* the guest account.
|
|
*/
|
|
if (isset($guest_account)) {
|
|
$guest_user_id = get_guest_account();
|
|
|
|
// find guest user
|
|
if (!empty($guest_user_id)) {
|
|
if (empty($_SESSION[SESS_USER_ID])) {
|
|
$_SESSION[SESS_USER_ID] = $guest_user_id;
|
|
$_SESSION[SESS_USER_AGENT] = $_SERVER['HTTP_USER_AGENT'];
|
|
$_SESSION[SESS_CLIENT_ADDR] = get_client_addr();
|
|
}
|
|
|
|
$current_user = db_fetch_row_prepared('SELECT *
|
|
FROM user_auth
|
|
WHERE id = ?',
|
|
[$_SESSION[SESS_USER_ID]]);
|
|
|
|
// GHSA-4rmr-wvjq-qxc2: fall through to the per-page realm check below
|
|
// instead of returning, so the guest account's access to this page is
|
|
// authorized rather than assumed.
|
|
}
|
|
}
|
|
|
|
/**
|
|
* If we are a guest user in a non-guest area, wipe credentials
|
|
* user will be redirected back to the login page.
|
|
*/
|
|
if (!isset($guest_account) && isset($_SESSION[SESS_USER_ID])) {
|
|
if (get_guest_account() === $_SESSION[SESS_USER_ID]) {
|
|
kill_session_var(SESS_USER_ID);
|
|
cacti_session_destroy();
|
|
cacti_session_start();
|
|
}
|
|
}
|
|
|
|
if (empty($_SESSION[SESS_USER_ID])) {
|
|
if (isset($auth_json) && $auth_json == true) {
|
|
print json_encode(
|
|
[
|
|
'status' => '500',
|
|
'statusText' => __('Not Logged In'),
|
|
'responseText' => __('You must be logged in to access this area of Cacti.')
|
|
]
|
|
);
|
|
} elseif (isset($auth_text) && $auth_text == true) {
|
|
// handle graph_image.php to respond with text.
|
|
print __('FATAL: You must be logged in to access this area of Cacti.');
|
|
} else {
|
|
require_once(CACTI_PATH_BASE . '/auth_login.php');
|
|
}
|
|
|
|
exit;
|
|
} else {
|
|
if (empty($_SESSION[SESS_USER_2FA]) && db_column_exists('user_auth', 'tfa_enabled')) {
|
|
if (read_config_option('secpass_2fa_enabled') == 'on') {
|
|
if (is_2fa_enabled($_SESSION[SESS_USER_ID])) {
|
|
header('Location: ' . CACTI_PATH_URL . 'auth_2fa.php');
|
|
exit;
|
|
} else {
|
|
$_SESSION[SESS_USER_2FA] = time();
|
|
}
|
|
}
|
|
}
|
|
|
|
$realm_id = 0;
|
|
|
|
if (isset($user_auth_realm_filenames[get_current_page()])) {
|
|
$realm_id = $user_auth_realm_filenames[get_current_page()];
|
|
}
|
|
|
|
// Are we upgrading from a version before 1.2 which has the Install/Upgrade realm 26
|
|
if ($realm_id == 26) {
|
|
// See if we can find any users that are allowed to upgrade
|
|
$install_sql_query = '
|
|
SELECT COUNT(*)
|
|
FROM (
|
|
SELECT realm_id
|
|
FROM user_auth_realm AS uar
|
|
WHERE uar.realm_id = ?';
|
|
|
|
$install_sql_params = [$realm_id];
|
|
|
|
// See if the group realms exist and if so, check if permission exists there too
|
|
if (db_table_exists('user_auth_group_realm') &&
|
|
db_table_exists('user_auth_group') &&
|
|
db_table_exists('user_auth_group_members')) {
|
|
$install_sql_query .= '
|
|
UNION
|
|
SELECT realm_id
|
|
FROM user_auth_group_realm AS uagr
|
|
INNER JOIN user_auth_group_members AS uagm
|
|
ON uagr.group_id=uagm.group_id
|
|
INNER JOIN user_auth_group AS uag
|
|
ON uag.id=uagr.group_id
|
|
WHERE uag.enabled="on"
|
|
AND uagr.realm_id = ?';
|
|
|
|
$install_sql_params = array_merge($install_sql_params, [$realm_id]);
|
|
}
|
|
|
|
$install_sql_query .= '
|
|
) AS authorized';
|
|
|
|
$has_install_user = db_fetch_cell_prepared($install_sql_query, $install_sql_params);
|
|
|
|
if (!$has_install_user) {
|
|
/* We did not find any existing users who can upgrade/install so add any admin *
|
|
* who has access to the system settings (realm 15) by default */
|
|
db_execute('INSERT INTO `user_auth_realm` (realm_id, user_id)
|
|
SELECT 26 as realm_id, ua.id
|
|
FROM user_auth ua
|
|
INNER JOIN user_auth_realm uar
|
|
ON uar.user_id=ua.id
|
|
LEFT JOIN user_auth_realm uar2
|
|
ON uar2.user_id=ua.id
|
|
AND uar2.realm_id=26
|
|
WHERE uar.realm_id=15
|
|
AND uar2.user_id IS NULL');
|
|
}
|
|
}
|
|
|
|
if ($realm_id > 0) {
|
|
$auth_sql_query = '
|
|
SELECT COUNT(*)
|
|
FROM (
|
|
SELECT realm_id
|
|
FROM user_auth_realm AS uar
|
|
WHERE uar.user_id = ?
|
|
AND uar.realm_id = ?';
|
|
|
|
$auth_sql_params = [$_SESSION[SESS_USER_ID], $realm_id];
|
|
|
|
/* Because we now expect installation to be done by authorized users, check the group_realm *
|
|
* exists before using it as this may not be present if upgrading from pre-1.x */
|
|
if (db_table_exists('user_auth_group_realm') &&
|
|
db_table_exists('user_auth_group') &&
|
|
db_table_exists('user_auth_group_members')) {
|
|
$auth_sql_query .= '
|
|
UNION
|
|
SELECT realm_id
|
|
FROM user_auth_group_realm AS uagr
|
|
INNER JOIN user_auth_group_members AS uagm
|
|
ON uagr.group_id = uagm.group_id
|
|
INNER JOIN user_auth_group AS uag
|
|
ON uag.id = uagr.group_id
|
|
WHERE uag.enabled = "on"
|
|
AND uagm.user_id = ?
|
|
AND uagr.realm_id = ?';
|
|
|
|
$auth_sql_params = array_merge($auth_sql_params, [$_SESSION[SESS_USER_ID], $realm_id]);
|
|
}
|
|
|
|
$auth_sql_query .= '
|
|
) AS authorized';
|
|
|
|
$authorized = db_fetch_cell_prepared($auth_sql_query, $auth_sql_params);
|
|
} else {
|
|
$authorized = false;
|
|
}
|
|
|
|
if ($realm_id != -1 && !$authorized) {
|
|
if (api_plugin_hook_function('custom_denied', OPER_MODE_NATIVE) == OPER_MODE_RESKIN) {
|
|
exit;
|
|
}
|
|
|
|
if (isset($_SERVER['HTTP_REFERER'])) {
|
|
$goBack = "<td colspan='2' class='center'>[<a href='" . validate_redirect_url($_SERVER['HTTP_REFERER'], $_SERVER['SCRIPT_NAME']) . "'>" . __('Return') . "</a> | <a href='" . CACTI_PATH_URL . "logout.php'>" . __('Login Again') . '</a>]</td>';
|
|
} elseif ($auth_method != AUTH_METHOD_BASIC) {
|
|
$goBack = "<td colspan='2' class='center'>[<a href='" . CACTI_PATH_URL . "logout.php'>" . __('Login Again') . '</a>]</td>';
|
|
} else {
|
|
$goBack = '';
|
|
}
|
|
|
|
raise_ajax_permission_denied();
|
|
|
|
$title_header = __('Permission Denied');
|
|
$title_body = '<p>' . __('You are not permitted to access this section of Cacti.') . '</p><p>' . __('If you feel that this is an error. Please contact your Cacti Administrator.');
|
|
|
|
if ($realm_id == 26) {
|
|
$title_header = __('Installation In Progress');
|
|
$title_body = '<p>' . __('There is an Installation or Upgrade in progress.') . '</p><p>' . __('Only Cacti Administrators with Install/Upgrade privilege may login at this time') . '</p>';
|
|
}
|
|
|
|
print "<!DOCTYPE html>\n";
|
|
print "<html>\n";
|
|
print "<head>\n";
|
|
html_common_header($title_header);
|
|
print "</head>\n";
|
|
print "<body class='logoutBody'>
|
|
<div class='logoutLeft'></div>
|
|
<div class='logoutCenter'>
|
|
<div class='logoutArea'>
|
|
<div class='cactiLogoutLogo'></div>
|
|
<legend>" . $title_header . "</legend>
|
|
<div class='logoutTitle'>
|
|
" . $title_body . '
|
|
</p>
|
|
<center>' . $goBack . "</center>
|
|
</div>
|
|
<div class='logoutErrors'></div>
|
|
</div>
|
|
<div class='versionInfo'>" . __('Version') . ' ' . $version . ' | ' . COPYRIGHT_YEARS_SHORT . "</div>
|
|
</div>
|
|
<div class='logoutRight'></div>
|
|
<script type='text/javascript' " . CactiSecureHeaders::getNonceAttribute() . ">
|
|
$(function() {
|
|
$('.loginLeft').css('width',parseInt($(window).width()*0.33)+'px');
|
|
$('.loginRight').css('width',parseInt($(window).width()*0.33)+'px');
|
|
});
|
|
</script>\n";
|
|
include_once('global_session.php');
|
|
print "</body>
|
|
</html>\n";
|
|
|
|
exit;
|
|
}
|
|
|
|
$current_user = db_fetch_row_prepared('SELECT *
|
|
FROM user_auth
|
|
WHERE id = ?',
|
|
[$_SESSION[SESS_USER_ID]]);
|
|
|
|
// Handle saved filter-layout AJAX (save/rename/delete/publish) posted to
|
|
// any filter page, now that the user is authenticated and authorized for
|
|
// the current page. Returns immediately for all non-layout requests.
|
|
filter_layouts_handle_request();
|
|
}
|