private function _search($base, $filter, $attributes, $sizelimit) { error_log("looking for {$base} {$filter}"); $filter_ = Net_LDAP2_Filter::parse($filter); if ($filter_ instanceof PEAR_Error) { error_log("filter {$filter} invalid (?)"); error_log($filter_->message); } $r = array("count" => 0); foreach ($this->_entries as $branch => $entries) { if ($base && !preg_match("/{$base}\$/", $branch)) { continue; } foreach ($entries as $entry) { if ($filter_->matches($entry)) { $r[] = MyTestLdap::get_one_entry($entry, $attributes); $r["count"] = $r["count"] + 1; if ($r["count"] == $sizelimit) { return $r; } } } } return $r; }
/** * @param $username string * @param $configFilter string * @return void */ private function PopulateUser($username, $configFilter) { $uidAttribute = $this->options->GetUserIdAttribute(); Log::Debug('LDAP - uid attribute: %s', $uidAttribute); $RequiredGroup = $this->options->GetRequiredGroup(); $filter = Net_LDAP2_Filter::create($uidAttribute, 'equals', $username); if ($configFilter) { $configFilter = Net_LDAP2_Filter::parse($configFilter); if (Net_LDAP2::isError($configFilter)) { $message = 'Could not parse search filter %s: ' . $configFilter->getMessage(); Log::Error($message, $username); } $filter = Net_LDAP2_Filter::combine('and', array($filter, $configFilter)); } $attributes = $this->options->Attributes(); Log::Debug('LDAP - Loading user attributes: %s', implode(', ', $attributes)); $options = array('attributes' => $attributes); Log::Debug('Searching ldap for user %s', $username); $searchResult = $this->ldap->search(null, $filter, $options); if (Net_LDAP2::isError($searchResult)) { $message = 'Could not search ldap for user %s: ' . $searchResult->getMessage(); Log::Error($message, $username); } $currentResult = $searchResult->current(); if ($searchResult->count() == 1 && $currentResult !== false) { Log::Debug('Found user %s', $username); if (!empty($RequiredGroup)) { Log::Debug('LDAP - Required Group: %s', $RequiredGroup); $group_filter = Net_LDAP2_Filter::create('uniquemember', 'equals', $currentResult->dn()); $group_searchResult = $this->ldap->search($RequiredGroup, $group_filter, null); if (Net_LDAP2::isError($group_searchResult) && !empty($RequiredGroup)) { $message = 'Could not match Required Group %s: ' . $group_searchResult->getMessage(); Log::Error($message, $username); } if ($group_searchResult->count() == 1 && $group_searchResult !== false) { Log::Debug('Matched Required Group %s', $RequiredGroup); /** @var Net_LDAP2_Entry $entry */ $this->user = new LdapUser($currentResult, $this->options->AttributeMapping()); } } else { /** @var Net_LDAP2_Entry $entry */ $this->user = new LdapUser($currentResult, $this->options->AttributeMapping()); } } else { Log::Debug('Could not find user %s', $username); } }
/** * Retrieve information from LDAP * * @param string $uid login or email * @return array */ public function getRemoteUserInfo($uid) { if (strpos($uid, '@') === false) { $filter = Net_LDAP2_Filter::create('uid', 'equals', $uid); } else { $filter = Net_LDAP2_Filter::create('mail', 'equals', $uid); } if (!empty($this->user_filter_string)) { $user_filter = Net_LDAP2_Filter::parse($this->user_filter_string); $filter = Net_LDAP2_Filter::combine('and', array($filter, $user_filter)); } $search = $this->connect()->search($this->basedn, $filter, array('sizelimit' => 1)); $entry = $search->shiftEntry(); if (!$entry || Misc::isError($entry)) { return null; } $details = array('uid' => $entry->get_value('uid'), 'full_name' => Misc::trim($entry->get_value('cn')), 'emails' => Misc::trim(Misc::lowercase($entry->get_value('mail', 'all'))), 'customer_id' => Misc::trim($entry->get_value($this->customer_id_attribute)) ?: null, 'contact_id' => Misc::trim($entry->get_value($this->contact_id_attribute)) ?: null); return $details; }
if ($r === NULL) { fail("searchUser {$token}", "invalid response\n{$js}"); } $got = map_obj_attr($r, 'uid'); expectToBe(json_encode($got), $expected, "searchUser {$token}"); } searchUser('Fooo Bar', '["fbar"]'); searchUser('o Bar', '["fbar","zbar"]'); searchUser('Fooo B', '["fbar"]'); searchUser('Fooo ', '["fbar"]'); searchUser('Fooo', '["fbar"]'); searchUser('Foo', '[]'); // no sub search if short token searchUser('Bar Fooo', '["fbar"]'); searchUser('Bar Foo', '["fbar"]'); searchUser('Bar', '["fbar","zbar"]'); // exact search on sn searchUser('99007', '["fbar"]'); // exact search on sn Xexpect('getSuperGroups structures', '{"diploma-L2T101":{"key":"diploma-L2T101","description":"L2T101 - Licence 1\\u00e8re ann\\u00e9e Droit (FC)","rawKey":"L2T101","name":"L2T101 - Licence 1\\u00e8re ann\\u00e9e Droit (FC)","category":"diploma","superGroups":[]}}', 'getSuperGroups', ['key' => 'structures-DGH']); $allGroups = <<<'EOS' [{"key":"groups-employees.administration.DGH","name":"employees.administration.DGH"},{"key":"groups-employees.administration.DGHA","name":"DSIUN-SAS : Service des applications et services num\u00e9riques","description":"employees.administration.DGH"},{"key":"groups-matiB1010514","name":"UFR 02 - Mati\u00e8re (Semestre 1) : Comptabilit\u00e9 d'entreprise","description":"<br>\n<br>\n<br>\n"},{"key":"affiliation-faculty","name":"Tous les enseignants","description":"Tous les enseignants"},{"key":"affiliation-teacher","name":"Tous les enseignants et charg\u00e9s d'enseignement","description":"Tous les enseignants et charg\u00e9s d'enseignement"},{"key":"affiliation-student","name":"Tous les \u00e9tudiants","description":"Tous les \u00e9tudiants"},{"key":"affiliation-staff","name":"Tous les Biatss","description":"Tous les Biatss"},{"key":"affiliation-researcher","name":"Tous les chercheurs","description":"Tous les chercheurs"},{"key":"affiliation-emeritus","name":"Tous les professeurs \u00e9m\u00e9rites","description":"Tous les professeurs \u00e9m\u00e9rites"},{"key":"affiliation-affiliate","name":"Tous les invit\u00e9s","description":"Tous les invit\u00e9s"},{"key":"businessCategory-research","name":"Laboratoires de recherche","description":"Laboratoires de recherche"},{"key":"businessCategory-library","name":"Biblioth\u00e8ques","description":"Biblioth\u00e8ques"},{"key":"businessCategory-doctoralSchool","name":"\u00c9coles doctorales","description":"\u00c9coles doctorales"},{"key":"businessCategory-administration","name":"Services","description":"Services"},{"key":"businessCategory-pedagogy","name":"Composantes personnels","description":"Composantes personnels"}] EOS; Xexpect('allGroups', $allGroups, 'allGroups', []); if (0) { $dn = 'uid=foo,dc=fr'; $entry = Net_LDAP2_Entry::createFresh($dn); $entry->add(array('entryDN' => $entry->dn())); $filter = Net_LDAP2_Filter::parse("(entryDN={$dn})"); var_dump($filter->matches($entry)); }
/** * Test match() */ public function testMatch() { // make up some local test entry $entry1 = Net_LDAP2_Entry::createFresh('cn=Simpson Homer,l=springfield,c=usa', array('cn' => 'Simpson Homer', 'sn' => 'Simpson', 'givenName' => 'Homer', 'fingers' => 5, 'hairColor' => 'black', 'donutsConsumed' => 4521875663232, 'height' => '175', 'mail' => '*****@*****.**', 'objectClass' => array('top', 'person', 'inetOrgPerson', 'myFancyTestClass'))); $entry2 = Net_LDAP2_Entry::createFresh('cn=Simpson Bart,l=springfield,c=usa', array('cn' => 'Simpson Bart', 'sn' => 'Simpson', 'givenName' => 'Bart', 'fingers' => 5, 'hairColor' => 'yellow', 'height' => '120', 'mail' => '*****@*****.**', 'objectClass' => array('top', 'person', 'inetOrgPerson', 'myFancyTestClass'))); $entry3 = Net_LDAP2_Entry::createFresh('cn=Brockman Kent,l=springfield,c=usa', array('cn' => 'Brockman Kent', 'sn' => 'Brockman', 'givenName' => 'Kent', 'fingers' => 5, 'hairColor' => 'white', 'height' => '185', 'mail' => '*****@*****.**', 'objectClass' => array('top', 'person', 'inetOrgPerson', 'myFancyTestClass'))); $allEntries = array($entry1, $entry2, $entry3); // Simple matching on single entry $filter = Net_LDAP2_Filter::create('cn', 'equals', 'Simpson Homer'); $this->assertEquals(1, $filter->matches($entry1)); $filter = Net_LDAP2_Filter::create('cn', 'equals', 'son'); $this->assertEquals(0, $filter->matches($entry1)); $filter = Net_LDAP2_Filter::create('mail', 'begins', 'Hom'); $this->assertEquals(1, $filter->matches($entry1)); $filter = Net_LDAP2_Filter::create('objectClass', 'contains', 'org'); // note the lowercase of 'org', as DirSTR is usually syntax CaseIgnore $this->assertEquals(1, $filter->matches($entry1)); // Simple negative tests on single entry $filter = Net_LDAP2_Filter::create('givenName', 'equals', 'Lisa-is-nonexistent'); $this->assertEquals(0, $filter->matches($entry1)); // Simple tests with multiple entries $filter = Net_LDAP2_Filter::create('cn', 'begins', 'Nomatch'); $this->assertEquals(0, $filter->matches($allEntries)); $filter = Net_LDAP2_Filter::create('cn', 'begins', 'Simpson Ho'); $this->assertEquals(1, $filter->matches($allEntries)); $filter = Net_LDAP2_Filter::create('cn', 'begins', 'Simpson'); $this->assertEquals(2, $filter->matches($allEntries)); // test with retrieving the resulting entries $filter = Net_LDAP2_Filter::create('cn', 'begins', 'Simpson Ho'); $filterresult = array(); $this->assertEquals(1, $filter->matches($allEntries, $filterresult)); $this->assertEquals(count($filterresult), $filter->matches($allEntries, $filterresult), "returned result and result counter differ!"); $this->assertEquals($entry1->dn(), array_shift($filterresult)->dn(), "Filtered entry does not equal expected entry! filter='" . $filter->asString() . "'"); // make sure return values are consistent with input and that all entries are found $filter = Net_LDAP2_Filter::parse('(objectClass=*)'); $filterresult = array(); $this->assertEquals(count($allEntries), $filter->matches($allEntries, $filterresult), "returned result does not match input data count"); $this->assertEquals(count($filterresult), $filter->matches($allEntries, $filterresult), "returned result and result counter differ!"); // Test for compliant "any" filtering: // Only entries should be returned, that have the attribute // Negation: Only Entries that don't have the attribute set at all $filter = Net_LDAP2_Filter::create('donutsConsumed', 'any'); // only homer consume donuts $filterresult = array(); $this->assertEquals(1, $filter->matches($allEntries, $filterresult)); $this->assertEquals($entry1->dn(), array_shift($filterresult)->dn(), "Filtered entry does not equal expected entry! filter='" . $filter->asString() . "'"); $filter = Net_LDAP2_Filter::combine('not', $filter); // all but homer consume donuts $this->assertEquals(count($allEntries) - 1, $filter->matches($allEntries, $filterresult), "Filtered entry does not equal expected entry! filter='" . $filter->asString() . "'"); // NOT combination test $filter = Net_LDAP2_Filter::create('givenName', 'not equals', 'Homer'); $filterresult = array(); $this->assertEquals(2, $filter->matches($allEntries, $filterresult)); $this->assertEquals($entry2->dn(), array_shift($filterresult)->dn(), "Filtered entry does not equal expected entry! filter='" . $filter->asString() . "'"); $this->assertEquals($entry3->dn(), array_shift($filterresult)->dn(), "Filtered entry does not equal expected entry! filter='" . $filter->asString() . "'"); // OR combination test $filter1 = Net_LDAP2_Filter::create('sn', 'equals', 'Simpson'); $filter2 = Net_LDAP2_Filter::create('givenName', 'equals', 'Kent'); $filter_or = Net_LDAP2_Filter::combine('or', array($filter1, $filter2)); $filterresult = array(); $this->assertEquals(3, $filter_or->matches($allEntries, $filterresult)); // AND combination test $filter1 = Net_LDAP2_Filter::create('sn', 'equals', 'Simpson'); $filter2 = Net_LDAP2_Filter::create('givenName', 'equals', 'Bart'); $filter_and = Net_LDAP2_Filter::combine('and', array($filter1, $filter2)); $filterresult = array(); $filter_and->matches($allEntries, $filterresult); $this->assertEquals(1, $filter_and->matches($allEntries, $filterresult), "AND Filter failed '" . $filter_and->asString() . "'"); // AND, NOT and OR combined test $filter1 = Net_LDAP2_Filter::combine('or', array(Net_LDAP2_Filter::create('hairColor', 'equals', 'white'), Net_LDAP2_Filter::create('hairColor', 'equals', 'black'))); $filter2 = Net_LDAP2_Filter::create('givenName', 'not equals', 'Homer'); // all except homer $filter_final = Net_LDAP2_Filter::combine('and', array($filter1, $filter2)); $this->assertEquals(2, $filter1->matches($allEntries)); // kent and homer $this->assertEquals(2, $filter2->matches($allEntries)); // kent and bart $filterresult = array(); $this->assertEquals(1, $filter_final->matches($allEntries, $filterresult)); // should leave only kent $this->assertEquals($entry3->dn(), array_shift($filterresult)->dn(), "Filtered entry does not equal expected entry! filter='" . $filter_final->asString() . "'"); // [TODO]: Further tests for >, <, >=, <= and ~=, when they are implemented. // ...until then: negative testing for those cases foreach (array('>', '<', '>=', '<=', '~=') as $to) { $filter = Net_LDAP2_Filter::parse("(fingers{$to}5)"); $this->assertInstanceOf('PEAR_Error', $filter->matches($allEntries), "Valid operator succeeded: WRITE THE TESTCASE FOR IT!"); } }
function authenticate($login, $password) { if ($login && $password) { if (!function_exists('ldap_connect')) { trigger_error('auth_ldap requires PHP\'s PECL LDAP package installed.'); return FALSE; } if (!(require_once 'Net/LDAP2.php')) { trigger_error('auth_ldap requires the PEAR package Net::LDAP2'); return FALSE; } $debugMode = defined('LDAP_AUTH_DEBUG') ? LDAP_AUTH_DEBUG : FALSE; $anonymousBeforeBind = defined('LDAP_AUTH_ANONYMOUSBEFOREBIND') ? LDAP_AUTH_ANONYMOUSBEFOREBIND : FALSE; $parsedURI = parse_url(LDAP_AUTH_SERVER_URI); if ($parsedURI === FALSE) { $this->_log('Could not parse LDAP_AUTH_SERVER_URI in config.php'); return FALSE; } $ldapConnParams = array('host' => $parsedURI['scheme'] . '://' . $parsedURI['host'], 'basedn' => LDAP_AUTH_BASEDN, 'options' => array('LDAP_OPT_REFERRALS' => 0)); if (!$anonymousBeforeBind) { $ldapConnParams['binddn'] = LDAP_AUTH_BINDDN; $ldapConnParams['bindpw'] = LDAP_AUTH_BINDPW; } $ldapConnParams['starttls'] = defined('LDAP_AUTH_USETLS') ? LDAP_AUTH_USETLS : FALSE; if (is_int($parsedURI['port'])) { $ldapConnParams['port'] = $parsedURI['port']; } $ldapSchemaCacheEnable = defined('LDAP_AUTH_SCHEMA_CACHE_ENABLE') ? LDAP_AUTH_SCHEMA_CACHE_ENABLE : TRUE; $ldapSchemaCacheTimeout = defined('LDAP_AUTH_SCHEMA_CACHE_TIMEOUT') ? LDAP_AUTH_SCHEMA_CACHE_TIMEOUT : 86400; $logAttempts = defined('LDAP_AUTH_LOG_ATTEMPTS') ? LDAP_AUTH_LOG_ATTEMPTS : FALSE; // Making connection to LDAP server if (LDAP_AUTH_ALLOW_UNTRUSTED_CERT === TRUE) { putenv('LDAPTLS_REQCERT=never'); } $ldapConn = Net_LDAP2::connect($ldapConnParams); if (Net_LDAP2::isError($ldapConn)) { $this->_log('Could not connect to LDAP Server: ' . $ldapConn->getMessage()); return FALSE; } // Bind with service account if orignal connexion was anonymous if ($anonymousBeforeBind) { $binding = $ldapConn->bind(LDAP_AUTH_BINDDN, LDAP_AUTH_BINDPW); if (Net_LDAP2::isError($binding)) { $this->_log('Cound not bind service account: ' . $binding->getMessage()); return FALSE; } } //Cache LDAP Schema if ($ldapSchemaCacheEnable) { if (!sys_get_temp_dir()) { $tmpFile = tmp; $tmpDir = dirname($tmpFile); unlink($tmpFile); unset($tmpFile); } else { $tmpDir = sys_get_temp_dir(); } if (empty($parsedURI['port'])) { $ldapPort = $parsedURI['scheme'] == 'ldaps' ? 636 : 389; } else { $ldapPort = $parsedURI['port']; } $cacheFileLoc = $tmpDir . '/ttrss-ldapCache-' . $parsedURI['host'] . ':' . $ldapPort . '.cache'; if ($debugMode) { $this->_log('Schema Cache File: ' . $cacheFileLoc, E_USER_NOTICE); } $schemaCacheConf = array('path' => $cacheFileLoc, 'max_age' => $ldapSchemaCacheTimeout); $schemaCacheObj = new Net_LDAP2_SimpleFileSchemaCache($schemaCacheConf); $ldapConn->registerSchemaCache($schemaCacheObj); $schemaCacheObj->storeSchema($ldapConn->schema()); } //Searching for user $completedSearchFiler = str_replace('???', $login, LDAP_AUTH_SEARCHFILTER); $filterObj = Net_LDAP2_Filter::parse($completedSearchFiler); $searchResults = $ldapConn->search(LDAP_AUTH_BASEDN, $filterObj); if (Net_LDAP2::isError($searchResults)) { $this->_log('LDAP Search Failed: ' . $searchResults->getMessage()); return FALSE; } elseif ($searchResults->count() === 0) { if ($logAttempts) { $this->_logAttempt((string) $login, 'Unknown User'); } return FALSE; } elseif ($searchResults->count() > 1) { $this->_log('Multiple DNs found for username ' . $login); return FALSE; } //Getting user's DN from search $userEntry = $searchResults->shiftEntry(); $userDN = $userEntry->dn(); //Binding with user's DN. $loginAttempt = $ldapConn->bind($userDN, $password); $ldapConn->disconnect(); if ($loginAttempt === TRUE) { if ($logAttempts) { $this->_logAttempt((string) $login, 'successful'); } return $this->base->auto_create_user($login); } elseif ($loginAttempt->getCode() == 49) { if ($logAttempts) { $this->_logAttempt((string) $login, 'bad password'); } return FALSE; } else { $this->_log('Unknown Error: Code: ' . $loginAttempt->getCode() . ' Message: ' . $loginAttempt->getMessage() . ' user(' . (string) $login . ')'); return FALSE; } } return false; }
/** * Main Authentication method * Required for plugin interface * @param unknown $login User's username * @param unknown $password User's password * @return boolean */ function authenticate($login, $password) { if ($login && $password) { if (!function_exists('ldap_connect')) { trigger_error('auth_ldap requires PHP\'s PECL LDAP package installed.'); return FALSE; } if (!(require_once 'Net/LDAP2.php')) { trigger_error('auth_ldap requires the PEAR package Net::LDAP2'); return FALSE; } /** Loading configuration **/ $this->_debugMode = defined('LDAP_AUTH_DEBUG') ? LDAP_AUTH_DEBUG : FALSE; $this->_anonBeforeBind = defined('LDAP_AUTH_ANONYMOUSBEFOREBIND') ? LDAP_AUTH_ANONYMOUSBEFOREBIND : FALSE; $this->_serviceBindDN = defined('LDAP_AUTH_BINDDN') ? LDAP_AUTH_BINDDN : null; $this->_serviceBindPass = defined('LDAP_AUTH_BINDPW') ? LDAP_AUTH_BINDPW : null; $this->_baseDN = defined('LDAP_AUTH_BASEDN') ? LDAP_AUTH_BASEDN : null; if (!defined('LDAP_AUTH_BASEDN')) { $this->_log('LDAP_AUTH_BASEDN is required and not defined.', E_USER_ERROR); return FALSE; } else { $this->_baseDN = LDAP_AUTH_BASEDN; } $parsedURI = parse_url(LDAP_AUTH_SERVER_URI); if ($parsedURI === FALSE) { $this->_log('Could not parse LDAP_AUTH_SERVER_URI in config.php', E_USER_ERROR); return FALSE; } $this->_host = $parsedURI['host']; $this->_scheme = $parsedURI['scheme']; if (is_int($parsedURI['port'])) { $this->_port = $parsedURI['port']; } else { $this->_port = $this->_scheme === 'ldaps' ? 636 : 389; } $this->_useTLS = defined('LDAP_AUTH_USETLS') ? LDAP_AUTH_USETLS : FALSE; $this->_allowUntrustedCerts = defined('LDAP_AUTH_ALLOW_UNTRUSTED_CERT') ? LDAP_AUTH_ALLOW_UNTRUSTED_CERT : FALSE; $this->_schemaCacheEnable = defined('LDAP_AUTH_SCHEMA_CACHE_ENABLE') ? LDAP_AUTH_SCHEMA_CACHE_ENABLE : TRUE; $this->_schemaCacheTimeout = defined('LDAP_AUTH_SCHEMA_CACHE_TIMEOUT') ? LDAP_AUTH_SCHEMA_CACHE_TIMEOUT : 86400; $this->_logAttempts = defined('LDAP_AUTH_LOG_ATTEMPTS') ? LDAP_AUTH_LOG_ATTEMPTS : FALSE; $this->_ldapLoginAttrib = defined('LDAP_AUTH_LOGIN_ATTRIB') ? LDAP_AUTH_LOGIN_ATTRIB : null; /** Building LDAP connection **/ $ldapConnParams = array('host' => $this->_scheme . '://' . $this->_host, 'options' => array('LDAP_OPT_REFERRALS' => 0), 'basedn' => $this->_baseDN, 'port' => $this->_port, 'starttls' => $this->_useTLS); if (!$this->_anonBeforeBind) { $ldapConnParams['binddn'] = $this->_serviceBindDN; $ldapConnParams['bindpw'] = $this->_serviceBindPass; } if ($this->_allowUntrustedCerts) { putenv('LDAPTLS_REQCERT=never'); } if ($this->_debugMode) { $this->_log(print_r($ldapConnParams, TRUE), E_USER_NOTICE); } $ldapConn = Net_LDAP2::connect($ldapConnParams); if (get_class($ldapConn) !== 'Net_LDAP2') { $this->_log('Could not connect to LDAP Server: ' . $ldapConn->getMessage() . ' with ' . $this->_getBindDNWord(), E_USER_ERROR); return FALSE; } else { $this->ldapObj = $ldapConn; $this->_log('Connected to LDAP Server: ' . LDAP_AUTH_SERVER_URI . ' with ' . $this->_getBindDNWord()); } // Bind with service account if orignal connexion was anonymous if ($this->_anonBeforeBind && strlen($this->_bindDN > 0)) { $binding = $this->ldapObj->bind($this->_serviceBindDN, $this->_serviceBindPass); if (get_class($binding) !== 'Net_LDAP2') { $this->_log('Cound not bind service account: ' . $binding->getMessage(), E_USER_ERROR); return FALSE; } else { $this->_log('Bind with ' . $this->_serviceBindDN . ' successful.', E_USER_NOTICE); } } //Cache LDAP Schema if ($ldapSchemaCacheEnable) { $this->_getSchemaCache(); } //Validate BaseDN $baseDNObj = $this->ldapObj->getEntry($this->_baseDN); if (get_class($baseDNObj) !== 'Net_LDAP2_Entry') { $this->_log('Cound not get LDAP_AUTH_BASEDN. Please check config.php', E_USER_ERROR); //return FALSE; } //Searching for user $escapedUserName = Net_LDAP2_Util::escape_filter_value(array($login)); $completedSearchFilter = str_replace('???', $escapedUserName[0], LDAP_AUTH_SEARCHFILTER); $filterObj = Net_LDAP2_Filter::parse($completedSearchFilter); if (get_class($filterObj) !== 'Net_LDAP2_Filter') { $this->_log('Could not parse LDAP Search filter', E_USER_ERROR); return FALSE; } if ($this->_debugMode) { $this->_log("Seaching for user {$login} with this query " . $filterObj->asString() . ' within ' . $this->_baseDN); } $searchResults = $this->ldapObj->search($this->_baseDN, $filterObj); if (get_class($searchResults) !== 'Net_LDAP2_Search') { $this->_log('LDAP Search Failed: ' . $searchResults->getMessage(), E_USER_ERROR); return FALSE; } elseif ($searchResults->count() === 0) { $this->_log((string) $login, 'Unknown User', E_USER_NOTICE); return FALSE; } elseif ($searchResults->count() > 1) { $this->_log('Multiple DNs found for username ' . (string) $login, E_USER_WARNING); return FALSE; } //Getting user's DN from search $userEntry = $searchResults->shiftEntry(); $userDN = $userEntry->dn(); //Binding with user's DN. if ($this->_debugMode) { $this->_log('Try to bind with user\'s DN: ' . $userDN); } $loginAttempt = $this->ldapObj->bind($userDN, $password); if ($loginAttempt === TRUE) { $this->_log('User: '******' authentication successful'); if (strlen($this->_ldapLoginAttrib) > 0) { if ($this->_debugMode) { $this->_log('Looking up TT-RSS username attribute in ' . $this->_ldapLoginAttrib); } $ttrssUsername = $userEntry->getValue($this->_ldapLoginAttrib, 'single'); $this->ldapObj->disconnect(); if (!is_string($ttrssUsername)) { $this->_log('Could not find user name attribute ' . $this->_ldapLoginAttrib . ' in LDAP entry', E_USER_WARNING); return FALSE; } return $this->base->auto_create_user($ttrssUsername); } else { $this->ldapObj->disconnect(); return $this->base->auto_create_user($login); } } elseif ($loginAttempt->getCode() == 49) { $this->ldapObj->disconnect(); $this->_log('User: '******' authentication failed'); return FALSE; } else { $this->ldapObj->disconnect(); $this->_log('Unknown Error: Code: ' . $loginAttempt->getCode() . ' Message: ' . $loginAttempt->getMessage() . ' user(' . (string) $login . ')', E_USER_WARNING); return FALSE; } } return false; }