24define(
'IL_LDAP_BIND_DEFAULT',0);
 
   25define(
'IL_LDAP_BIND_ADMIN',1);
 
   26define(
'IL_LDAP_BIND_TEST',2);
 
   27define(
'IL_LDAP_BIND_AUTH', 10);
 
   29include_once(
'Services/LDAP/classes/class.ilLDAPAttributeMapping.php');
 
   30include_once(
'Services/LDAP/classes/class.ilLDAPResult.php');
 
   31include_once(
'Services/LDAP/classes/class.ilLDAPQueryException.php');
 
   68                        $this->ldap_server_url = $a_url;
 
   72                        $this->ldap_server_url = $this->
settings->getUrl();
 
   76                $this->log = 
$GLOBALS[
'DIC']->logger()->auth();
 
  132                if(strlen($this->
settings->getGroupName()))
 
  134                        $this->log->debug(
'Searching for group members.');
 
  136                        $groups = $this->
settings->getGroupNames();
 
  137                        if(count($groups) <= 1)
 
  143                                foreach($groups as $group)
 
  150                if(!strlen($this->
settings->getGroupName()) or $this->settings->isMembershipOptional())
 
  152                        $this->log->info(
'Start reading all users...');
 
  154                        #throw new ilLDAPQueryException('LDAP: Called import of users without specifying group restrictions. NOT IMPLEMENTED YET!'); 
  156                return $this->users ? $this->users : array();
 
  170        public function query($a_search_base,$a_filter,$a_scope,$a_attributes)
 
  172                $res = $this->
