zoneminder/web/includes/auth.php

338 lines
13 KiB
PHP
Raw Normal View History

2018-04-07 02:31:11 +08:00
<?php
//
2018-04-07 02:36:23 +08:00
// ZoneMinder auth library, $Date$, $Revision$
2018-04-07 02:31:11 +08:00
// Copyright (C) 2001-2008 Philip Coombes
2018-10-09 22:07:40 +08:00
//
2018-04-07 02:31:11 +08:00
// 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.
2018-10-09 22:07:40 +08:00
//
2018-04-07 02:31:11 +08:00
// 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.
2018-10-09 22:07:40 +08:00
//
2018-04-07 02:31:11 +08:00
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2018-10-09 22:07:40 +08:00
//
//
require_once('session.php');
2019-05-05 19:50:52 +08:00
require_once(__DIR__.'/../vendor/autoload.php');
use \Firebase\JWT\JWT;
2018-04-07 02:31:11 +08:00
function password_type($password) {
if ( $password[0] == '*' ) {
return 'mysql';
} else if ( preg_match('/^\$2[ayb]\$.+$/', $password) ) {
return 'bcrypt';
} else if ( substr($password, 0,4) == '-ZM-' ) {
// zmupdate.pl adds a '-ZM-' prefix to overlay encrypted passwords
// this is done so that we don't spend cycles doing two bcrypt password_verify calls
// for every wrong password entered. This will only be invoked for passwords zmupdate.pl has
// overlay hashed
return 'mysql+bcrypt';
}
return 'plain';
}
2019-05-02 01:22:24 +08:00
// this function migrates mysql hashing to bcrypt, if you are using PHP >= 5.5
// will be called after successful login, only if mysql hashing is detected
function migrateHash($user, $pass) {
2019-05-24 02:27:17 +08:00
if ( function_exists('password_hash') ) {
ZM\Info("Migrating $user to bcrypt scheme");
2019-05-02 01:22:24 +08:00
// let it generate its own salt, and ensure bcrypt as PASSWORD_DEFAULT may change later
// we can modify this later to support argon2 etc as switch to its own password signature detection
$bcrypt_hash = password_hash($pass, PASSWORD_BCRYPT);
//ZM\Info ("hased bcrypt $pass is $bcrypt_hash");
$update_password_sql = 'UPDATE Users SET Password=\''.$bcrypt_hash.'\' WHERE Username=\''.$user.'\'';
dbQuery($update_password_sql);
# Since password field has changed, existing auth_hash is no longer valid
generateAuthHash(ZM_AUTH_HASH_IPS, true);
2019-05-24 02:27:17 +08:00
} else {
ZM\Info('Cannot migrate password scheme to bcrypt, as you are using PHP < 5.3');
2019-05-02 01:22:24 +08:00
return;
}
}
// core function used to load a User record by username and password
function validateUser($username='', $password='') {
$sql = 'SELECT * FROM Users WHERE Enabled=1 AND Username=?';
// local user, shouldn't affect the global user
$user = dbFetchOne($sql, NULL, array($username));
if ( ! $user ) {
return array(false, "Could not retrieve user $username details");
}
switch ( password_type($user['Password']) ) {
case 'mysql' :
// We assume we don't need to support mysql < 4.1
// Starting MY SQL 4.1, mysql concats a '*' in front of its password hash
// https://blog.pythian.com/hashing-algorithm-in-mysql-password-2/
ZM\Debug('Saved password is using MYSQL password function');
$input_password_hash = '*'.strtoupper(sha1(sha1($password, true)));
$password_correct = ($user['Password'] == $input_password_hash);
break;
case 'bcrypt' :
ZM\Debug('bcrypt signature found, assumed bcrypt password');
$password_correct = password_verify($password, $user['Password']);
break;
case 'mysql+bcrypt' :
// zmupdate.pl adds a '-ZM-' prefix to overlay encrypted passwords
// this is done so that we don't spend cycles doing two bcrypt password_verify calls
// for every wrong password entered. This will only be invoked for passwords zmupdate.pl has
// overlay hashed
ZM\Debug("Detected bcrypt overlay hashing for $username");
$bcrypt_hash = substr($user['Password'], 4);
$mysql_encoded_password = '*'.strtoupper(sha1(sha1($password, true)));
ZM\Debug("Comparing password $mysql_encoded_password to bcrypt hash: $bcrypt_hash");
$password_correct = password_verify($mysql_encoded_password, $bcrypt_hash);
break;
default:
// we really should nag the user not to use plain
ZM\Warning('assuming plain text password as signature is not known. Please do not use plain, it is very insecure');
$password_correct = ($user['Password'] == $password);
} // switch password_type
2019-05-02 01:22:24 +08:00
2019-05-24 02:27:17 +08:00
if ( $password_correct ) {
return array($user, 'OK');
2018-04-07 02:31:11 +08:00
}
return array(false, "Login denied for user \"$username\"");
} # end function validateUser
2018-04-07 02:31:11 +08:00
function userLogout() {
global $user;
ZM\Info('User "'.($user?$user['Username']:'no one').'" logged out');
$user = null;// unset only clears the local variable
zm_session_clear();
2018-04-07 02:31:11 +08:00
}
function validateToken($token, $allowed_token_type='access') {
global $user;
$key = ZM_AUTH_HASH_SECRET;
2019-05-09 02:07:48 +08:00
//if (ZM_AUTH_HASH_IPS) $key .= $_SERVER['REMOTE_ADDR'];
try {
$decoded_token = JWT::decode($token, $key, array('HS256'));
} catch (Exception $e) {
ZM\Error("Unable to authenticate user. error decoding JWT token:".$e->getMessage());
return array(false, $e->getMessage());
}
// convert from stdclass to array
$jwt_payload = json_decode(json_encode($decoded_token), true);
if ($allowed_token_type != 'any') {
$type = $jwt_payload['type'];
if ( $type != $allowed_token_type ) {
ZM\Error("Token type mismatch. Expected $allowed_token_type but got $type");
return array(false, 'Incorrect token type');
}
} else {
ZM\Debug('Not comparing token types as [any] was passed');
}
$username = $jwt_payload['user'];
$sql = 'SELECT * FROM Users WHERE Enabled=1 AND Username=?';
$saved_user_details = dbFetchOne($sql, NULL, array($username));
2019-05-24 02:27:17 +08:00
if ( $saved_user_details ) {
$issuedAt = $jwt_payload['iat'];
$minIssuedAt = $saved_user_details['TokenMinExpiry'];
2019-05-24 02:27:17 +08:00
if ( $issuedAt < $minIssuedAt ) {
ZM\Error("Token revoked for $username. Please generate a new token");
$user = null;// unset only clears the local variable
2019-05-24 02:27:17 +08:00
return array(false, 'Token revoked. Please re-generate');
}
$user = $saved_user_details;
2019-05-24 02:27:17 +08:00
return array($user, 'OK');
}
ZM\Error("Could not retrieve user $username details");
$user = null;// unset only clears the local variable
return array(false, 'No such user/credentials');
2019-05-24 02:27:17 +08:00
} // end function validateToken($token, $allowed_token_type='access')
function getAuthUser($auth) {
if ( ZM_OPT_USE_AUTH && (ZM_AUTH_RELAY == 'hashed') && !empty($auth) ) {
2018-04-07 02:31:11 +08:00
$remoteAddr = '';
if ( ZM_AUTH_HASH_IPS ) {
$remoteAddr = $_SERVER['REMOTE_ADDR'];
if ( !$remoteAddr ) {
ZM\Error("Can't determine remote address for authentication, using empty string");
2018-04-07 02:31:11 +08:00
$remoteAddr = '';
}
}
$values = array();
2018-04-13 06:43:57 +08:00
if ( isset($_SESSION['username']) ) {
2018-04-07 02:31:11 +08:00
# Most of the time we will be logged in already and the session will have our username, so we can significantly speed up our hash testing by only looking at our user.
# Only really important if you have a lot of users.
$sql = 'SELECT * FROM Users WHERE Enabled = 1 AND Username=?';
array_push($values, $_SESSION['username']);
2018-04-07 02:31:11 +08:00
} else {
$sql = 'SELECT * FROM Users WHERE Enabled = 1';
}
foreach ( dbFetchAll($sql, NULL, $values) as $user ) {
2018-04-07 02:31:11 +08:00
$now = time();
for ( $i = 0; $i < ZM_AUTH_HASH_TTL; $i++, $now -= 3600 ) { // Try for last TTL hours
2018-04-13 06:43:57 +08:00
$time = localtime($now);
2018-04-07 02:31:11 +08:00
$authKey = ZM_AUTH_HASH_SECRET.$user['Username'].$user['Password'].$remoteAddr.$time[2].$time[3].$time[4].$time[5];
2018-04-13 06:43:57 +08:00
$authHash = md5($authKey);
2018-04-07 02:31:11 +08:00
if ( $auth == $authHash ) {
return $user;
2019-08-29 00:20:03 +08:00
} // end if $auth == $authHash
2018-04-07 02:31:11 +08:00
} // end foreach hour
} // end foreach user
if ( isset($_SESSION['username']) ) {
# In a multi-server case, we might be logged in as another user and so the auth hash didn't work
$sql = 'SELECT * FROM Users WHERE Enabled = 1 AND Username != ?';
foreach ( dbFetchAll($sql, NULL, $values) as $user ) {
$now = time();
for ( $i = 0; $i < ZM_AUTH_HASH_TTL; $i++, $now -= 3600 ) { // Try for last TTL hours
$time = localtime($now);
$authKey = ZM_AUTH_HASH_SECRET.$user['Username'].$user['Password'].$remoteAddr.$time[2].$time[3].$time[4].$time[5];
$authHash = md5($authKey);
if ( $auth == $authHash ) {
return $user;
} // end if $auth == $authHash
} // end foreach hour
} // end foreach user
} // end if
} // end if using auth hash
ZM\Error("Unable to authenticate user from auth hash '$auth'");
return null;
2018-04-07 02:31:11 +08:00
} // end getAuthUser($auth)
function generateAuthHash($useRemoteAddr, $force=false) {
global $user;
if ( ZM_OPT_USE_AUTH and (ZM_AUTH_RELAY == 'hashed') and isset($user['Username']) and isset($user['Password']) ) {
2018-04-07 02:31:11 +08:00
$time = time();
2019-05-05 23:24:55 +08:00
# We use 1800 so that we regenerate the hash at half the TTL
2018-04-07 02:31:11 +08:00
$mintime = $time - ( ZM_AUTH_HASH_TTL * 1800 );
# Appending the remoteAddr prevents us from using an auth hash generated for a different ip
if ( $force or ( !isset($_SESSION['AuthHash'.$_SESSION['remoteAddr']]) ) or ( $_SESSION['AuthHashGeneratedAt'] < $mintime ) ) {
2018-04-07 02:31:11 +08:00
# Don't both regenerating Auth Hash if an hour hasn't gone by yet
$local_time = localtime();
$authKey = '';
if ( $useRemoteAddr ) {
$authKey = ZM_AUTH_HASH_SECRET.$user['Username'].$user['Password'].$_SESSION['remoteAddr'].$local_time[2].$local_time[3].$local_time[4].$local_time[5];
2018-04-07 02:31:11 +08:00
} else {
$authKey = ZM_AUTH_HASH_SECRET.$user['Username'].$user['Password'].$local_time[2].$local_time[3].$local_time[4].$local_time[5];
2018-04-07 02:31:11 +08:00
}
#ZM\Debug("Generated using hour:".$local_time[2] . ' mday:' . $local_time[3] . ' month:'.$local_time[4] . ' year: ' . $local_time[5] );
2018-05-01 01:02:53 +08:00
$auth = md5($authKey);
$_SESSION['AuthHash'.$_SESSION['remoteAddr']] = $auth;
$_SESSION['AuthHashGeneratedAt'] = $time;
# Because we don't write out the session, it shouldn't actually get written out to disk. However if it does, the GeneratedAt should protect us.
2018-04-07 02:31:11 +08:00
} # end if AuthHash is not cached
return $_SESSION['AuthHash'.$_SESSION['remoteAddr']];
} # end if using AUTH and AUTH_RELAY
return '';
2018-04-07 02:31:11 +08:00
}
2018-05-01 01:02:53 +08:00
function visibleMonitor($mid) {
2018-04-07 02:31:11 +08:00
global $user;
return ( $user && empty($user['MonitorIds']) || in_array($mid, explode(',', $user['MonitorIds'])) );
2018-04-07 02:31:11 +08:00
}
2018-05-01 01:02:53 +08:00
function canView($area, $mid=false) {
2018-04-07 02:31:11 +08:00
global $user;
return ( $user && ($user[$area] == 'View' || $user[$area] == 'Edit') && ( !$mid || visibleMonitor($mid) ) );
2018-04-07 02:31:11 +08:00
}
2018-05-01 01:02:53 +08:00
function canEdit($area, $mid=false) {
2018-04-07 02:31:11 +08:00
global $user;
return ( $user && ($user[$area] == 'Edit') && ( !$mid || visibleMonitor($mid) ));
2018-04-07 02:31:11 +08:00
}
2019-09-03 22:54:34 +08:00
function userFromSession() {
$user = null; // Not global
if ( isset($_SESSION['username']) ) {
if ( ZM_AUTH_HASH_LOGINS and (ZM_AUTH_RELAY == 'hashed') ) {
# Extra validation, if logged in, then the auth hash will be set in the session, so we can validate it.
# This prevent session modification to switch users
if ( isset($_SESSION['AuthHash'.$_SESSION['remoteAddr']]) )
$user = getAuthUser($_SESSION['AuthHash'.$_SESSION['remoteAddr']]);
else
2020-10-21 22:37:06 +08:00
ZM\Debug('No auth hash in session, there should have been');
2019-09-03 23:33:13 +08:00
} else {
# Need to refresh permissions and validate that the user still exists
$sql = 'SELECT * FROM Users WHERE Enabled=1 AND Username=?';
$user = dbFetchOne($sql, NULL, array($_SESSION['username']));
}
2019-09-03 22:54:34 +08:00
}
return $user;
}
function get_auth_relay() {
if ( ZM_OPT_USE_AUTH ) {
if ( ZM_AUTH_RELAY == 'hashed' ) {
return 'auth='.generateAuthHash(ZM_AUTH_HASH_IPS);
} else if ( ZM_AUTH_RELAY == 'plain' ) {
// password probably needs to be escaped
return 'username='.$_SESSION['username'].'&password='.urlencode($_SESSION['password']);
} else if ( ZM_AUTH_RELAY == 'none' ) {
return 'username='.$_SESSION['username'];
} else {
ZM\Error('Unknown value for ZM_AUTH_RELAY ' . ZM_AUTH_RELAY);
}
}
return '';
} // end function get_auth_relay
if ( ZM_OPT_USE_AUTH ) {
if ( !empty($_REQUEST['token']) ) {
// we only need to get the username here
// don't know the token type. That will
// be checked later
$ret = validateToken($_REQUEST['token'], 'any');
$user = $ret[0];
} else {
// Non token based auth
if ( ZM_AUTH_HASH_LOGINS && empty($user) && !empty($_REQUEST['auth']) ) {
$user = getAuthUser($_REQUEST['auth']);
} else if (
2019-08-20 00:08:41 +08:00
! (
empty($_REQUEST['username']) or
empty($_REQUEST['password']) or
(defined('ZM_OPT_USE_GOOG_RECAPTCHA') && ZM_OPT_USE_GOOG_RECAPTCHA)
) ) {
2019-08-20 00:08:41 +08:00
$ret = validateUser($_REQUEST['username'], $_REQUEST['password']);
if ( !$ret[0] ) {
ZM\Error($ret[1]);
unset($user); // unset should be ok here because we aren't in a function
return;
}
$user = $ret[0];
} else if ( (ZM_AUTH_TYPE == 'remote') and !empty($_SERVER['REMOTE_USER']) ) {
$sql = 'SELECT * FROM Users WHERE Enabled=1 AND Username=?';
// local user, shouldn't affect the global user
$user = dbFetchOne($sql, NULL, array($_SERVER['REMOTE_USER']));
} else {
$user = userFromSession();
}
2019-05-15 07:22:49 +08:00
if ( !empty($user) ) {
// generate it once here, while session is open. Value will be cached in session and return when called later on
generateAuthHash(ZM_AUTH_HASH_IPS);
}
} # end if token based auth
} else {
$user = $defaultUser;
} # end if ZM_OPT_USE_AUTH
2018-04-07 02:31:11 +08:00
?>