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 { 14*70e4a085SAndreas Gohr /* @var resource $con holds the LDAP connection*/ 15*70e4a085SAndreas Gohr protected $con = null; 16*70e4a085SAndreas Gohr 17*70e4a085SAndreas Gohr /* @var int $bound What type of connection does already exist? */ 18*70e4a085SAndreas Gohr protected $bound = 0; // 0: anonymous, 1: user, 2: superuser 19*70e4a085SAndreas Gohr 20*70e4a085SAndreas Gohr /* @var array $users User data cache */ 21*70e4a085SAndreas Gohr protected $users = null; 22*70e4a085SAndreas Gohr 23*70e4a085SAndreas Gohr /* @var array $_pattern User filter pattern */ 24*70e4a085SAndreas Gohr protected $_pattern = null; 25f4476bd9SJan Schumann 26f4476bd9SJan Schumann /** 27f4476bd9SJan Schumann * Constructor 28f4476bd9SJan Schumann */ 29*70e4a085SAndreas Gohr public function __construct() { 30454d868bSAndreas Gohr parent::__construct(); 31454d868bSAndreas Gohr 32f4476bd9SJan Schumann // ldap extension is needed 33f4476bd9SJan Schumann if(!function_exists('ldap_connect')) { 34*70e4a085SAndreas 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 39f4476bd9SJan Schumann // auth_ldap currently just handles authentication, so no 40f4476bd9SJan Schumann // capabilities are set 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> 51*70e4a085SAndreas Gohr * @param string $user 52*70e4a085SAndreas Gohr * @param string $pass 53f4476bd9SJan Schumann * @return bool 54f4476bd9SJan Schumann */ 55*70e4a085SAndreas 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 61*70e4a085SAndreas Gohr if($this->getConf('binddn') && $this->getConf('bindpw')) { 62f4476bd9SJan Schumann // use superuser credentials 63*70e4a085SAndreas Gohr if(!@ldap_bind($this->con, $this->getConf('binddn'), $this->getConf('bindpw'))) { 64*70e4a085SAndreas 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; 68*70e4a085SAndreas Gohr } else if($this->getConf('binddn') && 69*70e4a085SAndreas Gohr $this->getConf('usertree') && 70*70e4a085SAndreas Gohr $this->getConf('userfilter') 71*70e4a085SAndreas Gohr ) { 72f4476bd9SJan Schumann // special bind string 73*70e4a085SAndreas Gohr $dn = $this->_makeFilter( 74*70e4a085SAndreas Gohr $this->getConf('binddn'), 75*70e4a085SAndreas Gohr array('user'=> $user, 'server'=> $this->getConf('server')) 76*70e4a085SAndreas Gohr ); 77f4476bd9SJan Schumann 78*70e4a085SAndreas Gohr } else if(strpos($this->getConf('usertree'), '%{user}')) { 79f4476bd9SJan Schumann // direct user bind 80*70e4a085SAndreas Gohr $dn = $this->_makeFilter( 81*70e4a085SAndreas Gohr $this->getConf('usertree'), 82*70e4a085SAndreas Gohr array('user'=> $user, 'server'=> $this->getConf('server')) 83*70e4a085SAndreas 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); 89*70e4a085SAndreas 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)) { 98*70e4a085SAndreas Gohr $this->_debug("LDAP: bind with $dn failed", -1, __LINE__, __FILE__); 99*70e4a085SAndreas 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 106f4476bd9SJan Schumann $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)) { 115*70e4a085SAndreas Gohr $this->_debug("LDAP: bind with $dn failed", -1, __LINE__, __FILE__); 116*70e4a085SAndreas 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> 146*70e4a085SAndreas Gohr * 147*70e4a085SAndreas Gohr * @param string $user 148*70e4a085SAndreas Gohr * @param bool $inbind authldap specific, true if in bind phase 149f4476bd9SJan Schumann * @return array containing user data or false 150f4476bd9SJan Schumann */ 151*70e4a085SAndreas Gohr public function getUserData($user, $inbind = false) { 152f4476bd9SJan Schumann global $conf; 153f4476bd9SJan Schumann if(!$this->_openLDAP()) return false; 154f4476bd9SJan Schumann 155f4476bd9SJan Schumann // force superuser bind if wanted and not bound as superuser yet 156*70e4a085SAndreas Gohr if($this->getConf('binddn') && $this->getConf('bindpw') && $this->bound < 2) { 157f4476bd9SJan Schumann // use superuser credentials 158*70e4a085SAndreas Gohr if(!@ldap_bind($this->con, $this->getConf('binddn'), $this->getConf('bindpw'))) { 159*70e4a085SAndreas Gohr $this->_debug('LDAP bind as superuser: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 160f4476bd9SJan Schumann return false; 161f4476bd9SJan Schumann } 162f4476bd9SJan Schumann $this->bound = 2; 163f4476bd9SJan Schumann } elseif($this->bound == 0 && !$inbind) { 164f4476bd9SJan Schumann // in some cases getUserData is called outside the authentication workflow 165f4476bd9SJan Schumann // eg. for sending email notification on subscribed pages. This data might not 166f4476bd9SJan Schumann // be accessible anonymously, so we try to rebind the current user here 167f4476bd9SJan Schumann list($loginuser, $loginsticky, $loginpass) = auth_getCookie(); 168f4476bd9SJan Schumann if($loginuser && $loginpass) { 169f4476bd9SJan Schumann $loginpass = PMA_blowfish_decrypt($loginpass, auth_cookiesalt(!$loginsticky)); 170f4476bd9SJan Schumann $this->checkPass($loginuser, $loginpass); 171f4476bd9SJan Schumann } 172f4476bd9SJan Schumann } 173f4476bd9SJan Schumann 174f4476bd9SJan Schumann $info['user'] = $user; 175*70e4a085SAndreas Gohr $info['server'] = $this->getConf('server'); 176f4476bd9SJan Schumann 177f4476bd9SJan Schumann //get info for given user 178*70e4a085SAndreas Gohr $base = $this->_makeFilter($this->getConf('usertree'), $info); 179*70e4a085SAndreas Gohr if($this->getConf('userfilter')) { 180*70e4a085SAndreas Gohr $filter = $this->_makeFilter($this->getConf('userfilter'), $info); 181f4476bd9SJan Schumann } else { 182f4476bd9SJan Schumann $filter = "(ObjectClass=*)"; 183f4476bd9SJan Schumann } 184f4476bd9SJan Schumann 185*70e4a085SAndreas Gohr $sr = $this->_ldapsearch($this->con, $base, $filter, $this->getConf('userscope')); 186f4476bd9SJan Schumann $result = @ldap_get_entries($this->con, $sr); 187*70e4a085SAndreas Gohr $this->_debug('LDAP user search: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 188*70e4a085SAndreas Gohr $this->_debug('LDAP search at: '.htmlspecialchars($base.' '.$filter), 0, __LINE__, __FILE__); 189f4476bd9SJan Schumann 190f4476bd9SJan Schumann // Don't accept more or less than one response 191f4476bd9SJan Schumann if(!is_array($result) || $result['count'] != 1) { 192f4476bd9SJan Schumann return false; //user not found 193f4476bd9SJan Schumann } 194f4476bd9SJan Schumann 195f4476bd9SJan Schumann $user_result = $result[0]; 196f4476bd9SJan Schumann ldap_free_result($sr); 197f4476bd9SJan Schumann 198f4476bd9SJan Schumann // general user info 199f4476bd9SJan Schumann $info['dn'] = $user_result['dn']; 200f4476bd9SJan Schumann $info['gid'] = $user_result['gidnumber'][0]; 201f4476bd9SJan Schumann $info['mail'] = $user_result['mail'][0]; 202f4476bd9SJan Schumann $info['name'] = $user_result['cn'][0]; 203f4476bd9SJan Schumann $info['grps'] = array(); 204f4476bd9SJan Schumann 205f4476bd9SJan Schumann // overwrite if other attribs are specified. 206*70e4a085SAndreas Gohr if(is_array($this->getConf('mapping'))) { 207*70e4a085SAndreas Gohr foreach($this->getConf('mapping') as $localkey => $key) { 208f4476bd9SJan Schumann if(is_array($key)) { 209f4476bd9SJan Schumann // use regexp to clean up user_result 210f4476bd9SJan Schumann list($key, $regexp) = each($key); 211f4476bd9SJan Schumann if($user_result[$key]) foreach($user_result[$key] as $grp) { 212f4476bd9SJan Schumann if(preg_match($regexp, $grp, $match)) { 213f4476bd9SJan Schumann if($localkey == 'grps') { 214f4476bd9SJan Schumann $info[$localkey][] = $match[1]; 215f4476bd9SJan Schumann } else { 216f4476bd9SJan Schumann $info[$localkey] = $match[1]; 217f4476bd9SJan Schumann } 218f4476bd9SJan Schumann } 219f4476bd9SJan Schumann } 220f4476bd9SJan Schumann } else { 221f4476bd9SJan Schumann $info[$localkey] = $user_result[$key][0]; 222f4476bd9SJan Schumann } 223f4476bd9SJan Schumann } 224f4476bd9SJan Schumann } 225f4476bd9SJan Schumann $user_result = array_merge($info, $user_result); 226f4476bd9SJan Schumann 227f4476bd9SJan Schumann //get groups for given user if grouptree is given 228*70e4a085SAndreas Gohr if($this->getConf('grouptree') || $this->getConf('groupfilter')) { 229*70e4a085SAndreas Gohr $base = $this->_makeFilter($this->getConf('grouptree'), $user_result); 230*70e4a085SAndreas Gohr $filter = $this->_makeFilter($this->getConf('groupfilter'), $user_result); 231*70e4a085SAndreas Gohr $sr = $this->_ldapsearch($this->con, $base, $filter, $this->getConf('groupscope'), array($this->getConf('groupkey'))); 232*70e4a085SAndreas Gohr $this->_debug('LDAP group search: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 233*70e4a085SAndreas Gohr $this->_debug('LDAP search at: '.htmlspecialchars($base.' '.$filter), 0, __LINE__, __FILE__); 234*70e4a085SAndreas Gohr 235f4476bd9SJan Schumann if(!$sr) { 236f4476bd9SJan Schumann msg("LDAP: Reading group memberships failed", -1); 237f4476bd9SJan Schumann return false; 238f4476bd9SJan Schumann } 239f4476bd9SJan Schumann $result = ldap_get_entries($this->con, $sr); 240f4476bd9SJan Schumann ldap_free_result($sr); 241f4476bd9SJan Schumann 242f4476bd9SJan Schumann if(is_array($result)) foreach($result as $grp) { 243*70e4a085SAndreas Gohr if(!empty($grp[$this->getConf('groupkey')][0])) { 244*70e4a085SAndreas Gohr $this->_debug('LDAP usergroup: '.htmlspecialchars($grp[$this->getConf('groupkey')][0]), 0, __LINE__, __FILE__); 245*70e4a085SAndreas Gohr $info['grps'][] = $grp[$this->getConf('groupkey')][0]; 246f4476bd9SJan Schumann } 247f4476bd9SJan Schumann } 248f4476bd9SJan Schumann } 249f4476bd9SJan Schumann 250f4476bd9SJan Schumann // always add the default group to the list of groups 251f4476bd9SJan Schumann if(!in_array($conf['defaultgroup'], $info['grps'])) { 252f4476bd9SJan Schumann $info['grps'][] = $conf['defaultgroup']; 253f4476bd9SJan Schumann } 254f4476bd9SJan Schumann return $info; 255f4476bd9SJan Schumann } 256f4476bd9SJan Schumann 257f4476bd9SJan Schumann /** 258f4476bd9SJan Schumann * Most values in LDAP are case-insensitive 259*70e4a085SAndreas Gohr * 260*70e4a085SAndreas Gohr * @return bool 261f4476bd9SJan Schumann */ 262*70e4a085SAndreas Gohr public function isCaseSensitive() { 263f4476bd9SJan Schumann return false; 264f4476bd9SJan Schumann } 265f4476bd9SJan Schumann 266f4476bd9SJan Schumann /** 267f4476bd9SJan Schumann * Bulk retrieval of user data 268f4476bd9SJan Schumann * 269f4476bd9SJan Schumann * @author Dominik Eckelmann <dokuwiki@cosmocode.de> 270*70e4a085SAndreas Gohr * @param int $start index of first user to be returned 271*70e4a085SAndreas Gohr * @param int $limit max number of users to be returned 272*70e4a085SAndreas Gohr * @param array $filter array of field/pattern pairs, null for no filter 273f4476bd9SJan Schumann * @return array of userinfo (refer getUserData for internal userinfo details) 274f4476bd9SJan Schumann */ 275f4476bd9SJan Schumann function retrieveUsers($start = 0, $limit = -1, $filter = array()) { 276f4476bd9SJan Schumann if(!$this->_openLDAP()) return false; 277f4476bd9SJan Schumann 278*70e4a085SAndreas Gohr if(is_null($this->users)) { 279f4476bd9SJan Schumann // Perform the search and grab all their details 280*70e4a085SAndreas Gohr if($this->getConf('userfilter')) { 281*70e4a085SAndreas Gohr $all_filter = str_replace('%{user}', '*', $this->getConf('userfilter')); 282f4476bd9SJan Schumann } else { 283f4476bd9SJan Schumann $all_filter = "(ObjectClass=*)"; 284f4476bd9SJan Schumann } 285*70e4a085SAndreas Gohr $sr = ldap_search($this->con, $this->getConf('usertree'), $all_filter); 286f4476bd9SJan Schumann $entries = ldap_get_entries($this->con, $sr); 287f4476bd9SJan Schumann $users_array = array(); 288f4476bd9SJan Schumann for($i = 0; $i < $entries["count"]; $i++) { 289f4476bd9SJan Schumann array_push($users_array, $entries[$i]["uid"][0]); 290f4476bd9SJan Schumann } 291f4476bd9SJan Schumann asort($users_array); 292f4476bd9SJan Schumann $result = $users_array; 293f4476bd9SJan Schumann if(!$result) return array(); 294f4476bd9SJan Schumann $this->users = array_fill_keys($result, false); 295f4476bd9SJan Schumann } 296f4476bd9SJan Schumann $i = 0; 297f4476bd9SJan Schumann $count = 0; 298f4476bd9SJan Schumann $this->_constructPattern($filter); 299f4476bd9SJan Schumann $result = array(); 300f4476bd9SJan Schumann 301f4476bd9SJan Schumann foreach($this->users as $user => &$info) { 302f4476bd9SJan Schumann if($i++ < $start) { 303f4476bd9SJan Schumann continue; 304f4476bd9SJan Schumann } 305f4476bd9SJan Schumann if($info === false) { 306f4476bd9SJan Schumann $info = $this->getUserData($user); 307f4476bd9SJan Schumann } 308f4476bd9SJan Schumann if($this->_filter($user, $info)) { 309f4476bd9SJan Schumann $result[$user] = $info; 310f4476bd9SJan Schumann if(($limit >= 0) && (++$count >= $limit)) break; 311f4476bd9SJan Schumann } 312f4476bd9SJan Schumann } 313f4476bd9SJan Schumann return $result; 314f4476bd9SJan Schumann } 315f4476bd9SJan Schumann 316f4476bd9SJan Schumann /** 317f4476bd9SJan Schumann * Make LDAP filter strings. 318f4476bd9SJan Schumann * 319f4476bd9SJan Schumann * Used by auth_getUserData to make the filter 320f4476bd9SJan Schumann * strings for grouptree and groupfilter 321f4476bd9SJan Schumann * 322f4476bd9SJan Schumann * @author Troels Liebe Bentsen <tlb@rapanden.dk> 323*70e4a085SAndreas Gohr * @param string $filter ldap search filter with placeholders 324*70e4a085SAndreas Gohr * @param array $placeholders placeholders to fill in 325f4476bd9SJan Schumann * @return string 326f4476bd9SJan Schumann */ 327*70e4a085SAndreas Gohr protected function _makeFilter($filter, $placeholders) { 328f4476bd9SJan Schumann preg_match_all("/%{([^}]+)/", $filter, $matches, PREG_PATTERN_ORDER); 329f4476bd9SJan Schumann //replace each match 330f4476bd9SJan Schumann foreach($matches[1] as $match) { 331f4476bd9SJan Schumann //take first element if array 332f4476bd9SJan Schumann if(is_array($placeholders[$match])) { 333f4476bd9SJan Schumann $value = $placeholders[$match][0]; 334f4476bd9SJan Schumann } else { 335f4476bd9SJan Schumann $value = $placeholders[$match]; 336f4476bd9SJan Schumann } 337f4476bd9SJan Schumann $value = $this->_filterEscape($value); 338f4476bd9SJan Schumann $filter = str_replace('%{'.$match.'}', $value, $filter); 339f4476bd9SJan Schumann } 340f4476bd9SJan Schumann return $filter; 341f4476bd9SJan Schumann } 342f4476bd9SJan Schumann 343f4476bd9SJan Schumann /** 344*70e4a085SAndreas Gohr * return true if $user + $info match $filter criteria, false otherwise 345f4476bd9SJan Schumann * 346f4476bd9SJan Schumann * @author Chris Smith <chris@jalakai.co.uk> 347*70e4a085SAndreas Gohr * 348*70e4a085SAndreas Gohr * @param string $user the user's login name 349*70e4a085SAndreas Gohr * @param array $info the user's userinfo array 350*70e4a085SAndreas Gohr * @return bool 351f4476bd9SJan Schumann */ 352*70e4a085SAndreas Gohr protected function _filter($user, $info) { 353f4476bd9SJan Schumann foreach($this->_pattern as $item => $pattern) { 354f4476bd9SJan Schumann if($item == 'user') { 355*70e4a085SAndreas Gohr if(!preg_match($pattern, $user)) return false; 356f4476bd9SJan Schumann } else if($item == 'grps') { 357*70e4a085SAndreas Gohr if(!count(preg_grep($pattern, $info['grps']))) return false; 358f4476bd9SJan Schumann } else { 359*70e4a085SAndreas Gohr if(!preg_match($pattern, $info[$item])) return false; 360f4476bd9SJan Schumann } 361f4476bd9SJan Schumann } 362*70e4a085SAndreas Gohr return true; 363f4476bd9SJan Schumann } 364f4476bd9SJan Schumann 365*70e4a085SAndreas Gohr /** 366*70e4a085SAndreas Gohr * Set the filter pattern 367*70e4a085SAndreas Gohr * 368*70e4a085SAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 369*70e4a085SAndreas Gohr * 370*70e4a085SAndreas Gohr * @param $filter 371*70e4a085SAndreas Gohr * @return void 372*70e4a085SAndreas Gohr */ 373*70e4a085SAndreas Gohr protected function _constructPattern($filter) { 374f4476bd9SJan Schumann $this->_pattern = array(); 375f4476bd9SJan Schumann foreach($filter as $item => $pattern) { 376f4476bd9SJan Schumann $this->_pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters 377f4476bd9SJan Schumann } 378f4476bd9SJan Schumann } 379f4476bd9SJan Schumann 380f4476bd9SJan Schumann /** 381f4476bd9SJan Schumann * Escape a string to be used in a LDAP filter 382f4476bd9SJan Schumann * 383f4476bd9SJan Schumann * Ported from Perl's Net::LDAP::Util escape_filter_value 384f4476bd9SJan Schumann * 385f4476bd9SJan Schumann * @author Andreas Gohr 386*70e4a085SAndreas Gohr * @param string $string 387*70e4a085SAndreas Gohr * @return string 388f4476bd9SJan Schumann */ 389*70e4a085SAndreas Gohr protected function _filterEscape($string) { 390*70e4a085SAndreas Gohr return preg_replace( 391*70e4a085SAndreas Gohr '/([\x00-\x1F\*\(\)\\\\])/e', 392f4476bd9SJan Schumann '"\\\\\".join("",unpack("H2","$1"))', 393*70e4a085SAndreas Gohr $string 394*70e4a085SAndreas Gohr ); 395f4476bd9SJan Schumann } 396f4476bd9SJan Schumann 397f4476bd9SJan Schumann /** 398f4476bd9SJan Schumann * Opens a connection to the configured LDAP server and sets the wanted 399f4476bd9SJan Schumann * option on the connection 400f4476bd9SJan Schumann * 401f4476bd9SJan Schumann * @author Andreas Gohr <andi@splitbrain.org> 402f4476bd9SJan Schumann */ 403*70e4a085SAndreas Gohr protected function _openLDAP() { 404f4476bd9SJan Schumann if($this->con) return true; // connection already established 405f4476bd9SJan Schumann 406f4476bd9SJan Schumann $this->bound = 0; 407f4476bd9SJan Schumann 408*70e4a085SAndreas Gohr $port = $this->getConf('port'); 40993a7873eSAndreas Gohr $bound = false; 410*70e4a085SAndreas Gohr $servers = explode(',', $this->getConf('server')); 41193a7873eSAndreas Gohr foreach($servers as $server) { 41293a7873eSAndreas Gohr $server = trim($server); 41393a7873eSAndreas Gohr $this->con = @ldap_connect($server, $port); 414f4476bd9SJan Schumann if(!$this->con) { 41593a7873eSAndreas Gohr continue; 416f4476bd9SJan Schumann } 417f4476bd9SJan Schumann 41893a7873eSAndreas Gohr /* 41993a7873eSAndreas Gohr * When OpenLDAP 2.x.x is used, ldap_connect() will always return a resource as it does 42093a7873eSAndreas Gohr * not actually connect but just initializes the connecting parameters. The actual 42193a7873eSAndreas Gohr * connect happens with the next calls to ldap_* funcs, usually with ldap_bind(). 42293a7873eSAndreas Gohr * 42393a7873eSAndreas Gohr * So we should try to bind to server in order to check its availability. 42493a7873eSAndreas Gohr */ 42593a7873eSAndreas Gohr 426f4476bd9SJan Schumann //set protocol version and dependend options 427*70e4a085SAndreas Gohr if($this->getConf('version')) { 428*70e4a085SAndreas Gohr if(!@ldap_set_option( 429*70e4a085SAndreas Gohr $this->con, LDAP_OPT_PROTOCOL_VERSION, 430*70e4a085SAndreas Gohr $this->getConf('version') 431*70e4a085SAndreas Gohr ) 432*70e4a085SAndreas Gohr ) { 433*70e4a085SAndreas Gohr msg('Setting LDAP Protocol version '.$this->getConf('version').' failed', -1); 434*70e4a085SAndreas Gohr $this->_debug('LDAP version set: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 435f4476bd9SJan Schumann } else { 436f4476bd9SJan Schumann //use TLS (needs version 3) 437*70e4a085SAndreas Gohr if($this->getConf('starttls')) { 438f4476bd9SJan Schumann if(!@ldap_start_tls($this->con)) { 439f4476bd9SJan Schumann msg('Starting TLS failed', -1); 440*70e4a085SAndreas Gohr $this->_debug('LDAP TLS set: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 441f4476bd9SJan Schumann } 442f4476bd9SJan Schumann } 443f4476bd9SJan Schumann // needs version 3 444*70e4a085SAndreas Gohr if($this->getConf('referrals')) { 445*70e4a085SAndreas Gohr if(!@ldap_set_option( 446*70e4a085SAndreas Gohr $this->con, LDAP_OPT_REFERRALS, 447*70e4a085SAndreas Gohr $this->getConf('referrals') 448*70e4a085SAndreas Gohr ) 449*70e4a085SAndreas Gohr ) { 450f4476bd9SJan Schumann msg('Setting LDAP referrals to off failed', -1); 451*70e4a085SAndreas Gohr $this->_debug('LDAP referal set: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 452f4476bd9SJan Schumann } 453f4476bd9SJan Schumann } 454f4476bd9SJan Schumann } 455f4476bd9SJan Schumann } 456f4476bd9SJan Schumann 457f4476bd9SJan Schumann //set deref mode 458*70e4a085SAndreas Gohr if($this->getConf('deref')) { 459*70e4a085SAndreas Gohr if(!@ldap_set_option($this->con, LDAP_OPT_DEREF, $this->getConf('deref'))) { 460*70e4a085SAndreas Gohr msg('Setting LDAP Deref mode '.$this->getConf('deref').' failed', -1); 461*70e4a085SAndreas Gohr $this->_debug('LDAP deref set: '.htmlspecialchars(ldap_error($this->con)), 0, __LINE__, __FILE__); 462f4476bd9SJan Schumann } 463f4476bd9SJan Schumann } 46493a7873eSAndreas Gohr /* As of PHP 5.3.0 we can set timeout to speedup skipping of invalid servers */ 46593a7873eSAndreas Gohr if(defined('LDAP_OPT_NETWORK_TIMEOUT')) { 46693a7873eSAndreas Gohr ldap_set_option($this->con, LDAP_OPT_NETWORK_TIMEOUT, 1); 46793a7873eSAndreas Gohr } 46893a7873eSAndreas Gohr $bound = @ldap_bind($this->con); 46993a7873eSAndreas Gohr if($bound) { 47093a7873eSAndreas Gohr break; 47193a7873eSAndreas Gohr } 47293a7873eSAndreas Gohr } 47393a7873eSAndreas Gohr 47493a7873eSAndreas Gohr if(!$bound) { 47593a7873eSAndreas Gohr msg("LDAP: couldn't connect to LDAP server", -1); 47693a7873eSAndreas Gohr return false; 47793a7873eSAndreas Gohr } 47893a7873eSAndreas Gohr 479*70e4a085SAndreas Gohr $this->cando['getUsers'] = true; 480f4476bd9SJan Schumann return true; 481f4476bd9SJan Schumann } 482f4476bd9SJan Schumann 483f4476bd9SJan Schumann /** 484f4476bd9SJan Schumann * Wraps around ldap_search, ldap_list or ldap_read depending on $scope 485f4476bd9SJan Schumann * 486f4476bd9SJan Schumann * @author Andreas Gohr <andi@splitbrain.org> 487*70e4a085SAndreas Gohr * @param resource $link_identifier 488*70e4a085SAndreas Gohr * @param string $base_dn 489*70e4a085SAndreas Gohr * @param string $filter 490*70e4a085SAndreas Gohr * @param string $scope can be 'base', 'one' or 'sub' 491*70e4a085SAndreas Gohr * @param null $attributes 492*70e4a085SAndreas Gohr * @param int $attrsonly 493*70e4a085SAndreas Gohr * @param int $sizelimit 494*70e4a085SAndreas Gohr * @param int $timelimit 495*70e4a085SAndreas Gohr * @param int $deref 496*70e4a085SAndreas Gohr * @return resource 497f4476bd9SJan Schumann */ 498*70e4a085SAndreas Gohr protected function _ldapsearch($link_identifier, $base_dn, $filter, $scope = 'sub', $attributes = null, 499f4476bd9SJan Schumann $attrsonly = 0, $sizelimit = 0, $timelimit = 0, $deref = LDAP_DEREF_NEVER) { 500f4476bd9SJan Schumann if(is_null($attributes)) $attributes = array(); 501f4476bd9SJan Schumann 502f4476bd9SJan Schumann if($scope == 'base') { 503*70e4a085SAndreas Gohr return @ldap_read( 504*70e4a085SAndreas Gohr $link_identifier, $base_dn, $filter, $attributes, 505*70e4a085SAndreas Gohr $attrsonly, $sizelimit, $timelimit, $deref 506*70e4a085SAndreas Gohr ); 507f4476bd9SJan Schumann } elseif($scope == 'one') { 508*70e4a085SAndreas Gohr return @ldap_list( 509*70e4a085SAndreas Gohr $link_identifier, $base_dn, $filter, $attributes, 510*70e4a085SAndreas Gohr $attrsonly, $sizelimit, $timelimit, $deref 511*70e4a085SAndreas Gohr ); 512f4476bd9SJan Schumann } else { 513*70e4a085SAndreas Gohr return @ldap_search( 514*70e4a085SAndreas Gohr $link_identifier, $base_dn, $filter, $attributes, 515*70e4a085SAndreas Gohr $attrsonly, $sizelimit, $timelimit, $deref 516*70e4a085SAndreas Gohr ); 517f4476bd9SJan Schumann } 518f4476bd9SJan Schumann } 519*70e4a085SAndreas Gohr 520*70e4a085SAndreas Gohr /** 521*70e4a085SAndreas Gohr * Wrapper around msg() but outputs only when debug is enabled 522*70e4a085SAndreas Gohr * 523*70e4a085SAndreas Gohr * @param string $message 524*70e4a085SAndreas Gohr * @param int $err 525*70e4a085SAndreas Gohr * @param int $line 526*70e4a085SAndreas Gohr * @param string $file 527*70e4a085SAndreas Gohr * @return void 528*70e4a085SAndreas Gohr */ 529*70e4a085SAndreas Gohr protected function _debug($message, $err, $line, $file) { 530*70e4a085SAndreas Gohr if(!$this->getConf('debug')) return; 531*70e4a085SAndreas Gohr msg($message, $err, $line, $file); 532*70e4a085SAndreas Gohr } 533*70e4a085SAndreas Gohr 534f4476bd9SJan Schumann} 535