queryByScope($a_scope,$a_search_base,$a_filter,$a_attributes);
 
  176                                sprintf(
'DN: %s, Filter: %s, Scope: %s',
 
  190        public function modAdd($a_dn,$a_attribute)
 
  192                if(@ldap_mod_add($this->lh,$a_dn,$a_attribute))
 
  207                if(@ldap_mod_del($this->lh,$a_dn,$a_attribute))
 
  225                if(($dn = $this->
settings->getSearchBase()) && substr($dn,-1) != 
',')
 
  229                $dn .=  $this->
settings->getBaseDN();
 
  232                $filter = $this->
settings->getFilter();
 
  233                $page_filter = array(
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'-');
 
  234                $chars = array(
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z');
 
  236                foreach($page_filter as $letter)
 
  239                        $new_filter .= $filter;
 
  244                                        $new_filter .= (
'(!(|');
 
  245                                        foreach($chars as $char)
 
  247                                                $new_filter .= (
'('.$this->settings->getUserAttribute().
'='.$char.
'*)');
 
  249                                        $new_filter .= 
')))';
 
  253                                        $new_filter .= (
'('.$this->settings->getUserAttribute().
'='.$letter.
'*))');
 
  257                        $this->log->info(
'Searching with ldap search and filter '.$new_filter.
' in '.$dn);
 
  261                                array($this->
settings->getUserAttribute()));
 
  264                        if(!$tmp_result->numRows())
 
  266                                $this->log->notice(
'No users found. Aborting.');
 
  269                        $this->log->info(
'Found '.$tmp_result->numRows().
' users.');
 
  270                        $attribute = strtolower($this->
settings->getUserAttribute());
 
  271                        foreach($tmp_result->getRows() as 
$data)
 
  273                                if(isset(
$data[$attribute]))
 
  279                                        $this->log->warning(
'Unknown error. No user attribute found.');
 
  295                $group_names = $this->
getServer()->getGroupNames();
 
  297                if(!count($group_names))
 
  299                        $this->
getLogger()->debug(
'No LDAP group restrictions found');
 
  303                $group_dn = $this->
getServer()->getGroupDN();
 
  306                        (substr($group_dn, -1) != 
',')
 
  311                $group_dn .= $this->
getServer()->getBaseDN();
 
  313                foreach($group_names as $group)
 
  315                        $user = $a_ldap_user_name;
 
  316                        if($this->
getServer()->enabledGroupMemberIsDN())
 
  318                                $user = $ldap_user_data[
'dn'];
 
  321                        $filter = 
sprintf(
'(&(%s=%s)(%s=%s)%s)',
 
  328                        $this->
getLogger()->debug(
'Current group search base: ' . $group_dn);
 
  329                        $this->
getLogger()->debug(
'Current group filter: ' . $filter);
 
  341                        $group_result = $tmp_result->getRows();
 
  343                        $this->
getLogger()->debug(
'Group query returned: ');
 
  346                        if(count($group_result))
 
  353                if($this->
getServer()->isMembershipOptional())
 
  355                        $this->
getLogger()->debug(
'Group restrictions failed, checking user filter.');
 
  358                                $this->
getLogger()->debug(
'User filter matches.');
 
  362                $this->
getLogger()->debug(
'Group restrictions failed.');
 
  375                $group_name = strlen($a_name) ? $a_name : $this->
settings->getGroupName();
 
  378                $filter = 
sprintf(
'(&(%s=%s)%s)',
 
  379                        $this->
settings->getGroupAttribute(),
 
  381                        $this->settings->getGroupFilter());
 
  385                if(($gdn = $this->
settings->getGroupDN()) && substr($gdn,-1) != 
',')
 
  389                $gdn .= $this->
settings->getBaseDN();
 
  391                $this->log->debug(
'Using filter '.$filter);
 
  392                $this->log->debug(
'Using DN '.$gdn);
 
  396                        array($this->
settings->getGroupMember()));
 
  399                $group_data = $tmp_result->getRows();
 
  402                if(!$tmp_result->numRows())
 
  404                        $this->log->info(
'No group found.');
 
  408                $attribute_name = strtolower($this->
settings->getGroupMember());
 
  411                foreach($group_data as 
$data)
 
  413                        $this->log->debug(
'Found '.count(
$data[$attribute_name]).
' group members for group '.
$data[
'dn']);
 
  414                        if(is_array(
$data[$attribute_name]))
 
  416                                foreach(
$data[$attribute_name] as $name)
 
  436        private function readUserData($a_name,$a_check_dn = 
false,$a_try_group_user_filter = 
false)
 
  438                $filter = $this->
settings->getFilter();
 
  439                if($a_try_group_user_filter)
 
  441                        if($this->
settings->isMembershipOptional())
 
  443                                $filter = $this->
settings->getGroupUserFilter();
 
  448                if($this->
settings->enabledGroupMemberIsDN() and $a_check_dn)
 
  451                        #$res = $this->queryByScope(IL_LDAP_SCOPE_BASE,$dn,$filter,$this->user_fields); 
  453                        $fields = array_merge($this->user_fields,array(
'useraccountcontrol'));
 
  458                        $filter = 
sprintf(
'(&(%s=%s)%s)',
 
  459                                $this->
settings->getUserAttribute(),
 
  464                        if(($dn = $this->
settings->getSearchBase()) && substr($dn,-1) != 
',')
 
  468                        $dn .=  $this->
settings->getBaseDN();
 
  469                        $fields = array_merge($this->user_fields,array(
'useraccountcontrol'));
 
  475                if(!$tmp_result->numRows())
 
  477                        $this->log->info(
'LDAP: No user data found for: '.$a_name);
 
  482                if($user_data = $tmp_result->get())
 
  484                        if(isset($user_data[
'useraccountcontrol']))
 
  486                                if(($user_data[
'useraccountcontrol'] & 0x02))
 
  488                                        $this->log->notice(
'LDAP: '.$a_name.
' account disabled.');
 
  493                        $account = $user_data[strtolower($this->
settings->getUserAttribute())];
 
  494                        if(is_array($account))
 
  496                                $user_ext = strtolower(array_shift($account));
 
  500                                $user_ext = strtolower($account);
 
  504                        $auth_mode = $this->
settings->getAuthenticationMappingKey();
 
  506                        $this->users[$user_ext] = $user_data;
 
  517                return $this->
settings->getAuthenticationMappingKey();
 
  529        private function queryByScope($a_scope,$a_base_dn,$a_filter,$a_attributes)
 
  531                $a_filter = $a_filter ? $a_filter : 
"(objectclass=*)";
 
  536                                $res = @ldap_search($this->lh,$a_base_dn,$a_filter,$a_attributes);
 
  540                                $res = @ldap_list($this->lh,$a_base_dn,$a_filter,$a_attributes);
 
  545                                $res = @ldap_read($this->lh,$a_base_dn,$a_filter,$a_attributes);
 
  549                                $this->log->warning(
"LDAP: LDAPQuery: Unknown search scope");
 
  552                $error = ldap_error($this->lh);
 
  553                if(strcmp(
'Success', 
$error) !== 0)
 
  556                        $this->
getLogger()->warning(
'Base DN:' . $a_base_dn);
 
  557                        $this->
getLogger()->warning(
'Filter: ' . $a_filter);
 
  573                $this->lh = @ldap_connect($this->ldap_server_url);
 
  581                if(!ldap_set_option($this->lh,LDAP_OPT_PROTOCOL_VERSION,$this->
settings->getVersion()))
 
  586                if($this->
settings->isActiveReferrer())
 
  588                        if(!ldap_set_option($this->lh,LDAP_OPT_REFERRALS,
true))
 
  592                        #@ldap_set_rebind_proc($this->lh,'referralRebind'); 
  596                        ldap_set_option($this->lh,LDAP_OPT_REFERRALS,
false);
 
  597                        $this->log->debug(
'Switching referrals to false.');
 
  602                        if(!ldap_start_tls($this->lh))
 
  619                switch($a_binding_type)
 
  628                                        strlen($this->
settings->getBindUser())
 
  631                                        $user = $this->
settings->getBindUser();
 
  634                                        define(
'IL_LDAP_REBIND_USER',$user);
 
  635                                        define(
'IL_LDAP_REBIND_PASS',
$pass);
 
  636                                        $this->log->debug(
'Bind as '.$user);
 
  641                                        $this->log->debug(
'Bind anonymous');
 
  646                                $user = $this->
settings->getRoleBindDN();
 
  649                                if(!strlen($user) or !strlen(
$pass))
 
  651                                        $user = $this->
settings->getBindUser();
 
  655                                define(
'IL_LDAP_REBIND_USER',$user);
 
  656                                define(
'IL_LDAP_REBIND_PASS',
$pass);
 
  660                                $this->log->debug(
'Trying to bind as: ' . $a_user_dn);
 
  670                if(!@ldap_bind($this->lh,$user,
$pass))
 
  672                        throw new ilLDAPQueryException(
'LDAP: Cannot bind as '.$user.
' with message: '. ldap_err2str(ldap_errno($this->lh)).
' Trying fallback...', ldap_errno($this->lh));
 
  676                        $this->log->debug(
'Bind successful.');
 
  689                include_once(
'Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php');
 
  691                $this->user_fields = array_merge(
 
  692                        array($this->
settings->getUserAttribute()),
 
  694                        $this->mapping->getFields(),
 
  711                        @ldap_unbind($this->lh);
 
  727                        @ldap_unbind($this->lh);
 
  736        $ilLog->write(
'LDAP: Called referralRebind.');
 
  738        ldap_set_option($a_ds, LDAP_OPT_PROTOCOL_VERSION, 3);
 
  740        if (!ldap_bind($a_ds,IL_LDAP_REBIND_USER,IL_LDAP_REBIND_PASS))
 
  742                $ilLog->write(
'LDAP: Rebind failed');
 
sprintf('%.4f', $callTime)
An exception for terminatinating execution or to throw for unit testing.
const IL_LDAP_BIND_DEFAULT
referralRebind($a_ds, $a_url)
static _getInstanceByServerId($a_server_id)
Get instance of class.
modAdd($a_dn, $a_attribute)
Add value to an existing attribute.
fetchUsers()
Fetch all users.
queryByScope($a_scope, $a_base_dn, $a_filter, $a_attributes)
Query by scope IL_SCOPE_SUB => ldap_search IL_SCOPE_ONE => ldap_list.
fetchGroupMembers($a_name='')
Fetch group member ids.
fetchUserProfileFields()
fetch required fields of user profile data
bind($a_binding_type=IL_LDAP_BIND_DEFAULT, $a_user_dn='', $a_password='')
Bind to LDAP server.
connect()
Connect to LDAP server.
parseAuthMode()
Parse authentication mode.
readUserData($a_name, $a_check_dn=false, $a_try_group_user_filter=false)
Read user data.
__destruct()
Destructor unbind from ldap server.
readAllUsers()
Fetch all users This function splits the query to filters like e.g (uid=a*) (uid=b*)....
__construct(ilLDAPServer $a_server, $a_url='')
Constructur.
fetchUser($a_name)
Get one user by login name.
checkGroupMembership($a_ldap_user_name, $ldap_user_data)
check group membership
query($a_search_base, $a_filter, $a_scope, $a_attributes)
Perform a query.
modDelete($a_dn, $a_attribute)
Delete value from an existing attribute.
static getAttributeNames($a_server_id)
get all possible attribute names
const DEFAULT_NETWORK_TIMEOUT
static _checkExternalAuthAccount($a_auth, $a_account)
check whether external account and authentication method matches with a user
$GLOBALS['loaded']
Global hash that tracks already loaded includes.