1f4476bd9SJan Schumann<?php 28553d24dSAndreas Gohruse dokuwiki\Extension\AuthPlugin; 3ab9790caSAndreas Gohruse dokuwiki\PassHash; 40489c64bSMoisés Braga Ribeirouse dokuwiki\Utf8\Sort; 5d17fc5deSAndreas Gohr 6f4476bd9SJan Schumann/** 7f4476bd9SJan Schumann * LDAP authentication backend 8f4476bd9SJan Schumann * 9f4476bd9SJan Schumann * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 10f4476bd9SJan Schumann * @author Andreas Gohr <andi@splitbrain.org> 11f4476bd9SJan Schumann * @author Chris Smith <chris@jalakaic.co.uk> 12f4476bd9SJan Schumann * @author Jan Schumann <js@schumann-it.com> 13f4476bd9SJan Schumann */ 148553d24dSAndreas Gohrclass auth_plugin_authldap extends AuthPlugin 15d17fc5deSAndreas Gohr{ 1670e4a085SAndreas Gohr /* @var resource $con holds the LDAP connection */ 17ab9790caSAndreas Gohr protected $con; 1870e4a085SAndreas Gohr 1970e4a085SAndreas Gohr /* @var int $bound What type of connection does already exist? */ 2070e4a085SAndreas Gohr protected $bound = 0; // 0: anonymous, 1: user, 2: superuser 2170e4a085SAndreas Gohr 2270e4a085SAndreas Gohr /* @var array $users User data cache */ 23ab9790caSAndreas Gohr protected $users; 2470e4a085SAndreas Gohr 25d17fc5deSAndreas Gohr /* @var array $pattern User filter pattern */ 26ab9790caSAndreas Gohr protected $pattern; 27f4476bd9SJan Schumann 28f4476bd9SJan Schumann /** 29f4476bd9SJan Schumann * Constructor 30f4476bd9SJan Schumann */ 31d17fc5deSAndreas Gohr public function __construct() 32d17fc5deSAndreas Gohr { 33454d868bSAndreas Gohr parent::__construct(); 34454d868bSAndreas Gohr 35f4476bd9SJan Schumann // ldap extension is needed 36f4476bd9SJan Schumann if (!function_exists('ldap_connect')) { 37d17fc5deSAndreas Gohr $this->debug("LDAP err: PHP LDAP extension not found.", -1, __LINE__, __FILE__); 38f4476bd9SJan Schumann $this->success = false; 39f4476bd9SJan Schumann return; 40f4476bd9SJan Schumann } 41f4476bd9SJan Schumann 4206da270eSAxel Angel // Add the capabilities to change the password 436619ddf4SSascha Klopp $this->cando['modPass'] = $this->getConf('modPass'); 44f4476bd9SJan Schumann } 45f4476bd9SJan Schumann 46f4476bd9SJan Schumann /** 47f4476bd9SJan Schumann * Check user+password 48f4476bd9SJan Schumann * 49f4476bd9SJan Schumann * Checks if the given user exists and the given 50f4476bd9SJan Schumann * plaintext password is correct by trying to bind 51f4476bd9SJan Schumann * to the LDAP server 52f4476bd9SJan Schumann * 5370e4a085SAndreas Gohr * @param string $user 5470e4a085SAndreas Gohr * @param string $pass 55f4476bd9SJan Schumann * @return bool 56c0c77cd2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 57f4476bd9SJan Schumann */ 58d17fc5deSAndreas Gohr public function checkPass($user, $pass) 59d17fc5deSAndreas Gohr { 60f4476bd9SJan Schumann // reject empty password 61f4476bd9SJan Schumann if (empty($pass)) return false; 62d17fc5deSAndreas Gohr if (!$this->openLDAP()) return false; 63f4476bd9SJan Schumann 64f4476bd9SJan Schumann // indirect user bind 6570e4a085SAndreas Gohr if ($this->getConf('binddn') && $this->getConf('bindpw')) { 66f4476bd9SJan Schumann // use superuser credentials 678ef94e9eSAndreas Gohr if (!@ldap_bind($this->con, $this->getConf('binddn'), conf_decodeString($this->getConf('bindpw')))) { 68d17fc5deSAndreas Gohr $this->debug('LDAP bind as superuser: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); 69f4476bd9SJan Schumann return false; 70f4476bd9SJan Schumann } 71f4476bd9SJan Schumann $this->bound = 2; 72*7d34963bSAndreas Gohr } elseif ( 73*7d34963bSAndreas Gohr $this->getConf('binddn') && 7470e4a085SAndreas Gohr $this->getConf('usertree') && 7570e4a085SAndreas Gohr $this->getConf('userfilter') 7670e4a085SAndreas Gohr ) { 77f4476bd9SJan Schumann // special bind string 78d17fc5deSAndreas Gohr $dn = $this->makeFilter( 7970e4a085SAndreas Gohr $this->getConf('binddn'), 80ab9790caSAndreas Gohr ['user' => $user, 'server' => $this->getConf('server')] 8170e4a085SAndreas Gohr ); 8270e4a085SAndreas Gohr } elseif (strpos($this->getConf('usertree'), '%{user}')) { 83f4476bd9SJan Schumann // direct user bind 84d17fc5deSAndreas Gohr $dn = $this->makeFilter( 8570e4a085SAndreas Gohr $this->getConf('usertree'), 86ab9790caSAndreas Gohr ['user' => $user, 'server' => $this->getConf('server')] 8770e4a085SAndreas Gohr ); 88ab9790caSAndreas Gohr } elseif (!@ldap_bind($this->con)) { 89f4476bd9SJan Schumann // Anonymous bind 90f4476bd9SJan Schumann msg("LDAP: can not bind anonymously", -1); 91d17fc5deSAndreas Gohr $this->debug('LDAP anonymous bind: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); 92f4476bd9SJan Schumann return false; 93f4476bd9SJan Schumann } 94f4476bd9SJan Schumann 95f4476bd9SJan Schumann // Try to bind to with the dn if we have one. 96f4476bd9SJan Schumann if (!empty($dn)) { 97f4476bd9SJan Schumann // User/Password bind 98f4476bd9SJan Schumann if (!@ldap_bind($this->con, $dn, $pass)) { 99d17fc5deSAndreas Gohr $this->debug("LDAP: bind with $dn failed", -1, __LINE__, __FILE__); 100d17fc5deSAndreas Gohr $this->debug('LDAP user dn bind: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); 101f4476bd9SJan Schumann return false; 102f4476bd9SJan Schumann } 103f4476bd9SJan Schumann $this->bound = 1; 104f4476bd9SJan Schumann return true; 105f4476bd9SJan Schumann } else { 106f4476bd9SJan Schumann // See if we can find the user 107d17fc5deSAndreas Gohr $info = $this->fetchUserData($user, true); 108f4476bd9SJan Schumann if (empty($info['dn'])) { 109f4476bd9SJan Schumann return false; 110f4476bd9SJan Schumann } else { 111f4476bd9SJan Schumann $dn = $info['dn']; 112f4476bd9SJan Schumann } 113f4476bd9SJan Schumann 114f4476bd9SJan Schumann // Try to bind with the dn provided 115f4476bd9SJan Schumann if (!@ldap_bind($this->con, $dn, $pass)) { 116d17fc5deSAndreas Gohr $this->debug("LDAP: bind with $dn failed", -1, __LINE__, __FILE__); 117d17fc5deSAndreas Gohr $this->debug('LDAP user bind: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); 118f4476bd9SJan Schumann return false; 119f4476bd9SJan Schumann } 120f4476bd9SJan Schumann $this->bound = 1; 121f4476bd9SJan Schumann return true; 122f4476bd9SJan Schumann } 123f4476bd9SJan Schumann } 124f4476bd9SJan Schumann 125f4476bd9SJan Schumann /** 126f4476bd9SJan Schumann * Return user info 127f4476bd9SJan Schumann * 128f4476bd9SJan Schumann * Returns info about the given user needs to contain 129f4476bd9SJan Schumann * at least these fields: 130f4476bd9SJan Schumann * 131f4476bd9SJan Schumann * name string full name of the user 132f4476bd9SJan Schumann * mail string email addres of the user 133f4476bd9SJan Schumann * grps array list of groups the user is in 134f4476bd9SJan Schumann * 135f4476bd9SJan Schumann * This LDAP specific function returns the following 136f4476bd9SJan Schumann * addional fields: 137f4476bd9SJan Schumann * 138f4476bd9SJan Schumann * dn string distinguished name (DN) 139f4476bd9SJan Schumann * uid string Posix User ID 140f4476bd9SJan Schumann * inbind bool for internal use - avoid loop in binding 141f4476bd9SJan Schumann * 142c0c77cd2SAndreas Gohr * @param string $user 143c0c77cd2SAndreas Gohr * @param bool $requireGroups (optional) - ignored, groups are always supplied by this plugin 144c0c77cd2SAndreas Gohr * @return array containing user data or false 145f4476bd9SJan Schumann * @author <evaldas.auryla@pheur.org> 146f4476bd9SJan Schumann * @author Stephane Chazelas <stephane.chazelas@emerson.com> 1473e23f03eSSteScho * @author Steffen Schoch <schoch@dsb.net> 14870e4a085SAndreas Gohr * 149c0c77cd2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 150c0c77cd2SAndreas Gohr * @author Trouble 151c0c77cd2SAndreas Gohr * @author Dan Allen <dan.j.allen@gmail.com> 152d397e6daSChristopher Smith */ 153d17fc5deSAndreas Gohr public function getUserData($user, $requireGroups = true) 154d17fc5deSAndreas Gohr { 155d17fc5deSAndreas Gohr return $this->fetchUserData($user); 156d397e6daSChristopher Smith } 157d397e6daSChristopher Smith 158d397e6daSChristopher Smith /** 159d397e6daSChristopher Smith * @param string $user 16070e4a085SAndreas Gohr * @param bool $inbind authldap specific, true if in bind phase 161f4476bd9SJan Schumann * @return array containing user data or false 162f4476bd9SJan Schumann */ 163d17fc5deSAndreas Gohr protected function fetchUserData($user, $inbind = false) 164d17fc5deSAndreas Gohr { 165f4476bd9SJan Schumann global $conf; 166ab9790caSAndreas Gohr if (!$this->openLDAP()) return []; 167f4476bd9SJan Schumann 168f4476bd9SJan Schumann // force superuser bind if wanted and not bound as superuser yet 16970e4a085SAndreas Gohr if ($this->getConf('binddn') && $this->getConf('bindpw') && $this->bound < 2) { 170f4476bd9SJan Schumann // use superuser credentials 1718ef94e9eSAndreas Gohr if (!@ldap_bind($this->con, $this->getConf('binddn'), conf_decodeString($this->getConf('bindpw')))) { 172d17fc5deSAndreas Gohr $this->debug('LDAP bind as superuser: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); 173ab9790caSAndreas Gohr return []; 174f4476bd9SJan Schumann } 175f4476bd9SJan Schumann $this->bound = 2; 176f4476bd9SJan Schumann } elseif ($this->bound == 0 && !$inbind) { 177f4476bd9SJan Schumann // in some cases getUserData is called outside the authentication workflow 178f4476bd9SJan Schumann // eg. for sending email notification on subscribed pages. This data might not 179f4476bd9SJan Schumann // be accessible anonymously, so we try to rebind the current user here 180ab9790caSAndreas Gohr [$loginuser, $loginsticky, $loginpass] = auth_getCookie(); 181f4476bd9SJan Schumann if ($loginuser && $loginpass) { 182b4304655SMichael Hamann $loginpass = auth_decrypt($loginpass, auth_cookiesalt(!$loginsticky, true)); 183f4476bd9SJan Schumann $this->checkPass($loginuser, $loginpass); 184f4476bd9SJan Schumann } 185f4476bd9SJan Schumann } 186f4476bd9SJan Schumann 187ab9790caSAndreas Gohr $info = []; 188f4476bd9SJan Schumann $info['user'] = $user; 189d17fc5deSAndreas Gohr $this->debug('LDAP user to find: ' . hsc($info['user']), 0, __LINE__, __FILE__); 19040017f0bSItamar Shoham 19170e4a085SAndreas Gohr $info['server'] = $this->getConf('server'); 192d17fc5deSAndreas Gohr $this->debug('LDAP Server: ' . hsc($info['server']), 0, __LINE__, __FILE__); 193f4476bd9SJan Schumann 194f4476bd9SJan Schumann //get info for given user 195d17fc5deSAndreas Gohr $base = $this->makeFilter($this->getConf('usertree'), $info); 19670e4a085SAndreas Gohr if ($this->getConf('userfilter')) { 197d17fc5deSAndreas Gohr $filter = $this->makeFilter($this->getConf('userfilter'), $info); 198f4476bd9SJan Schumann } else { 199f4476bd9SJan Schumann $filter = "(ObjectClass=*)"; 200f4476bd9SJan Schumann } 201f4476bd9SJan Schumann 202d17fc5deSAndreas Gohr $this->debug('LDAP Filter: ' . hsc($filter), 0, __LINE__, __FILE__); 20340017f0bSItamar Shoham 204d17fc5deSAndreas Gohr $this->debug('LDAP user search: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); 205d17fc5deSAndreas Gohr $this->debug('LDAP search at: ' . hsc($base . ' ' . $filter), 0, __LINE__, __FILE__); 206c0c77cd2SAndreas Gohr $sr = $this->ldapSearch($this->con, $base, $filter, $this->getConf('userscope'), $this->getConf('attributes')); 20783616da3Shydrian if ($sr === false) { 20883616da3Shydrian $this->debug('User ldap_search failed. Check configuration.', 0, __LINE__, __FILE__); 2091c6fde61SBen Tyger return false; 21083616da3Shydrian } 21140017f0bSItamar Shoham 21240017f0bSItamar Shoham $result = @ldap_get_entries($this->con, $sr); 21340017f0bSItamar Shoham 21440017f0bSItamar Shoham // if result is not an array 21540017f0bSItamar Shoham if (!is_array($result)) { 21640017f0bSItamar Shoham // no objects found 217d17fc5deSAndreas Gohr $this->debug('LDAP search returned non-array result: ' . hsc(print($result)), -1, __LINE__, __FILE__); 218ab9790caSAndreas Gohr return []; 21940017f0bSItamar Shoham } 220f4476bd9SJan Schumann 221f4476bd9SJan Schumann // Don't accept more or less than one response 22240017f0bSItamar Shoham if ($result['count'] != 1) { 223d17fc5deSAndreas Gohr $this->debug( 22464159a61SAndreas Gohr 'LDAP search returned ' . hsc($result['count']) . ' results while it should return 1!', 225d17fc5deSAndreas Gohr -1, 226d17fc5deSAndreas Gohr __LINE__, 227d17fc5deSAndreas Gohr __FILE__ 22864159a61SAndreas Gohr ); 22940017f0bSItamar Shoham //for($i = 0; $i < $result["count"]; $i++) { 23064159a61SAndreas Gohr //$this->_debug('result: '.hsc(print_r($result[$i])), 0, __LINE__, __FILE__); 23140017f0bSItamar Shoham //} 232ab9790caSAndreas Gohr return []; 233f4476bd9SJan Schumann } 234f4476bd9SJan Schumann 235d17fc5deSAndreas Gohr $this->debug('LDAP search found single result !', 0, __LINE__, __FILE__); 23640017f0bSItamar Shoham 237f4476bd9SJan Schumann $user_result = $result[0]; 238f4476bd9SJan Schumann ldap_free_result($sr); 239f4476bd9SJan Schumann 240f4476bd9SJan Schumann // general user info 241f4476bd9SJan Schumann $info['dn'] = $user_result['dn']; 24282be9eabSGirish Ramakrishnan $info['gid'] = $user_result['gidnumber'][0] ?? null; 243f4476bd9SJan Schumann $info['mail'] = $user_result['mail'][0]; 244f4476bd9SJan Schumann $info['name'] = $user_result['cn'][0]; 245ab9790caSAndreas Gohr $info['grps'] = []; 246f4476bd9SJan Schumann 247f4476bd9SJan Schumann // overwrite if other attribs are specified. 24870e4a085SAndreas Gohr if (is_array($this->getConf('mapping'))) { 24970e4a085SAndreas Gohr foreach ($this->getConf('mapping') as $localkey => $key) { 250f4476bd9SJan Schumann if (is_array($key)) { 251f4476bd9SJan Schumann // use regexp to clean up user_result 2528f1011e8SPhy // $key = array($key=>$regexp), only handles the first key-value 2538f1011e8SPhy $regexp = current($key); 2548f1011e8SPhy $key = key($key); 2554f11d93dSGerrit Uitslag if ($user_result[$key]) foreach ($user_result[$key] as $grpkey => $grp) { 2564f11d93dSGerrit Uitslag if ($grpkey !== 'count' && preg_match($regexp, $grp, $match)) { 257f4476bd9SJan Schumann if ($localkey == 'grps') { 258f4476bd9SJan Schumann $info[$localkey][] = $match[1]; 259f4476bd9SJan Schumann } else { 260f4476bd9SJan Schumann $info[$localkey] = $match[1]; 261f4476bd9SJan Schumann } 262f4476bd9SJan Schumann } 263f4476bd9SJan Schumann } 264f4476bd9SJan Schumann } else { 265f4476bd9SJan Schumann $info[$localkey] = $user_result[$key][0]; 266f4476bd9SJan Schumann } 267f4476bd9SJan Schumann } 268f4476bd9SJan Schumann } 269f4476bd9SJan Schumann $user_result = array_merge($info, $user_result); 270f4476bd9SJan Schumann 271f4476bd9SJan Schumann //get groups for given user if grouptree is given 27270e4a085SAndreas Gohr if ($this->getConf('grouptree') || $this->getConf('groupfilter')) { 273d17fc5deSAndreas Gohr $base = $this->makeFilter($this->getConf('grouptree'), $user_result); 274d17fc5deSAndreas Gohr $filter = $this->makeFilter($this->getConf('groupfilter'), $user_result); 275d17fc5deSAndreas Gohr $sr = $this->ldapSearch( 27664159a61SAndreas Gohr $this->con, 27764159a61SAndreas Gohr $base, 27864159a61SAndreas Gohr $filter, 27964159a61SAndreas Gohr $this->getConf('groupscope'), 280ab9790caSAndreas Gohr [$this->getConf('groupkey')] 281d17fc5deSAndreas Gohr ); 282d17fc5deSAndreas Gohr $this->debug('LDAP group search: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); 283d17fc5deSAndreas Gohr $this->debug('LDAP search at: ' . hsc($base . ' ' . $filter), 0, __LINE__, __FILE__); 28470e4a085SAndreas Gohr 285f4476bd9SJan Schumann if (!$sr) { 286f4476bd9SJan Schumann msg("LDAP: Reading group memberships failed", -1); 287ab9790caSAndreas Gohr return []; 288f4476bd9SJan Schumann } 289f4476bd9SJan Schumann $result = ldap_get_entries($this->con, $sr); 290f4476bd9SJan Schumann ldap_free_result($sr); 291f4476bd9SJan Schumann 292f4476bd9SJan Schumann if (is_array($result)) foreach ($result as $grp) { 2939f72d639SAndreas Gohr if (!empty($grp[$this->getConf('groupkey')])) { 2949f72d639SAndreas Gohr $group = $grp[$this->getConf('groupkey')]; 2959f72d639SAndreas Gohr if (is_array($group)) { 2969f72d639SAndreas Gohr $group = $group[0]; 2979f72d639SAndreas Gohr } else { 298d17fc5deSAndreas Gohr $this->debug('groupkey did not return a detailled result', 0, __LINE__, __FILE__); 2999f72d639SAndreas Gohr } 3009f72d639SAndreas Gohr if ($group === '') continue; 3019f72d639SAndreas Gohr 302d17fc5deSAndreas Gohr $this->debug('LDAP usergroup: ' . hsc($group), 0, __LINE__, __FILE__); 3039f72d639SAndreas Gohr $info['grps'][] = $group; 304f4476bd9SJan Schumann } 305f4476bd9SJan Schumann } 306f4476bd9SJan Schumann } 307f4476bd9SJan Schumann 308f4476bd9SJan Schumann // always add the default group to the list of groups 309ab9790caSAndreas Gohr if (!$info['grps'] || !in_array($conf['defaultgroup'], $info['grps'])) { 310f4476bd9SJan Schumann $info['grps'][] = $conf['defaultgroup']; 311f4476bd9SJan Schumann } 312f4476bd9SJan Schumann return $info; 313f4476bd9SJan Schumann } 314f4476bd9SJan Schumann 315f4476bd9SJan Schumann /** 31606da270eSAxel Angel * Definition of the function modifyUser in order to modify the password 317b83b4364SGerrit Uitslag * 318b83b4364SGerrit Uitslag * @param string $user nick of the user to be changed 319b83b4364SGerrit Uitslag * @param array $changes array of field/value pairs to be changed (password will be clear text) 320b83b4364SGerrit Uitslag * @return bool true on success, false on error 32106da270eSAxel Angel */ 322d17fc5deSAndreas Gohr public function modifyUser($user, $changes) 323d17fc5deSAndreas Gohr { 32406da270eSAxel Angel 32506da270eSAxel Angel // open the connection to the ldap 326d17fc5deSAndreas Gohr if (!$this->openLDAP()) { 327d17fc5deSAndreas Gohr $this->debug('LDAP cannot connect: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); 32806da270eSAxel Angel return false; 32906da270eSAxel Angel } 33006da270eSAxel Angel 33106da270eSAxel Angel // find the information about the user, in particular the "dn" 33206da270eSAxel Angel $info = $this->getUserData($user, true); 33306da270eSAxel Angel if (empty($info['dn'])) { 334d17fc5deSAndreas Gohr $this->debug('LDAP cannot find your user dn', 0, __LINE__, __FILE__); 33506da270eSAxel Angel return false; 3368f2ea93bSAxel Angel } 33706da270eSAxel Angel $dn = $info['dn']; 33806da270eSAxel Angel 33906da270eSAxel Angel // find the old password of the user 340ab9790caSAndreas Gohr [$loginuser, $loginsticky, $loginpass] = auth_getCookie(); 341719c6730SAxel Angel if ($loginuser !== null) { // the user is currently logged in 34218496fe0SAndreas Gohr $secret = auth_cookiesalt(!$loginsticky, true); 34306da270eSAxel Angel $pass = auth_decrypt($loginpass, $secret); 34406da270eSAxel Angel 34506da270eSAxel Angel // bind with the ldap 34606da270eSAxel Angel if (!@ldap_bind($this->con, $dn, $pass)) { 347d17fc5deSAndreas Gohr $this->debug( 348d17fc5deSAndreas Gohr 'LDAP user bind failed: ' . hsc($dn) . ': ' . hsc(ldap_error($this->con)), 349d17fc5deSAndreas Gohr 0, 350d17fc5deSAndreas Gohr __LINE__, 351d17fc5deSAndreas Gohr __FILE__ 35264159a61SAndreas Gohr ); 35306da270eSAxel Angel return false; 35406da270eSAxel Angel } 355719c6730SAxel Angel } elseif ($this->getConf('binddn') && $this->getConf('bindpw')) { 356719c6730SAxel Angel // we are changing the password on behalf of the user (eg: forgotten password) 357719c6730SAxel Angel // bind with the superuser ldap 3588ef94e9eSAndreas Gohr if (!@ldap_bind($this->con, $this->getConf('binddn'), conf_decodeString($this->getConf('bindpw')))) { 359d17fc5deSAndreas Gohr $this->debug('LDAP bind as superuser: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); 360719c6730SAxel Angel return false; 361719c6730SAxel Angel } 362d17fc5deSAndreas Gohr } else { 363719c6730SAxel Angel return false; // no otherway 364719c6730SAxel Angel } 36506da270eSAxel Angel 36667723447SAxel Angel // Generate the salted hashed password for LDAP 367ab9790caSAndreas Gohr $phash = new PassHash(); 36867723447SAxel Angel $hash = $phash->hash_ssha($changes['pass']); 36967723447SAxel Angel 37006da270eSAxel Angel // change the password 371ab9790caSAndreas Gohr if (!@ldap_mod_replace($this->con, $dn, ['userpassword' => $hash])) { 372d17fc5deSAndreas Gohr $this->debug( 373d17fc5deSAndreas Gohr 'LDAP mod replace failed: ' . hsc($dn) . ': ' . hsc(ldap_error($this->con)), 374d17fc5deSAndreas Gohr 0, 375d17fc5deSAndreas Gohr __LINE__, 376d17fc5deSAndreas Gohr __FILE__ 37764159a61SAndreas Gohr ); 37806da270eSAxel Angel return false; 37906da270eSAxel Angel } 38006da270eSAxel Angel 38106da270eSAxel Angel return true; 38206da270eSAxel Angel } 38306da270eSAxel Angel 38406da270eSAxel Angel /** 385f4476bd9SJan Schumann * Most values in LDAP are case-insensitive 38670e4a085SAndreas Gohr * 38770e4a085SAndreas Gohr * @return bool 388f4476bd9SJan Schumann */ 389d17fc5deSAndreas Gohr public function isCaseSensitive() 390d17fc5deSAndreas Gohr { 391f4476bd9SJan Schumann return false; 392f4476bd9SJan Schumann } 393f4476bd9SJan Schumann 394f4476bd9SJan Schumann /** 395f4476bd9SJan Schumann * Bulk retrieval of user data 396f4476bd9SJan Schumann * 39770e4a085SAndreas Gohr * @param int $start index of first user to be returned 39870e4a085SAndreas Gohr * @param int $limit max number of users to be returned 39970e4a085SAndreas Gohr * @param array $filter array of field/pattern pairs, null for no filter 400f4476bd9SJan Schumann * @return array of userinfo (refer getUserData for internal userinfo details) 401c0c77cd2SAndreas Gohr * @author Dominik Eckelmann <dokuwiki@cosmocode.de> 402f4476bd9SJan Schumann */ 403ab9790caSAndreas Gohr public function retrieveUsers($start = 0, $limit = 0, $filter = []) 404d17fc5deSAndreas Gohr { 405ab9790caSAndreas Gohr if (!$this->openLDAP()) return []; 406f4476bd9SJan Schumann 40770e4a085SAndreas Gohr if (is_null($this->users)) { 408f4476bd9SJan Schumann // Perform the search and grab all their details 40970e4a085SAndreas Gohr if ($this->getConf('userfilter')) { 41070e4a085SAndreas Gohr $all_filter = str_replace('%{user}', '*', $this->getConf('userfilter')); 411f4476bd9SJan Schumann } else { 412f4476bd9SJan Schumann $all_filter = "(ObjectClass=*)"; 413f4476bd9SJan Schumann } 41470e4a085SAndreas Gohr $sr = ldap_search($this->con, $this->getConf('usertree'), $all_filter); 415f4476bd9SJan Schumann $entries = ldap_get_entries($this->con, $sr); 416ab9790caSAndreas Gohr $users_array = []; 4176619ddf4SSascha Klopp $userkey = $this->getConf('userkey'); 418f4476bd9SJan Schumann for ($i = 0; $i < $entries["count"]; $i++) { 419ab9790caSAndreas Gohr $users_array[] = $entries[$i][$userkey][0]; 420f4476bd9SJan Schumann } 4210489c64bSMoisés Braga Ribeiro Sort::asort($users_array); 422f4476bd9SJan Schumann $result = $users_array; 423ab9790caSAndreas Gohr if (!$result) return []; 424f4476bd9SJan Schumann $this->users = array_fill_keys($result, false); 425f4476bd9SJan Schumann } 426f4476bd9SJan Schumann $i = 0; 427f4476bd9SJan Schumann $count = 0; 428d17fc5deSAndreas Gohr $this->constructPattern($filter); 429ab9790caSAndreas Gohr $result = []; 430f4476bd9SJan Schumann 431f4476bd9SJan Schumann foreach ($this->users as $user => &$info) { 432f4476bd9SJan Schumann if ($i++ < $start) { 433f4476bd9SJan Schumann continue; 434f4476bd9SJan Schumann } 435f4476bd9SJan Schumann if ($info === false) { 436f4476bd9SJan Schumann $info = $this->getUserData($user); 437f4476bd9SJan Schumann } 438d17fc5deSAndreas Gohr if ($this->filter($user, $info)) { 439f4476bd9SJan Schumann $result[$user] = $info; 4409a2c73e8SAndreas Gohr if (($limit > 0) && (++$count >= $limit)) break; 441f4476bd9SJan Schumann } 442f4476bd9SJan Schumann } 443f4476bd9SJan Schumann return $result; 444f4476bd9SJan Schumann } 445f4476bd9SJan Schumann 446f4476bd9SJan Schumann /** 447f4476bd9SJan Schumann * Make LDAP filter strings. 448f4476bd9SJan Schumann * 449f4476bd9SJan Schumann * Used by auth_getUserData to make the filter 450f4476bd9SJan Schumann * strings for grouptree and groupfilter 451f4476bd9SJan Schumann * 45270e4a085SAndreas Gohr * @param string $filter ldap search filter with placeholders 45370e4a085SAndreas Gohr * @param array $placeholders placeholders to fill in 454f4476bd9SJan Schumann * @return string 455c0c77cd2SAndreas Gohr * @author Troels Liebe Bentsen <tlb@rapanden.dk> 456f4476bd9SJan Schumann */ 457d17fc5deSAndreas Gohr protected function makeFilter($filter, $placeholders) 458d17fc5deSAndreas Gohr { 459f4476bd9SJan Schumann preg_match_all("/%{([^}]+)/", $filter, $matches, PREG_PATTERN_ORDER); 460f4476bd9SJan Schumann //replace each match 461f4476bd9SJan Schumann foreach ($matches[1] as $match) { 462f4476bd9SJan Schumann //take first element if array 463f4476bd9SJan Schumann if (is_array($placeholders[$match])) { 464f4476bd9SJan Schumann $value = $placeholders[$match][0]; 465f4476bd9SJan Schumann } else { 466f4476bd9SJan Schumann $value = $placeholders[$match]; 467f4476bd9SJan Schumann } 468d17fc5deSAndreas Gohr $value = $this->filterEscape($value); 469f4476bd9SJan Schumann $filter = str_replace('%{' . $match . '}', $value, $filter); 470f4476bd9SJan Schumann } 471f4476bd9SJan Schumann return $filter; 472f4476bd9SJan Schumann } 473f4476bd9SJan Schumann 474f4476bd9SJan Schumann /** 47570e4a085SAndreas Gohr * return true if $user + $info match $filter criteria, false otherwise 476f4476bd9SJan Schumann * 47770e4a085SAndreas Gohr * @param string $user the user's login name 47870e4a085SAndreas Gohr * @param array $info the user's userinfo array 47970e4a085SAndreas Gohr * @return bool 480c0c77cd2SAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 481c0c77cd2SAndreas Gohr * 482f4476bd9SJan Schumann */ 483d17fc5deSAndreas Gohr protected function filter($user, $info) 484d17fc5deSAndreas Gohr { 485d17fc5deSAndreas Gohr foreach ($this->pattern as $item => $pattern) { 486f4476bd9SJan Schumann if ($item == 'user') { 48770e4a085SAndreas Gohr if (!preg_match($pattern, $user)) return false; 488f4476bd9SJan Schumann } elseif ($item == 'grps') { 48970e4a085SAndreas Gohr if (!count(preg_grep($pattern, $info['grps']))) return false; 490ab9790caSAndreas Gohr } elseif (!preg_match($pattern, $info[$item])) { 491ab9790caSAndreas Gohr return false; 492f4476bd9SJan Schumann } 493f4476bd9SJan Schumann } 49470e4a085SAndreas Gohr return true; 495f4476bd9SJan Schumann } 496f4476bd9SJan Schumann 49770e4a085SAndreas Gohr /** 49870e4a085SAndreas Gohr * Set the filter pattern 49970e4a085SAndreas Gohr * 50070e4a085SAndreas Gohr * @param $filter 50170e4a085SAndreas Gohr * @return void 502c0c77cd2SAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 503c0c77cd2SAndreas Gohr * 50470e4a085SAndreas Gohr */ 505d17fc5deSAndreas Gohr protected function constructPattern($filter) 506d17fc5deSAndreas Gohr { 507ab9790caSAndreas Gohr $this->pattern = []; 508f4476bd9SJan Schumann foreach ($filter as $item => $pattern) { 509d17fc5deSAndreas Gohr $this->pattern[$item] = '/' . str_replace('/', '\/', $pattern) . '/i'; // allow regex characters 510f4476bd9SJan Schumann } 511f4476bd9SJan Schumann } 512f4476bd9SJan Schumann 513f4476bd9SJan Schumann /** 514f4476bd9SJan Schumann * Escape a string to be used in a LDAP filter 515f4476bd9SJan Schumann * 516f4476bd9SJan Schumann * Ported from Perl's Net::LDAP::Util escape_filter_value 517f4476bd9SJan Schumann * 51870e4a085SAndreas Gohr * @param string $string 51970e4a085SAndreas Gohr * @return string 520c0c77cd2SAndreas Gohr * @author Andreas Gohr 521f4476bd9SJan Schumann */ 522d17fc5deSAndreas Gohr protected function filterEscape($string) 523d17fc5deSAndreas Gohr { 524234b5c9aSBernhard Liebl // see https://github.com/adldap/adLDAP/issues/22 525234b5c9aSBernhard Liebl return preg_replace_callback( 526234b5c9aSBernhard Liebl '/([\x00-\x1F\*\(\)\\\\])/', 527ab9790caSAndreas Gohr static fn($matches) => "\\" . implode("", unpack("H2", $matches[1])), 52870e4a085SAndreas Gohr $string 52970e4a085SAndreas Gohr ); 530f4476bd9SJan Schumann } 531f4476bd9SJan Schumann 532f4476bd9SJan Schumann /** 533f4476bd9SJan Schumann * Opens a connection to the configured LDAP server and sets the wanted 534f4476bd9SJan Schumann * option on the connection 535f4476bd9SJan Schumann * 536f4476bd9SJan Schumann * @author Andreas Gohr <andi@splitbrain.org> 537f4476bd9SJan Schumann */ 538d17fc5deSAndreas Gohr protected function openLDAP() 539d17fc5deSAndreas Gohr { 540f4476bd9SJan Schumann if ($this->con) return true; // connection already established 541f4476bd9SJan Schumann 542234b5c9aSBernhard Liebl if ($this->getConf('debug')) { 543d17fc5deSAndreas Gohr ldap_set_option(null, LDAP_OPT_DEBUG_LEVEL, 7); 544234b5c9aSBernhard Liebl } 545234b5c9aSBernhard Liebl 546f4476bd9SJan Schumann $this->bound = 0; 547f4476bd9SJan Schumann 54870e4a085SAndreas Gohr $port = $this->getConf('port'); 54993a7873eSAndreas Gohr $bound = false; 55070e4a085SAndreas Gohr $servers = explode(',', $this->getConf('server')); 55193a7873eSAndreas Gohr foreach ($servers as $server) { 55293a7873eSAndreas Gohr $server = trim($server); 55393a7873eSAndreas Gohr $this->con = @ldap_connect($server, $port); 554f4476bd9SJan Schumann if (!$this->con) { 55593a7873eSAndreas Gohr continue; 556f4476bd9SJan Schumann } 557f4476bd9SJan Schumann 55893a7873eSAndreas Gohr /* 55993a7873eSAndreas Gohr * When OpenLDAP 2.x.x is used, ldap_connect() will always return a resource as it does 56093a7873eSAndreas Gohr * not actually connect but just initializes the connecting parameters. The actual 56193a7873eSAndreas Gohr * connect happens with the next calls to ldap_* funcs, usually with ldap_bind(). 56293a7873eSAndreas Gohr * 56393a7873eSAndreas Gohr * So we should try to bind to server in order to check its availability. 56493a7873eSAndreas Gohr */ 56593a7873eSAndreas Gohr 566f4476bd9SJan Schumann //set protocol version and dependend options 56770e4a085SAndreas Gohr if ($this->getConf('version')) { 568*7d34963bSAndreas Gohr if ( 569*7d34963bSAndreas Gohr !@ldap_set_option( 570d17fc5deSAndreas Gohr $this->con, 571d17fc5deSAndreas Gohr LDAP_OPT_PROTOCOL_VERSION, 57270e4a085SAndreas Gohr $this->getConf('version') 57370e4a085SAndreas Gohr ) 57470e4a085SAndreas Gohr ) { 57570e4a085SAndreas Gohr msg('Setting LDAP Protocol version ' . $this->getConf('version') . ' failed', -1); 576d17fc5deSAndreas Gohr $this->debug('LDAP version set: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); 577f4476bd9SJan Schumann } else { 578f4476bd9SJan Schumann //use TLS (needs version 3) 57970e4a085SAndreas Gohr if ($this->getConf('starttls')) { 580f4476bd9SJan Schumann if (!@ldap_start_tls($this->con)) { 581f4476bd9SJan Schumann msg('Starting TLS failed', -1); 582d17fc5deSAndreas Gohr $this->debug('LDAP TLS set: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); 583f4476bd9SJan Schumann } 584f4476bd9SJan Schumann } 585f4476bd9SJan Schumann // needs version 3 586d75d76b2SAndreas Gohr if ($this->getConf('referrals') > -1) { 587*7d34963bSAndreas Gohr if ( 588*7d34963bSAndreas Gohr !@ldap_set_option( 589d17fc5deSAndreas Gohr $this->con, 590d17fc5deSAndreas Gohr LDAP_OPT_REFERRALS, 59170e4a085SAndreas Gohr $this->getConf('referrals') 59270e4a085SAndreas Gohr ) 59370e4a085SAndreas Gohr ) { 594d75d76b2SAndreas Gohr msg('Setting LDAP referrals failed', -1); 595d17fc5deSAndreas Gohr $this->debug('LDAP referal set: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); 596f4476bd9SJan Schumann } 597f4476bd9SJan Schumann } 598f4476bd9SJan Schumann } 599f4476bd9SJan Schumann } 600f4476bd9SJan Schumann 601f4476bd9SJan Schumann //set deref mode 60270e4a085SAndreas Gohr if ($this->getConf('deref')) { 60370e4a085SAndreas Gohr if (!@ldap_set_option($this->con, LDAP_OPT_DEREF, $this->getConf('deref'))) { 60470e4a085SAndreas Gohr msg('Setting LDAP Deref mode ' . $this->getConf('deref') . ' failed', -1); 605d17fc5deSAndreas Gohr $this->debug('LDAP deref set: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); 606f4476bd9SJan Schumann } 607f4476bd9SJan Schumann } 60893a7873eSAndreas Gohr /* As of PHP 5.3.0 we can set timeout to speedup skipping of invalid servers */ 60993a7873eSAndreas Gohr if (defined('LDAP_OPT_NETWORK_TIMEOUT')) { 61093a7873eSAndreas Gohr ldap_set_option($this->con, LDAP_OPT_NETWORK_TIMEOUT, 1); 61193a7873eSAndreas Gohr } 612a426a6cdSAndreas Gohr 613a426a6cdSAndreas Gohr if ($this->getConf('binddn') && $this->getConf('bindpw')) { 6148ef94e9eSAndreas Gohr $bound = @ldap_bind($this->con, $this->getConf('binddn'), conf_decodeString($this->getConf('bindpw'))); 615a426a6cdSAndreas Gohr $this->bound = 2; 616a426a6cdSAndreas Gohr } else { 61793a7873eSAndreas Gohr $bound = @ldap_bind($this->con); 618a426a6cdSAndreas Gohr } 61993a7873eSAndreas Gohr if ($bound) { 62093a7873eSAndreas Gohr break; 62193a7873eSAndreas Gohr } 62293a7873eSAndreas Gohr } 62393a7873eSAndreas Gohr 62493a7873eSAndreas Gohr if (!$bound) { 62593a7873eSAndreas Gohr msg("LDAP: couldn't connect to LDAP server", -1); 626d17fc5deSAndreas Gohr $this->debug(ldap_error($this->con), 0, __LINE__, __FILE__); 62793a7873eSAndreas Gohr return false; 62893a7873eSAndreas Gohr } 62993a7873eSAndreas Gohr 63070e4a085SAndreas Gohr $this->cando['getUsers'] = true; 631f4476bd9SJan Schumann return true; 632f4476bd9SJan Schumann } 633f4476bd9SJan Schumann 634f4476bd9SJan Schumann /** 635f4476bd9SJan Schumann * Wraps around ldap_search, ldap_list or ldap_read depending on $scope 636f4476bd9SJan Schumann * 63770e4a085SAndreas Gohr * @param resource $link_identifier 63870e4a085SAndreas Gohr * @param string $base_dn 63970e4a085SAndreas Gohr * @param string $filter 64070e4a085SAndreas Gohr * @param string $scope can be 'base', 'one' or 'sub' 641e0c26282SGerrit Uitslag * @param null|array $attributes 64270e4a085SAndreas Gohr * @param int $attrsonly 64370e4a085SAndreas Gohr * @param int $sizelimit 64470e4a085SAndreas Gohr * @return resource 645c0c77cd2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 646f4476bd9SJan Schumann */ 647d17fc5deSAndreas Gohr protected function ldapSearch( 648d17fc5deSAndreas Gohr $link_identifier, 649d17fc5deSAndreas Gohr $base_dn, 650d17fc5deSAndreas Gohr $filter, 651d17fc5deSAndreas Gohr $scope = 'sub', 652d17fc5deSAndreas Gohr $attributes = null, 653d17fc5deSAndreas Gohr $attrsonly = 0, 654d17fc5deSAndreas Gohr $sizelimit = 0 655d868eb89SAndreas Gohr ) { 656ab9790caSAndreas Gohr if (is_null($attributes)) $attributes = []; 657f4476bd9SJan Schumann 658f4476bd9SJan Schumann if ($scope == 'base') { 65970e4a085SAndreas Gohr return @ldap_read( 660d17fc5deSAndreas Gohr $link_identifier, 661d17fc5deSAndreas Gohr $base_dn, 662d17fc5deSAndreas Gohr $filter, 663d17fc5deSAndreas Gohr $attributes, 664d17fc5deSAndreas Gohr $attrsonly, 665d17fc5deSAndreas Gohr $sizelimit 66670e4a085SAndreas Gohr ); 667f4476bd9SJan Schumann } elseif ($scope == 'one') { 66870e4a085SAndreas Gohr return @ldap_list( 669d17fc5deSAndreas Gohr $link_identifier, 670d17fc5deSAndreas Gohr $base_dn, 671d17fc5deSAndreas Gohr $filter, 672d17fc5deSAndreas Gohr $attributes, 673d17fc5deSAndreas Gohr $attrsonly, 674d17fc5deSAndreas Gohr $sizelimit 67570e4a085SAndreas Gohr ); 676f4476bd9SJan Schumann } else { 67770e4a085SAndreas Gohr return @ldap_search( 678d17fc5deSAndreas Gohr $link_identifier, 679d17fc5deSAndreas Gohr $base_dn, 680d17fc5deSAndreas Gohr $filter, 681d17fc5deSAndreas Gohr $attributes, 682d17fc5deSAndreas Gohr $attrsonly, 683d17fc5deSAndreas Gohr $sizelimit 68470e4a085SAndreas Gohr ); 685f4476bd9SJan Schumann } 686f4476bd9SJan Schumann } 68770e4a085SAndreas Gohr 68870e4a085SAndreas Gohr /** 68970e4a085SAndreas Gohr * Wrapper around msg() but outputs only when debug is enabled 69070e4a085SAndreas Gohr * 69170e4a085SAndreas Gohr * @param string $message 69270e4a085SAndreas Gohr * @param int $err 69370e4a085SAndreas Gohr * @param int $line 69470e4a085SAndreas Gohr * @param string $file 69570e4a085SAndreas Gohr * @return void 69670e4a085SAndreas Gohr */ 697d17fc5deSAndreas Gohr protected function debug($message, $err, $line, $file) 698d17fc5deSAndreas Gohr { 69970e4a085SAndreas Gohr if (!$this->getConf('debug')) return; 70070e4a085SAndreas Gohr msg($message, $err, $line, $file); 70170e4a085SAndreas Gohr } 702f4476bd9SJan Schumann} 703