11078ec26SAndreas Gohr<?php 21078ec26SAndreas Gohr 31078ec26SAndreas Gohrnamespace dokuwiki\plugin\pureldap\classes; 41078ec26SAndreas Gohr 508ace392SAndreas Gohruse dokuwiki\ErrorHandler; 608ace392SAndreas Gohruse dokuwiki\Logger; 708ace392SAndreas Gohruse dokuwiki\Utf8\Clean; 880ac552fSAndreas Gohruse dokuwiki\Utf8\PhpString; 91078ec26SAndreas Gohruse FreeDSx\Ldap\Entry\Attribute; 101078ec26SAndreas Gohruse FreeDSx\Ldap\Exception\BindException; 111078ec26SAndreas Gohruse FreeDSx\Ldap\Exception\ConnectionException; 121078ec26SAndreas Gohruse FreeDSx\Ldap\Exception\OperationException; 131078ec26SAndreas Gohruse FreeDSx\Ldap\LdapClient; 141078ec26SAndreas Gohr 151078ec26SAndreas Gohrabstract class Client 161078ec26SAndreas Gohr{ 17*208fe81aSAndreas Gohr public const FILTER_EQUAL = 'equal'; 18*208fe81aSAndreas Gohr public const FILTER_CONTAINS = 'contains'; 19*208fe81aSAndreas Gohr public const FILTER_STARTSWITH = 'startsWith'; 20*208fe81aSAndreas Gohr public const FILTER_ENDSWITH = 'endsWith'; 21204fba68SAndreas Gohr 221078ec26SAndreas Gohr /** @var array the configuration */ 231078ec26SAndreas Gohr protected $config; 241078ec26SAndreas Gohr 251078ec26SAndreas Gohr /** @var LdapClient */ 261078ec26SAndreas Gohr protected $ldap; 271078ec26SAndreas Gohr 2808ace392SAndreas Gohr /** @var bool|string is this client authenticated already? Contains username if yes */ 291078ec26SAndreas Gohr protected $isAuthenticated = false; 301078ec26SAndreas Gohr 311078ec26SAndreas Gohr /** @var array cached user info */ 321078ec26SAndreas Gohr protected $userCache = []; 331078ec26SAndreas Gohr 345a3b9122SAndreas Gohr /** @var array cached group list */ 355a3b9122SAndreas Gohr protected $groupCache = []; 365a3b9122SAndreas Gohr 371078ec26SAndreas Gohr /** 381078ec26SAndreas Gohr * Client constructor. 391078ec26SAndreas Gohr * @param array $config 401078ec26SAndreas Gohr */ 411078ec26SAndreas Gohr public function __construct($config) 421078ec26SAndreas Gohr { 43*208fe81aSAndreas Gohr require_once __DIR__ . '/../vendor/autoload.php'; 44*208fe81aSAndreas Gohr 451078ec26SAndreas Gohr $this->config = $this->prepareConfig($config); 46bf69b89cSAndreas Gohr $this->prepareSSO(); 471078ec26SAndreas Gohr $this->ldap = new LdapClient($this->config); 481078ec26SAndreas Gohr } 491078ec26SAndreas Gohr 501078ec26SAndreas Gohr /** 511078ec26SAndreas Gohr * Setup sane config defaults 521078ec26SAndreas Gohr * 531078ec26SAndreas Gohr * @param array $config 541078ec26SAndreas Gohr * @return array 551078ec26SAndreas Gohr */ 561078ec26SAndreas Gohr protected function prepareConfig($config) 571078ec26SAndreas Gohr { 581a4f0e1fSAndreas Gohr // ensure we have the default keys 591a4f0e1fSAndreas Gohr /** @var array $conf */ 601a4f0e1fSAndreas Gohr include __DIR__ . '/../conf/default.php'; 611a4f0e1fSAndreas Gohr $defaults = $conf; 621a4f0e1fSAndreas Gohr $defaults['defaultgroup'] = 'user'; // we expect this to be passed from global conf 631078ec26SAndreas Gohr 641078ec26SAndreas Gohr $config = array_merge($defaults, $config); 651078ec26SAndreas Gohr 661078ec26SAndreas Gohr // default port depends on SSL setting 671078ec26SAndreas Gohr if (!$config['port']) { 686d90d5c8SAndreas Gohr $config['port'] = ($config['encryption'] === 'ssl') ? 636 : 389; 696d90d5c8SAndreas Gohr } 706d90d5c8SAndreas Gohr 716d90d5c8SAndreas Gohr // set ssl parameters 726d90d5c8SAndreas Gohr $config['use_ssl'] = ($config['encryption'] === 'ssl'); 736d90d5c8SAndreas Gohr if ($config['validate'] === 'none') { 746d90d5c8SAndreas Gohr $config['ssl_validate_cert'] = false; 756d90d5c8SAndreas Gohr } elseif ($config['validate'] === 'self') { 766d90d5c8SAndreas Gohr $config['ssl_allow_self_signed'] = true; 771078ec26SAndreas Gohr } 781078ec26SAndreas Gohr 79fde03b26SAndreas Gohr $config['suffix'] = ltrim(PhpString::strtolower($config['suffix']), '@'); 80c2500b44SAndreas Gohr $config['primarygroup'] = $this->cleanGroup($config['primarygroup']); 8180ac552fSAndreas Gohr 821078ec26SAndreas Gohr return $config; 831078ec26SAndreas Gohr } 841078ec26SAndreas Gohr 851078ec26SAndreas Gohr /** 86bf69b89cSAndreas Gohr * Extract user info from environment for SSO 87bf69b89cSAndreas Gohr */ 88bf69b89cSAndreas Gohr protected function prepareSSO() 89bf69b89cSAndreas Gohr { 90bf69b89cSAndreas Gohr global $INPUT; 91bf69b89cSAndreas Gohr 92bf69b89cSAndreas Gohr if (!$this->config['sso']) return; 93bf69b89cSAndreas Gohr if ($INPUT->server->str('REMOTE_USER') === '') return; 94bf69b89cSAndreas Gohr 95bf69b89cSAndreas Gohr $user = $INPUT->server->str('REMOTE_USER'); 96bf69b89cSAndreas Gohr 97bf69b89cSAndreas Gohr // make sure the right encoding is used 98bf69b89cSAndreas Gohr if ($this->config['sso_charset']) { 9908ace392SAndreas Gohr if (function_exists('iconv')) { 100bf69b89cSAndreas Gohr $user = iconv($this->config['sso_charset'], 'UTF-8', $user); 10108ace392SAndreas Gohr } elseif (function_exists('mb_convert_encoding')) { 10208ace392SAndreas Gohr $user = mb_convert_encoding($user, 'UTF-8', $this->config['sso_charset']); 10308ace392SAndreas Gohr } 10408ace392SAndreas Gohr } elseif (!Clean::isUtf8($user)) { 105bf69b89cSAndreas Gohr $user = utf8_encode($user); 106bf69b89cSAndreas Gohr } 107bf69b89cSAndreas Gohr $user = $this->cleanUser($user); 108bf69b89cSAndreas Gohr 109bf69b89cSAndreas Gohr // Prepare SSO 110bf69b89cSAndreas Gohr 111bf69b89cSAndreas Gohr // trust the incoming user 112bf69b89cSAndreas Gohr $INPUT->server->set('REMOTE_USER', $user); 113bf69b89cSAndreas Gohr 114bf69b89cSAndreas Gohr // we need to simulate a login 115bf69b89cSAndreas Gohr if (empty($_COOKIE[DOKU_COOKIE])) { 116bf69b89cSAndreas Gohr $INPUT->set('u', $user); 117bf69b89cSAndreas Gohr $INPUT->set('p', 'sso_only'); 118bf69b89cSAndreas Gohr } 119bf69b89cSAndreas Gohr } 120bf69b89cSAndreas Gohr 121bf69b89cSAndreas Gohr /** 12222654fdeSAndreas Gohr * Access to the config values 12322654fdeSAndreas Gohr * 12422654fdeSAndreas Gohr * Because the client class does configuration cleanup, the auth class should access 12522654fdeSAndreas Gohr * config values through the client 12622654fdeSAndreas Gohr * 12722654fdeSAndreas Gohr * @param string $key 12822654fdeSAndreas Gohr * @return mixed returns null on missing config 12922654fdeSAndreas Gohr */ 13008ace392SAndreas Gohr public function getConf($key) 13108ace392SAndreas Gohr { 13222654fdeSAndreas Gohr if (!isset($this->config[$key])) return null; 13322654fdeSAndreas Gohr return $this->config[$key]; 13422654fdeSAndreas Gohr } 13522654fdeSAndreas Gohr 13622654fdeSAndreas Gohr /** 13708ace392SAndreas Gohr * Authenticate as admin if not authenticated yet 1381078ec26SAndreas Gohr */ 1391078ec26SAndreas Gohr public function autoAuth() 1401078ec26SAndreas Gohr { 1411078ec26SAndreas Gohr if ($this->isAuthenticated) return true; 1429446f9efSAndreas Gohr 143a1128cc0SAndreas Gohr $user = $this->prepareBindUser($this->config['admin_username']); 1449446f9efSAndreas Gohr $ok = $this->authenticate($user, $this->config['admin_password']); 1458b2677edSAndreas Gohr if (!$ok) { 14608ace392SAndreas Gohr $this->error('Automatic bind failed. Probably wrong user/password.', __FILE__, __LINE__); 1478b2677edSAndreas Gohr } 1488b2677edSAndreas Gohr return $ok; 1491078ec26SAndreas Gohr } 1501078ec26SAndreas Gohr 1511078ec26SAndreas Gohr /** 1521078ec26SAndreas Gohr * Authenticates a given user. This client will remain authenticated 1531078ec26SAndreas Gohr * 1541078ec26SAndreas Gohr * @param string $user 1551078ec26SAndreas Gohr * @param string $pass 1561078ec26SAndreas Gohr * @return bool was the authentication successful? 1575a3b9122SAndreas Gohr * @noinspection PhpRedundantCatchClauseInspection 1581078ec26SAndreas Gohr */ 1591078ec26SAndreas Gohr public function authenticate($user, $pass) 1601078ec26SAndreas Gohr { 161a1128cc0SAndreas Gohr $user = $this->prepareBindUser($user); 16208ace392SAndreas Gohr $this->isAuthenticated = false; 16380ac552fSAndreas Gohr 16408ace392SAndreas Gohr if (!$this->ldap->isConnected() && $this->config['encryption'] === 'tls') { 1651078ec26SAndreas Gohr try { 1661078ec26SAndreas Gohr $this->ldap->startTls(); 16708ace392SAndreas Gohr } catch (ConnectionException | OperationException $e) { 168da369b60SAndreas Gohr $this->fatal($e); 16908ace392SAndreas Gohr return false; 1701078ec26SAndreas Gohr } 1711078ec26SAndreas Gohr } 1721078ec26SAndreas Gohr 1731078ec26SAndreas Gohr try { 1741078ec26SAndreas Gohr $this->ldap->bind($user, $pass); 1751078ec26SAndreas Gohr } catch (BindException $e) { 176fde03b26SAndreas Gohr $this->debug("Bind for $user failed: " . $e->getMessage(), $e->getFile(), $e->getLine()); 1771078ec26SAndreas Gohr return false; 17808ace392SAndreas Gohr } catch (ConnectionException | OperationException $e) { 179da369b60SAndreas Gohr $this->fatal($e); 1801078ec26SAndreas Gohr return false; 1811078ec26SAndreas Gohr } 1821078ec26SAndreas Gohr 18308ace392SAndreas Gohr $this->isAuthenticated = $user; 1841078ec26SAndreas Gohr return true; 1851078ec26SAndreas Gohr } 1861078ec26SAndreas Gohr 1871078ec26SAndreas Gohr /** 1881078ec26SAndreas Gohr * Get info for a single user, use cache if available 1891078ec26SAndreas Gohr * 1901078ec26SAndreas Gohr * @param string $username 1911078ec26SAndreas Gohr * @param bool $fetchgroups Are groups needed? 1921078ec26SAndreas Gohr * @return array|null 1931078ec26SAndreas Gohr */ 1941078ec26SAndreas Gohr public function getCachedUser($username, $fetchgroups = true) 1951078ec26SAndreas Gohr { 1966d90d5c8SAndreas Gohr global $conf; 1976d90d5c8SAndreas Gohr 1986d90d5c8SAndreas Gohr // memory cache first 1991078ec26SAndreas Gohr if (isset($this->userCache[$username])) { 2001078ec26SAndreas Gohr if (!$fetchgroups || is_array($this->userCache[$username]['grps'])) { 2011078ec26SAndreas Gohr return $this->userCache[$username]; 2021078ec26SAndreas Gohr } 2031078ec26SAndreas Gohr } 2041078ec26SAndreas Gohr 2056d90d5c8SAndreas Gohr // disk cache second 2065dcabedaSAndreas Gohr if ($this->config['usefscache']) { 2076d90d5c8SAndreas Gohr $cachename = getCacheName($username, '.pureldap-user'); 2086d90d5c8SAndreas Gohr $cachetime = @filemtime($cachename); 2096d90d5c8SAndreas Gohr if ($cachetime && (time() - $cachetime) < $conf['auth_security_timeout']) { 210*208fe81aSAndreas Gohr $this->userCache[$username] = json_decode( 211*208fe81aSAndreas Gohr file_get_contents($cachename), 212*208fe81aSAndreas Gohr true, 213*208fe81aSAndreas Gohr 512, 214*208fe81aSAndreas Gohr JSON_THROW_ON_ERROR 215*208fe81aSAndreas Gohr ); 2166d90d5c8SAndreas Gohr if (!$fetchgroups || is_array($this->userCache[$username]['grps'])) { 2176d90d5c8SAndreas Gohr return $this->userCache[$username]; 2186d90d5c8SAndreas Gohr } 2196d90d5c8SAndreas Gohr } 2205dcabedaSAndreas Gohr } 2216d90d5c8SAndreas Gohr 2221078ec26SAndreas Gohr // fetch fresh data 2231078ec26SAndreas Gohr $info = $this->getUser($username, $fetchgroups); 2241078ec26SAndreas Gohr 2251078ec26SAndreas Gohr // store in cache 2265dcabedaSAndreas Gohr if ($this->config['usefscache'] && $info !== null) { 2271078ec26SAndreas Gohr $this->userCache[$username] = $info; 22808ace392SAndreas Gohr /** @noinspection PhpUndefinedVariableInspection We know that cachename is defined */ 229*208fe81aSAndreas Gohr file_put_contents($cachename, json_encode($info, JSON_THROW_ON_ERROR)); 2301078ec26SAndreas Gohr } 2311078ec26SAndreas Gohr 2321078ec26SAndreas Gohr return $info; 2331078ec26SAndreas Gohr } 2341078ec26SAndreas Gohr 2351078ec26SAndreas Gohr /** 2361078ec26SAndreas Gohr * Fetch a single user 2371078ec26SAndreas Gohr * 2381078ec26SAndreas Gohr * @param string $username 2391078ec26SAndreas Gohr * @param bool $fetchgroups Shall groups be fetched, too? 2401078ec26SAndreas Gohr * @return null|array 2411078ec26SAndreas Gohr */ 2421078ec26SAndreas Gohr abstract public function getUser($username, $fetchgroups = true); 2431078ec26SAndreas Gohr 2441078ec26SAndreas Gohr /** 24508ace392SAndreas Gohr * Set a new password for a user 24608ace392SAndreas Gohr * 24708ace392SAndreas Gohr * @param string $username 24808ace392SAndreas Gohr * @param string $newpass 24908ace392SAndreas Gohr * @param string $oldpass Needed for self-service password change in AD 25008ace392SAndreas Gohr * @return bool 25108ace392SAndreas Gohr */ 25208ace392SAndreas Gohr abstract public function setPassword($username, $newpass, $oldpass = null); 25308ace392SAndreas Gohr 25408ace392SAndreas Gohr /** 2555a3b9122SAndreas Gohr * Return a list of all available groups, use cache if available 2565a3b9122SAndreas Gohr * 2575a3b9122SAndreas Gohr * @return string[] 2585a3b9122SAndreas Gohr */ 2595a3b9122SAndreas Gohr public function getCachedGroups() 2605a3b9122SAndreas Gohr { 2615a3b9122SAndreas Gohr if (empty($this->groupCache)) { 2625a3b9122SAndreas Gohr $this->groupCache = $this->getGroups(); 2635a3b9122SAndreas Gohr } 2645a3b9122SAndreas Gohr 2655a3b9122SAndreas Gohr return $this->groupCache; 2665a3b9122SAndreas Gohr } 2675a3b9122SAndreas Gohr 2685a3b9122SAndreas Gohr /** 2695a3b9122SAndreas Gohr * Return a list of all available groups 2705a3b9122SAndreas Gohr * 271b21740b4SAndreas Gohr * Optionally filter the list 272b21740b4SAndreas Gohr * 273b21740b4SAndreas Gohr * @param null|string $match Filter for this, null for all groups 274b21740b4SAndreas Gohr * @param string $filtermethod How to match the groups 2755a3b9122SAndreas Gohr * @return string[] 2765a3b9122SAndreas Gohr */ 277204fba68SAndreas Gohr abstract public function getGroups($match = null, $filtermethod = self::FILTER_EQUAL); 2785a3b9122SAndreas Gohr 27980ac552fSAndreas Gohr /** 280a1128cc0SAndreas Gohr * Clean the user name for use in DokuWiki 28180ac552fSAndreas Gohr * 282a1128cc0SAndreas Gohr * @param string $user 28380ac552fSAndreas Gohr * @return string 28480ac552fSAndreas Gohr */ 285a1128cc0SAndreas Gohr abstract public function cleanUser($user); 28680ac552fSAndreas Gohr 28780ac552fSAndreas Gohr /** 288a1128cc0SAndreas Gohr * Clean the group name for use in DokuWiki 28980ac552fSAndreas Gohr * 290a1128cc0SAndreas Gohr * @param string $group 29180ac552fSAndreas Gohr * @return string 29280ac552fSAndreas Gohr */ 293a1128cc0SAndreas Gohr abstract public function cleanGroup($group); 294a1128cc0SAndreas Gohr 295a1128cc0SAndreas Gohr /** 296a1128cc0SAndreas Gohr * Inheriting classes may want to manipulate the user before binding 297a1128cc0SAndreas Gohr * 298a1128cc0SAndreas Gohr * @param string $user 299a1128cc0SAndreas Gohr * @return string 300a1128cc0SAndreas Gohr */ 301a1128cc0SAndreas Gohr protected function prepareBindUser($user) 302a1128cc0SAndreas Gohr { 303a1128cc0SAndreas Gohr return $user; 304a1128cc0SAndreas Gohr } 30580ac552fSAndreas Gohr 3065a3b9122SAndreas Gohr /** 3071078ec26SAndreas Gohr * Helper method to get the first value of the given attribute 3081078ec26SAndreas Gohr * 3091078ec26SAndreas Gohr * The given attribute may be null, an empty string is returned then 3101078ec26SAndreas Gohr * 3111078ec26SAndreas Gohr * @param Attribute|null $attribute 3121078ec26SAndreas Gohr * @return string 3131078ec26SAndreas Gohr */ 3145a3b9122SAndreas Gohr protected function attr2str($attribute) 3155a3b9122SAndreas Gohr { 3161078ec26SAndreas Gohr if ($attribute !== null) { 3171078ec26SAndreas Gohr return $attribute->firstValue(); 3181078ec26SAndreas Gohr } 3191078ec26SAndreas Gohr return ''; 3201078ec26SAndreas Gohr } 3211078ec26SAndreas Gohr 3221078ec26SAndreas Gohr /** 3239c590892SAndreas Gohr * Get the attributes that should be fetched for a user 3249c590892SAndreas Gohr * 3259c590892SAndreas Gohr * Can be extended in sub classes 3269c590892SAndreas Gohr * 3279c590892SAndreas Gohr * @return Attribute[] 3289c590892SAndreas Gohr */ 3299c590892SAndreas Gohr protected function userAttributes() 3309c590892SAndreas Gohr { 3319c590892SAndreas Gohr // defaults 3329c590892SAndreas Gohr $attr = [ 3339c590892SAndreas Gohr new Attribute('dn'), 3349c590892SAndreas Gohr new Attribute('displayName'), 3359c590892SAndreas Gohr new Attribute('mail'), 3369c590892SAndreas Gohr ]; 3379c590892SAndreas Gohr // additionals 3389c590892SAndreas Gohr foreach ($this->config['attributes'] as $attribute) { 3399c590892SAndreas Gohr $attr[] = new Attribute($attribute); 3409c590892SAndreas Gohr } 3419c590892SAndreas Gohr return $attr; 3429c590892SAndreas Gohr } 3439c590892SAndreas Gohr 3449c590892SAndreas Gohr /** 3450f498d06SAndreas Gohr * Get the maximum age a password may have before it needs to be changed 3460f498d06SAndreas Gohr * 3470f498d06SAndreas Gohr * @return int 0 if no maximum age is set 3480f498d06SAndreas Gohr */ 349*208fe81aSAndreas Gohr public function getMaxPasswordAge() 350*208fe81aSAndreas Gohr { 3510f498d06SAndreas Gohr return 0; 3520f498d06SAndreas Gohr } 3530f498d06SAndreas Gohr 3540f498d06SAndreas Gohr /** 355da369b60SAndreas Gohr * Handle fatal exceptions 3561078ec26SAndreas Gohr * 3571078ec26SAndreas Gohr * @param \Exception $e 3581078ec26SAndreas Gohr */ 359da369b60SAndreas Gohr protected function fatal(\Exception $e) 3601078ec26SAndreas Gohr { 36108ace392SAndreas Gohr ErrorHandler::logException($e); 362c872f0e3SAndreas Gohr 3631078ec26SAndreas Gohr if (defined('DOKU_UNITTEST')) { 3648595f73eSAndreas Gohr throw new \RuntimeException('', 0, $e); 3651078ec26SAndreas Gohr } 366c872f0e3SAndreas Gohr 367c872f0e3SAndreas Gohr msg('[pureldap] ' . hsc($e->getMessage()), -1); 368da369b60SAndreas Gohr } 3691078ec26SAndreas Gohr 370da369b60SAndreas Gohr /** 371a1128cc0SAndreas Gohr * Handle error output 372a1128cc0SAndreas Gohr * 373a1128cc0SAndreas Gohr * @param string $msg 374a1128cc0SAndreas Gohr * @param string $file 375a1128cc0SAndreas Gohr * @param int $line 376a1128cc0SAndreas Gohr */ 37708ace392SAndreas Gohr public function error($msg, $file, $line) 378a1128cc0SAndreas Gohr { 37908ace392SAndreas Gohr Logger::error('[pureldap] ' . $msg, '', $file, $line); 380c872f0e3SAndreas Gohr 381a1128cc0SAndreas Gohr if (defined('DOKU_UNITTEST')) { 382a1128cc0SAndreas Gohr throw new \RuntimeException($msg . ' at ' . $file . ':' . $line); 383a1128cc0SAndreas Gohr } 384a1128cc0SAndreas Gohr 385c872f0e3SAndreas Gohr msg('[pureldap] ' . hsc($msg), -1); 386a1128cc0SAndreas Gohr } 387a1128cc0SAndreas Gohr 388a1128cc0SAndreas Gohr /** 389da369b60SAndreas Gohr * Handle debug output 390da369b60SAndreas Gohr * 391da369b60SAndreas Gohr * @param string $msg 392da369b60SAndreas Gohr * @param string $file 393da369b60SAndreas Gohr * @param int $line 394da369b60SAndreas Gohr */ 39508ace392SAndreas Gohr public function debug($msg, $file, $line) 396da369b60SAndreas Gohr { 397c872f0e3SAndreas Gohr global $conf; 398c872f0e3SAndreas Gohr 39908ace392SAndreas Gohr Logger::debug('[pureldap] ' . $msg, '', $file, $line); 400c872f0e3SAndreas Gohr 401c872f0e3SAndreas Gohr if ($conf['allowdebug']) { 40208ace392SAndreas Gohr msg('[pureldap] ' . hsc($msg) . ' at ' . $file . ':' . $line); 4031078ec26SAndreas Gohr } 4041078ec26SAndreas Gohr } 405c872f0e3SAndreas Gohr} 406