/usr/share/horde/gollem/lib/Auth.php is in php-horde-gollem 3.0.10-1.
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 | <?php
/**
* The Gollem_Auth class provides authentication for Gollem.
*
* Copyright 2004-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Michael Slusarz <slusarz@horde.org>
* @author Jan Schneider <jan@horde.org>
* @package Gollem
*/
class Gollem_Auth
{
/**
* Authenticate to the backend.
*
* @param array $credentials An array of login credentials. If empty,
* attempts to login to the cached session.
* <pre>
* 'password' - (string) The user password.
* 'backend' - (string) The backend key to use (from backends.php).
* 'userId' - (string) The username.
* </pre>
*
* @return mixed If authentication was successful, and no session
* exists, an array of data to add to the session.
* Otherwise returns false.
* @throws Horde_Auth_Exception
*/
static public function authenticate($credentials = array())
{
$result = false;
// Do 'horde' authentication.
$gollem_app = $GLOBALS['registry']->getApiInstance('gollem', 'application');
if (!empty($gollem_app->initParams['authentication']) &&
($gollem_app->initParams['authentication'] == 'horde')) {
if ($registry->getAuth()) {
return $result;
}
throw new Horde_Auth_Exception('', Horde_Auth::REASON_FAILED);
}
// Load backend.
if (!isset($credentials['backend_key'])) {
$credentials['backend_key'] = self::getPreferredBackend();
}
$backend = self::getBackend($credentials['backend_key']);
// Check for hordeauth.
if ((!isset($credentials['userId']) ||
!isset($credentials['password'])) &&
!$GLOBALS['session']->exists('gollem', 'backend_key') &&
self::canAutoLogin($credentials['backend_key'])) {
if (!empty($backend['hordeauth'])) {
$credentials['userId'] = self::getAutologinID($credentials['backend_key']);
$credentials['password'] = $GLOBALS['registry']->getAuthCredential('password');
}
}
// Check for hardcoded backend credentials.
if (!isset($credentials['userId']) &&
!empty($backend['params']['username'])) {
$credentials['userId'] = $backend['params']['username'];
}
if (!isset($credentials['password']) &&
!empty($backend['params']['password'])) {
$credentials['password'] = $backend['params']['password'];
}
if (!isset($credentials['userId']) ||
!isset($credentials['password'])) {
throw new Horde_Auth_Exception('', Horde_Auth::REASON_BADLOGIN);
}
try {
$vfs = $GLOBALS['injector']
->getInstance('Gollem_Factory_Vfs')
->create($credentials['backend_key']);
$params = array('username' => $credentials['userId'],
'password' => $credentials['password']);
foreach (array_keys($backend['loginparams']) as $param) {
if (isset($credentials[$param])) {
$backend['params'][$param] = $params[$param] = $credentials[$param];
}
}
$vfs->setParams($params);
$vfs->checkCredentials();
} catch (Horde_Exception $e) {
throw new Horde_Auth_Exception($e->getMessage(), Horde_Auth::REASON_MESSAGE);
}
// Set current backend.
Gollem::$backend = &$backend;
// Mark backend as authenticated.
$backend['auth'] = true;
// Save username in backend configuration.
if (!isset($backend['params']['username'])) {
$backend['params']['username'] = $credentials['userId'];
}
if (!isset($backend['params']['password'])) {
$backend['params']['password'] = $credentials['password'];
}
// Make sure we have a 'root' parameter.
if (empty($backend['root'])) {
$backend['root'] = '/';
}
$backend['root'] = Horde_Util::realPath($backend['root']);
// Make sure we have a 'home' parameter.
if (empty($backend['home'])) {
$backend['home'] = empty($backend['params']['home'])
? $vfs->getCurrentDirectory()
: $backend['params']['home'];
if (empty($backend['home'])) {
$backend['home'] = $backend['root'];
}
}
// Make sure the home parameter lives under root if it is a relative
// directory.
if (strpos($backend['home'], '/') !== 0) {
$backend['home'] = $backend['root'] . '/' . $backend['home'];
}
$backend['home'] = Horde_Util::realPath($backend['home']);
$backend['dir'] = $backend['home'];
// Verify that home is below root.
if (!Gollem::verifyDir($backend['home'])) {
throw new Horde_Auth_Exception('Backend Configuration Error: Home directory not below root.', Horde_Auth::REASON_MESSAGE);
}
// Create the home directory if it doesn't already exist.
if ($backend['home'] != '/' && !empty($backend['createhome'])) {
$pos = strrpos($backend['home'], '/');
$cr_dir = substr($backend['home'], 0, $pos);
$cr_file = substr($backend['home'], $pos + 1);
if (!$vfs->exists($cr_dir, $cr_file)) {
try {
$res = Gollem::createFolder($cr_dir, $cr_file, $vfs);
} catch (Gollem_Exception $e) {
throw new Horde_Auth_Exception('Backend Configuration Error: Could not create home directory ' . $backend['home'] . ': ' . $e->getMessage(), Horde_Auth::REASON_MESSAGE);
}
}
}
// Write the backend to the session.
$backends = self::_getBackends();
$backends[$credentials['backend_key']] = $backend;
self::_setBackends($backends);
return array('backend_key' => $credentials['backend_key']);
}
/**
* Perform transparent authentication.
*
* @param Horde_Auth_Application $auth_ob The authentication object.
*
* @return mixed If authentication was successful, and no session
* exists, an array of data to add to the session.
* Otherwise returns false.
*/
static public function transparent($auth_ob)
{
$credentials = $auth_ob->getCredential('credentials');
if (empty($credentials['transparent'])) {
/* Attempt hordeauth authentication. */
$credentials = self::canAutoLogin();
if ($credentials === false) {
return false;
}
} else {
/* It is possible that preauthenticate() set the credentials.
* If so, use that information instead of hordeauth. */
$credentials['userId'] = $auth_ob->getCredential('userId');
}
try {
return self::authenticate($credentials);
} catch (Horde_Auth_Exception $e) {
return false;
}
}
/**
* Loads the Gollem backend configuration from backends.php.
*
* @param string $backend Returns this labeled entry only.
*
* @return mixed If $backend is set return this entry; else, return the
* entire backends array. Returns false on error.
*/
static public function getBackend($backend = null)
{
if (!($backends = self::_getBackends())) {
try {
$backends = Horde::loadConfiguration('backends.php', 'backends', 'gollem');
if (is_null($backends)) {
return false;
}
} catch (Horde_Exception $e) {
Horde::log($e, 'ERR');
return false;
}
foreach (array_keys($backends) as $key) {
if (!empty($backends[$key]['disabled']) ||
!Gollem::checkPermissions('backend', Horde_Perms::SHOW, $key)) {
unset($backends[$key]);
}
}
self::_setBackends($backends);
}
if (is_null($backend)) {
return $backends;
}
/* Check for the existence of the backend in the config file. */
if (empty($backends[$backend]) || !is_array($backends[$backend])) {
$entry = sprintf('Invalid backend key "%s" from client [%s]',
$backend, $_SERVER['REMOTE_ADDR']);
Horde::log($entry, 'ERR');
return false;
}
return $backends[$backend];
}
/**
* Get the current preferred backend key.
*
* @return string The preferred backend key.
*/
static public function getPreferredBackend()
{
if ($backend_key = $GLOBALS['session']->get('gollem', 'backend_key')) {
return $backend_key;
}
/* Determine the preferred backend. */
foreach (self::getBackend() as $key => $backend) {
if (empty($backend_key) && (substr($key, 0, 1) != '_')) {
$backend_key = $key;
}
if (empty($backend['preferred'])) {
continue;
}
$preferred = is_array($backend['preferred'])
? $backend['preferred']
: array($backend['preferred']);
if (in_array($_SERVER['SERVER_NAME'], $preferred) ||
in_array($_SERVER['HTTP_HOST'], $preferred)) {
$backend_key = $key;
}
}
return $backend_key;
}
/**
* Get the authentication ID to use for autologins based on the value of
* the 'hordeauth' parameter.
*
* @param string $backend The backend to login to.
*
* @return string The ID string to use for logins.
*/
static public function getAutologinID($backend)
{
$config = self::getBackend($backend);
return (!empty($config['hordeauth']) &&
strcasecmp($config['hordeauth'], 'full') === 0)
? $GLOBALS['registry']->getAuth()
: $GLOBALS['registry']->getAuth('bare');
}
/**
* Can we log in without a login screen for the requested backend key?
*
* @param string $key The backend to login to.
*
* @return array The credentials needed to login ('userId', 'password',
* 'backend') or false if autologin not available.
*/
static public function canAutoLogin($key = null)
{
if (is_null($key)) {
$key = self::getPreferredBackend();
}
if ($key &&
$GLOBALS['registry']->getAuth() &&
($config = self::getBackend($key)) &&
empty($config['loginparams']) &&
!empty($config['hordeauth'])) {
return array(
'userId' => self::getAutologinID($key),
'password' => $GLOBALS['registry']->getAuthCredential('password'),
'backend_key' => $key
);
}
return false;
}
/**
* Change the currently active backend.
*
* @param string $key The ID of the backend to set as active.
*/
static public function changeBackend($key)
{
$GLOBALS['session']->set('gollem', 'backend_key', $key);
Gollem::$backend = self::getBackend($key);
}
/**
* Return stored backend list.
*
* @return array Backend configuration list.
*/
static protected function _getBackends()
{
global $session;
if ($backends = $session->get('gollem', 'backends', $session::TYPE_ARRAY)) {
$passwords = $session->get('gollem', 'backends_password', $session::TYPE_ARRAY);
if ($passwords) {
foreach ($passwords as $key => $val) {
$backends[$key]['params']['password'] = $val;
}
}
}
return $backends;
}
/**
* Store backend list.
*
* @param array $backends Backend configuration list.
*/
static protected function _setBackends($backends)
{
global $session;
$passwords = array();
foreach ($backends as $key => $val) {
if (isset($val['params']['password'])) {
$passwords[$key] = $val['params']['password'];
unset($backends[$key]['params']['password']);
}
}
$session->set('gollem', 'backends', $backends);
if (!empty($passwords)) {
$session->set('gollem', 'backends_password', $passwords, $session::ENCRYPT);
}
}
}
|