Files
librenms-librenms/html/includes/authentication/http-auth.inc.php
Tony Murray 683a10e723 fix: Improve authentication load time and security (#6615)
* fix: minimize session open time
page/graphs speedup part 2

Write close the session as soon as we no longer need to write to it. Prevents the session from blocking other requests.
Do not run through full authentication functions if the session is already authenticated.
Removes password from the session as well as some items to prevent session fixation from #4608.

WARNING: This will cause issues for ad/ldap users who do not have a bind user configured!

* Do no erase username when using cookie auth.
Properly close the session in ajax_setresolution.php

* write close the session as soon as possible in ajax_setresolution.php

* Remove session regeneration. It is not compatible with the current code and would require more changes.

* Totally refactor authentication.  Extract code to functions for re-use and improved readability

* Use exceptions for authentication and error logging
Tested: mysql, ad_auth with and without bind user

* fix a couple scrutinizer issues

* fix reauthenticate in radius
2017-05-15 22:18:23 -05:00

123 lines
3.1 KiB
PHP

<?php
use LibreNMS\Exceptions\AuthenticationException;
use Phpass\PasswordHash;
if (!isset($_SESSION['username'])) {
$_SESSION['username'] = '';
}
function authenticate($username, $password)
{
global $config;
if (isset($_SERVER['REMOTE_USER']) || isset($_SERVER['PHP_AUTH_USER'])) {
$_SESSION['username'] = mres($_SERVER['REMOTE_USER']) ?: mres($_SERVER['PHP_AUTH_USER']);
$row = @dbFetchRow('SELECT username FROM `users` WHERE `username`=?', array($_SESSION['username']));
if (isset($row['username']) && $row['username'] == $_SESSION['username']) {
return true;
} else {
$_SESSION['username'] = $config['http_auth_guest'];
return true;
}
}
throw new AuthenticationException();
}
function reauthenticate($sess_id, $token)
{
return false;
}
function passwordscanchange($username = '')
{
return 0;
}
function changepassword($username, $newpassword)
{
// Not supported
}
function auth_usermanagement()
{
return 1;
}
function adduser($username, $password, $level, $email = '', $realname = '', $can_modify_passwd = 1, $description = '')
{
if (!user_exists($username)) {
$hasher = new PasswordHash(8, false);
$encrypted = $hasher->HashPassword($password);
$userid = dbInsert(array('username' => $username, 'password' => $encrypted, 'level' => $level, 'email' => $email, 'realname' => $realname, 'can_modify_passwd' => $can_modify_passwd, 'descr' => $description), 'users');
if ($userid == false) {
return false;
} else {
foreach (dbFetchRows('select notifications.* from notifications where not exists( select 1 from notifications_attribs where notifications.notifications_id = notifications_attribs.notifications_id and notifications_attribs.user_id = ?) order by notifications.notifications_id desc', array($userid)) as $notif) {
dbInsert(array('notifications_id'=>$notif['notifications_id'],'user_id'=>$userid,'key'=>'read','value'=>1), 'notifications_attribs');
}
}
return $userid;
} else {
return false;
}
}
function user_exists($username)
{
// FIXME this doesn't seem right? (adama)
return dbFetchCell('SELECT * FROM `users` WHERE `username` = ?', array($username));
}
function get_userlevel($username)
{
return dbFetchCell('SELECT `level` FROM `users` WHERE `username`= ?', array($username));
}
function get_userid($username)
{
return dbFetchCell('SELECT `user_id` FROM `users` WHERE `username`= ?', array($username));
}
function deluser($username)
{
// Not supported
return 0;
}
function get_userlist()
{
return dbFetchRows('SELECT * FROM `users`');
}
function can_update_users()
{
// supported so return 1
return 1;
}
function get_user($user_id)
{
return dbFetchRow('SELECT * FROM `users` WHERE `user_id` = ?', array($user_id));
}
function update_user($user_id, $realname, $level, $can_modify_passwd, $email)
{
dbUpdate(array('realname' => $realname, 'level' => $level, 'can_modify_passwd' => $can_modify_passwd, 'email' => $email), 'users', '`user_id` = ?', array($user_id));
}