/usr/share/php/Horde/Auth/Sql.php is in php-horde-auth 2.1.11-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | <?php
/**
* Copyright 1999-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you did
* not receive this file, http://www.horde.org/licenses/lgpl21
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL-2.1
* @package Auth
*/
/**
* The Horde_Auth_Sql class provides a SQL implementation of the Horde
* authentication system.
*
* The table structure for the Auth system needs to be created with the shipped
* migration scripts. See "horde-db-migrate-component --help" for details.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL-2.1
* @package Auth
*/
class Horde_Auth_Sql extends Horde_Auth_Base
{
/**
* An array of capabilities, so that the driver can report which
* operations it supports and which it doesn't.
*
* @var array
*/
protected $_capabilities = array(
'add' => true,
'list' => true,
'remove' => true,
'resetpassword' => true,
'update' => true,
'authenticate' => true,
);
/**
* Handle for the current database connection.
*
* @var Horde_Db_Adapter
*/
protected $_db;
/**
* Constructor
*
* @param array $params Parameters:
* 'db' - (Horde_Db_Adapter) [REQUIRED] Database object.
* <pre>
* 'encryption' - (string) The encryption to use to store the password in
* the table (e.g. plain, crypt, md5-hex, md5-base64, smd5,
* sha, ssha, aprmd5).
* DEFAULT: 'md5-hex'
* 'hard_expiration_field' - (string) The name of the field containing a
* date after which the account is no longer
* valid and the user will not be able to log in
* at all.
* DEFAULT: none
* 'password_field' - (string) The name of the password field in the auth
* table.
* DEFAULT: 'user_pass'
* 'show_encryption' - (boolean) Whether or not to prepend the encryption
* in the password field.
* DEFAULT: false
* 'soft_expiration_field' - (string) The name of the field containing a
* date after which the system will request the
* user change his or her password.
* DEFAULT: none
* 'table' - (string) The name of the SQL table to use in 'database'.
* DEFAULT: 'horde_users'
* 'username_field' - (string) The name of the username field in the auth
* table.
* DEFAULT: 'user_uid'
* </pre>
*
* @throws InvalidArgumentException
*/
public function __construct(array $params = array())
{
if (!isset($params['db'])) {
throw new InvalidArgumentException('Missing db parameter.');
}
$this->_db = $params['db'];
unset($params['db']);
$params = array_merge(array(
'encryption' => 'md5-hex',
'password_field' => 'user_pass',
'show_encryption' => false,
'table' => 'horde_users',
'username_field' => 'user_uid',
'soft_expiration_field' => null,
'soft_expiration_window' => null,
'hard_expiration_field' => null,
'hard_expiration_window' => null
), $params);
parent::__construct($params);
/* Only allow limits when there is a storage configured */
if ((empty($params['soft_expiration_field'])) &&
($params['soft_expiration_window'] > 0)) {
throw new InvalidArgumentException('You cannot set [soft_expiration_window] without [soft_expiration_field].');
}
if (($params['hard_expiration_field'] == '') &&
($params['hard_expiration_window'] > 0)) {
throw new InvalidArgumentException('You cannot set [hard_expiration_window] without [hard_expiration_field].');
}
}
/**
* Find out if a set of login credentials are valid.
*
* @param string $userId The userId to check.
* @param array $credentials The credentials to use.
*
* @throws Horde_Auth_Exception
*/
protected function _authenticate($userId, $credentials)
{
/* Build the SQL query. */
$query = sprintf('SELECT * FROM %s WHERE %s = ?',
$this->_params['table'],
$this->_params['username_field']);
$values = array($userId);
try {
$row = $this->_db->selectOne($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Horde_Auth_Exception('', Horde_Auth::REASON_FAILED);
}
if (!$row ||
!$this->_comparePasswords($row[$this->_params['password_field']], $credentials['password'])) {
throw new Horde_Auth_Exception('', Horde_Auth::REASON_BADLOGIN);
}
$now = time();
if (!empty($this->_params['hard_expiration_field']) &&
!empty($row[$this->_params['hard_expiration_field']]) &&
($now > $row[$this->_params['hard_expiration_field']])) {
throw new Horde_Auth_Exception('', Horde_Auth::REASON_EXPIRED);
}
if (!empty($this->_params['soft_expiration_field']) &&
!empty($row[$this->_params['soft_expiration_field']]) &&
($now > $row[$this->_params['soft_expiration_field']])) {
$this->setCredential('change', true);
$this->setCredential('expire', $now);
}
}
/**
* Add a set of authentication credentials.
*
* @param string $userId The userId to add.
* @param array $credentials The credentials to add.
*
* @throws Horde_Auth_Exception
*/
public function addUser($userId, $credentials)
{
/* Build the SQL query. */
$query = sprintf('INSERT INTO %s (%s, %s',
$this->_params['table'],
$this->_params['username_field'],
$this->_params['password_field']);
$query_values_part = ' VALUES (?, ?';
$values = array($userId,
Horde_Auth::getCryptedPassword($credentials['password'],
'',
$this->_params['encryption'],
$this->_params['show_encryption']));
if (!empty($this->_params['soft_expiration_field'])) {
$query .= sprintf(', %s', $this->_params['soft_expiration_field']);
$query_values_part .= ', ?';
$values[] = $this->_calc_expiration('soft');
}
if (!empty($this->_params['hard_expiration_field'])) {
$query .= sprintf(', %s', $this->_params['hard_expiration_field']);
$query_values_part .= ', ?';
$values[] = $this->_calc_expiration('hard');
}
$query .= ')' . $query_values_part . ')';
try {
$this->_db->insert($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Horde_Auth_Exception($e);
}
}
/**
* Update a set of authentication credentials.
*
* @param string $oldID The old userId.
* @param string $newID The new userId.
* @param array $credentials The new credentials
*
* @throws Horde_Auth_Exception
*/
public function updateUser($oldID, $newID, $credentials)
{
$query = sprintf('UPDATE %s SET ', $this->_params['table']);
$values = array();
/* Build the SQL query. */
$query .= $this->_params['username_field'] . ' = ?';
$values[] = $newID;
$query .= ', ' . $this->_params['password_field'] . ' = ?';
$values[] = Horde_Auth::getCryptedPassword($credentials['password'], '', $this->_params['encryption'], $this->_params['show_encryption']);
if (!empty($this->_params['soft_expiration_field'])) {
$query .= ', ' . $this->_params['soft_expiration_field'] . ' = ?';
$values[] = $this->_calc_expiration('soft');
}
if (!empty($this->_params['hard_expiration_field'])) {
$query .= ', ' . $this->_params['hard_expiration_field'] . ' = ?';
$values[] = $this->_calc_expiration('hard');
}
$query .= sprintf(' WHERE %s = ?', $this->_params['username_field']);
$values[] = $oldID;
try {
$this->_db->update($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Horde_Auth_Exception($e);
}
}
/**
* Reset a user's password. Used for example when the user does not
* remember the existing password.
*
* @param string $userId The user id for which to reset the password.
*
* @return string The new password on success.
* @throws Horde_Auth_Exception
*/
public function resetPassword($userId)
{
/* Get a new random password. */
$password = Horde_Auth::genRandomPassword();
/* Build the SQL query. */
$query = sprintf('UPDATE %s SET %s = ?',
$this->_params['table'],
$this->_params['password_field']);
$values = array(Horde_Auth::getCryptedPassword($password,
'',
$this->_params['encryption'],
$this->_params['show_encryption']));
if (!empty($this->_params['soft_expiration_field'])) {
$query .= ', ' . $this->_params['soft_expiration_field'] . ' = ?';
$values[] = $this->_calc_expiration('soft');
}
if (!empty($this->_params['hard_expiration_field'])) {
$query .= ', ' . $this->_params['hard_expiration_field'] . ' = ?';
$values[] = $this->_calc_expiration('hard');
}
$query .= sprintf(' WHERE %s = ?', $this->_params['username_field']);
$values[] = $userId;
try {
$this->_db->update($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Horde_Auth_Exception($e);
}
return $password;
}
/**
* Delete a set of authentication credentials.
*
* @param string $userId The userId to delete.
*
* @throws Horde_Auth_Exception
*/
public function removeUser($userId)
{
/* Build the SQL query. */
$query = sprintf('DELETE FROM %s WHERE %s = ?',
$this->_params['table'],
$this->_params['username_field']);
$values = array($userId);
try {
$this->_db->delete($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Horde_Auth_Exception($e);
}
}
/**
* List all users in the system.
*
* @param boolean $sort Sort the users?
*
* @return array The array of userIds.
* @throws Horde_Auth_Exception
*/
public function listUsers($sort = false)
{
/* Build the SQL query. */
$query = sprintf('SELECT %s FROM %s',
$this->_params['username_field'],
$this->_params['table']);
if ($sort) {
$query .= sprintf(' ORDER BY %s ASC',
$this->_params['username_field']);
}
try {
return $this->_db->selectValues($query);
} catch (Horde_Db_Exception $e) {
throw new Horde_Auth_Exception($e);
}
}
/**
* Checks if a userId exists in the system.
*
* @param string $userId User ID for which to check
*
* @return boolean Whether or not the userId already exists.
*/
public function exists($userId)
{
/* Build the SQL query. */
$query = sprintf('SELECT 1 FROM %s WHERE %s = ?',
$this->_params['table'],
$this->_params['username_field']);
$values = array($userId);
try {
return (bool)$this->_db->selectValue($query, $values);
} catch (Horde_Db_Exception $e) {
return false;
}
}
/**
* Compare an encrypted password to a plaintext string to see if
* they match.
*
* @param string $encrypted The crypted password to compare against.
* @param string $plaintext The plaintext password to verify.
*
* @return boolean True if matched, false otherwise.
*/
protected function _comparePasswords($encrypted, $plaintext)
{
return $encrypted == Horde_Auth::getCryptedPassword($plaintext,
$encrypted,
$this->_params['encryption'],
$this->_params['show_encryption']);
}
/**
* Calculate a timestamp and return it along with the field name
*
* @param string $type The timestamp parameter.
*
* @return integer 'timestamp' intended field value or null
*/
private function _calc_expiration($type)
{
if (empty($this->_params[$type . '_expiration_window'])) {
return null;
} else {
$now = new Horde_Date(time());
return $now->add(array('mday' => $this->_params[$type.'_expiration_window']))->timestamp();
}
}
}
|