24 define(
'IL_LDAP_BIND_DEFAULT',0);
25 define(
'IL_LDAP_BIND_ADMIN',1);
26 define(
'IL_LDAP_BIND_TEST',2);
28 include_once(
'Services/LDAP/classes/class.ilLDAPAttributeMapping.php');
29 include_once(
'Services/LDAP/classes/class.ilLDAPResult.php');
30 include_once(
'Services/LDAP/classes/class.ilLDAPQueryException.php');
61 $this->settings = $a_server;
65 $this->ldap_server_url = $a_url;
69 $this->ldap_server_url = $this->settings->
getUrl();
88 $this->user_fields = array_merge(array($this->settings->getUserAttribute()),$this->mapping->getFields());
112 if(strlen($this->settings->getGroupName()))
114 $this->log->write(__METHOD__.
': Searching for group members.');
116 $groups = $this->settings->getGroupNames();
117 if(count($groups) <= 1)
123 foreach($groups as $group)
130 if(!strlen($this->settings->getGroupName()) or $this->settings->isMembershipOptional())
132 $this->log->write(__METHOD__.
': Start reading all users...');
134 #throw new ilLDAPQueryException('LDAP: Called import of users without specifying group restrictions. NOT IMPLEMENTED YET!');
136 return $this->users ? $this->users : array();
150 public function query($a_search_base,$a_filter,$a_scope,$a_attributes)
152 $res = $this->
queryByScope($a_scope,$a_search_base,$a_filter,$a_attributes);
156 sprintf(
'DN: %s, Filter: %s, Scope: %s',
170 public function modAdd($a_dn,$a_attribute)
172 if(@ldap_mod_add($this->lh,$a_dn,$a_attribute))
187 if(@ldap_mod_del($this->lh,$a_dn,$a_attribute))
205 if(($dn = $this->settings->getSearchBase()) && substr($dn,-1) !=
',')
209 $dn .= $this->settings->getBaseDN();
212 $filter = $this->settings->getFilter();
213 $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',
'-');
214 $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');
216 foreach($page_filter as $letter)
219 $new_filter .= $filter;
224 $new_filter .= (
'(!(|');
225 foreach($chars as $char)
227 $new_filter .= (
'('.$this->settings->getUserAttribute().
'='.$char.
'*)');
229 $new_filter .=
')))';
233 $new_filter .= (
'('.$this->settings->getUserAttribute().
'='.$letter.
'*))');
237 $this->log->write(__METHOD__.
': Searching with ldap search and filter '.$new_filter.
' in '.$dn);
241 array($this->settings->getUserAttribute()));
244 if(!$tmp_result->numRows())
246 $this->log->write(__METHOD__.
': No users found. Aborting.');
249 $this->log->write(__METHOD__.
': Found '.$tmp_result->numRows().
' users.');
250 foreach($tmp_result->getRows() as
$data)
252 if(isset(
$data[$this->settings->getUserAttribute()]))
258 $this->log->write(__METHOD__.
': Unknown error. No user attribute found.');
275 $group_name = strlen($a_name) ? $a_name : $this->settings->getGroupName();
278 $filter = sprintf(
'(&(%s=%s)%s)',
279 $this->settings->getGroupAttribute(),
281 $this->settings->getGroupFilter());
285 if(($gdn = $this->settings->getGroupDN()) && substr($gdn,-1) !=
',')
289 $gdn .= $this->settings->getBaseDN();
291 $this->log->write(
'LDAP: Using filter '.$filter);
292 $this->log->write(
'LDAP: Using DN '.$gdn);
296 array($this->settings->getGroupMember()));
299 $group_data = $tmp_result->getRows();
302 if(!$tmp_result->numRows())
304 $this->log->write(__METHOD__.
': No group found.');
308 $attribute_name = strtolower($this->settings->getGroupMember());
311 foreach($group_data as
$data)
313 $this->log->write(__METHOD__.
': found '.count($data[$attribute_name]).
' group members for group '.$data[
'dn']);
314 if(is_array($data[$attribute_name]))
316 foreach($data[$attribute_name] as $name)
336 private function readUserData($a_name,$a_check_dn =
true,$a_try_group_user_filter =
false)
338 $filter = $this->settings->getFilter();
339 if($a_try_group_user_filter)
341 if($this->settings->isMembershipOptional())
343 $filter = $this->settings->getGroupUserFilter();
348 if($this->settings->enabledGroupMemberIsDN() and $a_check_dn)
355 $filter = sprintf(
'(&(%s=%s)%s)',
356 $this->settings->getUserAttribute(),
361 if(($dn = $this->settings->getSearchBase()) && substr($dn,-1) !=
',')
365 $dn .= $this->settings->getBaseDN();
366 $fields = array_merge($this->user_fields,array(
'useraccountcontrol'));
367 $res = $this->
queryByScope($this->settings->getUserScope(),strtolower($dn),$filter,$fields);
372 if(!$tmp_result->numRows())
374 $this->log->write(
'LDAP: No user data found for: '.$a_name);
379 if($user_data = $tmp_result->get())
381 if(isset($user_data[
'useraccountcontrol']))
383 if(($user_data[
'useraccountcontrol'] & 0x02))
385 $this->log->write(__METHOD__.
': '.$a_name.
' account disabled.');
390 $user_ext = $user_data[strtolower($this->settings->getUserAttribute())];
392 $this->users[$user_ext] = $user_data;
406 private function queryByScope($a_scope,$a_base_dn,$a_filter,$a_attributes)
411 #$this->log->write('LDAP: Scope is: sub, using ldap_search');
412 $res = @ldap_search($this->lh,$a_base_dn,$a_filter,$a_attributes);
416 #$this->log->write('LDAP: Scope is: one, using ldap_list');
417 $res = @ldap_list($this->lh,$a_base_dn,$a_filter,$a_attributes);
421 $res = @ldap_read($this->lh,$a_base_dn,$a_filter,$a_attributes);
425 $this->log->write(
"LDAP: LDAPQuery: Unknown search scope");
441 $this->lh = @ldap_connect($this->ldap_server_url);
446 throw new ilLDAPQueryException(
"LDAP: Cannot connect to LDAP Server: ".$this->settings->getUrl());
449 if(!ldap_set_option($this->lh,LDAP_OPT_PROTOCOL_VERSION,$this->settings->getVersion()))
454 if($this->settings->isActiveReferrer())
456 if(!ldap_set_option($this->lh,LDAP_OPT_REFERRALS,
true))
460 #@ldap_set_rebind_proc($this->lh,'referralRebind');
464 ldap_set_option($this->lh,LDAP_OPT_REFERRALS,
false);
465 $this->log->write(__METHOD__.
': Switching referrals to false.');
468 if($this->settings->isActiveTLS())
470 if(!ldap_start_tls($this->lh))
487 switch($a_binding_type)
491 if(strlen($this->settings->getBindUser()))
493 $user = $this->settings->getBindUser();
494 $pass = $this->settings->getBindPassword();
496 define(
'IL_LDAP_REBIND_USER',
$user);
497 define(
'IL_LDAP_REBIND_PASS',
$pass);
498 $this->log->write(__METHOD__.
': Bind as '.
$user);
503 $this->log->write(__METHOD__.
': Bind anonymous');
508 $user = $this->settings->getRoleBindDN();
509 $pass = $this->settings->getRoleBindPassword();
513 $user = $this->settings->getBindUser();
514 $pass = $this->settings->getBindPassword();
517 define(
'IL_LDAP_REBIND_USER',
$user);
518 define(
'IL_LDAP_REBIND_PASS',
$pass);
522 if(!@ldap_bind($this->lh,$a_user_dn,$a_password))
538 $this->log->write(__METHOD__.
': Bind successful.');
551 include_once(
'Services/LDAP/classes/class.ilLDAPRoleAssignments.php');
553 $this->user_fields = array_merge(
554 array($this->settings->getUserAttribute()),
556 $this->mapping->getFields(),
572 @ldap_unbind($this->lh);
588 @ldap_unbind($this->lh);
597 $ilLog->write(
'LDAP: Called referralRebind.');
599 ldap_set_option($a_ds, LDAP_OPT_PROTOCOL_VERSION, 3);
601 if (!ldap_bind($a_ds,IL_LDAP_REBIND_USER,IL_LDAP_REBIND_PASS))
603 $ilLog->write(
'LDAP: Rebind failed');