1f4476bd9SJan Schumann<?php 2f4476bd9SJan Schumann// must be run within Dokuwiki 3f4476bd9SJan Schumannif(!defined('DOKU_INC')) die(); 4f4476bd9SJan Schumann 5f4476bd9SJan Schumann/** 6f4476bd9SJan Schumann * LDAP authentication backend 7f4476bd9SJan Schumann * 8f4476bd9SJan Schumann * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 9f4476bd9SJan Schumann * @author Andreas Gohr <andi@splitbrain.org> 10f4476bd9SJan Schumann * @author Chris Smith <chris@jalakaic.co.uk> 11f4476bd9SJan Schumann * @author Jan Schumann <js@schumann-it.com> 12f4476bd9SJan Schumann */ 1393a7873eSAndreas Gohrclass auth_plugin_authldap extends DokuWiki_Auth_Plugin { 1470e4a085SAndreas Gohr /* @var resource $con holds the LDAP connection*/ 1570e4a085SAndreas Gohr protected $con = null; 1670e4a085SAndreas Gohr 1770e4a085SAndreas Gohr /* @var int $bound What type of connection does already exist? */ 1870e4a085SAndreas Gohr protected $bound = 0; // 0: anonymous, 1: user, 2: superuser 1970e4a085SAndreas Gohr 2070e4a085SAndreas Gohr /* @var array $users User data cache */ 2170e4a085SAndreas Gohr protected $users = null; 2270e4a085SAndreas Gohr 2370e4a085SAndreas Gohr /* @var array $_pattern User filter pattern */ 2470e4a085SAndreas Gohr protected $_pattern = null; 25f4476bd9SJan Schumann 26f4476bd9SJan Schumann /** 27f4476bd9SJan Schumann * Constructor 28f4476bd9SJan Schumann */ 2970e4a085SAndreas Gohr public function __construct() { 30454d868bSAndreas Gohr parent::__construct(); 31454d868bSAndreas Gohr 32f4476bd9SJan Schumann // ldap extension is needed 33f4476bd9SJan Schumann if(!function_exists('ldap_connect')) { 3470e4a085SAndreas Gohr $this->_debug("LDAP err: PHP LDAP extension not found.", -1, __LINE__, __FILE__); 35f4476bd9SJan Schumann $this->success = false; 36f4476bd9SJan Schumann return; 37f4476bd9SJan Schumann } 38f4476bd9SJan Schumann 3906da270eSAxel Angel // Add the capabilities to change the password 406619ddf4SSascha Klopp $this->cando['modPass'] = $this->getConf('modPass'); 41f4476bd9SJan Schumann } 42f4476bd9SJan Schumann 43f4476bd9SJan Schumann /** 44f4476bd9SJan Schumann * Check user+password 45f4476bd9SJan Schumann * 46f4476bd9SJan Schumann * Checks if the given user exists and the given 47f4476bd9SJan Schumann * plaintext password is correct by trying to bind 48f4476bd9SJan Schumann * to the LDAP server 49f4476bd9SJan Schumann * 50f4476bd9SJan Schumann * @author Andreas Gohr <andi@splitbrain.org> 5170e4a085SAndreas Gohr * @param string $user 5270e4a085SAndreas Gohr * @param string $pass 53f4476bd9SJan Schumann * @return bool 54f4476bd9SJan Schumann */ 5570e4a085SAndreas Gohr public function checkPass($user, $pass) { 56f4476bd9SJan Schumann // reject empty password 57f4476bd9SJan Schumann if(empty($pass)) return false; 58f4476bd9SJan Schumann if(!$this->_openLDAP()) return false; 59f4476bd9SJan Schumann 60f4476bd9SJan Schumann // indirect user bind 6170e4a085SAndreas Gohr if($this->getConf('binddn') && $this->getConf('bindpw')) { 62f4476bd9SJan Schumann // use superuser credentials 638ef94e9eSAndreas Gohr if(!@ldap_bind($this->con, $this->getConf('binddn'), conf_decodeString($this->getConf('bindpw')))) { 6470e4a085SAndreas Gohr $this->_debug('LDAP bind as superuser: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 65f4476bd9SJan Schumann return false; 66f4476bd9SJan Schumann } 67f4476bd9SJan Schumann $this->bound = 2; 6870e4a085SAndreas Gohr } else if($this->getConf('binddn') && 6970e4a085SAndreas Gohr $this->getConf('usertree') && 7070e4a085SAndreas Gohr $this->getConf('userfilter') 7170e4a085SAndreas Gohr ) { 72f4476bd9SJan Schumann // special bind string 7370e4a085SAndreas Gohr $dn = $this->_makeFilter( 7470e4a085SAndreas Gohr $this->getConf('binddn'), 7570e4a085SAndreas Gohr array('user'=> $user, 'server'=> $this->getConf('server')) 7670e4a085SAndreas Gohr ); 77f4476bd9SJan Schumann 7870e4a085SAndreas Gohr } else if(strpos($this->getConf('usertree'), '%{user}')) { 79f4476bd9SJan Schumann // direct user bind 8070e4a085SAndreas Gohr $dn = $this->_makeFilter( 8170e4a085SAndreas Gohr $this->getConf('usertree'), 8270e4a085SAndreas Gohr array('user'=> $user, 'server'=> $this->getConf('server')) 8370e4a085SAndreas Gohr ); 84f4476bd9SJan Schumann 85f4476bd9SJan Schumann } else { 86f4476bd9SJan Schumann // Anonymous bind 87f4476bd9SJan Schumann if(!@ldap_bind($this->con)) { 88f4476bd9SJan Schumann msg("LDAP: can not bind anonymously", -1); 8970e4a085SAndreas Gohr $this->_debug('LDAP anonymous bind: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 90f4476bd9SJan Schumann return false; 91f4476bd9SJan Schumann } 92f4476bd9SJan Schumann } 93f4476bd9SJan Schumann 94f4476bd9SJan Schumann // Try to bind to with the dn if we have one. 95f4476bd9SJan Schumann if(!empty($dn)) { 96f4476bd9SJan Schumann // User/Password bind 97f4476bd9SJan Schumann if(!@ldap_bind($this->con, $dn, $pass)) { 9870e4a085SAndreas Gohr $this->_debug("LDAP: bind with $dn failed", -1, __LINE__, __FILE__); 9970e4a085SAndreas Gohr $this->_debug('LDAP user dn bind: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 100f4476bd9SJan Schumann return false; 101f4476bd9SJan Schumann } 102f4476bd9SJan Schumann $this->bound = 1; 103f4476bd9SJan Schumann return true; 104f4476bd9SJan Schumann } else { 105f4476bd9SJan Schumann // See if we can find the user 106d397e6daSChristopher Smith $info = $this->_getUserData($user, true); 107f4476bd9SJan Schumann if(empty($info['dn'])) { 108f4476bd9SJan Schumann return false; 109f4476bd9SJan Schumann } else { 110f4476bd9SJan Schumann $dn = $info['dn']; 111f4476bd9SJan Schumann } 112f4476bd9SJan Schumann 113f4476bd9SJan Schumann // Try to bind with the dn provided 114f4476bd9SJan Schumann if(!@ldap_bind($this->con, $dn, $pass)) { 11570e4a085SAndreas Gohr $this->_debug("LDAP: bind with $dn failed", -1, __LINE__, __FILE__); 11670e4a085SAndreas Gohr $this->_debug('LDAP user bind: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 117f4476bd9SJan Schumann return false; 118f4476bd9SJan Schumann } 119f4476bd9SJan Schumann $this->bound = 1; 120f4476bd9SJan Schumann return true; 121f4476bd9SJan Schumann } 122f4476bd9SJan Schumann } 123f4476bd9SJan Schumann 124f4476bd9SJan Schumann /** 125f4476bd9SJan Schumann * Return user info 126f4476bd9SJan Schumann * 127f4476bd9SJan Schumann * Returns info about the given user needs to contain 128f4476bd9SJan Schumann * at least these fields: 129f4476bd9SJan Schumann * 130f4476bd9SJan Schumann * name string full name of the user 131f4476bd9SJan Schumann * mail string email addres of the user 132f4476bd9SJan Schumann * grps array list of groups the user is in 133f4476bd9SJan Schumann * 134f4476bd9SJan Schumann * This LDAP specific function returns the following 135f4476bd9SJan Schumann * addional fields: 136f4476bd9SJan Schumann * 137f4476bd9SJan Schumann * dn string distinguished name (DN) 138f4476bd9SJan Schumann * uid string Posix User ID 139f4476bd9SJan Schumann * inbind bool for internal use - avoid loop in binding 140f4476bd9SJan Schumann * 141f4476bd9SJan Schumann * @author Andreas Gohr <andi@splitbrain.org> 142f4476bd9SJan Schumann * @author Trouble 143f4476bd9SJan Schumann * @author Dan Allen <dan.j.allen@gmail.com> 144f4476bd9SJan Schumann * @author <evaldas.auryla@pheur.org> 145f4476bd9SJan Schumann * @author Stephane Chazelas <stephane.chazelas@emerson.com> 1463e23f03eSSteScho * @author Steffen Schoch <schoch@dsb.net> 14770e4a085SAndreas Gohr * 14870e4a085SAndreas Gohr * @param string $user 1492046a654SChristopher Smith * @param bool $requireGroups (optional) - ignored, groups are always supplied by this plugin 150d397e6daSChristopher Smith * @return array containing user data or false 151d397e6daSChristopher Smith */ 1522046a654SChristopher Smith public function getUserData($user, $requireGroups=true) { 153d397e6daSChristopher Smith return $this->_getUserData($user); 154d397e6daSChristopher Smith } 155d397e6daSChristopher Smith 156d397e6daSChristopher Smith /** 157d397e6daSChristopher Smith * @param string $user 15870e4a085SAndreas Gohr * @param bool $inbind authldap specific, true if in bind phase 159f4476bd9SJan Schumann * @return array containing user data or false 160f4476bd9SJan Schumann */ 161d397e6daSChristopher Smith protected function _getUserData($user, $inbind = false) { 162f4476bd9SJan Schumann global $conf; 163f4476bd9SJan Schumann if(!$this->_openLDAP()) return false; 164f4476bd9SJan Schumann 165f4476bd9SJan Schumann // force superuser bind if wanted and not bound as superuser yet 16670e4a085SAndreas Gohr if($this->getConf('binddn') && $this->getConf('bindpw') && $this->bound < 2) { 167f4476bd9SJan Schumann // use superuser credentials 1688ef94e9eSAndreas Gohr if(!@ldap_bind($this->con, $this->getConf('binddn'), conf_decodeString($this->getConf('bindpw')))) { 16970e4a085SAndreas Gohr $this->_debug('LDAP bind as superuser: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 170f4476bd9SJan Schumann return false; 171f4476bd9SJan Schumann } 172f4476bd9SJan Schumann $this->bound = 2; 173f4476bd9SJan Schumann } elseif($this->bound == 0 && !$inbind) { 174f4476bd9SJan Schumann // in some cases getUserData is called outside the authentication workflow 175f4476bd9SJan Schumann // eg. for sending email notification on subscribed pages. This data might not 176f4476bd9SJan Schumann // be accessible anonymously, so we try to rebind the current user here 177f4476bd9SJan Schumann list($loginuser, $loginsticky, $loginpass) = auth_getCookie(); 178f4476bd9SJan Schumann if($loginuser && $loginpass) { 179b4304655SMichael Hamann $loginpass = auth_decrypt($loginpass, auth_cookiesalt(!$loginsticky, true)); 180f4476bd9SJan Schumann $this->checkPass($loginuser, $loginpass); 181f4476bd9SJan Schumann } 182f4476bd9SJan Schumann } 183f4476bd9SJan Schumann 18459bc3b48SGerrit Uitslag $info = array(); 185f4476bd9SJan Schumann $info['user'] = $user; 18640017f0bSItamar Shoham $this->_debug('LDAP user to find: '.htmlspecialchars($info['user']), 0, __LINE__, __FILE__); 18740017f0bSItamar Shoham 18870e4a085SAndreas Gohr $info['server'] = $this->getConf('server'); 18940017f0bSItamar Shoham $this->_debug('LDAP Server: '.htmlspecialchars($info['server']), 0, __LINE__, __FILE__); 19040017f0bSItamar Shoham 191f4476bd9SJan Schumann 192f4476bd9SJan Schumann //get info for given user 19370e4a085SAndreas Gohr $base = $this->_makeFilter($this->getConf('usertree'), $info); 19470e4a085SAndreas Gohr if($this->getConf('userfilter')) { 19570e4a085SAndreas Gohr $filter = $this->_makeFilter($this->getConf('userfilter'), $info); 196f4476bd9SJan Schumann } else { 197f4476bd9SJan Schumann $filter = "(ObjectClass=*)"; 198f4476bd9SJan Schumann } 199f4476bd9SJan Schumann 20040017f0bSItamar Shoham $this->_debug('LDAP Filter: '.htmlspecialchars($filter), 0, __LINE__, __FILE__); 20140017f0bSItamar Shoham 20270e4a085SAndreas Gohr $this->_debug('LDAP user search: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 20370e4a085SAndreas Gohr $this->_debug('LDAP search at: '.htmlspecialchars($base.' '.$filter), 0, __LINE__, __FILE__); 20440017f0bSItamar Shoham $sr = $this->_ldapsearch($this->con, $base, $filter, $this->getConf('userscope')); 20540017f0bSItamar Shoham 20640017f0bSItamar Shoham $result = @ldap_get_entries($this->con, $sr); 20740017f0bSItamar Shoham 20840017f0bSItamar Shoham // if result is not an array 20940017f0bSItamar Shoham if(!is_array($result)) { 21040017f0bSItamar Shoham // no objects found 21140017f0bSItamar Shoham $this->_debug('LDAP search returned non-array result: '.htmlspecialchars(print($result)), -1, __LINE__, __FILE__); 21240017f0bSItamar Shoham return false; 21340017f0bSItamar Shoham } 214f4476bd9SJan Schumann 215f4476bd9SJan Schumann // Don't accept more or less than one response 21640017f0bSItamar Shoham if ($result['count'] != 1) { 21740017f0bSItamar Shoham $this->_debug('LDAP search returned '.htmlspecialchars($result['count']).' results while it should return 1!', -1, __LINE__, __FILE__); 21840017f0bSItamar Shoham //for($i = 0; $i < $result["count"]; $i++) { 21940017f0bSItamar Shoham //$this->_debug('result: '.htmlspecialchars(print_r($result[$i])), 0, __LINE__, __FILE__); 22040017f0bSItamar Shoham //} 22140017f0bSItamar Shoham return false; 222f4476bd9SJan Schumann } 223f4476bd9SJan Schumann 22440017f0bSItamar Shoham 22540017f0bSItamar Shoham $this->_debug('LDAP search found single result !', 0, __LINE__, __FILE__); 22640017f0bSItamar Shoham 227f4476bd9SJan Schumann $user_result = $result[0]; 228f4476bd9SJan Schumann ldap_free_result($sr); 229f4476bd9SJan Schumann 230f4476bd9SJan Schumann // general user info 231f4476bd9SJan Schumann $info['dn'] = $user_result['dn']; 232f4476bd9SJan Schumann $info['gid'] = $user_result['gidnumber'][0]; 233f4476bd9SJan Schumann $info['mail'] = $user_result['mail'][0]; 234f4476bd9SJan Schumann $info['name'] = $user_result['cn'][0]; 235f4476bd9SJan Schumann $info['grps'] = array(); 236f4476bd9SJan Schumann 237f4476bd9SJan Schumann // overwrite if other attribs are specified. 23870e4a085SAndreas Gohr if(is_array($this->getConf('mapping'))) { 23970e4a085SAndreas Gohr foreach($this->getConf('mapping') as $localkey => $key) { 240f4476bd9SJan Schumann if(is_array($key)) { 241f4476bd9SJan Schumann // use regexp to clean up user_result 242*8f1011e8SPhy // $key = array($key=>$regexp), only handles the first key-value 243*8f1011e8SPhy $regexp = current($key); 244*8f1011e8SPhy $key = key($key); 2454f11d93dSGerrit Uitslag if($user_result[$key]) foreach($user_result[$key] as $grpkey => $grp) { 2464f11d93dSGerrit Uitslag if($grpkey !== 'count' && preg_match($regexp, $grp, $match)) { 247f4476bd9SJan Schumann if($localkey == 'grps') { 248f4476bd9SJan Schumann $info[$localkey][] = $match[1]; 249f4476bd9SJan Schumann } else { 250f4476bd9SJan Schumann $info[$localkey] = $match[1]; 251f4476bd9SJan Schumann } 252f4476bd9SJan Schumann } 253f4476bd9SJan Schumann } 254f4476bd9SJan Schumann } else { 255f4476bd9SJan Schumann $info[$localkey] = $user_result[$key][0]; 256f4476bd9SJan Schumann } 257f4476bd9SJan Schumann } 258f4476bd9SJan Schumann } 259f4476bd9SJan Schumann $user_result = array_merge($info, $user_result); 260f4476bd9SJan Schumann 261f4476bd9SJan Schumann //get groups for given user if grouptree is given 26270e4a085SAndreas Gohr if($this->getConf('grouptree') || $this->getConf('groupfilter')) { 26370e4a085SAndreas Gohr $base = $this->_makeFilter($this->getConf('grouptree'), $user_result); 26470e4a085SAndreas Gohr $filter = $this->_makeFilter($this->getConf('groupfilter'), $user_result); 26570e4a085SAndreas Gohr $sr = $this->_ldapsearch($this->con, $base, $filter, $this->getConf('groupscope'), array($this->getConf('groupkey'))); 26670e4a085SAndreas Gohr $this->_debug('LDAP group search: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 26770e4a085SAndreas Gohr $this->_debug('LDAP search at: '.htmlspecialchars($base.' '.$filter), 0, __LINE__, __FILE__); 26870e4a085SAndreas Gohr 269f4476bd9SJan Schumann if(!$sr) { 270f4476bd9SJan Schumann msg("LDAP: Reading group memberships failed", -1); 271f4476bd9SJan Schumann return false; 272f4476bd9SJan Schumann } 273f4476bd9SJan Schumann $result = ldap_get_entries($this->con, $sr); 274f4476bd9SJan Schumann ldap_free_result($sr); 275f4476bd9SJan Schumann 276f4476bd9SJan Schumann if(is_array($result)) foreach($result as $grp) { 2779f72d639SAndreas Gohr if(!empty($grp[$this->getConf('groupkey')])) { 2789f72d639SAndreas Gohr $group = $grp[$this->getConf('groupkey')]; 2799f72d639SAndreas Gohr if(is_array($group)){ 2809f72d639SAndreas Gohr $group = $group[0]; 2819f72d639SAndreas Gohr } else { 2829f72d639SAndreas Gohr $this->_debug('groupkey did not return a detailled result', 0, __LINE__, __FILE__); 2839f72d639SAndreas Gohr } 2849f72d639SAndreas Gohr if($group === '') continue; 2859f72d639SAndreas Gohr 2869f72d639SAndreas Gohr $this->_debug('LDAP usergroup: '.htmlspecialchars($group), 0, __LINE__, __FILE__); 2879f72d639SAndreas Gohr $info['grps'][] = $group; 288f4476bd9SJan Schumann } 289f4476bd9SJan Schumann } 290f4476bd9SJan Schumann } 291f4476bd9SJan Schumann 292f4476bd9SJan Schumann // always add the default group to the list of groups 29304e4890dSAndreas Gohr if(!$info['grps'] or !in_array($conf['defaultgroup'], $info['grps'])) { 294f4476bd9SJan Schumann $info['grps'][] = $conf['defaultgroup']; 295f4476bd9SJan Schumann } 296f4476bd9SJan Schumann return $info; 297f4476bd9SJan Schumann } 298f4476bd9SJan Schumann 299f4476bd9SJan Schumann /** 30006da270eSAxel Angel * Definition of the function modifyUser in order to modify the password 301b83b4364SGerrit Uitslag * 302b83b4364SGerrit Uitslag * @param string $user nick of the user to be changed 303b83b4364SGerrit Uitslag * @param array $changes array of field/value pairs to be changed (password will be clear text) 304b83b4364SGerrit Uitslag * @return bool true on success, false on error 30506da270eSAxel Angel */ 30606da270eSAxel Angel 30706da270eSAxel Angel function modifyUser($user,$changes){ 30806da270eSAxel Angel 30906da270eSAxel Angel // open the connection to the ldap 31006da270eSAxel Angel if(!$this->_openLDAP()){ 3118f03c311SPatrick Brown $this->_debug('LDAP cannot connect: '. htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 31206da270eSAxel Angel return false; 31306da270eSAxel Angel } 31406da270eSAxel Angel 31506da270eSAxel Angel // find the information about the user, in particular the "dn" 31606da270eSAxel Angel $info = $this->getUserData($user,true); 31706da270eSAxel Angel if(empty($info['dn'])) { 3188f03c311SPatrick Brown $this->_debug('LDAP cannot find your user dn', 0, __LINE__, __FILE__); 31906da270eSAxel Angel return false; 3208f2ea93bSAxel Angel } 32106da270eSAxel Angel $dn = $info['dn']; 32206da270eSAxel Angel 32306da270eSAxel Angel // find the old password of the user 32406da270eSAxel Angel list($loginuser,$loginsticky,$loginpass) = auth_getCookie(); 325719c6730SAxel Angel if ($loginuser !== null) { // the user is currently logged in 32618496fe0SAndreas Gohr $secret = auth_cookiesalt(!$loginsticky, true); 32706da270eSAxel Angel $pass = auth_decrypt($loginpass, $secret); 32806da270eSAxel Angel 32906da270eSAxel Angel // bind with the ldap 33006da270eSAxel Angel if(!@ldap_bind($this->con, $dn, $pass)){ 3318f03c311SPatrick Brown $this->_debug('LDAP user bind failed: '. htmlspecialchars($dn) .': '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 33206da270eSAxel Angel return false; 33306da270eSAxel Angel } 334719c6730SAxel Angel } elseif ($this->getConf('binddn') && $this->getConf('bindpw')) { 335719c6730SAxel Angel // we are changing the password on behalf of the user (eg: forgotten password) 336719c6730SAxel Angel // bind with the superuser ldap 3378ef94e9eSAndreas Gohr if (!@ldap_bind($this->con, $this->getConf('binddn'), conf_decodeString($this->getConf('bindpw')))){ 338719c6730SAxel Angel $this->_debug('LDAP bind as superuser: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 339719c6730SAxel Angel return false; 340719c6730SAxel Angel } 341719c6730SAxel Angel } 342719c6730SAxel Angel else { 343719c6730SAxel Angel return false; // no otherway 344719c6730SAxel Angel } 34506da270eSAxel Angel 34667723447SAxel Angel // Generate the salted hashed password for LDAP 34767723447SAxel Angel $phash = new PassHash(); 34867723447SAxel Angel $hash = $phash->hash_ssha($changes['pass']); 34967723447SAxel Angel 35006da270eSAxel Angel // change the password 35106da270eSAxel Angel if(!@ldap_mod_replace($this->con, $dn,array('userpassword' => $hash))){ 3528f03c311SPatrick Brown $this->_debug('LDAP mod replace failed: '. htmlspecialchars($dn) .': '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 35306da270eSAxel Angel return false; 35406da270eSAxel Angel } 35506da270eSAxel Angel 35606da270eSAxel Angel return true; 35706da270eSAxel Angel } 35806da270eSAxel Angel 35906da270eSAxel Angel /** 360f4476bd9SJan Schumann * Most values in LDAP are case-insensitive 36170e4a085SAndreas Gohr * 36270e4a085SAndreas Gohr * @return bool 363f4476bd9SJan Schumann */ 36470e4a085SAndreas Gohr public function isCaseSensitive() { 365f4476bd9SJan Schumann return false; 366f4476bd9SJan Schumann } 367f4476bd9SJan Schumann 368f4476bd9SJan Schumann /** 369f4476bd9SJan Schumann * Bulk retrieval of user data 370f4476bd9SJan Schumann * 371f4476bd9SJan Schumann * @author Dominik Eckelmann <dokuwiki@cosmocode.de> 37270e4a085SAndreas Gohr * @param int $start index of first user to be returned 37370e4a085SAndreas Gohr * @param int $limit max number of users to be returned 37470e4a085SAndreas Gohr * @param array $filter array of field/pattern pairs, null for no filter 375f4476bd9SJan Schumann * @return array of userinfo (refer getUserData for internal userinfo details) 376f4476bd9SJan Schumann */ 3779a2c73e8SAndreas Gohr function retrieveUsers($start = 0, $limit = 0, $filter = array()) { 378f4476bd9SJan Schumann if(!$this->_openLDAP()) return false; 379f4476bd9SJan Schumann 38070e4a085SAndreas Gohr if(is_null($this->users)) { 381f4476bd9SJan Schumann // Perform the search and grab all their details 38270e4a085SAndreas Gohr if($this->getConf('userfilter')) { 38370e4a085SAndreas Gohr $all_filter = str_replace('%{user}', '*', $this->getConf('userfilter')); 384f4476bd9SJan Schumann } else { 385f4476bd9SJan Schumann $all_filter = "(ObjectClass=*)"; 386f4476bd9SJan Schumann } 38770e4a085SAndreas Gohr $sr = ldap_search($this->con, $this->getConf('usertree'), $all_filter); 388f4476bd9SJan Schumann $entries = ldap_get_entries($this->con, $sr); 389f4476bd9SJan Schumann $users_array = array(); 3906619ddf4SSascha Klopp $userkey = $this->getConf('userkey'); 391f4476bd9SJan Schumann for($i = 0; $i < $entries["count"]; $i++) { 3926619ddf4SSascha Klopp array_push($users_array, $entries[$i][$userkey][0]); 393f4476bd9SJan Schumann } 394f4476bd9SJan Schumann asort($users_array); 395f4476bd9SJan Schumann $result = $users_array; 396f4476bd9SJan Schumann if(!$result) return array(); 397f4476bd9SJan Schumann $this->users = array_fill_keys($result, false); 398f4476bd9SJan Schumann } 399f4476bd9SJan Schumann $i = 0; 400f4476bd9SJan Schumann $count = 0; 401f4476bd9SJan Schumann $this->_constructPattern($filter); 402f4476bd9SJan Schumann $result = array(); 403f4476bd9SJan Schumann 404f4476bd9SJan Schumann foreach($this->users as $user => &$info) { 405f4476bd9SJan Schumann if($i++ < $start) { 406f4476bd9SJan Schumann continue; 407f4476bd9SJan Schumann } 408f4476bd9SJan Schumann if($info === false) { 409f4476bd9SJan Schumann $info = $this->getUserData($user); 410f4476bd9SJan Schumann } 411f4476bd9SJan Schumann if($this->_filter($user, $info)) { 412f4476bd9SJan Schumann $result[$user] = $info; 4139a2c73e8SAndreas Gohr if(($limit > 0) && (++$count >= $limit)) break; 414f4476bd9SJan Schumann } 415f4476bd9SJan Schumann } 416f4476bd9SJan Schumann return $result; 417f4476bd9SJan Schumann } 418f4476bd9SJan Schumann 419f4476bd9SJan Schumann /** 420f4476bd9SJan Schumann * Make LDAP filter strings. 421f4476bd9SJan Schumann * 422f4476bd9SJan Schumann * Used by auth_getUserData to make the filter 423f4476bd9SJan Schumann * strings for grouptree and groupfilter 424f4476bd9SJan Schumann * 425f4476bd9SJan Schumann * @author Troels Liebe Bentsen <tlb@rapanden.dk> 42670e4a085SAndreas Gohr * @param string $filter ldap search filter with placeholders 42770e4a085SAndreas Gohr * @param array $placeholders placeholders to fill in 428f4476bd9SJan Schumann * @return string 429f4476bd9SJan Schumann */ 43070e4a085SAndreas Gohr protected function _makeFilter($filter, $placeholders) { 431f4476bd9SJan Schumann preg_match_all("/%{([^}]+)/", $filter, $matches, PREG_PATTERN_ORDER); 432f4476bd9SJan Schumann //replace each match 433f4476bd9SJan Schumann foreach($matches[1] as $match) { 434f4476bd9SJan Schumann //take first element if array 435f4476bd9SJan Schumann if(is_array($placeholders[$match])) { 436f4476bd9SJan Schumann $value = $placeholders[$match][0]; 437f4476bd9SJan Schumann } else { 438f4476bd9SJan Schumann $value = $placeholders[$match]; 439f4476bd9SJan Schumann } 440f4476bd9SJan Schumann $value = $this->_filterEscape($value); 441f4476bd9SJan Schumann $filter = str_replace('%{'.$match.'}', $value, $filter); 442f4476bd9SJan Schumann } 443f4476bd9SJan Schumann return $filter; 444f4476bd9SJan Schumann } 445f4476bd9SJan Schumann 446f4476bd9SJan Schumann /** 44770e4a085SAndreas Gohr * return true if $user + $info match $filter criteria, false otherwise 448f4476bd9SJan Schumann * 449f4476bd9SJan Schumann * @author Chris Smith <chris@jalakai.co.uk> 45070e4a085SAndreas Gohr * 45170e4a085SAndreas Gohr * @param string $user the user's login name 45270e4a085SAndreas Gohr * @param array $info the user's userinfo array 45370e4a085SAndreas Gohr * @return bool 454f4476bd9SJan Schumann */ 45570e4a085SAndreas Gohr protected function _filter($user, $info) { 456f4476bd9SJan Schumann foreach($this->_pattern as $item => $pattern) { 457f4476bd9SJan Schumann if($item == 'user') { 45870e4a085SAndreas Gohr if(!preg_match($pattern, $user)) return false; 459f4476bd9SJan Schumann } else if($item == 'grps') { 46070e4a085SAndreas Gohr if(!count(preg_grep($pattern, $info['grps']))) return false; 461f4476bd9SJan Schumann } else { 46270e4a085SAndreas Gohr if(!preg_match($pattern, $info[$item])) return false; 463f4476bd9SJan Schumann } 464f4476bd9SJan Schumann } 46570e4a085SAndreas Gohr return true; 466f4476bd9SJan Schumann } 467f4476bd9SJan Schumann 46870e4a085SAndreas Gohr /** 46970e4a085SAndreas Gohr * Set the filter pattern 47070e4a085SAndreas Gohr * 47170e4a085SAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 47270e4a085SAndreas Gohr * 47370e4a085SAndreas Gohr * @param $filter 47470e4a085SAndreas Gohr * @return void 47570e4a085SAndreas Gohr */ 47670e4a085SAndreas Gohr protected function _constructPattern($filter) { 477f4476bd9SJan Schumann $this->_pattern = array(); 478f4476bd9SJan Schumann foreach($filter as $item => $pattern) { 479f4476bd9SJan Schumann $this->_pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters 480f4476bd9SJan Schumann } 481f4476bd9SJan Schumann } 482f4476bd9SJan Schumann 483f4476bd9SJan Schumann /** 484f4476bd9SJan Schumann * Escape a string to be used in a LDAP filter 485f4476bd9SJan Schumann * 486f4476bd9SJan Schumann * Ported from Perl's Net::LDAP::Util escape_filter_value 487f4476bd9SJan Schumann * 488f4476bd9SJan Schumann * @author Andreas Gohr 48970e4a085SAndreas Gohr * @param string $string 49070e4a085SAndreas Gohr * @return string 491f4476bd9SJan Schumann */ 49270e4a085SAndreas Gohr protected function _filterEscape($string) { 493234b5c9aSBernhard Liebl // see https://github.com/adldap/adLDAP/issues/22 494234b5c9aSBernhard Liebl return preg_replace_callback( 495234b5c9aSBernhard Liebl '/([\x00-\x1F\*\(\)\\\\])/', 496234b5c9aSBernhard Liebl function ($matches) { 497234b5c9aSBernhard Liebl return "\\".join("", unpack("H2", $matches[1])); 498234b5c9aSBernhard Liebl }, 49970e4a085SAndreas Gohr $string 50070e4a085SAndreas Gohr ); 501f4476bd9SJan Schumann } 502f4476bd9SJan Schumann 503f4476bd9SJan Schumann /** 504f4476bd9SJan Schumann * Opens a connection to the configured LDAP server and sets the wanted 505f4476bd9SJan Schumann * option on the connection 506f4476bd9SJan Schumann * 507f4476bd9SJan Schumann * @author Andreas Gohr <andi@splitbrain.org> 508f4476bd9SJan Schumann */ 50970e4a085SAndreas Gohr protected function _openLDAP() { 510f4476bd9SJan Schumann if($this->con) return true; // connection already established 511f4476bd9SJan Schumann 512234b5c9aSBernhard Liebl if($this->getConf('debug')) { 513234b5c9aSBernhard Liebl ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7); 514234b5c9aSBernhard Liebl } 515234b5c9aSBernhard Liebl 516f4476bd9SJan Schumann $this->bound = 0; 517f4476bd9SJan Schumann 51870e4a085SAndreas Gohr $port = $this->getConf('port'); 51993a7873eSAndreas Gohr $bound = false; 52070e4a085SAndreas Gohr $servers = explode(',', $this->getConf('server')); 52193a7873eSAndreas Gohr foreach($servers as $server) { 52293a7873eSAndreas Gohr $server = trim($server); 52393a7873eSAndreas Gohr $this->con = @ldap_connect($server, $port); 524f4476bd9SJan Schumann if(!$this->con) { 52593a7873eSAndreas Gohr continue; 526f4476bd9SJan Schumann } 527f4476bd9SJan Schumann 52893a7873eSAndreas Gohr /* 52993a7873eSAndreas Gohr * When OpenLDAP 2.x.x is used, ldap_connect() will always return a resource as it does 53093a7873eSAndreas Gohr * not actually connect but just initializes the connecting parameters. The actual 53193a7873eSAndreas Gohr * connect happens with the next calls to ldap_* funcs, usually with ldap_bind(). 53293a7873eSAndreas Gohr * 53393a7873eSAndreas Gohr * So we should try to bind to server in order to check its availability. 53493a7873eSAndreas Gohr */ 53593a7873eSAndreas Gohr 536f4476bd9SJan Schumann //set protocol version and dependend options 53770e4a085SAndreas Gohr if($this->getConf('version')) { 53870e4a085SAndreas Gohr if(!@ldap_set_option( 53970e4a085SAndreas Gohr $this->con, LDAP_OPT_PROTOCOL_VERSION, 54070e4a085SAndreas Gohr $this->getConf('version') 54170e4a085SAndreas Gohr ) 54270e4a085SAndreas Gohr ) { 54370e4a085SAndreas Gohr msg('Setting LDAP Protocol version '.$this->getConf('version').' failed', -1); 54470e4a085SAndreas Gohr $this->_debug('LDAP version set: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 545f4476bd9SJan Schumann } else { 546f4476bd9SJan Schumann //use TLS (needs version 3) 54770e4a085SAndreas Gohr if($this->getConf('starttls')) { 548f4476bd9SJan Schumann if(!@ldap_start_tls($this->con)) { 549f4476bd9SJan Schumann msg('Starting TLS failed', -1); 55070e4a085SAndreas Gohr $this->_debug('LDAP TLS set: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 551f4476bd9SJan Schumann } 552f4476bd9SJan Schumann } 553f4476bd9SJan Schumann // needs version 3 554d75d76b2SAndreas Gohr if($this->getConf('referrals') > -1) { 55570e4a085SAndreas Gohr if(!@ldap_set_option( 55670e4a085SAndreas Gohr $this->con, LDAP_OPT_REFERRALS, 55770e4a085SAndreas Gohr $this->getConf('referrals') 55870e4a085SAndreas Gohr ) 55970e4a085SAndreas Gohr ) { 560d75d76b2SAndreas Gohr msg('Setting LDAP referrals failed', -1); 56170e4a085SAndreas Gohr $this->_debug('LDAP referal set: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 562f4476bd9SJan Schumann } 563f4476bd9SJan Schumann } 564f4476bd9SJan Schumann } 565f4476bd9SJan Schumann } 566f4476bd9SJan Schumann 567f4476bd9SJan Schumann //set deref mode 56870e4a085SAndreas Gohr if($this->getConf('deref')) { 56970e4a085SAndreas Gohr if(!@ldap_set_option($this->con, LDAP_OPT_DEREF, $this->getConf('deref'))) { 57070e4a085SAndreas Gohr msg('Setting LDAP Deref mode '.$this->getConf('deref').' failed', -1); 57170e4a085SAndreas Gohr $this->_debug('LDAP deref set: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 572f4476bd9SJan Schumann } 573f4476bd9SJan Schumann } 57493a7873eSAndreas Gohr /* As of PHP 5.3.0 we can set timeout to speedup skipping of invalid servers */ 57593a7873eSAndreas Gohr if(defined('LDAP_OPT_NETWORK_TIMEOUT')) { 57693a7873eSAndreas Gohr ldap_set_option($this->con, LDAP_OPT_NETWORK_TIMEOUT, 1); 57793a7873eSAndreas Gohr } 578a426a6cdSAndreas Gohr 579a426a6cdSAndreas Gohr if($this->getConf('binddn') && $this->getConf('bindpw')) { 5808ef94e9eSAndreas Gohr $bound = @ldap_bind($this->con, $this->getConf('binddn'), conf_decodeString($this->getConf('bindpw'))); 581a426a6cdSAndreas Gohr $this->bound = 2; 582a426a6cdSAndreas Gohr } else { 58393a7873eSAndreas Gohr $bound = @ldap_bind($this->con); 584a426a6cdSAndreas Gohr } 58593a7873eSAndreas Gohr if($bound) { 58693a7873eSAndreas Gohr break; 58793a7873eSAndreas Gohr } 58893a7873eSAndreas Gohr } 58993a7873eSAndreas Gohr 59093a7873eSAndreas Gohr if(!$bound) { 59193a7873eSAndreas Gohr msg("LDAP: couldn't connect to LDAP server", -1); 592234b5c9aSBernhard Liebl $this->_debug(ldap_error($this->con), 0, __LINE__, __FILE__); 59393a7873eSAndreas Gohr return false; 59493a7873eSAndreas Gohr } 59593a7873eSAndreas Gohr 59670e4a085SAndreas Gohr $this->cando['getUsers'] = true; 597f4476bd9SJan Schumann return true; 598f4476bd9SJan Schumann } 599f4476bd9SJan Schumann 600f4476bd9SJan Schumann /** 601f4476bd9SJan Schumann * Wraps around ldap_search, ldap_list or ldap_read depending on $scope 602f4476bd9SJan Schumann * 603f4476bd9SJan Schumann * @author Andreas Gohr <andi@splitbrain.org> 60470e4a085SAndreas Gohr * @param resource $link_identifier 60570e4a085SAndreas Gohr * @param string $base_dn 60670e4a085SAndreas Gohr * @param string $filter 60770e4a085SAndreas Gohr * @param string $scope can be 'base', 'one' or 'sub' 608e0c26282SGerrit Uitslag * @param null|array $attributes 60970e4a085SAndreas Gohr * @param int $attrsonly 61070e4a085SAndreas Gohr * @param int $sizelimit 61170e4a085SAndreas Gohr * @return resource 612f4476bd9SJan Schumann */ 61370e4a085SAndreas Gohr protected function _ldapsearch($link_identifier, $base_dn, $filter, $scope = 'sub', $attributes = null, 614e7fbe189SAndreas Gohr $attrsonly = 0, $sizelimit = 0) { 615f4476bd9SJan Schumann if(is_null($attributes)) $attributes = array(); 616f4476bd9SJan Schumann 617f4476bd9SJan Schumann if($scope == 'base') { 61870e4a085SAndreas Gohr return @ldap_read( 61970e4a085SAndreas Gohr $link_identifier, $base_dn, $filter, $attributes, 620e7fbe189SAndreas Gohr $attrsonly, $sizelimit 62170e4a085SAndreas Gohr ); 622f4476bd9SJan Schumann } elseif($scope == 'one') { 62370e4a085SAndreas Gohr return @ldap_list( 62470e4a085SAndreas Gohr $link_identifier, $base_dn, $filter, $attributes, 625e7fbe189SAndreas Gohr $attrsonly, $sizelimit 62670e4a085SAndreas Gohr ); 627f4476bd9SJan Schumann } else { 62870e4a085SAndreas Gohr return @ldap_search( 62970e4a085SAndreas Gohr $link_identifier, $base_dn, $filter, $attributes, 630e7fbe189SAndreas Gohr $attrsonly, $sizelimit 63170e4a085SAndreas Gohr ); 632f4476bd9SJan Schumann } 633f4476bd9SJan Schumann } 63470e4a085SAndreas Gohr 63570e4a085SAndreas Gohr /** 63670e4a085SAndreas Gohr * Wrapper around msg() but outputs only when debug is enabled 63770e4a085SAndreas Gohr * 63870e4a085SAndreas Gohr * @param string $message 63970e4a085SAndreas Gohr * @param int $err 64070e4a085SAndreas Gohr * @param int $line 64170e4a085SAndreas Gohr * @param string $file 64270e4a085SAndreas Gohr * @return void 64370e4a085SAndreas Gohr */ 64470e4a085SAndreas Gohr protected function _debug($message, $err, $line, $file) { 64570e4a085SAndreas Gohr if(!$this->getConf('debug')) return; 64670e4a085SAndreas Gohr msg($message, $err, $line, $file); 64770e4a085SAndreas Gohr } 64870e4a085SAndreas Gohr 649f4476bd9SJan Schumann} 650