public function LDAP_Filter_factory($ldap, $attr_name, $match, $value = '', $escape = true) { if ($ldap instanceof Net_LDAP2) { return Net_LDAP2_Filter::create($attr_name, $match, $value, $escape); } else { return Net_LDAP_Filter::create($attr_name, $match, $value, $escape); } }
public function getByEmail($usr) { $filter = Net_LDAP2_Filter::create('mail', 'equals', $usr->email); $requested_attributes = array('cn', 'uid', 'mail'); $search = $this->connect()->search($this->basedn, $filter, array('attributes' => $requested_attributes)); if (Misc::isError($search)) { $entry = $search; error_log($entry->getCode() . ': ' . $entry->getMessage()); return null; } if ($search->count() <= 0) { return false; } $entry = $search->current(); $usr->uid = $entry->get_value('uid'); $usr->full_name = $entry->get_value('cn'); return true; }
/** * get an LDAP entry for a user with a given username * * @param string $username * $param array $attributes LDAP attributes to retrieve * @return string DN */ function get_user($username, $attributes = array()) { $ldap = $this->get_ldap_connection(); $filter = Net_LDAP2_Filter::create($this->attributes['username'], 'equals', $username); $options = array('attributes' => $attributes); $search = $ldap->search(null, $filter, $options); if (PEAR::isError($search)) { common_log(LOG_WARNING, 'Error while getting DN for user: '******'Found ' . $search->count() . ' ldap user with the username: ' . $username); return false; } } }
public function get_groups($force_reload = false) { $this->get_user_attributes($force_reload); // ensure we have a connection to the ldap server if ($this->bind() != 'LDAP_SUCCESS') { $this->add_log('ldap', 'Reuse of ldap connection failed: ' . $this->ldaplink->getMessage() . ' at line ' . __LINE__ . ' in ' . __FILE__); return false; } $filter1 = Net_LDAP2_Filter::create('objectClass', 'equals', $this->options['groupoc']); if (!empty($this->options['groupmemberattr'])) { // get membership from group information if ($this->options['groupmemberisdn']) { if ($this->user_attributes['dn'] == null) { return false; } $filter2 = Net_LDAP2_Filter::create($this->options['groupmemberattr'], 'equals', $this->user_dn()); } else { $filter2 = Net_LDAP2_Filter::create($this->options['groupmemberattr'], 'equals', $this->options['username']); } $filter = Net_LDAP2_Filter::combine('and', array($filter1, $filter2)); } else { if (!empty($this->options['usergroupattr'])) { // get membership from user information $ugi =& $this->user_attributes[$this->options['usergroupattr']]; if (!empty($ugi)) { if (!is_array($ugi)) { $ugi = array($ugi); } if (count($ugi) == 1) { // one gid $filter3 = Net_LDAP2_Filter::create($this->options['groupgroupattr'], 'equals', $ugi[0]); } else { // mor gids $filtertmp = array(); foreach ($ugi as $g) { $filtertmp[] = Net_LDAP2_Filter::create($this->options['groupgroupattr'], 'equals', $g); } $filter3 = Net_LDAP2_Filter::combine('or', $filtertmp); } $filter = Net_LDAP2_Filter::combine('and', array($filter1, $filter3)); } else { // User has no group $filter = NULL; } } else { // not possible to get groups - return empty array return array(); } } if (Net_LDAP2::isError($filter)) { $this->add_log('ldap', 'LDAP Filter creation error: ' . $filter->getMessage() . ' at line ' . __LINE__ . ' in ' . __FILE__); return false; } $this->add_log('ldap', 'Searching for group entries with filter: ' . $filter->asString() . ' base ' . $this->groupbase_dn() . ' at line ' . __LINE__ . ' in ' . __FILE__); $searchoptions = array('scope' => $this->options['scope']); $searchresult = $this->ldaplink->search($this->groupbase_dn(), $filter, $searchoptions); if (Net_LDAP2::isError($searchresult)) { $this->add_log('ldap', 'Search failed: ' . $searchresult->getMessage() . ' at line ' . __LINE__ . ' in ' . __FILE__); return false; } $this->add_log('ldap', 'Found ' . $searchresult->count() . ' entries. Extracting entries now.'); $this->groups = array(); while ($entry = $searchresult->shiftEntry()) { if (Net_LDAP2::isError($entry)) { $this->add_log('ldap', 'Error fetching group entries: ' . $entry->getMessage() . ' at line ' . __LINE__ . ' in ' . __FILE__); return false; } $this->groups[$entry->dn()] = $entry->getValues(); // no error checking necessary here } $this->add_log('ldap', count($this->groups) . ' groups found at line ' . __LINE__ . ' in ' . __FILE__); return $this->groups; }
/** * @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); } }
/** * Check if $user and $password are related to a valid user and password * * @param string $check_password * @return boolean */ function isValidPasswordLdap($user, $password, $config) { // Connecting using the configuration: require_once "Net/LDAP2.php"; $ldap = Net_LDAP2::connect($config); // Testing for connection error if (PEAR::isError($ldap)) { return false; } $filter = Net_LDAP2_Filter::create($config['uid'], 'equals', $user); $search = $ldap->search(null, $filter, null); if (Net_LDAP2::isError($search)) { return false; } if ($search->count() != 1) { return false; } // User exists so we may rebind to authenticate the password $entries = $search->entries(); $bind_result = $ldap->bind($entries[0]->dn(), $password); if (PEAR::isError($bind_result)) { return false; } return true; }
/** * 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; }
/** * Haal de rollen van de huidige gebruiker op voor een opgegeven applicatie naam * In onze LDAP server vertegenwoordigd het veld description de volledige naam van de applicatie * * @param KVDutil_Auth_Gebruiker $gebruiker * @param string $applicatieNaam * structuur: 'ou='.$applicatieNaam.',ou=productie,ou=groups,dc=vioe,dc=be' * @return KVDutil_AuthRolCollectie $rollen */ public function getRollenVoorApplicatieNaam(KVDutil_Auth_Gebruiker $gebruiker, $applicatieNaam) { $filter = Net_LDAP2_Filter::create($this->parameters['gebruiker_bij_rol'], 'contains', $gebruiker->getId()); $options = array('scope' => 'sub', 'attributes' => array($this->parameters['rol_naam'], $this->parameters['rol_beschrijving'])); //Voer zoekactie uit op boven meegegeven searchbase met de opgegeven options en filters $search = $this->connectie->search($applicatieNaam, $filter, $options); if (Net_LDAP2::isError($search)) { throw new Exception($search->getMessage()); } $results = array(); //objecten worden 1 voor 1 volledig geladen en in een array geplaatst. foreach ($search as $dn => $entry) { $results[$dn] = new KVDutil_Auth_Rol($dn, $entry->getValue($this->parameters['rol_naam'], 'single'), $entry->getValue($this->parameters['rol_beschrijving'], 'single')); } //De array met objecten wordt in een KVDdom_DomainObjectCollection geplaatst. return new KVDutil_Auth_RolCollectie($results); }
/** * 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!"); } }
/** * testSearch(). */ public function testSearch() { if (!$this->ldapcfg) { $this->markTestSkipped('No ldapconfig.ini found. Skipping test!'); } else { $ldap = $this->connect(); // some testdata, so we can test sizelimit $base = $this->ldapcfg['global']['server_base_dn']; $ou1 = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_search1,' . $base, array('objectClass' => array('top', 'organizationalUnit'), 'ou' => 'Net_LDAP2_Test_search1')); $ou1_1 = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_search1_1,' . $ou1->dn(), array('objectClass' => array('top', 'organizationalUnit'), 'ou' => 'Net_LDAP2_Test_search2')); $ou2 = Net_LDAP2_Entry::createFresh('ou=Net_LDAP2_Test_search2,' . $base, array('objectClass' => array('top', 'organizationalUnit'), 'ou' => 'Net_LDAP2_Test_search2')); $this->assertTrue($ldap->add($ou1)); $this->assertTrue($ldap->dnExists($ou1->dn())); $this->assertTrue($ldap->add($ou1_1)); $this->assertTrue($ldap->dnExists($ou1_1->dn())); $this->assertTrue($ldap->add($ou2)); $this->assertTrue($ldap->dnExists($ou2->dn())); // Search for testfilter, should at least return our two test entries $res = $ldap->search(null, '(ou=Net_LDAP2*)', array('attributes' => '1.1')); $this->assertInstanceOf('Net_LDAP2_Search', $res); $this->assertThat($res->count(), $this->greaterThanOrEqual(2)); // Same, but with Net_LDAP2_Filter object $filtero = Net_LDAP2_Filter::create('ou', 'begins', 'Net_LDAP2'); $this->assertInstanceOf('Net_LDAP2_Filter', $filtero); $res = $ldap->search(null, $filtero, array('attributes' => '1.1')); $this->assertInstanceOf('Net_LDAP2_Search', $res); $this->assertThat($res->count(), $this->greaterThanOrEqual(2)); // Search using default filter for base-onelevel scope // should at least return our two test entries $res = $ldap->search(null, null, array('scope' => 'one', 'attributes' => '1.1')); $this->assertInstanceOf('Net_LDAP2_Search', $res); $this->assertThat($res->count(), $this->greaterThanOrEqual(2)); // Base-search using custom base (string) // should only return the test entry $ou1 and not the entry below it. $res = $ldap->search($ou1->dn(), null, array('scope' => 'base', 'attributes' => '1.1')); $this->assertInstanceOf('Net_LDAP2_Search', $res); $this->assertEquals(1, $res->count()); // Search using custom base, this time using an entry object // This tests if passing an entry object as base works // should only return the test entry $ou1 $res = $ldap->search($ou1, '(ou=*)', array('scope' => 'base', 'attributes' => '1.1')); $this->assertInstanceOf('Net_LDAP2_Search', $res); $this->assertEquals(1, $res->count()); // Search using default filter for base-onelevel scope with sizelimit // should of course return more than one entry, // but not more than sizelimit $res = $ldap->search(null, null, array('scope' => 'one', 'sizelimit' => 1, 'attributes' => '1.1')); $this->assertInstanceOf('Net_LDAP2_Search', $res); $this->assertEquals(1, $res->count()); $this->assertTrue($res->sizeLimitExceeded()); // sizelimit should be exceeded now // Bad filter $res = $ldap->search(null, 'somebadfilter', array('attributes' => '1.1')); $this->assertInstanceOf('Net_LDAP2_Error', $res); // Bad base $res = $ldap->search('badbase', null, array('attributes' => '1.1')); $this->assertInstanceOf('Net_LDAP2_Error', $res); // Passing Error object as base and as filter object $error = new Net_LDAP2_Error('Testerror'); $res = $ldap->search($error, null, array('attributes' => '1.1')); $this->assertInstanceOf('Net_LDAP2_Error', $res); $res = $ldap->search(null, $error, array('attributes' => '1.1')); $this->assertInstanceOf('Net_LDAP2_Error', $res); // Nullresult $res = $ldap->search(null, '(cn=nevermatching_filter)', array('scope' => 'base', 'attributes' => '1.1')); $this->assertInstanceOf('Net_LDAP2_Search', $res); $this->assertEquals(0, $res->count()); // cleanup $this->assertTrue($ldap->delete($ou1_1), 'Cleanup failed, please delete manually'); $this->assertTrue($ldap->delete($ou1), 'Cleanup failed, please delete manually'); $this->assertTrue($ldap->delete($ou2), 'Cleanup failed, please delete manually'); } }
// We use the Net_LDAP2_Filter class for this purpose, // because so we don't need to worry about // RFC-2254 ;) // In this example, we want all users with first names // starting with "bened" and the last names ending with "ger". // Additionally, we want to exclude all users with names // containing "smith", which will be done throug a "negation". // Basic filter building $filter_sn = Net_LDAP2_Filter::create('gn', 'begins', 'bened'); $filter_gn = Net_LDAP2_Filter::create('sn', 'ends', 'ger'); // Building and negating the "no smith" filter component // this must be done in two steps, because // you are able to negate EVERY filter, not just leave filters. // the $filter_smith will not be used afterwards and is only // necessary for negation here. $filter_smith = Net_LDAP2_Filter::create('sn', 'contains', 'smith'); $filter_nosmith = Net_LDAP2_Filter::combine('not', $filter_smith); // Now combine all filter components to build our search filter $filter = Net_LDAP2_Filter::combine('and', array($filter_sn, $filter_gn, $filter_nosmith)); // The filter is ready now, so we can // use this filter now to search for entries. // The scope we use is "sub", meaning "all entries below the search base". // The base is "null", meaning the base defined in $ldap_config. This is similar // to call $ldap->search($ldap_config['base'], ... $requested_attributes = array('sn', 'gn', 'telephonenumber'); $search = $ldap->search(null, $filter, array('attributes' => $requested_attributes)); if (Net_LDAP2::isError($search)) { die('LDAP search failed: ' . $search->getMessage()); } // Lets see what entries we got and print the names and telephone numbers: if ($search->count() > 0) {