Example #1
0
 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;
 }
Example #2
0
 /**
  * @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;
 }
Example #4
0
 /**
  * Constructor of a new part of a LDAP filter.
  *
  * The following matching rules exists:
  *    - equals:         One of the attributes values is exactly $value
  *                      Please note that case sensitiviness is depends on the
  *                      attributes syntax configured in the server.
  *    - begins:         One of the attributes values must begin with $value
  *    - ends:           One of the attributes values must end with $value
  *    - contains:       One of the attributes values must contain $value
  *    - present | any:  The attribute can contain any value but must be existent
  *    - greater:        The attributes value is greater than $value
  *    - less:           The attributes value is less than $value
  *    - greaterOrEqual: The attributes value is greater or equal than $value
  *    - lessOrEqual:    The attributes value is less or equal than $value
  *    - approx:         One of the attributes values is similar to $value
  *
  * Negation ("not") can be done by prepending the above operators with the
  * "not" or "!" keyword, see example below.
  *
  * If $escape is set to true (default) then $value will be escaped
  * properly. If it is set to false then $value will be treaten as raw filter value string.
  * You should escape yourself using {@link Net_LDAP2_Util::escape_filter_value()}!
  *
  * Examples:
  * <code>
  *   // This will find entries that contain an attribute "sn" that ends with "foobar":
  *   $filter = Net_LDAP2_Filter::create('sn', 'ends', 'foobar');
  *
  *   // This will find entries that contain an attribute "sn" that has any value set:
  *   $filter = Net_LDAP2_Filter::create('sn', 'any');
  *
  *   // This will build a negated equals filter:
  *   $filter = Net_LDAP2_Filter::create('sn', 'not equals', 'foobar');
  * </code>
  *
  * @param string  $attr_name Name of the attribute the filter should apply to
  * @param string  $match     Matching rule (equals, begins, ends, contains, greater, less, greaterOrEqual, lessOrEqual, approx, any)
  * @param string  $value     (optional) if given, then this is used as a filter
  * @param boolean $escape    Should $value be escaped? (default: yes, see {@link Net_LDAP2_Util::escape_filter_value()} for detailed information)
  *
  * @return Net_LDAP2_Filter|Net_LDAP2_Error
  */
 public static function &create($attr_name, $match, $value = '', $escape = true)
 {
     $leaf_filter = new Net_LDAP2_Filter();
     if ($escape) {
         $array = Net_LDAP2_Util::escape_filter_value(array($value));
         $value = $array[0];
     }
     $match = strtolower($match);
     // detect negation
     $neg_matches = array();
     $negate_filter = false;
     if (preg_match('/^(?:not|!)[\\s_-](.+)/', $match, $neg_matches)) {
         $negate_filter = true;
         $match = $neg_matches[1];
     }
     // build basic filter
     switch ($match) {
         case 'equals':
         case '=':
         case '==':
             $leaf_filter->_filter = '(' . $attr_name . '=' . $value . ')';
             break;
         case 'begins':
             $leaf_filter->_filter = '(' . $attr_name . '=' . $value . '*)';
             break;
         case 'ends':
             $leaf_filter->_filter = '(' . $attr_name . '=*' . $value . ')';
             break;
         case 'contains':
             $leaf_filter->_filter = '(' . $attr_name . '=*' . $value . '*)';
             break;
         case 'greater':
         case '>':
             $leaf_filter->_filter = '(' . $attr_name . '>' . $value . ')';
             break;
         case 'less':
         case '<':
             $leaf_filter->_filter = '(' . $attr_name . '<' . $value . ')';
             break;
         case 'greaterorequal':
         case '>=':
             $leaf_filter->_filter = '(' . $attr_name . '>=' . $value . ')';
             break;
         case 'lessorequal':
         case '<=':
             $leaf_filter->_filter = '(' . $attr_name . '<=' . $value . ')';
             break;
         case 'approx':
         case '~=':
             $leaf_filter->_filter = '(' . $attr_name . '~=' . $value . ')';
             break;
         case 'any':
         case 'present':
             // alias that may improve user code readability
             $leaf_filter->_filter = '(' . $attr_name . '=*)';
             break;
         default:
             return PEAR::raiseError('Net_LDAP2_Filter create error: matching rule "' . $match . '" not known!');
     }
     // negate if requested
     if ($negate_filter) {
         $leaf_filter = Net_LDAP2_Filter::combine('!', $leaf_filter);
     }
     return $leaf_filter;
 }
 /**
  * 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!");
     }
 }
Example #6
0
// 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) {
    echo "Found " . $search->count() . ' entries:<br>';
    // Note, this is is only one of several ways to fetch entries!
    // You can also retrieve all entries in an array with